@@ -551,8 +551,18 @@ void DwarfCompileUnit::constructScopeDIE(LexicalScope *Scope,
551
551
// Emit lexical blocks.
552
552
DIE *ScopeDIE = constructLexicalScopeDIE (Scope);
553
553
assert (ScopeDIE && " Scope DIE should not be null." );
554
-
555
554
ParentScopeDIE.addChild (ScopeDIE);
555
+
556
+ // Track abstract and concrete lexical block scopes.
557
+ if (Scope->isAbstractScope ()) {
558
+ assert (!getAbstractScopeDIEs ().count (DS) &&
559
+ " Abstract DIE for this scope exists!" );
560
+ getAbstractScopeDIEs ()[DS] = ScopeDIE;
561
+ } else if (!Scope->getInlinedAt ()) {
562
+ assert (!LocalScopeDIEs.count (DS) && " Concrete DIE for this scope exists!" );
563
+ LocalScopeDIEs[DS] = ScopeDIE;
564
+ }
565
+
556
566
createAndAddScopeChildren (Scope, *ScopeDIE);
557
567
}
558
568
@@ -646,7 +656,7 @@ DIE *DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) {
646
656
auto *InlinedSP = getDISubprogram (DS);
647
657
// Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
648
658
// was inlined from another compile unit.
649
- DIE *OriginDIE = getAbstractSPDies ()[InlinedSP];
659
+ DIE *OriginDIE = getAbstractScopeDIEs ()[InlinedSP];
650
660
assert (OriginDIE && " Unable to find original DIE for an inlined subprogram." );
651
661
652
662
auto ScopeDIE = DIE::get (DIEValueAllocator, dwarf::DW_TAG_inlined_subroutine);
@@ -1001,6 +1011,12 @@ DIE &DwarfCompileUnit::constructSubprogramScopeDIE(const DISubprogram *Sub,
1001
1011
if (Scope) {
1002
1012
assert (!Scope->getInlinedAt ());
1003
1013
assert (!Scope->isAbstractScope ());
1014
+
1015
+ // Remember the subrogram before creating child entities.
1016
+ assert (!LocalScopeDIEs.count (Sub) &&
1017
+ " Concrete DIE for the subprogram exists!" );
1018
+ LocalScopeDIEs[Sub] = &ScopeDIE;
1019
+
1004
1020
// Collect lexical scope children first.
1005
1021
// ObjectPointer might be a local (non-argument) local variable if it's a
1006
1022
// block's synthetic this pointer.
@@ -1036,27 +1052,18 @@ DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
1036
1052
for (DbgVariable *DV : Locals)
1037
1053
ScopeDIE.addChild (constructVariableDIE (*DV, *Scope, ObjectPointer));
1038
1054
1039
- // Emit imported entities (skipped in gmlt-like data).
1040
- if (!includeMinimalInlineScopes ()) {
1041
- for (const auto *IE : ImportedEntities[Scope->getScopeNode ()])
1042
- ScopeDIE.addChild (constructImportedEntityDIE (cast<DIImportedEntity>(IE)));
1043
- }
1044
-
1045
1055
// Emit labels.
1046
1056
for (DbgLabel *DL : DU->getScopeLabels ().lookup (Scope))
1047
1057
ScopeDIE.addChild (constructLabelDIE (*DL, *Scope));
1048
1058
1049
1059
// Emit inner lexical scopes.
1050
- auto needToEmitLexicalScope = [this ](LexicalScope *LS) {
1060
+ auto needToEmitLexicalScope = [this ](LexicalScope *LS) -> bool {
1051
1061
if (isa<DISubprogram>(LS->getScopeNode ()))
1052
1062
return true ;
1053
1063
auto Vars = DU->getScopeVariables ().lookup (LS);
1054
1064
if (!Vars.Args .empty () || !Vars.Locals .empty ())
1055
1065
return true ;
1056
- if (!includeMinimalInlineScopes () &&
1057
- !ImportedEntities[LS->getScopeNode ()].empty ())
1058
- return true ;
1059
- return false ;
1066
+ return LocalScopesWithLocalDecls.count (LS->getScopeNode ());
1060
1067
};
1061
1068
for (LexicalScope *LS : Scope->getChildren ()) {
1062
1069
// If the lexical block doesn't have non-scope children, skip
@@ -1072,11 +1079,10 @@ DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
1072
1079
1073
1080
void DwarfCompileUnit::constructAbstractSubprogramScopeDIE (
1074
1081
LexicalScope *Scope) {
1075
- DIE *&AbsDef = getAbstractSPDies ()[Scope->getScopeNode ()];
1076
- if (AbsDef)
1077
- return ;
1078
1082
1079
1083
auto *SP = cast<DISubprogram>(Scope->getScopeNode ());
1084
+ if (auto *SPDie = getAbstractScopeDIEs ().lookup (SP))
1085
+ return ;
1080
1086
1081
1087
DIE *ContextDIE;
1082
1088
DwarfCompileUnit *ContextCU = this ;
@@ -1100,14 +1106,19 @@ void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
1100
1106
1101
1107
// Passing null as the associated node because the abstract definition
1102
1108
// shouldn't be found by lookup.
1103
- AbsDef = &ContextCU->createAndAddDIE (dwarf::DW_TAG_subprogram, *ContextDIE, nullptr );
1104
- ContextCU->applySubprogramAttributesToDefinition (SP, *AbsDef);
1105
- ContextCU->addSInt (*AbsDef, dwarf::DW_AT_inline,
1109
+ DIE &AbsDef = ContextCU->createAndAddDIE (dwarf::DW_TAG_subprogram,
1110
+ *ContextDIE, nullptr );
1111
+
1112
+ // Store the DIE before creating children.
1113
+ getAbstractScopeDIEs ()[SP] = &AbsDef;
1114
+
1115
+ ContextCU->applySubprogramAttributesToDefinition (SP, AbsDef);
1116
+ ContextCU->addSInt (AbsDef, dwarf::DW_AT_inline,
1106
1117
DD->getDwarfVersion () <= 4 ? Optional<dwarf::Form>()
1107
1118
: dwarf::DW_FORM_implicit_const,
1108
1119
dwarf::DW_INL_inlined);
1109
- if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren (Scope, * AbsDef))
1110
- ContextCU->addDIEEntry (* AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
1120
+ if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren (Scope, AbsDef))
1121
+ ContextCU->addDIEEntry (AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
1111
1122
}
1112
1123
1113
1124
bool DwarfCompileUnit::useGNUAnalogForDwarf5Feature () const {
@@ -1241,47 +1252,61 @@ void DwarfCompileUnit::constructCallSiteParmEntryDIEs(
1241
1252
}
1242
1253
}
1243
1254
1244
- DIE *DwarfCompileUnit::constructImportedEntityDIE (
1245
- const DIImportedEntity *Module) {
1246
- DIE *IMDie = DIE::get (DIEValueAllocator, (dwarf::Tag)Module-> getTag () );
1247
- insertDIE (Module, IMDie);
1255
+ DIE *DwarfCompileUnit::createImportedEntityDIE ( const DIImportedEntity *IE) {
1256
+ DIE *IMDie = DIE::get (DIEValueAllocator, (dwarf::Tag)IE-> getTag ());
1257
+ insertDIE (IE, IMDie );
1258
+
1248
1259
DIE *EntityDie;
1249
- auto *Entity = Module ->getEntity ();
1260
+ auto *Entity = IE ->getEntity ();
1250
1261
if (auto *NS = dyn_cast<DINamespace>(Entity))
1251
1262
EntityDie = getOrCreateNameSpace (NS);
1252
1263
else if (auto *M = dyn_cast<DIModule>(Entity))
1253
1264
EntityDie = getOrCreateModule (M);
1254
- else if (auto *SP = dyn_cast<DISubprogram>(Entity))
1255
- EntityDie = getOrCreateSubprogramDIE (SP);
1256
- else if (auto *T = dyn_cast<DIType>(Entity))
1265
+ else if (auto *SP = dyn_cast<DISubprogram>(Entity)) {
1266
+ // If we have abstract subprogram created, refer it.
1267
+ if (auto *AbsSPDie = getAbstractScopeDIEs ().lookup (SP))
1268
+ EntityDie = AbsSPDie;
1269
+ else
1270
+ EntityDie = getOrCreateSubprogramDIE (SP);
1271
+ } else if (auto *T = dyn_cast<DIType>(Entity))
1257
1272
EntityDie = getOrCreateTypeDIE (T);
1258
1273
else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity))
1259
1274
EntityDie = getOrCreateGlobalVariableDIE (GV, {});
1260
1275
else
1261
1276
EntityDie = getDIE (Entity);
1262
1277
assert (EntityDie);
1263
- addSourceLine (*IMDie, Module->getLine (), Module->getFile ());
1278
+
1279
+ addSourceLine (*IMDie, IE->getLine (), IE->getFile ());
1264
1280
addDIEEntry (*IMDie, dwarf::DW_AT_import, *EntityDie);
1265
- StringRef Name = Module ->getName ();
1281
+ StringRef Name = IE ->getName ();
1266
1282
if (!Name.empty ())
1267
1283
addString (*IMDie, dwarf::DW_AT_name, Name);
1268
1284
1269
1285
// This is for imported module with renamed entities (such as variables and
1270
1286
// subprograms).
1271
- DINodeArray Elements = Module ->getElements ();
1287
+ DINodeArray Elements = IE ->getElements ();
1272
1288
for (const auto *Element : Elements) {
1273
1289
if (!Element)
1274
1290
continue ;
1275
- IMDie->addChild (
1276
- constructImportedEntityDIE (cast<DIImportedEntity>(Element)));
1291
+ IMDie->addChild (createImportedEntityDIE (cast<DIImportedEntity>(Element)));
1277
1292
}
1278
-
1279
1293
return IMDie;
1280
1294
}
1281
1295
1296
+ void DwarfCompileUnit::createAndAddImportedEntityDIE (
1297
+ const DIImportedEntity *IE) {
1298
+ DIE *ContextDIE = getOrCreateContextDIE (IE->getScope ());
1299
+ assert (ContextDIE &&
1300
+ " Could not get or create scope for the imported entity!" );
1301
+ if (!ContextDIE)
1302
+ return ;
1303
+
1304
+ ContextDIE->addChild (createImportedEntityDIE (IE));
1305
+ }
1306
+
1282
1307
void DwarfCompileUnit::finishSubprogramDefinition (const DISubprogram *SP) {
1283
1308
DIE *D = getDIE (SP);
1284
- if (DIE *AbsSPDIE = getAbstractSPDies ().lookup (SP)) {
1309
+ if (DIE *AbsSPDIE = getAbstractScopeDIEs ().lookup (SP)) {
1285
1310
if (D)
1286
1311
// If this subprogram has an abstract definition, reference that
1287
1312
addDIEEntry (*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE);
@@ -1571,3 +1596,38 @@ void DwarfCompileUnit::createBaseTypeDIEs() {
1571
1596
Btr.Die = &Die;
1572
1597
}
1573
1598
}
1599
+
1600
+ static DIE *
1601
+ findLocalScopeDIE (const DILocalScope *LS,
1602
+ DenseMap<const DILocalScope *, DIE *> &ScopeDIEs) {
1603
+ DIE *ScopeDIE = ScopeDIEs.lookup (LS);
1604
+ if (isa<DISubprogram>(LS) && !ScopeDIE)
1605
+ return nullptr ;
1606
+ if (!ScopeDIE)
1607
+ return findLocalScopeDIE (cast<DILocalScope>(LS->getScope ()), ScopeDIEs);
1608
+ return ScopeDIE;
1609
+ }
1610
+
1611
+ DIE *DwarfCompileUnit::findLocalScopeDIE (const DIScope *S) {
1612
+ auto *LScope = dyn_cast_or_null<DILocalScope>(S);
1613
+ if (!LScope)
1614
+ return nullptr ;
1615
+
1616
+ // Check if we have an abstract tree.
1617
+ if (getAbstractScopeDIEs ().count (LScope->getSubprogram ()))
1618
+ return ::findLocalScopeDIE (LScope, getAbstractScopeDIEs ());
1619
+
1620
+ return ::findLocalScopeDIE (LScope, LocalScopeDIEs);
1621
+ }
1622
+
1623
+ DIE *DwarfCompileUnit::getOrCreateContextDIE (const DIScope *Context) {
1624
+ if (auto *LScope = dyn_cast_or_null<DILocalScope>(Context)) {
1625
+ if (DIE *ScopeDIE = findLocalScopeDIE (LScope))
1626
+ return ScopeDIE;
1627
+
1628
+ // If nothing was found, fall back to DISubprogram and let
1629
+ // DwarfUnit::getOrCreateContextDIE() create a new DIE for it.
1630
+ Context = LScope->getSubprogram ();
1631
+ }
1632
+ return DwarfUnit::getOrCreateContextDIE (Context);
1633
+ }
0 commit comments