1 //===-- BitReader.cpp -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm-c/BitReader.h"
11 #include "llvm-c/Core.h"
12 #include "llvm/Bitcode/ReaderWriter.h"
13 #include "llvm/IR/DiagnosticPrinter.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/raw_ostream.h"
23 /* Builds a module from the bitcode in the specified memory buffer, returning a
24 reference to the module via the OutModule parameter. Returns 0 on success.
25 Optionally returns a human-readable error message via OutMessage. */
26 LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule,
28 return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
32 LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
33 LLVMModuleRef *OutModule) {
34 return LLVMParseBitcodeInContext2(wrap(&getGlobalContext()), MemBuf,
38 static void diagnosticHandler(const DiagnosticInfo &DI, void *C) {
39 auto *Message = reinterpret_cast<std::string *>(C);
40 raw_string_ostream Stream(*Message);
41 DiagnosticPrinterRawOStream DP(Stream);
45 LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
46 LLVMMemoryBufferRef MemBuf,
47 LLVMModuleRef *OutModule,
49 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
50 LLVMContext &Ctx = *unwrap(ContextRef);
52 LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
53 Ctx.getDiagnosticHandler();
54 void *OldDiagnosticContext = Ctx.getDiagnosticContext();
56 Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);
58 ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
60 Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
62 if (ModuleOrErr.getError()) {
64 *OutMessage = strdup(Message.c_str());
65 *OutModule = wrap((Module *)nullptr);
69 *OutModule = wrap(ModuleOrErr.get().release());
73 LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
74 LLVMMemoryBufferRef MemBuf,
75 LLVMModuleRef *OutModule) {
76 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
77 LLVMContext &Ctx = *unwrap(ContextRef);
79 ErrorOr<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
80 if (ModuleOrErr.getError()) {
81 *OutModule = wrap((Module *)nullptr);
85 *OutModule = wrap(ModuleOrErr.get().release());
89 /* Reads a module from the specified path, returning via the OutModule parameter
90 a module provider which performs lazy deserialization. Returns 0 on success.
91 Optionally returns a human-readable error message via OutMessage. */
92 LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
93 LLVMMemoryBufferRef MemBuf,
94 LLVMModuleRef *OutM, char **OutMessage) {
95 LLVMContext &Ctx = *unwrap(ContextRef);
96 LLVMContext::DiagnosticHandlerTy OldDiagnosticHandler =
97 Ctx.getDiagnosticHandler();
98 void *OldDiagnosticContext = Ctx.getDiagnosticContext();
101 Ctx.setDiagnosticHandler(diagnosticHandler, &Message, true);
102 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
104 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
105 getLazyBitcodeModule(std::move(Owner), Ctx);
107 Ctx.setDiagnosticHandler(OldDiagnosticHandler, OldDiagnosticContext, true);
109 if (ModuleOrErr.getError()) {
110 *OutM = wrap((Module *)nullptr);
112 *OutMessage = strdup(Message.c_str());
116 *OutM = wrap(ModuleOrErr.get().release());
121 LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
122 LLVMMemoryBufferRef MemBuf,
123 LLVMModuleRef *OutM) {
124 LLVMContext &Ctx = *unwrap(ContextRef);
125 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
127 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
128 getLazyBitcodeModule(std::move(Owner), Ctx);
131 if (ModuleOrErr.getError()) {
132 *OutM = wrap((Module *)nullptr);
136 *OutM = wrap(ModuleOrErr.get().release());
140 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
142 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
146 LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
147 LLVMModuleRef *OutM) {
148 return LLVMGetBitcodeModuleInContext2(LLVMGetGlobalContext(), MemBuf, OutM);