[llvm] r319345 - Make TypeTableBuilder inherit from TypeCollection.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 29 11:35:22 PST 2017


Author: zturner
Date: Wed Nov 29 11:35:21 2017
New Revision: 319345

URL: http://llvm.org/viewvc/llvm-project?rev=319345&view=rev
Log:
Make TypeTableBuilder inherit from TypeCollection.

A couple of places in LLD were passing references to
TypeTableCollections around, which makes it hard to change the
implementation at runtime.  However, these cases only needed to
iterate over the types in the collection, and TypeCollection
already provides a handy abstract interface for this purpose.

By implementing this interface, we can get rid of the need to
pass TypeTableBuilder references around, which should allow us
to swap the implementation at runtime in subsequent patches.

Modified:
    llvm/trunk/include/llvm/DebugInfo/CodeView/TypeCollection.h
    llvm/trunk/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h
    llvm/trunk/lib/DebugInfo/CodeView/TypeTableBuilder.cpp
    llvm/trunk/tools/llvm-pdbutil/llvm-pdbutil.cpp
    llvm/trunk/tools/llvm-readobj/COFFDumper.cpp

Modified: llvm/trunk/include/llvm/DebugInfo/CodeView/TypeCollection.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/TypeCollection.h?rev=319345&r1=319344&r2=319345&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/CodeView/TypeCollection.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/CodeView/TypeCollection.h Wed Nov 29 11:35:21 2017
@@ -31,6 +31,16 @@ public:
   virtual bool contains(TypeIndex Index) = 0;
   virtual uint32_t size() = 0;
   virtual uint32_t capacity() = 0;
+
+  template <typename TFunc> void ForEachRecord(TFunc Func) {
+    Optional<TypeIndex> Next = getFirst();
+
+    while (Next.hasValue()) {
+      TypeIndex N = *Next;
+      Func(N, getType(N));
+      Next = getNext(N);
+    }
+  }
 };
 }
 }

Modified: llvm/trunk/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h?rev=319345&r1=319344&r2=319345&view=diff
==============================================================================
--- llvm/trunk/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h (original)
+++ llvm/trunk/include/llvm/DebugInfo/CodeView/TypeTableBuilder.h Wed Nov 29 11:35:21 2017
@@ -14,15 +14,11 @@
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/DebugInfo/CodeView/CodeView.h"
-#include "llvm/DebugInfo/CodeView/RecordSerialization.h"
 #include "llvm/DebugInfo/CodeView/SimpleTypeSerializer.h"
+#include "llvm/DebugInfo/CodeView/TypeCollection.h"
 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
 #include "llvm/DebugInfo/CodeView/TypeRecord.h"
-#include "llvm/DebugInfo/CodeView/TypeRecordMapping.h"
-#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
 #include "llvm/Support/Allocator.h"
-#include "llvm/Support/BinaryByteStream.h"
-#include "llvm/Support/BinaryStreamWriter.h"
 #include "llvm/Support/Error.h"
 #include <cassert>
 #include <cstdint>
@@ -35,7 +31,7 @@ namespace codeview {
 class ContinuationRecordBuilder;
 class TypeHasher;
 
-class TypeTableBuilder {
+class TypeTableBuilder : public TypeCollection {
 
   BumpPtrAllocator &RecordStorage;
   SimpleTypeSerializer SimpleSerializer;
@@ -54,10 +50,17 @@ public:
   explicit TypeTableBuilder(BumpPtrAllocator &Storage, bool Hash = true);
   ~TypeTableBuilder();
 
-  void reset();
-
-  bool empty() const { return SeenRecords.empty(); }
+  // TypeTableCollection overrides
+  Optional<TypeIndex> getFirst() override;
+  Optional<TypeIndex> getNext(TypeIndex Prev) override;
+  CVType getType(TypeIndex Index) override;
+  StringRef getTypeName(TypeIndex Index) override;
+  bool contains(TypeIndex Index) override;
+  uint32_t size() override;
+  uint32_t capacity() override;
 
+  // public interface
+  void reset();
   TypeIndex nextTypeIndex() const;
 
   BumpPtrAllocator &getAllocator() { return RecordStorage; }
@@ -71,15 +74,6 @@ public:
     ArrayRef<uint8_t> Data = SimpleSerializer.serialize(Record);
     return insertRecordBytes(Data);
   }
-
-  template <typename TFunc> void ForEachRecord(TFunc Func) {
-    uint32_t Index = TypeIndex::FirstNonSimpleIndex;
-
-    for (auto Record : SeenRecords) {
-      Func(TypeIndex(Index), Record);
-      ++Index;
-    }
-  }
 };
 
 } // end namespace codeview

