[llvm-dwp] Don't rely on implicit move assignment operator (MSVC won't synthesize...
[oota-llvm.git] / lib / DebugInfo / DWARF / DWARFUnitIndex.cpp
index 0bb786d7b724b33a17e0b6487a06483c7865ebbe..1f1921649b57ae760ce9d711a53c11e0b8def6be 100644 (file)
@@ -30,6 +30,18 @@ void DWARFUnitIndex::Header::dump(raw_ostream &OS) const {
 }
 
 bool DWARFUnitIndex::parse(DataExtractor IndexData) {
+  bool b = parseImpl(IndexData);
+  if (!b) {
+    // Make sure we don't try to dump anything
+    Header.NumBuckets = 0;
+    // Release any partially initialized data.
+    ColumnKinds.reset();
+    Rows.reset();
+  }
+  return b;
+}
+
+bool DWARFUnitIndex::parseImpl(DataExtractor IndexData) {
   uint32_t Offset = 0;
   if (!Header.parse(IndexData, &Offset))
     return false;
@@ -39,10 +51,10 @@ bool DWARFUnitIndex::parse(DataExtractor IndexData) {
                       (2 * Header.NumUnits + 1) * 4 * Header.NumColumns))
     return false;
 
-  Rows = llvm::make_unique<HashRow[]>(Header.NumBuckets);
+  Rows = llvm::make_unique<Entry[]>(Header.NumBuckets);
   auto Contribs =
-      llvm::make_unique<HashRow::SectionContribution *[]>(Header.NumUnits);
-  ColumnKinds = llvm::make_unique<DwarfSection[]>(Header.NumColumns);
+      llvm::make_unique<Entry::SectionContribution *[]>(Header.NumUnits);
+  ColumnKinds = llvm::make_unique<DWARFSectionKind[]>(Header.NumColumns);
 
   // Read Hash Table of Signatures
   for (unsigned i = 0; i != Header.NumBuckets; ++i)
@@ -53,35 +65,43 @@ bool DWARFUnitIndex::parse(DataExtractor IndexData) {
     auto Index = IndexData.getU32(&Offset);
     if (!Index)
       continue;
+    Rows[i].Index = this;
     Rows[i].Contributions =
-        llvm::make_unique<HashRow::SectionContribution[]>(Header.NumColumns);
+        llvm::make_unique<Entry::SectionContribution[]>(Header.NumColumns);
     Contribs[Index - 1] = Rows[i].Contributions.get();
   }
 
   // Read the Column Headers
-  for (unsigned i = 0; i != Header.NumColumns; ++i)
-    ColumnKinds[i] = static_cast<DwarfSection>(IndexData.getU32(&Offset));
+  for (unsigned i = 0; i != Header.NumColumns; ++i) {
+    ColumnKinds[i] = static_cast<DWARFSectionKind>(IndexData.getU32(&Offset));
+    if (ColumnKinds[i] == InfoColumnKind) {
+      if (InfoColumn != -1)
+        return false;
+      InfoColumn = i;
+    }
+  }
+
+  if (InfoColumn == -1)
+    return false;
 
   // Read Table of Section Offsets
   for (unsigned i = 0; i != Header.NumUnits; ++i) {
     auto *Contrib = Contribs[i];
-    for (unsigned i = 0; i != Header.NumColumns; ++i) {
+    for (unsigned i = 0; i != Header.NumColumns; ++i)
       Contrib[i].Offset = IndexData.getU32(&Offset);
-    }
   }
 
   // Read Table of Section Sizes
   for (unsigned i = 0; i != Header.NumUnits; ++i) {
     auto *Contrib = Contribs[i];
-    for (unsigned i = 0; i != Header.NumColumns; ++i) {
-      Contrib[i].Size = IndexData.getU32(&Offset);
-    }
+    for (unsigned i = 0; i != Header.NumColumns; ++i)
+      Contrib[i].Length = IndexData.getU32(&Offset);
   }
 
   return true;
 }
 
-StringRef DWARFUnitIndex::getColumnHeader(DwarfSection DS) {
+StringRef DWARFUnitIndex::getColumnHeader(DWARFSectionKind DS) {
 #define CASE(DS)                                                               \
   case DW_SECT_##DS:                                                           \
     return #DS;
@@ -95,14 +115,17 @@ StringRef DWARFUnitIndex::getColumnHeader(DwarfSection DS) {
     CASE(MACINFO);
     CASE(MACRO);
   }
-  llvm_unreachable("unknown DwarfSection");
+  llvm_unreachable("unknown DWARFSectionKind");
 }
 
 void DWARFUnitIndex::dump(raw_ostream &OS) const {
+  if (!Header.NumBuckets)
+    return;
+
   Header.dump(OS);
   OS << "Index Signature         ";
   for (unsigned i = 0; i != Header.NumColumns; ++i)
-    OS << format(" %-24s", getColumnHeader(ColumnKinds[i]));
+    OS << ' ' << left_justify(getColumnHeader(ColumnKinds[i]), 24);
   OS << "\n----- ------------------";
   for (unsigned i = 0; i != Header.NumColumns; ++i)
     OS << " ------------------------";
@@ -113,11 +136,33 @@ void DWARFUnitIndex::dump(raw_ostream &OS) const {
       OS << format("%5u 0x%016" PRIx64 " ", i, Row.Signature);
       for (unsigned i = 0; i != Header.NumColumns; ++i) {
         auto &Contrib = Contribs[i];
-        OS << format("[0x%08u, 0x%08u) ", Contrib.Offset,
-                     Contrib.Offset + Contrib.Size);
+        OS << format("[0x%08x, 0x%08x) ", Contrib.Offset,
+                     Contrib.Offset + Contrib.Length);
       }
       OS << '\n';
     }
   }
 }
+
+const DWARFUnitIndex::Entry::SectionContribution *
+DWARFUnitIndex::Entry::getOffset(DWARFSectionKind Sec) const {
+  uint32_t i = 0;
+  for (; i != Index->Header.NumColumns; ++i)
+    if (Index->ColumnKinds[i] == Sec)
+      return &Contributions[i];
+  return nullptr;
+}
+const DWARFUnitIndex::Entry::SectionContribution *
+DWARFUnitIndex::Entry::getOffset() const {
+  return &Contributions[Index->InfoColumn];
+}
+
+const DWARFUnitIndex::Entry *
+DWARFUnitIndex::getFromOffset(uint32_t Offset) const {
+  for (uint32_t i = 0; i != Header.NumBuckets; ++i)
+    if (const auto &Contribs = Rows[i].Contributions)
+      if (Contribs[InfoColumn].Offset == Offset)
+        return &Rows[i];
+  return nullptr;
+}
 }