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
This commit is contained in:
Rafael Espindola 2015-06-16 22:27:55 +00:00
parent 3bcfd461f6
commit 20a6785cd2
8 changed files with 42 additions and 44 deletions

View File

@ -32,7 +32,7 @@ namespace llvm {
/// deserialization of function bodies. If ShouldLazyLoadMetadata is true, /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
/// lazily load metadata as well. If successful, this moves Buffer. On /// lazily load metadata as well. If successful, this moves Buffer. On
/// error, this *does not* move Buffer. /// error, this *does not* move Buffer.
ErrorOr<Module *> ErrorOr<std::unique_ptr<Module>>
getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
LLVMContext &Context, LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler = nullptr, DiagnosticHandlerFunction DiagnosticHandler = nullptr,
@ -52,7 +52,7 @@ namespace llvm {
DiagnosticHandlerFunction DiagnosticHandler = nullptr); DiagnosticHandlerFunction DiagnosticHandler = nullptr);
/// Read the specified bitcode file, returning the module. /// Read the specified bitcode file, returning the module.
ErrorOr<Module *> ErrorOr<std::unique_ptr<Module>>
parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler = nullptr); DiagnosticHandlerFunction DiagnosticHandler = nullptr);

View File

@ -39,7 +39,7 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
raw_string_ostream Stream(Message); raw_string_ostream Stream(Message);
DiagnosticPrinterRawOStream DP(Stream); DiagnosticPrinterRawOStream DP(Stream);
ErrorOr<Module *> ModuleOrErr = parseBitcodeFile( ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(
Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); }); Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); });
if (ModuleOrErr.getError()) { if (ModuleOrErr.getError()) {
if (OutMessage) { if (OutMessage) {
@ -50,7 +50,7 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
return 1; return 1;
} }
*OutModule = wrap(ModuleOrErr.get()); *OutModule = wrap(ModuleOrErr.get().release());
return 0; return 0;
} }
@ -64,7 +64,7 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
std::string Message; std::string Message;
std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf)); std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
ErrorOr<Module *> ModuleOrErr = ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef)); getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
Owner.release(); Owner.release();
@ -75,7 +75,7 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
return 1; return 1;
} }
*OutM = wrap(ModuleOrErr.get()); *OutM = wrap(ModuleOrErr.get().release());
return 0; return 0;

View File

@ -4600,24 +4600,24 @@ const std::error_category &llvm::BitcodeErrorCategory() {
/// ///
/// \param[in] WillMaterializeAll Set to \c true if the caller promises to /// \param[in] WillMaterializeAll Set to \c true if the caller promises to
/// materialize everything -- in particular, if this isn't truly lazy. /// 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, getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
LLVMContext &Context, bool WillMaterializeAll, LLVMContext &Context, bool WillMaterializeAll,
DiagnosticHandlerFunction DiagnosticHandler, DiagnosticHandlerFunction DiagnosticHandler,
bool ShouldLazyLoadMetadata = false) { bool ShouldLazyLoadMetadata = false) {
Module *M = new Module(Buffer->getBufferIdentifier(), Context); std::unique_ptr<Module> M =
make_unique<Module>(Buffer->getBufferIdentifier(), Context);
BitcodeReader *R = BitcodeReader *R =
new BitcodeReader(Buffer.get(), Context, DiagnosticHandler); new BitcodeReader(Buffer.get(), Context, DiagnosticHandler);
M->setMaterializer(R); M->setMaterializer(R);
auto cleanupOnError = [&](std::error_code EC) { auto cleanupOnError = [&](std::error_code EC) {
R->releaseBuffer(); // Never take ownership on error. R->releaseBuffer(); // Never take ownership on error.
delete M; // Also deletes R.
return EC; return EC;
}; };
// Delay parsing Metadata if ShouldLazyLoadMetadata is true. // 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); return cleanupOnError(EC);
if (!WillMaterializeAll) if (!WillMaterializeAll)
@ -4626,14 +4626,12 @@ getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &&Buffer,
return cleanupOnError(EC); return cleanupOnError(EC);
Buffer.release(); // The BitcodeReader owns it now. Buffer.release(); // The BitcodeReader owns it now.
return M; return std::move(M);
} }
ErrorOr<Module *> ErrorOr<std::unique_ptr<Module>> llvm::getLazyBitcodeModule(
llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer, std::unique_ptr<MemoryBuffer> &&Buffer, LLVMContext &Context,
LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler, bool ShouldLazyLoadMetadata) {
DiagnosticHandlerFunction DiagnosticHandler,
bool ShouldLazyLoadMetadata) {
return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false, return getLazyBitcodeModuleImpl(std::move(Buffer), Context, false,
DiagnosticHandler, ShouldLazyLoadMetadata); DiagnosticHandler, ShouldLazyLoadMetadata);
} }
@ -4650,25 +4648,23 @@ llvm::getStreamedBitcodeModule(StringRef Name, DataStreamer *Streamer,
return std::move(M); return std::move(M);
} }
ErrorOr<Module *> ErrorOr<std::unique_ptr<Module>>
llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, llvm::parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
DiagnosticHandlerFunction DiagnosticHandler) { DiagnosticHandlerFunction DiagnosticHandler) {
std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false); 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); std::move(Buf), Context, true, DiagnosticHandler);
if (!ModuleOrErr) if (!ModuleOrErr)
return ModuleOrErr; return ModuleOrErr;
Module *M = ModuleOrErr.get(); std::unique_ptr<Module> &M = ModuleOrErr.get();
// Read in the entire module, and destroy the BitcodeReader. // Read in the entire module, and destroy the BitcodeReader.
if (std::error_code EC = M->materializeAllPermanently()) { if (std::error_code EC = M->materializeAllPermanently())
delete M;
return EC; return EC;
}
// TODO: Restore the use-lists to the in-memory state when the bitcode was // 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. // written. We must defer until the Module has been fully materialized.
return M; return std::move(M);
} }
std::string std::string

