Home | History | Annotate | Download | only in IRReader
      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"
     17 #include "llvm/Support/Timer.h"
     18 
     19 using namespace llvm;
     20 
     21 namespace llvm {
     22   extern bool TimePassesIsEnabled;
     23 }
     24 
     25 static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
     26 static const char *const TimeIRParsingName = "Parse IR";
     27 
     28 
     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())) {
     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,
     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());
     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) {
     62   NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
     63                      TimePassesIsEnabled);
     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, 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 }
     90