[llvm] [llvm-dwarfdump] Add a null-check in `prettyPrintBaseTypeRef`. (PR #93156)

Igor Kudrin via llvm-commits llvm-commits at lists.llvm.org
Mon May 27 22:03:32 PDT 2024


================
@@ -240,17 +240,21 @@ static void prettyPrintBaseTypeRef(DWARFUnit *U, raw_ostream &OS,
                                    ArrayRef<uint64_t> Operands,
                                    unsigned Operand) {
   assert(Operand < Operands.size() && "operand out of bounds");
-  auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
-  if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {
-    OS << " (";
-    if (DumpOpts.Verbose)
-      OS << format("0x%08" PRIx64 " -> ", Operands[Operand]);
-    OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
-    if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
-      OS << " \"" << *Name << "\"";
+  if (U) {
+    auto Die = U->getDIEForOffset(U->getOffset() + Operands[Operand]);
+    if (Die && Die.getTag() == dwarf::DW_TAG_base_type) {
+      OS << " (";
+      if (DumpOpts.Verbose)
+        OS << format("0x%08" PRIx64 " -> ", Operands[Operand]);
+      OS << format("0x%08" PRIx64 ")", U->getOffset() + Operands[Operand]);
+      if (auto Name = dwarf::toString(Die.find(dwarf::DW_AT_name)))
+        OS << " \"" << *Name << "\"";
+    } else {
+      OS << format(" <invalid base_type ref: 0x%" PRIx64 ">",
+                   Operands[Operand]);
+    }
   } else {
-    OS << format(" <invalid base_type ref: 0x%" PRIx64 ">",
-                 Operands[Operand]);
+    OS << format(" <base_type ref: 0x%" PRIx64 ">", Operands[Operand]);
----------------
igorkudrin wrote:

Moving this to the front of the function and using an early exit would simplify the patch and reduce nesting.
```cpp
  if (!U) {
    OS << format(" <base_type ref: 0x%" PRIx64 ">", Operands[Operand]);
    return;
  }
  auto Die = ...
```

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


More information about the llvm-commits mailing list