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/Core.h"
12 #include "llvm-c/IRReader.h"
13 #include "llvm/AsmParser/Parser.h"
14 #include "llvm/Bitcode/BitcodeReader.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>
26 extern bool TimePassesIsEnabled;
29 static const char *const TimeIRParsingGroupName = "irparse";
30 static const char *const TimeIRParsingGroupDescription = "LLVM IR Parsing";
31 static const char *const TimeIRParsingName = "parse";
32 static const char *const TimeIRParsingDescription = "Parse IR";
34 static std::unique_ptr<Module>
35 getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
36 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
37 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
38 (const unsigned char *)Buffer->getBufferEnd())) {
39 Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
40 std::move(Buffer), Context, ShouldLazyLoadMetadata);
41 if (Error E = ModuleOrErr.takeError()) {
42 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
43 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
48 return std::move(ModuleOrErr.get());
51 return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
54 std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
57 bool ShouldLazyLoadMetadata) {
58 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
59 MemoryBuffer::getFileOrSTDIN(Filename);
60 if (std::error_code EC = FileOrErr.getError()) {
61 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
62 "Could not open input file: " + EC.message());
66 return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
67 ShouldLazyLoadMetadata);
70 std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
71 LLVMContext &Context) {
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);
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);
92 std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
93 LLVMContext &Context) {
94 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
95 MemoryBuffer::getFileOrSTDIN(Filename);
96 if (std::error_code EC = FileOrErr.getError()) {
97 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
98 "Could not open input file: " + EC.message());
102 return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context);
105 //===----------------------------------------------------------------------===//
107 //===----------------------------------------------------------------------===//
109 LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
110 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
114 std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
116 wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
121 raw_string_ostream os(buf);
123 Diag.print(nullptr, os, false);
126 *OutMessage = strdup(buf.c_str());