LCOV - code coverage report
Current view: top level - lib/IRReader - IRReader.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 24 38 63.2 %
Date: 2018-10-20 13:21:21 Functions: 4 5 80.0 %
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/IRReader.h"
      12             : #include "llvm/AsmParser/Parser.h"
      13             : #include "llvm/Bitcode/BitcodeReader.h"
      14             : #include "llvm/IR/LLVMContext.h"
      15             : #include "llvm/IR/Module.h"
      16             : #include "llvm/Support/MemoryBuffer.h"
      17             : #include "llvm/Support/SourceMgr.h"
      18             : #include "llvm/Support/Timer.h"
      19             : #include "llvm/Support/raw_ostream.h"
      20             : #include <system_error>
      21             : 
      22             : using namespace llvm;
      23             : 
      24             : namespace llvm {
      25             :   extern bool TimePassesIsEnabled;
      26             : }
      27             : 
      28             : static const char *const TimeIRParsingGroupName = "irparse";
      29             : static const char *const TimeIRParsingGroupDescription = "LLVM IR Parsing";
      30             : static const char *const TimeIRParsingName = "parse";
      31             : static const char *const TimeIRParsingDescription = "Parse IR";
      32             : 
      33             : static std::unique_ptr<Module>
      34         547 : getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
      35             :                 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
      36         547 :   if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
      37         547 :                 (const unsigned char *)Buffer->getBufferEnd())) {
      38             :     Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
      39         362 :         std::move(Buffer), Context, ShouldLazyLoadMetadata);
      40         181 :     if (Error E = ModuleOrErr.takeError()) {
      41           0 :       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
      42             :         Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
      43             :                            EIB.message());
      44             :       });
      45             :       return nullptr;
      46             :     }
      47             :     return std::move(ModuleOrErr.get());
      48             :   }
      49             : 
      50         366 :   return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
      51             : }
      52             : 
      53         547 : std::unique_ptr<Module> llvm::getLazyIRFileModule(StringRef Filename,
      54             :                                                   SMDiagnostic &Err,
      55             :                                                   LLVMContext &Context,
      56             :                                                   bool ShouldLazyLoadMetadata) {
      57             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
      58         547 :       MemoryBuffer::getFileOrSTDIN(Filename);
      59         547 :   if (std::error_code EC = FileOrErr.getError()) {
      60           0 :     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
      61           0 :                        "Could not open input file: " + EC.message());
      62             :     return nullptr;
      63             :   }
      64             : 
      65             :   return getLazyIRModule(std::move(FileOrErr.get()), Err, Context,
      66        1641 :                          ShouldLazyLoadMetadata);
      67             : }
      68             : 
      69       31394 : std::unique_ptr<Module> llvm::parseIR(MemoryBufferRef Buffer, SMDiagnostic &Err,
      70             :                                       LLVMContext &Context,
      71             :                                       bool UpgradeDebugInfo,
      72             :                                       StringRef DataLayoutString) {
      73             :   NamedRegionTimer T(TimeIRParsingName, TimeIRParsingDescription,
      74             :                      TimeIRParsingGroupName, TimeIRParsingGroupDescription,
      75       62788 :                      TimePassesIsEnabled);
      76       31394 :   if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
      77             :                 (const unsigned char *)Buffer.getBufferEnd())) {
      78             :     Expected<std::unique_ptr<Module>> ModuleOrErr =
      79         588 :         parseBitcodeFile(Buffer, Context);
      80         294 :     if (Error E = ModuleOrErr.takeError()) {
      81           0 :       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
      82             :         Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
      83             :                            EIB.message());
      84             :       });
      85             :       return nullptr;
      86             :     }
      87         294 :     if (!DataLayoutString.empty())
      88           1 :       ModuleOrErr.get()->setDataLayout(DataLayoutString);
      89             :     return std::move(ModuleOrErr.get());
      90             :   }
      91             : 
      92             :   return parseAssembly(Buffer, Err, Context, nullptr, UpgradeDebugInfo,
      93       31100 :                        DataLayoutString);
      94             : }
      95             : 
      96       31366 : std::unique_ptr<Module> llvm::parseIRFile(StringRef Filename, SMDiagnostic &Err,
      97             :                                           LLVMContext &Context,
      98             :                                           bool UpgradeDebugInfo,
      99             :                                           StringRef DataLayoutString) {
     100             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
     101       31366 :       MemoryBuffer::getFileOrSTDIN(Filename);
     102       31366 :   if (std::error_code EC = FileOrErr.getError()) {
     103           4 :     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
     104           6 :                        "Could not open input file: " + EC.message());
     105             :     return nullptr;
     106             :   }
     107             : 
     108             :   return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context,
     109       62728 :                  UpgradeDebugInfo, DataLayoutString);
     110             : }
     111             : 
     112             : //===----------------------------------------------------------------------===//
     113             : // C API.
     114             : //===----------------------------------------------------------------------===//
     115             : 
     116           0 : LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
     117             :                               LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
     118             :                               char **OutMessage) {
     119           0 :   SMDiagnostic Diag;
     120             : 
     121             :   std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
     122           0 :   *OutM =
     123           0 :       wrap(parseIR(MB->getMemBufferRef(), Diag, *unwrap(ContextRef)).release());
     124             : 
     125           0 :   if(!*OutM) {
     126           0 :     if (OutMessage) {
     127             :       std::string buf;
     128           0 :       raw_string_ostream os(buf);
     129             : 
     130           0 :       Diag.print(nullptr, os, false);
     131             :       os.flush();
     132             : 
     133           0 :       *OutMessage = strdup(buf.c_str());
     134             :     }
     135           0 :     return 1;
     136             :   }
     137             : 
     138             :   return 0;
     139             : }

Generated by: LCOV version 1.13