1

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.

  • (1) Code in header files may be inlined and therefore this is no code to execute that would generate a coverage report. And (2) _"...so when I compile test1.h..."_ why are you compiling header files ? – Richard Critten Oct 16 '21 at 12:48
  • for your (2) point, I have edited the question, it is `test1.cpp` – Vaibhav Thakkar Oct 16 '21 at 12:56

1 Answers1

1

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.

amon
  • 57,091
  • 2
  • 89
  • 149