Modified: llvm/trunk/lib/DebugInfo/CodeView/TypeTableBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/CodeView/TypeTableBuilder.cpp?rev=319345&r1=319344&r2=319345&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/CodeView/TypeTableBuilder.cpp (original)
+++ llvm/trunk/lib/DebugInfo/CodeView/TypeTableBuilder.cpp Wed Nov 29 11:35:21 2017
@@ -142,6 +142,43 @@ TypeTableBuilder::TypeTableBuilder(BumpP
 
 TypeTableBuilder::~TypeTableBuilder() = default;
 
+Optional<TypeIndex> TypeTableBuilder::getFirst() {
+  if (empty())
+    return None;
+
+  return TypeIndex(TypeIndex::FirstNonSimpleIndex);
+}
+
+Optional<TypeIndex> TypeTableBuilder::getNext(TypeIndex Prev) {
+  if (++Prev == nextTypeIndex())
+    return None;
+  return Prev;
+}
+
+CVType TypeTableBuilder::getType(TypeIndex Index) {
+  CVType Type;
+  Type.RecordData = SeenRecords[Index.toArrayIndex()];
+  const RecordPrefix *P =
+      reinterpret_cast<const RecordPrefix *>(Type.RecordData.data());
+  Type.Type = static_cast<TypeLeafKind>(uint16_t(P->RecordKind));
+  return Type;
+}
+
+StringRef TypeTableBuilder::getTypeName(TypeIndex Index) {
+  llvm_unreachable("Method not implemented");
+}
+
+bool TypeTableBuilder::contains(TypeIndex Index) {
+  if (Index.isSimple() || Index.isNoneType())
+    return false;
+
+  return Index.toArrayIndex() < SeenRecords.size();
+}
+
+uint32_t TypeTableBuilder::size() { return SeenRecords.size(); }
+
+uint32_t TypeTableBuilder::capacity() { return SeenRecords.size(); }
+
 ArrayRef<ArrayRef<uint8_t>> TypeTableBuilder::records() const {
   return SeenRecords;
 }

Modified: llvm/trunk/tools/llvm-pdbutil/llvm-pdbutil.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-pdbutil/llvm-pdbutil.cpp?rev=319345&r1=319344&r2=319345&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-pdbutil/llvm-pdbutil.cpp (original)
+++ llvm/trunk/tools/llvm-pdbutil/llvm-pdbutil.cpp Wed Nov 29 11:35:21 2017
@@ -1020,11 +1020,11 @@ static void mergePdbs() {
 
   auto &DestTpi = Builder.getTpiBuilder();
   auto &DestIpi = Builder.getIpiBuilder();
-  MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, ArrayRef<uint8_t> Data) {
-    DestTpi.addTypeRecord(Data, None);
+  MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, const CVType &Type) {
+    DestTpi.addTypeRecord(Type.RecordData, None);
   });
-  MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, ArrayRef<uint8_t> Data) {
-    DestIpi.addTypeRecord(Data, None);
+  MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, const CVType &Type) {
+    DestIpi.addTypeRecord(Type.RecordData, None);
   });
   Builder.getInfoBuilder().addFeature(PdbRaw_FeatureSig::VC140);
 

Modified: llvm/trunk/tools/llvm-readobj/COFFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/COFFDumper.cpp?rev=319345&r1=319344&r2=319345&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/COFFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/COFFDumper.cpp Wed Nov 29 11:35:21 2017
@@ -1808,8 +1808,8 @@ void llvm::dumpCodeViewMergedTypes(Scope
                                    llvm::codeview::TypeTableBuilder &CVTypes) {
   // Flatten it first, then run our dumper on it.
   SmallString<0> TypeBuf;
-  CVTypes.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Record) {
-    TypeBuf.append(Record.begin(), Record.end());
+  CVTypes.ForEachRecord([&](TypeIndex TI, const CVType &Record) {
+    TypeBuf.append(Record.RecordData.begin(), Record.RecordData.end());
   });
 
   TypeTableCollection TpiTypes(CVTypes.records());




More information about the llvm-commits mailing list