Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 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/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" |
Eli Bendersky | 1629965 | 2013-04-03 15:33:45 +0000 | [diff] [blame^] | 17 | #include "llvm/Support/Timer.h" |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
Eli Bendersky | 1629965 | 2013-04-03 15:33:45 +0000 | [diff] [blame^] | 21 | namespace llvm { |
| 22 | extern bool TimePassesIsEnabled; |
| 23 | } |
| 24 | |
| 25 | static const char *TimeIRParsingGroupName = "LLVM IR Parsing"; |
| 26 | static const char *TimeIRParsingName = "Parse IR"; |
| 27 | |
| 28 | |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 29 | Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err, |
Eli Bendersky | 19801a6 | 2013-04-01 19:47:56 +0000 | [diff] [blame] | 30 | LLVMContext &Context) { |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 31 | if (isBitcode((const unsigned char *)Buffer->getBufferStart(), |
| 32 | (const unsigned char *)Buffer->getBufferEnd())) { |
| 33 | std::string ErrMsg; |
| 34 | Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg); |
| 35 | if (M == 0) { |
| 36 | Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, |
| 37 | ErrMsg); |
| 38 | // ParseBitcodeFile does not take ownership of the Buffer in the |
| 39 | // case of an error. |
| 40 | delete Buffer; |
| 41 | } |
| 42 | return M; |
| 43 | } |
| 44 | |
| 45 | return ParseAssembly(Buffer, 0, Err, Context); |
| 46 | } |
| 47 | |
| 48 | Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err, |
Eli Bendersky | 19801a6 | 2013-04-01 19:47:56 +0000 | [diff] [blame] | 49 | LLVMContext &Context) { |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 50 | OwningPtr<MemoryBuffer> File; |
| 51 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) { |
| 52 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 53 | "Could not open input file: " + ec.message()); |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | return getLazyIRModule(File.take(), Err, Context); |
| 58 | } |
| 59 | |
| 60 | Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err, |
| 61 | LLVMContext &Context) { |
Eli Bendersky | 1629965 | 2013-04-03 15:33:45 +0000 | [diff] [blame^] | 62 | NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName, |
| 63 | TimePassesIsEnabled); |
Chandler Carruth | 7fc162f | 2013-03-26 02:25:37 +0000 | [diff] [blame] | 64 | if (isBitcode((const unsigned char *)Buffer->getBufferStart(), |
| 65 | (const unsigned char *)Buffer->getBufferEnd())) { |
| 66 | std::string ErrMsg; |
| 67 | Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg); |
| 68 | if (M == 0) |
| 69 | Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error, |
| 70 | ErrMsg); |
| 71 | // ParseBitcodeFile does not take ownership of the Buffer. |
| 72 | delete Buffer; |
| 73 | return M; |
| 74 | } |
| 75 | |
| 76 | return ParseAssembly(Buffer, 0, Err, Context); |
| 77 | } |
| 78 | |
| 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.c_str(), File)) { |
| 83 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 84 | "Could not open input file: " + ec.message()); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | return ParseIR(File.take(), Err, Context); |
| 89 | } |