View File

@ -34,14 +34,14 @@ getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
LLVMContext &Context) { LLVMContext &Context) {
if (isBitcode((const unsigned char *)Buffer->getBufferStart(), if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
(const unsigned char *)Buffer->getBufferEnd())) { (const unsigned char *)Buffer->getBufferEnd())) {
ErrorOr<Module *> ModuleOrErr = ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
getLazyBitcodeModule(std::move(Buffer), Context); getLazyBitcodeModule(std::move(Buffer), Context);
if (std::error_code EC = ModuleOrErr.getError()) { if (std::error_code EC = ModuleOrErr.getError()) {
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
EC.message()); EC.message());
return nullptr; return nullptr;
} }
return std::unique_ptr<Module>(ModuleOrErr.get()); return std::move(ModuleOrErr.get());
} }
return parseAssembly(Buffer->getMemBufferRef(), Err, Context); return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
@ -67,13 +67,14 @@ std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
TimePassesIsEnabled); TimePassesIsEnabled);
if (isBitcode((const unsigned char *)Buffer.getBufferStart(), if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
(const unsigned char *)Buffer.getBufferEnd())) { (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()) { if (std::error_code EC = ModuleOrErr.getError()) {
Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error, Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
EC.message()); EC.message());
return nullptr; return nullptr;
} }
return std::unique_ptr<Module>(ModuleOrErr.get()); return std::move(ModuleOrErr.get());
} }
return parseAssembly(Buffer, Err, Context); return parseAssembly(Buffer, Err, Context);

View File

