Skip to content

Commit bc8fd0a

Browse files
committed
[SwiftWarningControl] Keep track of enabled diagnostic group identifiers
And add public API on `WarningControlRegionTree` to query them: `getEnabledDiagnosticGroups`. This is intended to be used by clients (the compiler) to be able to query whether a given diagnostic group has been enabled in a given source file.
1 parent 02f8220 commit bc8fd0a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Sources/SwiftWarningControl/WarningControlRegions.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,14 @@ public struct WarningControlRegionTree {
103103
/// Root region representing top-level (file) scope
104104
private var rootRegionNode: WarningControlRegionNode
105105

106+
/// All of the diagnostic group identifiers contained in this tree
107+
/// which have at least one occurence with a non-`ignored` behavior
108+
/// specifier
109+
@_spi(ExperimentalLanguageFeatures)
110+
public private(set) var enabledGroups: Set<DiagnosticGroupIdentifier> = []
111+
106112
/// Inheritance tree among diagnostic group identifiers
107-
let groupInheritanceTree: DiagnosticGroupInheritanceTree
113+
private let groupInheritanceTree: DiagnosticGroupInheritanceTree
108114

109115
init(
110116
range: Range<AbsolutePosition>,
@@ -130,6 +136,10 @@ public struct WarningControlRegionTree {
130136
let groupIdentifier = groups.removeFirst()
131137
processedGroups.insert(groupIdentifier)
132138
newNode.addWarningGroupControl(for: groupIdentifier, control: control)
139+
if control != .ignored {
140+
enabledGroups.insert(diagnosticGroupIdentifier)
141+
}
142+
133143
let newSubGroups = groupInheritanceTree.subgroups(of: groupIdentifier).filter { !processedGroups.contains($0) }
134144
// Ensure we add a corresponding control to each direct and
135145
// transitive sub-group of the one specified on this control.

Tests/SwiftWarningControlTest/WarningControlTests.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,40 @@ public class WarningGroupControlTests: XCTestCase {
7575
)
7676
}
7777

78+
func testEnabledGroupIdentifiers() throws {
79+
let source =
80+
"""
81+
@warn(Group1, as: warning)
82+
@warn(Group2, as: error)
83+
@warn(Group3, as: ignored)
84+
func foo() {
85+
@warn(Group4, as: warning)
86+
func bar() {
87+
@warn(Group5, as: ignored)
88+
@warn(Group6, as: ignored)
89+
func baz() {}
90+
@warn(Group7, as: error)
91+
func qux() {
92+
@warn(Group8, as: warning)
93+
func corge() {}
94+
}
95+
}
96+
}
97+
"""
98+
var parser = Parser(source)
99+
let parseTree = SourceFileSyntax.parse(from: &parser)
100+
let warningControlTree = parseTree.warningGroupControlRegionTree()
101+
let enabledDiagnosticGroups = warningControlTree.enabledGroups
102+
XCTAssertTrue(enabledDiagnosticGroups.contains("Group1"))
103+
XCTAssertTrue(enabledDiagnosticGroups.contains("Group2"))
104+
XCTAssertFalse(enabledDiagnosticGroups.contains("Group3"))
105+
XCTAssertTrue(enabledDiagnosticGroups.contains("Group4"))
106+
XCTAssertFalse(enabledDiagnosticGroups.contains("Group5"))
107+
XCTAssertFalse(enabledDiagnosticGroups.contains("Group6"))
108+
XCTAssertTrue(enabledDiagnosticGroups.contains("Group7"))
109+
XCTAssertTrue(enabledDiagnosticGroups.contains("Group8"))
110+
}
111+
78112
func testNominalDeclWarningGroupControl() throws {
79113
try assertWarningGroupControl(
80114
"""

0 commit comments

Comments
 (0)