Skip to content

Commit e51937f

Browse files
committed
Don't emit freestanding macros in inlined scopes
1 parent bccc080 commit e51937f

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -220,35 +220,44 @@ const SILDebugScope *SILGenFunction::getOrCreateScope(SourceLoc SLoc) {
220220
return Scope;
221221
}
222222

223-
static std::pair<SILLocation, std::string>
224-
getMacroName(GeneratedSourceInfo &Info) {
223+
namespace {
224+
struct MacroInfo {
225+
MacroInfo(SourceLoc SLoc) : Loc(SLoc) {}
226+
RegularLocation Loc;
227+
std::string Name;
228+
bool Freestanding = false;
229+
};
230+
}
231+
232+
static MacroInfo getMacroInfo(GeneratedSourceInfo &Info) {
225233
SourceLoc MacroSLoc = Info.generatedSourceRange.getStart();
226-
RegularLocation Loc(MacroSLoc);
227-
Mangle::ASTMangler mangler;
228-
std::string Name = "__unknown_macro__";
234+
MacroInfo Result(MacroSLoc);
235+
Result.Name = "__unknown_macro__";
229236
if (!Info.astNode)
230-
return {Loc, Name};
237+
return Result;
231238

232239
// Keep this in sync with ASTMangler::appendMacroExpansionContext().
240+
Mangle::ASTMangler mangler;
233241
switch (Info.kind) {
234242
case GeneratedSourceInfo::ExpressionMacroExpansion: {
235243
auto parent = ASTNode::getFromOpaqueValue(Info.astNode);
236244
if (auto expr =
237245
cast_or_null<MacroExpansionExpr>(parent.dyn_cast<Expr *>())) {
238-
Loc = RegularLocation(expr);
239-
Name = mangler.mangleMacroExpansion(expr);
246+
Result.Loc = RegularLocation(expr);
247+
Result.Name = mangler.mangleMacroExpansion(expr);
240248
} else {
241249
auto decl = cast<MacroExpansionDecl>(parent.get<Decl *>());
242-
Loc = RegularLocation(decl);
243-
Name = mangler.mangleMacroExpansion(decl);
250+
Result.Loc = RegularLocation(decl);
251+
Result.Name = mangler.mangleMacroExpansion(decl);
244252
}
245253
break;
246254
}
247255
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
248256
auto expansion = cast<MacroExpansionDecl>(
249257
ASTNode::getFromOpaqueValue(Info.astNode).get<Decl *>());
250-
Loc = RegularLocation(expansion);
251-
Name = mangler.mangleMacroExpansion(expansion);
258+
Result.Loc = RegularLocation(expansion);
259+
Result.Name = mangler.mangleMacroExpansion(expansion);
260+
Result.Freestanding = true;
252261
break;
253262
}
254263
case GeneratedSourceInfo::AccessorMacroExpansion:
@@ -259,16 +268,17 @@ getMacroName(GeneratedSourceInfo &Info) {
259268
auto decl = ASTNode::getFromOpaqueValue(Info.astNode).get<Decl *>();
260269
auto attr = Info.attachedMacroCustomAttr;
261270
if (auto *macroDecl = decl->getResolvedMacro(attr)) {
262-
Loc = RegularLocation(macroDecl);
263-
Name = macroDecl->getBaseName().userFacingName();
271+
Result.Loc = RegularLocation(macroDecl);
272+
Result.Name = macroDecl->getBaseName().userFacingName();
273+
Result.Freestanding = true;
264274
}
265275
break;
266276
}
267277
case GeneratedSourceInfo::PrettyPrinted:
268278
case GeneratedSourceInfo::ReplacedFunctionBody:
269279
break;
270280
}
271-
return {Loc, Name};
281+
return Result;
272282
}
273283

274284
const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
@@ -278,6 +288,15 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
278288
if (!GeneratedSourceInfo)
279289
return nullptr;
280290

291+
// There is no good way to represent freestanding macros as inlined functions,
292+
// because entire function would need to be "inlined" into a top-level
293+
// declaration that isn't part of a real function. By not handling them here,
294+
// source locations will still point into the macro expansion buffer, but
295+
// debug info doesn't know what macro that buffer was expanded from.
296+
auto Macro = getMacroInfo(*GeneratedSourceInfo);
297+
if (Macro.Freestanding)
298+
return nullptr;
299+
281300
SourceLoc OrigSLoc = GeneratedSourceInfo->originalSourceRange.getStart();
282301
if (!OrigSLoc)
283302
return nullptr;
@@ -298,22 +317,24 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
298317
/*yields*/
299318
{},
300319
/*Results*/ {}, None, SubstitutionMap(), SubstitutionMap(), ASTContext);
301-
auto LocName = getMacroName(*GeneratedSourceInfo);
302-
RegularLocation MacroLoc(LocName.first);
303-
StringRef MacroName = ASTContext.getIdentifier(LocName.second).str();
320+
StringRef MacroName = ASTContext.getIdentifier(Macro.Name).str();
321+
304322
SILFunction *MacroFn = B.getOrCreateFunction(
305-
MacroLoc, MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
323+
Macro.Loc, MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
306324
IsNotBare, IsNotTransparent, IsNotSerialized, IsNotDynamic,
307325
IsNotDistributed, IsNotRuntimeAccessible);
308326
// At the end of the chain OrigSLoc should be a macro expansion node.
309-
const SILDebugScope *InlinedAt = getOrCreateScope(OrigSLoc);
327+
const SILDebugScope *InlinedAt = nullptr;
328+
const SILDebugScope *OrigScope = getOrCreateScope(OrigSLoc);
329+
RegularLocation OrigLoc(OrigSLoc);
310330
// Inject an extra scope to hold the inlined call site.
311-
if (InlinedAt)
312-
InlinedAt =
313-
new (SGM.M) SILDebugScope(RegularLocation(OrigSLoc), nullptr, InlinedAt,
314-
InlinedAt->InlinedCallSite);
331+
if (OrigScope)
332+
InlinedAt = new (SGM.M)
333+
SILDebugScope(Macro.Freestanding ? Macro.Loc : OrigLoc, nullptr,
334+
OrigScope, OrigScope->InlinedCallSite);
335+
315336
const SILDebugScope *Scope =
316-
new (SGM.M) SILDebugScope(MacroLoc, MacroFn, nullptr, InlinedAt);
337+
new (SGM.M) SILDebugScope(Macro.Loc, MacroFn, nullptr, InlinedAt);
317338

318339
InlinedScopeMap.insert({BufferID, Scope});
319340
return Scope;

test/Macros/macro_expand.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ func testNestedDeclInExpr() {
244244
@freestanding(declaration, names: named(A), named(B), named(foo), named(addOne))
245245
macro defineDeclsWithKnownNames() = #externalMacro(module: "MacroDefinition", type: "DefineDeclsWithKnownNamesMacro")
246246

247+
// Freestanding macros are not in inlined scopes.
248+
// CHECK-SIL: sil_scope {{.*}} { loc "@__swiftmacro_9MacroUser016testFreestandingA9ExpansionyyF4Foo2L_V25defineDeclsWithKnownNamesfMf0_.swift":9:14 {{.*}} -> Int }
249+
247250
// FIXME: Macros producing arbitrary names are not supported yet
248251
#if false
249252
#bitwidthNumberedStructs("MyIntGlobal")

0 commit comments

Comments
 (0)