From 7ba3323aff198a93d9f6ba50c11152e1007fdf3b Mon Sep 17 00:00:00 2001 From: Yunzhong Gao Date: Fri, 11 Sep 2015 20:01:53 +0000 Subject: [PATCH] Add a non-exiting diagnostic handler for LTO. This is in order to give LTO clients a chance to do some clean-up before terminating the process. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247461 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/LTO/LTOCodeGenerator.cpp | 10 ++++++++-- test/LTO/X86/diagnostic-handler-noexit.ll | 13 +++++++++++++ tools/llvm-lto/llvm-lto.cpp | 5 ++++- 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/LTO/X86/diagnostic-handler-noexit.ll diff --git a/lib/LTO/LTOCodeGenerator.cpp b/lib/LTO/LTOCodeGenerator.cpp index 51515b0de50..5dda8292bd6 100644 --- a/lib/LTO/LTOCodeGenerator.cpp +++ b/lib/LTO/LTOCodeGenerator.cpp @@ -64,17 +64,23 @@ const char* LTOCodeGenerator::getVersionString() { #endif } +static void handleLTODiagnostic(const DiagnosticInfo &DI) { + DiagnosticPrinterRawOStream DP(errs()); + DI.print(DP); + errs() << "\n"; +} + LTOCodeGenerator::LTOCodeGenerator() : Context(getGlobalContext()), MergedModule(new Module("ld-temp.o", Context)), - IRLinker(MergedModule.get()) { + IRLinker(MergedModule.get(), handleLTODiagnostic) { initializeLTOPasses(); } LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr Context) : OwnedContext(std::move(Context)), Context(*OwnedContext), MergedModule(new Module("ld-temp.o", *OwnedContext)), - IRLinker(MergedModule.get()) { + IRLinker(MergedModule.get(), handleLTODiagnostic) { initializeLTOPasses(); } diff --git a/test/LTO/X86/diagnostic-handler-noexit.ll b/test/LTO/X86/diagnostic-handler-noexit.ll new file mode 100644 index 00000000000..be768c900f1 --- /dev/null +++ b/test/LTO/X86/diagnostic-handler-noexit.ll @@ -0,0 +1,13 @@ +; LTO default diagnostic handler should be non-exiting. +; This test verifies that after addModule() encounters an error, the diagnostic +; handler does not call exit(1) and instead returns to the caller of addModule. + +; RUN: llvm-as <%s >%t1 +; RUN: llvm-as <%s >%t2 +; RUN: not llvm-lto -o /dev/null %t1 %t2 2>&1 | FileCheck %s + +target triple = "x86_64-unknown-linux-gnu" + +; CHECK: Linking globals named 'goodboy': symbol multiply defined! +; CHECK: llvm-lto{{.*}}: error adding file +@goodboy = global i32 3203383023, align 4 ; 0xbeefbeef diff --git a/tools/llvm-lto/llvm-lto.cpp b/tools/llvm-lto/llvm-lto.cpp index ddde23175a3..cdb1bcaa6bc 100644 --- a/tools/llvm-lto/llvm-lto.cpp +++ b/tools/llvm-lto/llvm-lto.cpp @@ -216,8 +216,11 @@ int main(int argc, char **argv) { if (SetMergedModule && i == BaseArg) { // Transfer ownership to the code generator. CodeGen.setModule(std::move(Module)); - } else if (!CodeGen.addModule(Module.get())) + } else if (!CodeGen.addModule(Module.get())) { + // Print a message here so that we know addModule() did not abort. + errs() << argv[0] << ": error adding file '" << InputFilenames[i] << "'\n"; return 1; + } } // Add all the exported symbols to the table of symbols to preserve.