@@ -15,6 +15,7 @@ import BasicBridging
1515@_spi ( PluginMessage) @_spi ( ExperimentalLanguageFeature) import SwiftCompilerPluginMessageHandling
1616import SwiftDiagnostics
1717import SwiftOperators
18+ import SwiftParser
1819import SwiftSyntax
1920import SwiftSyntaxBuilder
2021@_spi ( ExperimentalLanguageFeature) @_spi ( Compiler) import SwiftSyntaxMacroExpansion
@@ -243,8 +244,8 @@ func checkDefaultArgumentMacroExpression(
243244@_cdecl ( " swift_ASTGen_checkMacroDefinition " )
244245func checkMacroDefinition(
245246 diagEnginePtr: UnsafeMutableRawPointer ,
246- sourceFilePtr : UnsafeRawPointer ,
247- macroLocationPtr : UnsafePointer < UInt8 > ,
247+ sourceFileBuffer : BridgedStringRef ,
248+ macroDeclText : BridgedStringRef ,
248249 externalMacroOutPtr: UnsafeMutablePointer < BridgedStringRef > ,
249250 replacementsPtr: UnsafeMutablePointer < UnsafeMutablePointer < Int > ? > ,
250251 numReplacementsPtr: UnsafeMutablePointer < Int > ,
@@ -255,20 +256,25 @@ func checkMacroDefinition(
255256 assert ( externalMacroOutPtr. pointee. isEmptyInitialized)
256257 assert ( replacementsPtr. pointee == nil && numReplacementsPtr. pointee == 0 )
257258
258- let sourceFilePtr = sourceFilePtr. bindMemory ( to: ExportedSourceFile . self, capacity: 1 )
259-
260- // Find the macro declaration.
261- guard
262- let macroDecl = findSyntaxNodeInSourceFile (
263- sourceFilePtr: sourceFilePtr,
264- sourceLocationPtr: macroLocationPtr,
265- type: MacroDeclSyntax . self
266- )
267- else {
259+ // Parse 'macro' decl.
260+ // FIXME: Use 'ExportedSourceFile' when C++ parser is replaced.
261+ let textBuffer = UnsafeBufferPointer < UInt8 > ( start: macroDeclText. data, count: macroDeclText. count)
262+ var parser = Parser ( textBuffer)
263+ guard let macroDecl = DeclSyntax . parse ( from: & parser) . as ( MacroDeclSyntax . self) else {
268264 // FIXME: Produce an error
269265 return - 1
270266 }
271267
268+ func diagnose( diagnostic: Diagnostic ) {
269+ emitDiagnostic (
270+ diagnosticEngine: BridgedDiagnosticEngine ( raw: diagEnginePtr) ,
271+ sourceFileBuffer: UnsafeBufferPointer ( start: sourceFileBuffer. data, count: sourceFileBuffer. count) ,
272+ sourceFileBufferOffset: macroDeclText. data! - sourceFileBuffer. data!,
273+ diagnostic: diagnostic,
274+ diagnosticSeverity: diagnostic. diagMessage. severity
275+ )
276+ }
277+
272278 // Check the definition
273279 do {
274280 let definition = try macroDecl. checkDefinition ( )
@@ -299,9 +305,7 @@ func checkMacroDefinition(
299305
300306 default :
301307 // Warn about the unknown builtin.
302- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
303- srcMgr. insert ( sourceFilePtr)
304- srcMgr. diagnose (
308+ diagnose (
305309 diagnostic: . init(
306310 node: node,
307311 message: ASTGenMacroDiagnostic . unknownBuiltin ( type)
@@ -321,9 +325,7 @@ func checkMacroDefinition(
321325 " #externalMacro(module: \( literal: module) , type: \( literal: type) ) "
322326
323327 // Warn about the use of old-style external macro syntax here.
324- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
325- srcMgr. insert ( sourceFilePtr)
326- srcMgr. diagnose (
328+ diagnose (
327329 diagnostic: . init(
328330 node: node,
329331 message: ASTGenMacroDiagnostic . oldStyleExternalMacro,
@@ -350,9 +352,7 @@ func checkMacroDefinition(
350352 firstArgLabel == " module " ,
351353 let module = identifierFromStringLiteral ( firstArg. expression)
352354 else {
353- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
354- srcMgr. insert ( sourceFilePtr)
355- srcMgr. diagnose (
355+ diagnose (
356356 diagnostic: . init(
357357 node: Syntax ( expansionSyntax) ,
358358 message: ASTGenMacroDiagnostic . notStringLiteralArgument ( " module " )
@@ -367,9 +367,7 @@ func checkMacroDefinition(
367367 secondArgLabel == " type " ,
368368 let type = identifierFromStringLiteral ( secondArg. expression)
369369 else {
370- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
371- srcMgr. insert ( sourceFilePtr)
372- srcMgr. diagnose (
370+ diagnose (
373371 diagnostic: . init(
374372 node: Syntax ( expansionSyntax) ,
375373 message: ASTGenMacroDiagnostic . notStringLiteralArgument ( " type " )
@@ -431,16 +429,12 @@ func checkMacroDefinition(
431429#endif
432430 }
433431 } catch let errDiags as DiagnosticsError {
434- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
435- srcMgr. insert ( sourceFilePtr)
436432 for diag in errDiags. diagnostics {
437- srcMgr . diagnose ( diagnostic: diag)
433+ diagnose ( diagnostic: diag)
438434 }
439435 return - 1
440436 } catch let error {
441- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
442- srcMgr. insert ( sourceFilePtr)
443- srcMgr. diagnose (
437+ diagnose (
444438 diagnostic: . init(
445439 node: Syntax ( macroDecl) ,
446440 message: ASTGenMacroDiagnostic . thrownError ( error)
0 commit comments