LLVM  10.0.0svn
BitReader.cpp
Go to the documentation of this file.
1 //===-- BitReader.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm-c/BitReader.h"
10 #include "llvm-c/Core.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Module.h"
16 #include <cstring>
17 #include <string>
18 
19 using namespace llvm;
20 
21 /* Builds a module from the bitcode in the specified memory buffer, returning a
22  reference to the module via the OutModule parameter. Returns 0 on success.
23  Optionally returns a human-readable error message via OutMessage. */
25  char **OutMessage) {
26  return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
27  OutMessage);
28 }
29 
31  LLVMModuleRef *OutModule) {
32  return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
33 }
34 
36  LLVMMemoryBufferRef MemBuf,
37  LLVMModuleRef *OutModule,
38  char **OutMessage) {
39  MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
40  LLVMContext &Ctx = *unwrap(ContextRef);
41 
42  Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
43  if (Error Err = ModuleOrErr.takeError()) {
44  std::string Message;
45  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
46  Message = EIB.message();
47  });
48  if (OutMessage)
49  *OutMessage = strdup(Message.c_str());
50  *OutModule = wrap((Module *)nullptr);
51  return 1;
52  }
53 
54  *OutModule = wrap(ModuleOrErr.get().release());
55  return 0;
56 }
57 
59  LLVMMemoryBufferRef MemBuf,
60  LLVMModuleRef *OutModule) {
61  MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
62  LLVMContext &Ctx = *unwrap(ContextRef);
63 
64  ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
66  if (ModuleOrErr.getError()) {
67  *OutModule = wrap((Module *)nullptr);
68  return 1;
69  }
70 
71  *OutModule = wrap(ModuleOrErr.get().release());
72  return 0;
73 }
74 
75 /* Reads a module from the specified path, returning via the OutModule parameter
76  a module provider which performs lazy deserialization. Returns 0 on success.
77  Optionally returns a human-readable error message via OutMessage. */
79  LLVMMemoryBufferRef MemBuf,
80  LLVMModuleRef *OutM, char **OutMessage) {
81  LLVMContext &Ctx = *unwrap(ContextRef);
82  std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
84  getOwningLazyBitcodeModule(std::move(Owner), Ctx);
85  // Release the buffer if we didn't take ownership of it since we never owned
86  // it anyway.
87  (void)Owner.release();
88 
89  if (Error Err = ModuleOrErr.takeError()) {
90  std::string Message;
91  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
92  Message = EIB.message();
93  });
94  if (OutMessage)
95  *OutMessage = strdup(Message.c_str());
96  *OutM = wrap((Module *)nullptr);
97  return 1;
98  }
99 
100  *OutM = wrap(ModuleOrErr.get().release());
101 
102  return 0;
103 }
104 
106  LLVMMemoryBufferRef MemBuf,
107  LLVMModuleRef *OutM) {
108  LLVMContext &Ctx = *unwrap(ContextRef);
109  std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
110 
112  Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
113  Owner.release();
114 
115  if (ModuleOrErr.getError()) {
116  *OutM = wrap((Module *)nullptr);
117  return 1;
118  }
119 
120  *OutM = wrap(ModuleOrErr.get().release());
121  return 0;
122 }
123 
125  char **OutMessage) {
127  OutMessage);
128 }
129 
131  LLVMModuleRef *OutM) {
133 }
Represents either an error or a value T.
Definition: ErrorOr.h:56
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:66
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:56
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage)
Definition: BitReader.cpp:24
Base class for error info classes.
Definition: Error.h:48
LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition: BitReader.cpp:58
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:204
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
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:35
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:105
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
std::error_code getError() const
Definition: ErrorOr.h:159
LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage)
Definition: BitReader.cpp:124
int LLVMBool
Definition: Types.h:29
LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition: BitReader.cpp:30
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:924
Module.h This file contains the declarations for the Module class.
reference get()
Returns a reference to the stored T value.
Definition: Error.h:532
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:78
ErrorOr< T > expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected< T > Val)
Definition: BitcodeReader.h:41
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:199
LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM)
Definition: BitReader.cpp:130
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:157
LLVMContextRef LLVMGetGlobalContext(void)
Obtain the global context instance.
Definition: Core.cpp:82
reference get()
Definition: ErrorOr.h:156