Coverage suppressions simplify results analysis and compliance reporting for code bases that include uncoverable code. Uncoverable code can be annotated with appropriate in-code markers that include justification. It is also possible to use regular expression-based suppressions to suppress reporting coverage from repeatable lines of code that follow specific patterns. Code coverage suppressions are typically utilized when dealing with code which is difficult or impossible to cover, such as: 

  • Low-level hardware specialized code
  • Complex error handling code
  • Legacy code

After applying suppressions, the suppressed code elements will be treated as covered. However, for visibility, the number of suppressed code elements will be reported using the `(+<suppressed>)` notation.

Example:

`SC=100% 3(+5)/8` - 3 out of 8 statements were covered, while other 5 statements were suppressed 

The process of applying coverage suppressions: 

  1. Annotate uncoverable code with an appropriate suppression marker. For details see In-code Suppressions.
    Skip this point if your uncoverable code is easier to be suppressed with regular expressions. For details see Regex-based Line Suppression.
  2. Build instrumented code and execute your project.
  3. Generate coverage data files using the cpptestcov compute command.
    Example:
    cpptestcov compute -map=.cpptest/cpptestcc -clog=cpptest_results.clog -out=.coverage
  4. Apply suppressions using the cpptestcov suppress command.
    Example (only in-code suppressions are used):
    cpptestcov suppress .coverage
    Example (regex-based line suppressions used):
    cpptestcov suppress --regex-cov-reason ".*while\(1\).*" DC "ignore decision coverage for while(1)" .coverage 
  5. Generate the report using the cpptestcov report command.
    Example: 
    cpptestcov report text .coverage

In-code Suppressions

Line Suppression

Line suppression allows for ignoring coverable elements which begin in a given line. The suppression comment must be specified at the end of the line of code, using the following syntax:

// parasoft-cov-suppress <coverage-id>|ALL "<suppression comment>"

Examples:

int getValue(int a, int b)
{
    if ((a > 0) &&  // parasoft-cov-suppress MCDC "ingore boolean-decision"
        (b > 0))  // parasoft-cov-suppress SCC "ignore simple condition"
    {
        return a * b; // parasoft-cov-suppress SC DC "ignore statement and branch"
    }

    if (a > 0) { // parasoft-cov-suppress DC "ignore branch-decision"
        return a; // parasoft-cov-suppress "ignore all metrics - ALL is optional"
    } else {
        return b; 
    }

    return 0;
}

Next Line Suppression

Next-line suppression allows for ignoring coverable elements which begin in the next line. The suppression comment must be specified in the preceding line, using the following syntax:

// parasoft-cov-suppress-next-line <coverage-id>|ALL "<suppression comment>"

Examples:

int getValue(int a, int b)
{
    if (a > 0) {
        return a;
    } else {
        return b; 
    }
    // parasoft-cov-suppress-next-line ALL "ignore all coverage metrics in the next line"
    return 0;
}

Block Suppression

Block suppression allows for ignoring coverable elements in a block of code. The suppression begin/end comments must be specified before/after the block of code, using the following syntax:

// parasoft-cov-begin-suppress <coverage-id>|ALL "<suppression comment>"
...code block...
// parasoft-cov-end-suppress

Examples:

// parasoft-cov-begin-suppress ALL "ignore coverage analysis for the function"
int deadCode(int i)
{
    int k = i + 10;
    return getValue(i, k);
}
// parasoft-cov-end-suppress

Regex-based Line Suppression

Regex-based line suppression allows for ignoring coverable elements in a line that matches a regular expression. Regular expressions can be specified with the cpptestcov suppress command:

  • suppressing all coverage metrics in lines matching REGEX with an auto-generated justification (reason):
    cpptestcov suppress --regex REGEX <COV_DATA_DIR>
  • suppressing selected coverage metrics in lines matching REGEX with a specific justification (reason):
    cpptestcov suppress --regex-cov-reason REGEX COVTYPE REASON <COV_DATA_DIR>

Examples: 

cpptestcov suppress --regex ".*DEBUG.*" --regex ".*ASSERT.*" cov-data-run1
cpptestcov suppress --regex-cov-reason ".*while\(1\).*" DC "ignore decision coverage for while(1)" cov-data-run1


  • No labels