|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +/// A struct wrapper for a diagnostic group inheritance tree |
| 14 | +/// represented with a dictionary of a group identifier to an array |
| 15 | +/// of its sub-group identifiers. |
| 16 | +@_spi(ExperimentalLanguageFeatures) |
| 17 | +public struct DiagnosticGroupInheritanceTree { |
| 18 | + private let subGroups: [DiagnosticGroupIdentifier: [DiagnosticGroupIdentifier]] |
| 19 | + public init(subGroups: [DiagnosticGroupIdentifier: [DiagnosticGroupIdentifier]]) throws { |
| 20 | + self.subGroups = subGroups |
| 21 | + if hasCycle() { |
| 22 | + throw WarningControlError.groupInheritanceCycle |
| 23 | + } |
| 24 | + } |
| 25 | + init() { |
| 26 | + self.subGroups = [:] |
| 27 | + } |
| 28 | + |
| 29 | + /// Diagnostic groups that inherit from `group`. |
| 30 | + func subgroups(of group: DiagnosticGroupIdentifier) -> [DiagnosticGroupIdentifier] { subGroups[group] ?? [] } |
| 31 | +} |
| 32 | + |
| 33 | +extension DiagnosticGroupInheritanceTree { |
| 34 | + // Check the subgroup tree for possible cycles |
| 35 | + func hasCycle() -> Bool { |
| 36 | + var visited: Set<DiagnosticGroupIdentifier> = [] |
| 37 | + var recursionStack: Set<DiagnosticGroupIdentifier> = [] |
| 38 | + func hasCycleFromGroup(_ group: DiagnosticGroupIdentifier) -> Bool { |
| 39 | + if visited.insert(group).inserted { |
| 40 | + recursionStack.insert(group) |
| 41 | + let subgroups = self.subgroups(of: group) |
| 42 | + for subGroup in subgroups { |
| 43 | + if recursionStack.contains(subGroup) { |
| 44 | + return true |
| 45 | + } else if !visited.contains(subGroup), hasCycleFromGroup(subGroup) { |
| 46 | + return true |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + recursionStack.remove(group) |
| 51 | + return false |
| 52 | + } |
| 53 | + |
| 54 | + for group in subGroups.keys { |
| 55 | + if !visited.contains(group), hasCycleFromGroup(group) { |
| 56 | + return true |
| 57 | + } |
| 58 | + } |
| 59 | + return false |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// Describes the kinds of diagnostics that can occur when processing warning |
| 64 | +/// group control queries. This is an Error-conforming type so we can throw errors when |
| 65 | +/// needed. |
| 66 | +@_spi(ExperimentalLanguageFeatures) |
| 67 | +public enum WarningControlError: Error, CustomStringConvertible { |
| 68 | + case groupInheritanceCycle |
| 69 | + |
| 70 | + public var description: String { |
| 71 | + switch self { |
| 72 | + case .groupInheritanceCycle: |
| 73 | + return "cycle detected in the warning group inheritance hierarchy" |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments