[llvm] r252791 - Add a libLTO diagnostic handler that supports lto_get_error_message API

Yunzhong Gao via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 11 11:59:08 PST 2015


Author: ygao
Date: Wed Nov 11 13:59:08 2015
New Revision: 252791

URL: http://llvm.org/viewvc/llvm-project?rev=252791&view=rev
Log:
Add a libLTO diagnostic handler that supports lto_get_error_message API

This is a follow-up from the previous discussion on the thread:
http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20151019/307763.html

The LibLTO lto_get_error_message() API reads error messages from a std::string
sLastErrorString. Instead of passing this string around as an argument, this
patch creates a diagnostic handler and then sends this handler to the
constructor of LTOCodeGenerator.

Differential Revision: http://reviews.llvm.org/D14313


Modified:
    llvm/trunk/lib/LTO/LTOCodeGenerator.cpp
    llvm/trunk/test/LTO/X86/diagnostic-handler-noexit.ll
    llvm/trunk/tools/lto/lto.cpp

Modified: llvm/trunk/lib/LTO/LTOCodeGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/LTO/LTOCodeGenerator.cpp?rev=252791&r1=252790&r2=252791&view=diff
==============================================================================
--- llvm/trunk/lib/LTO/LTOCodeGenerator.cpp (original)
+++ llvm/trunk/lib/LTO/LTOCodeGenerator.cpp Wed Nov 11 13:59:08 2015
@@ -64,23 +64,17 @@ const char* LTOCodeGenerator::getVersion
 #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(), handleLTODiagnostic) {
+      IRLinker(MergedModule.get()) {
   initializeLTOPasses();
 }
 
 LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
     : OwnedContext(std::move(Context)), Context(*OwnedContext),
       MergedModule(new Module("ld-temp.o", *OwnedContext)),
-      IRLinker(MergedModule.get(), handleLTODiagnostic) {
+      IRLinker(MergedModule.get()) {
   initializeLTOPasses();
 }
 

Modified: llvm/trunk/test/LTO/X86/diagnostic-handler-noexit.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/LTO/X86/diagnostic-handler-noexit.ll?rev=252791&r1=252790&r2=252791&view=diff
==============================================================================
--- llvm/trunk/test/LTO/X86/diagnostic-handler-noexit.ll (original)
+++ llvm/trunk/test/LTO/X86/diagnostic-handler-noexit.ll Wed Nov 11 13:59:08 2015
@@ -4,10 +4,10 @@
 
 ; RUN: llvm-as <%s >%t1
 ; RUN: llvm-as <%s >%t2
-; RUN: not llvm-lto -o /dev/null %t1 %t2 2>&1 | FileCheck %s
+; RUN: not llvm-lto -use-diagnostic-handler -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: Linking globals named 'goodboy': symbol multiply defined!
 ; CHECK: llvm-lto{{.*}}: error adding file
 @goodboy = global i32 3203383023, align 4    ; 0xbeefbeef

Modified: llvm/trunk/tools/lto/lto.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/lto/lto.cpp?rev=252791&r1=252790&r2=252791&view=diff
==============================================================================
--- llvm/trunk/tools/lto/lto.cpp (original)
+++ llvm/trunk/tools/lto/lto.cpp Wed Nov 11 13:59:08 2015
@@ -85,13 +85,21 @@ static void lto_initialize() {
 
 namespace {
 
+static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity,
+                                   const char *Msg, void *) {
+  sLastErrorString = Msg;
+  sLastErrorString += "\n";
+}
+
 // 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() {
+    setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
   LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
-      : LTOCodeGenerator(std::move(Context)) {}
+      : LTOCodeGenerator(std::move(Context)) {
+    setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
 
   std::unique_ptr<MemoryBuffer> NativeObjectFile;
 };




More information about the llvm-commits mailing list