@@ -117,15 +117,6 @@ extension SourceManager.MacroExpansionContext: MacroExpansionContext {
117117 return nil
118118 }
119119
120- // Determine the filename to use in the resulting location.
121- let fileName : String
122- switch filePathMode {
123- case . fileID:
124- fileName = " \( exportedSourceFile. moduleName) / \( exportedSourceFile. fileName. basename) "
125-
126- case . filePath:
127- fileName = exportedSourceFile. fileName
128- }
129120
130121 // Find the node's offset relative to its root.
131122 let rawPosition : AbsolutePosition
@@ -146,8 +137,37 @@ extension SourceManager.MacroExpansionContext: MacroExpansionContext {
146137 let offsetWithinSyntaxNode =
147138 rawPosition. utf8Offset - node. position. utf8Offset
148139
140+ var location = exportedSourceFile. sourceLocationConverter. location (
141+ for: rootPosition. advanced ( by: offsetWithinSyntaxNode)
142+ )
143+
144+ switch filePathMode {
145+ case . fileID:
146+ // The `SourceLocationConverter` in `exportedSourceFile` uses `filePath` as the file mode. When the `fileID` mode
147+ // is requested, we need to adjust the file and presumed file to the `fileID`.
148+ let fileID = " \( exportedSourceFile. moduleName) / \( exportedSourceFile. fileName. basename) "
149+ var adjustedFile = location. file
150+ if adjustedFile == exportedSourceFile. fileName {
151+ adjustedFile = fileID
152+ }
153+ var adjustedPresumedFile = location. presumedFile
154+ if adjustedPresumedFile == exportedSourceFile. fileName {
155+ adjustedPresumedFile = fileID
156+ }
157+ location = SourceLocation (
158+ line: location. line,
159+ column: location. column,
160+ offset: location. offset,
161+ file: adjustedFile,
162+ presumedLine: location. presumedLine,
163+ presumedFile: adjustedPresumedFile
164+ )
165+
166+ case . filePath:
167+ break
168+ }
169+
149170 // Do the location lookup.
150- let converter = SourceLocationConverter ( file: fileName, tree: sourceFile)
151- return AbstractSourceLocation ( converter. location ( for: rootPosition. advanced ( by: offsetWithinSyntaxNode) ) )
171+ return AbstractSourceLocation ( location)
152172 }
153173}
0 commit comments