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.
1528 public init(json: Data) throws {
1628 let decoder = JSONDecoder()
1728 let format = try decoder.decode(Format.self, from: json)
1828 guard format.type == "llvm.coverage.json.export", format.version.hasPrefix("3.") else {
192 throw CoverageExportError.unsupportedFormat(type: format.type, version: format.version)
2026 }
2126 let contents = try decoder.decode(Contents.self, from: json)
2225 guard contents.data.count == 1 else {
231 throw CoverageExportError.unexpectedDataCount(contents.data.count)
2424 }
2524 files = contents.data[0].files
2624 }
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.
6846 public init(from decoder: any Decoder) throws {
6946 let values = try decoder.container(keyedBy: CodingKeys.self)
7046 path = try values.decode(String.self, forKey: .path)
7146 segments = try values.decode([CoverageSegment].self, forKey: .segments)
7245 branches = try values.decodeIfPresent([BranchRegion].self, forKey: .branches) ?? []
7345 conditionRecords =
7445 try values.decodeIfPresent([ConditionRecord].self, forKey: .conditionRecords) ?? []
7545 }
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
97281 ) {
98281 self.line = line
99281 self.column = column
100281 self.count = count
101281 self.hasCount = hasCount
102281 self.isRegionEntry = isRegionEntry
103281 self.isGapRegion = isGapRegion
104281 }
105}
106
107extension CoverageSegment: Decodable {
108261 public init(from decoder: any Decoder) throws {
109261 var fields = try decoder.unkeyedContainer()
110261 self.init(
111261 line: try fields.decode(Int.self),
112261 column: try fields.decode(Int.self),
113261 count: try fields.decode(UInt64.self),
114261 hasCount: try fields.decode(Bool.self),
115260 isRegionEntry: try fields.decode(Bool.self),
116260 isGapRegion: try fields.decode(Bool.self)
117260 )
118260 }
119}