diff --git a/include/llvm/Bitcode/ReaderWriter.h b/include/llvm/Bitcode/ReaderWriter.h index 3a6b205055f..0918e92cb9b 100644 --- a/include/llvm/Bitcode/ReaderWriter.h +++ b/include/llvm/Bitcode/ReaderWriter.h @@ -50,11 +50,10 @@ namespace llvm { LLVMContext &Context, std::string *ErrMsg = 0); - /// ParseBitcodeFile - Read the specified bitcode file, returning the module. - /// If an error occurs, this returns null and fills in *ErrMsg if it is - /// non-null. This method *never* takes ownership of Buffer. - Module *ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext &Context, - std::string *ErrMsg = 0); + /// Read the specified bitcode file, returning the module. + /// This method *never* takes ownership of Buffer. + ErrorOr parseBitcodeFile(MemoryBuffer *Buffer, + LLVMContext &Context); /// WriteBitcodeToFile - Write the specified module to the specified /// raw output stream. For streams where it matters, the given stream diff --git a/lib/Bitcode/Reader/BitReader.cpp b/lib/Bitcode/Reader/BitReader.cpp index 4136cec7c32..3e360a8b760 100644 --- a/lib/Bitcode/Reader/BitReader.cpp +++ b/lib/Bitcode/Reader/BitReader.cpp @@ -30,16 +30,16 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage) { - std::string Message; - - *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef), - &Message)); - if (!*OutModule) { + ErrorOr ModuleOrErr = + parseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef)); + if (error_code EC = ModuleOrErr.getError()) { if (OutMessage) - *OutMessage = strdup(Message.c_str()); + *OutMessage = strdup(EC.message().c_str()); + *OutModule = wrap((Module*)0); return 1; } + *OutModule = wrap(ModuleOrErr.get()); return 0; } diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index c36dae892c9..dd2f40f3903 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -3342,16 +3342,11 @@ Module *llvm::getStreamedBitcodeModule(const std::string &name, return M; } -/// ParseBitcodeFile - Read the specified bitcode file, returning the module. -/// If an error occurs, return null and fill in *ErrMsg if non-null. -Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context, - std::string *ErrMsg){ +ErrorOr llvm::parseBitcodeFile(MemoryBuffer *Buffer, + LLVMContext &Context) { ErrorOr ModuleOrErr = getLazyBitcodeModule(Buffer, Context); - if (error_code EC = ModuleOrErr.getError()) { - if (ErrMsg) - *ErrMsg = EC.message(); - return 0; - } + if (!ModuleOrErr) + return ModuleOrErr; Module *M = ModuleOrErr.get(); // Don't let the BitcodeReader dtor delete 'Buffer', regardless of whether @@ -3360,10 +3355,8 @@ Module *llvm::ParseBitcodeFile(MemoryBuffer *Buffer, LLVMContext& Context, // Read in the entire module, and destroy the BitcodeReader. if (error_code EC = M->materializeAllPermanently()) { - if (ErrMsg) - *ErrMsg = EC.message(); delete M; - return 0; + return EC; } // TODO: Restore the use-lists to the in-memory state when the bitcode was diff --git a/lib/IRReader/IRReader.cpp b/lib/IRReader/IRReader.cpp index 7356fd005ad..70801c43f1a 100644 --- a/lib/IRReader/IRReader.cpp +++ b/lib/IRReader/IRReader.cpp @@ -69,12 +69,14 @@ Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, TimePassesIsEnabled); if (isBitcode((const unsigned char *)Buffer->getBufferStart(), (const unsigned char *)Buffer->getBufferEnd())) { - std::string ErrMsg; - Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg); - if (M == 0) + ErrorOr ModuleOrErr = parseBitcodeFile(Buffer, Context); + Module *M = 0; + if (error_code EC = ModuleOrErr.getError()) Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, - ErrMsg); - // ParseBitcodeFile does not take ownership of the Buffer. + EC.message()); + else + M = ModuleOrErr.get(); + // parseBitcodeFile does not take ownership of the Buffer. delete Buffer; return M; } diff --git a/tools/llvm-nm/llvm-nm.cpp b/tools/llvm-nm/llvm-nm.cpp index 8449c293519..6822c9dbd45 100644 --- a/tools/llvm-nm/llvm-nm.cpp +++ b/tools/llvm-nm/llvm-nm.cpp @@ -570,16 +570,14 @@ static void DumpSymbolNamesFromFile(std::string &Filename) { sys::fs::file_magic magic = sys::fs::identify_magic(Buffer->getBuffer()); LLVMContext &Context = getGlobalContext(); - std::string ErrorMessage; if (magic == sys::fs::file_magic::bitcode) { - Module *Result = 0; - Result = ParseBitcodeFile(Buffer.get(), Context, &ErrorMessage); - if (Result) { + ErrorOr ModuleOrErr = parseBitcodeFile(Buffer.get(), Context); + if (error(ModuleOrErr.getError(), Filename)) { + return; + } else { + Module *Result = ModuleOrErr.get(); DumpSymbolNamesFromModule(Result); delete Result; - } else { - error(ErrorMessage, Filename); - return; } } else if (magic == sys::fs::file_magic::archive) { OwningPtr arch; @@ -616,11 +614,10 @@ static void DumpSymbolNamesFromFile(std::string &Filename) { OwningPtr buff; if (error(i->getMemoryBuffer(buff))) return; - Module *Result = 0; - if (buff) - Result = ParseBitcodeFile(buff.get(), Context, &ErrorMessage); - if (Result) { + ErrorOr ModuleOrErr = parseBitcodeFile(buff.get(), Context); + if (ModuleOrErr) { + Module *Result = ModuleOrErr.get(); DumpSymbolNamesFromModule(Result); delete Result; }