Files
conflict-range/cpp/run-coverage.sh
T
andrew 273ad72f0c Polish release docs and add CI checks
- Correct stale references and headline-path wording in READMEs and Lean docstrings
- Document build steps, comparator requirements, sweep scope, and coverage
- Unify Apache headers across Lean and C++ sources
- Add a bwrap-free axiom audit for the headline theorems
- Add a Gitea workflow running lake build, the axiom audit, C++ tests, and coverage
- Match LLVM coverage tool versions to the compiler and recreate stale build dirs
2026-11-17 21:29:16 -05:00

104 lines
3.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build with Clang source-based coverage and require 100% line and branch
# coverage of the summarised implementation (`src/fast.cpp`). `exhaustive_test`
# enumerates small readable instances deterministically and is sufficient on its
# own.
set -euo pipefail
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
build="${1:-$here/build-coverage}"
cxx="${CXX:-clang++-21}"
# Prefer LLVM tools whose version matches the compiler (for example
# `clang++-21` with `llvm-profdata-21`/`llvm-cov-21`). Do not guess among
# versioned tools: mixing profile formats fails in confusing ways.
if [[ "$(basename "$cxx")" =~ -([0-9]+)$ ]]; then
cxx_version="${BASH_REMATCH[1]}"
else
cxx_version=""
fi
cxx_major=""
if [[ -z "$cxx_version" ]]; then
cxx_major="$("$cxx" -dumpversion 2>/dev/null | cut -d. -f1 || true)"
fi
pick_tool() {
local tool="$1" override="$2"
if [[ -n "$override" ]]; then
printf '%s\n' "$override"
return 0
fi
if [[ -n "$cxx_version" ]]; then
if command -v "${tool}-${cxx_version}" >/dev/null 2>&1; then
command -v "${tool}-${cxx_version}"
return 0
fi
return 1
fi
if command -v "$tool" >/dev/null 2>&1; then
command -v "$tool"
return 0
fi
if [[ -n "$cxx_major" ]] && command -v "${tool}-${cxx_major}" >/dev/null 2>&1; then
command -v "${tool}-${cxx_major}"
return 0
fi
return 1
}
profdata="$(pick_tool llvm-profdata "${LLVM_PROFDATA:-}")" || true
cov="$(pick_tool llvm-cov "${LLVM_COV:-}")" || true
if [[ -z "$profdata" || -z "$cov" ]]; then
echo "llvm-profdata and llvm-cov are required" >&2
echo "install the LLVM tools matching $cxx, or set LLVM_PROFDATA and LLVM_COV" >&2
exit 2
fi
# Reusing a coverage build directory after changing the compiler makes CMake
# discard its cache, which also drops the instrumentation flags. Recreate the
# directory instead of silently producing an uninstrumented binary.
cxx_path="$(command -v "$cxx" 2>/dev/null || printf '%s' "$cxx")"
cache="$build/CMakeCache.txt"
if [[ -f "$cache" ]]; then
cached_cxx="$(sed -n 's/^CMAKE_CXX_COMPILER:.*=//p' "$cache" | head -n 1)"
if [[ -n "$cached_cxx" && "$cached_cxx" != "$cxx" && "$cached_cxx" != "$cxx_path" ]]; then
echo "coverage build used $cached_cxx; recreating it for $cxx" >&2
rm -rf "$build"
fi
fi
cmake -S "$here" -B "$build" -G Ninja \
-DCMAKE_CXX_COMPILER="$cxx" \
-DCMAKE_BUILD_TYPE=Debug \
-DCR_BUILD_FUZZERS=OFF \
-DCR_BUILD_BENCH=OFF \
-DCMAKE_CXX_FLAGS="-fprofile-instr-generate -fcoverage-mapping" \
-DCMAKE_EXE_LINKER_FLAGS="-fprofile-instr-generate"
cmake --build "$build" --target exhaustive_test
profdir="$build/prof"
rm -rf "$profdir"
mkdir -p "$profdir"
cd "$here"
LLVM_PROFILE_FILE="$profdir/exhaustive-%p.profraw" "$build/exhaustive_test"
"$profdata" merge -sparse "$profdir"/*.profraw -o "$profdir/cov.profdata"
report() {
"$cov" report "$build/exhaustive_test" \
-instr-profile="$profdir/cov.profdata" src/fast.cpp
}
output="$(report)"
printf '%s\n' "$output"
line_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $10 }')"
branch_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $13 }')"
if [[ "$line_cover" != "100.00%" ]]; then
echo "fast.cpp line coverage is $line_cover, expected 100.00%" >&2
exit 1
fi
if [[ "$branch_cover" != "100.00%" ]]; then
echo "fast.cpp branch coverage is $branch_cover, expected 100.00%" >&2
exit 1
fi
echo "fast.cpp: 100.00% line and branch coverage"