Pass a std::unique_ptr<MemoryBuffer>& 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
This commit is contained in:
Rafael Espindola 2014-08-26 22:00:09 +00:00
parent 2292996e1a
commit fb1af0a48a
8 changed files with 22 additions and 22 deletions

View File

@ -16,6 +16,7 @@
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/MemoryBuffer.h"
#include <memory>
#include <string>
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<Module *> getLazyBitcodeModule(MemoryBuffer *Buffer,
ErrorOr<Module *> getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &Buffer,
LLVMContext &Context);
/// getStreamedBitcodeModule - Read the header of the specified stream

View File

@ -51,8 +51,11 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
LLVMModuleRef *OutM,
char **OutMessage) {
std::string Message;
std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
ErrorOr<Module *> ModuleOrErr =
getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef));
getLazyBitcodeModule(Owner, *unwrap(ContextRef));
Owner.release();
if (std::error_code EC = ModuleOrErr.getError()) {
*OutM = wrap((Module *)nullptr);

View File

@ -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<Module *> getLazyBitcodeModuleImpl(MemoryBuffer *Buffer,
LLVMContext &Context,
bool WillMaterializeAll) {
static ErrorOr<Module *>
getLazyBitcodeModuleImpl(std::unique_ptr<MemoryBuffer> &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<Module *> getLazyBitcodeModuleImpl(MemoryBuffer *Buffer,
if (std::error_code EC = R->materializeForwardReferencedFunctions())
return cleanupOnError(EC);
Buffer.release(); // The BitcodeReader owns it now.
return M;
}
ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
LLVMContext &Context) {
ErrorOr<Module *>
llvm::getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &Buffer,
LLVMContext &Context) {
return getLazyBitcodeModuleImpl(Buffer, Context, false);
}
@ -3567,11 +3569,9 @@ Module *llvm::getStreamedBitcodeModule(const std::string &name,
ErrorOr<Module *> llvm::parseBitcodeFile(MemoryBufferRef Buffer,
LLVMContext &Context) {
std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Buffer, false);
ErrorOr<Module *> ModuleOrErr =
getLazyBitcodeModuleImpl(Buf.get(), Context, true);
ErrorOr<Module *> 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()) {

View File

@ -35,14 +35,12 @@ getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
(const unsigned char *)Buffer->getBufferEnd())) {
std::string ErrMsg;
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer.get(), Context);
ErrorOr<Module *> 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<Module>(ModuleOrErr.get());
}

View File

@ -270,10 +270,9 @@ llvm::object::IRObjectFile::createIRObjectFile(MemoryBufferRef Object,
std::unique_ptr<MemoryBuffer> Buff(MemoryBuffer::getMemBuffer(Object, false));
ErrorOr<Module *> MOrErr = getLazyBitcodeModule(Buff.get(), Context);
ErrorOr<Module *> MOrErr = getLazyBitcodeModule(Buff, Context);
if (std::error_code EC = MOrErr.getError())
return EC;
Buff.release();
std::unique_ptr<Module> M(MOrErr.get());
return new IRObjectFile(Object, std::move(M));

View File

@ -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<Module *> MOrErr = getLazyBitcodeModule(Buffer.get(), Context);
ErrorOr<Module *> 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<Module> M(MOrErr.get());

View File

@ -51,7 +51,8 @@ static std::unique_ptr<Module> getLazyModuleFromAssembly(LLVMContext &Context,
SmallString<1024> &Mem,
const char *Assembly) {
writeModuleToBuffer(parseAssembly(Assembly), Mem);
MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
std::unique_ptr<MemoryBuffer> Buffer(
MemoryBuffer::getMemBuffer(Mem.str(), "test", false));
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
return std::unique_ptr<Module>(ModuleOrErr.get());
}

View File

@ -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<MemoryBuffer> BitcodeBuffer(
MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test"));
ErrorOr<Module*> ModuleOrErr = getLazyBitcodeModule(BitcodeBuffer, Context);
if (std::error_code EC = ModuleOrErr.getError()) {
ADD_FAILURE() << EC.message();
delete BitcodeBuffer;
return nullptr;
}
std::unique_ptr<Module> Owner(ModuleOrErr.get());