Use a non-fatal diag handler in the C API. FIxes PR22368.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 3 Feb 2015 00:49:57 +0000 (00:49 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 3 Feb 2015 00:49:57 +0000 (00:49 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@227903 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Bitcode/Reader/BitReader.cpp
test/Bindings/llvm-c/Inputs/invalid.ll.bc [new file with mode: 0644]
test/Bindings/llvm-c/invalid-bitcode.test [new file with mode: 0644]

index 9b3acb5ca0a9042d7bb224351214ec9cc3752c15..8641cd1487ca218a44eb17981f098c686ebf112e 100644 (file)
@@ -9,9 +9,11 @@
 
 #include "llvm-c/BitReader.h"
 #include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/IR/DiagnosticPrinter.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
 #include <cstring>
 #include <string>
 
@@ -30,8 +32,15 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
                                    LLVMMemoryBufferRef MemBuf,
                                    LLVMModuleRef *OutModule,
                                    char **OutMessage) {
-  ErrorOr<Module *> ModuleOrErr =
-      parseBitcodeFile(unwrap(MemBuf)->getMemBufferRef(), *unwrap(ContextRef));
+  MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
+  LLVMContext &Ctx = *unwrap(ContextRef);
+
+  std::string Message;
+  raw_string_ostream Stream(Message);
+  DiagnosticPrinterRawOStream DP(Stream);
+
+  ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(
+      Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); });
   if (std::error_code EC = ModuleOrErr.getError()) {
     if (OutMessage)
       *OutMessage = strdup(EC.message().c_str());
diff --git a/test/Bindings/llvm-c/Inputs/invalid.ll.bc b/test/Bindings/llvm-c/Inputs/invalid.ll.bc
new file mode 100644 (file)
index 0000000..a85c364
Binary files /dev/null and b/test/Bindings/llvm-c/Inputs/invalid.ll.bc differ
diff --git a/test/Bindings/llvm-c/invalid-bitcode.test b/test/Bindings/llvm-c/invalid-bitcode.test
new file mode 100644 (file)
index 0000000..997f7b5
--- /dev/null
@@ -0,0 +1,3 @@
+; RUN: not llvm-c-test --module-dump < %S/Inputs/invalid.ll.bc 2>&1 | FileCheck %s
+
+CHECK: Error parsing bitcode: Corrupted bitcode