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