@@ -176,7 +176,7 @@ SILGenFunction::getSILDebugLocation(SILBuilder &B, SILLocation Loc,
176
176
const SILDebugScope *Scope = B.getCurrentDebugScope ();
177
177
if (!Scope)
178
178
Scope = F.getDebugScope ();
179
- if (auto *SILScope = getScopeOrNull (Loc)) {
179
+ if (auto *SILScope = getScopeOrNull (Loc, ForMetaInstruction )) {
180
180
Scope = SILScope;
181
181
// Metainstructions such as a debug_value may break the flow of scopes and
182
182
// should not change the state of the builder.
@@ -187,14 +187,16 @@ SILGenFunction::getSILDebugLocation(SILBuilder &B, SILLocation Loc,
187
187
return SILDebugLocation (overriddenLoc, Scope);
188
188
}
189
189
190
- const SILDebugScope *SILGenFunction::getScopeOrNull (SILLocation Loc) {
191
- if (Loc.getKind () == SILLocation::CleanupKind ||
192
- Loc.getKind () == SILLocation::ImplicitReturnKind ||
193
- // The source locations produced by the ResultBuilder transformation are
194
- // all over the place.
195
- Loc.isImplicit () ||
196
- Loc.isAutoGenerated ())
197
- return nullptr ;
190
+ const SILDebugScope *SILGenFunction::getScopeOrNull (SILLocation Loc,
191
+ bool ForMetaInstruction) {
192
+ if (!ForMetaInstruction) {
193
+ if (Loc.getKind () == SILLocation::CleanupKind ||
194
+ Loc.getKind () == SILLocation::ImplicitReturnKind ||
195
+ // The source locations produced by the ResultBuilder transformation are
196
+ // all over the place.
197
+ Loc.isImplicit () || Loc.isAutoGenerated ())
198
+ return nullptr ;
199
+ }
198
200
199
201
SourceLoc SLoc = Loc.getSourceLoc ();
200
202
if (!SF || LastSourceLoc == SLoc)
@@ -218,35 +220,44 @@ const SILDebugScope *SILGenFunction::getOrCreateScope(SourceLoc SLoc) {
218
220
return Scope;
219
221
}
220
222
221
- static std::pair<SILLocation, std::string>
222
- 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) {
223
233
SourceLoc MacroSLoc = Info.generatedSourceRange .getStart ();
224
- RegularLocation Loc (MacroSLoc);
225
- Mangle::ASTMangler mangler;
226
- std::string Name = " __unknown_macro__" ;
234
+ MacroInfo Result (MacroSLoc);
235
+ Result.Name = " __unknown_macro__" ;
227
236
if (!Info.astNode )
228
- return {Loc, Name} ;
237
+ return Result ;
229
238
230
239
// Keep this in sync with ASTMangler::appendMacroExpansionContext().
240
+ Mangle::ASTMangler mangler;
231
241
switch (Info.kind ) {
232
242
case GeneratedSourceInfo::ExpressionMacroExpansion: {
233
243
auto parent = ASTNode::getFromOpaqueValue (Info.astNode );
234
244
if (auto expr =
235
245
cast_or_null<MacroExpansionExpr>(parent.dyn_cast <Expr *>())) {
236
- Loc = RegularLocation (expr);
237
- Name = mangler.mangleMacroExpansion (expr);
246
+ Result. Loc = RegularLocation (expr);
247
+ Result. Name = mangler.mangleMacroExpansion (expr);
238
248
} else {
239
249
auto decl = cast<MacroExpansionDecl>(parent.get <Decl *>());
240
- Loc = RegularLocation (decl);
241
- Name = mangler.mangleMacroExpansion (decl);
250
+ Result. Loc = RegularLocation (decl);
251
+ Result. Name = mangler.mangleMacroExpansion (decl);
242
252
}
243
253
break ;
244
254
}
245
255
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
246
256
auto expansion = cast<MacroExpansionDecl>(
247
257
ASTNode::getFromOpaqueValue (Info.astNode ).get <Decl *>());
248
- Loc = RegularLocation (expansion);
249
- Name = mangler.mangleMacroExpansion (expansion);
258
+ Result.Loc = RegularLocation (expansion);
259
+ Result.Name = mangler.mangleMacroExpansion (expansion);
260
+ Result.Freestanding = true ;
250
261
break ;
251
262
}
252
263
case GeneratedSourceInfo::AccessorMacroExpansion:
@@ -257,16 +268,17 @@ getMacroName(GeneratedSourceInfo &Info) {
257
268
auto decl = ASTNode::getFromOpaqueValue (Info.astNode ).get <Decl *>();
258
269
auto attr = Info.attachedMacroCustomAttr ;
259
270
if (auto *macroDecl = decl->getResolvedMacro (attr)) {
260
- Loc = RegularLocation (macroDecl);
261
- Name = macroDecl->getBaseName ().userFacingName ();
271
+ Result.Loc = RegularLocation (macroDecl);
272
+ Result.Name = macroDecl->getBaseName ().userFacingName ();
273
+ Result.Freestanding = true ;
262
274
}
263
275
break ;
264
276
}
265
277
case GeneratedSourceInfo::PrettyPrinted:
266
278
case GeneratedSourceInfo::ReplacedFunctionBody:
267
279
break ;
268
280
}
269
- return {Loc, Name} ;
281
+ return Result ;
270
282
}
271
283
272
284
const SILDebugScope *SILGenFunction::getMacroScope (SourceLoc SLoc) {
@@ -276,6 +288,15 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
276
288
if (!GeneratedSourceInfo)
277
289
return nullptr ;
278
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
+
279
300
SourceLoc OrigSLoc = GeneratedSourceInfo->originalSourceRange .getStart ();
280
301
if (!OrigSLoc)
281
302
return nullptr ;
@@ -296,22 +317,24 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
296
317
/* yields*/
297
318
{},
298
319
/* Results*/ {}, None, SubstitutionMap (), SubstitutionMap (), ASTContext);
299
- auto LocName = getMacroName (*GeneratedSourceInfo);
300
- RegularLocation MacroLoc (LocName.first );
301
- StringRef MacroName = ASTContext.getIdentifier (LocName.second ).str ();
320
+ StringRef MacroName = ASTContext.getIdentifier (Macro.Name ).str ();
321
+
302
322
SILFunction *MacroFn = B.getOrCreateFunction (
303
- MacroLoc , MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
323
+ Macro. Loc , MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
304
324
IsNotBare, IsNotTransparent, IsNotSerialized, IsNotDynamic,
305
325
IsNotDistributed, IsNotRuntimeAccessible);
306
326
// At the end of the chain OrigSLoc should be a macro expansion node.
307
- const SILDebugScope *InlinedAt = getOrCreateScope (OrigSLoc);
327
+ const SILDebugScope *InlinedAt = nullptr ;
328
+ const SILDebugScope *OrigScope = getOrCreateScope (OrigSLoc);
329
+ RegularLocation OrigLoc (OrigSLoc);
308
330
// Inject an extra scope to hold the inlined call site.
309
- if (InlinedAt)
310
- InlinedAt =
311
- new (SGM.M ) SILDebugScope (RegularLocation (OrigSLoc), nullptr , InlinedAt,
312
- InlinedAt->InlinedCallSite );
331
+ if (OrigScope)
332
+ InlinedAt = new (SGM.M )
333
+ SILDebugScope (Macro.Freestanding ? Macro.Loc : OrigLoc, nullptr ,
334
+ OrigScope, OrigScope->InlinedCallSite );
335
+
313
336
const SILDebugScope *Scope =
314
- new (SGM.M ) SILDebugScope (MacroLoc , MacroFn, nullptr , InlinedAt);
337
+ new (SGM.M ) SILDebugScope (Macro. Loc , MacroFn, nullptr , InlinedAt);
315
338
316
339
InlinedScopeMap.insert ({BufferID, Scope});
317
340
return Scope;
0 commit comments