How is lines_to_cover calculated when no coverage report is provided

Hi,

Just curious about this metric for C++ projects:

When a project is analyzed with no test coverage report whatsoever, how is lines_to_cover derived? If it’s based on a lexicographical scan of source code, what does it take for a line to be considered executable thus cover-able (versus lines like function/variable declarations and such)?

Thanks.

(SonarQube 8.2 w/ cfamily plugin)

Hi @0xfff0,

they correspond to the executable lines of code (see https://docs.sonarqube.org/latest/user-guide/metric-definitions/). In particular, in cfamily analyzer we look at lines containing statements.

Thank you for the quick reply. Is it correct to claim that it’s always better to instrument builds & run sonar scanner with coverage reports? I imagine a lexer would miscount on occasions (e.g. unreachable code).

Hi @0xfff0,

we are counting statements after parsing, not at lexer level.

Well, unreachable code is executable code which cannot be covered by tests, which is exactly the point of code coverage, to show parts of code which have not been touched by tests.

If you want to import code coverage into SonarQube then you should do it all the time.

Sorry I wasn’t clear, let me clarify - I’m curious about code coverage on two types of projects we have:

  1. Projects that have no tests whatsoever; FWIW in this case we’d run sonar-scanner with -Dsonar.coverageReportPaths=
  2. Projects that only part of the code base is instrumented and unit tested. For example say at project root we have tools/, src/, and test/ directories. All the tests are for code under src/, meanwhile all code under tools/ is neither instrumented nor tested. Here we’ll pass the partial coverage report to sonar-scanner with -Dsonar.coverageReportPaths=<Generic Test Data XML>

What I noticed is that for 1), SonarQube will say there are x number of lines to cover and 0% of coverage. However for 2), all the files under tools/ would show - as all of Coverage, Uncovered Lines, and Uncovered Conditions.

This seems to me (and as you confirmed) that SonarQube has a way to determine executable lines of code at analysis time. So my question is how do I get the 0% coverage to show up for the second senario, for code under tools/?