#pragma GCC suppress_coverage begin ¶#pragma GCC suppress_coverage endThe lines of code between the begin and end will not be
considered towards any coverage. This is independent of the code
structure, and end may appear in a different scope than the
corresponding begin.
This pragma is useful to suppress coverage for code that is unreachable yet counts towards coverage and pollutes the report, which is a common problem with defensive code or when asserting invariants and properties. This attribute does nothing unless coverage is enabled, e.g. with --coverage or -fpath-coverage.
A end without a begin, or a begin when another is
already in effect, will generate a warning. A begin without a
matching end will suppress the rest of the file.
#pragma GCC suppress_coverage begin
if (cond)
{
/* Coverage is disbled for all statements in this block. */
foo ();
}
else
{
#pragma GCC suppress_coverage end
/* Coverage is restored. */
bar ();
}
/* Counts towards coverage. */
int a = 42;
#pragma GCC suppress_coverage begin
{
/* Does not count towards coverage. */
bar (&a);
...
}
Asserts introduce a branch, and the goal of asserting invariants is that the assert should not trigger, which would leave the assertion-failure branch uncovered. These branches should not be taken (or cannot, in some cases), and would make full coverage impossible, yet clearly contribute to correctness. By suppressing coverage for asserts we can fix this problem.
#define REQUIRE(pred, msg) do { \
_Pragma ("GCC suppress_coverage begin") \
assert (((void)msg, pred)); \
_Pragma ("GCC suppress_coverage end") \
} while (0)
double div (double x, double y)
{
/* gcov reports that this function has no branches. */
REQUIRE (y != 0, "division by zero");
return x / y;
}
‘pragma GCC suppress_coverage’ can suppress expressions like the
condition in if statements:
#pragma GCC suppress_coverage begin
if (x < y) // Does not count towards coverage.
#pragma GCC suppress_coverage end
{
/* Counts towards coverage. */
foo ();
}