[Object] Modify OwningBinary's interface to separate inspection from ownership.
authorLang Hames <lhames@gmail.com>
Fri, 31 Oct 2014 21:37:49 +0000 (21:37 +0000)
committerLang Hames <lhames@gmail.com>
Fri, 31 Oct 2014 21:37:49 +0000 (21:37 +0000)
The getBinary and getBuffer method now return ordinary pointers of appropriate
const-ness. Ownership is transferred by calling takeBinary(), which returns a
pair of the Binary and a MemoryBuffer.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221003 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Object/Binary.h
lib/ExecutionEngine/MCJIT/MCJIT.cpp
lib/ProfileData/CoverageMappingReader.cpp
tools/llvm-cov/TestingSupport.cpp
tools/llvm-symbolizer/LLVMSymbolize.cpp
tools/llvm-symbolizer/LLVMSymbolize.h

index e4cb6f49a09e693f027f6ccfe9b8f0a6b0cbd6ba..4b2b7e6835ccdf7e68407eb472574c0abf40eaba 100644 (file)
@@ -139,9 +139,10 @@ public:
   OwningBinary(OwningBinary<T>&& Other);
   OwningBinary<T> &operator=(OwningBinary<T> &&Other);
 
   OwningBinary(OwningBinary<T>&& Other);
   OwningBinary<T> &operator=(OwningBinary<T> &&Other);
 
-  std::unique_ptr<T> &getBinary();
-  const std::unique_ptr<T> &getBinary() const;
-  std::unique_ptr<MemoryBuffer> &getBuffer();
+  std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
+
+  T* getBinary();
+  const T* getBinary() const;
 };
 
 template <typename T>
 };
 
 template <typename T>
@@ -162,18 +163,18 @@ OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
   return *this;
 }
 
   return *this;
 }
 
-template <typename T> std::unique_ptr<T> &OwningBinary<T>::getBinary() {
-  return Bin;
+template <typename T>
+std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
+OwningBinary<T>::takeBinary() {
+  return std::make_pair(std::move(Bin), std::move(Buf));
 }
 
 }
 
-template <typename T>
-const std::unique_ptr<T> &OwningBinary<T>::getBinary() const {
-  return Bin;
+template <typename T> T* OwningBinary<T>::getBinary() {
+  return Bin.get();
 }
 
 }
 
-template <typename T>
-std::unique_ptr<MemoryBuffer> &OwningBinary<T>::getBuffer() {
-  return Buf;
+template <typename T> const T* OwningBinary<T>::getBinary() const {
+  return Bin.get();
 }
 
 ErrorOr<OwningBinary<Binary>> createBinary(StringRef Path);
 }
 
 ErrorOr<OwningBinary<Binary>> createBinary(StringRef Path);