@ -147,9 +147,10 @@ LTOModule *LTOModule::createInContext(const void *mem, size_t length,
return makeLTOModule(Buffer, options, errMsg, Context); return makeLTOModule(Buffer, options, errMsg, Context);
} }
static Module *parseBitcodeFileImpl(MemoryBufferRef Buffer, static std::unique_ptr<Module> parseBitcodeFileImpl(MemoryBufferRef Buffer,
LLVMContext &Context, bool ShouldBeLazy, LLVMContext &Context,
std::string &ErrMsg) { bool ShouldBeLazy,
std::string &ErrMsg) {
// Find the buffer. // Find the buffer.
ErrorOr<MemoryBufferRef> MBOrErr = ErrorOr<MemoryBufferRef> MBOrErr =
@ -168,22 +169,22 @@ static Module *parseBitcodeFileImpl(MemoryBufferRef Buffer,
if (!ShouldBeLazy) { if (!ShouldBeLazy) {
// Parse the full file. // Parse the full file.
ErrorOr<Module *> M = ErrorOr<std::unique_ptr<Module>> M =
parseBitcodeFile(*MBOrErr, Context, DiagnosticHandler); parseBitcodeFile(*MBOrErr, Context, DiagnosticHandler);
if (!M) if (!M)
return nullptr; return nullptr;
return *M; return std::move(*M);
} }
// Parse lazily. // Parse lazily.
std::unique_ptr<MemoryBuffer> LightweightBuf = std::unique_ptr<MemoryBuffer> LightweightBuf =
MemoryBuffer::getMemBuffer(*MBOrErr, false); MemoryBuffer::getMemBuffer(*MBOrErr, false);
ErrorOr<Module *> M = getLazyBitcodeModule(std::move(LightweightBuf), Context, ErrorOr<std::unique_ptr<Module>> M =
DiagnosticHandler, getLazyBitcodeModule(std::move(LightweightBuf), Context,
true/*ShouldLazyLoadMetadata*/); DiagnosticHandler, true /*ShouldLazyLoadMetadata*/);
if (!M) if (!M)
return nullptr; return nullptr;
return *M; return std::move(*M);
} }
LTOModule *LTOModule::makeLTOModule(MemoryBufferRef Buffer, 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 // If we own a context, we know this is being used only for symbol
// extraction, not linking. Be lazy in that case. // extraction, not linking. Be lazy in that case.
std::unique_ptr<Module> M(parseBitcodeFileImpl( std::unique_ptr<Module> M = parseBitcodeFileImpl(
Buffer, *Context, Buffer, *Context,
/* ShouldBeLazy */ static_cast<bool>(OwnedContext), errMsg)); /* ShouldBeLazy */ static_cast<bool>(OwnedContext), errMsg);
if (!M) if (!M)
return nullptr; return nullptr;

View File

@ -304,12 +304,12 @@ llvm::object::IRObjectFile::create(MemoryBufferRef Object,
std::unique_ptr<MemoryBuffer> Buff( std::unique_ptr<MemoryBuffer> Buff(
MemoryBuffer::getMemBuffer(BCOrErr.get(), false)); MemoryBuffer::getMemBuffer(BCOrErr.get(), false));
ErrorOr<Module *> MOrErr = ErrorOr<std::unique_ptr<Module>> MOrErr =
getLazyBitcodeModule(std::move(Buff), Context, nullptr, getLazyBitcodeModule(std::move(Buff), Context, nullptr,
/*ShouldLazyLoadMetadata*/ true); /*ShouldLazyLoadMetadata*/ true);
if (std::error_code EC = MOrErr.getError()) if (std::error_code EC = MOrErr.getError())
return EC; 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)); return llvm::make_unique<IRObjectFile>(Object, std::move(M));
} }

View File

@ -159,14 +159,14 @@ std::unique_ptr<Module> TempFile::readBitcode(LLVMContext &Context) const {
} }
MemoryBuffer *Buffer = BufferOr.get().get(); MemoryBuffer *Buffer = BufferOr.get().get();
ErrorOr<Module *> ModuleOr = ErrorOr<std::unique_ptr<Module>> ModuleOr =
parseBitcodeFile(Buffer->getMemBufferRef(), Context); parseBitcodeFile(Buffer->getMemBufferRef(), Context);
if (!ModuleOr) { if (!ModuleOr) {
errs() << "verify-uselistorder: error: " << ModuleOr.getError().message() errs() << "verify-uselistorder: error: " << ModuleOr.getError().message()
<< "\n"; << "\n";
return nullptr; return nullptr;
} }
return std::unique_ptr<Module>(ModuleOr.get()); return std::move(ModuleOr.get());
} }
std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const { std::unique_ptr<Module> TempFile::readAssembly(LLVMContext &Context) const {

View File

@ -53,9 +53,9 @@ static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
writeModuleToBuffer(parseAssembly(Assembly), Mem); writeModuleToBuffer(parseAssembly(Assembly), Mem);
std::unique_ptr<MemoryBuffer> Buffer = std::unique_ptr<MemoryBuffer> Buffer =
MemoryBuffer::getMemBuffer(Mem.str(), "test", false); MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
ErrorOr<Module *> ModuleOrErr = ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
getLazyBitcodeModule(std::move(Buffer), Context); getLazyBitcodeModule(std::move(Buffer), Context);
return std::unique_ptr<Module>(ModuleOrErr.get()); return std::move(ModuleOrErr.get());
} }
TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) { TEST(BitReaderTest, DematerializeFunctionPreservesLinkageType) {