Compute branch reachability from the control-flow graph (supersedes #2047)
TL;DR: The original check in #2047 marked reachable code as unreachable. This PR fixes the check, carries the IsReachable flag into reports, and adds regression tests. It changes 10 source files in coverlet.core, 7 test files and the changelog. Coverage figures are unchanged on the guard-clause sample from my review.
This PR supersedes #2047, as @Bertk asked in https://github.com/coverlet-coverage/coverlet/pull/2047#issuecomment-6263178125. It keeps Bertk's IsReachable property on BranchPoint and BranchInfo, with a default of true. It adds the changes from my review of #2047 and regression tests. Refs #2036, but this PR is unlikely to change the partial coverage reported there (see "What to expect in reports").
What changes
- Analysis.
CecilSymbolHelper.ComputeReachableInstructionswalks each method's control-flow graph once, from the method entry. The walk follows branch targets, stops at return, throw andjmp, and enters the filter and handler of each exception handler whose try entry is reached. Every path of a branch gets the reachability of its branch instruction. This replaces the first #2047 version's check of the instructions laid out before each path. That check marked the normal path of a guard clause (if (s == null) throw ...;) as unreachable. - Pass-through.
BranchinInstrumenterResult.csgets[DataMember] public bool IsReachable = true;. Both places inInstrumenterthat create aBranchcopy the flag, and so do the fourBranchInfoinitializers inCoverage. - State files.
DataContractSerializerdoes not run field initializers. Without help, a state file written before this change has noIsReachableelement, and every branch reads back as unreachable.Branchtherefore sets the field totruein an[OnDeserializing]callback. Incoverlet.msbuild.tasks.tests,Execute_StateUnderTest_WithInstrumentationState_ThresholdTypereads such a file fromTestAssets. It fails without the callback, including at #2047's current head, c415187. - Reports.
CoverageSummary.CalculateBranchCoverageand the Cobertura<conditions>count reachable branches only, as in #2047. LCOV branch records and OpenCover branch points also exclude unreachable branches so that report details agree with their totals. A Cobertura line whose branches are all unreachable is no longer markedbranch="True"; before, it showed100% (0/0)with empty conditions. The JSON report keeps every branch and carries the flag. TeamCity output already used the filtered totals. Documentation/Changelog.mdhas an entry under Unreleased.
How this relates to the commits on #2047
Bertk's c415187 already adopts the graph walk and the pass-through. When I use c415187's CecilSymbolHelper.cs in this PR, all my tests pass except the new jmp test. Differences from c415187:
- the
[OnDeserializing]callback above, thejmpcase, and the LCOV, OpenCover and Cobertura line changes; CoverageResult.Mergestill matches branches as it did before. c415187 addsIsReachableto the match. When the flag is the same on both sides, the extra condition changes nothing. When the flags disagree, c415187 keeps two entries for one branch. That can happen with aMergeWithreport written before this change: such a report has noIsReachablefield, so its branches read back as reachable;- the tests (below) replace the synthetic-method tests in
BranchPointReachabilityTests.cs. From #2047 I kept, with small changes, theCoverageSummaryand Cobertura filter tests; - no unrelated reformatting, and
BuildPointsForBranchandBuildPointsForSwitchCaseskeep their signatures. Path 1 and the switch cases copy the flag from path 0.
Tests
The C# samples in the new BranchPointReachabilityTests are compiled at test time with Roslyn, in Debug and in Release. The branch points therefore come from real IL with real offsets and sequence points. Every test that reads branch points asserts the exact list of paths and flags, with no Count >= 2 guards. The tests cover:
- a guard clause with
throw,if (x) return, a plainif/elseand aswitch, each in Debug and Release; - a branch inside a
catchblock, and one inside acatchnested in anothercatch; - methods built with Cecil and read back from a
MemoryStream: a reachable branch, a branch and aswitchthat nothing reaches, and a branch afterjmp; - an end-to-end test in
coverlet.core.coverage.tests: it compiles a module with a PDB, appends a branch that nothing reaches, runsCoverage.PrepareModulesandGetCoverageResult, and checksIsReachableon both theBranchand theBranchInfoobjects; - the four
BranchInfoinitializers inCoverage; CoveragePrepareResultserialization, with and without theIsReachableelement;- LCOV, OpenCover and Cobertura output for a method with an unreachable branch that has hits;
- Cobertura output for a line whose branches are all unreachable.
The end-to-end test writes a compiled module and its PDB to a temporary directory. .github/copilot-instructions.md asks unit tests not to use the file system, so the second commit moves this test from coverlet.core.tests to coverlet.core.coverage.tests, as the Copilot review suggested. It does not run the instrumented code, so it runs in the test process.
Results on Linux with SDK 10.0.402, Debug:
| master b5ba919 | this PR | |
|---|---|---|
coverlet.core.tests net10.0 | 337 passed, 13 skipped, 0 failed | 361 passed, 13 skipped, 0 failed |
coverlet.core.tests net8.0 | 337 passed, 13 skipped, 0 failed | 361 passed, 13 skipped, 0 failed |
coverlet.core.coverage.tests net10.0 and net8.0, the end-to-end test only | passed | |
coverlet.collector.tests net10.0 and net8.0 | 48 passed per target | 48 passed per target |
coverlet.msbuild.tasks.tests net10.0 | 14 passed | 14 passed |
coverlet.msbuild.tasks.tests on net8.0 fails all 14 tests on master and on this PR alike, because MSBuildLocator finds no .NET 8 SDK on my machine.
To check that the tests can fail, I broke the code one way at a time and, on net10.0, ran the whole coverlet.core.tests suite and the end-to-end test. Each of these changes turns at least one new test red:
| Change to the code | New tests that fail |
|---|---|
| analysis always returns reachable | 3 (dead code, jmp, end-to-end) |
| analysis always returns unreachable | 14 |
| skip exception handlers in the walk | 4 (both catch tests, Debug and Release) |
| walk exception handlers only twice, without repeating to a fixed point | 2 (nested catch) |
fall through after jmp | 1 |
| first #2047 look-back instead of the graph walk | 7 |
drop the copy in either Instrumenter site | 1 each (end-to-end) |
drop the copy in any one of the four Coverage sites | 1 or 2 each |
| path 1 does not copy the flag from path 0 | 3 |
| switch cases do not copy the flag from path 0 | 1 (dead code) |
remove [DataMember] from Branch.IsReachable | 2 (serialization) |
remove the [OnDeserializing] callback | 1 (state file without the field) |
CoverageSummary counts unreachable branches | 5 |
Cobertura <conditions> include unreachable branches | 1 |
| Cobertura marks a line with only unreachable branches as a branch line | 1 |
LCOV writes BRDA records for unreachable branches | 1 |
| OpenCover writes branch points for unreachable branches | 1 |
Three changes leave every test green: removing the filter entry from the walk, setting the default of Branch.IsReachable to false, and setting the default of BranchPoint.IsReachable to false. In the current production paths, branches inside filters are skipped and handlers are entered independently. Every production site that constructs a Branch or BranchPoint assigns the flag explicitly. The Branch deserialization callback also supplies the missing-field default. These mutations caused no failures in the exercised suite.
What to expect in reports
In the compiled C# assemblies surveyed here, no branch point was unreachable under the graph walk. With this PR, GetBranchPoints marks every branch point in this PR's coverlet.core.dll as reachable: 2,358 in Debug and 2,348 in Release. With the original look-back check from #2047, the same survey marks 52 branch points unreachable in Debug and 368 in Release. On the small guard-clause sample from my review, the Cobertura report from coverlet.console is identical to master's apart from its timestamp, and the JSON report gains "IsReachable": true on each branch. So this PR is unlikely to change the partial coverage reported in #2036. As I wrote in the review, that issue looks like a hit-counting problem, and I have not run the #2036 project.
Not tested
- Windows and macOS, .NET Framework targets, and the
LEGACYbuild. coverlet.integration.tests, the MTP test projects, and the rest ofcoverlet.core.coverage.tests.- The #2036 project.