Index: clang/lib/CodeGen/CGOpenMPRuntime.cpp =================================================================== --- clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -3012,7 +3012,8 @@ return; llvm::LLVMContext C; - auto ME = llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C); + auto ME = expectedToErrorOrAndEmitErrors( + C, llvm::parseBitcodeFile(Buf.get()->getMemBufferRef(), C)); if (ME.getError()) return; Index: clang/lib/CodeGen/CodeGenAction.cpp =================================================================== --- clang/lib/CodeGen/CodeGenAction.cpp +++ clang/lib/CodeGen/CodeGenAction.cpp @@ -771,11 +771,13 @@ return nullptr; } - ErrorOr> ModuleOrErr = + Expected> ModuleOrErr = getOwningLazyBitcodeModule(std::move(*BCBuf), *VMContext); - if (std::error_code EC = ModuleOrErr.getError()) { - CI.getDiagnostics().Report(diag::err_cannot_open_file) << LinkBCFile - << EC.message(); + if (!ModuleOrErr) { + handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) { + CI.getDiagnostics().Report(diag::err_cannot_open_file) + << LinkBCFile << EIB.message(); + }); LinkModules.clear(); return nullptr; } Index: llvm/include/llvm/Bitcode/BitcodeReader.h =================================================================== --- llvm/include/llvm/Bitcode/BitcodeReader.h +++ llvm/include/llvm/Bitcode/BitcodeReader.h @@ -43,14 +43,14 @@ /// Read the header of the specified bitcode buffer and prepare for lazy /// deserialization of function bodies. If ShouldLazyLoadMetadata is true, /// lazily load metadata as well. - ErrorOr> + Expected> getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata = false); /// Like getLazyBitcodeModule, except that the module takes ownership of /// the memory buffer if successful. If successful, this moves Buffer. On /// error, this *does not* move Buffer. - ErrorOr> + Expected> getOwningLazyBitcodeModule(std::unique_ptr &&Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata = false); @@ -70,8 +70,8 @@ Expected getBitcodeProducerString(MemoryBufferRef Buffer); /// Read the specified bitcode file, returning the module. - ErrorOr> parseBitcodeFile(MemoryBufferRef Buffer, - LLVMContext &Context); + Expected> parseBitcodeFile(MemoryBufferRef Buffer, + LLVMContext &Context); /// Check if the given bitcode buffer contains a summary block. Expected hasGlobalValueSummary(MemoryBufferRef Buffer); Index: llvm/include/llvm/Object/IRObjectFile.h =================================================================== --- llvm/include/llvm/Object/IRObjectFile.h +++ llvm/include/llvm/Object/IRObjectFile.h @@ -75,8 +75,8 @@ static ErrorOr findBitcodeInMemBuffer(MemoryBufferRef Object); - static ErrorOr> create(MemoryBufferRef Object, - LLVMContext &Context); + static Expected> create(MemoryBufferRef Object, + LLVMContext &Context); }; } } Index: llvm/include/llvm/Transforms/IPO/FunctionImport.h =================================================================== --- llvm/include/llvm/Transforms/IPO/FunctionImport.h +++ llvm/include/llvm/Transforms/IPO/FunctionImport.h @@ -44,10 +44,12 @@ /// The set contains an entry for every global value the module exports. typedef std::unordered_set ExportSetTy; + /// A function of this type is used to load modules referenced by the index. + typedef std::function>(StringRef Identifier)> + ModuleLoaderTy; + /// Create a Function Importer. - FunctionImporter( - const ModuleSummaryIndex &Index, - std::function(StringRef Identifier)> ModuleLoader) + FunctionImporter(const ModuleSummaryIndex &Index, ModuleLoaderTy ModuleLoader) : Index(Index), ModuleLoader(std::move(ModuleLoader)) {} /// Import functions in Module \p M based on the supplied import list. @@ -63,7 +65,7 @@ const ModuleSummaryIndex &Index; /// Factory function to load a Module for a given identifier - std::function(StringRef Identifier)> ModuleLoader; + ModuleLoaderTy ModuleLoader; }; /// The function importing pass Index: llvm/lib/Bitcode/Reader/BitReader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/BitReader.cpp +++ llvm/lib/Bitcode/Reader/BitReader.cpp @@ -34,13 +34,6 @@ return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule); } -static void diagnosticHandler(const DiagnosticInfo &DI, void *C) { - auto *Message = reinterpret_cast(C); - raw_string_ostream Stream(*Message); - DiagnosticPrinterRawOStream DP(Stream); - DI.print(DP); -} - LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, @@ -48,17 +41,12 @@ MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef(); LLVMContext &Ctx = *unwrap(ContextRef); - LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler = - Ctx.getDiagnosticHandler(); - void *OldDiagnosticContext = Ctx.getDiagnosticContext(); - std::string Message; - Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true); - - ErrorOr> ModuleOrErr = parseBitcodeFile(Buf, Ctx); - - Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true); - - if (ModuleOrErr.getError()) { + Expected> ModuleOrErr = parseBitcodeFile(Buf, Ctx); + if (Error Err = ModuleOrErr.takeError()) { + std::string Message; + handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { + Message = EIB.message(); + }); if (OutMessage) *OutMessage = strdup(Message.c_str()); *OutModule = wrap((Module *)nullptr); @@ -75,7 +63,8 @@ MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef(); LLVMContext &Ctx = *unwrap(ContextRef); - ErrorOr> ModuleOrErr = parseBitcodeFile(Buf, Ctx); + ErrorOr> ModuleOrErr = + expectedToErrorOrAndEmitErrors(Ctx, parseBitcodeFile(Buf, Ctx)); if (ModuleOrErr.getError()) { *OutModule = wrap((Module *)nullptr); return 1; @@ -92,23 +81,19 @@ LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage) { LLVMContext &Ctx = *unwrap(ContextRef); - LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler = - Ctx.getDiagnosticHandler(); - void *OldDiagnosticContext = Ctx.getDiagnosticContext(); - - std::string Message; - Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true); std::unique_ptr Owner(unwrap(MemBuf)); - - ErrorOr> ModuleOrErr = + Expected> ModuleOrErr = getOwningLazyBitcodeModule(std::move(Owner), Ctx); Owner.release(); - Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true); - if (ModuleOrErr.getError()) { - *OutM = wrap((Module *)nullptr); + if (Error Err = ModuleOrErr.takeError()) { + std::string Message; + handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) { + Message = EIB.message(); + }); if (OutMessage) *OutMessage = strdup(Message.c_str()); + *OutM = wrap((Module *)nullptr); return 1; } @@ -123,8 +108,8 @@ LLVMContext &Ctx = *unwrap(ContextRef); std::unique_ptr Owner(unwrap(MemBuf)); - ErrorOr> ModuleOrErr = - getOwningLazyBitcodeModule(std::move(Owner), Ctx); + ErrorOr> ModuleOrErr = expectedToErrorOrAndEmitErrors( + Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx)); Owner.release(); if (ModuleOrErr.getError()) { Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -6571,13 +6571,13 @@ /// /// \param[in] MaterializeAll Set to \c true if we should materialize /// everything. -static ErrorOr> +static Expected> getLazyBitcodeModuleImpl(MemoryBufferRef Buffer, LLVMContext &Context, bool MaterializeAll, bool ShouldLazyLoadMetadata = false) { Expected StreamOrErr = initStream(Buffer); if (!StreamOrErr) - return errorToErrorCodeAndEmitErrors(Context, StreamOrErr.takeError()); + return StreamOrErr.takeError(); BitcodeReader *R = new BitcodeReader(std::move(*StreamOrErr), Context); @@ -6587,28 +6587,28 @@ // Delay parsing Metadata if ShouldLazyLoadMetadata is true. if (Error Err = R->parseBitcodeInto(M.get(), ShouldLazyLoadMetadata)) - return errorToErrorCodeAndEmitErrors(Context, std::move(Err)); + return std::move(Err); if (MaterializeAll) { // Read in the entire module, and destroy the BitcodeReader. if (Error Err = M->materializeAll()) - return errorToErrorCodeAndEmitErrors(Context, std::move(Err)); + return std::move(Err); } else { // Resolve forward references from blockaddresses. if (Error Err = R->materializeForwardReferencedFunctions()) - return errorToErrorCodeAndEmitErrors(Context, std::move(Err)); + return std::move(Err); } return std::move(M); } -ErrorOr> +Expected> llvm::getLazyBitcodeModule(MemoryBufferRef Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata) { return getLazyBitcodeModuleImpl(Buffer, Context, false, ShouldLazyLoadMetadata); } -ErrorOr> +Expected> llvm::getOwningLazyBitcodeModule(std::unique_ptr &&Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata) { @@ -6618,8 +6618,8 @@ return MOrErr; } -ErrorOr> llvm::parseBitcodeFile(MemoryBufferRef Buffer, - LLVMContext &Context) { +Expected> llvm::parseBitcodeFile(MemoryBufferRef Buffer, + LLVMContext &Context) { return getLazyBitcodeModuleImpl(Buffer, Context, true); // 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. Index: llvm/lib/CodeGen/ParallelCG.cpp =================================================================== --- llvm/lib/CodeGen/ParallelCG.cpp +++ llvm/lib/CodeGen/ParallelCG.cpp @@ -79,7 +79,7 @@ CodegenThreadPool.async( [TMFactory, FileType, ThreadOS](const SmallString<0> &BC) { LLVMContext Ctx; - ErrorOr> MOrErr = parseBitcodeFile( + Expected> MOrErr = parseBitcodeFile( MemoryBufferRef(StringRef(BC.data(), BC.size()), ""), Ctx); Index: llvm/lib/IRReader/IRReader.cpp =================================================================== --- llvm/lib/IRReader/IRReader.cpp +++ llvm/lib/IRReader/IRReader.cpp @@ -34,11 +34,13 @@ LLVMContext &Context, bool ShouldLazyLoadMetadata) { if (isBitcode((const unsigned char *)Buffer->getBufferStart(), (const unsigned char *)Buffer->getBufferEnd())) { - ErrorOr> ModuleOrErr = getOwningLazyBitcodeModule( + Expected> ModuleOrErr = getOwningLazyBitcodeModule( std::move(Buffer), Context, ShouldLazyLoadMetadata); - if (std::error_code EC = ModuleOrErr.getError()) { - Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, - EC.message()); + if (Error E = ModuleOrErr.takeError()) { + handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { + Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, + EIB.message()); + }); return nullptr; } return std::move(ModuleOrErr.get()); @@ -69,11 +71,13 @@ TimePassesIsEnabled); if (isBitcode((const unsigned char *)Buffer.getBufferStart(), (const unsigned char *)Buffer.getBufferEnd())) { - ErrorOr> ModuleOrErr = + Expected> ModuleOrErr = parseBitcodeFile(Buffer, Context); - if (std::error_code EC = ModuleOrErr.getError()) { - Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error, - EC.message()); + if (Error E = ModuleOrErr.takeError()) { + handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) { + Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error, + EIB.message()); + }); return nullptr; } return std::move(ModuleOrErr.get()); Index: llvm/lib/LTO/LTO.cpp =================================================================== --- llvm/lib/LTO/LTO.cpp +++ llvm/lib/LTO/LTO.cpp @@ -104,17 +104,16 @@ llvm::loadModuleFromBuffer(const MemoryBufferRef &Buffer, LLVMContext &Context, bool Lazy) { SMDiagnostic Err; - ErrorOr> ModuleOrErr(nullptr); - if (Lazy) { - ModuleOrErr = getLazyBitcodeModule(Buffer, Context, - /* ShouldLazyLoadMetadata */ Lazy); - } else { - ModuleOrErr = parseBitcodeFile(Buffer, Context); - } - if (std::error_code EC = ModuleOrErr.getError()) { - Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error, - EC.message()); - Err.print("ThinLTO", errs()); + Expected> ModuleOrErr = + Lazy ? getLazyBitcodeModule(Buffer, Context, + /* ShouldLazyLoadMetadata */ true) + : parseBitcodeFile(Buffer, Context); + if (!ModuleOrErr) { + handleAllErrors(ModuleOrErr.takeError(), [&](ErrorInfoBase &EIB) { + SMDiagnostic Err = SMDiagnostic(Buffer.getBufferIdentifier(), + SourceMgr::DK_Error, EIB.message()); + Err.print("ThinLTO", errs()); + }); report_fatal_error("Can't load module, abort."); } return std::move(ModuleOrErr.get()); @@ -204,25 +203,13 @@ Expected> InputFile::create(MemoryBufferRef Object) { std::unique_ptr File(new InputFile); - std::string Msg; - auto DiagHandler = [](const DiagnosticInfo &DI, void *MsgP) { - auto *Msg = reinterpret_cast(MsgP); - raw_string_ostream OS(*Msg); - DiagnosticPrinterRawOStream DP(OS); - DI.print(DP); - }; - File->Ctx.setDiagnosticHandler(DiagHandler, static_cast(&Msg)); - ErrorOr> IRObj = + Expected> IRObj = IRObjectFile::create(Object, File->Ctx); - if (!Msg.empty()) - return make_error(Msg, inconvertibleErrorCode()); if (!IRObj) - return errorCodeToError(IRObj.getError()); + return IRObj.takeError(); File->Obj = std::move(*IRObj); - File->Ctx.setDiagnosticHandler(nullptr, nullptr); - for (const auto &C : File->Obj->getModule().getComdatSymbolTable()) { auto P = File->ComdatMap.insert(std::make_pair(&C.second, File->Comdats.size())); @@ -346,10 +333,10 @@ llvm::make_unique("ld-temp.o", RegularLTO.Ctx); RegularLTO.Mover = llvm::make_unique(*RegularLTO.CombinedModule); } - ErrorOr> ObjOrErr = + Expected> ObjOrErr = IRObjectFile::create(Input->Obj->getMemoryBufferRef(), RegularLTO.Ctx); if (!ObjOrErr) - return errorCodeToError(ObjOrErr.getError()); + return ObjOrErr.takeError(); std::unique_ptr Obj = std::move(*ObjOrErr); Module &M = Obj->getModule(); @@ -571,9 +558,10 @@ MapVector &ModuleMap) { auto RunThinBackend = [&](AddStreamFn AddStream) { LTOLLVMContext BackendContext(Conf); - ErrorOr> MOrErr = + Expected> MOrErr = parseBitcodeFile(MBRef, BackendContext); - assert(MOrErr && "Unable to load module in thread?"); + if (!MOrErr) + return MOrErr.takeError(); return thinBackend(Conf, Task, AddStream, **MOrErr, CombinedIndex, ImportList, DefinedGlobals, ModuleMap); Index: llvm/lib/LTO/LTOBackend.cpp =================================================================== --- llvm/lib/LTO/LTOBackend.cpp +++ llvm/lib/LTO/LTOBackend.cpp @@ -237,7 +237,7 @@ CodegenThreadPool.async( [&](const SmallString<0> &BC, unsigned ThreadId) { LTOLLVMContext Ctx(C); - ErrorOr> MOrErr = parseBitcodeFile( + Expected> MOrErr = parseBitcodeFile( MemoryBufferRef(StringRef(BC.data(), BC.size()), "ld-temp.o"), Ctx); if (!MOrErr) @@ -353,10 +353,8 @@ auto ModuleLoader = [&](StringRef Identifier) { assert(Mod.getContext().isODRUniquingDebugTypes() && "ODR Type uniquing should be enabled on the context"); - return std::move(getLazyBitcodeModule(ModuleMap[Identifier], - Mod.getContext(), - /*ShouldLazyLoadMetadata=*/true) - .get()); + return getLazyBitcodeModule(ModuleMap[Identifier], Mod.getContext(), + /*ShouldLazyLoadMetadata=*/true); }; FunctionImporter Importer(CombinedIndex, ModuleLoader); Index: llvm/lib/LTO/LTOModule.cpp =================================================================== --- llvm/lib/LTO/LTOModule.cpp +++ llvm/lib/LTO/LTOModule.cpp @@ -184,18 +184,14 @@ if (!ShouldBeLazy) { // Parse the full file. - ErrorOr> M = parseBitcodeFile(*MBOrErr, Context); - if (std::error_code EC = M.getError()) - return EC; - return std::move(*M); + return expectedToErrorOrAndEmitErrors(Context, + parseBitcodeFile(*MBOrErr, Context)); } // Parse lazily. - ErrorOr> M = - getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/); - if (std::error_code EC = M.getError()) - return EC; - return std::move(*M); + return expectedToErrorOrAndEmitErrors( + Context, + getLazyBitcodeModule(*MBOrErr, Context, true /*ShouldLazyLoadMetadata*/)); } ErrorOr> Index: llvm/lib/Object/IRObjectFile.cpp =================================================================== --- llvm/lib/Object/IRObjectFile.cpp +++ llvm/lib/Object/IRObjectFile.cpp @@ -310,18 +310,18 @@ } } -ErrorOr> +Expected> llvm::object::IRObjectFile::create(MemoryBufferRef Object, LLVMContext &Context) { ErrorOr BCOrErr = findBitcodeInMemBuffer(Object); if (!BCOrErr) - return BCOrErr.getError(); + return errorCodeToError(BCOrErr.getError()); - ErrorOr> MOrErr = + Expected> MOrErr = getLazyBitcodeModule(*BCOrErr, Context, /*ShouldLazyLoadMetadata*/ true); - if (std::error_code EC = MOrErr.getError()) - return EC; + if (!MOrErr) + return MOrErr.takeError(); std::unique_ptr &M = MOrErr.get(); return llvm::make_unique(BCOrErr.get(), std::move(M)); Index: llvm/lib/Object/SymbolicFile.cpp =================================================================== --- llvm/lib/Object/SymbolicFile.cpp +++ llvm/lib/Object/SymbolicFile.cpp @@ -35,7 +35,7 @@ switch (Type) { case sys::fs::file_magic::bitcode: if (Context) - return errorOrToExpected(IRObjectFile::create(Object, *Context)); + return IRObjectFile::create(Object, *Context); LLVM_FALLTHROUGH; case sys::fs::file_magic::unknown: case sys::fs::file_magic::archive: @@ -73,9 +73,9 @@ if (!BCData) return std::move(Obj); - return errorOrToExpected(IRObjectFile::create( - MemoryBufferRef(BCData->getBuffer(), - Object.getBufferIdentifier()), *Context)); + return IRObjectFile::create( + MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()), + *Context); } } llvm_unreachable("Unexpected Binary File Type"); Index: llvm/lib/Transforms/IPO/FunctionImport.cpp =================================================================== --- llvm/lib/Transforms/IPO/FunctionImport.cpp +++ llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -624,7 +624,10 @@ // Get the module for the import const auto &FunctionsToImportPerModule = ImportList.find(Name); assert(FunctionsToImportPerModule != ImportList.end()); - std::unique_ptr SrcModule = ModuleLoader(Name); + Expected> SrcModuleOrErr = ModuleLoader(Name); + if (!SrcModuleOrErr) + return SrcModuleOrErr.takeError(); + std::unique_ptr SrcModule = std::move(*SrcModuleOrErr); assert(&DestModule.getContext() == &SrcModule->getContext() && "Context mismatch"); Index: llvm/tools/llvm-dis/llvm-dis.cpp =================================================================== --- llvm/tools/llvm-dis/llvm-dis.cpp +++ llvm/tools/llvm-dis/llvm-dis.cpp @@ -142,9 +142,9 @@ static std::unique_ptr openInputFile(LLVMContext &Context) { std::unique_ptr MB = ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(InputFilename))); - std::unique_ptr M = ExitOnErr(errorOrToExpected( - getOwningLazyBitcodeModule(std::move(MB), Context, - /*ShouldLazyLoadMetadata=*/true))); + std::unique_ptr M = + ExitOnErr(getOwningLazyBitcodeModule(std::move(MB), Context, + /*ShouldLazyLoadMetadata=*/true)); if (MaterializeMetadata) ExitOnErr(M->materializeMetadata()); else Index: llvm/tools/verify-uselistorder/verify-uselistorder.cpp =================================================================== --- llvm/tools/verify-uselistorder/verify-uselistorder.cpp +++ llvm/tools/verify-uselistorder/verify-uselistorder.cpp @@ -160,11 +160,11 @@ } MemoryBuffer *Buffer = BufferOr.get().get(); - ErrorOr> ModuleOr = + Expected> ModuleOr = parseBitcodeFile(Buffer->getMemBufferRef(), Context); if (!ModuleOr) { - errs() << "verify-uselistorder: error: " << ModuleOr.getError().message() - << "\n"; + logAllUnhandledErrors(ModuleOr.takeError(), errs(), + "verify-uselistorder: error: "); return nullptr; } return std::move(ModuleOr.get()); Index: llvm/unittests/Bitcode/BitReaderTest.cpp =================================================================== --- llvm/unittests/Bitcode/BitReaderTest.cpp +++ llvm/unittests/Bitcode/BitReaderTest.cpp @@ -55,8 +55,10 @@ SmallString<1024> &Mem, const char *Assembly) { writeModuleToBuffer(parseAssembly(Context, Assembly), Mem); - ErrorOr> ModuleOrErr = + Expected> ModuleOrErr = getLazyBitcodeModule(MemoryBufferRef(Mem.str(), "test"), Context); + if (!ModuleOrErr) + report_fatal_error("Could not parse bitcode module"); return std::move(ModuleOrErr.get()); }