Commit 7e76413
Port LLVM's real coverage-segment semantics; complete-or-nil coverage evidence (#29)
* Port LLVM's real coverage-segment semantics; complete-or-nil coverage evidence
SourceCoverageReader.executedLines tracked a single "currently open
region," closing it at the next segment's line -- a hand-rolled
approximation, not LLVM's own algorithm. #27 (merged) fixed one real
symptom of this (a same-line region-closing drop). Investigating a
planned test strengthening for that fix (using Fixtures/SwiftPackageMacOS's
Pricing.swift line 26 as a single-test-exclusive attribution marker) found
a second, deeper gap: line 26 was dropped entirely, not by #27's same-line
shape, but because the single-open-region model has no way to represent
"control re-enters an enclosing region's count after a nested region
closes via a non-entry segment." Confirmed against real, live-captured
coverage segments (`--enable-code-coverage`, raw codecov/Pricing.json),
not assumed.
## A direct LLVM LineCoverageStats port
Replaces the whole algorithm with a Swift port of LLVM's own
LineCoverageIterator/LineCoverageStats
(llvm/lib/ProfileData/Coverage/CoverageMapping.cpp, fetched and quoted
verbatim). Per source line, carries forward the last region-entry segment
(`wrapped`) across lines with no entry segment of their own;
mapped/executionCount use the same variable names and control flow as
upstream, so a future LLVM change is a mechanical diff to re-apply, not a
re-derivation. Deliberately not a region stack -- LLVM's segments are
per-position coverage state, not push/pop events.
Two verified, understood behavior changes, both in the same (safe)
direction -- previously under-reporting coverage, a false-noCoverage risk
since MutationRunner's baseline fast path skips building/testing a mutant
entirely off CoverageMap.isKnownUncovered:
- A region's own closing boundary line is now correctly reported covered
(previously excluded).
- A line whose own fresh region entry has count 0 can still be covered
when an enclosing carried-forward count is nonzero.
## Complete-or-nil coverage evidence
parse/read were partial-evidence fail-open: a malformed segment in one
file, or a corrupted/unreadable coverage JSON among several, silently
dropped only that one file's contribution and still returned a map built
from whatever else succeeded -- the same partial-map failure class #26
closed for per-test attribution, here at the baseline-coverage level. A
covered line silently missing from the map reads as "known uncovered,"
not "unknown," which can fast-path that line's mutant straight to
noCoverage.
executedLines now returns nil (distinct from a legitimately-empty []) on
a malformed segment. An internal ParseOutcome (.malformed / .parsed([...]))
keeps genuine malformation distinguishable from a validly-parsed file
that legitimately covers nothing (an untested module/target), so a
directory with one real export and one legitimately-empty one still
returns the real coverage, never nil. The public parse(_:projectRoot:)
API is behavior-unchanged.
Extended, after further review against LLVM's own exporter
(llvm/tools/llvm-cov/CoverageExporterJson.cpp), to fail closed on every
structural element a full/detailed export always emits but a given
document does not: a top-level `type` other than
"llvm.coverage.json.export"; a `filename` missing or wrong-typed for any
file entry; a `segments` key missing or wrong-typed for a file entry
inside projectRoot (a full export always includes this key for every
in-scope file, even as [] -- its absence is the shape --summary-only
output takes, meaning detailed coverage was never captured, not that the
file has zero coverage); a module entry missing its own `files` key; a
segment with an out-of-range line/column/count, or a (line, column) pair
that regresses backwards across the segment list (LLVM's own exporter
always emits segments in ascending order, and executedLines's own
per-line grouping assumes it). A structurally-empty `segments` array, a
file entry with no `segments` key when outside projectRoot, or a file
outside projectRoot entirely remain legitimate exclusions, still skipped
rather than failing the whole read.
## Tests
- SourceCoverageReaderExecutedLinesTests.realBulkDiscountRateSegmentsCoverLine26:
the exact raw segments captured live from a real
`swift test --enable-code-coverage --filter bulkDiscountRoughly` run --
not a hand-simplified stand-in.
- Existing synthetic unit tests updated to match the corrected semantics,
with worked-out reasoning for each change; zeroCountSegmentsAreNotExecuted
(genuinely all-zero, no preceding positive context) is unaffected and
still confirms the true-negative case.
- readSurvivesALegitimatelyEmptyFile / readFailsWholeOnModuleMissingFilesArray
/ fileWithoutFilenameFailsClosed / fileWithoutSegmentsKeyFailsClosed
(+ its own read(directory:) counterpart) / wrongTypedColumnFailsClosed /
outOfOrderSegmentsFailClosed: the full set of complete-or-nil regression
tests, each directly exercising the exact gap it closes.
- Upgraded SwiftPackageMacOSSwiftTestingSelectionAcceptanceTests
.bulkDiscountRoughlyRoundTrips (the exact-attribution assertion was
previously deferred -- see #25) to assert exact attribution on line 26
(bulkDiscountRoughly() alone) against the real toolchain -- confirmed
passing.
- Full unit suite: 2029/2029 at initial landing (one unrelated timing
flake, confirmed transient by isolated rerun); targeted
SourceCoverageReader suites: 34/34 after the final round of fixes above.
- SwiftLint clean (split SourceCoverageReaderTests into two suites to stay
under type_body_length after the new tests -- organizational only).
- Three independent-review rounds, all findings fixed in this same
branch, none left for a follow-up: (1) the initial complete-or-nil
implementation conflated malformed with legitimately-empty; (2) a module
missing its own files key fell through the same gap; (3) the same gap
one level down, at filename/segments/column/ordering.
Independent of the other open PRs in this train -- touches only
SourceCoverageReader.swift and its own tests, plus one acceptance-test
assertion upgrade.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PpcUZbmC1oxNLWRr1FjYkm
* Fix vertical whitespace lint violation
* Fix a backwards contract statement in a test's own doc comment
readSurvivesALegitimatelyEmptyFile's comment read "a genuinely malformed
file must not discard the whole directory's coverage" -- the opposite of
what the fix (and this test) actually establishes: a malformed sibling
file must still discard the whole directory's coverage (fail-closed);
only a validly-parsed, legitimately-empty one may not. The test's own
assertions were already correct; only the prose describing them was
backwards.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PpcUZbmC1oxNLWRr1FjYkm
---------
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>1 parent 9a29098 commit 7e76413
3 files changed
Lines changed: 652 additions & 152 deletions
File tree
- Sources/AppleBuildAdapters
- Tests/MutantKitTests
- Acceptance
- Unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
23 | 46 | | |
24 | 47 | | |
25 | 48 | | |
26 | 49 | | |
27 | 50 | | |
28 | 51 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
33 | 60 | | |
34 | 61 | | |
35 | 62 | | |
| |||
39 | 66 | | |
40 | 67 | | |
41 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
42 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
43 | 134 | | |
44 | | - | |
| 135 | + | |
| 136 | + | |
45 | 137 | | |
46 | 138 | | |
47 | 139 | | |
48 | 140 | | |
49 | 141 | | |
50 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
51 | 150 | | |
52 | | - | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
53 | 156 | | |
54 | 157 | | |
55 | 158 | | |
56 | | - | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
57 | 163 | | |
58 | | - | |
| 164 | + | |
59 | 165 | | |
60 | 166 | | |
61 | 167 | | |
62 | 168 | | |
63 | 169 | | |
64 | | - | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
65 | 189 | | |
66 | 190 | | |
67 | 191 | | |
68 | 192 | | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
75 | 207 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
86 | 218 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
107 | 245 | | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
108 | 252 | | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
117 | 267 | | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
118 | 273 | | |
119 | 274 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
124 | 301 | | |
125 | 302 | | |
126 | | - | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
127 | 320 | | |
128 | 321 | | |
129 | 322 | | |
| |||
0 commit comments