Skip to content

Commit 86ac634

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 86ac634

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

Sources/SwiftWarningControl/WarningControlRegions.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,13 @@ 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+
private var enabledGroups: Set<DiagnosticGroupIdentifier> = []
110+
106111
/// Inheritance tree among diagnostic group identifiers
107-
let groupInheritanceTree: DiagnosticGroupInheritanceTree
112+
private let groupInheritanceTree: DiagnosticGroupInheritanceTree
108113

109114
init(
110115
range: Range<AbsolutePosition>,
@@ -130,6 +135,10 @@ public struct WarningControlRegionTree {
130135
let groupIdentifier = groups.removeFirst()
131136
processedGroups.insert(groupIdentifier)
132137
newNode.addWarningGroupControl(for: groupIdentifier, control: control)
138+
if control != .ignored {
139+
enabledGroups.insert(diagnosticGroupIdentifier)
140+
}
141+
133142
let newSubGroups = groupInheritanceTree.subgroups(of: groupIdentifier).filter { !processedGroups.contains($0) }
134143
// Ensure we add a corresponding control to each direct and
135144
// transitive sub-group of the one specified on this control.
@@ -201,6 +210,15 @@ extension WarningControlRegionTree {
201210
}
202211
}
203212

213+
extension WarningControlRegionTree {
214+
/// Produce a set of diagnostic group identifiers contained in
215+
/// this tree which have a behavior specifier other than `ignored`.
216+
@_spi(ExperimentalLanguageFeatures)
217+
public func getEnabledDiagnosticGroups() -> Set<DiagnosticGroupIdentifier> {
218+
return enabledGroups
219+
}
220+
}
221+
204222
/// A node in the warning control region tree, representing a collection of warning
205223
/// group controls and references to its nested child regions.
206224
private class WarningControlRegionNode {

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.getEnabledDiagnosticGroups()
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)