1010//
1111//===----------------------------------------------------------------------===//
1212
13- import SwiftSyntax
1413import SwiftDiagnostics
14+ import SwiftSyntax
15+ import SwiftSyntaxBuilder
1516
1617/// Interface to extract information about the context in which a given
1718/// macro is expanded.
@@ -41,11 +42,31 @@ public protocol MacroExpansionContext: AnyObject {
4142 /// - Returns: the source location within the given node, or `nil` if the
4243 /// given syntax node is not rooted in a source file that the macro
4344 /// expansion context knows about.
45+ @available ( * , deprecated, message: " Please use AbstractSourceLocation version " )
4446 func location< Node: SyntaxProtocol > (
4547 of node: Node ,
4648 at position: PositionInSyntaxNode ,
4749 filePathMode: SourceLocationFilePathMode
4850 ) -> 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 ?
4970}
5071
5172extension MacroExpansionContext {
@@ -58,11 +79,64 @@ extension MacroExpansionContext {
5879 /// - Returns: the source location within the given node, or `nil` if the
5980 /// given syntax node is not rooted in a source file that the macro
6081 /// expansion context knows about.
82+ @available ( * , deprecated, message: " Please use AbstractSourceLocation version " )
6183 public func location< Node: SyntaxProtocol > (
6284 of node: Node
6385 ) -> SourceLocation ? {
6486 return location ( of: node, at: . afterLeadingTrivia, filePathMode: . fileID)
6587 }
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+ }
66140}
67141
68142/// Diagnostic message used for thrown errors.
0 commit comments