[PATCH] D14313: Add a libLTO diagnostic handler that supports lto_get_error_message API

Yunzhong Gao via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 6 13:49:52 PST 2015


ygao updated this revision to Diff 39586.
ygao added a comment.

Well it is possible that the LTO client may call lto_codegen_create and then
immediately call lto_codegen_set_diagnotic_handler, before setModule or
addModule. In that case, if lto_codegen_create has installed a default
handler, it should be allowed to be updated.

I looked at the llvm::Linker's constructor again. So, the default diagnostic
handler there is LLVMContext::diagnose(). LLVMContext::diagnose() will check
first whether there has been a custom diagnostic handler installed, in which
case it will dispatch to that custom handler instead of the default behavior
of printing the diagnostic message and exiting. So my idea is to call
setDiagnosticHandler() in the constructor of LTOCodeGenerator, which sets
a custom dianostic handler in the associated LLVMContext as well. Inside
LTOCodeGenerator.cpp, I can give it a default non-exiting handler that prints
to stderr; and in lto.cpp, I can give it a handler that prints to
sLastErrorString.


http://reviews.llvm.org/D14313

Files:
  lib/LTO/LTOCodeGenerator.cpp
  tools/lto/lto.cpp

Index: tools/lto/lto.cpp
===================================================================
--- tools/lto/lto.cpp
+++ tools/lto/lto.cpp
@@ -85,13 +85,21 @@

 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;
 };
Index: lib/LTO/LTOCodeGenerator.cpp
===================================================================
--- lib/LTO/LTOCodeGenerator.cpp
+++ lib/LTO/LTOCodeGenerator.cpp
@@ -64,16 +64,32 @@
 #endif
 }

-static void handleLTODiagnostic(const DiagnosticInfo &DI) {
-  DiagnosticPrinterRawOStream DP(errs());
-  DI.print(DP);
-  errs() << "\n";
+static const char *
+getLTODiagnosticPrefix(lto_codegen_diagnostic_severity_t Severity) {
+  switch (Severity) {
+  case LTO_DS_NOTE:
+    return "note: ";
+  case LTO_DS_REMARK:
+    return "remark: ";
+  case LTO_DS_ERROR:
+    return "error: ";
+  case LTO_DS_WARNING:
+    return "warning: ";
+  default:
+    return "";
+  }
 }

+static void handleLTODiagnostic(lto_codegen_diagnostic_severity_t Severity,
+                                const char *Msg, void *) {
+  errs() << getLTODiagnosticPrefix(Severity) << Msg << "\n";
+}
+
 LTOCodeGenerator::LTOCodeGenerator()
     : Context(getGlobalContext()),
       MergedModule(new Module("ld-temp.o", Context)),
-      IRLinker(MergedModule.get(), handleLTODiagnostic) {
+      IRLinker(MergedModule.get()) {
+  setDiagnosticHandler(handleLTODiagnostic, nullptr);
   initializeLTOPasses();
 }

@@ -80,7 +96,8 @@
 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()) {
+  setDiagnosticHandler(handleLTODiagnostic, nullptr);
   initializeLTOPasses();
 }



-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14313.39586.patch
Type: text/x-patch
Size: 2577 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151106/b9eab604/attachment-0001.bin>


More information about the llvm-commits mailing list