LLVM  10.0.0svn
Main.cpp
Go to the documentation of this file.
1 //===- Main.cpp - Top-Level TableGen implementation -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // TableGen is a tool which can be used to build up a description of something,
10 // then invoke one or more "tablegen backends" to emit information about the
11 // description in some predefined format. In practice, this is used by the LLVM
12 // code generators to automate generation of a code generator through a
13 // high-level description of the target.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #include "llvm/TableGen/Main.h"
18 #include "TGParser.h"
19 #include "llvm/ADT/StringExtras.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 
49 MacroNames("D", cl::desc("Name of the macro to be defined"),
50  cl::value_desc("macro name"), cl::Prefix);
51 
52 static int reportError(const char *ProgName, Twine Msg) {
53  errs() << ProgName << ": " << Msg;
54  errs().flush();
55  return 1;
56 }
57 
58 /// Create a dependency file for `-d` option.
59 ///
60 /// This functionality is really only for the benefit of the build system.
61 /// It is similar to GCC's `-M*` family of options.
62 static int createDependencyFile(const TGParser &Parser, const char *argv0) {
63  if (OutputFilename == "-")
64  return reportError(argv0, "the option -d must be used together with -o\n");
65 
66  std::error_code EC;
68  if (EC)
69  return reportError(argv0, "error opening " + DependFilename + ":" +
70  EC.message() + "\n");
71  DepOut.os() << OutputFilename << ":";
72  for (const auto &Dep : Parser.getDependencies()) {
73  DepOut.os() << ' ' << Dep.first;
74  }
75  DepOut.os() << "\n";
76  DepOut.keep();
77  return 0;
78 }
79 
80 int llvm::TableGenMain(char *argv0, TableGenMainFn *MainFn) {
81  RecordKeeper Records;
82 
83  // Parse the input file.
86  if (std::error_code EC = FileOrErr.getError())
87  return reportError(argv0, "Could not open input file '" + InputFilename +
88  "': " + EC.message() + "\n");
89 
90  // Tell SrcMgr about this buffer, which is what TGParser will pick up.
91  SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
92 
93  // Record the location of the include directory so that the lexer can find
94  // it later.
96 
97  TGParser Parser(SrcMgr, MacroNames, Records);
98 
99  if (Parser.ParseFile())
100  return 1;
101 
102  // Write output to memory.
103  std::string OutString;
104  raw_string_ostream Out(OutString);
105  if (MainFn(Out, Records))
106  return 1;
107 
108  // Always write the depfile, even if the main output hasn't changed.
109  // If it's missing, Ninja considers the output dirty. If this was below
110  // the early exit below and someone deleted the .inc.d file but not the .inc
111  // file, tablegen would never write the depfile.
112  if (!DependFilename.empty()) {
113  if (int Ret = createDependencyFile(Parser, argv0))
114  return Ret;
115  }
116 
117  // Only updates the real output file if there are any differences.
118  // This prevents recompilation of all the files depending on it if there
119  // aren't any.
120  if (auto ExistingOrErr = MemoryBuffer::getFile(OutputFilename))
121  if (std::move(ExistingOrErr.get())->getBuffer() == Out.str())
122  return 0;
123 
124  std::error_code EC;
126  if (EC)
127  return reportError(argv0, "error opening " + OutputFilename + ":" +
128  EC.message() + "\n");
129  OutFile.os() << Out.str();
130 
131  if (ErrorsPrinted > 0)
132  return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
133 
134  // Declare success.
135  OutFile.keep();
136  return 0;
137 }
Represents either an error or a value T.
Definition: ErrorOr.h:56
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
SourceMgr SrcMgr
Definition: Error.cpp:23
static cl::list< std::string > MacroNames("D", cl::desc("Name of the macro to be defined"), cl::value_desc("macro name"), cl::Prefix)
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:80
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:153
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:432
std::error_code getError() const
Definition: ErrorOr.h:159
int TableGenMain(char *argv0, TableGenMainFn *MainFn)
Definition: Main.cpp:80
std::string & str()
Flushes the stream contents to the target string and returns the string&#39;s reference.
Definition: raw_ostream.h:519
const TGLexer::DependenciesMapTy & getDependencies() const
Definition: TGParser.h:132
static int reportError(const char *ProgName, Twine Msg)
Definition: Main.cpp:52
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:23
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...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:503
void setIncludeDirs(const std::vector< std::string > &Dirs)
Definition: SourceMgr.h:113
static int createDependencyFile(const TGParser &Parser, const char *argv0)
Create a dependency file for -d option.
Definition: Main.cpp:62
Represents a location in source code.
Definition: SMLoc.h:23
raw_fd_ostream & os()
Return the contained raw_fd_ostream.