LCOV - code coverage report
Current view: top level - lib/IRReader - IRReader.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 27 52 51.9 %
Date: 2017-09-14 15:23:50 Functions: 4 7 57.1 %
Legend: Lines: hit not hit

          Line data    Source code
       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-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>
      22             : 
      23             : using namespace llvm;
      24             : 
      25             : namespace llvm {
      26             :   extern bool TimePassesIsEnabled;
      27             : }
      28             : 
      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";
      33             : 
      34             : static std::unique_ptr<Module>
      35         532 : getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
      36             :                 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
      37         532 :   if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
      38         532 :                 (const unsigned char *)Buffer->getBufferEnd())) {
      39             :     Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
      40         358 :         std::move(Buffer), Context, ShouldLazyLoadMetadata);
      41         537 :     if (Error E = ModuleOrErr.takeError()) {
      42           0 :       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
      43           0 :         Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
      44           0 :                            EIB.message());
      45           0 :       });
      46           0 :       return nullptr;
      47             :     }
      48         179 :     return std::move(ModuleOrErr.get());
      49             :   }
      50             : 
      51         353 :   return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
      52             : }
      53             : 
      54         532 : std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
      55             :                                                   SMDiagnostic &Err,
      56             :                                                   LLVMContext &Context,
      57             :                                                   bool ShouldLazyLoadMetadata) {
      58             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
      59        1064 :       MemoryBuffer::getFileOrSTDIN(Filename);
      60         532 :   if (std::error_code EC = FileOrErr.getError()) {
      61           0 :     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
      62           0 :                        "Could not open input file: " + EC.message());
      63           0 :     return nullptr;
      64             :   }
      65             : 
      66         532 :   return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
      67        1596 :                          ShouldLazyLoadMetadata);
      68             : }
      69             : 
      70       25578 : std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
      71             :                                       LLVMContext &Context) {
      72             :   NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
      73             :                      TimeIRParsingGroupName, TimeIRParsingGroupDescription,
      74      153467 :                      TimePassesIsEnabled);
      75       25578 :   if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
      76       25578 :                 (const unsigned char *)Buffer.getBufferEnd())) {
      77             :     Expected<std::unique_ptr<Module>> ModuleOrErr =
      78         550 :         parseBitcodeFile(Buffer, Context);
      79         825 :     if (Error E = ModuleOrErr.takeError()) {
      80           0 :       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
      81           0 :         Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
      82           0 :                            EIB.message());
      83           0 :       });
      84           0 :       return nullptr;
      85             :     }
      86         275 :     return std::move(ModuleOrErr.get());
      87             :   }
      88             : 
      89       25303 :   return parseAssembly(Buffer, Err, Context);
      90             : }
      91             : 
      92       25559 : std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
      93             :                                           LLVMContext &Context) {
      94             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
      95       51117 :       MemoryBuffer::getFileOrSTDIN(Filename);
      96       25559 :   if (std::error_code EC = FileOrErr.getError()) {
      97           2 :     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
      98           6 :                        "Could not open input file: " + EC.message());
      99           2 :     return nullptr;
     100             :   }
     101             : 
     102       51114 :   return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context);
     103             : }
     104             : 
     105             : //===----------------------------------------------------------------------===//
     106             : // C API.
     107             : //===----------------------------------------------------------------------===//
     108             : 
     109           0 : LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
     110             :                               LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
     111             :                               char **OutMessage) {
     112           0 :   SMDiagnostic Diag;
     113             : 
     114           0 :   std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
     115           0 :   *OutM =
     116           0 :       wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
     117             : 
     118           0 :   if(!*OutM) {
     119           0 :     if (OutMessage) {
     120           0 :       std::string buf;
     121           0 :       raw_string_ostream os(buf);
     122             : 
     123           0 :       Diag.print(nullptr, os, false);
     124           0 :       os.flush();
     125             : 
     126           0 :       *OutMessage = strdup(buf.c_str());
     127             :     }
     128             :     return 1;
     129             :   }
     130             : 
     131             :   return 0;
     132             : }

Generated by: LCOV version 1.13