[llvm-pdbutil] Don't crash when printing unknown CodeView type records
authorAlexandre Ganea <alexandre.ganea@ubisoft.com>
Thu, 7 Jan 2021 20:41:47 +0000 (15:41 -0500)
committerAlexandre Ganea <alexandre.ganea@ubisoft.com>
Thu, 7 Jan 2021 20:44:55 +0000 (15:44 -0500)
Differential Revision: https://reviews.llvm.org/D93720

llvm/test/tools/llvm-pdbutil/Inputs/unknown-record.obj [new file with mode: 0644]
llvm/test/tools/llvm-pdbutil/unknown-records.test [new file with mode: 0644]
llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
llvm/tools/llvm-pdbutil/FormatUtil.cpp
llvm/tools/llvm-pdbutil/FormatUtil.h

diff --git a/llvm/test/tools/llvm-pdbutil/Inputs/unknown-record.obj b/llvm/test/tools/llvm-pdbutil/Inputs/unknown-record.obj
new file mode 100644 (file)
index 0000000..0be00ff
Binary files /dev/null and b/llvm/test/tools/llvm-pdbutil/Inputs/unknown-record.obj differ
diff --git a/llvm/test/tools/llvm-pdbutil/unknown-records.test b/llvm/test/tools/llvm-pdbutil/unknown-records.test
new file mode 100644 (file)
index 0000000..9773b85
--- /dev/null
@@ -0,0 +1,3 @@
+; REQUIRES: diasdk
+; RUN: llvm-pdbutil dump -all %p/Inputs/unknown-record.obj | FileCheck %s
+; CHECK: UNKNOWN RECORD (0x1609)
index aa185e8..3d8a86f 100644 (file)
@@ -738,21 +738,17 @@ namespace {
 constexpr uint32_t kNoneUdtKind = 0;
 constexpr uint32_t kSimpleUdtKind = 1;
 constexpr uint32_t kUnknownUdtKind = 2;
-const StringRef NoneLabel("<none type>");
-const StringRef SimpleLabel("<simple type>");
-const StringRef UnknownLabel("<unknown type>");
-
 } // namespace
 
-static StringRef getUdtStatLabel(uint32_t Kind) {
+static std::string getUdtStatLabel(uint32_t Kind) {
   if (Kind == kNoneUdtKind)
-    return NoneLabel;
+    return "<none type>";
 
   if (Kind == kSimpleUdtKind)
-    return SimpleLabel;
+    return "<simple type>";
 
   if (Kind == kUnknownUdtKind)
-    return UnknownLabel;
+    return "<unknown type>";
 
   return formatTypeLeafKind(static_cast<TypeLeafKind>(Kind));
 }
@@ -760,7 +756,7 @@ static StringRef getUdtStatLabel(uint32_t Kind) {
 static uint32_t getLongestTypeLeafName(const StatCollection &Stats) {
   size_t L = 0;
   for (const auto &Stat : Stats.Individual) {
-    StringRef Label = getUdtStatLabel(Stat.first);
+    std::string Label = getUdtStatLabel(Stat.first);
     L = std::max(L, Label.size());
   }
   return static_cast<uint32_t>(L);
@@ -869,7 +865,7 @@ Error DumpOutputStyle::dumpUdtStats() {
 
   P.formatLine("{0}", fmt_repeat('-', TableWidth));
   for (const auto &Stat : UdtTargetStats.getStatsSortedBySize()) {
-    StringRef Label = getUdtStatLabel(Stat.first);
+    std::string Label = getUdtStatLabel(Stat.first);
     P.formatLine("{0} | {1:N}  {2:N}",
                  fmt_align(Label, AlignStyle::Right, FieldWidth),
                  fmt_align(Stat.second.Count, AlignStyle::Right, CD),
index c9ef196..b483739 100644 (file)
@@ -156,16 +156,17 @@ std::string llvm::pdb::formatSymbolKind(SymbolKind K) {
   return formatUnknownEnum(K);
 }
 
-StringRef llvm::pdb::formatTypeLeafKind(TypeLeafKind K) {
+std::string llvm::pdb::formatTypeLeafKind(TypeLeafKind K) {
   switch (K) {
 #define TYPE_RECORD(EnumName, value, name)                                     \
   case EnumName:                                                               \
     return #EnumName;
 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
   default:
-    llvm_unreachable("Unknown type leaf kind!");
+    return formatv("UNKNOWN RECORD ({0:X})",
+                   static_cast<std::underlying_type_t<TypeLeafKind>>(K))
+        .str();
   }
-  return "";
 }
 
 std::string llvm::pdb::formatSegmentOffset(uint16_t Segment, uint32_t Offset) {
index 133a0eb..b99ccec 100644 (file)
@@ -66,7 +66,7 @@ std::string typesetStringList(uint32_t IndentLevel,
 std::string formatChunkKind(codeview::DebugSubsectionKind Kind,
                             bool Friendly = true);
 std::string formatSymbolKind(codeview::SymbolKind K);
-StringRef formatTypeLeafKind(codeview::TypeLeafKind K);
+std::string formatTypeLeafKind(codeview::TypeLeafKind K);
 
 /// Returns the number of digits in the given integer.
 inline int NumDigits(uint64_t N) {