From 9d2bfc48741a5c0c3a3eb455959a627afcc3b722 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Mon, 14 Dec 2015 23:17:03 +0000 Subject: Use diagnostic handler in the LLVMContext This patch converts code that has access to a LLVMContext to not take a diagnostic handler. This has a few advantages * It is easier to use a consistent diagnostic handler in a single program. * Less clutter since we are not passing a handler around. It does make it a bit awkward to implement some C APIs that return a diagnostic string. I will propose new versions of these APIs and deprecate the current ones. llvm-svn: 255571 --- llvm/lib/Bitcode/Reader/BitReader.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'llvm/lib/Bitcode/Reader/BitReader.cpp') diff --git a/llvm/lib/Bitcode/Reader/BitReader.cpp b/llvm/lib/Bitcode/Reader/BitReader.cpp index 289c76e85b4..beef56bec42 100644 --- a/llvm/lib/Bitcode/Reader/BitReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitReader.cpp @@ -28,6 +28,13 @@ LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, OutMessage); } +static void diagnosticHandler(const DiagnosticInfo &DI, void *C) { + auto *Message = reinterpret_cast(C); + raw_string_ostream Stream(*Message); + DiagnosticPrinterRawOStream DP(Stream); + DI.print(DP); +} + LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, @@ -35,17 +42,19 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef(); LLVMContext &Ctx = *unwrap(ContextRef); + LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler = + Ctx.getDiagnosticHandler(); + void *OldDiagnosticContext = Ctx.getDiagnosticContext(); std::string Message; - raw_string_ostream Stream(Message); - DiagnosticPrinterRawOStream DP(Stream); + Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true); + + ErrorOr> ModuleOrErr = parseBitcodeFile(Buf, Ctx); + + Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true); - ErrorOr> ModuleOrErr = parseBitcodeFile( - Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); }); if (ModuleOrErr.getError()) { - if (OutMessage) { - Stream.flush(); + if (OutMessage) *OutMessage = strdup(Message.c_str()); - } *OutModule = wrap((Module*)nullptr); return 1; } -- cgit v1.2.3