Source based code coverage is available since rust version 1.60.
The following RUSTFLAGS should be set before compiling:
Shell:
export RUSTFLAGS="-C instrument-coverage"
Fish:
set -x RUSTFLAGS "-C instrument-coverage"
Before running tests, the LLVM_PROFILE_FILE can be used to set name of the generated profraw files:
Shell:
export LLVM_PROFILE_FILE "test-%p-%m.profraw"
Fish:
set -x LLVM_PROFILE_FILE "test-%p-%m.profraw"
LLVM coverage tools are needed to process coverage data and generate reports.
llvm-profdata is needed to merge all raw profile data files into indexed profile data file:
Shell:
llvm-profdata merge -o a.profdata $(find . -type f -name "*profraw" -exec ls \{\} \;)
Fish:
llvm-profdata merge -o a.profdata (find . -type f -name "*profraw" -exec ls \{\} \;)
llvm-cov is needed to generate reports:
llvm-cov report --instr-profile=a.profdata --ignore-filename-regex=".cargo" --ignore-filename-regex="rustc" -object <BIN/OBJ>[ -object <BIN/OBJ>]...
The llvm-tools you installed may be not compatible with the ones rustc used. You can use llvm-tools via rustup:
rustup component add llvm-tools
cargo install cargo-binutils
To run the bundled tools, just use cargo <cmd> -- <params>
.