@@ -15,6 +15,7 @@ import BasicBridging
15
15
@_spi ( PluginMessage) @_spi ( ExperimentalLanguageFeature) import SwiftCompilerPluginMessageHandling
16
16
import SwiftDiagnostics
17
17
import SwiftOperators
18
+ import SwiftParser
18
19
import SwiftSyntax
19
20
import SwiftSyntaxBuilder
20
21
@_spi ( ExperimentalLanguageFeature) @_spi ( Compiler) import SwiftSyntaxMacroExpansion
@@ -243,8 +244,8 @@ func checkDefaultArgumentMacroExpression(
243
244
@_cdecl ( " swift_ASTGen_checkMacroDefinition " )
244
245
func checkMacroDefinition(
245
246
diagEnginePtr: UnsafeMutableRawPointer ,
246
- sourceFilePtr : UnsafeRawPointer ,
247
- macroLocationPtr : UnsafePointer < UInt8 > ,
247
+ sourceFileBuffer : BridgedStringRef ,
248
+ macroDeclText : BridgedStringRef ,
248
249
externalMacroOutPtr: UnsafeMutablePointer < BridgedStringRef > ,
249
250
replacementsPtr: UnsafeMutablePointer < UnsafeMutablePointer < Int > ? > ,
250
251
numReplacementsPtr: UnsafeMutablePointer < Int > ,
@@ -255,20 +256,25 @@ func checkMacroDefinition(
255
256
assert ( externalMacroOutPtr. pointee. isEmptyInitialized)
256
257
assert ( replacementsPtr. pointee == nil && numReplacementsPtr. pointee == 0 )
257
258
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 {
268
264
// FIXME: Produce an error
269
265
return - 1
270
266
}
271
267
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
+
272
278
// Check the definition
273
279
do {
274
280
let definition = try macroDecl. checkDefinition ( )
@@ -299,9 +305,7 @@ func checkMacroDefinition(
299
305
300
306
default :
301
307
// Warn about the unknown builtin.
302
- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
303
- srcMgr. insert ( sourceFilePtr)
304
- srcMgr. diagnose (
308
+ diagnose (
305
309
diagnostic: . init(
306
310
node: node,
307
311
message: ASTGenMacroDiagnostic . unknownBuiltin ( type)
@@ -321,9 +325,7 @@ func checkMacroDefinition(
321
325
" #externalMacro(module: \( literal: module) , type: \( literal: type) ) "
322
326
323
327
// 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 (
327
329
diagnostic: . init(
328
330
node: node,
329
331
message: ASTGenMacroDiagnostic . oldStyleExternalMacro,
@@ -350,9 +352,7 @@ func checkMacroDefinition(
350
352
firstArgLabel == " module " ,
351
353
let module = identifierFromStringLiteral ( firstArg. expression)
352
354
else {
353
- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
354
- srcMgr. insert ( sourceFilePtr)
355
- srcMgr. diagnose (
355
+ diagnose (
356
356
diagnostic: . init(
357
357
node: Syntax ( expansionSyntax) ,
358
358
message: ASTGenMacroDiagnostic . notStringLiteralArgument ( " module " )
@@ -367,9 +367,7 @@ func checkMacroDefinition(
367
367
secondArgLabel == " type " ,
368
368
let type = identifierFromStringLiteral ( secondArg. expression)
369
369
else {
370
- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
371
- srcMgr. insert ( sourceFilePtr)
372
- srcMgr. diagnose (
370
+ diagnose (
373
371
diagnostic: . init(
374
372
node: Syntax ( expansionSyntax) ,
375
373
message: ASTGenMacroDiagnostic . notStringLiteralArgument ( " type " )
@@ -431,16 +429,12 @@ func checkMacroDefinition(
431
429
#endif
432
430
}
433
431
} catch let errDiags as DiagnosticsError {
434
- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
435
- srcMgr. insert ( sourceFilePtr)
436
432
for diag in errDiags. diagnostics {
437
- srcMgr . diagnose ( diagnostic: diag)
433
+ diagnose ( diagnostic: diag)
438
434
}
439
435
return - 1
440
436
} catch let error {
441
- let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
442
- srcMgr. insert ( sourceFilePtr)
443
- srcMgr. diagnose (
437
+ diagnose (
444
438
diagnostic: . init(
445
439
node: Syntax ( macroDecl) ,
446
440
message: ASTGenMacroDiagnostic . thrownError ( error)
0 commit comments