@@ -57,7 +57,7 @@ SILGenFunction::SILGenFunction(SILGenModule &SGM, SILFunction &F,
57
57
SourceLoc SLoc = F.getLocation ().getSourceLoc ();
58
58
if (SF && SLoc) {
59
59
FnASTScope = ast_scope::ASTScopeImpl::findStartingScopeForLookup (SF, SLoc);
60
- ScopeMap.insert ({FnASTScope, F.getDebugScope ()});
60
+ ScopeMap.insert ({{ FnASTScope, nullptr } , F.getDebugScope ()});
61
61
}
62
62
}
63
63
@@ -215,47 +215,49 @@ const SILDebugScope *SILGenFunction::getOrCreateScope(SourceLoc SLoc) {
215
215
if (!astScope->getParent ())
216
216
return nullptr ;
217
217
218
- const SILDebugScope *Scope = getOrCreateScope (astScope);
218
+ const SILDebugScope *Scope = getOrCreateScope (astScope, F. getDebugScope () );
219
219
assert (Scope && " failed to construct SILDebugScope from ASTScope" );
220
220
return Scope;
221
221
}
222
222
223
223
namespace {
224
224
struct MacroInfo {
225
- MacroInfo (SourceLoc SLoc) : Loc(SLoc) {}
226
- RegularLocation Loc;
227
- std::string Name;
225
+ MacroInfo (SourceLoc SLoc, SourceLoc ExpansionSLoc)
226
+ : SLoc(SLoc), ExpansionSLoc(ExpansionSLoc) {}
227
+ SourceLoc SLoc;
228
+ SourceLoc ExpansionSLoc;
229
+ RegularLocation ExpansionLoc = RegularLocation((Decl*)nullptr );
230
+ std::string Name = " __unknown_macro__" ;
228
231
bool Freestanding = false ;
229
232
};
230
233
}
231
234
235
+ // / Return location of the macro expansion and the macro name.
232
236
static MacroInfo getMacroInfo (GeneratedSourceInfo &Info) {
233
- SourceLoc MacroSLoc = Info.generatedSourceRange .getStart ();
234
- MacroInfo Result (MacroSLoc);
235
- Result.Name = " __unknown_macro__" ;
237
+ MacroInfo Result (Info.generatedSourceRange .getStart (),
238
+ Info.originalSourceRange .getStart ());
236
239
if (!Info.astNode )
237
240
return Result;
238
-
239
241
// Keep this in sync with ASTMangler::appendMacroExpansionContext().
240
242
Mangle::ASTMangler mangler;
241
243
switch (Info.kind ) {
242
244
case GeneratedSourceInfo::ExpressionMacroExpansion: {
243
245
auto parent = ASTNode::getFromOpaqueValue (Info.astNode );
244
246
if (auto expr =
245
247
cast_or_null<MacroExpansionExpr>(parent.dyn_cast <Expr *>())) {
246
- Result.Loc = RegularLocation (expr);
248
+ Result.ExpansionLoc = RegularLocation (expr);
247
249
Result.Name = mangler.mangleMacroExpansion (expr);
248
250
} else {
249
251
auto decl = cast<MacroExpansionDecl>(parent.get <Decl *>());
250
- Result.Loc = RegularLocation (decl);
252
+ Result.ExpansionLoc = RegularLocation (decl);
251
253
Result.Name = mangler.mangleMacroExpansion (decl);
252
254
}
253
255
break ;
254
256
}
255
257
case GeneratedSourceInfo::FreestandingDeclMacroExpansion: {
256
258
auto expansion = cast<MacroExpansionDecl>(
257
259
ASTNode::getFromOpaqueValue (Info.astNode ).get <Decl *>());
258
- Result.Loc = RegularLocation (expansion);
260
+ Result.ExpansionLoc = RegularLocation (expansion);
259
261
Result.Name = mangler.mangleMacroExpansion (expansion);
260
262
Result.Freestanding = true ;
261
263
break ;
@@ -268,7 +270,7 @@ static MacroInfo getMacroInfo(GeneratedSourceInfo &Info) {
268
270
auto decl = ASTNode::getFromOpaqueValue (Info.astNode ).get <Decl *>();
269
271
auto attr = Info.attachedMacroCustomAttr ;
270
272
if (auto *macroDecl = decl->getResolvedMacro (attr)) {
271
- Result.Loc = RegularLocation (macroDecl);
273
+ Result.ExpansionLoc = RegularLocation (macroDecl);
272
274
Result.Name = macroDecl->getBaseName ().userFacingName ();
273
275
Result.Freestanding = true ;
274
276
}
@@ -297,61 +299,67 @@ const SILDebugScope *SILGenFunction::getMacroScope(SourceLoc SLoc) {
297
299
if (Macro.Freestanding )
298
300
return nullptr ;
299
301
300
- SourceLoc OrigSLoc = GeneratedSourceInfo->originalSourceRange .getStart ();
301
- if (!OrigSLoc)
302
- return nullptr ;
303
-
302
+ const SILDebugScope *TopLevelScope;
304
303
auto It = InlinedScopeMap.find (BufferID);
305
304
if (It != InlinedScopeMap.end ())
306
- return It->second ;
305
+ TopLevelScope = It->second ;
306
+ else {
307
+ // Recursively create one inlined function + scope per layer of generated
308
+ // sources. Chains of Macro expansions are representad as flat
309
+ // function-level scopes.
310
+ SILGenFunctionBuilder B (SGM);
311
+ auto &ASTContext = SGM.M .getASTContext ();
312
+ auto ExtInfo = SILFunctionType::ExtInfo::getThin ();
313
+ auto FunctionType = SILFunctionType::get (
314
+ nullptr , ExtInfo, SILCoroutineKind::None,
315
+ ParameterConvention::Direct_Unowned, /* Params*/ {},
316
+ /* yields*/
317
+ {},
318
+ /* Results*/ {}, None, SubstitutionMap (), SubstitutionMap (), ASTContext);
319
+ StringRef MacroName = ASTContext.getIdentifier (Macro.Name ).str ();
320
+ RegularLocation MacroLoc (Macro.SLoc );
321
+ // Use the ExpansionLoc as the location so IRGenDebugInfo can extract the
322
+ // human-readable macro name from the MacroExpansionDecl.
323
+ SILFunction *MacroFn = B.getOrCreateFunction (
324
+ Macro.ExpansionLoc , MacroName,
325
+ SILLinkage::DefaultForDeclaration, FunctionType, IsNotBare,
326
+ IsNotTransparent, IsNotSerialized, IsNotDynamic, IsNotDistributed,
327
+ IsNotRuntimeAccessible);
328
+ // At the end of the chain ExpansionLoc should be a macro expansion node.
329
+ const SILDebugScope *InlinedAt = nullptr ;
330
+ const SILDebugScope *ExpansionScope = getOrCreateScope (Macro.ExpansionSLoc );
331
+
332
+ // Inject an extra scope to hold the inlined call site.
333
+ if (ExpansionScope)
334
+ InlinedAt = new (SGM.M )
335
+ SILDebugScope (Macro.ExpansionLoc , nullptr , ExpansionScope,
336
+ ExpansionScope->InlinedCallSite );
337
+
338
+ TopLevelScope =
339
+ new (SGM.M ) SILDebugScope (MacroLoc, MacroFn, nullptr , InlinedAt);
340
+
341
+ InlinedScopeMap.insert ({BufferID, TopLevelScope});
342
+ }
307
343
308
- // Recursively create one inlined function + scope per layer of generated
309
- // sources. Chains of Macro expansions are representad as flat function-level
310
- // scopes.
311
- SILGenFunctionBuilder B (SGM);
312
- auto &ASTContext = SGM.M .getASTContext ();
313
- auto ExtInfo = SILFunctionType::ExtInfo::getThin ();
314
- auto FunctionType = SILFunctionType::get (
315
- nullptr , ExtInfo, SILCoroutineKind::None,
316
- ParameterConvention::Direct_Unowned, /* Params*/ {},
317
- /* yields*/
318
- {},
319
- /* Results*/ {}, None, SubstitutionMap (), SubstitutionMap (), ASTContext);
320
- StringRef MacroName = ASTContext.getIdentifier (Macro.Name ).str ();
321
-
322
- SILFunction *MacroFn = B.getOrCreateFunction (
323
- Macro.Loc , MacroName, SILLinkage::DefaultForDeclaration, FunctionType,
324
- IsNotBare, IsNotTransparent, IsNotSerialized, IsNotDynamic,
325
- IsNotDistributed, IsNotRuntimeAccessible);
326
- // At the end of the chain OrigSLoc should be a macro expansion node.
327
- const SILDebugScope *InlinedAt = nullptr ;
328
- const SILDebugScope *OrigScope = getOrCreateScope (OrigSLoc);
329
- RegularLocation OrigLoc (OrigSLoc);
330
- // Inject an extra scope to hold the inlined call site.
331
- if (OrigScope)
332
- InlinedAt = new (SGM.M )
333
- SILDebugScope (Macro.Freestanding ? Macro.Loc : OrigLoc, nullptr ,
334
- OrigScope, OrigScope->InlinedCallSite );
335
-
336
- const SILDebugScope *Scope =
337
- new (SGM.M ) SILDebugScope (Macro.Loc , MacroFn, nullptr , InlinedAt);
338
-
339
- InlinedScopeMap.insert ({BufferID, Scope});
340
- return Scope;
344
+ // Create the scope hierarchy inside the macro expansion.
345
+ auto *MacroAstScope =
346
+ ast_scope::ASTScopeImpl::findStartingScopeForLookup (SF, Macro.SLoc );
347
+ return getOrCreateScope (MacroAstScope, TopLevelScope,
348
+ TopLevelScope->InlinedCallSite );
341
349
}
342
350
343
351
const SILDebugScope *
344
- SILGenFunction::getOrCreateScope (const ast_scope::ASTScopeImpl *ASTScope) {
345
- const SILDebugScope *FnScope = F. getDebugScope ();
346
-
352
+ SILGenFunction::getOrCreateScope (const ast_scope::ASTScopeImpl *ASTScope,
353
+ const SILDebugScope *FnScope,
354
+ const SILDebugScope *InlinedAt) {
347
355
if (!ASTScope)
348
356
return FnScope;
349
357
350
358
// Top-level function scope?
351
359
if (ASTScope == FnASTScope)
352
360
return FnScope;
353
361
354
- auto It = ScopeMap.find (ASTScope);
362
+ auto It = ScopeMap.find ({ ASTScope, InlinedAt} );
355
363
if (It != ScopeMap.end ())
356
364
return It->second ;
357
365
@@ -374,20 +382,21 @@ SILGenFunction::getOrCreateScope(const ast_scope::ASTScopeImpl *ASTScope) {
374
382
// Since the arguments to Constructor aren't marked as implicit,
375
383
// argument b is in the scope of v, but the call to Constructor
376
384
// isn't, which correctly triggers the scope hole verifier.
377
- return B.getCurrentDebugScope ();
385
+ auto *CurScope = B.getCurrentDebugScope ();
386
+ return CurScope->InlinedCallSite != InlinedAt ? FnScope : CurScope;
378
387
}
379
388
380
389
// Collapse BraceStmtScopes whose parent is a .*BodyScope.
381
390
if (auto Parent = ASTScope->getParent ().getPtrOrNull ())
382
391
if (Parent->getSourceRangeOfThisASTNode () ==
383
392
ASTScope->getSourceRangeOfThisASTNode ())
384
- return getOrCreateScope (Parent);
393
+ return getOrCreateScope (Parent, FnScope, InlinedAt );
385
394
386
395
// The calls to defer closures have cleanup source locations pointing to the
387
396
// defer. Reparent them into the current debug scope.
388
397
auto *AncestorScope = ASTScope->getParent ().getPtrOrNull ();
389
398
while (AncestorScope && AncestorScope != FnASTScope &&
390
- !ScopeMap.count (AncestorScope)) {
399
+ !ScopeMap.count ({ AncestorScope, InlinedAt} )) {
391
400
if (auto *FD = dyn_cast_or_null<FuncDecl>(
392
401
AncestorScope->getDeclIfAny ().getPtrOrNull ())) {
393
402
if (cast<DeclContext>(FD) != FunctionDC)
@@ -404,10 +413,16 @@ SILGenFunction::getOrCreateScope(const ast_scope::ASTScopeImpl *ASTScope) {
404
413
};
405
414
406
415
const SILDebugScope *Parent =
407
- getOrCreateScope (ASTScope->getParent ().getPtrOrNull ());
408
- RegularLocation Loc (ASTScope->getSourceRangeOfThisASTNode ().Start );
409
- SILScope = new (SGM.M ) SILDebugScope (Loc, &F, Parent);
410
- ScopeMap.insert ({ASTScope, SILScope});
416
+ getOrCreateScope (ASTScope->getParent ().getPtrOrNull (), FnScope, InlinedAt);
417
+ SourceLoc SLoc = ASTScope->getSourceRangeOfThisASTNode ().Start ;
418
+ RegularLocation Loc (SLoc);
419
+ SILScope = new (SGM.M )
420
+ SILDebugScope (Loc, FnScope->getParentFunction (), Parent, InlinedAt);
421
+ ScopeMap.insert ({{ASTScope, InlinedAt}, SILScope});
422
+
423
+ assert (SILScope->getParentFunction () == &F &&
424
+ " inlinedAt points to other function" );
425
+
411
426
return SILScope;
412
427
}
413
428
0 commit comments