LLVM 22.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(getGlobalContextForCAPI(), MemBuf, OutModule,
26 OutMessage);
27}
28
34
37 LLVMModuleRef *OutModule,
38 char **OutMessage) {
39 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
40 LLVMContext &Ctx = *unwrap(ContextRef);
41
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
60 LLVMModuleRef *OutModule) {
61 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
62 LLVMContext &Ctx = *unwrap(ContextRef);
63
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. */
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
LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition BitReader.cpp:29
LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage)
Definition BitReader.cpp:23
LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM)
LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage)
static ManagedStatic< DebugCounterOwner > Owner
Module.h This file contains the declarations for the Module class.
Base class for error info classes.
Definition Error.h:44
virtual std::string message() const
Return the error message as a string.
Definition Error.h:52
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:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition BitReader.cpp:58
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
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 given memory buffer, returning via the OutMP parameter a module provider whic...
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.
LLVM_ABI 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:990
ErrorOr< T > expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected< T > Val)
LLVM_ABI LLVMContextRef getGlobalContextForCAPI()
Get the deprecated global context for use by the C API.
Definition Core.cpp:100
Attribute unwrap(LLVMAttributeRef Attr)
Definition Attributes.h:351
LLVMAttributeRef wrap(Attribute Attr)
Definition Attributes.h:346
LLVM_ABI 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.