1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

Update getLazyBitcodeModule to use ErrorOr for error handling.

llvm-svn: 199125
This commit is contained in:
Rafael Espindola 2014-01-13 18:31:04 +00:00
parent c159ef589c
commit 899c410e64
7 changed files with 41 additions and 34 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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