Skip to content

Commit e52ea22

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 bd53a85 commit e52ea22

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
@@ -106,6 +106,11 @@ public struct WarningControlRegionTree {
106106
/// Root region representing top-level (file) scope
107107
private var rootRegionNode: WarningControlRegionNode
108108

109+
/// All of the diagnostic identifiers contained in this tree
110+
/// which have at least one occurence with a non-`ignored` behavior
111+
/// specifier
112+
private var enabledIdentifiers: Set<DiagnosticGroupIdentifier> = []
113+
109114
init(range: Range<AbsolutePosition>) {
110115
rootRegionNode = WarningControlRegionNode(range: range)
111116
}
@@ -118,6 +123,9 @@ public struct WarningControlRegionTree {
118123
let newNode = WarningControlRegionNode(range: range)
119124
for (diagnosticGroupIdentifier, behavior) in controls {
120125
newNode.addWarningGroupControl(for: diagnosticGroupIdentifier, behavior: behavior)
126+
if behavior != .ignored {
127+
enabledIdentifiers.insert(diagnosticGroupIdentifier)
128+
}
121129
}
122130
insertIntoSubtree(newNode, parent: rootRegionNode)
123131
}
@@ -183,6 +191,15 @@ extension WarningControlRegionTree {
183191
}
184192
}
185193

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