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
33360 public init(state: LineState, count: UInt64?, missedSpans: [ColumnSpan] = []) {
34360 self.state = state
35360 self.count = count
36360 self.missedSpans = missedSpans
37360 }
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.
4353 public static func lines(from segments: [CoverageSegment], lineCount: Int) -> [LineCoverage] {
4453 var lines: [LineCoverage] = []
4553 lines.reserveCapacity(max(lineCount, 0))
4653 var next = segments.startIndex
4753 // The last segment of the nearest earlier line that has any: its region is
4853 // still open at the start of this line.
4953 var wrapped: CoverageSegment?
50317 for line in stride(from: 1, through: lineCount, by: 1) {
51317 let first = next
52495 while next < segments.endIndex, segments[next].line == line {
53178 next += 1
54317 }
55317 let onLine = segments[first..<next]
56317 lines.append(LineCoverage(segments: onLine, wrapped: wrapped))
57317 wrapped = onLine.last ?? wrapped
58317 }
5953 return lines
6053 }
61
62 /// Mirrors `LineCoverageStats` in LLVM's CoverageMapping.cpp, including the rule
63 /// that any counted region entry on the line makes it code.
64317 private init(segments onLine: ArraySlice<CoverageSegment>, wrapped: CoverageSegment?) {
65317 let regionStarts = onLine.filter { !$0.isGapRegion && $0.hasCount && $0.isRegionEntry }
66317 let startsSkippedCode = onLine.first.map { !$0.hasCount && $0.isRegionEntry } ?? false
67317 let isCode =
68317 (!startsSkippedCode && (wrapped?.hasCount == true || !regionStarts.isEmpty))
69317 || onLine.contains { $0.isRegionEntry && $0.hasCount }
70317 guard isCode else {
71106 self.init(state: .notCode, count: nil)
72106 return
73211 }
74211 let count = max(wrapped?.count ?? 0, regionStarts.map(\.count).max() ?? 0)
75211 guard count > 0 else {
7619 self.init(state: .missed, count: 0)
7719 return
78192 }
79192 let missedSpans = Self.missedSpans(segments: onLine, wrapped: wrapped)
80192 self.init(
81192 state: missedSpans.isEmpty ? .covered : .partlyCovered, count: count,
82192 missedSpans: missedSpans)
83192 }
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?
89192 ) -> [ColumnSpan] {
90192 var spans: [ColumnSpan] = []
91192 var active = wrapped
92192 var activeStart = 1
93346 func close(at end: Int?) {
94346 guard let region = active, region.hasCount, region.count == 0, !region.isGapRegion,
95346 activeStart != end
96346 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 }
103192 for segment in onLine {
104154 close(at: segment.column)
105154 active = segment
106154 activeStart = segment.column
107192 }
108192 close(at: nil)
109192 return spans
110192 }
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
1182098 public init(count: Int, covered: Int) {
1192098 self.count = count
1202098 self.covered = covered
1212098 }
122
123108 public init(lines: [LineCoverage]) {
124108 self.init(
125283 count: lines.count { $0.state != .notCode },
126283 covered: lines.count { $0.state == .covered || $0.state == .partlyCovered })
127108 }
128}