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/Core.h"
12 #include "llvm-c/IRReader.h"
13 #include "llvm/AsmParser/Parser.h"
14 #include "llvm/Bitcode/ReaderWriter.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/Timer.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <system_error>
22
23 using namespace llvm;
24
25 namespace llvm {
26 extern bool TimePassesIsEnabled;
27 }
28
29 static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
30 static const char *const TimeIRParsingName = "Parse IR";
31
32 static std::unique_ptr<Module>
getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer,SMDiagnostic & Err,LLVMContext & Context)33 getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
34 LLVMContext &Context) {
35 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
36 (const unsigned char *)Buffer->getBufferEnd())) {
37 ErrorOr<Module *> ModuleOrErr =
38 getLazyBitcodeModule(std::move(Buffer), Context);
39 if (std::error_code EC = ModuleOrErr.getError()) {
40 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
41 EC.message());
42 return nullptr;
43 }
44 return std::unique_ptr<Module>(ModuleOrErr.get());
45 }
46
47 return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
48 }
49
getLazyIRFileModule(StringRef Filename,SMDiagnostic & Err,LLVMContext & Context)50 std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
51 SMDiagnostic &Err,
52 LLVMContext &Context) {
53 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
54 MemoryBuffer::getFileOrSTDIN(Filename);
55 if (std::error_code EC = FileOrErr.getError()) {
56 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
57 "Could not open input file: " + EC.message());
58 return nullptr;
59 }
60
61 return getLazyIRModule(std::move(FileOrErr.get()), Err, Context);
62 }
63
parseIR(MemoryBufferRef Buffer,SMDiagnostic & Err,LLVMContext & Context)64 std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
65 LLVMContext &Context) {
66 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
67 TimePassesIsEnabled);
68 if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
69 (const unsigned char *)Buffer.getBufferEnd())) {
70 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
71 if (std::error_code EC = ModuleOrErr.getError()) {
72 Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
73 EC.message());
74 return nullptr;
75 }
76 return std::unique_ptr<Module>(ModuleOrErr.get());
77 }
78
79 return parseAssembly(Buffer, Err, Context);
80 }
81
parseIRFile(StringRef Filename,SMDiagnostic & Err,LLVMContext & Context)82 std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
83 LLVMContext &Context) {
84 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
85 MemoryBuffer::getFileOrSTDIN(Filename);
86 if (std::error_code EC = FileOrErr.getError()) {
87 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
88 "Could not open input file: " + EC.message());
89 return nullptr;
90 }
91
92 return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context);
93 }
94
95 //===----------------------------------------------------------------------===//
96 // C API.
97 //===----------------------------------------------------------------------===//
98
LLVMParseIRInContext(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM,char ** OutMessage)99 LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
100 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
101 char **OutMessage) {
102 SMDiagnostic Diag;
103
104 std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
105 *OutM =
106 wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
107
108 if(!*OutM) {
109 if (OutMessage) {
110 std::string buf;
111 raw_string_ostream os(buf);
112
113 Diag.print(nullptr, os, false);
114 os.flush();
115
116 *OutMessage = strdup(buf.c_str());
117 }
118 return 1;
119 }
120
121 return 0;
122 }
123