[llvm] r251750 - Don't store a Child to the first regular member.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 31 14:44:42 PDT 2015


Author: rafael
Date: Sat Oct 31 16:44:42 2015
New Revision: 251750

URL: http://llvm.org/viewvc/llvm-project?rev=251750&view=rev
Log:
Don't store a Child to the first regular member.

This is a bit ugly, but has a few advantages:
* Archive is now easy to copy since there is no Archive -> Child -> Archive
  loop.
* It makes it clear that we already checked for errors when finding the Child
  data.

Modified:
    llvm/trunk/include/llvm/Object/Archive.h
    llvm/trunk/lib/Object/Archive.cpp

Modified: llvm/trunk/include/llvm/Object/Archive.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/Archive.h?rev=251750&r1=251749&r2=251750&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/Archive.h (original)
+++ llvm/trunk/include/llvm/Object/Archive.h Sat Oct 31 16:44:42 2015
@@ -52,6 +52,7 @@ class Archive : public Binary {
   virtual void anchor();
 public:
   class Child {
+    friend Archive;
     const Archive *Parent;
     /// \brief Includes header but not padding byte.
     StringRef Data;
@@ -66,6 +67,7 @@ public:
 
   public:
     Child(const Archive *Parent, const char *Start);
+    Child(const Archive *Parent, StringRef Data, uint16_t StartOfFile);
 
     bool operator ==(const Child &other) const {
       assert(Parent == other.Parent);
@@ -206,7 +208,11 @@ public:
 private:
   StringRef SymbolTable;
   StringRef StringTable;
-  child_iterator FirstRegular;
+
+  StringRef FirstRegularData;
+  uint16_t FirstRegularStartOfFile = -1;
+  void setFirstRegular(const Child &C);
+
   unsigned Format : 2;
   unsigned IsThin : 1;
   mutable std::vector<std::unique_ptr<MemoryBuffer>> ThinBuffers;

Modified: llvm/trunk/lib/Object/Archive.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/Archive.cpp?rev=251750&r1=251749&r2=251750&view=diff
==============================================================================
--- llvm/trunk/lib/Object/Archive.cpp (original)
+++ llvm/trunk/lib/Object/Archive.cpp Sat Oct 31 16:44:42 2015
@@ -82,6 +82,10 @@ unsigned ArchiveMemberHeader::getGID() c
   return Ret;
 }
 
+Archive::Child::Child(const Archive *Parent, StringRef Data,
+                      uint16_t StartOfFile)
+    : Parent(Parent), Data(Data), StartOfFile(StartOfFile) {}
+
 Archive::Child::Child(const Archive *Parent, const char *Start)
     : Parent(Parent) {
   if (!Start)
@@ -232,8 +236,13 @@ ErrorOr<std::unique_ptr<Archive>> Archiv
   return std::move(Ret);
 }
 
+void Archive::setFirstRegular(const Child &C) {
+  FirstRegularData = C.Data;
+  FirstRegularStartOfFile = C.StartOfFile;
+}
+
 Archive::Archive(MemoryBufferRef Source, std::error_code &ec)
-    : Binary(Binary::ID_Archive, Source), FirstRegular(child_end()) {
+    : Binary(Binary::ID_Archive, Source) {
   StringRef Buffer = Data.getBuffer();
   // Check for sufficient magic.
   if (Buffer.startswith(ThinMagic)) {
@@ -281,7 +290,7 @@ Archive::Archive(MemoryBufferRef Source,
     // there is no error.
     SymbolTable = *i->getBuffer();
     ++i;
-    FirstRegular = i;
+    setFirstRegular(*i);
     ec = std::error_code();
     return;
   }
@@ -300,7 +309,7 @@ Archive::Archive(MemoryBufferRef Source,
       SymbolTable = *i->getBuffer();
       ++i;
     }
-    FirstRegular = i;
+    setFirstRegular(*i);
     return;
   }
 
@@ -331,14 +340,14 @@ Archive::Archive(MemoryBufferRef Source,
     // ErrorOr.
     StringTable = *i->getBuffer();
     ++i;
-    FirstRegular = i;
+    setFirstRegular(*i);
     ec = std::error_code();
     return;
   }
 
   if (Name[0] != '/') {
     Format = has64SymTable ? K_MIPS64 : K_GNU;
-    FirstRegular = i;
+    setFirstRegular(*i);
     ec = std::error_code();
     return;
   }
@@ -355,7 +364,7 @@ Archive::Archive(MemoryBufferRef Source,
 
   ++i;
   if (i == e) {
-    FirstRegular = i;
+    setFirstRegular(*i);
     ec = std::error_code();
     return;
   }
@@ -369,7 +378,7 @@ Archive::Archive(MemoryBufferRef Source,
     ++i;
   }
 
-  FirstRegular = i;
+  setFirstRegular(*i);
   ec = std::error_code();
 }
 
@@ -378,7 +387,7 @@ Archive::child_iterator Archive::child_b
     return child_end();
 
   if (SkipInternal)
-    return FirstRegular;
+    return Child(this, FirstRegularData, FirstRegularStartOfFile);
 
   const char *Loc = Data.getBufferStart() + strlen(Magic);
   Child c(this, Loc);




More information about the llvm-commits mailing list