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/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"
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,
31 LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
32 LLVMMemoryBufferRef MemBuf,
33 LLVMModuleRef *OutModule,
35 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
36 LLVMContext &Ctx = *unwrap(ContextRef);
39 raw_string_ostream Stream(Message);
40 DiagnosticPrinterRawOStream DP(Stream);
42 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(
43 Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); });
44 if (std::error_code EC = ModuleOrErr.getError()) {
46 *OutMessage = strdup(EC.message().c_str());
47 *OutModule = wrap((Module*)nullptr);
51 *OutModule = wrap(ModuleOrErr.get());
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,
63 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
65 ErrorOr<Module *> ModuleOrErr =
66 getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
69 if (std::error_code EC = ModuleOrErr.getError()) {
70 *OutM = wrap((Module *)nullptr);
72 *OutMessage = strdup(EC.message().c_str());
76 *OutM = wrap(ModuleOrErr.get());
82 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
84 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
88 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
89 LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
90 LLVMMemoryBufferRef MemBuf,
91 LLVMModuleProviderRef *OutMP,
93 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
94 reinterpret_cast<LLVMModuleRef*>(OutMP),
98 /* Deprecated: Use LLVMGetBitcodeModule instead. */
99 LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
100 LLVMModuleProviderRef *OutMP,
102 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,