LCOV - code coverage report
Current view: top level - lib/TableGen - Main.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 31 50 62.0 %
Date: 2017-09-14 15:23:50 Functions: 4 5 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
       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             : // TableGen is a tool which can be used to build up a description of something,
      11             : // then invoke one or more "tablegen backends" to emit information about the
      12             : // description in some predefined format.  In practice, this is used by the LLVM
      13             : // code generators to automate generation of a code generator through a
      14             : // high-level description of the target.
      15             : //
      16             : //===----------------------------------------------------------------------===//
      17             : 
      18             : #include "llvm/TableGen/Main.h"
      19             : #include "TGParser.h"
      20             : #include "llvm/ADT/StringExtras.h"
      21             : #include "llvm/Support/CommandLine.h"
      22             : #include "llvm/Support/FileSystem.h"
      23             : #include "llvm/Support/MemoryBuffer.h"
      24             : #include "llvm/Support/ToolOutputFile.h"
      25             : #include "llvm/TableGen/Error.h"
      26             : #include "llvm/TableGen/Record.h"
      27             : #include <algorithm>
      28             : #include <cstdio>
      29             : #include <system_error>
      30             : using namespace llvm;
      31             : 
      32             : static cl::opt<std::string>
      33        1375 : OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
      34         825 :                cl::init("-"));
      35             : 
      36             : static cl::opt<std::string>
      37         275 : DependFilename("d",
      38         825 :                cl::desc("Dependency filename"),
      39         825 :                cl::value_desc("filename"),
      40         825 :                cl::init(""));
      41             : 
      42             : static cl::opt<std::string>
      43         550 : InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
      44             : 
      45             : static cl::list<std::string>
      46        1100 : IncludeDirs("I", cl::desc("Directory of include files"),
      47        1100 :             cl::value_desc("directory"), cl::Prefix);
      48             : 
      49           2 : static int reportError(const char *ProgName, Twine Msg) {
      50           4 :   errs() << ProgName << ": " << Msg;
      51           4 :   errs().flush();
      52           2 :   return 1;
      53             : }
      54             : 
      55             : /// \brief Create a dependency file for `-d` option.
      56             : ///
      57             : /// This functionality is really only for the benefit of the build system.
      58             : /// It is similar to GCC's `-M*` family of options.
      59           0 : static int createDependencyFile(const TGParser &Parser, const char *argv0) {
      60           0 :   if (OutputFilename == "-")
      61           0 :     return reportError(argv0, "the option -d must be used together with -o\n");
      62             : 
      63           0 :   std::error_code EC;
      64           0 :   tool_output_file DepOut(DependFilename, EC, sys::fs::F_Text);
      65           0 :   if (EC)
      66           0 :     return reportError(argv0, "error opening " + DependFilename + ":" +
      67           0 :                                   EC.message() + "\n");
      68           0 :   DepOut.os() << OutputFilename << ":";
      69           0 :   for (const auto &Dep : Parser.getDependencies()) {
      70           0 :     DepOut.os() << ' ' << Dep.first;
      71             :   }
      72           0 :   DepOut.os() << "\n";
      73           0 :   DepOut.keep();
      74           0 :   return 0;
      75             : }
      76             : 
      77         275 : int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
      78         550 :   RecordKeeper Records;
      79             : 
      80             :   // Parse the input file.
      81             :   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
      82         550 :       MemoryBuffer::getFileOrSTDIN(InputFilename);
      83         275 :   if (std::error_code EC = FileOrErr.getError())
      84           0 :     return reportError(argv0, "Could not open input file '" + InputFilename +
      85           0 :                                   "': " + EC.message() + "\n");
      86             : 
      87             :   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
      88        1100 :   SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
      89             : 
      90             :   // Record the location of the include directory so that the lexer can find
      91             :   // it later.
      92         550 :   SrcMgr.setIncludeDirs(IncludeDirs);
      93             : 
      94         550 :   TGParser Parser(SrcMgr, Records);
      95             : 
      96         275 :   if (Parser.ParseFile())
      97             :     return 1;
      98             : 
      99         274 :   std::error_code EC;
     100         274 :   tool_output_file Out(OutputFilename, EC, sys::fs::F_Text);
     101         274 :   if (EC)
     102           0 :     return reportError(argv0, "error opening " + OutputFilename + ":" +
     103           0 :                                   EC.message() + "\n");
     104         274 :   if (!DependFilename.empty()) {
     105           0 :     if (int Ret = createDependencyFile(Parser, argv0))
     106             :       return Ret;
     107             :   }
     108             : 
     109         274 :   if (MainFn(Out.os(), Records))
     110             :     return 1;
     111             : 
     112         274 :   if (ErrorsPrinted > 0)
     113           8 :     return reportError(argv0, utostr(ErrorsPrinted) + " errors.\n");
     114             : 
     115             :   // Declare success.
     116         272 :   Out.keep();
     117         272 :   return 0;
     118         825 : }

Generated by: LCOV version 1.13