LLVM  3.7.0
Main.cpp
Go to the documentation of this file.
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"
24 #include "llvm/TableGen/Error.h"
25 #include "llvm/TableGen/Record.h"
26 #include <algorithm>
27 #include <cstdio>
28 #include <system_error>
29 using namespace llvm;
30 
32 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
33  cl::init("-"));
34 
36 DependFilename("d",
37  cl::desc("Dependency filename"),
38  cl::value_desc("filename"),
39  cl::init(""));
40 
42 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
43 
45 IncludeDirs("I", cl::desc("Directory of include files"),
46  cl::value_desc("directory"), cl::Prefix);
47 
48 /// \brief Create a dependency file for `-d` option.
49 ///
50 /// This functionality is really only for the benefit of the build system.
51 /// It is similar to GCC's `-M*` family of options.
52 static int createDependencyFile(const TGParser &Parser, const char *argv0) {
53  if (OutputFilename == "-") {
54  errs() << argv0 << ": the option -d must be used together with -o\n";
55  return 1;
56  }
57  std::error_code EC;
59  if (EC) {
60  errs() << argv0 << ": error opening " << DependFilename << ":"
61  << EC.message() << "\n";
62  return 1;
63  }
64  DepOut.os() << OutputFilename << ":";
65  for (const auto &Dep : Parser.getDependencies()) {
66  DepOut.os() << ' ' << Dep.first;
67  }
68  DepOut.os() << "\n";
69  DepOut.keep();
70  return 0;
71 }
72 
73 int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
74  RecordKeeper Records;
75 
76  // Parse the input file.
79  if (std::error_code EC = FileOrErr.getError()) {
80  errs() << "Could not open input file '" << InputFilename
81  << "': " << EC.message() << "\n";
82  return 1;
83  }
84 
85  // Tell SrcMgr about this buffer, which is what TGParser will pick up.
86  SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
87 
88  // Record the location of the include directory so that the lexer can find
89  // it later.
91 
92  TGParser Parser(SrcMgr, Records);
93 
94  if (Parser.ParseFile())
95  return 1;
96 
97  std::error_code EC;
99  if (EC) {
100  errs() << argv0 << ": error opening " << OutputFilename << ":"
101  << EC.message() << "\n";
102  return 1;
103  }
104  if (!DependFilename.empty()) {
105  if (int Ret = createDependencyFile(Parser, argv0))
106  return Ret;
107  }
108 
109  if (MainFn(Out.os(), Records))
110  return 1;
111 
112  if (ErrorsPrinted > 0) {
113  errs() << argv0 << ": " << ErrorsPrinted << " errors.\n";
114  return 1;
115  }
116 
117  // Declare success.
118  Out.keep();
119  return 0;
120 }
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
SourceMgr SrcMgr
raw_fd_ostream & os()
Return the contained raw_fd_ostream.
static cl::list< std::string > IncludeDirs("I", cl::desc("Directory of include files"), cl::value_desc("directory"), cl::Prefix)
unsigned ErrorsPrinted
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
bool TableGenMainFn(raw_ostream &OS, RecordKeeper &Records)
Perform the action using Records, and write output to OS.
Definition: Main.h:23
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:123
static cl::opt< std::string > DependFilename("d", cl::desc("Dependency filename"), cl::value_desc("filename"), cl::init(""))
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
const TGLexer::DependenciesMapTy & getDependencies() const
Definition: TGParser.h:101
This class contains a raw_fd_ostream and adds a few extra features commonly needed for compiler-like ...
static cl::opt< std::string > InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"))
int TableGenMain(char *argv0, TableGenMainFn *MainFn)
Definition: Main.cpp:73
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:592
void keep()
Indicate that the tool's job wrt this output file has been successful and the file should not be dele...
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
void setIncludeDirs(const std::vector< std::string > &Dirs)
Definition: SourceMgr.h:83
static int createDependencyFile(const TGParser &Parser, const char *argv0)
Create a dependency file for -d option.
Definition: Main.cpp:52
Represents a location in source code.
Definition: SMLoc.h:23