From e2d639cb824e88a1097a9e74b775e7eaa97001ba Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Fri, 31 Oct 2014 21:37:49 +0000 Subject: [PATCH] [Object] Modify OwningBinary's interface to separate inspection from ownership. 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. llvm-svn: 221003 --- include/llvm/Object/Binary.h | 23 ++++++++++++----------- lib/ExecutionEngine/MCJIT/MCJIT.cpp | 9 ++++++--- lib/ProfileData/CoverageMappingReader.cpp | 2 +- tools/llvm-cov/TestingSupport.cpp | 2 +- tools/llvm-symbolizer/LLVMSymbolize.cpp | 6 +++--- tools/llvm-symbolizer/LLVMSymbolize.h | 9 ++++++--- 6 files changed, 29 insertions(+), 22 deletions(-) diff --git a/include/llvm/Object/Binary.h b/include/llvm/Object/Binary.h index e4cb6f49a09..4b2b7e6835c 100644 --- a/include/llvm/Object/Binary.h +++ b/include/llvm/Object/Binary.h @@ -139,9 +139,10 @@ public: OwningBinary(OwningBinary&& Other); OwningBinary &operator=(OwningBinary &&Other); - std::unique_ptr &getBinary(); - const std::unique_ptr &getBinary() const; - std::unique_ptr &getBuffer(); + std::pair, std::unique_ptr> takeBinary(); + + T* getBinary(); + const T* getBinary() const; }; template @@ -162,18 +163,18 @@ OwningBinary &OwningBinary::operator=(OwningBinary &&Other) { return *this; } -template std::unique_ptr &OwningBinary::getBinary() { - return Bin; +template +std::pair, std::unique_ptr> +OwningBinary::takeBinary() { + return std::make_pair(std::move(Bin), std::move(Buf)); } -template -const std::unique_ptr &OwningBinary::getBinary() const { - return Bin; +template T* OwningBinary::getBinary() { + return Bin.get(); } -template -std::unique_ptr &OwningBinary::getBuffer() { - return Buf; +template const T* OwningBinary::getBinary() const { + return Bin.get(); } ErrorOr> createBinary(StringRef Path); diff --git a/lib/ExecutionEngine/MCJIT/MCJIT.cpp b/lib/ExecutionEngine/MCJIT/MCJIT.cpp index 8f1662f6ee7..da5f03799e3 100644 --- a/lib/ExecutionEngine/MCJIT/MCJIT.cpp +++ b/lib/ExecutionEngine/MCJIT/MCJIT.cpp @@ -109,8 +109,11 @@ void MCJIT::addObjectFile(std::unique_ptr Obj) { } void MCJIT::addObjectFile(object::OwningBinary Obj) { - addObjectFile(std::move(Obj.getBinary())); - Buffers.push_back(std::move(Obj.getBuffer())); + std::unique_ptr ObjFile; + std::unique_ptr MemBuf; + std::tie(ObjFile, MemBuf) = Obj.takeBinary(); + addObjectFile(std::move(ObjFile)); + Buffers.push_back(std::move(MemBuf)); } void MCJIT::addArchive(object::OwningBinary A) { @@ -290,7 +293,7 @@ uint64_t MCJIT::getSymbolAddress(const std::string &Name, return Addr; for (object::OwningBinary &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()) { diff --git a/lib/ProfileData/CoverageMappingReader.cpp b/lib/ProfileData/CoverageMappingReader.cpp index ce99f4d8a7b..6476d28ec35 100644 --- a/lib/ProfileData/CoverageMappingReader.cpp +++ b/lib/ProfileData/CoverageMappingReader.cpp @@ -485,7 +485,7 @@ ObjectFileCoverageMappingReader::ObjectFileCoverageMappingReader( } std::error_code ObjectFileCoverageMappingReader::readHeader() { - ObjectFile *OF = Object.getBinary().get(); + const ObjectFile *OF = Object.getBinary(); if (!OF) return getError(); auto BytesInAddress = OF->getBytesInAddress(); diff --git a/tools/llvm-cov/TestingSupport.cpp b/tools/llvm-cov/TestingSupport.cpp index aa07a79e78d..537f133c647 100644 --- a/tools/llvm-cov/TestingSupport.cpp +++ b/tools/llvm-cov/TestingSupport.cpp @@ -41,7 +41,7 @@ int convertForTestingMain(int argc, const char *argv[]) { 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"; diff --git a/tools/llvm-symbolizer/LLVMSymbolize.cpp b/tools/llvm-symbolizer/LLVMSymbolize.cpp index 31bbedf8f03..760d83bf38a 100644 --- a/tools/llvm-symbolizer/LLVMSymbolize.cpp +++ b/tools/llvm-symbolizer/LLVMSymbolize.cpp @@ -326,7 +326,7 @@ ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath, if (EC != errc::no_such_file_or_directory && !error(EC)) { OwningBinary B = std::move(BinaryOrErr.get()); ObjectFile *DbgObj = - getObjectFileFromBinary(B.getBinary().get(), ArchName); + getObjectFileFromBinary(B.getBinary(), ArchName); const MachOObjectFile *MachDbgObj = dyn_cast(DbgObj); if (!MachDbgObj) continue; @@ -350,7 +350,7 @@ LLVMSymbolizer::getOrCreateObjects(const std::string &Path, ErrorOr> BinaryOrErr = createBinary(Path); if (!error(BinaryOrErr.getError())) { OwningBinary &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; @@ -369,7 +369,7 @@ LLVMSymbolizer::getOrCreateObjects(const std::string &Path, BinaryOrErr = createBinary(DebugBinaryPath); if (!error(BinaryOrErr.getError())) { OwningBinary B = std::move(BinaryOrErr.get()); - DbgObj = getObjectFileFromBinary(B.getBinary().get(), ArchName); + DbgObj = getObjectFileFromBinary(B.getBinary(), ArchName); addOwningBinary(std::move(B)); } } diff --git a/tools/llvm-symbolizer/LLVMSymbolize.h b/tools/llvm-symbolizer/LLVMSymbolize.h index 52f1fc99171..db3f56237b4 100644 --- a/tools/llvm-symbolizer/LLVMSymbolize.h +++ b/tools/llvm-symbolizer/LLVMSymbolize.h @@ -81,9 +81,12 @@ private: // Owns all the parsed binaries and object files. SmallVector, 4> ParsedBinariesAndObjects; SmallVector, 4> MemoryBuffers; - void addOwningBinary(OwningBinary Bin) { - ParsedBinariesAndObjects.push_back(std::move(Bin.getBinary())); - MemoryBuffers.push_back(std::move(Bin.getBuffer())); + void addOwningBinary(OwningBinary OwningBin) { + std::unique_ptr Bin; + std::unique_ptr MemBuf; + std::tie(Bin, MemBuf) = OwningBin.takeBinary(); + ParsedBinariesAndObjects.push_back(std::move(Bin)); + MemoryBuffers.push_back(std::move(MemBuf)); } // Owns module info objects.