Fix error handling in LLVMGetBitcodeModuleInContext.
authorRafael Espindola <rafael.espindola@gmail.com>
Fri, 18 Dec 2015 13:58:05 +0000 (13:58 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Fri, 18 Dec 2015 13:58:05 +0000 (13:58 +0000)
It was not setting OutMessage.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255998 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Bitcode/Reader/BitReader.cpp
test/Bindings/llvm-c/invalid-bitcode.test

index 7ace802..2d15165 100644 (file)
@@ -71,17 +71,24 @@ LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
                                        LLVMMemoryBufferRef MemBuf,
                                        LLVMModuleRef *OutM,
                                        char **OutMessage) {
+  LLVMContext &Ctx = *unwrap(ContextRef);
+  LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
+      Ctx.getDiagnosticHandler();
+  void *OldDiagnosticContext = Ctx.getDiagnosticContext();
+
   std::string Message;
+  Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);
   std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
 
   ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
-      getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
+      getLazyBitcodeModule(std::move(Owner), Ctx);
   Owner.release();
+  Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
 
-  if (std::error_code EC = ModuleOrErr.getError()) {
+  if (ModuleOrErr.getError()) {
     *OutM = wrap((Module *)nullptr);
     if (OutMessage)
-      *OutMessage = strdup(EC.message().c_str());
+      *OutMessage = strdup(Message.c_str());
     return 1;
   }
 
index f8835b9..65b97e6 100644 (file)
@@ -1,3 +1,4 @@
 ; RUN: not llvm-c-test --module-dump < %S/Inputs/invalid.ll.bc 2>&1 | FileCheck %s
+; RUN: not llvm-c-test --lazy-module-dump < %S/Inputs/invalid.ll.bc 2>&1 | FileCheck %s
 
 CHECK: Error parsing bitcode: Unknown attribute kind (52)