#!/usr/bin/env bash # Build with Clang source-based coverage and require 100% line and branch # coverage of the summarised implementation (`src/fast.cpp`). `sweep_test` # deterministically enumerates a finite class of small readable instances 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 sweep_test profdir="$build/prof" rm -rf "$profdir" mkdir -p "$profdir" cd "$here" LLVM_PROFILE_FILE="$profdir/sweep-%p.profraw" "$build/sweep_test" "$profdata" merge -sparse "$profdir"/*.profraw -o "$profdir/cov.profdata" report() { "$cov" report "$build/sweep_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"