generating branch coverage data for lcov

2019-03-12 13:10发布

i'm trying to use lcov for code coverage metrics, but I cannot get branches coverage to work.

Here's how i'm using it:

g++ -ggdb3 --coverage src/read.c tests/test.cpp -o bin/test 
lcov --zerocounters --directory $PWD 
lcov --capture --initial --directory $PWD --output-file coverage_output  
./bin/test 
lcov --no-checksum --directory $PWD --capture --output-file coverage_output 
genhtml --branch-coverage --highlight --legend --output-directory out coverage_output

but i get:

Overall coverage rate:
lines......: 100.0% (60 of 60 lines)
functions..: 100.0% (26 of 26 functions)
branches...: no data found

any ideas?

4条回答
啃猪蹄的小仙女
2楼-- · 2019-03-12 13:35

Sorry, not so much an "idea" as a confirmation that you're doing everything correctly. Your exact commands worked on this simple code:

#include <iostream>
using namespace std;

bool foo(int i)
{
    if (i != 0) {
        return 12 / i;
    } else {
        return 0;
    }
}

int main(int argc, char** argv)
{
    cout << foo(argc) << endl;
    return 0;
}

The lcov coverage table has statistics for Lines, Functions, and Branches. Maybe double-check that you're actually looking at the correct output HTML?

查看更多
forever°为你锁心
3楼-- · 2019-03-12 13:39

The latest version of LCOV disabled branch coverage by default.

You need to re-enable it by either:

  • editing your .lcovrc file (copied from /etc/lcovrc) to change lcov_branch_coverage setting to 1
  • adding --rc lcov_branch_coverage=1 to your lcov command lines
查看更多
趁早两清
4楼-- · 2019-03-12 13:50

based on this post, the difference may depend on the version of gcc you are using. Can you share which versions you are using. I am not getting branch coverage on:

 i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-03-12 13:55

.lcovrc files is file of settings that need to place in path of lcov file. Frankly, I didn't research much on use of this file.

You need to add additional parameter as "--rc lcov_branch_coverage=1" to lcov for all calls. In your case add this parameter to all your three calls. If you miss one, it will drop branch coverage.

Also --branch-coverage is needed for genhtml.

查看更多
登录 后发表回答