[llvm] [llvm-objdump] Fix output corruption when DWARF reg-name callback misses. (PR #200975)

James Henderson via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 9 00:05:03 PDT 2026


================
@@ -96,16 +96,27 @@ void LiveVariable::print(raw_ostream &OS, const MCRegisterInfo &MRI) const {
                      Unit->getContext().isLittleEndian(), 0);
   DWARFExpression Expression(Data, Unit->getAddressByteSize());
 
-  auto GetRegName = [&MRI, &OS](uint64_t DwarfRegNum, bool IsEH) -> StringRef {
+  // Buffer the "<unknown register N>" surface here rather than writing it
+  // directly to OS. The compact printer may invoke this callback
+  // speculatively and then succeed via its own fallback (e.g. ASCII packed
+  // virtual-register decoding); flushing eagerly would interleave the
+  // unknown text with the decoded name. We replay the buffer only if the
+  // printer ultimately reports failure, which preserves the original
+  // "<unknown register N>" surface for truly unmappable reg nums.
+  std::string localBuf;
+  raw_string_ostream localOS(localBuf);
+  auto GetRegName = [&MRI, &localOS](uint64_t DwarfRegNum,
+                                     bool IsEH) -> StringRef {
     if (std::optional<MCRegister> LLVMRegNum =
             MRI.getLLVMRegNum(DwarfRegNum, IsEH))
       if (const char *RegName = MRI.getName(*LLVMRegNum))
         return StringRef(RegName);
-    OS << "<unknown register " << DwarfRegNum << ">";
+    localOS << "<unknown register " << DwarfRegNum << ">";
     return {};
   };
 
-  printDwarfExpressionCompact(&Expression, OS, GetRegName);
+  if (!printDwarfExpressionCompact(&Expression, OS, GetRegName))
+    OS << localBuf;
----------------
jh7370 wrote:

+1 to having an llvm-objdump test for the case. As things stand, the code shows that if we do the same thing in another case, we get the right behaviour, but it doesn't show that llvm-objdump is doing the right thing. In other words, the test doesn't fail without your fix, which is a fundamental requirement of tests for new fixes.

https://github.com/llvm/llvm-project/pull/200975


More information about the llvm-commits mailing list