1 //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/IRReader/IRReader.h"
11 #include "llvm-c/IRReader.h"
12 #include "llvm/AsmParser/Parser.h"
13 #include "llvm/Bitcode/BitcodeReader.h"
14 #include "llvm/IR/LLVMContext.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include "llvm/Support/SourceMgr.h"
18 #include "llvm/Support/Timer.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <system_error>
25 extern bool TimePassesIsEnabled
;
28 static const char *const TimeIRParsingGroupName
= "irparse";
29 static const char *const TimeIRParsingGroupDescription
= "LLVM IR Parsing";
30 static const char *const TimeIRParsingName
= "parse";
31 static const char *const TimeIRParsingDescription
= "Parse IR";
33 static std::unique_ptr
<Module
>
34 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 bool UpgradeDebugInfo
,
72 StringRef DataLayoutString
) {
73 NamedRegionTimer
T(TimeIRParsingName
, TimeIRParsingDescription
,
74 TimeIRParsingGroupName
, TimeIRParsingGroupDescription
,
76 if (isBitcode((const unsigned char *)Buffer
.getBufferStart(),
77 (const unsigned char *)Buffer
.getBufferEnd())) {
78 Expected
<std::unique_ptr
<Module
>> ModuleOrErr
=
79 parseBitcodeFile(Buffer
, Context
);
80 if (Error E
= ModuleOrErr
.takeError()) {
81 handleAllErrors(std::move(E
), [&](ErrorInfoBase
&EIB
) {
82 Err
= SMDiagnostic(Buffer
.getBufferIdentifier(), SourceMgr::DK_Error
,
87 if (!DataLayoutString
.empty())
88 ModuleOrErr
.get()->setDataLayout(DataLayoutString
);
89 return std::move(ModuleOrErr
.get());
92 return parseAssembly(Buffer
, Err
, Context
, nullptr, UpgradeDebugInfo
,
96 std::unique_ptr
<Module
> llvm::parseIRFile(StringRef Filename
, SMDiagnostic
&Err
,
98 bool UpgradeDebugInfo
,
99 StringRef DataLayoutString
) {
100 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
101 MemoryBuffer::getFileOrSTDIN(Filename
);
102 if (std::error_code EC
= FileOrErr
.getError()) {
103 Err
= SMDiagnostic(Filename
, SourceMgr::DK_Error
,
104 "Could not open input file: " + EC
.message());
108 return parseIR(FileOrErr
.get()->getMemBufferRef(), Err
, Context
,
109 UpgradeDebugInfo
, DataLayoutString
);
112 //===----------------------------------------------------------------------===//
114 //===----------------------------------------------------------------------===//
116 LLVMBool
LLVMParseIRInContext(LLVMContextRef ContextRef
,
117 LLVMMemoryBufferRef MemBuf
, LLVMModuleRef
*OutM
,
121 std::unique_ptr
<MemoryBuffer
> MB(unwrap(MemBuf
));
123 wrap(parseIR(MB
->getMemBufferRef(), Diag
, *unwrap(ContextRef
)).release());
128 raw_string_ostream
os(buf
);
130 Diag
.print(nullptr, os
, false);
133 *OutMessage
= strdup(buf
.c_str());