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<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(
43 Buf, Ctx, [&](const DiagnosticInfo &DI) { DI.print(DP); });
44 if (ModuleOrErr.getError()) {
47 *OutMessage = strdup(Message.c_str());
49 *OutModule = wrap((Module*)nullptr);
53 *OutModule = wrap(ModuleOrErr.get().release());
57 /* Reads a module from the specified path, returning via the OutModule parameter
58 a module provider which performs lazy deserialization. Returns 0 on success.
59 Optionally returns a human-readable error message via OutMessage. */
60 LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
61 LLVMMemoryBufferRef MemBuf,
65 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
67 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
68 getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
71 if (std::error_code EC = ModuleOrErr.getError()) {
72 *OutM = wrap((Module *)nullptr);
74 *OutMessage = strdup(EC.message().c_str());
78 *OutM = wrap(ModuleOrErr.get().release());
84 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
86 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
90 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
91 LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
92 LLVMMemoryBufferRef MemBuf,
93 LLVMModuleProviderRef *OutMP,
95 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
96 reinterpret_cast<LLVMModuleRef*>(OutMP),
100 /* Deprecated: Use LLVMGetBitcodeModule instead. */
101 LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
102 LLVMModuleProviderRef *OutMP,
104 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,