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
33345 public init(state: LineState, count: UInt64?, missedSpans: [ColumnSpan] = []) {
34345 self.state = state
35345 self.count = count
36345 self.missedSpans = missedSpans
37345 }
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.
4339 public static func lines(from segments: [CoverageSegment], lineCount: Int) -> [LineCoverage] {
4439 var lines: [LineCoverage] = []
4539 lines.reserveCapacity(max(lineCount, 0))
4639 var next = segments.startIndex
4739 // The last segment of the nearest earlier line that has any: its region is
4839 // still open at the start of this line.
4939 var wrapped: CoverageSegment?
50302 for line in stride(from: 1, through: lineCount, by: 1) {
51302 let first = next
52468 while next < segments.endIndex, segments[next].line == line {
53166 next += 1
54302 }
55302 let onLine = segments[first..<next]
56302 lines.append(LineCoverage(segments: onLine, wrapped: wrapped))
57302 wrapped = onLine.last ?? wrapped
58302 }
5939 return lines
6039 }
61
62 /// Mirrors `LineCoverageStats` in LLVM's CoverageMapping.cpp, including the rule
63 /// that any counted region entry on the line makes it code.
64302 private init(segments onLine: ArraySlice<CoverageSegment>, wrapped: CoverageSegment?) {
65302 let regionStarts = onLine.filter { !$0.isGapRegion && $0.hasCount && $0.isRegionEntry }
66302 let startsSkippedCode = onLine.first.map { !$0.hasCount && $0.isRegionEntry } ?? false
67302 let isCode =
68302 (!startsSkippedCode && (wrapped?.hasCount == true || !regionStarts.isEmpty))
69302 || onLine.contains { $0.isRegionEntry && $0.hasCount }
70302 guard isCode else {
71106 self.init(state: .notCode, count: nil)
72106 return
73196 }
74196 let count = max(wrapped?.count ?? 0, regionStarts.map(\.count).max() ?? 0)
75196 guard count > 0 else {
7619 self.init(state: .missed, count: 0)
7719 return
78177 }
79177 let missedSpans = Self.missedSpans(segments: onLine, wrapped: wrapped)
80177 self.init(
81177 state: missedSpans.isEmpty ? .covered : .partlyCovered, count: count,
82177 missedSpans: missedSpans)
83177 }
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?
89177 ) -> [ColumnSpan] {
90177 var spans: [ColumnSpan] = []
91177 var active = wrapped
92177 var activeStart = 1
93318 func close(at end: Int?) {
94318 guard let region = active, region.hasCount, region.count == 0, !region.isGapRegion,
95318 activeStart != end
96318 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 }
103177 for segment in onLine {
104142 close(at: segment.column)
105142 active = segment
106142 activeStart = segment.column
107177 }
108177 close(at: nil)
109177 return spans
110177 }
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
118458 public init(count: Int, covered: Int) {
119458 self.count = count
120458 self.covered = covered
121458 }
122
12378 public init(lines: [LineCoverage]) {
12478 self.init(
125253 count: lines.count { $0.state != .notCode },
126253 covered: lines.count { $0.state == .covered || $0.state == .partlyCovered })
12778 }
128}