Skip to content

Commit 00b24c9

Browse files
committed
Fix SILDebugScopes for closures expanded from an expression macro.
An expression macro can expand not just to code inside the function, but also to a closure. Such a closure needs to be treated similar to any functions generated from a freestanding macro: Its instructions should have locations that point straight into the macro buffer. Instructions that are expanded into the same function as the macro expansion can be represented using inline locations pointing back to the macro expansion, but this is not an option for top-level function declaration. #65484 rdar://108618562
1 parent 01086bc commit 00b24c9

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,16 @@ struct MacroInfo {
232232
};
233233
}
234234

235+
static DeclContext *getInnermostFunctionContext(DeclContext *DC) {
236+
for (; DC; DC = DC->getParent())
237+
if (DC->getContextKind() == DeclContextKind::AbstractFunctionDecl)
238+
return DC;
239+
return nullptr;
240+
}
241+
235242
/// Return location of the macro expansion and the macro name.
236-
static MacroInfo getMacroInfo(GeneratedSourceInfo &Info) {
243+
static MacroInfo getMacroInfo(GeneratedSourceInfo &Info,
244+
DeclContext *FunctionDC) {
237245
MacroInfo Result(Info.generatedSourceRange.getStart(),
238246
Info.originalSourceRange.getStart());
239247
if (!Info.astNode)
@@ -252,6 +260,13 @@ static MacroInfo getMacroInfo(GeneratedSourceInfo &Info) {
252260
Result.ExpansionLoc = RegularLocation(decl);
253261
Result.Name = mangler.mangleMacroExpansion(decl);
254262
}
263+
// If the parent function of the macro expansion expression is not the
264+
// current function, then the macro expanded to a closure or nested
265+
// function. As far as the generated SIL is concerned this is the same as a
266+
// function generated from a freestanding macro expansion.
267+
DeclContext *MacroContext = getInnermostFunctionContext(Info.declContext);
268+
if (MacroContext != FunctionDC)
269+
Result.Freestanding = true;
255270
break;
256271
}
257272
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
@@ -295,7 +310,7 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
295310
// declaration that isn't part of a real function. By not handling them here,
296311
// source locations will still point into the macro expansion buffer, but
297312
// debug info doesn't know what macro that buffer was expanded from.
298-
auto Macro = getMacroInfo(*GeneratedSourceInfo);
313+
auto Macro = getMacroInfo(*GeneratedSourceInfo, FunctionDC);
299314
if (Macro.Freestanding)
300315
return nullptr;
301316

test/Macros/macro_expand_closure.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,26 @@
66
// RUN: %target-codesign %t/main
77
// RUN: %target-run %t/main | %FileCheck %s
88

9+
10+
// Debug info SIL testing
11+
// 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
12+
13+
914
@freestanding(expression) public macro multiStatement() -> Int = #externalMacro(module: "MacroDefinition", type: "MultiStatementClosure")
1015

1116
func multiStatementInference() -> Int {
1217
#multiStatement()
1318
}
1419

20+
// The closure intruduced by the macro expansion should not contain any inline
21+
// locations, but instead point directly into the macro buffer.
22+
// CHECK-SIL: sil_scope [[S0:[0-9]+]] { loc "@__swiftmacro_9MacroUser23multiStatementInferenceSiyF0cD0fMf_.swift":1:1 parent @$s9MacroUser23multiStatementInferenceSiyFSiyXEfU_
23+
// CHECK-SIL: sil_scope [[S1:[0-9]+]] { loc "@__swiftmacro_9MacroUser23multiStatementInferenceSiyF0cD0fMf_.swift":2:7 parent [[S0]] }
24+
// CHECK-SIL: sil_scope [[S2:[0-9]+]] { loc "@__swiftmacro_9MacroUser23multiStatementInferenceSiyF0cD0fMf_.swift":2:14 parent [[S1]] }
25+
26+
// CHECK-SIL: sil {{.*}} @$s9MacroUser23multiStatementInferenceSiyFSiyXEfU_
27+
// CHECK-SIL-NOT: return
28+
// CHECK-SIL: %0 = integer_literal $Builtin.Int64, 10, loc "@__swiftmacro_9MacroUser23multiStatementInferenceSiyF0cD0fMf_.swift":2:14, scope [[S2]]
29+
1530
// CHECK: 10
1631
print(multiStatementInference())

0 commit comments

Comments
 (0)