From 20a6785cd235990cc8cb3e2e402b7f8caca609ec Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 16 Jun 2015 22:27:55 +0000 Subject: [PATCH] Return a unique_ptr from getLazyBitcodeModule and parseBitcodeFile. NFC. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239858 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Bitcode/ReaderWriter.h | 4 +-- lib/Bitcode/Reader/BitReader.cpp | 8 ++--- lib/Bitcode/Reader/BitcodeReader.cpp | 30 ++++++++----------- lib/IRReader/IRReader.cpp | 9 +++--- lib/LTO/LTOModule.cpp | 23 +++++++------- lib/Object/IRObjectFile.cpp | 4 +-- .../verify-uselistorder.cpp | 4 +-- unittests/Bitcode/BitReaderTest.cpp | 4 +-- 8 files changed, 42 insertions(+), 44 deletions(-) diff --git a/include/llvm/Bitcode/ReaderWriter.h b/include/llvm/Bitcode/ReaderWriter.h index 9d30098fdde..b3b944515c7 100644 --- a/include/llvm/Bitcode/ReaderWriter.h +++ b/include/llvm/Bitcode/ReaderWriter.h @@ -32,7 +32,7 @@ namespace llvm { /// deserialization of function bodies. If ShouldLazyLoadMetadata is true, /// lazily load metadata as well. If successful, this moves Buffer. On /// error, this *does not* move Buffer. - ErrorOr + ErrorOr> getLazyBitcodeModule(std::unique_ptr &&Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler = nullptr, @@ -52,7 +52,7 @@ namespace llvm { DiagnosticHandlerFunction DiagnosticHandler = nullptr); /// Read the specified bitcode file, returning the module. - ErrorOr + ErrorOr> parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler = nullptr); diff --git a/lib/Bitcode/Reader/BitReader.cpp b/lib/Bitcode/Reader/BitReader.cpp index 868fbf010db..289c76e85b4 100644 --- a/lib/Bitcode/Reader/BitReader.cpp +++ b/lib/Bitcode/Reader/BitReader.cpp @@ -39,7 +39,7 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, raw_string_ostream Stream(Message); DiagnosticPrinterRawOStream DP(Stream); - ErrorOr ModuleOrErr = parseBitcodeFile( + ErrorOr> ModuleOrErr = parseBitcodeFile( Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); }); if (ModuleOrErr.getError()) { if (OutMessage) { @@ -50,7 +50,7 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, return 1; } - *OutModule = wrap(ModuleOrErr.get()); + *OutModule = wrap(ModuleOrErr.get().release()); return 0; } @@ -64,7 +64,7 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, std::string Message; std::unique_ptr Owner(unwrap(MemBuf)); - ErrorOr ModuleOrErr = + ErrorOr> ModuleOrErr = getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef)); Owner.release(); @@ -75,7 +75,7 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, return 1; } - *OutM = wrap(ModuleOrErr.get()); + *OutM = wrap(ModuleOrErr.get().release()); return 0; diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index a4488ae05ab..d012da61c4d 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -4600,24 +4600,24 @@ 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 +static ErrorOr> getLazyBitcodeModuleImpl(std::unique_ptr &&Buffer, LLVMContext &Context, bool WillMaterializeAll, DiagnosticHandlerFunction DiagnosticHandler, bool ShouldLazyLoadMetadata = false) { - Module *M = new Module(Buffer->getBufferIdentifier(), Context); + std::unique_ptr M = + make_unique(Buffer->getBufferIdentifier(), Context); BitcodeReader *R = new BitcodeReader(Buffer.get(), Context, DiagnosticHandler); M->setMaterializer(R); auto cleanupOnError = [&](std::error_code EC) { R->releaseBuffer(); // Never take ownership on error. - delete M; // Also deletes R. return EC; }; // Delay parsing Metadata if ShouldLazyLoadMetadata is true. - if (std::error_code EC = R->parseBitcodeInto(M, ShouldLazyLoadMetadata)) + if (std::error_code EC = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata)) return cleanupOnError(EC); if (!WillMaterializeAll) @@ -4626,14 +4626,12 @@ getLazyBitcodeModuleImpl(std::unique_ptr &&Buffer, return cleanupOnError(EC); Buffer.release(); // The BitcodeReader owns it now. - return M; + return std::move(M); } -ErrorOr -llvm::getLazyBitcodeModule(std::unique_ptr &&Buffer, - LLVMContext &Context, - DiagnosticHandlerFunction DiagnosticHandler, - bool ShouldLazyLoadMetadata) { +ErrorOr> llvm::getLazyBitcodeModule( + std::unique_ptr &&Buffer, LLVMContext &Context, + DiagnosticHandlerFunction DiagnosticHandler, bool ShouldLazyLoadMetadata) { return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false, DiagnosticHandler, ShouldLazyLoadMetadata); } @@ -4650,25 +4648,23 @@ llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer, return std::move(M); } -ErrorOr +ErrorOr> llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler) { std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Buffer, false); - ErrorOr ModuleOrErr = getLazyBitcodeModuleImpl( + ErrorOr> ModuleOrErr = getLazyBitcodeModuleImpl( std::move(Buf), Context, true, DiagnosticHandler); if (!ModuleOrErr) return ModuleOrErr; - Module *M = ModuleOrErr.get(); + std::unique_ptr &M = ModuleOrErr.get(); // Read in the entire module, and destroy the BitcodeReader. - if (std::error_code EC = M->materializeAllPermanently()) { - delete M; + if (std::error_code EC = M->materializeAllPermanently()) return EC; - } // TODO: Restore the use-lists to the in-memory state when the bitcode was // written. We must defer until the Module has been fully materialized. - return M; + return std::move(M); } std::string diff --git a/lib/IRReader/IRReader.cpp b/lib/IRReader/IRReader.cpp index 7bc6f076d62..43fee65db7f 100644 --- a/lib/IRReader/IRReader.cpp +++ b/lib/IRReader/IRReader.cpp @@ -34,14 +34,14 @@ getLazyIRModule(std::unique_ptr Buffer, SMDiagnostic &Err, LLVMContext &Context) { if (isBitcode((const unsigned char *)Buffer->getBufferStart(), (const unsigned char *)Buffer->getBufferEnd())) { - ErrorOr ModuleOrErr = + ErrorOr> ModuleOrErr = getLazyBitcodeModule(std::move(Buffer), Context); if (std::error_code EC = ModuleOrErr.getError()) { Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, EC.message()); return nullptr; } - return std::unique_ptr(ModuleOrErr.get()); + return std::move(ModuleOrErr.get()); } return parseAssembly(Buffer->getMemBufferRef(), Err, Context); @@ -67,13 +67,14 @@ std::unique_ptr llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err, TimePassesIsEnabled); if (isBitcode((const unsigned char *)Buffer.getBufferStart(), (const unsigned char *)Buffer.getBufferEnd())) { - ErrorOr ModuleOrErr = parseBitcodeFile(Buffer, Context); + ErrorOr> ModuleOrErr = + parseBitcodeFile(Buffer, Context); if (std::error_code EC = ModuleOrErr.getError()) { Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error, EC.message()); return nullptr; } - return std::unique_ptr(ModuleOrErr.get()); + return std::move(ModuleOrErr.get()); } return parseAssembly(Buffer, Err, Context); diff --git a/lib/LTO/LTOModule.cpp b/lib/LTO/LTOModule.cpp index 0794d3c9ea4..bbb3b6df30c 100644 --- a/lib/LTO/LTOModule.cpp +++ b/lib/LTO/LTOModule.cpp @@ -147,9 +147,10 @@ LTOModule *LTOModule::createInContext(const void *mem, size_t length, return makeLTOModule(Buffer, options, errMsg, Context); } -static Module *parseBitcodeFileImpl(MemoryBufferRef Buffer, - LLVMContext &Context, bool ShouldBeLazy, - std::string &ErrMsg) { +static std::unique_ptr parseBitcodeFileImpl(MemoryBufferRef Buffer, + LLVMContext &Context, + bool ShouldBeLazy, + std::string &ErrMsg) { // Find the buffer. ErrorOr MBOrErr = @@ -168,22 +169,22 @@ static Module *parseBitcodeFileImpl(MemoryBufferRef Buffer, if (!ShouldBeLazy) { // Parse the full file. - ErrorOr M = + ErrorOr> M = parseBitcodeFile(*MBOrErr, Context, DiagnosticHandler); if (!M) return nullptr; - return *M; + return std::move(*M); } // Parse lazily. std::unique_ptr LightweightBuf = MemoryBuffer::getMemBuffer(*MBOrErr, false); - ErrorOr M = getLazyBitcodeModule(std::move(LightweightBuf), Context, - DiagnosticHandler, - true/*ShouldLazyLoadMetadata*/); + ErrorOr> M = + getLazyBitcodeModule(std::move(LightweightBuf), Context, + DiagnosticHandler, true /*ShouldLazyLoadMetadata*/); if (!M) return nullptr; - return *M; + return std::move(*M); } LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer, @@ -197,9 +198,9 @@ LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer, // If we own a context, we know this is being used only for symbol // extraction, not linking. Be lazy in that case. - std::unique_ptr M(parseBitcodeFileImpl( + std::unique_ptr M = parseBitcodeFileImpl( Buffer, *Context, - /* ShouldBeLazy */ static_cast(OwnedContext), errMsg)); + /* ShouldBeLazy */ static_cast(OwnedContext), errMsg); if (!M) return nullptr; diff --git a/lib/Object/IRObjectFile.cpp b/lib/Object/IRObjectFile.cpp index 36eb88cd115..e90e08d786f 100644 --- a/lib/Object/IRObjectFile.cpp +++ b/lib/Object/IRObjectFile.cpp @@ -304,12 +304,12 @@ llvm::object::IRObjectFile::create(MemoryBufferRef Object, std::unique_ptr Buff( MemoryBuffer::getMemBuffer(BCOrErr.get(), false)); - ErrorOr MOrErr = + ErrorOr> MOrErr = getLazyBitcodeModule(std::move(Buff), Context, nullptr, /*ShouldLazyLoadMetadata*/ true); if (std::error_code EC = MOrErr.getError()) return EC; - std::unique_ptr M(MOrErr.get()); + std::unique_ptr &M = MOrErr.get(); return llvm::make_unique(Object, std::move(M)); } diff --git a/tools/verify-uselistorder/verify-uselistorder.cpp b/tools/verify-uselistorder/verify-uselistorder.cpp index 795d035d3df..02b7ea6fdd2 100644 --- a/tools/verify-uselistorder/verify-uselistorder.cpp +++ b/tools/verify-uselistorder/verify-uselistorder.cpp @@ -159,14 +159,14 @@ std::unique_ptr TempFile::readBitcode(LLVMContext &Context) const { } MemoryBuffer *Buffer = BufferOr.get().get(); - ErrorOr ModuleOr = + ErrorOr> ModuleOr = parseBitcodeFile(Buffer->getMemBufferRef(), Context); if (!ModuleOr) { errs() << "verify-uselistorder: error: " << ModuleOr.getError().message() << "\n"; return nullptr; } - return std::unique_ptr(ModuleOr.get()); + return std::move(ModuleOr.get()); } std::unique_ptr TempFile::readAssembly(LLVMContext &Context) const { diff --git a/unittests/Bitcode/BitReaderTest.cpp b/unittests/Bitcode/BitReaderTest.cpp index 2e6dd0b499d..691a217d20e 100644 --- a/unittests/Bitcode/BitReaderTest.cpp +++ b/unittests/Bitcode/BitReaderTest.cpp @@ -53,9 +53,9 @@ static std::unique_ptr getLazyModuleFromAssembly(LLVMContext &Context, writeModuleToBuffer(parseAssembly(Assembly), Mem); std::unique_ptr Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false); - ErrorOr ModuleOrErr = + ErrorOr> ModuleOrErr = getLazyBitcodeModule(std::move(Buffer), Context); - return std::unique_ptr(ModuleOrErr.get()); + return std::move(ModuleOrErr.get()); } TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) {