LLVM  3.7.0
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"
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  LLVMModuleRef *OutModule, char **OutMessage) {
27  return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
28  OutMessage);
29 }
30 
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 
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().release());
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. */
61  LLVMMemoryBufferRef MemBuf,
62  LLVMModuleRef *OutM,
63  char **OutMessage) {
64  std::string Message;
65  std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
66 
67  ErrorOr<std::unique_ptr<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().release());
79 
80  return 0;
81 
82 }
83 
85  char **OutMessage) {
87  OutMessage);
88 }
89 
90 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
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. */
102  LLVMModuleProviderRef *OutMP,
103  char **OutMessage) {
105  OutMP, OutMessage);
106 }
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
Used to pass regions of memory through LLVM interfaces.
Definition: Support.h:36
LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage)
Definition: BitReader.cpp:25
LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf, LLVMModuleProviderRef *OutMP, char **OutMessage)
Deprecated: Use LLVMGetBitcodeModule instead.
Definition: BitReader.cpp:101
LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleProviderRef *OutMP, char **OutMessage)
Deprecated: Use LLVMGetBitcodeModuleInContext instead.
Definition: BitReader.cpp:91
struct LLVMOpaqueModuleProvider * LLVMModuleProviderRef
Interface used to provide a module to JIT or interpreter.
Definition: Core.h:113
LLVMTargetDataRef wrap(const DataLayout *P)
Definition: DataLayout.h:469
LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage)
Definition: BitReader.cpp:31
DataLayout * unwrap(LLVMTargetDataRef P)
Definition: DataLayout.h:465
This is the base abstract class for diagnostic reporting in the backend.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
ErrorOr< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr)
Read the specified bitcode file, returning the module.
struct LLVMOpaqueContext * LLVMContextRef
The top-level container for all LLVM global data.
Definition: Core.h:70
LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage)
Definition: BitReader.cpp:84
int LLVMBool
Definition: Support.h:29
virtual void print(DiagnosticPrinter &DP) const =0
Print using the given DP a user-friendly message.
Module.h This file contains the declarations for the Module class.
Basic diagnostic printer that uses an underlying raw_ostream.
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:60
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Core.h:78
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:465
ErrorOr< std::unique_ptr< Module > > getLazyBitcodeModule(std::unique_ptr< MemoryBuffer > &&Buffer, LLVMContext &Context, DiagnosticHandlerFunction DiagnosticHandler=nullptr, bool ShouldLazyLoadMetadata=false)
Read the header of the specified bitcode buffer and prepare for lazy deserialization of function bodi...
LLVMContextRef LLVMGetGlobalContext(void)
Obtain the global context instance.
Definition: Core.cpp:80
LLVMContext & getGlobalContext()
getGlobalContext - Returns a global context.
Definition: LLVMContext.cpp:30
reference get()
Definition: ErrorOr.h:175