Skip to content

Generating coverage reports for CXX unit tests

Benjamin Bannier edited this page Jul 1, 2020 · 2 revisions

This document describes how to collect coverage information for C++ unit tests with an LLVM stack.

Sources need to be compiled with -fprofile-instr-generate -fcoverage-mapping added to CMake's CMAKE_CXX_FLAGS.

# Execute unit tests and collect coverage information tagged by PID.
LLVM_PROFILE_FILE='/tmp/prof/default.profraw.%p' ninja check

# Combine coverage output into a single file.
llvm-profdata merge -sparse /tmp/prof/default.profraw.* -o default.profdata

# Export coverage information for each executable in lcov format, e.g., for `hilti-rt-tests`:
llvm-cov export \
    ./bin/hilti-rt-tests \
    --instr-profile=default.profdata \
    --ignore-filename-regex='.*\/3rdparty\/.*' \
    --format=lcov > hilti-rt-tests.lcov

# Generate a HTML report.
genhtml hilti-rt-tests.lcov

This process currently does not work for running JIT'ed code as HLTO files are not compiled with above coverage compiler flags.