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. */
LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule,char ** OutMessage)25 LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
26 LLVMModuleRef *OutModule, char **OutMessage) {
27 return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
28 OutMessage);
29 }
30
LLVMParseBitcodeInContext(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule,char ** OutMessage)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 (ModuleOrErr.getError()) {
45 if (OutMessage) {
46 Stream.flush();
47 *OutMessage = strdup(Message.c_str());
48 }
49 *OutModule = wrap((Module*)nullptr);
50 return 1;
51 }
52
53 *OutModule = wrap(ModuleOrErr.get());
54 return 0;
55 }
56
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. */
LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM,char ** OutMessage)60 LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
61 LLVMMemoryBufferRef MemBuf,
62 LLVMModuleRef *OutM,
63 char **OutMessage) {
64 std::string Message;
65 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
66
67 ErrorOr<Module *> ModuleOrErr =
68 getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
69 Owner.release();
70
71 if (std::error_code EC = ModuleOrErr.getError()) {
72 *OutM = wrap((Module *)nullptr);
73 if (OutMessage)
74 *OutMessage = strdup(EC.message().c_str());
75 return 1;
76 }
77
78 *OutM = wrap(ModuleOrErr.get());
79
80 return 0;
81
82 }
83
LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM,char ** OutMessage)84 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
85 char **OutMessage) {
86 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
87 OutMessage);
88 }
89
90 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleProviderRef * OutMP,char ** OutMessage)91 LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
92 LLVMMemoryBufferRef MemBuf,
93 LLVMModuleProviderRef *OutMP,
94 char **OutMessage) {
95 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
96 reinterpret_cast<LLVMModuleRef*>(OutMP),
97 OutMessage);
98 }
99
100 /* Deprecated: Use LLVMGetBitcodeModule instead. */
LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,LLVMModuleProviderRef * OutMP,char ** OutMessage)101 LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
102 LLVMModuleProviderRef *OutMP,
103 char **OutMessage) {
104 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,
105 OutMP, OutMessage);
106 }
107