c++gcovboost-range

Ignore or exclude code in external libraries with gcov


I am working on a project which uses a couple of boost libraries. When looking at our test reports, we have seen that test coverage information sometimes does fit to our source code. I was able to track it down to boost::range. I think it is because of some static initialization inside the library, although I cannot say anything more specific.

Basically, if you compile the following code with gcc --coverage, run it and print the coverage report with gcov -b, there will be four additional branches and an additional line, which I would like to ignore.

#include <boost/range.hpp>

int foo(int x)
{
    return x+1;
}

int main(int argc, char* argv[])
{
    return foo(argc);
}

The coverage report is: Lines executed:100.00% of 5 Branches executed:100.00% of 4 Taken at least once:50.00% of 4 Calls executed:100.00% of 2 Creating 'test_gcov.cpp.gcov'

I guess that the count of 5 lines comes from the two function signatures, the function bodies and one additional line in the boost::range library. I don't know where exactly, but the generated gcov-file shows that some static initialization is going on, so I guess that is where the branches are located.

I would like to know if there is a way to tell gcov to ignore any code in the boost namespace, or any other means.


Solution

  • You can use the --remove or -r flag to ignore files from external libraries. For example:

    lcov -c -d <build_dir> -o <output_trace_file>
    lcov -r <output_trace_file> "/usr*" -o <output_trace_file>
    

    You can replace "/usr*" with whatever pattern you're trying to remove.

    The blog post here gives a good example of how to use that flag (and covers the whole lcov process, start to finish.)