index 8f1662f6ee7c74b36d096d803f62cae0ad925a1c..da5f03799e354326a2e31bcc2aea322a64cb8a09 100644 (file)
@@ -109,8 +109,11 @@ void MCJIT::addObjectFile(std::unique_ptr<object::ObjectFile> Obj) {
 }
 
 void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) {
 }
 
 void MCJIT::addObjectFile(object::OwningBinary<object::ObjectFile> Obj) {
-  addObjectFile(std::move(Obj.getBinary()));
-  Buffers.push_back(std::move(Obj.getBuffer()));
+  std::unique_ptr<object::ObjectFile> ObjFile;
+  std::unique_ptr<MemoryBuffer> MemBuf;
+  std::tie(ObjFile, MemBuf) = Obj.takeBinary();
+  addObjectFile(std::move(ObjFile));
+  Buffers.push_back(std::move(MemBuf));
 }
 
 void MCJIT::addArchive(object::OwningBinary<object::Archive> A) {
 }
 
 void MCJIT::addArchive(object::OwningBinary<object::Archive> A) {
@@ -290,7 +293,7 @@ uint64_t MCJIT::getSymbolAddress(const std::string &Name,
     return Addr;
 
   for (object::OwningBinary<object::Archive> &OB : Archives) {
     return Addr;
 
   for (object::OwningBinary<object::Archive> &OB : Archives) {
-    object::Archive *A = OB.getBinary().get();
+    object::Archive *A = OB.getBinary();
     // Look for our symbols in each Archive
     object::Archive::child_iterator ChildIt = A->findSym(Name);
     if (ChildIt != A->child_end()) {
     // Look for our symbols in each Archive
     object::Archive::child_iterator ChildIt = A->findSym(Name);
     if (ChildIt != A->child_end()) {
index ce99f4d8a7b0892ca9c3c4849034242670f2deca..6476d28ec35d2d8f1b663b4c8d7397c81f272a43 100644 (file)
@@ -485,7 +485,7 @@ ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader(
 }
 
 std::error_code ObjectFileCoverageMappingReader::readHeader() {
 }
 
 std::error_code ObjectFileCoverageMappingReader::readHeader() {
-  ObjectFile *OF = Object.getBinary().get();
+  const ObjectFile *OF = Object.getBinary();
   if (!OF)
     return getError();
   auto BytesInAddress = OF->getBytesInAddress();
   if (!OF)
     return getError();
   auto BytesInAddress = OF->getBytesInAddress();
index aa07a79e78db369e44b3377981272f269799375b..537f133c64762f66cc05fe8a1d607dfbf8c6e225 100644 (file)
@@ -41,7 +41,7 @@ int convertForTestingMain(int argc, const char *argv[]) {
     errs() << "error: " << Err.message() << "\n";
     return 1;
   }
     errs() << "error: " << Err.message() << "\n";
     return 1;
   }
-  ObjectFile *OF = ObjErr.get().getBinary().get();
+  ObjectFile *OF = ObjErr.get().getBinary();
   auto BytesInAddress = OF->getBytesInAddress();
   if (BytesInAddress != 8) {
     errs() << "error: 64 bit binary expected\n";
   auto BytesInAddress = OF->getBytesInAddress();
   if (BytesInAddress != 8) {
     errs() << "error: 64 bit binary expected\n";
index 31bbedf8f032b58e1d4ef34a8a99739b975c6e2e..760d83bf38acf854331ddbaf4008dd1101b50de0 100644 (file)
@@ -326,7 +326,7 @@ ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
     if (EC != errc::no_such_file_or_directory && !error(EC)) {
       OwningBinary<Binary> B = std::move(BinaryOrErr.get());
       ObjectFile *DbgObj =
     if (EC != errc::no_such_file_or_directory && !error(EC)) {
       OwningBinary<Binary> B = std::move(BinaryOrErr.get());
       ObjectFile *DbgObj =
-          getObjectFileFromBinary(B.getBinary().get(), ArchName);
+          getObjectFileFromBinary(B.getBinary(), ArchName);
       const MachOObjectFile *MachDbgObj =
           dyn_cast<const MachOObjectFile>(DbgObj);
       if (!MachDbgObj) continue;
       const MachOObjectFile *MachDbgObj =
           dyn_cast<const MachOObjectFile>(DbgObj);
       if (!MachDbgObj) continue;
@@ -350,7 +350,7 @@ LLVMSymbolizer::getOrCreateObjects(const std::string &Path,
   ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
   if (!error(BinaryOrErr.getError())) {
     OwningBinary<Binary> &B = BinaryOrErr.get();
   ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
   if (!error(BinaryOrErr.getError())) {
     OwningBinary<Binary> &B = BinaryOrErr.get();
-    Obj = getObjectFileFromBinary(B.getBinary().get(), ArchName);
+    Obj = getObjectFileFromBinary(B.getBinary(), ArchName);
     if (!Obj) {
       ObjectPair Res = std::make_pair(nullptr, nullptr);
       ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
     if (!Obj) {
       ObjectPair Res = std::make_pair(nullptr, nullptr);
       ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
@@ -369,7 +369,7 @@ LLVMSymbolizer::getOrCreateObjects(const std::string &Path,
         BinaryOrErr = createBinary(DebugBinaryPath);
         if (!error(BinaryOrErr.getError())) {
           OwningBinary<Binary> B = std::move(BinaryOrErr.get());
         BinaryOrErr = createBinary(DebugBinaryPath);
         if (!error(BinaryOrErr.getError())) {
           OwningBinary<Binary> B = std::move(BinaryOrErr.get());
-          DbgObj = getObjectFileFromBinary(B.getBinary().get(), ArchName);
+          DbgObj = getObjectFileFromBinary(B.getBinary(), ArchName);
           addOwningBinary(std::move(B));
         }
       }
           addOwningBinary(std::move(B));
         }
       }
index 52f1fc99171569e81689c1225d2f370cea19d618..db3f56237b47f65c8fada1e03aa494eec9fb8c95 100644 (file)
@@ -81,9 +81,12 @@ private:
   // Owns all the parsed binaries and object files.
   SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
   SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
   // Owns all the parsed binaries and object files.
   SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
   SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
-  void addOwningBinary(OwningBinary<Binary> Bin) {
-    ParsedBinariesAndObjects.push_back(std::move(Bin.getBinary()));
-    MemoryBuffers.push_back(std::move(Bin.getBuffer()));
+  void addOwningBinary(OwningBinary<Binary> OwningBin) {
+    std::unique_ptr<Binary> Bin;
+    std::unique_ptr<MemoryBuffer> MemBuf;
+    std::tie(Bin, MemBuf) = OwningBin.takeBinary();
+    ParsedBinariesAndObjects.push_back(std::move(Bin));
+    MemoryBuffers.push_back(std::move(MemBuf));
   }
 
   // Owns module info objects.
   }
 
   // Owns module info objects.