1/// What one source line's coverage looks like on a page.
2public enum LineState: Sendable, Equatable {
3 /// No region with a count starts on or runs through the line: blank lines,
4 /// comments, declarations, skipped code.
5 case notCode
6 case covered
7 /// Ran, but part of it never did, such as an `else` on the same line.
8 case partlyCovered
9 case missed
10}
11
12/// A run of columns on one line, 1-based like llvm-cov's.
13public struct ColumnSpan: Sendable, Equatable {
14 public let start: Int
15 /// Exclusive; nil when the span runs to the end of the line.
16 public let end: Int?
17
1823 public init(start: Int, end: Int?) {
1923 self.start = start
2023 self.end = end
2123 }
22}
23
24/// One source line's coverage, worked out from the segments the way llvm-cov's own
25/// `LineCoverageStats` does, so totals match its reports.
26public struct LineCoverage: Sendable, Equatable {
27 public let state: LineState
28 /// The highest execution count of the regions on the line; nil when not code.
29 public let count: UInt64?
30 /// The parts of a partly covered line that never ran; empty for other states.
31 public let missedSpans: [ColumnSpan]
32
33385 public init(state: LineState, count: UInt64?, missedSpans: [ColumnSpan] = []) {
34385 self.state = state
35385 self.count = count
36385 self.missedSpans = missedSpans
37385 }
38
39 /// The coverage of lines `1...lineCount`, in order.
40 ///
41 /// Expects `segments` sorted by line, then column, as llvm-cov exports them.
42 /// Segments past `lineCount` are ignored.
4363 public static func lines(from segments: [CoverageSegment], lineCount: Int) -> [LineCoverage] {
4463 var lines: [LineCoverage] = []
4563 lines.reserveCapacity(max(lineCount, 0))
4663 var next = segments.startIndex
4763 // The last segment of the nearest earlier line that has any: its region is
4863 // still open at the start of this line.
4963 var wrapped: CoverageSegment?
50342 for line in stride(from: 1, through: lineCount, by: 1) {
51342 let first = next
52540 while next < segments.endIndex, segments[next].line == line {
53198 next += 1
54342 }
55342 let onLine = segments[first..<next]
56342 lines.append(LineCoverage(segments: onLine, wrapped: wrapped))
57342 wrapped = onLine.last ?? wrapped
58342 }
5963 return lines
6063 }
61
62 /// Mirrors `LineCoverageStats` in LLVM's CoverageMapping.cpp, including the rule
63 /// that any counted region entry on the line makes it code.
64342 private init(segments onLine: ArraySlice<CoverageSegment>, wrapped: CoverageSegment?) {
65342 let regionStarts = onLine.filter { !$0.isGapRegion && $0.hasCount && $0.isRegionEntry }
66342 let startsSkippedCode = onLine.first.map { !$0.hasCount && $0.isRegionEntry } ?? false
67342 let isCode =
68342 (!startsSkippedCode && (wrapped?.hasCount == true || !regionStarts.isEmpty))
69342 || onLine.contains { $0.isRegionEntry && $0.hasCount }
70342 guard isCode else {
71106 self.init(state: .notCode, count: nil)
72106 return
73236 }
74236 let count = max(wrapped?.count ?? 0, regionStarts.map(\.count).max() ?? 0)
75236 guard count > 0 else {
7619 self.init(state: .missed, count: 0)
7719 return
78217 }
79217 let missedSpans = Self.missedSpans(segments: onLine, wrapped: wrapped)
80217 self.init(
81217 state: missedSpans.isEmpty ? .covered : .partlyCovered, count: count,
82217 missedSpans: missedSpans)
83217 }
84
85 /// The column runs where the active region has a count of zero. Gap regions are
86 /// whitespace between regions, so they never make a span.
87 private static func missedSpans(
88 segments onLine: ArraySlice<CoverageSegment>, wrapped: CoverageSegment?
89217 ) -> [ColumnSpan] {
90217 var spans: [ColumnSpan] = []
91217 var active = wrapped
92217 var activeStart = 1
93391 func close(at end: Int?) {
94391 guard let region = active, region.hasCount, region.count == 0, !region.isGapRegion,
95391 activeStart != end
96391 else { return }
9713 if let last = spans.last, last.end == activeStart {
981 spans[spans.count - 1] = ColumnSpan(start: last.start, end: end)
9912 } else {
10012 spans.append(ColumnSpan(start: activeStart, end: end))
10112 }
10213 }
103217 for segment in onLine {
104174 close(at: segment.column)
105174 active = segment
106174 activeStart = segment.column
107217 }
108217 close(at: nil)
109217 return spans
110217 }
111}
112
113/// How many lines hold code and how many of those ran, each line counted once.
114public struct LineSummary: Sendable, Equatable {
115 public let count: Int
116 public let covered: Int
117
1181944 public init(count: Int, covered: Int) {
1191944 self.count = count
1201944 self.covered = covered
1211944 }
122
123128 public init(lines: [LineCoverage]) {
124128 self.init(
125333 count: lines.count { $0.state != .notCode },
126333 covered: lines.count { $0.state == .covered || $0.state == .partlyCovered })
127128 }
128}