LCOV - code coverage report
Current view: top level - lib/IRReader - IRReader.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 24 43 55.8 %
Date: 2018-06-17 00:07:59 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/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         542 : getLazyIRModule(std::unique_ptr<MemoryBuffer> Buffer, SMDiagnostic &Err,
      35             :                 LLVMContext &Context, bool ShouldLazyLoadMetadata) {
      36         542 :   if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
      37         542 :                 (const unsigned char *)Buffer->getBufferEnd())) {
      38             :     Expected<std::unique_ptr<Module>> ModuleOrErr = getOwningLazyBitcodeModule(
      39         364 :         std::move(Buffer), Context, ShouldLazyLoadMetadata);
      40         182 :     if (Error E = ModuleOrErr.takeError()) {
      41           0 :       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
      42           0 :         Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
      43           0 :                            EIB.message());
      44           0 :       });
      45             :       return nullptr;
      46             :     }
      47             :     return std::move(ModuleOrErr.get());
      48             :   }
      49             : 
      50         360 :   return parseAssembly(Buffer->getMemBufferRef(), Err, Context);
      51             : }
      52             : 
      53         542 : 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         542 :       MemoryBuffer::getFileOrSTDIN(Filename);
      59         542 :   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        1626 :                          ShouldLazyLoadMetadata);
      67             : }
      68             : 
      69       29982 : 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       59964 :                      TimePassesIsEnabled);
      76       29982 :   if (isBitcode((const unsigned char *)Buffer.getBufferStart(),
      77             :                 (const unsigned char *)Buffer.getBufferEnd())) {
      78             :     Expected<std::unique_ptr<Module>> ModuleOrErr =
      79         598 :         parseBitcodeFile(Buffer, Context);
      80         299 :     if (Error E = ModuleOrErr.takeError()) {
      81           0 :       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
      82           0 :         Err = SMDiagnostic(Buffer.getBufferIdentifier(), SourceMgr::DK_Error,
      83           0 :                            EIB.message());
      84           0 :       });
      85             :       return nullptr;
      86             :     }
      87         299 :     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       29683 :                        DataLayoutString);
      94             : }
      95             : 
      96       29962 : 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       29962 :       MemoryBuffer::getFileOrSTDIN(Filename);
     102       29962 :   if (std::error_code EC = FileOrErr.getError()) {
     103           4 :     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
     104          12 :                        "Could not open input file: " + EC.message());
     105             :     return nullptr;
     106             :   }
     107             : 
     108             :   return parseIR(FileOrErr.get()->getMemBufferRef(), Err, Context,
     109       59916 :                  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             :     return 1;
     136             :   }
     137             : 
     138             :   return 0;
     139             : }

Generated by: LCOV version 1.13