DWARFAcceleratorTable: Add an iterator-based api for accessing names in the index

Summary:
Back when we were introducing the DWARF v5 name index, there was a
short discussion whether we shouldn't have a nicer api for iterating
over the index. At that time, I did not find it necessary since the
iteration over names was done only from within the index itself (and I
figured the internal implementation can deal with a slightly rough
interface).

However, now I ran into a use for this kind of API in LLDB (for finding
all names matching a regular expression), so it looked like a nice
opportunity to introduce one. To make the API more useful, I've made the
NameTableEntry class a bit smarter: it now stores the string section
reference (so it can return its name) and its position in the name index
(mainly useful for dumping/logging).

I also convert the internal users to use the new API, which also gives
test coverage for the added code.

Reviewers: JDevlieghere, aprantl, dblaikie

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D47590

llvm-svn: 333738
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
index 852536b..c2ba9e6 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
@@ -635,7 +635,7 @@
   uint32_t StringOffset = AS.getRelocatedValue(4, &StringOffsetOffset);
   uint32_t EntryOffset = AS.getU32(&EntryOffsetOffset);
   EntryOffset += EntriesBase;
-  return {StringOffset, EntryOffset};
+  return {Section.StringSection, Index, StringOffset, EntryOffset};
 }
 
 uint32_t
@@ -670,19 +670,18 @@
   return true;
 }
 
-void DWARFDebugNames::NameIndex::dumpName(ScopedPrinter &W, uint32_t Index,
+void DWARFDebugNames::NameIndex::dumpName(ScopedPrinter &W,
+                                          const NameTableEntry &NTE,
                                           Optional<uint32_t> Hash) const {
-  const DataExtractor &SS = Section.StringSection;
-  NameTableEntry NTE = getNameTableEntry(Index);
-
-  DictScope NameScope(W, ("Name " + Twine(Index)).str());
+  DictScope NameScope(W, ("Name " + Twine(NTE.getIndex())).str());
   if (Hash)
     W.printHex("Hash", *Hash);
 
-  W.startLine() << format("String: 0x%08x", NTE.StringOffset);
-  W.getOStream() << " \"" << SS.getCStr(&NTE.StringOffset) << "\"\n";
+  W.startLine() << format("String: 0x%08x", NTE.getStringOffset());
+  W.getOStream() << " \"" << NTE.getString() << "\"\n";
 
-  while (dumpEntry(W, &NTE.EntryOffset))
+  uint32_t EntryOffset = NTE.getEntryOffset();
+  while (dumpEntry(W, &EntryOffset))
     /*empty*/;
 }
 
@@ -736,7 +735,7 @@
     if (Hash % Hdr.BucketCount != Bucket)
       break;
 
-    dumpName(W, Index, Hash);
+    dumpName(W, getNameTableEntry(Index), Hash);
   }
 }
 
@@ -755,8 +754,8 @@
   }
 
   W.startLine() << "Hash table not present\n";
-  for (uint32_t Index = 1; Index <= Hdr.NameCount; ++Index)
-    dumpName(W, Index, None);
+  for (NameTableEntry NTE : *this)
+    dumpName(W, NTE, None);
 }
 
 llvm::Error DWARFDebugNames::extract() {
@@ -787,10 +786,9 @@
   const Header &Hdr = CurrentIndex->Hdr;
   if (Hdr.BucketCount == 0) {
     // No Hash Table, We need to search through all names in the Name Index.
-    for (uint32_t Index = 1; Index <= Hdr.NameCount; ++Index) {
-      NameTableEntry NTE = CurrentIndex->getNameTableEntry(Index);
-      if (CurrentIndex->Section.StringSection.getCStr(&NTE.StringOffset) == Key)
-        return NTE.EntryOffset;
+    for (NameTableEntry NTE : *CurrentIndex) {
+      if (NTE.getString() == Key)
+        return NTE.getEntryOffset();
     }
     return None;
   }
@@ -810,8 +808,8 @@
       return None; // End of bucket
 
     NameTableEntry NTE = CurrentIndex->getNameTableEntry(Index);
-    if (CurrentIndex->Section.StringSection.getCStr(&NTE.StringOffset) == Key)
-      return NTE.EntryOffset;
+    if (NTE.getString() == Key)
+      return NTE.getEntryOffset();
   }
   return None;
 }