LLVM  8.0.1
BitReader.cpp
Go to the documentation of this file.
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-c/Core.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.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. */
26  char **OutMessage) {
27  return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
28  OutMessage);
29 }
30 
32  LLVMModuleRef *OutModule) {
33  return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
34 }
35 
37  LLVMMemoryBufferRef MemBuf,
38  LLVMModuleRef *OutModule,
39  char **OutMessage) {
40  MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
41  LLVMContext &Ctx = *unwrap(ContextRef);
42 
43  Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
44  if (Error Err = ModuleOrErr.takeError()) {
45  std::string Message;
46  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
47  Message = EIB.message();
48  });
49  if (OutMessage)
50  *OutMessage = strdup(Message.c_str());
51  *OutModule = wrap((Module *)nullptr);
52  return 1;
53  }
54 
55  *OutModule = wrap(ModuleOrErr.get().release());
56  return 0;
57 }
58 
60  LLVMMemoryBufferRef MemBuf,
61  LLVMModuleRef *OutModule) {
62  MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
63  LLVMContext &Ctx = *unwrap(ContextRef);
64 
65  ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
67  if (ModuleOrErr.getError()) {
68  *OutModule = wrap((Module *)nullptr);
69  return 1;
70  }
71 
72  *OutModule = wrap(ModuleOrErr.get().release());
73  return 0;
74 }
75 
76 /* Reads a module from the specified path, returning via the OutModule parameter
77  a module provider which performs lazy deserialization. Returns 0 on success.
78  Optionally returns a human-readable error message via OutMessage. */
80  LLVMMemoryBufferRef MemBuf,
81  LLVMModuleRef *OutM, char **OutMessage) {
82  LLVMContext &Ctx = *unwrap(ContextRef);
83  std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
85  getOwningLazyBitcodeModule(std::move(Owner), Ctx);
86  // Release the buffer if we didn't take ownership of it since we never owned
87  // it anyway.
88  (void)Owner.release();
89 
90  if (Error Err = ModuleOrErr.takeError()) {
91  std::string Message;
92  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
93  Message = EIB.message();
94  });
95  if (OutMessage)
96  *OutMessage = strdup(Message.c_str());
97  *OutM = wrap((Module *)nullptr);
98  return 1;
99  }
100 
101  *OutM = wrap(ModuleOrErr.get().release());
102 
103  return 0;
104 }
105 
107  LLVMMemoryBufferRef MemBuf,
108  LLVMModuleRef *OutM) {
109  LLVMContext &Ctx = *unwrap(ContextRef);
110  std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
111 
113  Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
114  Owner.release();
115 
116  if (ModuleOrErr.getError()) {
117  *OutM = wrap((Module *)nullptr);
118  return 1;
119  }
120 
121  *OutM = wrap(ModuleOrErr.get().release());
122  return 0;
123 }
124 
126  char **OutMessage) {
128  OutMessage);
129 }
130 
132  LLVMModuleRef *OutM) {
134 }
Represents either an error or a value T.
Definition: ErrorOr.h:57
This class represents lattice values for constants.
Definition: AllocatorList.h:24
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Types.h:62
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:49
virtual std::string message() const
Return the error message as a string.
Definition: Error.h:57
Error takeError()
Take ownership of the stored error.
Definition: Error.h:553
LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage)
Definition: BitReader.cpp:25
Base class for error info classes.
Definition: Error.h:49
LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition: BitReader.cpp:59
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:195
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
struct LLVMOpaqueContext * LLVMContextRef
The top-level container for all LLVM global data.
Definition: Types.h:54
LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage)
Definition: BitReader.cpp:36
LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM)
Reads a module from the specified path, returning via the OutMP parameter a module provider which per...
Definition: BitReader.cpp:106
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
std::error_code getError() const
Definition: ErrorOr.h:160
LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage)
Definition: BitReader.cpp:125
int LLVMBool
Definition: Types.h:29
LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition: BitReader.cpp:31
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:905
Module.h This file contains the declarations for the Module class.
reference get()
Returns a reference to the stored T value.
Definition: Error.h:533
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context)
Read the specified bitcode file, returning the module.
LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage)
Reads a module from the specified path, returning via the OutMP parameter a module provider which per...
Definition: BitReader.cpp:79
ErrorOr< T > expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected< T > Val)
Definition: BitcodeReader.h:42
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:190
LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM)
Definition: BitReader.cpp:131
Expected< std::unique_ptr< Module > > getOwningLazyBitcodeModule(std::unique_ptr< MemoryBuffer > &&Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata=false, bool IsImporting=false)
Like getLazyBitcodeModule, except that the module takes ownership of the memory buffer if successful...
Lightweight error class with error context and mandatory checking.
Definition: Error.h:158
LLVMContextRef LLVMGetGlobalContext(void)
Obtain the global context instance.
Definition: Core.cpp:83
reference get()
Definition: ErrorOr.h:157