23

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?

Hugo
  • 2,139
  • 4
  • 22
  • 30

4 Answers4

41

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
Zeta
  • 103,620
  • 13
  • 194
  • 236
Paul Rutland
  • 605
  • 5
  • 4
  • 8
    as Abhay Joshi said below: remember to add the --rc line on every single lcov invocation, otherwise it will drop the branch stats. Also, genhtml option is --rc genhtml_branch_coverage=1 – garph0 Sep 24 '14 at 16:38
  • 2
    Do you, by chance, know the rationale for that change? Intuitively this feels very wrong. – Griwes Aug 26 '16 at 08:17
  • The collection of branch data was disabled in 2012 (i.e. lcov version 1.11). The change notes some possible performance implications when having this feature enabled (https://github.com/linux-test-project/lcov/commit/263de2b40e21193ef8d11e899eb55aa52b17225d). How noticeable those are depends on the size of your program. Usually, this shouldn't be an issue. – maxschlepzig Apr 29 '17 at 18:29
  • 2
    If you like to use `lcov -l file.info` to output an text based summary the `--rc lcov...` parameter does not work. So editing the `.locvrc` file seems to be the btter choice – eDeviser Sep 20 '17 at 07:05
14

.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.

Abhay Joshi
  • 161
  • 1
  • 6
  • Unfortunately, I see `geninfo: Note: --initial does not generate branch coverage data`, so at least one of those won't run with branch coverage. – Evgen May 02 '17 at 03:48
  • 1
    for genhtml I found the parameter is `--rc genhtml_branch_coverage=1` not `--branch-coverage` – Rick Wildes Dec 03 '19 at 18:00
  • @Abhay Joshi: That's the key. The additional parameter has really always to be used for each call of lcov. Even if you just want to remove std include files from your report. – tangoal Mar 15 '20 at 19:21
  • **for all calls** save my ass today! – GeekLee Sep 17 '22 at 06:46
0

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?

Tom Panning
  • 4,613
  • 2
  • 26
  • 47
  • Hi Tom, your example doesn't work for me either. The command output says "branches...: no data found", so I don't think i missing something in the html. But, thanks!, if you can see branch data using my commands maybe is an installation problem or something about the tool.. – Hugo Sep 12 '12 at 12:37
  • You likely had used an old LCOV version (< 1.11) where branch data collection was enabled, by default. – maxschlepzig Apr 29 '17 at 18:32
0

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)
Community
  • 1
  • 1
MikeF
  • 764
  • 9
  • 26
  • This is another issue: The version string seems to implicate that this is some LLVM clang version from Apple that is used instead of GCC. And LLVM is known to generate coverage data in an old format version that is usually incompatible with what LCOV expects. – maxschlepzig Apr 29 '17 at 18:36