| 1 | | import Foundation |
| 2 | | |
| 3 | | /// The coverage of every source file in one `llvm-cov export -format=text` run. |
| 4 | | /// |
| 5 | | /// Reads format version 3 (LLVM 12 and later). Only the parts the report uses are |
| 6 | | /// decoded: each file's path and its segments. llvm-cov's own summaries are left out: |
| 7 | | /// they add up lines per function, so a line holding a closure counts twice. |
| 8 | | public struct CoverageExport: Sendable { |
| 9 | | public let files: [FileCoverage] |
| 10 | | |
| 11 | | /// Decodes an export. |
| 12 | | /// |
| 13 | | /// - Throws: `CoverageExportError` when the JSON isn't a version 3 llvm-cov export |
| 14 | | /// holding exactly one set of files, or `DecodingError` when it is malformed. |
| 15 | 28 | public init(json: Data) throws { |
| 16 | 28 | let decoder = JSONDecoder() |
| 17 | 28 | let format = try decoder.decode(Format.self, from: json) |
| 18 | 28 | guard format.type == "llvm.coverage.json.export", format.version.hasPrefix("3.") else { |
| 19 | 2 | throw CoverageExportError.unsupportedFormat(type: format.type, version: format.version) |
| 20 | 26 | } |
| 21 | 26 | let contents = try decoder.decode(Contents.self, from: json) |
| 22 | 25 | guard contents.data.count == 1 else { |
| 23 | 1 | throw CoverageExportError.unexpectedDataCount(contents.data.count) |
| 24 | 24 | } |
| 25 | 24 | files = contents.data[0].files |
| 26 | 24 | } |
| 27 | | |
| 28 | | /// Read on its own first, so an export of another version fails on its version |
| 29 | | /// instead of on whichever field changed shape. |
| 30 | | private struct Format: Decodable { |
| 31 | | let type: String |
| 32 | | let version: String |
| 33 | | } |
| 34 | | |
| 35 | | private struct Contents: Decodable { |
| 36 | | struct Entry: Decodable { |
| 37 | | let files: [FileCoverage] |
| 38 | | } |
| 39 | | let data: [Entry] |
| 40 | | } |
| 41 | | } |
| 42 | | |
| 43 | | public enum CoverageExportError: Error, Equatable { |
| 44 | | case unsupportedFormat(type: String, version: String) |
| 45 | | /// llvm-cov writes one entry per export; more would mean a merged or unknown file. |
| 46 | | case unexpectedDataCount(Int) |
| 47 | | } |
| 48 | | |
| 49 | | /// One source file's coverage as llvm-cov reports it. |
| 50 | | public struct FileCoverage: Sendable, Decodable { |
| 51 | | /// The absolute path llvm-cov recorded at build time. |
| 52 | | public let path: String |
| 53 | | /// Ordered by line, then column. |
| 54 | | public let segments: [CoverageSegment] |
| 55 | | /// Empty for Swift, which llvm-cov exports no branch data for. |
| 56 | | public let branches: [BranchRegion] |
| 57 | | /// Empty unless the code was built with clang's `-fcoverage-mcdc`. |
| 58 | | public let conditionRecords: [ConditionRecord] |
| 59 | | |
| 60 | | private enum CodingKeys: String, CodingKey { |
| 61 | | case path = "filename" |
| 62 | | case segments |
| 63 | | case branches |
| 64 | | case conditionRecords = "mcdc_records" |
| 65 | | } |
| 66 | | |
| 67 | | /// The branch keys may be missing; the file then has none. |
| 68 | 46 | public init(from decoder: any Decoder) throws { |
| 69 | 46 | let values = try decoder.container(keyedBy: CodingKeys.self) |
| 70 | 46 | path = try values.decode(String.self, forKey: .path) |
| 71 | 46 | segments = try values.decode([CoverageSegment].self, forKey: .segments) |
| 72 | 45 | branches = try values.decodeIfPresent([BranchRegion].self, forKey: .branches) ?? [] |
| 73 | 45 | conditionRecords = |
| 74 | 45 | try values.decodeIfPresent([ConditionRecord].self, forKey: .conditionRecords) ?? [] |
| 75 | 45 | } |
| 76 | | } |
| 77 | | |
| 78 | | /// A point in a file where the active coverage region changes. |
| 79 | | /// |
| 80 | | /// The region that starts here runs until the next segment. Encoded by llvm-cov as |
| 81 | | /// `[line, column, count, hasCount, isRegionEntry, isGapRegion]`. |
| 82 | | public struct CoverageSegment: Sendable, Equatable { |
| 83 | | public let line: Int |
| 84 | | /// 1-based. |
| 85 | | public let column: Int |
| 86 | | public let count: UInt64 |
| 87 | | /// False for code the compiler skipped (for example an inactive `#if` branch). |
| 88 | | public let hasCount: Bool |
| 89 | | /// True when a region starts here; false when an enclosing region resumes. |
| 90 | | public let isRegionEntry: Bool |
| 91 | | /// True for the whitespace between regions, which never counts as code. |
| 92 | | public let isGapRegion: Bool |
| 93 | | |
| 94 | | public init( |
| 95 | | line: Int, column: Int, count: UInt64, hasCount: Bool = true, |
| 96 | | isRegionEntry: Bool = true, isGapRegion: Bool = false |
| 97 | 281 | ) { |
| 98 | 281 | self.line = line |
| 99 | 281 | self.column = column |
| 100 | 281 | self.count = count |
| 101 | 281 | self.hasCount = hasCount |
| 102 | 281 | self.isRegionEntry = isRegionEntry |
| 103 | 281 | self.isGapRegion = isGapRegion |
| 104 | 281 | } |
| 105 | | } |
| 106 | | |
| 107 | | extension CoverageSegment: Decodable { |
| 108 | 261 | public init(from decoder: any Decoder) throws { |
| 109 | 261 | var fields = try decoder.unkeyedContainer() |
| 110 | 261 | self.init( |
| 111 | 261 | line: try fields.decode(Int.self), |
| 112 | 261 | column: try fields.decode(Int.self), |
| 113 | 261 | count: try fields.decode(UInt64.self), |
| 114 | 261 | hasCount: try fields.decode(Bool.self), |
| 115 | 260 | isRegionEntry: try fields.decode(Bool.self), |
| 116 | 260 | isGapRegion: try fields.decode(Bool.self) |
| 117 | 260 | ) |
| 118 | 260 | } |
| 119 | | } |