views:

608

answers:

2

I recently started using lcov to visualize my code coverage. It's a great tool.

One thing I'm noticing is that it generates code coverage reports for all the files that I'm using - including those that I'm not interested in. For example, it will give me code coverage reports for boost and mysql++ files.

Is there an easy way to force lcov to only generate coverage reports for specific files?

I have tried using the -k parameter like so:

/usr/bin/lcov -q -c -i -b . -d .obj -k src/ -k include/ -o app_base.info

{run unit tests now}

/usr/bin/lcov -q -c -b . -d .obj -k src/ -k include/ -o app_test.info
/usr/bin/lcov -q -a app_base.info -a app_test.info -o app_total.info
/usr/bin/genhtml -q -o lcov_output_directory app_total.info

(Meaning that I only want coverage files for the "include" and "src" directories.)

However, this doesn't seem to work. The report still shows me all the extraneous files. Any suggestions are very much appreciated. Thanks!

+5  A: 

lcov supports a command line argument --remove to do exactly what you are asking for.

lothar
The `--extract` option may be more useful. Generate the tracefile with everything, and then extract out just the directories you care about into a second tracefile.
Dan
A: 

A possible approach is to constrain which files are compiled with the coverage flags (-fprofile-arcs -ftest-coverage). If you don't want to engineer your make file system to be selective about which files are built with test instrumentation, the following trick might work for you:

  • Build your application without instrumentation.
  • Remove the .o files for source that you want to instrument
  • Turn on instrumentation and rebuild. Only the deleted object files will be rebuilt with instrumentation.
  • Run lcov

This should result in only the targeted areas emitting gcov artifacts, which are blindly consumed by the lcov scripts.

Jeremy Mayhew