10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- import SwiftSyntax
14
13
import SwiftDiagnostics
14
+ import SwiftSyntax
15
+ import SwiftSyntaxBuilder
15
16
16
17
/// Interface to extract information about the context in which a given
17
18
/// macro is expanded.
@@ -41,11 +42,31 @@ public protocol MacroExpansionContext: AnyObject {
41
42
/// - Returns: the source location within the given node, or `nil` if the
42
43
/// given syntax node is not rooted in a source file that the macro
43
44
/// expansion context knows about.
45
+ @available ( * , deprecated, message: " Please use AbstractSourceLocation version " )
44
46
func location< Node: SyntaxProtocol > (
45
47
of node: Node ,
46
48
at position: PositionInSyntaxNode ,
47
49
filePathMode: SourceLocationFilePathMode
48
50
) -> SourceLocation ?
51
+
52
+ /// Retrieve a source location for the given syntax node.
53
+ ///
54
+ /// - Parameters:
55
+ /// - node: The syntax node whose source location to produce.
56
+ /// - position: The position within the syntax node for the resulting
57
+ /// location.
58
+ /// - filePathMode: How the file name contained in the source location is
59
+ /// formed.
60
+ ///
61
+ /// - Returns: the source location within the given node, or `nil` if the
62
+ /// given syntax node is not rooted in a source file that the macro
63
+ /// expansion context knows about.
64
+ @_disfavoredOverload
65
+ func location< Node: SyntaxProtocol > (
66
+ of node: Node ,
67
+ at position: PositionInSyntaxNode ,
68
+ filePathMode: SourceLocationFilePathMode
69
+ ) -> AbstractSourceLocation ?
49
70
}
50
71
51
72
extension MacroExpansionContext {
@@ -58,11 +79,64 @@ extension MacroExpansionContext {
58
79
/// - Returns: the source location within the given node, or `nil` if the
59
80
/// given syntax node is not rooted in a source file that the macro
60
81
/// expansion context knows about.
82
+ @available ( * , deprecated, message: " Please use AbstractSourceLocation version " )
61
83
public func location< Node: SyntaxProtocol > (
62
84
of node: Node
63
85
) -> SourceLocation ? {
64
86
return location ( of: node, at: . afterLeadingTrivia, filePathMode: . fileID)
65
87
}
88
+
89
+ /// Retrieve a source location for the given syntax node's starting token
90
+ /// (after leading trivia) using file naming according to `#fileID`.
91
+ ///
92
+ /// - Parameters:
93
+ /// - node: The syntax node whose source location to produce.
94
+ ///
95
+ /// - Returns: the source location within the given node, or `nil` if the
96
+ /// given syntax node is not rooted in a source file that the macro
97
+ /// expansion context knows about.
98
+ @_disfavoredOverload
99
+ public func location< Node: SyntaxProtocol > (
100
+ of node: Node
101
+ ) -> AbstractSourceLocation ? {
102
+ return location ( of: node, at: . afterLeadingTrivia, filePathMode: . fileID)
103
+ }
104
+ }
105
+
106
+ extension MacroExpansionContext {
107
+ /// Retrieve a source location for the given syntax node.
108
+ ///
109
+ /// - Parameters:
110
+ /// - node: The syntax node whose source location to produce.
111
+ /// - position: The position within the syntax node for the resulting
112
+ /// location.
113
+ /// - filePathMode: How the file name contained in the source location is
114
+ /// formed.
115
+ ///
116
+ /// - Returns: the source location within the given node, or `nil` if the
117
+ /// given syntax node is not rooted in a source file that the macro
118
+ /// expansion context knows about.
119
+ @_disfavoredOverload
120
+ @available ( * , deprecated, message: " Please use AbstractSourceLocation version " )
121
+ public func location< Node: SyntaxProtocol > (
122
+ of node: Node ,
123
+ at position: PositionInSyntaxNode ,
124
+ filePathMode: SourceLocationFilePathMode
125
+ ) -> AbstractSourceLocation ? {
126
+ guard let sourceLoc: SourceLocation = location ( of: node, at: position, filePathMode: filePathMode) ,
127
+ let file = sourceLoc. file,
128
+ let line = sourceLoc. line,
129
+ let column = sourceLoc. column
130
+ else {
131
+ return nil
132
+ }
133
+
134
+ return AbstractSourceLocation (
135
+ file: " \( literal: file) " ,
136
+ line: " \( literal: line) " ,
137
+ column: " \( literal: column) "
138
+ )
139
+ }
66
140
}
67
141
68
142
/// Diagnostic message used for thrown errors.
0 commit comments