Search code examples
c++clangcode-coveragegcovlcov

lcov marking lines with function declarations as reachable but not covered


I'm trying to use lcov (v1.13, on OS X, with clang as compiler) to generate code coverage for my test suite and I've hit one annoying problem that I don't know how to solve. There's a few similar questions on SO, but I couldn't find the solution to this one. For some reason, function/member declarations are marked as reachable but not executed, kind of like in the example below (this is inline method definition in a header):

enter image description here

This renders line coverage metrics useless, so I was hoping there's a way to fix it without marking each declaration as LCOV_EXCL_LINE.

Compiler flags used are pretty standard:

-g -O0 -fno-inline -ftest-coverage -fprofile-arcs -fno-elide-constructors

What's strange is that method definitions in source files are also marked as red, although the bodies are not, e.g.:

// header.h
class Foo { 
    void bar();      // ignored, marked as unreachable
}  

// header.cpp
void Foo::bar() {    // marked as red (reachable / not executed)
    do_something();  // marked as covered
}

If it's of any importance, the source files are part of a static library that's statically linked to the test harness in CMake.


Solution

  • Answering my own question:

    Apparently, lcov -i (initial capture) assumes that starting lines of functions are instrumented, whereas with LLVM they are actually not (whereas with GCC where they are). There is an upstream GitHub issue (linux-test-project/lcov#30) documenting this in more detail.

    Until this is fixed upstream in lcov, I've posted a simple workaround -- a Python script that removes function starting lines from base coverage file, which should "fix" it, at least temporarily.