Merge bitcoin/bitcoin#31933: doc: Add Clang/LLVM based coverage report generation

b96f1a696aa792068b5a4fa16e2d4a342e4f55b8 add clang/llvm based coverage report generation (Prabhat Verma)

Pull request description:

  Followed up from the [comment](https://github.com/bitcoin/bitcoin/issues/31927#issuecomment-2674522975) on the issue [#31927](https://github.com/bitcoin/bitcoin/issues/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 b96f1a696aa792068b5a4fa16e2d4a342e4f55b8
  hodlinator:
    re-ACK b96f1a696aa792068b5a4fa16e2d4a342e4f55b8
  janb84:
    Re ACK [b96f1a6](b96f1a696a)

Tree-SHA512: bc54f170e84bb76b3eba7285bd49f051c0b99b784d583a550d8e51511497bcc4df8964bbe3991777648d2f829809db8eabb0cbf0d25f9da5e49e1cfc62f6d8d0
This commit is contained in:
merge-script 2025-04-02 14:15:09 +08:00
commit cfa7f70f6c
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

View File

@ -487,6 +487,8 @@ $ ./build/test/functional/test_runner.py --valgrind
### Compiling for test coverage
#### Using LCOV
LCOV can be used to generate a test coverage report based upon `ctest`
execution. LCOV must be installed on your system (e.g. the `lcov` package
on Debian/Ubuntu).
@ -516,6 +518,63 @@ To enable test parallelism:
cmake -DJOBS=$(nproc) -P build/Coverage.cmake
```
#### Using LLVM/Clang toolchain
The following generates a coverage report for unit tests and functional tests.
Configure the build with the following flags:
> Consider building with a clean state using `rm -rf build`
```shell
# MacOS may instead require `-DCMAKE_C_COMPILER="$(brew --prefix llvm)/bin/clang" -DCMAKE_CXX_COMPILER="$(brew --prefix llvm)/bin/clang++"`
cmake -B build -DCMAKE_C_COMPILER="clang" \
-DCMAKE_CXX_COMPILER="clang++" \
-DAPPEND_CFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
-DAPPEND_CXXFLAGS="-fprofile-instr-generate -fcoverage-mapping" \
-DAPPEND_LDFLAGS="-fprofile-instr-generate -fcoverage-mapping"
cmake --build build # Use "-j N" here for N parallel jobs.
```
Generating the raw profile data based on `ctest` and functional tests execution:
```shell
# Create directory for raw profile data
mkdir -p build/raw_profile_data
# Run tests to generate profiles
LLVM_PROFILE_FILE="$(pwd)/build/raw_profile_data/%m_%p.profraw" ctest --test-dir build # Use "-j N" here for N parallel jobs.
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
# Merge all the raw profile data into a single file
find build/raw_profile_data -name "*.profraw" | xargs llvm-profdata merge -o build/coverage.profdata
```
> **Note:** The "counter mismatch" warning can be safely ignored, though it can be resolved by updating to Clang 19.
> The warning occurs due to version mismatches but doesn't affect the coverage report generation.
Generating the coverage report:
```shell
llvm-cov show \
--object=build/bin/test_bitcoin \
--object=build/bin/bitcoind \
-Xdemangler=llvm-cxxfilt \
--instr-profile=build/coverage.profdata \
--ignore-filename-regex="src/crc32c/|src/leveldb/|src/minisketch/|src/secp256k1/|src/test/" \
--format=html \
--show-instantiation-summary \
--show-line-counts-or-regions \
--show-expansions \
--output-dir=build/coverage_report \
--project-title="Bitcoin Core Coverage Report"
```
> **Note:** The "functions have mismatched data" warning can be safely ignored, the coverage report will still be generated correctly despite this warning.
> This warning occurs due to profdata mismatch created during the merge process for shared libraries.
The generated coverage report can be accessed at `build/coverage_report/index.html`.
### Performance profiling with perf
Profiling is a good way to get a precise idea of where time is being spent in