Skip to content

Commit 45401fd

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 90e6d96 commit 45401fd

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

Sources/SwiftWarningControl/WarningControlRegions.swift

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

106+
/// All of the diagnostic identifiers contained in this tree
107+
/// which have at least one occurence with a non-`ignored` behavior
108+
/// specifier
109+
private var enabledIdentifiers: Set<DiagnosticGroupIdentifier> = []
110+
106111
init(range: Range<AbsolutePosition>) {
107112
rootRegionNode = WarningControlRegionNode(range: range)
108113
}
@@ -116,6 +121,9 @@ public struct WarningControlRegionTree {
116121
let newNode = WarningControlRegionNode(range: range)
117122
for (diagnosticGroupIdentifier, control) in controls {
118123
newNode.addWarningGroupControl(for: diagnosticGroupIdentifier, control: control)
124+
if control != .ignored {
125+
enabledIdentifiers.insert(diagnosticGroupIdentifier)
126+
}
119127
}
120128
insertIntoSubtree(newNode, parent: rootRegionNode)
121129
}
@@ -182,6 +190,15 @@ extension WarningControlRegionTree {
182190
}
183191
}
184192

193+
extension WarningControlRegionTree {
194+
/// Produce an array of diagnostic group identifiers contained in
195+
/// this tree which have a behavior specifier other than `ignored`.
196+
@_spi(ExperimentalLanguageFeatures)
197+
public func getEnabledDiagnosticGroups() -> Set<DiagnosticGroupIdentifier> {
198+
return enabledIdentifiers
199+
}
200+
}
201+
185202
/// A node in the warning control region tree, representing a collection of warning
186203
/// group controls and references to its nested child regions.
187204
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)