LCOV - code coverage report
Current view: top level - lib/AsmParser - Parser.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 45 64 70.3 %
Date: 2018-10-20 13:21:21 Functions: 9 12 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
       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             : // This library implements the functionality defined in llvm/AsmParser/Parser.h
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/AsmParser/Parser.h"
      15             : #include "LLParser.h"
      16             : #include "llvm/ADT/STLExtras.h"
      17             : #include "llvm/IR/Module.h"
      18             : #include "llvm/IR/ModuleSummaryIndex.h"
      19             : #include "llvm/Support/MemoryBuffer.h"
      20             : #include "llvm/Support/SourceMgr.h"
      21             : #include "llvm/Support/raw_ostream.h"
      22             : #include <cstring>
      23             : #include <system_error>
      24             : using namespace llvm;
      25             : 
      26       34601 : bool llvm::parseAssemblyInto(MemoryBufferRef F, Module *M,
      27             :                              ModuleSummaryIndex *Index, SMDiagnostic &Err,
      28             :                              SlotMapping *Slots, bool UpgradeDebugInfo,
      29             :                              StringRef DataLayoutString) {
      30             :   SourceMgr SM;
      31       34601 :   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
      32       69202 :   SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
      33             : 
      34       34601 :   LLVMContext Context;
      35      103775 :   return LLParser(F.getBuffer(), SM, Err, M, Index,
      36       34601 :                   M ? M->getContext() : Context, Slots, UpgradeDebugInfo,
      37             :                   DataLayoutString)
      38       34600 :       .Run();
      39             : }
      40             : 
      41             : std::unique_ptr<Module>
      42       33354 : llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
      43             :                     SlotMapping *Slots, bool UpgradeDebugInfo,
      44             :                     StringRef DataLayoutString) {
      45             :   std::unique_ptr<Module> M =
      46       66707 :       make_unique<Module>(F.getBufferIdentifier(), Context);
      47             : 
      48       66708 :   if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, UpgradeDebugInfo,
      49             :                         DataLayoutString))
      50             :     return nullptr;
      51             : 
      52             :   return M;
      53             : }
      54             : 
      55             : std::unique_ptr<Module>
      56         762 : llvm::parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
      57             :                         LLVMContext &Context, SlotMapping *Slots,
      58             :                         bool UpgradeDebugInfo, StringRef DataLayoutString) {
      59             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
      60         762 :       MemoryBuffer::getFileOrSTDIN(Filename);
      61         762 :   if (std::error_code EC = FileOrErr.getError()) {
      62           0 :     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
      63           0 :                        "Could not open input file: " + EC.message());
      64             :     return nullptr;
      65             :   }
      66             : 
      67             :   return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots,
      68        1524 :                        UpgradeDebugInfo, DataLayoutString);
      69             : }
      70             : 
      71        1247 : ParsedModuleAndIndex llvm::parseAssemblyWithIndex(
      72             :     MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
      73             :     SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
      74             :   std::unique_ptr<Module> M =
      75        2467 :       make_unique<Module>(F.getBufferIdentifier(), Context);
      76             :   std::unique_ptr<ModuleSummaryIndex> Index =
      77        1220 :       make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
      78             : 
      79        2494 :   if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
      80             :                         DataLayoutString))
      81         198 :     return {nullptr, nullptr};
      82             : 
      83             :   return {std::move(M), std::move(Index)};
      84             : }
      85             : 
      86        1247 : ParsedModuleAndIndex llvm::parseAssemblyFileWithIndex(
      87             :     StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
      88             :     SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
      89             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
      90        1247 :       MemoryBuffer::getFileOrSTDIN(Filename);
      91        1247 :   if (std::error_code EC = FileOrErr.getError()) {
      92           0 :     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
      93           0 :                        "Could not open input file: " + EC.message());
      94           0 :     return {nullptr, nullptr};
      95             :   }
      96             : 
      97             :   return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
      98             :                                 Context, Slots, UpgradeDebugInfo,
      99        2494 :                                 DataLayoutString);
     100             : }
     101             : 
     102             : std::unique_ptr<Module>
     103         226 : llvm::parseAssemblyString(StringRef AsmString, SMDiagnostic &Err,
     104             :                           LLVMContext &Context, SlotMapping *Slots,
     105             :                           bool UpgradeDebugInfo, StringRef DataLayoutString) {
     106             :   MemoryBufferRef F(AsmString, "<string>");
     107             :   return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo,
     108         226 :                        DataLayoutString);
     109             : }
     110             : 
     111           0 : static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F,
     112             :                                           ModuleSummaryIndex &Index,
     113             :                                           SMDiagnostic &Err) {
     114             :   SourceMgr SM;
     115           0 :   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
     116           0 :   SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
     117             : 
     118             :   // The parser holds a reference to a context that is unused when parsing the
     119             :   // index, but we need to initialize it.
     120           0 :   LLVMContext unusedContext;
     121           0 :   return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext).Run();
     122             : }
     123             : 
     124             : std::unique_ptr<ModuleSummaryIndex>
     125           0 : llvm::parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err) {
     126             :   std::unique_ptr<ModuleSummaryIndex> Index =
     127           0 :       make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
     128             : 
     129           0 :   if (parseSummaryIndexAssemblyInto(F, *Index, Err))
     130             :     return nullptr;
     131             : 
     132             :   return Index;
     133             : }
     134             : 
     135             : std::unique_ptr<ModuleSummaryIndex>
     136           0 : llvm::parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err) {
     137             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
     138           0 :       MemoryBuffer::getFileOrSTDIN(Filename);
     139           0 :   if (std::error_code EC = FileOrErr.getError()) {
     140           0 :     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
     141           0 :                        "Could not open input file: " + EC.message());
     142             :     return nullptr;
     143             :   }
     144             : 
     145           0 :   return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
     146             : }
     147             : 
     148        1042 : Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err,
     149             :                                    const Module &M, const SlotMapping *Slots) {
     150             :   SourceMgr SM;
     151        1042 :   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
     152        3126 :   SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
     153             :   Constant *C;
     154        2084 :   if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
     155        1042 :           .parseStandaloneConstantValue(C, Slots))
     156             :     return nullptr;
     157        1036 :   return C;
     158             : }
     159             : 
     160          11 : Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
     161             :                       const SlotMapping *Slots) {
     162             :   unsigned Read;
     163          11 :   Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
     164          11 :   if (!Ty)
     165             :     return nullptr;
     166          22 :   if (Read != Asm.size()) {
     167             :     SourceMgr SM;
     168           1 :     std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
     169           3 :     SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
     170           2 :     Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
     171           1 :                         SourceMgr::DK_Error, "expected end of string");
     172             :     return nullptr;
     173             :   }
     174             :   return Ty;
     175             : }
     176          22 : Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read,
     177             :                                  SMDiagnostic &Err, const Module &M,
     178             :                                  const SlotMapping *Slots) {
     179             :   SourceMgr SM;
     180          22 :   std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
     181          66 :   SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
     182             :   Type *Ty;
     183          44 :   if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
     184          22 :           .parseTypeAtBeginning(Ty, Read, Slots))
     185             :     return nullptr;
     186          22 :   return Ty;
     187             : }

Generated by: LCOV version 1.13