LLVM  6.0.0svn
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"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
18 #include <cstring>
19 #include <string>
20 
21 using namespace llvm;
22 
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. */
27  char **OutMessage) {
28  return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
29  OutMessage);
30 }
31 
33  LLVMModuleRef *OutModule) {
34  return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
35 }
36 
38  LLVMMemoryBufferRef MemBuf,
39  LLVMModuleRef *OutModule,
40  char **OutMessage) {
41  MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
42  LLVMContext &Ctx = *unwrap(ContextRef);
43 
44  Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
45  if (Error Err = ModuleOrErr.takeError()) {
46  std::string Message;
47  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
48  Message = EIB.message();
49  });
50  if (OutMessage)
51  *OutMessage = strdup(Message.c_str());
52  *OutModule = wrap((Module *)nullptr);
53  return 1;
54  }
55 
56  *OutModule = wrap(ModuleOrErr.get().release());
57  return 0;
58 }
59 
61  LLVMMemoryBufferRef MemBuf,
62  LLVMModuleRef *OutModule) {
63  MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
64  LLVMContext &Ctx = *unwrap(ContextRef);
65 
66  ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
68  if (ModuleOrErr.getError()) {
69  *OutModule = wrap((Module *)nullptr);
70  return 1;
71  }
72 
73  *OutModule = wrap(ModuleOrErr.get().release());
74  return 0;
75 }
76 
77 /* Reads a module from the specified path, returning via the OutModule parameter
78  a module provider which performs lazy deserialization. Returns 0 on success.
79  Optionally returns a human-readable error message via OutMessage. */
81  LLVMMemoryBufferRef MemBuf,
82  LLVMModuleRef *OutM, char **OutMessage) {
83  LLVMContext &Ctx = *unwrap(ContextRef);
84  std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
86  getOwningLazyBitcodeModule(std::move(Owner), Ctx);
87  // Release the buffer if we didn't take ownership of it since we never owned
88  // it anyway.
89  (void)Owner.release();
90 
91  if (Error Err = ModuleOrErr.takeError()) {
92  std::string Message;
93  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
94  Message = EIB.message();
95  });
96  if (OutMessage)
97  *OutMessage = strdup(Message.c_str());
98  *OutM = wrap((Module *)nullptr);
99  return 1;
100  }
101 
102  *OutM = wrap(ModuleOrErr.get().release());
103 
104  return 0;
105 }
106 
108  LLVMMemoryBufferRef MemBuf,
109  LLVMModuleRef *OutM) {
110  LLVMContext &Ctx = *unwrap(ContextRef);
111  std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
112 
114  Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
115  Owner.release();
116 
117  if (ModuleOrErr.getError()) {
118  *OutM = wrap((Module *)nullptr);
119  return 1;
120  }
121 
122  *OutM = wrap(ModuleOrErr.get().release());
123  return 0;
124 }
125 
127  char **OutMessage) {
129  OutMessage);
130 }
131 
133  LLVMModuleRef *OutM) {
135 }
Represents either an error or a value T.
Definition: ErrorOr.h:69
Compute iterated dominance frontiers using a linear time algorithm.
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:63
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:55
Error takeError()
Take ownership of the stored error.
Definition: Error.h:537
LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage)
Definition: BitReader.cpp:26
Base class for error info classes.
Definition: Error.h:47
LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition: BitReader.cpp:60
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:37
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:107
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
std::error_code getError() const
Definition: ErrorOr.h:172
LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage)
Definition: BitReader.cpp:126
int LLVMBool
Definition: Types.h:29
LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition: BitReader.cpp:32
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that it requires that all errors be handled by the given han...
Definition: Error.h:889
Module.h This file contains the declarations for the Module class.
reference get()
Returns a reference to the stored T value.
Definition: Error.h:517
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:80
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:132
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:156
LLVMContextRef LLVMGetGlobalContext(void)
Obtain the global context instance.
Definition: Core.cpp:83
reference get()
Definition: ErrorOr.h:169