Skip to content

Fix SILDebugScopes for closures expanded from an expression macro. #65742

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 1 commit into from
May 7, 2023
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
19 changes: 17 additions & 2 deletions lib/SILGen/SILGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,16 @@ struct MacroInfo {
};
}

static DeclContext *getInnermostFunctionContext(DeclContext *DC) {
for (; DC; DC = DC->getParent())
if (DC->getContextKind() == DeclContextKind::AbstractFunctionDecl)
return DC;
return nullptr;
}

/// Return location of the macro expansion and the macro name.
static MacroInfo getMacroInfo(GeneratedSourceInfo &Info) {
static MacroInfo getMacroInfo(GeneratedSourceInfo &Info,
DeclContext *FunctionDC) {
MacroInfo Result(Info.generatedSourceRange.getStart(),
Info.originalSourceRange.getStart());
if (!Info.astNode)
Expand All @@ -252,6 +260,13 @@ static MacroInfo getMacroInfo(GeneratedSourceInfo &Info) {
Result.ExpansionLoc = RegularLocation(decl);
Result.Name = mangler.mangleMacroExpansion(decl);
}
// If the parent function of the macro expansion expression is not the
// current function, then the macro expanded to a closure or nested
// function. As far as the generated SIL is concerned this is the same as a
// function generated from a freestanding macro expansion.
DeclContext *MacroContext = getInnermostFunctionContext(Info.declContext);
if (MacroContext != FunctionDC)
Result.Freestanding = true;
break;
}
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
Expand Down Expand Up @@ -295,7 +310,7 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
// declaration that isn't part of a real function. By not handling them here,
// source locations will still point into the macro expansion buffer, but
// debug info doesn't know what macro that buffer was expanded from.
auto Macro = getMacroInfo(*GeneratedSourceInfo);
auto Macro = getMacroInfo(*GeneratedSourceInfo, FunctionDC);
if (Macro.Freestanding)
return nullptr;

Expand Down
15 changes: 15 additions & 0 deletions test/Macros/macro_expand_closure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@
// RUN: %target-codesign %t/main
// RUN: %target-run %t/main | %FileCheck %s


// Debug info SIL testing
// RUN: %target-swift-frontend -swift-version 5 -emit-sil -enable-experimental-feature FreestandingMacros -load-plugin-library %t/%target-library-name(MacroDefinition) %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-SIL %s


@freestanding(expression) public macro multiStatement() -> Int = #externalMacro(module: "MacroDefinition", type: "MultiStatementClosure")

func multiStatementInference() -> Int {
#multiStatement()
}

// The closure intruduced by the macro expansion should not contain any inline
// locations, but instead point directly into the macro buffer.
// CHECK-SIL: sil_scope [[S0:[0-9]+]] { loc "@__swiftmacro_9MacroUser23multiStatementInferenceSiyF0cD0fMf_.swift":1:1 parent @$s9MacroUser23multiStatementInferenceSiyFSiyXEfU_
// CHECK-SIL: sil_scope [[S1:[0-9]+]] { loc "@__swiftmacro_9MacroUser23multiStatementInferenceSiyF0cD0fMf_.swift":2:7 parent [[S0]] }
// CHECK-SIL: sil_scope [[S2:[0-9]+]] { loc "@__swiftmacro_9MacroUser23multiStatementInferenceSiyF0cD0fMf_.swift":2:14 parent [[S1]] }

// CHECK-SIL: sil {{.*}} @$s9MacroUser23multiStatementInferenceSiyFSiyXEfU_
// CHECK-SIL-NOT: return
// CHECK-SIL: %0 = integer_literal $Builtin.Int64, 10, loc "@__swiftmacro_9MacroUser23multiStatementInferenceSiyF0cD0fMf_.swift":2:14, scope [[S2]]

// CHECK: 10
print(multiStatementInference())