From fb1af0a48a61fff1bc70906898b7925280fc81aa Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 26 Aug 2014 22:00:09 +0000 Subject: [PATCH] Pass a std::unique_ptr& to getLazyBitcodeModule. By taking a reference we can do the ownership transfer in one place instead of expecting every caller to do it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216492 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Bitcode/ReaderWriter.h | 3 ++- lib/Bitcode/Reader/BitReader.cpp | 5 ++++- lib/Bitcode/Reader/BitcodeReader.cpp | 18 +++++++++--------- lib/IRReader/IRReader.cpp | 4 +--- lib/Object/IRObjectFile.cpp | 3 +-- tools/gold/gold-plugin.cpp | 3 +-- unittests/Bitcode/BitReaderTest.cpp | 3 ++- unittests/ExecutionEngine/JIT/JITTest.cpp | 5 ++--- 8 files changed, 22 insertions(+), 22 deletions(-) diff --git a/include/llvm/Bitcode/ReaderWriter.h b/include/llvm/Bitcode/ReaderWriter.h index c1eca8d9eaf..287311032f2 100644 --- a/include/llvm/Bitcode/ReaderWriter.h +++ b/include/llvm/Bitcode/ReaderWriter.h @@ -16,6 +16,7 @@ #include "llvm/Support/ErrorOr.h" #include "llvm/Support/MemoryBuffer.h" +#include #include namespace llvm { @@ -29,7 +30,7 @@ namespace llvm { /// Read the header of the specified bitcode buffer and prepare for lazy /// deserialization of function bodies. If successful, this takes ownership /// of 'buffer. On error, this *does not* take ownership of Buffer. - ErrorOr getLazyBitcodeModule(MemoryBuffer *Buffer, + ErrorOr getLazyBitcodeModule(std::unique_ptr &Buffer, LLVMContext &Context); /// getStreamedBitcodeModule - Read the header of the specified stream diff --git a/lib/Bitcode/Reader/BitReader.cpp b/lib/Bitcode/Reader/BitReader.cpp index e21d29ac374..76a7e6ae159 100644 --- a/lib/Bitcode/Reader/BitReader.cpp +++ b/lib/Bitcode/Reader/BitReader.cpp @@ -51,8 +51,11 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, LLVMModuleRef *OutM, char **OutMessage) { std::string Message; + std::unique_ptr Owner(unwrap(MemBuf)); + ErrorOr ModuleOrErr = - getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef)); + getLazyBitcodeModule(Owner, *unwrap(ContextRef)); + Owner.release(); if (std::error_code EC = ModuleOrErr.getError()) { *OutM = wrap((Module *)nullptr); diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index 8dc0aa32315..1b88e5f5fa5 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -3519,11 +3519,11 @@ const std::error_category &llvm::BitcodeErrorCategory() { /// /// \param[in] WillMaterializeAll Set to \c true if the caller promises to /// materialize everything -- in particular, if this isn't truly lazy. -static ErrorOr getLazyBitcodeModuleImpl(MemoryBuffer *Buffer, - LLVMContext &Context, - bool WillMaterializeAll) { +static ErrorOr +getLazyBitcodeModuleImpl(std::unique_ptr &Buffer, + LLVMContext &Context, bool WillMaterializeAll) { Module *M = new Module(Buffer->getBufferIdentifier(), Context); - BitcodeReader *R = new BitcodeReader(Buffer, Context); + BitcodeReader *R = new BitcodeReader(Buffer.get(), Context); M->setMaterializer(R); auto cleanupOnError = [&](std::error_code EC) { @@ -3540,11 +3540,13 @@ static ErrorOr getLazyBitcodeModuleImpl(MemoryBuffer *Buffer, if (std::error_code EC = R->materializeForwardReferencedFunctions()) return cleanupOnError(EC); + Buffer.release(); // The BitcodeReader owns it now. return M; } -ErrorOr llvm::getLazyBitcodeModule(MemoryBuffer *Buffer, - LLVMContext &Context) { +ErrorOr +llvm::getLazyBitcodeModule(std::unique_ptr &Buffer, + LLVMContext &Context) { return getLazyBitcodeModuleImpl(Buffer, Context, false); } @@ -3567,11 +3569,9 @@ Module *llvm::getStreamedBitcodeModule(const std::string &name, ErrorOr llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context) { std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false); - ErrorOr ModuleOrErr = - getLazyBitcodeModuleImpl(Buf.get(), Context, true); + ErrorOr ModuleOrErr = getLazyBitcodeModuleImpl(Buf, Context, true); if (!ModuleOrErr) return ModuleOrErr; - Buf.release(); // The BitcodeReader owns it now. Module *M = ModuleOrErr.get(); // Read in the entire module, and destroy the BitcodeReader. if (std::error_code EC = M->materializeAllPermanently()) { diff --git a/lib/IRReader/IRReader.cpp b/lib/IRReader/IRReader.cpp index 5c3124821a7..a50e3865285 100644 --- a/lib/IRReader/IRReader.cpp +++ b/lib/IRReader/IRReader.cpp @@ -35,14 +35,12 @@ getLazyIRModule(std::unique_ptr Buffer, SMDiagnostic &Err, if (isBitcode((const unsigned char *)Buffer->getBufferStart(), (const unsigned char *)Buffer->getBufferEnd())) { std::string ErrMsg; - ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer.get(), Context); + ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context); if (std::error_code EC = ModuleOrErr.getError()) { Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, EC.message()); return nullptr; } - // getLazyBitcodeModule takes ownership of the Buffer when successful. - Buffer.release(); return std::unique_ptr(ModuleOrErr.get()); } diff --git a/lib/Object/IRObjectFile.cpp b/lib/Object/IRObjectFile.cpp index 964c7ed691d..4c050d5badb 100644 --- a/lib/Object/IRObjectFile.cpp +++ b/lib/Object/IRObjectFile.cpp @@ -270,10 +270,9 @@ llvm::object::IRObjectFile::createIRObjectFile(MemoryBufferRef Object, std::unique_ptr Buff(MemoryBuffer::getMemBuffer(Object, false)); - ErrorOr MOrErr = getLazyBitcodeModule(Buff.get(), Context); + ErrorOr MOrErr = getLazyBitcodeModule(Buff, Context); if (std::error_code EC = MOrErr.getError()) return EC; - Buff.release(); std::unique_ptr M(MOrErr.get()); return new IRObjectFile(Object, std::move(M)); diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index 55151e0352b..33602563731 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -552,12 +552,11 @@ getModuleForFile(LLVMContext &Context, claimed_file &F, raw_fd_ostream *ApiFile, if (release_input_file(F.handle) != LDPS_OK) message(LDPL_FATAL, "Failed to release file information"); - ErrorOr MOrErr = getLazyBitcodeModule(Buffer.get(), Context); + ErrorOr MOrErr = getLazyBitcodeModule(Buffer, Context); if (std::error_code EC = MOrErr.getError()) message(LDPL_FATAL, "Could not read bitcode from file : %s", EC.message().c_str()); - Buffer.release(); std::unique_ptr M(MOrErr.get()); diff --git a/unittests/Bitcode/BitReaderTest.cpp b/unittests/Bitcode/BitReaderTest.cpp index adc8851f279..663024679c9 100644 --- a/unittests/Bitcode/BitReaderTest.cpp +++ b/unittests/Bitcode/BitReaderTest.cpp @@ -51,7 +51,8 @@ static std::unique_ptr getLazyModuleFromAssembly(LLVMContext &Context, SmallString<1024> &Mem, const char *Assembly) { writeModuleToBuffer(parseAssembly(Assembly), Mem); - MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false); + std::unique_ptr Buffer( + MemoryBuffer::getMemBuffer(Mem.str(), "test", false)); ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context); return std::unique_ptr(ModuleOrErr.get()); } diff --git a/unittests/ExecutionEngine/JIT/JITTest.cpp b/unittests/ExecutionEngine/JIT/JITTest.cpp index 4ba54b1d486..55b86125776 100644 --- a/unittests/ExecutionEngine/JIT/JITTest.cpp +++ b/unittests/ExecutionEngine/JIT/JITTest.cpp @@ -632,12 +632,11 @@ std::string AssembleToBitcode(LLVMContext &Context, const char *Assembly) { ExecutionEngine *getJITFromBitcode( LLVMContext &Context, const std::string &Bitcode, Module *&M) { // c_str() is null-terminated like MemoryBuffer::getMemBuffer requires. - MemoryBuffer *BitcodeBuffer = - MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test"); + std::unique_ptr BitcodeBuffer( + MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test")); ErrorOr ModuleOrErr = getLazyBitcodeModule(BitcodeBuffer, Context); if (std::error_code EC = ModuleOrErr.getError()) { ADD_FAILURE() << EC.message(); - delete BitcodeBuffer; return nullptr; } std::unique_ptr Owner(ModuleOrErr.get()); -- 2.34.1