[llvm] r239858 - Return a unique_ptr from getLazyBitcodeModule and parseBitcodeFile. NFC.

David Blaikie dblaikie at gmail.com
Tue Jun 16 15:37:46 PDT 2015


On Tue, Jun 16, 2015 at 3:27 PM, Rafael Espindola <
rafael.espindola at gmail.com> wrote:

> Author: rafael
> Date: Tue Jun 16 17:27:55 2015
> New Revision: 239858
>
> URL: http://llvm.org/viewvc/llvm-project?rev=239858&view=rev
> Log:
> Return a unique_ptr from getLazyBitcodeModule and parseBitcodeFile. NFC.
>
> Modified:
>     llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
>     llvm/trunk/lib/Bitcode/Reader/BitReader.cpp
>     llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
>     llvm/trunk/lib/IRReader/IRReader.cpp
>     llvm/trunk/lib/LTO/LTOModule.cpp
>     llvm/trunk/lib/Object/IRObjectFile.cpp
>     llvm/trunk/tools/verify-uselistorder/verify-uselistorder.cpp
>     llvm/trunk/unittests/Bitcode/BitReaderTest.cpp
>
> Modified: llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/ReaderWriter.h?rev=239858&r1=239857&r2=239858&view=diff
>
> ==============================================================================
> --- llvm/trunk/include/llvm/Bitcode/ReaderWriter.h (original)
> +++ llvm/trunk/include/llvm/Bitcode/ReaderWriter.h Tue Jun 16 17:27:55 2015
> @@ -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<Module *>
> +  ErrorOr<std::unique_ptr<Module>>
>    getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
>                         LLVMContext &Context,
>                         DiagnosticHandlerFunction DiagnosticHandler =
> nullptr,
> @@ -52,7 +52,7 @@ namespace llvm {
>                           DiagnosticHandlerFunction DiagnosticHandler =
> nullptr);
>
>    /// Read the specified bitcode file, returning the module.
> -  ErrorOr<Module *>
> +  ErrorOr<std::unique_ptr<Module>>
>    parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
>                     DiagnosticHandlerFunction DiagnosticHandler = nullptr);
>
>
> Modified: llvm/trunk/lib/Bitcode/Reader/BitReader.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitReader.cpp?rev=239858&r1=239857&r2=239858&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Bitcode/Reader/BitReader.cpp (original)
> +++ llvm/trunk/lib/Bitcode/Reader/BitReader.cpp Tue Jun 16 17:27:55 2015
> @@ -39,7 +39,7 @@ LLVMBool LLVMParseBitcodeInContext(LLVMC
>    raw_string_ostream Stream(Message);
>    DiagnosticPrinterRawOStream DP(Stream);
>
> -  ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(
> +  ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(
>        Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); });
>    if (ModuleOrErr.getError()) {
>      if (OutMessage) {
> @@ -50,7 +50,7 @@ LLVMBool LLVMParseBitcodeInContext(LLVMC
>      return 1;
>    }
>
> -  *OutModule = wrap(ModuleOrErr.get());
> +  *OutModule = wrap(ModuleOrErr.get().release());
>    return 0;
>  }
>
> @@ -64,7 +64,7 @@ LLVMBool LLVMGetBitcodeModuleInContext(L
>    std::string Message;
>    std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
>
> -  ErrorOr<Module *> ModuleOrErr =
> +  ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
>        getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
>    Owner.release();
>
> @@ -75,7 +75,7 @@ LLVMBool LLVMGetBitcodeModuleInContext(L
>      return 1;
>    }
>
> -  *OutM = wrap(ModuleOrErr.get());
> +  *OutM = wrap(ModuleOrErr.get().release());
>
>    return 0;
>
>
> Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=239858&r1=239857&r2=239858&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
> +++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Tue Jun 16 17:27:55
> 2015
> @@ -4600,24 +4600,24 @@ const std::error_category &llvm::Bitcode
>  ///
>  /// \param[in] WillMaterializeAll Set to \c true if the caller promises to
>  /// materialize everything -- in particular, if this isn't truly lazy.
> -static ErrorOr<Module *>
> +static ErrorOr<std::unique_ptr<Module>>
>  getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
>                           LLVMContext &Context, bool WillMaterializeAll,
>                           DiagnosticHandlerFunction DiagnosticHandler,
>                           bool ShouldLazyLoadMetadata = false) {
> -  Module *M = new Module(Buffer->getBufferIdentifier(), Context);
> +  std::unique_ptr<Module> M =
> +      make_unique<Module>(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
>        return cleanupOnError(EC);
>
>    Buffer.release(); // The BitcodeReader owns it now.
> -  return M;
> +  return std::move(M);
>  }
>
> -ErrorOr<Module *>
> -llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
> -                           LLVMContext &Context,
> -                           DiagnosticHandlerFunction DiagnosticHandler,
> -                           bool ShouldLazyLoadMetadata) {
> +ErrorOr<std::unique_ptr<Module>> llvm::getLazyBitcodeModule(
> +    std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
> +    DiagnosticHandlerFunction DiagnosticHandler, bool
> ShouldLazyLoadMetadata) {
>    return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
>                                    DiagnosticHandler,
> ShouldLazyLoadMetadata);
>  }
> @@ -4650,25 +4648,23 @@ llvm::getStreamedBitcodeModule(StringRef
>    return std::move(M);
>  }
>
> -ErrorOr<Module *>
> +ErrorOr<std::unique_ptr<Module>>
>  llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
>                         DiagnosticHandlerFunction DiagnosticHandler) {
>    std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer,
> false);
> -  ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModuleImpl(
> +  ErrorOr<std::unique_ptr<Module>> ModuleOrErr = getLazyBitcodeModuleImpl(
>        std::move(Buf), Context, true, DiagnosticHandler);
>    if (!ModuleOrErr)
>      return ModuleOrErr;
> -  Module *M = ModuleOrErr.get();
> +  std::unique_ptr<Module> &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
>
> Modified: llvm/trunk/lib/IRReader/IRReader.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IRReader/IRReader.cpp?rev=239858&r1=239857&r2=239858&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/IRReader/IRReader.cpp (original)
> +++ llvm/trunk/lib/IRReader/IRReader.cpp Tue Jun 16 17:27:55 2015
> @@ -34,14 +34,14 @@ getLazyIRModule(std::unique_ptr<MemoryBu
>                  LLVMContext &Context) {
>    if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
>                  (const unsigned char *)Buffer->getBufferEnd())) {
> -    ErrorOr<Module *> ModuleOrErr =
> +    ErrorOr<std::unique_ptr<Module>> 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<Module>(ModuleOrErr.get());
> +    return std::move(ModuleOrErr.get());
>

You could probably just "return ModuleOrErr;" here?


>    }
>
>    return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
> @@ -67,13 +67,14 @@ std::unique_ptr<Module> llvm::parseIR(Me
>                       TimePassesIsEnabled);
>    if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
>                  (const unsigned char *)Buffer.getBufferEnd())) {
> -    ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
> +    ErrorOr<std::unique_ptr<Module>> 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<Module>(ModuleOrErr.get());
> +    return std::move(ModuleOrErr.get());
>

And here.


>    }
>
>    return parseAssembly(Buffer, Err, Context);
>
> Modified: llvm/trunk/lib/LTO/LTOModule.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOModule.cpp?rev=239858&r1=239857&r2=239858&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/LTO/LTOModule.cpp (original)
> +++ llvm/trunk/lib/LTO/LTOModule.cpp Tue Jun 16 17:27:55 2015
> @@ -147,9 +147,10 @@ LTOModule *LTOModule::createInContext(co
>    return makeLTOModule(Buffer, options, errMsg, Context);
>  }
>
> -static Module *parseBitcodeFileImpl(MemoryBufferRef Buffer,
> -                                    LLVMContext &Context, bool
> ShouldBeLazy,
> -                                    std::string &ErrMsg) {
> +static std::unique_ptr<Module> parseBitcodeFileImpl(MemoryBufferRef
> Buffer,
> +                                                    LLVMContext &Context,
> +                                                    bool ShouldBeLazy,
> +                                                    std::string &ErrMsg) {
>
>    // Find the buffer.
>    ErrorOr<MemoryBufferRef> MBOrErr =
> @@ -168,22 +169,22 @@ static Module *parseBitcodeFileImpl(Memo
>
>    if (!ShouldBeLazy) {
>      // Parse the full file.
> -    ErrorOr<Module *> M =
> +    ErrorOr<std::unique_ptr<Module>> M =
>          parseBitcodeFile(*MBOrErr, Context, DiagnosticHandler);
>      if (!M)
>        return nullptr;
> -    return *M;
> +    return std::move(*M);
>    }
>
>    // Parse lazily.
>    std::unique_ptr<MemoryBuffer> LightweightBuf =
>        MemoryBuffer::getMemBuffer(*MBOrErr, false);
> -  ErrorOr<Module *> M = getLazyBitcodeModule(std::move(LightweightBuf),
> Context,
> -                                             DiagnosticHandler,
> -
>  true/*ShouldLazyLoadMetadata*/);
> +  ErrorOr<std::unique_ptr<Module>> 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(Memo
>
>    // 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<Module> M(parseBitcodeFileImpl(
> +  std::unique_ptr<Module> M = parseBitcodeFileImpl(
>        Buffer, *Context,
> -      /* ShouldBeLazy */ static_cast<bool>(OwnedContext), errMsg));
> +      /* ShouldBeLazy */ static_cast<bool>(OwnedContext), errMsg);
>    if (!M)
>      return nullptr;
>
>
> Modified: llvm/trunk/lib/Object/IRObjectFile.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/IRObjectFile.cpp?rev=239858&r1=239857&r2=239858&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/Object/IRObjectFile.cpp (original)
> +++ llvm/trunk/lib/Object/IRObjectFile.cpp Tue Jun 16 17:27:55 2015
> @@ -304,12 +304,12 @@ llvm::object::IRObjectFile::create(Memor
>    std::unique_ptr<MemoryBuffer> Buff(
>        MemoryBuffer::getMemBuffer(BCOrErr.get(), false));
>
> -  ErrorOr<Module *> MOrErr =
> +  ErrorOr<std::unique_ptr<Module>> MOrErr =
>        getLazyBitcodeModule(std::move(Buff), Context, nullptr,
>                             /*ShouldLazyLoadMetadata*/ true);
>    if (std::error_code EC = MOrErr.getError())
>      return EC;
>
> -  std::unique_ptr<Module> M(MOrErr.get());
> +  std::unique_ptr<Module> &M = MOrErr.get();
>    return llvm::make_unique<IRObjectFile>(Object, std::move(M));
>  }
>
> Modified: llvm/trunk/tools/verify-uselistorder/verify-uselistorder.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/verify-uselistorder/verify-uselistorder.cpp?rev=239858&r1=239857&r2=239858&view=diff
>
> ==============================================================================
> --- llvm/trunk/tools/verify-uselistorder/verify-uselistorder.cpp (original)
> +++ llvm/trunk/tools/verify-uselistorder/verify-uselistorder.cpp Tue Jun
> 16 17:27:55 2015
> @@ -159,14 +159,14 @@ std::unique_ptr<Module> TempFile::readBi
>    }
>
>    MemoryBuffer *Buffer = BufferOr.get().get();
> -  ErrorOr<Module *> ModuleOr =
> +  ErrorOr<std::unique_ptr<Module>> ModuleOr =
>        parseBitcodeFile(Buffer->getMemBufferRef(), Context);
>    if (!ModuleOr) {
>      errs() << "verify-uselistorder: error: " <<
> ModuleOr.getError().message()
>             << "\n";
>      return nullptr;
>    }
> -  return std::unique_ptr<Module>(ModuleOr.get());
> +  return std::move(ModuleOr.get());
>  }
>
>  std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context)
> const {
>
> Modified: llvm/trunk/unittests/Bitcode/BitReaderTest.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Bitcode/BitReaderTest.cpp?rev=239858&r1=239857&r2=239858&view=diff
>
> ==============================================================================
> --- llvm/trunk/unittests/Bitcode/BitReaderTest.cpp (original)
> +++ llvm/trunk/unittests/Bitcode/BitReaderTest.cpp Tue Jun 16 17:27:55 2015
> @@ -53,9 +53,9 @@ static std::unique_ptr<Module> getLazyMo
>    writeModuleToBuffer(parseAssembly(Assembly), Mem);
>    std::unique_ptr<MemoryBuffer> Buffer =
>        MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
> -  ErrorOr<Module *> ModuleOrErr =
> +  ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
>        getLazyBitcodeModule(std::move(Buffer), Context);
> -  return std::unique_ptr<Module>(ModuleOrErr.get());
> +  return std::move(ModuleOrErr.get());
>  }
>
>  TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) {
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150616/99629fb8/attachment.html>


More information about the llvm-commits mailing list