HOWTO: Generating a branch coverage report

It is possible to generate the coverage data without waiting for #38608 or building a custom rustc, by linking to a pre-built profiler library. It even works with 1.17.0 stable. I've checked on both macOS and Debian.

The problem of skipping the proper fix #38608, AFAIK, are:

  • There might be incompatibility problem due to compiler_rt version difference
  • The coverage metadata is not stored into the debug info.

Instructions

  1. Install the necessary library

    • macOS: install Xcode command line tools.
    • Debian/Ubuntu: apt-get install libclang-common-3.8-dev (or apt-get install clang to pull it in via dependency) (higher LLVM version should also work)
  2. Clean up.

    cargo clean && rm -f *.gcda *.gcno
    
  3. Build the test. Only the last 2 lines (-L… -l…) are added to OP's instruction.

    # macOS:
    cargo rustc -- --test \
                   -Ccodegen-units=1 \
                   -Clink-dead-code \
                   -Cpasses=insert-gcov-profiling \
                   -Zno-landing-pads \
                   -L/Library/Developer/CommandLineTools/usr/lib/clang/8.1.0/lib/darwin/ \
                   -lclang_rt.profile_osx
    # Debian 9.0
    cargo rustc -- --test \
                   -Ccodegen-units=1 \
                   -Clink-dead-code \
                   -Cpasses=insert-gcov-profiling \
                   -Zno-landing-pads \
                   -L/usr/lib/llvm-3.8/lib/clang/3.8.1/lib/linux/ \
                   -lclang_rt.profile-x86_64
    
  4. Verify a .gcno file is generated.

    ls *.gcno
    
  5. Run the test

    ./target/debug/deps/«name-of-the-executable»
    
  6. Verify a .gcda file is generated

    ls *.gcda
    
  7. Continue from OP's "Creating a coverage report" step.

3 Likes