1import 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.
8public 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.
1531 public init(json: Data) throws {
1631 let decoder = JSONDecoder()
1731 let format = try decoder.decode(Format.self, from: json)
1831 guard format.type == "llvm.coverage.json.export", format.version.hasPrefix("3.") else {
192 throw CoverageExportError.unsupportedFormat(type: format.type, version: format.version)
2029 }
2129 let contents = try decoder.decode(Contents.self, from: json)
2228 guard contents.data.count == 1 else {
231 throw CoverageExportError.unexpectedDataCount(contents.data.count)
2427 }
2527 files = contents.data[0].files
2627 }
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
43public 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.
50public 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.
6852 public init(from decoder: any Decoder) throws {
6952 let values = try decoder.container(keyedBy: CodingKeys.self)
7052 path = try values.decode(String.self, forKey: .path)
7152 segments = try values.decode([CoverageSegment].self, forKey: .segments)
7251 branches = try values.decodeIfPresent([BranchRegion].self, forKey: .branches) ?? []
7351 conditionRecords =
7451 try values.decodeIfPresent([ConditionRecord].self, forKey: .conditionRecords) ?? []
7551 }
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]`.
82public 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
97293 ) {
98293 self.line = line
99293 self.column = column
100293 self.count = count
101293 self.hasCount = hasCount
102293 self.isRegionEntry = isRegionEntry
103293 self.isGapRegion = isGapRegion
104293 }
105}
106
107extension CoverageSegment: Decodable {
108273 public init(from decoder: any Decoder) throws {
109273 var fields = try decoder.unkeyedContainer()
110273 self.init(
111273 line: try fields.decode(Int.self),
112273 column: try fields.decode(Int.self),
113273 count: try fields.decode(UInt64.self),
114273 hasCount: try fields.decode(Bool.self),
115272 isRegionEntry: try fields.decode(Bool.self),
116272 isGapRegion: try fields.decode(Bool.self)
117272 )
118272 }
119}