Update getLazyBitcodeModule to use ErrorOr for error handling.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199125 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2014-01-13 18:31:04 +00:00
parent 36713c2c0a
commit 99c7fec2c9
7 changed files with 41 additions and 34 deletions

View File

@ -14,6 +14,7 @@
#ifndef LLVM_BITCODE_READERWRITER_H
#define LLVM_BITCODE_READERWRITER_H
#include "llvm/Support/ErrorOr.h"
#include <string>
namespace llvm {
@ -25,14 +26,11 @@ namespace llvm {
class ModulePass;
class raw_ostream;
/// getLazyBitcodeModule - Read the header of the specified bitcode buffer
/// and prepare for lazy deserialization of function bodies. If successful,
/// this takes ownership of 'buffer' and returns a non-null pointer. On
/// error, this returns null, *does not* take ownership of Buffer, and fills
/// in *ErrMsg with an error description if ErrMsg is non-null.
Module *getLazyBitcodeModule(MemoryBuffer *Buffer,
LLVMContext &Context,
std::string *ErrMsg = 0);
/// 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,
LLVMContext &Context);
/// getStreamedBitcodeModule - Read the header of the specified stream
/// and prepare for lazy deserialization and streaming of function bodies.

View File

@ -51,15 +51,18 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
LLVMModuleRef *OutM,
char **OutMessage) {
std::string Message;
ErrorOr<Module *> ModuleOrErr =
getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef));
*OutM = wrap(getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef),
&Message));
if (!*OutM) {
if (error_code EC = ModuleOrErr.getError()) {
*OutM = wrap((Module *)NULL);
if (OutMessage)
*OutMessage = strdup(Message.c_str());
*OutMessage = strdup(EC.message().c_str());
return 1;
}
*OutM = wrap(ModuleOrErr.get());
return 0;
}

View File

@ -3274,18 +3274,14 @@ const error_category &BitcodeReader::BitcodeErrorCategory() {
/// getLazyBitcodeModule - lazy function-at-a-time loading from a file.
///
Module *llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
LLVMContext& Context,
std::string *ErrMsg) {
ErrorOr<Module *> llvm::getLazyBitcodeModule(MemoryBuffer *Buffer,
LLVMContext &Context) {
Module *M = new Module(Buffer->getBufferIdentifier(), Context);
BitcodeReader *R = new BitcodeReader(Buffer, Context);
M->setMaterializer(R);
if (error_code EC = R->ParseBitcodeInto(M)) {
if (ErrMsg)
*ErrMsg = EC.message();
delete M; // Also deletes R.
return 0;
return EC;
}
// Have the BitcodeReader dtor delete 'Buffer'.
R->setBufferOwned(true);
@ -3317,8 +3313,13 @@ Module *llvm::getStreamedBitcodeModule(const std::string &name,
/// If an error occurs, return null and fill in *ErrMsg if non-null.
Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context,
std::string *ErrMsg){
Module *M = getLazyBitcodeModule(Buffer, Context, ErrMsg);
if (!M) return 0;
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
if (error_code EC = ModuleOrErr.getError()) {
if (ErrMsg)
*ErrMsg = EC.message();
return 0;
}
Module *M = ModuleOrErr.get();
// Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether
// there was an error.

View File

@ -36,15 +36,16 @@ Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
(const unsigned char *)Buffer->getBufferEnd())) {
std::string ErrMsg;
Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
if (M == 0) {
ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
if (error_code EC = ModuleOrErr.getError()) {
Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
ErrMsg);
EC.message());
// ParseBitcodeFile does not take ownership of the Buffer in the
// case of an error.
delete Buffer;
return NULL;
}
return M;
return ModuleOrErr.get();
}
return ParseAssembly(Buffer, 0, Err, Context);

View File

@ -135,12 +135,14 @@ LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
TargetOptions options,
std::string &errMsg) {
// parse bitcode buffer
OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
&errMsg));
if (!m) {
ErrorOr<Module *> ModuleOrErr =
getLazyBitcodeModule(buffer, getGlobalContext());
if (error_code EC = ModuleOrErr.getError()) {
errMsg = EC.message();
delete buffer;
return NULL;
}
OwningPtr<Module> m(ModuleOrErr.get());
std::string TripleStr = m->getTargetTriple();
if (TripleStr.empty())

View File

@ -54,8 +54,9 @@ TEST(BitReaderTest, MaterializeFunctionsForBlockAddr) { // PR11677
SmallString<1024> Mem;
writeModuleToBuffer(Mem);
MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(Mem.str(), "test", false);
std::string errMsg;
OwningPtr<Module> m(getLazyBitcodeModule(Buffer, getGlobalContext(), &errMsg));
ErrorOr<Module *> ModuleOrErr =
getLazyBitcodeModule(Buffer, getGlobalContext());
OwningPtr<Module> m(ModuleOrErr.get());
PassManager passes;
passes.add(createVerifierPass());
passes.run(*m);

View File

@ -631,13 +631,14 @@ ExecutionEngine *getJITFromBitcode(
// c_str() is null-terminated like MemoryBuffer::getMemBuffer requires.
MemoryBuffer *BitcodeBuffer =
MemoryBuffer::getMemBuffer(Bitcode, "Bitcode for test");
std::string errMsg;
M = getLazyBitcodeModule(BitcodeBuffer, Context, &errMsg);
if (M == NULL) {
ADD_FAILURE() << errMsg;
ErrorOr<Module*> ModuleOrErr = getLazyBitcodeModule(BitcodeBuffer, Context);
if (error_code EC = ModuleOrErr.getError()) {
ADD_FAILURE() << EC.message();
delete BitcodeBuffer;
return NULL;
}
M = ModuleOrErr.get();
std::string errMsg;
ExecutionEngine *TheJIT = EngineBuilder(M)
.setEngineKind(EngineKind::JIT)
.setErrorStr(&errMsg)