How to get branch coverage by using 'source-based code coverage'

Hi, Alan

Thanks for making it clear. But I was more confused now :frowning:

I tested on a simple program and used both gcov and lcov to get branch coverage.
The code and build commands as below:

Example simple.cc

#include
// If not comment this line, the branch coverage won’t reach to 100%
// #include
int main(int argc, const char* argv) {
std::string str = “foo”;
str = argv[1];
if (str == “foo” || str == “bar”) {
int a = 5;
a = 7;
} else {
int b = 6;
b = 4;
}
return 0;
}

Coverage build commands

#!/bin/bash
g++ -o simple -fno-exceptions -fno-inline -fprofile-arcs -ftest-coverage simple.cc
./simple “foo”
./simple “bar”
./simple “hello1”
./simple “hello2”
./simple “hello3”
./simple “hello4”
./simple “hello5”
./simple “hello6”
./simple “hello7”
./simple “hello8”
lcov --rc lcov_branch_coverage=1 --capture --directory . --output-file simple.lcov.info
genhtml simple.lcov.info --function-coverage --branch-coverage --output-directory lcov_out/
gcov -b -c simple.gcno

Both gcov and lcov will give a 100% branch coverage report:

cov1.png

My questions:

  • What’s the “official” evaluation formula on branch coverage?
    Sorry I can’t find any compelling documents to describe the accurate algorithm on this issue.
    I just thought gcov/lcov won’t consider how many times the decision evaluated to False.
    Please see the gcov file on my example: simple.cc. There are 6 branches in total which are all hit
    So the result would be 6/6 * 100% = 100%

cov3.png

  • Are there any existing post-processing tools based on clang/llvm source based coverage?

I just saw a clion plugin at github which may generate branch coverage by using region coverage data. But this depends on clion
which is not easy to implement automation test.

  • How to disable STL (or other) noise in gcov branch coverage?
    Sorry, this is not clang/llvm question. But I am still looking forward to some suggestions. For example, if I add iostream header back to

simple.cc, then we won’t get a 100% branch coverage. This may because iostream will introduce some global destruction branch.

Or if it’s impossible, can we think clang region coverage should be a best practice in C++ testing world??

AFAIK, chromium is using region coverage now.

FYI.

Thanks,
Ted Xie

Phipps, Alan <a-phipps@ti.com> 于2020年5月4日周一 上午12:54写道: