xref: /llvm-project/llvm/lib/TableGen/Main.cpp (revision f675ec6165ab6add5e57cd43a2e9fa1a9bc21d81)
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 "TGLexer.h"
19 #include "TGParser.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/SMLoc.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/ToolOutputFile.h"
29 #include "llvm/Support/raw_ostream.h"
30 #include "llvm/TableGen/Error.h"
31 #include "llvm/TableGen/Record.h"
32 #include <memory>
33 #include <string>
34 #include <system_error>
35 #include <utility>
36 #include <vector>
37 using namespace llvm;
38 
39 static cl::opt<std::string>
40 OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"),
41                cl::init("-"));
42 
43 static cl::opt<std::string>
44 DependFilename("d",
45                cl::desc("Dependency filename"),
46                cl::value_desc("filename"),
47                cl::init(""));
48 
49 static cl::opt<std::string>
50 InputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
51 
52 static cl::list<std::string>
53 IncludeDirs("I", cl::desc("Directory of include files"),
54             cl::value_desc("directory"), cl::Prefix);
55 
56 static cl::list<std::string>
57 MacroNames("D", cl::desc("Name of the macro to be defined"),
58             cl::value_desc("macro name"), cl::Prefix);
59 
60 static cl::opt<bool>
61 WriteIfChanged("write-if-changed", cl::desc("Only write output if it changed"));
62 
63 static cl::opt<bool>
64 TimePhases("time-phases", cl::desc("Time phases of parser and backend"));
65 
66 static cl::opt<bool> NoWarnOnUnusedTemplateArgs(
67     "no-warn-on-unused-template-args",
68     cl::desc("Disable unused template argument warnings."));
69 
70 static int reportError(const char *ProgName, Twine Msg) {
71   errs() << ProgName << ": " << Msg;
72   errs().flush();
73   return 1;
74 }
75 
76 /// Create a dependency file for `-d` option.
77 ///
78 /// This functionality is really only for the benefit of the build system.
79 /// It is similar to GCC's `-M*` family of options.
80 static int createDependencyFile(const TGParser &Parser, const char *argv0) {
81   if (OutputFilename == "-")
82     return reportError(argv0, "the option -d must be used together with -o\n");
83 
84   std::error_code EC;
85   ToolOutputFile DepOut(DependFilename, EC, sys::fs::OF_Text);
86   if (EC)
87     return reportError(argv0, "error opening " + DependFilename + ":" +
88                                   EC.message() + "\n");
89   DepOut.os() << OutputFilename << ":";
90   for (const auto &Dep : Parser.getDependencies()) {
91     DepOut.os() << ' ' << Dep;
92   }
93   DepOut.os() << "\n";
94   DepOut.keep();
95   return 0;
96 }
97 
98 int llvm::TableGenMain(const char *argv0,
99                        std::function<TableGenMainFn> MainFn) {
100   RecordKeeper Records;
101 
102   if (TimePhases)
103     Records.startPhaseTiming();
104 
105   // Parse the input file.
106 
107   Records.startTimer("Parse, build records");
108   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
109       MemoryBuffer::getFileOrSTDIN(InputFilename, /*IsText=*/true);
110   if (std::error_code EC = FileOrErr.getError())
111     return reportError(argv0, "Could not open input file '" + InputFilename +
112                                   "': " + EC.message() + "\n");
113 
114   Records.saveInputFilename(InputFilename);
115 
116   // Tell SrcMgr about this buffer, which is what TGParser will pick up.
117   SrcMgr.AddNewSourceBuffer(std::move(*FileOrErr), SMLoc());
118 
119   // Record the location of the include directory so that the lexer can find
120   // it later.
121   SrcMgr.setIncludeDirs(IncludeDirs);
122 
123   TGParser Parser(SrcMgr, MacroNames, Records, NoWarnOnUnusedTemplateArgs);
124 
125   if (Parser.ParseFile())
126     return 1;
127   Records.stopTimer();
128 
129   // Write output to memory.
130   Records.startBackendTimer("Backend overall");
131   std::string OutString;
132   raw_string_ostream Out(OutString);
133   unsigned status = 0;
134   if (MainFn)
135     status = MainFn(Out, Records);
136   else
137     return 1;
138   Records.stopBackendTimer();
139   if (status)
140     return 1;
141 
142   // Always write the depfile, even if the main output hasn't changed.
143   // If it's missing, Ninja considers the output dirty.  If this was below
144   // the early exit below and someone deleted the .inc.d file but not the .inc
145   // file, tablegen would never write the depfile.
146   if (!DependFilename.empty()) {
147     if (int Ret = createDependencyFile(Parser, argv0))
148       return Ret;
149   }
150 
151   Records.startTimer("Write output");
152   bool WriteFile = true;
153   if (WriteIfChanged) {
154     // Only updates the real output file if there are any differences.
155     // This prevents recompilation of all the files depending on it if there
156     // aren't any.
157     if (auto ExistingOrErr =
158             MemoryBuffer::getFile(OutputFilename, /*IsText=*/true))
159       if (std::move(ExistingOrErr.get())->getBuffer() == Out.str())
160         WriteFile = false;
161   }
162   if (WriteFile) {
163     std::error_code EC;
164     ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_Text);
165     if (EC)
166       return reportError(argv0, "error opening " + OutputFilename + ": " +
167                                     EC.message() + "\n");
168     OutFile.os() << Out.str();
169     if (ErrorsPrinted == 0)
170       OutFile.keep();
171   }
172 
173   Records.stopTimer();
174   Records.stopPhaseTiming();
175 
176   if (ErrorsPrinted > 0)
177     return reportError(argv0, Twine(ErrorsPrinted) + " errors.\n");
178   return 0;
179 }
180