Skip to content

Commit 9de913d

Browse files
authored
Merge pull request #73257 from rintaro/6.0-macros-checkdefinition-nosourcefile
[6.0][Macros] Don't use 'ExportedSourceFile' for macro definition checking
2 parents 838530f + a56276b commit 9de913d

File tree

5 files changed

+50
-41
lines changed

5 files changed

+50
-41
lines changed

include/swift/Bridging/ASTGen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ bool swift_ASTGen_checkDefaultArgumentMacroExpression(
7373
const void *_Nonnull macroSourceLocation);
7474

7575
ptrdiff_t swift_ASTGen_checkMacroDefinition(
76-
void *_Nonnull diagEngine, void *_Nonnull sourceFile,
77-
const void *_Nonnull macroSourceLocation,
76+
void *_Nonnull diagEngine, BridgedStringRef sourceFileBuffer,
77+
BridgedStringRef macroDeclText,
7878
BridgedStringRef *_Nonnull expansionSourceOutPtr,
7979
ptrdiff_t *_Nullable *_Nonnull replacementsPtr,
8080
ptrdiff_t *_Nonnull numReplacements,

lib/ASTGen/Sources/ASTGen/DiagnosticsBridge.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ fileprivate func emitDiagnosticParts(
2121
message: String,
2222
severity: DiagnosticSeverity,
2323
position: AbsolutePosition,
24+
offset: Int,
2425
highlights: [Syntax] = [],
2526
fixItChanges: [FixIt.Change] = []
2627
) {
2728
// Map severity
2829
let bridgedSeverity = severity.bridged
2930

3031
func bridgedSourceLoc(at position: AbsolutePosition) -> BridgedSourceLoc {
31-
return BridgedSourceLoc(at: position, in: sourceFileBuffer)
32+
return BridgedSourceLoc(at: position.advanced(by: offset), in: sourceFileBuffer)
3233
}
3334

3435
// Emit the diagnostic
@@ -96,6 +97,7 @@ fileprivate func emitDiagnosticParts(
9697
func emitDiagnostic(
9798
diagnosticEngine: BridgedDiagnosticEngine,
9899
sourceFileBuffer: UnsafeBufferPointer<UInt8>,
100+
sourceFileBufferOffset: Int = 0,
99101
diagnostic: Diagnostic,
100102
diagnosticSeverity: DiagnosticSeverity,
101103
messageSuffix: String? = nil
@@ -107,6 +109,7 @@ func emitDiagnostic(
107109
message: diagnostic.diagMessage.message + (messageSuffix ?? ""),
108110
severity: diagnosticSeverity,
109111
position: diagnostic.position,
112+
offset: sourceFileBufferOffset,
110113
highlights: diagnostic.highlights
111114
)
112115

@@ -118,6 +121,7 @@ func emitDiagnostic(
118121
message: fixIt.message.message,
119122
severity: .note,
120123
position: diagnostic.position,
124+
offset: sourceFileBufferOffset,
121125
fixItChanges: fixIt.changes
122126
)
123127
}
@@ -129,7 +133,8 @@ func emitDiagnostic(
129133
sourceFileBuffer: sourceFileBuffer,
130134
message: note.message,
131135
severity: .note,
132-
position: note.position
136+
position: note.position,
137+
offset: sourceFileBufferOffset
133138
)
134139
}
135140
}

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import BasicBridging
1515
@_spi(PluginMessage) @_spi(ExperimentalLanguageFeature) import SwiftCompilerPluginMessageHandling
1616
import SwiftDiagnostics
1717
import SwiftOperators
18+
import SwiftParser
1819
import SwiftSyntax
1920
import SwiftSyntaxBuilder
2021
@_spi(ExperimentalLanguageFeature) @_spi(Compiler) import SwiftSyntaxMacroExpansion
@@ -243,8 +244,8 @@ func checkDefaultArgumentMacroExpression(
243244
@_cdecl("swift_ASTGen_checkMacroDefinition")
244245
func 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)

lib/Sema/TypeCheckMacros.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "swift/AST/PrettyStackTrace.h"
3434
#include "swift/AST/SourceFile.h"
3535
#include "swift/AST/TypeCheckRequests.h"
36+
#include "swift/Basic/BasicBridging.h"
3637
#include "swift/Basic/Defer.h"
3738
#include "swift/Basic/Lazy.h"
3839
#include "swift/Basic/SourceManager.h"
@@ -150,11 +151,23 @@ MacroDefinition MacroDefinitionRequest::evaluate(
150151
ptrdiff_t numReplacements = 0;
151152
ptrdiff_t *genericReplacements = nullptr;
152153
ptrdiff_t numGenericReplacements = 0;
154+
155+
// Parse 'macro' decl in swift_ASTGen_checkMacroDefinition.
156+
// NOTE: We don't use source->getExportedSourceFile() because it parses the
157+
// entire source buffer, which we don't want. Usually 'macro' decl is not in
158+
// the same file as the expansion, so we only want to parse only the decl.
159+
// FIXME: When we migrate to SwiftParser, use the parsed syntax tree.
160+
auto &SM = ctx.SourceMgr;
161+
StringRef sourceFileText =
162+
SM.getEntireTextForBuffer(*sourceFile->getBufferID());
163+
StringRef macroDeclText =
164+
SM.extractText(Lexer::getCharSourceRangeFromSourceRange(
165+
SM, macro->getSourceRangeIncludingAttrs()));
166+
153167
auto checkResult = swift_ASTGen_checkMacroDefinition(
154-
&ctx.Diags, sourceFile->getExportedSourceFile(),
155-
macro->getLoc().getOpaquePointerValue(), &externalMacroName,
156-
&replacements, &numReplacements,
157-
&genericReplacements, &numGenericReplacements);
168+
&ctx.Diags, sourceFileText, macroDeclText, &externalMacroName,
169+
&replacements, &numReplacements, &genericReplacements,
170+
&numGenericReplacements);
158171

159172
// Clean up after the call.
160173
SWIFT_DEFER {

test/Macros/lazy_parsing.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
// RUN: %empty-directory(%t/stats-lookup)
99
// RUN: split-file %s %t
1010

11-
// rdar://124548628 (additional ExportedSourceFileRequest(s) in `lazy_parsing.swift` (NCGenerics Enablement))
12-
// XFAIL: *
13-
1411
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath
1512

1613
// RUN: %target-swift-frontend -typecheck -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -stats-output-dir %t/stats-no-lookup -primary-file %t/b.swift %t/a.swift

0 commit comments

Comments
 (0)