Use a non-fatal diag handler in the C API. FIxes PR22368.
[oota-llvm.git] / lib / Bitcode / Reader / BitReader.cpp
1 //===-- BitReader.cpp -----------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm-c/BitReader.h"
11 #include "llvm/Bitcode/ReaderWriter.h"
12 #include "llvm/IR/DiagnosticPrinter.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <cstring>
18 #include <string>
19
20 using namespace llvm;
21
22 /* Builds a module from the bitcode in the specified memory buffer, returning a
23    reference to the module via the OutModule parameter. Returns 0 on success.
24    Optionally returns a human-readable error message via OutMessage. */
25 LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
26                           LLVMModuleRef *OutModule, char **OutMessage) {
27   return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
28                                    OutMessage);
29 }
30
31 LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
32                                    LLVMMemoryBufferRef MemBuf,
33                                    LLVMModuleRef *OutModule,
34                                    char **OutMessage) {
35   MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
36   LLVMContext &Ctx = *unwrap(ContextRef);
37
38   std::string Message;
39   raw_string_ostream Stream(Message);
40   DiagnosticPrinterRawOStream DP(Stream);
41
42   ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(
43       Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); });
44   if (std::error_code EC = ModuleOrErr.getError()) {
45     if (OutMessage)
46       *OutMessage = strdup(EC.message().c_str());
47     *OutModule = wrap((Module*)nullptr);
48     return 1;
49   }
50
51   *OutModule = wrap(ModuleOrErr.get());
52   return 0;
53 }
54
55 /* Reads a module from the specified path, returning via the OutModule parameter
56    a module provider which performs lazy deserialization. Returns 0 on success.
57    Optionally returns a human-readable error message via OutMessage. */
58 LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
59                                        LLVMMemoryBufferRef MemBuf,
60                                        LLVMModuleRef *OutM,
61                                        char **OutMessage) {
62   std::string Message;
63   std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
64
65   ErrorOr<Module *> ModuleOrErr =
66       getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
67   Owner.release();
68
69   if (std::error_code EC = ModuleOrErr.getError()) {
70     *OutM = wrap((Module *)nullptr);
71     if (OutMessage)
72       *OutMessage = strdup(EC.message().c_str());
73     return 1;
74   }
75
76   *OutM = wrap(ModuleOrErr.get());
77
78   return 0;
79
80 }
81
82 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
83                               char **OutMessage) {
84   return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
85                                        OutMessage);
86 }
87
88 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
89 LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
90                                                LLVMMemoryBufferRef MemBuf,
91                                                LLVMModuleProviderRef *OutMP,
92                                                char **OutMessage) {
93   return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
94                                        reinterpret_cast<LLVMModuleRef*>(OutMP),
95                                        OutMessage);
96 }
97
98 /* Deprecated: Use LLVMGetBitcodeModule instead. */
99 LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
100                                       LLVMModuleProviderRef *OutMP,
101                                       char **OutMessage) {
102   return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,
103                                                OutMP, OutMessage);
104 }