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/ADT/OwningPtr.h"
12 #include "llvm/Assembly/Parser.h"
13 #include "llvm/Bitcode/ReaderWriter.h"
14 #include "llvm/Support/MemoryBuffer.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/system_error.h"
17 #include "llvm/Support/Timer.h"
22 extern bool TimePassesIsEnabled;
25 static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
26 static const char *const TimeIRParsingName = "Parse IR";
29 Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
30 LLVMContext &Context) {
31 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
32 (const unsigned char *)Buffer->getBufferEnd())) {
34 Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
36 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
38 // ParseBitcodeFile does not take ownership of the Buffer in the
45 return ParseAssembly(Buffer, 0, Err, Context);
48 Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
49 LLVMContext &Context) {
50 OwningPtr<MemoryBuffer> File;
51 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
52 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
53 "Could not open input file: " + ec.message());
57 return getLazyIRModule(File.take(), Err, Context);
60 Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
61 LLVMContext &Context) {
62 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
64 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
65 (const unsigned char *)Buffer->getBufferEnd())) {
67 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
69 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
71 // ParseBitcodeFile does not take ownership of the Buffer.
76 return ParseAssembly(Buffer, 0, Err, Context);
79 Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
80 LLVMContext &Context) {
81 OwningPtr<MemoryBuffer> File;
82 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
83 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
84 "Could not open input file: " + ec.message());
88 return ParseIR(File.take(), Err, Context);