[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
Tue Nov 3 18:01:19 PST 2015


ygao created this revision.
ygao added subscribers: rafael, dexonsmith, pcc, llvm-commits.

Hi Rafael, Duncan, Peter,
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.

There are still a few cases inside lto.cpp where sLastErrorString is being passed around as an
argument. I'd like to look into cleaning those up in a separate patch.

This patch did not addressing testing: llvm-lto uses a custom diagnostic handler; maybe I can
overwrite that custom handler with a new handler, but then I would be testing the new handler
in llvm-lto.cpp instead of the handler in lto.cpp. I'll have to think about it.

- Gao

http://reviews.llvm.org/D14313

Files:
  include/llvm/LTO/LTOCodeGenerator.h
  lib/LTO/LTOCodeGenerator.cpp
  tools/lto/lto.cpp

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<LLVMContext> Context)
-      : LTOCodeGenerator(std::move(Context)) {}
+      : LTOCodeGenerator(std::move(Context), handleLibLTODiagnostic) {}
 
   std::unique_ptr<MemoryBuffer> NativeObjectFile;
 };
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<LLVMContext> Context)
+LTOCodeGenerator::LTOCodeGenerator()
+    : LTOCodeGenerator(handleLTODiagnostic) {}
+
+LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> 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<LLVMContext> Context)
+    : LTOCodeGenerator(std::move(Context), handleLTODiagnostic) {}
+
 LTOCodeGenerator::~LTOCodeGenerator() {}
 
 // Initialize LTO passes. Please keep this function in sync with
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<LLVMContext> Context);
+  LTOCodeGenerator(DiagnosticHandlerFunction DiagnosticHandler);
+  LTOCodeGenerator(std::unique_ptr<LLVMContext> Context,
+                   DiagnosticHandlerFunction DiagnosticHandler);
   ~LTOCodeGenerator();
 
   /// Merge given module.  Return true on success.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D14313.39140.patch
Type: text/x-patch
Size: 3164 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151104/bc11ecf9/attachment.bin>


More information about the llvm-commits mailing list