Skip to content

Commit cfa7f70

Browse files
committed
Merge bitcoin#31933: doc: Add Clang/LLVM based coverage report generation
b96f1a6 add clang/llvm based coverage report generation (Prabhat Verma) Pull request description: Followed up from the [comment](bitcoin#31927 (comment)) on the issue [bitcoin#31927](bitcoin#31927) , issues have been observed building coverage reports with `gcov` in MacOs and NixOs. This PR adds the steps to generate a coverage report based on the default llvm/clang tooling. ACKs for top commit: Crypt-iQ: tACK b96f1a6 hodlinator: re-ACK b96f1a6 janb84: Re ACK [b96f1a6](bitcoin@b96f1a6) Tree-SHA512: bc54f170e84bb76b3eba7285bd49f051c0b99b784d583a550d8e51511497bcc4df8964bbe3991777648d2f829809db8eabb0cbf0d25f9da5e49e1cfc62f6d8d0
2 parents 772996a + b96f1a6 commit cfa7f70

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

doc/developer-notes.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ $ ./build/test/functional/test_runner.py --valgrind
487487

488488
### Compiling for test coverage
489489

490+
#### Using LCOV
491+
490492
LCOV can be used to generate a test coverage report based upon `ctest`
491493
execution. LCOV must be installed on your system (e.g. the `lcov` package
492494
on Debian/Ubuntu).
@@ -516,6 +518,63 @@ To enable test parallelism:
516518
cmake -DJOBS=$(nproc) -P build/Coverage.cmake
517519
```
518520

521+
#### Using LLVM/Clang toolchain
522+
523+
The following generates a coverage report for unit tests and functional tests.
524+
525+
Configure the build with the following flags:
526+
527+
> Consider building with a clean state using `rm -rf build`
528+
529+
```shell
530+
# MacOS may instead require `-DCMAKE_C_COMPILER="$(brew --prefix llvm)/bin/clang" -DCMAKE_CXX_COMPILER="$(brew --prefix llvm)/bin/clang++"`
531+
cmake -B build -DCMAKE_C_COMPILER="clang" \
532+
-DCMAKE_CXX_COMPILER="clang++" \
533+
-DAPPEND_CFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
534+
-DAPPEND_CXXFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
535+
-DAPPEND_LDFLAGS="-fprofile-instr-generate -fcoverage-mapping"
536+
cmake --build build # Use "-j N" here for N parallel jobs.
537+
```
538+
539+
Generating the raw profile data based on `ctest` and functional tests execution:
540+
541+
```shell
542+
# Create directory for raw profile data
543+
mkdir -p build/raw_profile_data
544+
545+
# Run tests to generate profiles
546+
LLVM_PROFILE_FILE="$(pwd)/build/raw_profile_data/%m_%p.profraw" ctest --test-dir build # Use "-j N" here for N parallel jobs.
547+
LLVM_PROFILE_FILE="$(pwd)/build/raw_profile_data/%m_%p.profraw" build/test/functional/test_runner.py # Use "-j N" here for N parallel jobs
548+
549+
# Merge all the raw profile data into a single file
550+
find build/raw_profile_data -name "*.profraw" | xargs llvm-profdata merge -o build/coverage.profdata
551+
```
552+
553+
> **Note:** The "counter mismatch" warning can be safely ignored, though it can be resolved by updating to Clang 19.
554+
> The warning occurs due to version mismatches but doesn't affect the coverage report generation.
555+
556+
Generating the coverage report:
557+
558+
```shell
559+
llvm-cov show \
560+
--object=build/bin/test_bitcoin \
561+
--object=build/bin/bitcoind \
562+
-Xdemangler=llvm-cxxfilt \
563+
--instr-profile=build/coverage.profdata \
564+
--ignore-filename-regex="src/crc32c/|src/leveldb/|src/minisketch/|src/secp256k1/|src/test/" \
565+
--format=html \
566+
--show-instantiation-summary \
567+
--show-line-counts-or-regions \
568+
--show-expansions \
569+
--output-dir=build/coverage_report \
570+
--project-title="Bitcoin Core Coverage Report"
571+
```
572+
573+
> **Note:** The "functions have mismatched data" warning can be safely ignored, the coverage report will still be generated correctly despite this warning.
574+
> This warning occurs due to profdata mismatch created during the merge process for shared libraries.
575+
576+
The generated coverage report can be accessed at `build/coverage_report/index.html`.
577+
519578
### Performance profiling with perf
520579

521580
Profiling is a good way to get a precise idea of where time is being spent in

0 commit comments

Comments
 (0)