Skip to content

Teach DeclContext::getParentSourceFile() to return the innermost source file #62911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2034,11 +2034,11 @@ SourceLoc PatternBindingDecl::getEqualLoc(unsigned i) const {
}

SourceLoc TopLevelCodeDecl::getStartLoc() const {
return Body->getStartLoc();
return Body ? Body->getStartLoc() : SourceLoc();
}

SourceRange TopLevelCodeDecl::getSourceRange() const {
return Body->getSourceRange();
return Body? Body->getSourceRange() : SourceRange();
}

SourceRange IfConfigDecl::getSourceRange() const {
Expand Down
39 changes: 37 additions & 2 deletions lib/AST/DeclContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,44 @@ ModuleDecl *DeclContext::getParentModule() const {

SourceFile *DeclContext::getParentSourceFile() const {
const DeclContext *DC = this;
while (!DC->isModuleScopeContext())
SourceLoc loc;
while (!DC->isModuleScopeContext()) {
// If we don't have a source location yet, try to grab one from this
// context.
if (loc.isInvalid()) {
switch (DC->getContextKind()) {
case DeclContextKind::AbstractClosureExpr:
loc = cast<AbstractClosureExpr>(DC)->getLoc();
break;

case DeclContextKind::AbstractFunctionDecl:
case DeclContextKind::EnumElementDecl:
case DeclContextKind::ExtensionDecl:
case DeclContextKind::GenericTypeDecl:
case DeclContextKind::MacroDecl:
case DeclContextKind::SubscriptDecl:
case DeclContextKind::TopLevelCodeDecl:
loc = DC->getAsDecl()->getLoc(/*SerializedOK=*/false);
break;

case DeclContextKind::Initializer:
case DeclContextKind::FileUnit:
case DeclContextKind::Module:
case DeclContextKind::SerializedLocal:
break;
}
}

DC = DC->getParent();
return const_cast<SourceFile *>(dyn_cast<SourceFile>(DC));
}

auto fallbackSF = const_cast<SourceFile *>(dyn_cast<SourceFile>(DC));
if (auto module = DC->getParentModule()) {
if (auto sf = module->getSourceFileContainingLocation(loc))
return sf;
}

return fallbackSF;
}

DeclContext *DeclContext::getModuleScopeContext() const {
Expand Down
4 changes: 4 additions & 0 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2029,15 +2029,19 @@ swift::_getRef__AbstractClosureExpr_getActorIsolation() {

#define FORWARD_SOURCE_LOCS_TO(CLASS, NODE) \
SourceRange CLASS::getSourceRange() const { \
if (!NODE) { return SourceRange(); } \
return (NODE)->getSourceRange(); \
} \
SourceLoc CLASS::getStartLoc() const { \
if (!NODE) { return SourceLoc(); } \
return (NODE)->getStartLoc(); \
} \
SourceLoc CLASS::getEndLoc() const { \
if (!NODE) { return SourceLoc(); } \
return (NODE)->getEndLoc(); \
} \
SourceLoc CLASS::getLoc() const { \
if (!NODE) { return SourceLoc(); } \
return (NODE)->getStartLoc(); \
}

Expand Down
5 changes: 3 additions & 2 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,8 @@ void ModuleDecl::updateSourceFileLocationMap() {
}

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

// First, add all of the source files with a backing buffer.
for (auto *fileUnit : getFiles()) {
for (auto *fileUnit : files) {
if (auto sourceFile = dyn_cast<SourceFile>(fileUnit)) {
if (sourceFile->getBufferID())
sourceFileLocationMap->allSourceFiles.push_back(sourceFile);
Expand Down
13 changes: 13 additions & 0 deletions test/Macros/Inputs/syntax_macro_definitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,16 @@ public class RecursiveMacro: ExpressionMacro {
return "()"
}
}

public class NestedDeclInExprMacro: ExpressionMacro {
public static func expansion(
of macro: MacroExpansionExprSyntax, in context: inout MacroExpansionContext
) -> ExprSyntax {
return """
{ () -> Void in
struct Foo { }
return ()
}
"""
}
}
7 changes: 7 additions & 0 deletions test/Macros/macro_expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
#recurse(true) // expected-note{{in expansion of macro 'recurse' here}}
#endif
}

// Make sure we don't crash with declarations produced by expansions.
@expression macro nestedDeclInExpr: () -> Void = #externalMacro(module: "MacroDefinition", type: "NestedDeclInExprMacro")

func testNestedDeclInExpr() {
let _: () -> Void = #nestedDeclInExpr
}