Skip to content

Clang code coverage

Vasil Dimov edited this page Jun 20, 2024 · 1 revision
# Compile normal debug build.
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Debug \
    -DAPPEND_CXXFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
    -DAPPEND_LDFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
    -B /tmp/build/debug
cmake --build /tmp/build/debug -j32

# Compile fuzz build.
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Debug \
    -DAPPEND_CXXFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
    -DAPPEND_LDFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
    -DENABLE_FUZZ=ON -DSANITIZERS=fuzzer,integer,undefined \
    -B /tmp/build/fuzz
cmake --build /tmp/build/fuzz -j32

export LLVM_PROFILE_FILE="/tmp/build/coverage/%m_%p.profraw"

# Run unit tests.
cd /tmp/build/debug
ctest

# Run functional tests.
test/functional/test_runner.py

# Run fuzz tests.
cd /tmp/build/fuzz
test/fuzz/test_runner.py .../bitcoin-core/qa-assets/fuzz_seed_corpus

llvm-profdata merge /tmp/build/coverage/*.profraw -output=/tmp/build/coverage/all.profdata

llvm-cov show \
    -instr-profile=/tmp/build/coverage/all.profdata \
    -object=/tmp/build/debug/src/test/test_bitcoin \
    -object=/tmp/build/debug/src/bitcoind \
    -object=/tmp/build/fuzz/src/test/fuzz/fuzz \
    -Xdemangler=llvm-cxxfilt \
    -ignore-filename-regex="src/crc32c/|src/leveldb/|src/minisketch/|src/secp256k1/|src/test/" \
    -format=html \
    -output-dir=/tmp/build/coverage/result

# enjoy /tmp/build/coverage/result/index.html
Clone this wiki locally