@@ -227,6 +227,20 @@ llvm::DIScope *CGDebugInfo::getContextDescriptor(const Decl *Context,
227
227
return Default;
228
228
}
229
229
230
+ void CGDebugInfo::recordDeclarationLexicalScope (const Decl &D) {
231
+ assert (LexicalBlockMap.find (&D) == LexicalBlockMap.end () &&
232
+ " D is already mapped to a lexical block scope" );
233
+ if (!LexicalBlockStack.empty ())
234
+ LexicalBlockMap.insert ({&D, LexicalBlockStack.back ()});
235
+ }
236
+
237
+ llvm::DIScope *CGDebugInfo::getDeclarationLexicalScope (const Decl *D) {
238
+ auto I = LexicalBlockMap.find (D);
239
+ if (I != LexicalBlockMap.end ())
240
+ return I->second ;
241
+ return getDeclContextDescriptor (cast<Decl>(D));
242
+ }
243
+
230
244
PrintingPolicy CGDebugInfo::getPrintingPolicy () const {
231
245
PrintingPolicy PP = CGM.getContext ().getPrintingPolicy ();
232
246
@@ -1346,13 +1360,13 @@ llvm::DIType *CGDebugInfo::CreateType(const TypedefType *Ty,
1346
1360
// declared.
1347
1361
SourceLocation Loc = Ty->getDecl ()->getLocation ();
1348
1362
1363
+ llvm::DIScope *TDContext = getDeclarationLexicalScope (Ty->getDecl ());
1349
1364
uint32_t Align = getDeclAlignIfRequired (Ty->getDecl (), CGM.getContext ());
1350
1365
// Typedefs are derived from some other type.
1351
1366
llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations (Ty->getDecl ());
1352
1367
return DBuilder.createTypedef (Underlying, Ty->getDecl ()->getName (),
1353
1368
getOrCreateFile (Loc), getLineNumber (Loc),
1354
- getDeclContextDescriptor (Ty->getDecl ()), Align,
1355
- Annotations);
1369
+ TDContext, Align, Annotations);
1356
1370
}
1357
1371
1358
1372
static unsigned getDwarfCC (CallingConv CC) {
@@ -3251,7 +3265,7 @@ llvm::DIType *CGDebugInfo::CreateEnumType(const EnumType *Ty) {
3251
3265
// entered into the ReplaceMap: finalize() will replace the first
3252
3266
// FwdDecl with the second and then replace the second with
3253
3267
// complete type.
3254
- llvm::DIScope *EDContext = getDeclContextDescriptor (ED);
3268
+ llvm::DIScope *EDContext = getDeclarationLexicalScope (ED);
3255
3269
llvm::DIFile *DefUnit = getOrCreateFile (ED->getLocation ());
3256
3270
llvm::TempDIScope TmpContext (DBuilder.createReplaceableCompositeType (
3257
3271
llvm::dwarf::DW_TAG_enumeration_type, " " , TheCU, DefUnit, 0 ));
@@ -3294,7 +3308,7 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const EnumType *Ty) {
3294
3308
3295
3309
llvm::DIFile *DefUnit = getOrCreateFile (ED->getLocation ());
3296
3310
unsigned Line = getLineNumber (ED->getLocation ());
3297
- llvm::DIScope *EnumContext = getDeclContextDescriptor (ED);
3311
+ llvm::DIScope *EnumContext = getDeclarationLexicalScope (ED);
3298
3312
llvm::DIType *ClassTy = getOrCreateType (ED->getIntegerType (), DefUnit);
3299
3313
return DBuilder.createEnumerationType (EnumContext, ED->getName (), DefUnit,
3300
3314
Line, Size, Align, EltArray, ClassTy,
@@ -3597,7 +3611,7 @@ llvm::DICompositeType *CGDebugInfo::CreateLimitedType(const RecordType *Ty) {
3597
3611
Line = getLineNumber (Loc);
3598
3612
}
3599
3613
3600
- llvm::DIScope *RDContext = getDeclContextDescriptor (RD);
3614
+ llvm::DIScope *RDContext = getDeclarationLexicalScope (RD);
3601
3615
3602
3616
// If we ended up creating the type during the context chain construction,
3603
3617
// just return that.
@@ -3790,6 +3804,14 @@ void CGDebugInfo::collectVarDeclProps(const VarDecl *VD, llvm::DIFile *&Unit,
3790
3804
TemplateParameters = nullptr ;
3791
3805
}
3792
3806
3807
+ // Get context for static locals (that are technically globals) the same way
3808
+ // we do for "local" locals -- by using current lexical block.
3809
+ if (VD->isStaticLocal ()) {
3810
+ assert (!LexicalBlockStack.empty () && " Region stack mismatch, stack empty!" );
3811
+ VDContext = LexicalBlockStack.back ();
3812
+ return ;
3813
+ }
3814
+
3793
3815
// Since we emit declarations (DW_AT_members) for static members, place the
3794
3816
// definition of those static members in the namespace they were declared in
3795
3817
// in the source code (the lexical decl context).
0 commit comments