| 1 | | /// What one source line's coverage looks like on a page. |
| 2 | | public 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. |
| 13 | | public 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 | | |
| 18 | 23 | public init(start: Int, end: Int?) { |
| 19 | 23 | self.start = start |
| 20 | 23 | self.end = end |
| 21 | 23 | } |
| 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. |
| 26 | | public 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 | | |
| 33 | 360 | public init(state: LineState, count: UInt64?, missedSpans: [ColumnSpan] = []) { |
| 34 | 360 | self.state = state |
| 35 | 360 | self.count = count |
| 36 | 360 | self.missedSpans = missedSpans |
| 37 | 360 | } |
| 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. |
| 43 | 45 | public static func lines(from segments: [CoverageSegment], lineCount: Int) -> [LineCoverage] { |
| 44 | 45 | var lines: [LineCoverage] = [] |
| 45 | 45 | lines.reserveCapacity(max(lineCount, 0)) |
| 46 | 45 | var next = segments.startIndex |
| 47 | 45 | // The last segment of the nearest earlier line that has any: its region is |
| 48 | 45 | // still open at the start of this line. |
| 49 | 45 | var wrapped: CoverageSegment? |
| 50 | 317 | for line in stride(from: 1, through: lineCount, by: 1) { |
| 51 | 317 | let first = next |
| 52 | 495 | while next < segments.endIndex, segments[next].line == line { |
| 53 | 178 | next += 1 |
| 54 | 317 | } |
| 55 | 317 | let onLine = segments[first..<next] |
| 56 | 317 | lines.append(LineCoverage(segments: onLine, wrapped: wrapped)) |
| 57 | 317 | wrapped = onLine.last ?? wrapped |
| 58 | 317 | } |
| 59 | 45 | return lines |
| 60 | 45 | } |
| 61 | | |
| 62 | | /// Mirrors `LineCoverageStats` in LLVM's CoverageMapping.cpp, including the rule |
| 63 | | /// that any counted region entry on the line makes it code. |
| 64 | 317 | private init(segments onLine: ArraySlice<CoverageSegment>, wrapped: CoverageSegment?) { |
| 65 | 317 | let regionStarts = onLine.filter { !$0.isGapRegion && $0.hasCount && $0.isRegionEntry } |
| 66 | 317 | let startsSkippedCode = onLine.first.map { !$0.hasCount && $0.isRegionEntry } ?? false |
| 67 | 317 | let isCode = |
| 68 | 317 | (!startsSkippedCode && (wrapped?.hasCount == true || !regionStarts.isEmpty)) |
| 69 | 317 | || onLine.contains { $0.isRegionEntry && $0.hasCount } |
| 70 | 317 | guard isCode else { |
| 71 | 106 | self.init(state: .notCode, count: nil) |
| 72 | 106 | return |
| 73 | 211 | } |
| 74 | 211 | let count = max(wrapped?.count ?? 0, regionStarts.map(\.count).max() ?? 0) |
| 75 | 211 | guard count > 0 else { |
| 76 | 19 | self.init(state: .missed, count: 0) |
| 77 | 19 | return |
| 78 | 192 | } |
| 79 | 192 | let missedSpans = Self.missedSpans(segments: onLine, wrapped: wrapped) |
| 80 | 192 | self.init( |
| 81 | 192 | state: missedSpans.isEmpty ? .covered : .partlyCovered, count: count, |
| 82 | 192 | missedSpans: missedSpans) |
| 83 | 192 | } |
| 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? |
| 89 | 192 | ) -> [ColumnSpan] { |
| 90 | 192 | var spans: [ColumnSpan] = [] |
| 91 | 192 | var active = wrapped |
| 92 | 192 | var activeStart = 1 |
| 93 | 346 | func close(at end: Int?) { |
| 94 | 346 | guard let region = active, region.hasCount, region.count == 0, !region.isGapRegion, |
| 95 | 346 | activeStart != end |
| 96 | 346 | else { return } |
| 97 | 13 | if let last = spans.last, last.end == activeStart { |
| 98 | 1 | spans[spans.count - 1] = ColumnSpan(start: last.start, end: end) |
| 99 | 12 | } else { |
| 100 | 12 | spans.append(ColumnSpan(start: activeStart, end: end)) |
| 101 | 12 | } |
| 102 | 13 | } |
| 103 | 192 | for segment in onLine { |
| 104 | 154 | close(at: segment.column) |
| 105 | 154 | active = segment |
| 106 | 154 | activeStart = segment.column |
| 107 | 192 | } |
| 108 | 192 | close(at: nil) |
| 109 | 192 | return spans |
| 110 | 192 | } |
| 111 | | } |
| 112 | | |
| 113 | | /// How many lines hold code and how many of those ran, each line counted once. |
| 114 | | public struct LineSummary: Sendable, Equatable { |
| 115 | | public let count: Int |
| 116 | | public let covered: Int |
| 117 | | |
| 118 | 618 | public init(count: Int, covered: Int) { |
| 119 | 618 | self.count = count |
| 120 | 618 | self.covered = covered |
| 121 | 618 | } |
| 122 | | |
| 123 | 90 | public init(lines: [LineCoverage]) { |
| 124 | 90 | self.init( |
| 125 | 283 | count: lines.count { $0.state != .notCode }, |
| 126 | 283 | covered: lines.count { $0.state == .covered || $0.state == .partlyCovered }) |
| 127 | 90 | } |
| 128 | | } |