From f4a288c0f06f5333925e1bc8a7b35be9c3e88f12 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Sat, 25 Oct 2014 04:31:08 +0000 Subject: [PATCH] Allow the C API users to keep relying on the OutMessages parameter. Should fix the Ocaml tests. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220611 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Linker/LinkModules.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index c9563e6c272..2467f678316 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -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; } -- 2.11.0