Allow the C API users to keep relying on the OutMessages parameter.
[oota-llvm.git] / lib / Linker / LinkModules.cpp
index c9563e6c272bb333d2b0affb8e167317a732c26a..2467f67831615e32a217ee1c8cdd1ec18d9752be 100644 (file)
@@ -1766,8 +1766,33 @@ bool Linker::LinkModules(Module *Dest, Module *Src, unsigned Mode) {
 // C API.
 //===----------------------------------------------------------------------===//
 
+static void bindingDiagnosticHandler(const llvm::DiagnosticInfo &DI,
+                                     void *Context) {
+  if (DI.getSeverity() != DS_Error)
+    return;
+
+  std::string *Message = (std::string *)Context;
+  {
+    raw_string_ostream Stream(*Message);
+    DiagnosticPrinterRawOStream DP(Stream);
+    DI.print(DP);
+  }
+}
+
+
 LLVMBool LLVMLinkModules(LLVMModuleRef Dest, LLVMModuleRef Src,
                          LLVMLinkerMode Mode, char **OutMessages) {
-  LLVMBool Result = Linker::LinkModules(unwrap(Dest), unwrap(Src), Mode);
+  Module *D = unwrap(Dest);
+  LLVMContext &Ctx = D->getContext();
+
+  LLVMContext::DiagnosticHandlerTy OldHandler = Ctx.getDiagnosticHandler();
+  void *OldDiagnosticContext = Ctx.getDiagnosticContext();
+  std::string Message;
+  Ctx.setDiagnosticHandler(bindingDiagnosticHandler, &Message);
+  LLVMBool Result = Linker::LinkModules(D, unwrap(Src), Mode);
+  Ctx.setDiagnosticHandler(OldHandler, OldDiagnosticContext);
+
+  if (OutMessages && Result)
+    *OutMessages = strdup(Message.c_str());
   return Result;
 }