Index: include/llvm/LTO/LTOCodeGenerator.h =================================================================== --- include/llvm/LTO/LTOCodeGenerator.h +++ include/llvm/LTO/LTOCodeGenerator.h @@ -63,6 +63,9 @@ LTOCodeGenerator(); LTOCodeGenerator(std::unique_ptr Context); + LTOCodeGenerator(DiagnosticHandlerFunction DiagnosticHandler); + LTOCodeGenerator(std::unique_ptr Context, + DiagnosticHandlerFunction DiagnosticHandler); ~LTOCodeGenerator(); /// Merge given module. Return true on success. Index: lib/LTO/LTOCodeGenerator.cpp =================================================================== --- lib/LTO/LTOCodeGenerator.cpp +++ lib/LTO/LTOCodeGenerator.cpp @@ -70,20 +70,27 @@ errs() << "\n"; } -LTOCodeGenerator::LTOCodeGenerator() +LTOCodeGenerator::LTOCodeGenerator(DiagnosticHandlerFunction DiagnosticHandler) : Context(getGlobalContext()), MergedModule(new Module("ld-temp.o", Context)), - IRLinker(MergedModule.get(), handleLTODiagnostic) { + IRLinker(MergedModule.get(), DiagnosticHandler) { initializeLTOPasses(); } -LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr Context) +LTOCodeGenerator::LTOCodeGenerator() + : LTOCodeGenerator(handleLTODiagnostic) {} + +LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr Context, + DiagnosticHandlerFunction DiagnosticHandler) : OwnedContext(std::move(Context)), Context(*OwnedContext), MergedModule(new Module("ld-temp.o", *OwnedContext)), - IRLinker(MergedModule.get(), handleLTODiagnostic) { + IRLinker(MergedModule.get(), DiagnosticHandler) { initializeLTOPasses(); } +LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr Context) + : LTOCodeGenerator(std::move(Context), handleLTODiagnostic) {} + LTOCodeGenerator::~LTOCodeGenerator() {} // Initialize LTO passes. Please keep this function in sync with Index: tools/lto/lto.cpp =================================================================== --- tools/lto/lto.cpp +++ tools/lto/lto.cpp @@ -15,6 +15,7 @@ #include "llvm-c/lto.h" #include "llvm/ADT/STLExtras.h" #include "llvm/CodeGen/CommandFlags.h" +#include "llvm/IR/DiagnosticPrinter.h" #include "llvm/IR/LLVMContext.h" #include "llvm/LTO/LTOCodeGenerator.h" #include "llvm/LTO/LTOModule.h" @@ -85,13 +86,20 @@ namespace { +static void handleLibLTODiagnostic(const DiagnosticInfo &DI) { + raw_string_ostream Stream(sLastErrorString); + DiagnosticPrinterRawOStream DP(Stream); + DI.print(DP); + Stream.flush(); +} + // This derived class owns the native object file. This helps implement the // libLTO API semantics, which require that the code generator owns the object // file. struct LibLTOCodeGenerator : LTOCodeGenerator { - LibLTOCodeGenerator() {} + LibLTOCodeGenerator() : LTOCodeGenerator(handleLibLTODiagnostic) {} LibLTOCodeGenerator(std::unique_ptr Context) - : LTOCodeGenerator(std::move(Context)) {} + : LTOCodeGenerator(std::move(Context), handleLibLTODiagnostic) {} std::unique_ptr NativeObjectFile; };