@@ -2297,8 +2297,37 @@ void DWARFLinker::DIECloner::generateLineTableForUnit(CompileUnit &Unit) {
22972297
22982298 // Create a map of stmt sequence offsets to original row indices.
22992299 DenseMap<uint64_t , unsigned > SeqOffToOrigRow;
2300- for (const DWARFDebugLine::Sequence &Seq : LT->Sequences )
2301- SeqOffToOrigRow[Seq.StmtSeqOffset ] = Seq.FirstRowIndex ;
2300+ // The DWARF parser's discovery of sequences can be incomplete. To
2301+ // ensure all DW_AT_LLVM_stmt_sequence attributes can be patched, we
2302+ // build a map from both the parser's results and a manual
2303+ // reconstruction.
2304+ if (!LT->Rows .empty ()) {
2305+ // First, trust the sequences that the DWARF parser did identify.
2306+ for (const DWARFDebugLine::Sequence &Seq : LT->Sequences )
2307+ SeqOffToOrigRow.try_emplace (Seq.StmtSeqOffset , Seq.FirstRowIndex );
2308+
2309+ // Second, manually find sequence boundaries and match them to the
2310+ // sorted attributes to handle sequences the parser might have missed.
2311+ auto StmtAttrs = Unit.getStmtSeqListAttributes ();
2312+ llvm::sort (StmtAttrs,
2313+ [](const PatchLocation &A, const PatchLocation &B) {
2314+ return A.get () < B.get ();
2315+ });
2316+
2317+ std::vector<size_t > SeqStartRows;
2318+ SeqStartRows.push_back (0 );
2319+ for (size_t i = 0 ; i < LT->Rows .size () - 1 ; ++i)
2320+ if (LT->Rows [i].EndSequence )
2321+ SeqStartRows.push_back (i + 1 );
2322+
2323+ // Correlate the sorted attributes with the reconstructed sequence
2324+ // starts. This provides a partial mapping if counts are mismatched,
2325+ // maximizing the number of correctly patched attributes.
2326+ size_t NumMappings = std::min (StmtAttrs.size (), SeqStartRows.size ());
2327+ for (size_t i = 0 ; i < NumMappings; ++i) {
2328+ SeqOffToOrigRow.try_emplace (StmtAttrs[i].get (), SeqStartRows[i]);
2329+ }
2330+ }
23022331
23032332 // Create a map of original row indices to new row indices.
23042333 DenseMap<size_t , size_t > OrigRowToNewRow;
0 commit comments