1import Foundation
2
3/// How many branch outcomes or MC/DC conditions a file, module or run has and how many
4/// were covered (DESIGN.md D4).
5public struct CoverageCount: Sendable, Equatable {
6 public let total: Int
7 public let covered: Int
8
9243 public init(total: Int, covered: Int) {
10243 self.total = total
11243 self.covered = covered
12243 }
13
14 /// Sums of several counts, such as a module's files.
15175 init(adding counts: some Sequence<CoverageCount>) {
16175 var total = 0
17175 var covered = 0
18188 for count in counts {
19188 total += count.total
20188 covered += count.covered
21188 }
22175 self.init(total: total, covered: covered)
23175 }
24
25 /// The covered share, such as `62.5%`; a count of nothing shows 100%, matching its
26 /// tier.
27139 var percentText: String {
28139 String(format: "%.1f%%", total == 0 ? 100 : Double(covered) * 100 / Double(total))
29139 }
30
3183 var tier: CoverageTier { CoverageTier(covered: covered, total: total) }
32}
33
34/// One branch from `branches` in the export: a decision with two outcomes.
35public struct BranchRegion: Sendable, Equatable, Decodable {
36 /// Times the true outcome ran.
37 public let trueCount: UInt64
38 public let falseCount: UInt64
39
4026 public init(trueCount: UInt64, falseCount: UInt64) {
4126 self.trueCount = trueCount
4226 self.falseCount = falseCount
4326 }
44
45 /// llvm-cov encodes a branch as `[lineStart, columnStart, lineEnd, columnEnd,
46 /// executionCount, falseExecutionCount, fileId, expandedFileId, kind]`.
4724 public init(from decoder: any Decoder) throws {
4824 var fields = try decoder.unkeyedContainer()
4996 for _ in 0..<Self.regionPositionFieldCount { _ = try fields.decode(Int.self) }
5024 self.init(
5124 trueCount: try fields.decode(UInt64.self), falseCount: try fields.decode(UInt64.self))
5224 }
53
54 /// The four line and column fields that open the array.
55 private static let regionPositionFieldCount = 4
56}
57
58/// One MC/DC decision from `mcdc_records` in the export, with one flag per condition.
59public struct ConditionRecord: Sendable, Equatable, Decodable {
60 /// True for each condition that has shown it independently changes the decision.
61 public let conditionsCovered: [Bool]
62
6314 public init(conditionsCovered: [Bool]) {
6414 self.conditionsCovered = conditionsCovered
6514 }
66
67 /// llvm-cov encodes a record as nine numbers (the decision's line and column range,
68 /// its execution counts, file ids and kind) followed by the array of condition flags.
6912 public init(from decoder: any Decoder) throws {
7012 var fields = try decoder.unkeyedContainer()
71108 for _ in 0..<Self.leadingFieldCount { _ = try fields.decode(UInt64.self) }
7212 self.init(conditionsCovered: try fields.decode([Bool].self))
7312 }
74
75 private static let leadingFieldCount = 9
76}
77
78extension CoverageCount {
79 /// The sum of the counts that exist; nil when none do, so a module of files without
80 /// data has none either.
812546 init?(addingPresent counts: some Sequence<CoverageCount?>) {
823145 let present = counts.compactMap { $0 }
832546 guard !present.isEmpty else { return nil }
84174 self.init(adding: present)
85174 }
86}
87
88extension FileCoverage {
89 /// Covered of all branch outcomes (each branch has a true and a false one); nil when
90 /// the export holds no branches, as for Swift.
9153 public var branchCount: CoverageCount? {
9253 guard !branches.isEmpty else { return nil }
9312 let covered = branches.reduce(0) { sum, branch in
9412 sum + (branch.trueCount > 0 ? 1 : 0) + (branch.falseCount > 0 ? 1 : 0)
9512 }
963 return CoverageCount(total: branches.count * Self.outcomesPerBranch, covered: covered)
9753 }
98
99 /// Covered of all MC/DC conditions; nil when the export holds no MC/DC records.
10053 public var conditionCount: CoverageCount? {
10153 guard !conditionRecords.isEmpty else { return nil }
1026 let flags = conditionRecords.flatMap(\.conditionsCovered)
10312 return CoverageCount(total: flags.count, covered: flags.count { $0 })
10453 }
105
106 private static let outcomesPerBranch = 2
107}