Vaibhav Thakkar
Vaibhav Thakkar

Reputation: 69

gcovr not showing code coverage for untouched header files

I have the following structure for a header only library

include
  - header1.h
  - header2.h
  - header3.h
test
  - test1.cpp

Now test1.cpp only uses header1.h, so when I compile test1.cpp with --coverage option, run the test, and then run gcovr to generate the report (with -r ../include from test directory), it only shows me coverage for header1.h.
I want to show coverage of 0% for header2.h and header3.h also, because it represents a more accurate representation of code covered by tests.

Upvotes: 1

Views: 1509

Answers (2)

David
David

Reputation: 3442

The newer version of gcovr (8.2+) now has an --include option to pull in all source files so that you can show them all in a report even if they were never seen by gcov coverage or actively 'tested'.

Upvotes: 0

amon
amon

Reputation: 57600

Coverage data is collected per compilation unit. In your example, you have one compilation unit for the test1.cpp file. The compiler will generate a .gcno file that allows other tools to associate coverage data with lines in the files in this compilation unit. But since only header1.h was included, there will not be any such information in the .gcno file about header2 and header3.

Coverage tools like gcov and gcovr are language-agnostic, and do not search your directories for files that might contain uncovered source code. If you want the coverage report to know about the files, you must include the file in a compilation unit that you compile with --coverage. Even then, you will note that GCC ignores inline functions that were not called. You can only get coverage data when machine code was generated for that code.

This is a fundamental limitation of the gcov coverage mechanism, and means that you have to take care when interpreting coverage metrics for header files. Do not mistake a high coverage percentage to mean that everything that could be covered also was covered – it merely means that the machine code generated for code in that file was thoroughly exercised.

Upvotes: 1

Related Questions