1//===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
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/IRReader/IRReader.h"
10#include "llvm-c/IRReader.h"
11#include "llvm/AsmParser/Parser.h"
12#include "llvm/Bitcode/BitcodeReader.h"
13#include "llvm/IR/LLVMContext.h"
14#include "llvm/IR/Module.h"
15#include "llvm/Support/MemoryBuffer.h"
16#include "llvm/Support/SourceMgr.h"
17#include "llvm/Support/Timer.h"
18#include "llvm/Support/raw_ostream.h"
19#include <optional>
20#include <system_error>
21
22using namespace llvm;
23
24namespace llvm {
25 extern bool TimePassesIsEnabled;
26}
27
28const char TimeIRParsingGroupName[] = "irparse";
29const char TimeIRParsingGroupDescription[] = "LLVM IR Parsing";
30const char TimeIRParsingName[] = "parse";
31const char TimeIRParsingDescription[] = "Parse IR";
32
33std::unique_ptr<Module>
34llvm::getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
35 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
36 if (isBitcode(BufPtr: (const unsigned char *)Buffer->getBufferStart(),
37 BufEnd: (const unsigned char *)Buffer->getBufferEnd())) {
38 Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
39 Buffer: std::move(Buffer), Context, ShouldLazyLoadMetadata);
40 if (Error E = ModuleOrErr.takeError()) {
41 handleAllErrors(E: std::move(E), Handlers: [&](ErrorInfoBase &EIB) {
42 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
43 EIB.message());
44 });
45 return nullptr;
46 }
47 return std::move(ModuleOrErr.get());
48 }
49
50 return parseAssembly(F: Buffer->getMemBufferRef(), Err, Context);
51}
52
53std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
54 SMDiagnostic &Err,
55 LLVMContext &Context,
56 bool ShouldLazyLoadMetadata) {
57 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
58 MemoryBuffer::getFileOrSTDIN(Filename);
59 if (std::error_code EC = FileOrErr.getError()) {
60 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
61 "Could not open input file: " + EC.message());
62 return nullptr;
63 }
64
65 return getLazyIRModule(Buffer: std::move(FileOrErr.get()), Err, Context,
66 ShouldLazyLoadMetadata);
67}
68
69std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
70 LLVMContext &Context,
71 ParserCallbacks Callbacks) {
72 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
73 TimeIRParsingGroupName, TimeIRParsingGroupDescription,
74 TimePassesIsEnabled);
75 if (isBitcode(BufPtr: (const unsigned char *)Buffer.getBufferStart(),
76 BufEnd: (const unsigned char *)Buffer.getBufferEnd())) {
77 Expected<std::unique_ptr<Module>> ModuleOrErr =
78 parseBitcodeFile(Buffer, Context, Callbacks);
79 if (Error E = ModuleOrErr.takeError()) {
80 handleAllErrors(E: std::move(E), Handlers: [&](ErrorInfoBase &EIB) {
81 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
82 EIB.message());
83 });
84 return nullptr;
85 }
86 return std::move(ModuleOrErr.get());
87 }
88
89 return parseAssembly(F: Buffer, Err, Context, Slots: nullptr,
90 DataLayoutCallback: Callbacks.DataLayout.value_or(
91 u: [](StringRef, StringRef) { return std::nullopt; }));
92}
93
94std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
95 LLVMContext &Context,
96 ParserCallbacks Callbacks) {
97 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
98 MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
99 if (std::error_code EC = FileOrErr.getError()) {
100 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
101 "Could not open input file: " + EC.message());
102 return nullptr;
103 }
104
105 return parseIR(Buffer: FileOrErr.get()->getMemBufferRef(), Err, Context, Callbacks);
106}
107
108//===----------------------------------------------------------------------===//
109// C API.
110//===----------------------------------------------------------------------===//
111
112LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
113 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
114 char **OutMessage) {
115 SMDiagnostic Diag;
116
117 std::unique_ptr<MemoryBuffer> MB(unwrap(P: MemBuf));
118 *OutM =
119 wrap(P: parseIR(Buffer: MB->getMemBufferRef(), Err&: Diag, Context&: *unwrap(P: ContextRef)).release());
120
121 if(!*OutM) {
122 if (OutMessage) {
123 std::string buf;
124 raw_string_ostream os(buf);
125
126 Diag.print(ProgName: nullptr, S&: os, ShowColors: false);
127 os.flush();
128
129 *OutMessage = strdup(s: buf.c_str());
130 }
131 return 1;
132 }
133
134 return 0;
135}
136