@@ -102,15 +102,6 @@ extension SourceManager.MacroExpansionContext: MacroExpansionContext {
102102 return nil
103103 }
104104
105- // Determine the filename to use in the resulting location.
106- let fileName : String
107- switch filePathMode {
108- case . fileID:
109- fileName = " \( exportedSourceFile. moduleName) / \( exportedSourceFile. fileName. basename) "
110-
111- case . filePath:
112- fileName = exportedSourceFile. fileName
113- }
114105
115106 // Find the node's offset relative to its root.
116107 let rawPosition : AbsolutePosition
@@ -131,8 +122,37 @@ extension SourceManager.MacroExpansionContext: MacroExpansionContext {
131122 let offsetWithinSyntaxNode =
132123 rawPosition. utf8Offset - node. position. utf8Offset
133124
125+ var location = exportedSourceFile. sourceLocationConverter. location (
126+ for: rootPosition. advanced ( by: offsetWithinSyntaxNode)
127+ )
128+
129+ switch filePathMode {
130+ case . fileID:
131+ // The `SourceLocationConverter` in `exportedSourceFile` uses `filePath` as the file mode. When the `fileID` mode
132+ // is requested, we need to adjust the file and presumed file to the `fileID`.
133+ let fileID = " \( exportedSourceFile. moduleName) / \( exportedSourceFile. fileName. basename) "
134+ var adjustedFile = location. file
135+ if adjustedFile == exportedSourceFile. fileName {
136+ adjustedFile = fileID
137+ }
138+ var adjustePresumedFile = location. presumedFile
139+ if adjustePresumedFile == exportedSourceFile. fileName {
140+ adjustePresumedFile = fileID
141+ }
142+ location = SourceLocation (
143+ line: location. line,
144+ column: location. column,
145+ offset: location. offset,
146+ file: adjustedFile,
147+ presumedLine: location. presumedLine,
148+ presumedFile: adjustePresumedFile
149+ )
150+
151+ case . filePath:
152+ break
153+ }
154+
134155 // Do the location lookup.
135- let converter = SourceLocationConverter ( file: fileName, tree: sourceFile)
136- return AbstractSourceLocation ( converter. location ( for: rootPosition. advanced ( by: offsetWithinSyntaxNode) ) )
156+ return AbstractSourceLocation ( location)
137157 }
138158}
0 commit comments