1 //===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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>
21
22 using namespace llvm;
23
24 namespace llvm {
25 extern bool TimePassesIsEnabled;
26 }
27
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";
32
33 static std::unique_ptr<Module>
getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer,SMDiagnostic & Err,LLVMContext & Context,bool ShouldLazyLoadMetadata)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,
43 EIB.message());
44 });
45 return nullptr;
46 }
47 return std::move(ModuleOrErr.get());
48 }
49
50 return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
51 }
52
getLazyIRFileModule(StringRef Filename,SMDiagnostic & Err,LLVMContext & Context,bool ShouldLazyLoadMetadata)53 std::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(std::move(FileOrErr.get()), Err, Context,
66 ShouldLazyLoadMetadata);
67 }
68
parseIR(MemoryBufferRef Buffer,SMDiagnostic & Err,LLVMContext & Context,bool UpgradeDebugInfo,StringRef DataLayoutString)69 std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
70 LLVMContext &Context,
71 bool UpgradeDebugInfo,
72 StringRef DataLayoutString) {
73 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
74 TimeIRParsingGroupName, TimeIRParsingGroupDescription,
75 TimePassesIsEnabled);
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,
83 EIB.message());
84 });
85 return nullptr;
86 }
87 if (!DataLayoutString.empty())
88 ModuleOrErr.get()->setDataLayout(DataLayoutString);
89 return std::move(ModuleOrErr.get());
90 }
91
92 return parseAssembly(Buffer, Err, Context, nullptr, UpgradeDebugInfo,
93 DataLayoutString);
94 }
95
parseIRFile(StringRef Filename,SMDiagnostic & Err,LLVMContext & Context,bool UpgradeDebugInfo,StringRef DataLayoutString)96 std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
97 LLVMContext &Context,
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());
105 return nullptr;
106 }
107
108 return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context,
109 UpgradeDebugInfo, DataLayoutString);
110 }
111
112 //===----------------------------------------------------------------------===//
113 // C API.
114 //===----------------------------------------------------------------------===//
115
LLVMParseIRInContext(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM,char ** OutMessage)116 LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
117 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
118 char **OutMessage) {
119 SMDiagnostic Diag;
120
121 std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
122 *OutM =
123 wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
124
125 if(!*OutM) {
126 if (OutMessage) {
127 std::string buf;
128 raw_string_ostream os(buf);
129
130 Diag.print(nullptr, os, false);
131 os.flush();
132
133 *OutMessage = strdup(buf.c_str());
134 }
135 return 1;
136 }
137
138 return 0;
139 }
140