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