LLVM  6.0.0svn
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"
20 #include "llvm/ADT/StringExtras.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 
33 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
34  cl::init("-"));
35 
37 DependFilename("d",
38  cl::desc("Dependency filename"),
39  cl::value_desc("filename"),
40  cl::init(""));
41 
43 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
44 
46 IncludeDirs("I", cl::desc("Directory of include files"),
47  cl::value_desc("directory"), cl::Prefix);
48 
49 static int reportError(const char *ProgName, Twine Msg) {
50  errs() << ProgName << ": " << Msg;
51  errs().flush();
52  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 static int createDependencyFile(const TGParser &Parser, const char *argv0) {
60  if (OutputFilename == "-")
61  return reportError(argv0, "the option -d must be used together with -o\n");
62 
63  std::error_code EC;
65  if (EC)
66  return reportError(argv0, "error opening " + DependFilename + ":" +
67  EC.message() + "\n");
68  DepOut.os() << OutputFilename << ":";
69  for (const auto &Dep : Parser.getDependencies()) {
70  DepOut.os() << ' ' << Dep.first;
71  }
72  DepOut.os() << "\n";
73  DepOut.keep();
74  return 0;
75 }
76 
77 int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
78  RecordKeeper Records;
79 
80  // Parse the input file.
83  if (std::error_code EC = FileOrErr.getError())
84  return reportError(argv0, "Could not open input file '" + InputFilename +
85  "': " + EC.message() + "\n");
86 
87  // Tell SrcMgr about this buffer, which is what TGParser will pick up.
88  SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
89 
90  // Record the location of the include directory so that the lexer can find
91  // it later.
93 
94  TGParser Parser(SrcMgr, Records);
95 
96  if (Parser.ParseFile())
97  return 1;
98 
99  std::error_code EC;
101  if (EC)
102  return reportError(argv0, "error opening " + OutputFilename + ":" +
103  EC.message() + "\n");
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  return reportError(argv0, utostr(ErrorsPrinted) + " errors.\n");
114 
115  // Declare success.
116  Out.keep();
117  return 0;
118 }
Represents either an error or a value T.
Definition: ErrorOr.h:69
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
SourceMgr SrcMgr
Definition: Error.cpp:23
static cl::list< std::string > IncludeDirs("I", cl::desc("Directory of include files"), cl::value_desc("directory"), cl::Prefix)
unsigned ErrorsPrinted
Definition: Error.cpp:24
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
This class contains a raw_fd_ostream and adds a few extra features commonly needed for compiler-like ...
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:125
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:406
std::error_code getError() const
Definition: ErrorOr.h:172
int TableGenMain(char *argv0, TableGenMainFn *MainFn)
Definition: Main.cpp:77
const TGLexer::DependenciesMapTy & getDependencies() const
Definition: TGParser.h:100
std::string utostr(uint64_t X, bool isNeg=false)
Definition: StringExtras.h:174
The file should be opened in text mode on platforms that make this distinction.
Definition: FileSystem.h:769
static int reportError(const char *ProgName, Twine Msg)
Definition: Main.cpp:49
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
bool(raw_ostream &OS, RecordKeeper &Records) TableGenMainFn
Perform the action using Records, and write output to OS.
Definition: Main.h:24
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
static cl::opt< std::string > InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"))
void keep()
Indicate that the tool&#39;s job wrt this output file has been successful and the file should not be dele...
void setIncludeDirs(const std::vector< std::string > &Dirs)
Definition: SourceMgr.h:85
static int createDependencyFile(const TGParser &Parser, const char *argv0)
Create a dependency file for -d option.
Definition: Main.cpp:59
Represents a location in source code.
Definition: SMLoc.h:24
raw_fd_ostream & os()
Return the contained raw_fd_ostream.