Index: clang/lib/CodeGen/BackendUtil.cpp =================================================================== --- clang/lib/CodeGen/BackendUtil.cpp +++ clang/lib/CodeGen/BackendUtil.cpp @@ -724,14 +724,12 @@ // If we are performing a ThinLTO importing compile, load the function index // into memory and pass it into thinBackend, which will run the function // importer and invoke LTO passes. - ErrorOr> IndexOrErr = - llvm::getModuleSummaryIndexForFile( - CGOpts.ThinLTOIndexFile, - [&](const DiagnosticInfo &DI) { M->getContext().diagnose(DI); }); - if (std::error_code EC = IndexOrErr.getError()) { - std::string Error = EC.message(); - errs() << "Error loading index file '" << CGOpts.ThinLTOIndexFile - << "': " << Error << "\n"; + Expected> IndexOrErr = + llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile); + if (!IndexOrErr) { + logAllUnhandledErrors(IndexOrErr.takeError(), errs(), + "Error loading index file '" + + CGOpts.ThinLTOIndexFile + "': "); return; } std::unique_ptr CombinedIndex = std::move(*IndexOrErr); Index: llvm/include/llvm/Bitcode/ReaderWriter.h =================================================================== --- llvm/include/llvm/Bitcode/ReaderWriter.h +++ llvm/include/llvm/Bitcode/ReaderWriter.h @@ -91,9 +91,8 @@ Expected hasGlobalValueSummary(MemoryBufferRef Buffer); /// Parse the specified bitcode buffer, returning the module summary index. - ErrorOr> - getModuleSummaryIndex(MemoryBufferRef Buffer, - const DiagnosticHandlerFunction &DiagnosticHandler); + Expected> + getModuleSummaryIndex(MemoryBufferRef Buffer); /// \brief Write the specified module to the specified raw output stream. /// Index: llvm/include/llvm/Object/ModuleSummaryIndexObjectFile.h =================================================================== --- llvm/include/llvm/Object/ModuleSummaryIndexObjectFile.h +++ llvm/include/llvm/Object/ModuleSummaryIndexObjectFile.h @@ -82,16 +82,15 @@ /// \brief Parse module summary index in the given memory buffer. /// Return new ModuleSummaryIndexObjectFile instance containing parsed module /// summary/index. - static ErrorOr> - create(MemoryBufferRef Object, - const DiagnosticHandlerFunction &DiagnosticHandler); + static Expected> + create(MemoryBufferRef Object); }; } /// Parse the module summary index out of an IR file and return the module /// summary index object if found, or nullptr if not. -ErrorOr> getModuleSummaryIndexForFile( - StringRef Path, const DiagnosticHandlerFunction &DiagnosticHandler); +Expected> +getModuleSummaryIndexForFile(StringRef Path); } #endif Index: llvm/lib/Bitcode/Reader/BitcodeReader.cpp =================================================================== --- llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -6668,22 +6668,19 @@ } // Parse the specified bitcode buffer, returning the function info index. -ErrorOr> llvm::getModuleSummaryIndex( - MemoryBufferRef Buffer, - const DiagnosticHandlerFunction &DiagnosticHandler) { +Expected> +llvm::getModuleSummaryIndex(MemoryBufferRef Buffer) { Expected StreamOrErr = initStream(Buffer); if (!StreamOrErr) - return errorToErrorCodeAndEmitErrors(DiagnosticHandler, - StreamOrErr.takeError()); + return StreamOrErr.takeError(); ModuleSummaryIndexBitcodeReader R(std::move(*StreamOrErr)); auto Index = llvm::make_unique(); - if (std::error_code EC = errorToErrorCodeAndEmitErrors( - DiagnosticHandler, - R.parseSummaryIndexInto(Index.get(), Buffer.getBufferIdentifier()))) - return EC; + if (Error Err = + R.parseSummaryIndexInto(Index.get(), Buffer.getBufferIdentifier())) + return std::move(Err); return std::move(Index); } Index: llvm/lib/LTO/LTO.cpp =================================================================== --- llvm/lib/LTO/LTO.cpp +++ llvm/lib/LTO/LTO.cpp @@ -416,11 +416,10 @@ collectUsedGlobalVariables(M, Used, /*CompilerUsed*/ false); MemoryBufferRef MBRef = Input->Obj->getMemoryBufferRef(); - ErrorOr> - SummaryObjOrErr = - object::ModuleSummaryIndexObjectFile::create(MBRef, Conf.DiagHandler); + Expected> + SummaryObjOrErr = object::ModuleSummaryIndexObjectFile::create(MBRef); if (!SummaryObjOrErr) - return errorCodeToError(SummaryObjOrErr.getError()); + return SummaryObjOrErr.takeError(); ThinLTO.CombinedIndex.mergeFrom((*SummaryObjOrErr)->takeIndex(), ThinLTO.ModuleMap.size()); Index: llvm/lib/LTO/ThinLTOCodeGenerator.cpp =================================================================== --- llvm/lib/LTO/ThinLTOCodeGenerator.cpp +++ llvm/lib/LTO/ThinLTOCodeGenerator.cpp @@ -68,12 +68,6 @@ static cl::opt ThreadCount("threads", cl::init(llvm::heavyweight_hardware_concurrency())); -static void diagnosticHandler(const DiagnosticInfo &DI) { - DiagnosticPrinterRawOStream DP(errs()); - DI.print(DP); - errs() << '\n'; -} - // Simple helper to save temporary files for debug. static void saveTempBitcode(const Module &TheModule, StringRef TempDir, unsigned count, StringRef Suffix) { @@ -512,13 +506,13 @@ std::unique_ptr CombinedIndex; uint64_t NextModuleId = 0; for (auto &ModuleBuffer : Modules) { - ErrorOr> ObjOrErr = - object::ModuleSummaryIndexObjectFile::create(ModuleBuffer, - diagnosticHandler); - if (std::error_code EC = ObjOrErr.getError()) { + Expected> ObjOrErr = + object::ModuleSummaryIndexObjectFile::create(ModuleBuffer); + if (!ObjOrErr) { // FIXME diagnose - errs() << "error: can't create ModuleSummaryIndexObjectFile for buffer: " - << EC.message() << "\n"; + logAllUnhandledErrors( + ObjOrErr.takeError(), errs(), + "error: can't create ModuleSummaryIndexObjectFile for buffer: "); return nullptr; } auto Index = (*ObjOrErr)->takeIndex(); Index: llvm/lib/Object/ModuleSummaryIndexObjectFile.cpp =================================================================== --- llvm/lib/Object/ModuleSummaryIndexObjectFile.cpp +++ llvm/lib/Object/ModuleSummaryIndexObjectFile.cpp @@ -70,44 +70,37 @@ // Parse module summary index in the given memory buffer. // Return new ModuleSummaryIndexObjectFile instance containing parsed // module summary/index. -ErrorOr> -ModuleSummaryIndexObjectFile::create( - MemoryBufferRef Object, - const DiagnosticHandlerFunction &DiagnosticHandler) { - std::unique_ptr Index; - +Expected> +ModuleSummaryIndexObjectFile::create(MemoryBufferRef Object) { ErrorOr BCOrErr = findBitcodeInMemBuffer(Object); if (!BCOrErr) - return BCOrErr.getError(); - - ErrorOr> IOrErr = - getModuleSummaryIndex(BCOrErr.get(), DiagnosticHandler); + return errorCodeToError(BCOrErr.getError()); - if (std::error_code EC = IOrErr.getError()) - return EC; + Expected> IOrErr = + getModuleSummaryIndex(BCOrErr.get()); - Index = std::move(IOrErr.get()); + if (!IOrErr) + return std::move(IOrErr.takeError()); + std::unique_ptr Index = std::move(IOrErr.get()); return llvm::make_unique(Object, std::move(Index)); } // Parse the module summary index out of an IR file and return the summary // index object if found, or nullptr if not. -ErrorOr> llvm::getModuleSummaryIndexForFile( - StringRef Path, const DiagnosticHandlerFunction &DiagnosticHandler) { +Expected> +llvm::getModuleSummaryIndexForFile(StringRef Path) { ErrorOr> FileOrErr = MemoryBuffer::getFileOrSTDIN(Path); std::error_code EC = FileOrErr.getError(); if (EC) - return EC; + return errorCodeToError(EC); MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef(); - ErrorOr> ObjOrErr = - object::ModuleSummaryIndexObjectFile::create(BufferRef, - DiagnosticHandler); - EC = ObjOrErr.getError(); - if (EC) - return EC; + Expected> ObjOrErr = + object::ModuleSummaryIndexObjectFile::create(BufferRef); + if (!ObjOrErr) + return ObjOrErr.takeError(); object::ModuleSummaryIndexObjectFile &Obj = **ObjOrErr; return Obj.takeIndex(); Index: llvm/lib/Transforms/IPO/FunctionImport.cpp =================================================================== --- llvm/lib/Transforms/IPO/FunctionImport.cpp +++ llvm/lib/Transforms/IPO/FunctionImport.cpp @@ -739,36 +739,6 @@ SummaryFile("summary-file", cl::desc("The summary file to use for function importing.")); -static void diagnosticHandler(const DiagnosticInfo &DI) { - raw_ostream &OS = errs(); - DiagnosticPrinterRawOStream DP(OS); - DI.print(DP); - OS << '\n'; -} - -/// Parse the summary index out of an IR file and return the summary -/// index object if found, or nullptr if not. -static std::unique_ptr getModuleSummaryIndexForFile( - StringRef Path, std::string &Error, - const DiagnosticHandlerFunction &DiagnosticHandler) { - std::unique_ptr Buffer; - ErrorOr> BufferOrErr = - MemoryBuffer::getFile(Path); - if (std::error_code EC = BufferOrErr.getError()) { - Error = EC.message(); - return nullptr; - } - Buffer = std::move(BufferOrErr.get()); - ErrorOr> ObjOrErr = - object::ModuleSummaryIndexObjectFile::create(Buffer->getMemBufferRef(), - DiagnosticHandler); - if (std::error_code EC = ObjOrErr.getError()) { - Error = EC.message(); - return nullptr; - } - return (*ObjOrErr)->takeIndex(); -} - static bool doImportingForModule(Module &M, const ModuleSummaryIndex *Index) { if (SummaryFile.empty() && !Index) report_fatal_error("error: -function-import requires -summary-file or " @@ -777,13 +747,14 @@ if (!SummaryFile.empty()) { if (Index) report_fatal_error("error: -summary-file and index from frontend\n"); - std::string Error; - IndexPtr = - getModuleSummaryIndexForFile(SummaryFile, Error, diagnosticHandler); - if (!IndexPtr) { - errs() << "Error loading file '" << SummaryFile << "': " << Error << "\n"; + Expected> IndexPtrOrErr = + getModuleSummaryIndexForFile(SummaryFile); + if (!IndexPtrOrErr) { + logAllUnhandledErrors(IndexPtrOrErr.takeError(), errs(), + "Error loading file '" + SummaryFile + "': "); return false; } + IndexPtr = std::move(*IndexPtrOrErr); Index = IndexPtr.get(); } Index: llvm/tools/llvm-link/llvm-link.cpp =================================================================== --- llvm/tools/llvm-link/llvm-link.cpp +++ llvm/tools/llvm-link/llvm-link.cpp @@ -180,7 +180,7 @@ } } // anonymous namespace -static void diagnosticHandler(const DiagnosticInfo &DI) { +static void diagnosticHandler(const DiagnosticInfo &DI, void *C) { unsigned Severity = DI.getSeverity(); switch (Severity) { case DS_Error: @@ -201,23 +201,13 @@ errs() << '\n'; } -static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, void *C) { - diagnosticHandler(DI); -} - /// Import any functions requested via the -import option. static bool importFunctions(const char *argv0, LLVMContext &Context, Linker &L) { if (SummaryIndex.empty()) return true; - ErrorOr> IndexOrErr = - llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler); - std::error_code EC = IndexOrErr.getError(); - if (EC) { - errs() << EC.message() << '\n'; - return false; - } - auto Index = std::move(IndexOrErr.get()); + std::unique_ptr Index = + ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex)); // Map of Module -> List of globals to import from the Module std::map> ModuleToGlobalsToImportMap; @@ -319,14 +309,8 @@ // If a module summary index is supplied, load it so linkInModule can treat // local functions/variables as exported and promote if necessary. if (!SummaryIndex.empty()) { - ErrorOr> IndexOrErr = - llvm::getModuleSummaryIndexForFile(SummaryIndex, diagnosticHandler); - std::error_code EC = IndexOrErr.getError(); - if (EC) { - errs() << EC.message() << '\n'; - return false; - } - auto Index = std::move(IndexOrErr.get()); + std::unique_ptr Index = + ExitOnErr(llvm::getModuleSummaryIndexForFile(SummaryIndex)); // Promotion if (renameModuleForThinLTO(*M, *Index)) @@ -353,7 +337,7 @@ ExitOnErr.setBanner(std::string(argv[0]) + ": "); LLVMContext Context; - Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); + Context.setDiagnosticHandler(diagnosticHandler, nullptr, true); llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. cl::ParseCommandLineOptions(argc, argv, "llvm linker\n"); Index: llvm/tools/llvm-lto/llvm-lto.cpp =================================================================== --- llvm/tools/llvm-lto/llvm-lto.cpp +++ llvm/tools/llvm-lto/llvm-lto.cpp @@ -196,7 +196,7 @@ } static std::string CurrentActivity; -static void diagnosticHandler(const DiagnosticInfo &DI) { +static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) { raw_ostream &OS = errs(); OS << "llvm-lto: "; switch (DI.getSeverity()) { @@ -225,11 +225,6 @@ exit(1); } -static void diagnosticHandlerWithContext(const DiagnosticInfo &DI, - void *Context) { - diagnosticHandler(DI); -} - static void error(const Twine &Msg) { errs() << "llvm-lto: " << Msg << '\n'; exit(1); @@ -259,7 +254,7 @@ Buffer = std::move(BufferOrErr.get()); CurrentActivity = ("loading file '" + Path + "'").str(); std::unique_ptr Context = llvm::make_unique(); - Context->setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); + Context->setDiagnosticHandler(diagnosticHandler, nullptr, true); ErrorOr> Ret = LTOModule::createInLocalContext( std::move(Context), Buffer->getBufferStart(), Buffer->getBufferSize(), Options, Path); @@ -271,12 +266,9 @@ /// Print some statistics on the index for each input files. void printIndexStats() { for (auto &Filename : InputFilenames) { - CurrentActivity = "loading file '" + Filename + "'"; - ErrorOr> IndexOrErr = - llvm::getModuleSummaryIndexForFile(Filename, diagnosticHandler); - error(IndexOrErr, "error " + CurrentActivity); - std::unique_ptr Index = std::move(IndexOrErr.get()); - CurrentActivity = ""; + ExitOnError ExitOnErr("llvm-lto: error loading file '" + Filename + "': "); + std::unique_ptr Index = + ExitOnErr(llvm::getModuleSummaryIndexForFile(Filename)); // Skip files without a module summary. if (!Index) report_fatal_error(Filename + " does not contain an index"); @@ -329,12 +321,9 @@ ModuleSummaryIndex CombinedIndex; uint64_t NextModuleId = 0; for (auto &Filename : InputFilenames) { - CurrentActivity = "loading file '" + Filename + "'"; - ErrorOr> IndexOrErr = - llvm::getModuleSummaryIndexForFile(Filename, diagnosticHandler); - error(IndexOrErr, "error " + CurrentActivity); - std::unique_ptr Index = std::move(IndexOrErr.get()); - CurrentActivity = ""; + ExitOnError ExitOnErr("llvm-lto: error loading file '" + Filename + "': "); + std::unique_ptr Index = + ExitOnErr(llvm::getModuleSummaryIndexForFile(Filename)); // Skip files without a module summary. if (!Index) continue; @@ -399,11 +388,9 @@ std::unique_ptr loadCombinedIndex() { if (ThinLTOIndex.empty()) report_fatal_error("Missing -thinlto-index for ThinLTO promotion stage"); - auto CurrentActivity = "loading file '" + ThinLTOIndex + "'"; - ErrorOr> IndexOrErr = - llvm::getModuleSummaryIndexForFile(ThinLTOIndex, diagnosticHandler); - error(IndexOrErr, "error " + CurrentActivity); - return std::move(IndexOrErr.get()); + ExitOnError ExitOnErr("llvm-lto: error loading file '" + ThinLTOIndex + + "': "); + return ExitOnErr(llvm::getModuleSummaryIndexForFile(ThinLTOIndex)); } static std::unique_ptr loadModule(StringRef Filename, @@ -801,7 +788,7 @@ unsigned BaseArg = 0; LLVMContext Context; - Context.setDiagnosticHandler(diagnosticHandlerWithContext, nullptr, true); + Context.setDiagnosticHandler(diagnosticHandler, nullptr, true); LTOCodeGenerator CodeGen(Context);