How do I tell gcov to ignore un-hittable lines of C++ code?

jchl picture jchl · Aug 24, 2010 · Viewed 14.5k times · Source

I'm using gcov to measure coverage in my C++ code. I'd like to get to 100% coverage, but am hampered by the fact that there are some lines of code that are theoretically un-hittable (methods that are required to be implemented but which are never called, default branches of switch statements, etc.). Each of these branches contains an assert( false ); statement, but gcov still marks them as un-hit.

I'd like to be able to tell gcov to ignore these branches. Is there any way to give gcov that information -- by annotating the source code, or by any other mechanism?

Answer

Torsten Scheck picture Torsten Scheck · Jan 20, 2011

Please use lcov. It hides gcov's complexity, produces nice output, allows detailed output per test, features easy file filtering and - ta-taa - line markers for already reviewed lines:

From geninfo(1):

The following markers are recognized by geninfo:

  • LCOV_EXCL_LINE
    • Lines containing this marker will be excluded.
  • LCOV_EXCL_START
    • Marks the beginning of an excluded section. The current line is part of this section.
  • LCOV_EXCL_STOP
    • Marks the end of an excluded section. The current line not part of this section.