Skip to content

Commit 08c5553

Browse files
authored
Merge pull request #62911 from DougGregor/parent-source-file-in-generated-buffers
Teach DeclContext::getParentSourceFile() to return the innermost source file
2 parents f72816e + ab4ecc3 commit 08c5553

File tree

6 files changed

+66
-6
lines changed

6 files changed

+66
-6
lines changed

lib/AST/Decl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,11 +2027,11 @@ SourceLoc PatternBindingDecl::getEqualLoc(unsigned i) const {
20272027
}
20282028

20292029
SourceLoc TopLevelCodeDecl::getStartLoc() const {
2030-
return Body->getStartLoc();
2030+
return Body ? Body->getStartLoc() : SourceLoc();
20312031
}
20322032

20332033
SourceRange TopLevelCodeDecl::getSourceRange() const {
2034-
return Body->getSourceRange();
2034+
return Body? Body->getSourceRange() : SourceRange();
20352035
}
20362036

20372037
SourceRange IfConfigDecl::getSourceRange() const {

lib/AST/DeclContext.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,44 @@ ModuleDecl *DeclContext::getParentModule() const {
283283

284284
SourceFile *DeclContext::getParentSourceFile() const {
285285
const DeclContext *DC = this;
286-
while (!DC->isModuleScopeContext())
286+
SourceLoc loc;
287+
while (!DC->isModuleScopeContext()) {
288+
// If we don't have a source location yet, try to grab one from this
289+
// context.
290+
if (loc.isInvalid()) {
291+
switch (DC->getContextKind()) {
292+
case DeclContextKind::AbstractClosureExpr:
293+
loc = cast<AbstractClosureExpr>(DC)->getLoc();
294+
break;
295+
296+
case DeclContextKind::AbstractFunctionDecl:
297+
case DeclContextKind::EnumElementDecl:
298+
case DeclContextKind::ExtensionDecl:
299+
case DeclContextKind::GenericTypeDecl:
300+
case DeclContextKind::MacroDecl:
301+
case DeclContextKind::SubscriptDecl:
302+
case DeclContextKind::TopLevelCodeDecl:
303+
loc = DC->getAsDecl()->getLoc(/*SerializedOK=*/false);
304+
break;
305+
306+
case DeclContextKind::Initializer:
307+
case DeclContextKind::FileUnit:
308+
case DeclContextKind::Module:
309+
case DeclContextKind::SerializedLocal:
310+
break;
311+
}
312+
}
313+
287314
DC = DC->getParent();
288-
return const_cast<SourceFile *>(dyn_cast<SourceFile>(DC));
315+
}
316+
317+
auto fallbackSF = const_cast<SourceFile *>(dyn_cast<SourceFile>(DC));
318+
if (auto module = DC->getParentModule()) {
319+
if (auto sf = module->getSourceFileContainingLocation(loc))
320+
return sf;
321+
}
322+
323+
return fallbackSF;
289324
}
290325

291326
DeclContext *DeclContext::getModuleScopeContext() const {

lib/AST/Expr.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,15 +2024,19 @@ swift::_getRef__AbstractClosureExpr_getActorIsolation() {
20242024

20252025
#define FORWARD_SOURCE_LOCS_TO(CLASS, NODE) \
20262026
SourceRange CLASS::getSourceRange() const { \
2027+
if (!NODE) { return SourceRange(); } \
20272028
return (NODE)->getSourceRange(); \
20282029
} \
20292030
SourceLoc CLASS::getStartLoc() const { \
2031+
if (!NODE) { return SourceLoc(); } \
20302032
return (NODE)->getStartLoc(); \
20312033
} \
20322034
SourceLoc CLASS::getEndLoc() const { \
2035+
if (!NODE) { return SourceLoc(); } \
20332036
return (NODE)->getEndLoc(); \
20342037
} \
20352038
SourceLoc CLASS::getLoc() const { \
2039+
if (!NODE) { return SourceLoc(); } \
20362040
return (NODE)->getStartLoc(); \
20372041
}
20382042

lib/AST/Module.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,8 @@ void ModuleDecl::updateSourceFileLocationMap() {
573573
}
574574

575575
// If we are up-to-date, there's nothing to do.
576-
if (sourceFileLocationMap->numFiles == getFiles().size() &&
576+
ArrayRef<FileUnit *> files = Files;
577+
if (sourceFileLocationMap->numFiles == files.size() &&
577578
sourceFileLocationMap->numAuxiliaryFiles ==
578579
AuxiliaryFiles.size())
579580
return;
@@ -582,7 +583,7 @@ void ModuleDecl::updateSourceFileLocationMap() {
582583
sourceFileLocationMap->allSourceFiles.clear();
583584

584585
// First, add all of the source files with a backing buffer.
585-
for (auto *fileUnit : getFiles()) {
586+
for (auto *fileUnit : files) {
586587
if (auto sourceFile = dyn_cast<SourceFile>(fileUnit)) {
587588
if (sourceFile->getBufferID())
588589
sourceFileLocationMap->allSourceFiles.push_back(sourceFile);

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,16 @@ public class RecursiveMacro: ExpressionMacro {
167167
return "()"
168168
}
169169
}
170+
171+
public class NestedDeclInExprMacro: ExpressionMacro {
172+
public static func expansion(
173+
of macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
174+
) -> ExprSyntax {
175+
return """
176+
{ () -> Void in
177+
struct Foo { }
178+
return ()
179+
}
180+
"""
181+
}
182+
}

test/Macros/macro_expand.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,10 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
9292
#recurse(true) // expected-note{{in expansion of macro 'recurse' here}}
9393
#endif
9494
}
95+
96+
// Make sure we don't crash with declarations produced by expansions.
97+
@expression macro nestedDeclInExpr: () -> Void = #externalMacro(module: "MacroDefinition", type: "NestedDeclInExprMacro")
98+
99+
func testNestedDeclInExpr() {
100+
let _: () -> Void = #nestedDeclInExpr
101+
}

0 commit comments

Comments
 (0)