@@ -220,35 +220,44 @@ const SILDebugScope *SILGenFunction::getOrCreateScope(SourceLoc SLoc) {
220
220
return Scope;
221
221
}
222
222
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) {
225
233
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__" ;
229
236
if (!Info.astNode )
230
- return {Loc, Name} ;
237
+ return Result ;
231
238
232
239
// Keep this in sync with ASTMangler::appendMacroExpansionContext().
240
+ Mangle::ASTMangler mangler;
233
241
switch (Info.kind ) {
234
242
case GeneratedSourceInfo::ExpressionMacroExpansion: {
235
243
auto parent = ASTNode::getFromOpaqueValue (Info.astNode );
236
244
if (auto expr =
237
245
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);
240
248
} else {
241
249
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);
244
252
}
245
253
break ;
246
254
}
247
255
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
248
256
auto expansion = cast<MacroExpansionDecl>(
249
257
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 ;
252
261
break ;
253
262
}
254
263
case GeneratedSourceInfo::AccessorMacroExpansion:
@@ -259,16 +268,17 @@ getMacroName(GeneratedSourceInfo &Info) {
259
268
auto decl = ASTNode::getFromOpaqueValue (Info.astNode ).get <Decl *>();
260
269
auto attr = Info.attachedMacroCustomAttr ;
261
270
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 ;
264
274
}
265
275
break ;
266
276
}
267
277
case GeneratedSourceInfo::PrettyPrinted:
268
278
case GeneratedSourceInfo::ReplacedFunctionBody:
269
279
break ;
270
280
}
271
- return {Loc, Name} ;
281
+ return Result ;
272
282
}
273
283
274
284
const SILDebugScope *SILGenFunction::getMacroScope (SourceLoc SLoc) {
@@ -278,6 +288,15 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
278
288
if (!GeneratedSourceInfo)
279
289
return nullptr ;
280
290
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
+
281
300
SourceLoc OrigSLoc = GeneratedSourceInfo->originalSourceRange .getStart ();
282
301
if (!OrigSLoc)
283
302
return nullptr ;
@@ -298,22 +317,24 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
298
317
/* yields*/
299
318
{},
300
319
/* 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
+
304
322
SILFunction *MacroFn = B.getOrCreateFunction (
305
- MacroLoc , MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
323
+ Macro. Loc , MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
306
324
IsNotBare, IsNotTransparent, IsNotSerialized, IsNotDynamic,
307
325
IsNotDistributed, IsNotRuntimeAccessible);
308
326
// 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);
310
330
// 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
+
315
336
const SILDebugScope *Scope =
316
- new (SGM.M ) SILDebugScope (MacroLoc , MacroFn, nullptr , InlinedAt);
337
+ new (SGM.M ) SILDebugScope (Macro. Loc , MacroFn, nullptr , InlinedAt);
317
338
318
339
InlinedScopeMap.insert ({BufferID, Scope});
319
340
return Scope;
0 commit comments