Skip to content

Commit 1462312

Browse files
committed
Add name for diagnostics for syntax node child
1 parent 7608990 commit 1462312

29 files changed

+5367
-196
lines changed

Sources/SwiftSyntax/Misc.swift.gyb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ extension Syntax {
3939
% for node in NON_BASE_SYNTAX_NODES:
4040
case .${node.swift_syntax_kind}(let node):
4141
return node
42+
% end
43+
}
44+
}
45+
46+
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? {
47+
switch self.as(SyntaxEnum.self) {
48+
case .token(let node):
49+
return node.childNameForDiagnostics(index)
50+
case .unknown(let node):
51+
return node.childNameForDiagnostics(index)
52+
% for node in NON_BASE_SYNTAX_NODES:
53+
case .${node.swift_syntax_kind}(let node):
54+
return node.childNameForDiagnostics(index)
4255
% end
4356
}
4457
}

Sources/SwiftSyntax/Syntax.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ public protocol SyntaxProtocol: CustomStringConvertible,
131131
/// Converts the given `Syntax` node to this type. Returns `nil` if the
132132
/// conversion is not possible.
133133
init?(_ syntaxNode: Syntax)
134+
135+
/// Return a name with which the child at the given `index` can be referred to
136+
/// in diagnostics.
137+
func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String?
134138
}
135139

136140
extension SyntaxProtocol {

Sources/SwiftSyntax/SyntaxBaseNodes.swift.gyb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ public struct ${node.name}: ${node.name}Protocol, SyntaxHashable {
120120
public func asProtocol(_: ${node.name}Protocol.Protocol) -> ${node.name}Protocol {
121121
return Syntax(self).asProtocol(${node.name}Protocol.self)!
122122
}
123+
124+
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? {
125+
return Syntax(self).childNameForDiagnostics(index)
126+
}
123127
}
124128

125129
extension ${node.name}: CustomReflectable {

Sources/SwiftSyntax/SyntaxChildren.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public struct SyntaxChildrenIndexData: Comparable {
2020
fileprivate let offset: UInt32
2121
/// The index of the node in the parent.
2222
/// See `AbsoluteSyntaxPosition.indexInParent`
23-
fileprivate let indexInParent: UInt32
23+
let indexInParent: UInt32
2424
/// Unique value for a node within its own tree.
2525
/// See `SyntaxIdentifier.indexIntree`
2626
fileprivate let indexInTree: SyntaxIndexInTree
@@ -61,7 +61,7 @@ public struct SyntaxChildrenIndex: Comparable, ExpressibleByNilLiteral {
6161
/// SyntaxChildrenIndex` an enumbecause the optional value can be
6262
/// force-unwrapped when we know that an index is not the end index, saving a
6363
/// switch case comparison.
64-
fileprivate let data: SyntaxChildrenIndexData?
64+
let data: SyntaxChildrenIndexData?
6565

6666
fileprivate init(offset: UInt32, indexInParent: UInt32,
6767
indexInTree: SyntaxIndexInTree) {

Sources/SwiftSyntax/SyntaxCollections.swift.gyb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ public struct ${node.name}: SyntaxCollection, SyntaxHashable {
217217
self = withTrailingTrivia(newValue ?? [])
218218
}
219219
}
220+
221+
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? {
222+
return nil
223+
}
220224
}
221225

222226
/// Conformance for `${node.name}` to the `BidirectionalCollection` protocol.

Sources/SwiftSyntax/SyntaxNodes.swift.gyb.template

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,21 @@ public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
171171
return ${node.name}(newData)
172172
}
173173
% end
174+
175+
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? {
176+
switch index.data?.indexInParent {
177+
% for (index, child) in enumerate(node.children):
178+
case ${index}:
179+
% if child.name_for_diagnostics:
180+
return "${child.name_for_diagnostics}"
181+
% else:
182+
return nil
183+
% end
184+
% end
185+
default:
186+
fatalError("Invalid index")
187+
}
188+
}
174189
}
175190

176191
extension ${node.name}: CustomReflectable {

Sources/SwiftSyntax/SyntaxOtherNodes.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public struct UnknownSyntax: SyntaxProtocol, SyntaxHashable {
3939
let data = SyntaxData.forRoot(raw)
4040
self.init(data)
4141
}
42+
43+
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? {
44+
return nil
45+
}
4246
}
4347

4448
extension UnknownSyntax: CustomReflectable {
@@ -192,6 +196,10 @@ public struct TokenSyntax: SyntaxProtocol, SyntaxHashable {
192196
public var totalLength: SourceLength {
193197
return raw.totalLength
194198
}
199+
200+
public func childNameForDiagnostics(_ index: SyntaxChildrenIndex) -> String? {
201+
return nil
202+
}
195203
}
196204

197205
extension TokenSyntax: CustomReflectable {

0 commit comments

Comments
 (0)