From: Rafael Espindola Date: Tue, 3 Feb 2015 00:49:57 +0000 (+0000) Subject: Use a non-fatal diag handler in the C API. FIxes PR22368. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d0d95dbf3c8ca94bdd3c6d8afc1aca60b12325a4;p=oota-llvm.git Use a non-fatal diag handler in the C API. FIxes PR22368. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@227903 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Bitcode/Reader/BitReader.cpp b/lib/Bitcode/Reader/BitReader.cpp index 9b3acb5ca0a..8641cd1487c 100644 --- a/lib/Bitcode/Reader/BitReader.cpp +++ b/lib/Bitcode/Reader/BitReader.cpp @@ -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 #include @@ -30,8 +32,15 @@ LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage) { - ErrorOr 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 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 index 00000000000..a85c3644b3a 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 index 00000000000..997f7b5d5b7 --- /dev/null +++ b/test/Bindings/llvm-c/invalid-bitcode.test @@ -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