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/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>
26 extern bool TimePassesIsEnabled;
29 static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
30 static const char *const TimeIRParsingName = "Parse IR";
32 static Module *getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
33 LLVMContext &Context) {
34 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
35 (const unsigned char *)Buffer->getBufferEnd())) {
37 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
38 if (std::error_code EC = ModuleOrErr.getError()) {
39 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
41 // getLazyBitcodeModule does not take ownership of the Buffer in the
46 return ModuleOrErr.get();
49 return ParseAssembly(Buffer, nullptr, Err, Context);
52 Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
53 LLVMContext &Context) {
54 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
55 MemoryBuffer::getFileOrSTDIN(Filename);
56 if (std::error_code EC = FileOrErr.getError()) {
57 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
58 "Could not open input file: " + EC.message());
62 return getLazyIRModule(FileOrErr.get().release(), Err, Context);
65 Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
66 LLVMContext &Context) {
67 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
69 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
70 (const unsigned char *)Buffer->getBufferEnd())) {
71 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
73 if (std::error_code EC = ModuleOrErr.getError())
74 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
77 M = ModuleOrErr.get();
78 // parseBitcodeFile does not take ownership of the Buffer.
82 return ParseAssembly(MemoryBuffer::getMemBuffer(
83 Buffer->getBuffer(), Buffer->getBufferIdentifier()),
84 nullptr, Err, Context);
87 Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
88 LLVMContext &Context) {
89 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
90 MemoryBuffer::getFileOrSTDIN(Filename);
91 if (std::error_code EC = FileOrErr.getError()) {
92 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
93 "Could not open input file: " + EC.message());
97 return ParseIR(FileOrErr.get().get(), Err, Context);
100 //===----------------------------------------------------------------------===//
102 //===----------------------------------------------------------------------===//
104 LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
105 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
109 std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
110 *OutM = wrap(ParseIR(MB.get(), Diag, *unwrap(ContextRef)));
115 raw_string_ostream os(buf);
117 Diag.print(nullptr, os, false);
120 *OutMessage = strdup(buf.c_str());