1 //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
20 #include <system_error>
25 extern bool TimePassesIsEnabled
;
28 const char TimeIRParsingGroupName
[] = "irparse";
29 const char TimeIRParsingGroupDescription
[] = "LLVM IR Parsing";
30 const char TimeIRParsingName
[] = "parse";
31 const char TimeIRParsingDescription
[] = "Parse IR";
33 std::unique_ptr
<Module
>
34 llvm::getLazyIRModule(std::unique_ptr
<MemoryBuffer
> Buffer
, SMDiagnostic
&Err
,
35 LLVMContext
&Context
, bool ShouldLazyLoadMetadata
) {
36 if (isBitcode((const unsigned char *)Buffer
->getBufferStart(),
37 (const unsigned char *)Buffer
->getBufferEnd())) {
38 Expected
<std::unique_ptr
<Module
>> ModuleOrErr
= getOwningLazyBitcodeModule(
39 std::move(Buffer
), Context
, ShouldLazyLoadMetadata
);
40 if (Error E
= ModuleOrErr
.takeError()) {
41 handleAllErrors(std::move(E
), [&](ErrorInfoBase
&EIB
) {
42 Err
= SMDiagnostic(Buffer
->getBufferIdentifier(), SourceMgr::DK_Error
,
47 return std::move(ModuleOrErr
.get());
50 return parseAssembly(Buffer
->getMemBufferRef(), Err
, Context
);
53 std::unique_ptr
<Module
> llvm::getLazyIRFileModule(StringRef Filename
,
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());
65 return getLazyIRModule(std::move(FileOrErr
.get()), Err
, Context
,
66 ShouldLazyLoadMetadata
);
69 std::unique_ptr
<Module
> llvm::parseIR(MemoryBufferRef Buffer
, SMDiagnostic
&Err
,
71 ParserCallbacks Callbacks
) {
72 NamedRegionTimer
T(TimeIRParsingName
, TimeIRParsingDescription
,
73 TimeIRParsingGroupName
, TimeIRParsingGroupDescription
,
75 if (isBitcode((const unsigned char *)Buffer
.getBufferStart(),
76 (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(std::move(E
), [&](ErrorInfoBase
&EIB
) {
81 Err
= SMDiagnostic(Buffer
.getBufferIdentifier(), SourceMgr::DK_Error
,
86 return std::move(ModuleOrErr
.get());
89 return parseAssembly(Buffer
, Err
, Context
, nullptr,
90 Callbacks
.DataLayout
.value_or(
91 [](StringRef
, StringRef
) { return std::nullopt
; }));
94 std::unique_ptr
<Module
> llvm::parseIRFile(StringRef Filename
, SMDiagnostic
&Err
,
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());
105 return parseIR(FileOrErr
.get()->getMemBufferRef(), Err
, Context
, Callbacks
);
108 //===----------------------------------------------------------------------===//
110 //===----------------------------------------------------------------------===//
112 LLVMBool
LLVMParseIRInContext(LLVMContextRef ContextRef
,
113 LLVMMemoryBufferRef MemBuf
, LLVMModuleRef
*OutM
,
117 std::unique_ptr
<MemoryBuffer
> MB(unwrap(MemBuf
));
119 wrap(parseIR(MB
->getMemBufferRef(), Diag
, *unwrap(ContextRef
)).release());
124 raw_string_ostream
os(buf
);
126 Diag
.print(nullptr, os
, false);
129 *OutMessage
= strdup(buf
.c_str());