@@ -461,32 +461,42 @@ getUnitForOffset(DIEBuilder &Builder, DWARFContext &DWCtx,
461461 return nullptr ;
462462}
463463
464- uint32_t DIEBuilder::finalizeDIEs (
465- DWARFUnit &CU, DIE &Die,
466- std::vector<std:: optional<BOLTDWARF5AccelTableData *>> &Parents ,
467- uint32_t &CurOffset) {
464+ uint32_t
465+ DIEBuilder::finalizeDIEs ( DWARFUnit &CU, DIE &Die,
466+ std::optional<BOLTDWARF5AccelTableData *> Parent ,
467+ uint32_t NumberParentsInChain, uint32_t &CurOffset) {
468468 getState ().DWARFDieAddressesParsed .erase (Die.getOffset ());
469469 uint32_t CurSize = 0 ;
470470 Die.setOffset (CurOffset);
471471 std::optional<BOLTDWARF5AccelTableData *> NameEntry =
472472 DebugNamesTable.addAccelTableEntry (
473473 CU, Die, SkeletonCU ? SkeletonCU->getDWOId () : std::nullopt ,
474- Parents. back () );
474+ NumberParentsInChain, Parent );
475475 // It is possible that an indexed debugging information entry has a parent
476476 // that is not indexed (for example, if its parent does not have a name
477477 // attribute). In such a case, a parent attribute may point to a nameless
478478 // index entry (that is, one that cannot be reached from any entry in the name
479479 // table), or it may point to the nearest ancestor that does have an index
480480 // entry.
481+ // Although as discussed in https://github.com/llvm/llvm-project/pull/91808
482+ // skipping entry is not very useful. So this follows clang where children of
483+ // forward declaration won't have DW_IDX_parent.
484+
485+ // If Parent is nullopt and NumberParentsInChain is not zero, then forward
486+ // declaration was encountered in this DF traversal. Propogating nullopt for
487+ // Parent to children.
488+ if (!Parent && NumberParentsInChain)
489+ NameEntry = std::nullopt ;
481490 if (NameEntry)
482- Parents. push_back ( std::move (NameEntry)) ;
491+ ++NumberParentsInChain ;
483492 for (DIEValue &Val : Die.values ())
484493 CurSize += Val.sizeOf (CU.getFormParams ());
485494 CurSize += getULEB128Size (Die.getAbbrevNumber ());
486495 CurOffset += CurSize;
487496
488497 for (DIE &Child : Die.children ()) {
489- uint32_t ChildSize = finalizeDIEs (CU, Child, Parents, CurOffset);
498+ uint32_t ChildSize =
499+ finalizeDIEs (CU, Child, NameEntry, NumberParentsInChain, CurOffset);
490500 CurSize += ChildSize;
491501 }
492502 // for children end mark.
@@ -496,9 +506,6 @@ uint32_t DIEBuilder::finalizeDIEs(
496506 }
497507
498508 Die.setSize (CurSize);
499- if (NameEntry)
500- Parents.pop_back ();
501-
502509 return CurSize;
503510}
504511
@@ -510,7 +517,7 @@ void DIEBuilder::finish() {
510517 DebugNamesTable.setCurrentUnit (CU, UnitStartOffset);
511518 std::vector<std::optional<BOLTDWARF5AccelTableData *>> Parents;
512519 Parents.push_back (std::nullopt );
513- finalizeDIEs (CU, *UnitDIE, Parents , CurOffset);
520+ finalizeDIEs (CU, *UnitDIE, std:: nullopt , 0 , CurOffset);
514521
515522 DWARFUnitInfo &CurUnitInfo = getUnitInfoByDwarfUnit (CU);
516523 CurUnitInfo.UnitOffset = UnitStartOffset;
0 commit comments