blob: 51b1cb093b213b8799fc6b7a6feec6473b31fc6e [file] [log] [blame]
Michael J. Spencer41ee0412012-12-05 00:29:32 +00001//===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Michael J. Spencer41ee0412012-12-05 00:29:32 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/TableGen/Error.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000010#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/SmallString.h"
12#include "llvm/ADT/Twine.h"
Chandler Carruthb034cb72013-01-02 10:26:28 +000013#include "llvm/TableGen/Record.h"
14#include "llvm/TableGen/TableGenBackend.h"
Rui Ueyama8fb5a912013-08-28 20:04:31 +000015#include <cctype>
Chandler Carruth442f7842014-03-04 10:07:28 +000016#include <cstring>
Michael J. Spencer41ee0412012-12-05 00:29:32 +000017#include <map>
18
19using namespace llvm;
20
Rui Ueyama8fb5a912013-08-28 20:04:31 +000021// Ordering on Info. The logic should match with the consumer-side function in
22// llvm/Option/OptTable.h.
Craig Topper2b8419a2017-05-31 19:01:11 +000023// FIXME: Mmake this take StringRefs instead of null terminated strings to
24// simplify callers.
Michael J. Spencer41ee0412012-12-05 00:29:32 +000025static int StrCmpOptionName(const char *A, const char *B) {
Rui Ueyama8fb5a912013-08-28 20:04:31 +000026 const char *X = A, *Y = B;
27 char a = tolower(*A), b = tolower(*B);
Rui Ueyamac3779ff2013-08-28 00:02:06 +000028 while (a == b) {
29 if (a == '\0')
Rui Ueyama8fb5a912013-08-28 20:04:31 +000030 return strcmp(A, B);
Rui Ueyamac3779ff2013-08-28 00:02:06 +000031
Rui Ueyama8fb5a912013-08-28 20:04:31 +000032 a = tolower(*++X);
33 b = tolower(*++Y);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000034 }
Rui Ueyamac3779ff2013-08-28 00:02:06 +000035
36 if (a == '\0') // A is a prefix of B.
37 return 1;
38 if (b == '\0') // B is a prefix of A.
39 return -1;
40
41 // Otherwise lexicographic.
42 return (a < b) ? -1 : 1;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000043}
44
Benjamin Kramer8817cca2013-09-22 14:09:50 +000045static int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
46 const Record *A = *Av;
47 const Record *B = *Bv;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000048
49 // Sentinel options precede all others and are only ordered by precedence.
50 bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
51 bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
52 if (ASent != BSent)
53 return ASent ? -1 : 1;
54
55 // Compare options by name, unless they are sentinels.
56 if (!ASent)
Craig Topper2b8419a2017-05-31 19:01:11 +000057 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").str().c_str(),
58 B->getValueAsString("Name").str().c_str()))
Rui Ueyama8fb5a912013-08-28 20:04:31 +000059 return Cmp;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000060
61 if (!ASent) {
Craig Topper2b8419a2017-05-31 19:01:11 +000062 std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes");
63 std::vector<StringRef> BPrefixes = B->getValueAsListOfStrings("Prefixes");
Michael J. Spencer41ee0412012-12-05 00:29:32 +000064
Craig Topper2b8419a2017-05-31 19:01:11 +000065 for (std::vector<StringRef>::const_iterator APre = APrefixes.begin(),
66 AEPre = APrefixes.end(),
67 BPre = BPrefixes.begin(),
68 BEPre = BPrefixes.end();
69 APre != AEPre &&
70 BPre != BEPre;
71 ++APre, ++BPre) {
72 if (int Cmp = StrCmpOptionName(APre->str().c_str(), BPre->str().c_str()))
Michael J. Spencer41ee0412012-12-05 00:29:32 +000073 return Cmp;
74 }
75 }
76
77 // Then by the kind precedence;
78 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
79 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
80 if (APrec == BPrec &&
81 A->getValueAsListOfStrings("Prefixes") ==
82 B->getValueAsListOfStrings("Prefixes")) {
Peter Collingbournea5906f02013-10-20 03:19:25 +000083 PrintError(A->getLoc(), Twine("Option is equivalent to"));
Michael J. Spencer41ee0412012-12-05 00:29:32 +000084 PrintError(B->getLoc(), Twine("Other defined here"));
85 PrintFatalError("Equivalent Options found.");
86 }
87 return APrec < BPrec ? -1 : 1;
88}
89
90static const std::string getOptionName(const Record &R) {
91 // Use the record name unless EnumName is defined.
92 if (isa<UnsetInit>(R.getValueInit("EnumName")))
93 return R.getName();
94
95 return R.getValueAsString("EnumName");
96}
97
98static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
99 OS << '"';
100 OS.write_escaped(Str);
101 OS << '"';
102 return OS;
103}
104
105/// OptParserEmitter - This tablegen backend takes an input .td file
106/// describing a list of options and emits a data structure for parsing and
107/// working with those options when given an input command line.
108namespace llvm {
109void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
110 // Get the option groups and options.
111 const std::vector<Record*> &Groups =
112 Records.getAllDerivedDefinitions("OptionGroup");
113 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
114
115 emitSourceFileHeader("Option Parsing Definitions", OS);
116
117 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
118 // Generate prefix groups.
119 typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
120 typedef std::map<PrefixKeyT, std::string> PrefixesT;
121 PrefixesT Prefixes;
122 Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
123 unsigned CurPrefix = 0;
124 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
125 const Record &R = *Opts[i];
Craig Topper2b8419a2017-05-31 19:01:11 +0000126 std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000127 PrefixKeyT prfkey(prf.begin(), prf.end());
128 unsigned NewPrefix = CurPrefix + 1;
129 if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
130 Twine(NewPrefix)).str())).second)
131 CurPrefix = NewPrefix;
132 }
133
134 // Dump prefixes.
135
136 OS << "/////////\n";
137 OS << "// Prefixes\n\n";
138 OS << "#ifdef PREFIX\n";
139 OS << "#define COMMA ,\n";
140 for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
141 I != E; ++I) {
142 OS << "PREFIX(";
143
144 // Prefix name.
145 OS << I->second;
146
147 // Prefix values.
148 OS << ", {";
149 for (PrefixKeyT::const_iterator PI = I->first.begin(),
150 PE = I->first.end(); PI != PE; ++PI) {
151 OS << "\"" << *PI << "\" COMMA ";
152 }
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000153 OS << "nullptr})\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000154 }
155 OS << "#undef COMMA\n";
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000156 OS << "#endif // PREFIX\n\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000157
158 OS << "/////////\n";
159 OS << "// Groups\n\n";
160 OS << "#ifdef OPTION\n";
161 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
162 const Record &R = *Groups[i];
163
164 // Start a single option entry.
Hans Wennborg8669b972013-07-31 23:28:51 +0000165 OS << "OPTION(";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000166
167 // The option prefix;
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000168 OS << "nullptr";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000169
170 // The option string.
171 OS << ", \"" << R.getValueAsString("Name") << '"';
172
173 // The option identifier name.
174 OS << ", "<< getOptionName(R);
175
176 // The option kind.
177 OS << ", Group";
178
179 // The containing option group (if any).
180 OS << ", ";
181 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
182 OS << getOptionName(*DI->getDef());
183 else
184 OS << "INVALID";
185
186 // The other option arguments (unused for groups).
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000187 OS << ", INVALID, nullptr, 0, 0";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000188
189 // The option help text.
190 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
191 OS << ",\n";
192 OS << " ";
193 write_cstring(OS, R.getValueAsString("HelpText"));
194 } else
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000195 OS << ", nullptr";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000196
197 // The option meta-variable name (unused).
Yuka Takahashiba5d4af2017-06-20 16:31:31 +0000198 OS << ", nullptr";
199
200 // The option Values (unused for groups).
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000201 OS << ", nullptr)\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000202 }
203 OS << "\n";
204
205 OS << "//////////\n";
206 OS << "// Options\n\n";
207 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
208 const Record &R = *Opts[i];
209
210 // Start a single option entry.
Hans Wennborg8669b972013-07-31 23:28:51 +0000211 OS << "OPTION(";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000212
213 // The option prefix;
Craig Topper2b8419a2017-05-31 19:01:11 +0000214 std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000215 OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
216
217 // The option string.
218 write_cstring(OS, R.getValueAsString("Name"));
219
220 // The option identifier name.
221 OS << ", "<< getOptionName(R);
222
223 // The option kind.
224 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
225
226 // The containing option group (if any).
227 OS << ", ";
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000228 const ListInit *GroupFlags = nullptr;
229 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
230 GroupFlags = DI->getDef()->getValueAsListInit("Flags");
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000231 OS << getOptionName(*DI->getDef());
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000232 } else
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000233 OS << "INVALID";
234
235 // The option alias (if any).
236 OS << ", ";
237 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
238 OS << getOptionName(*DI->getDef());
239 else
240 OS << "INVALID";
241
Hans Wennborg5fdcf862013-07-31 22:44:41 +0000242 // The option alias arguments (if any).
243 // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
244 // would become "foo\0bar\0". Note that the compiler adds an implicit
245 // terminating \0 at the end.
246 OS << ", ";
Craig Topper2b8419a2017-05-31 19:01:11 +0000247 std::vector<StringRef> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
Hans Wennborg5fdcf862013-07-31 22:44:41 +0000248 if (AliasArgs.size() == 0) {
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000249 OS << "nullptr";
Hans Wennborg5fdcf862013-07-31 22:44:41 +0000250 } else {
251 OS << "\"";
252 for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
253 OS << AliasArgs[i] << "\\0";
254 OS << "\"";
255 }
256
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000257 // The option flags.
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000258 OS << ", ";
259 int NumFlags = 0;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000260 const ListInit *LI = R.getValueAsListInit("Flags");
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000261 for (Init *I : *LI)
262 OS << (NumFlags++ ? " | " : "")
263 << cast<DefInit>(I)->getDef()->getName();
264 if (GroupFlags) {
265 for (Init *I : *GroupFlags)
266 OS << (NumFlags++ ? " | " : "")
267 << cast<DefInit>(I)->getDef()->getName();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000268 }
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000269 if (NumFlags == 0)
270 OS << '0';
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000271
272 // The option parameter field.
273 OS << ", " << R.getValueAsInt("NumArgs");
274
275 // The option help text.
276 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
277 OS << ",\n";
278 OS << " ";
279 write_cstring(OS, R.getValueAsString("HelpText"));
280 } else
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000281 OS << ", nullptr";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000282
283 // The option meta-variable name.
284 OS << ", ";
285 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
286 write_cstring(OS, R.getValueAsString("MetaVarName"));
287 else
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000288 OS << "nullptr";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000289
Yuka Takahashiba5d4af2017-06-20 16:31:31 +0000290 // The option Values. Used for shell autocompletion.
291 OS << ", ";
292 if (!isa<UnsetInit>(R.getValueInit("Values")))
293 write_cstring(OS, R.getValueAsString("Values"));
294 else
295 OS << "nullptr";
296
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000297 OS << ")\n";
298 }
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000299 OS << "#endif // OPTION\n";
Yuka Takahashi24bc6a42017-08-29 00:09:31 +0000300
301 OS << "\n";
302 OS << "#ifdef OPTTABLE_ARG_INIT\n";
303 OS << "//////////\n";
304 OS << "// Option Values\n\n";
305 for (unsigned I = 0, E = Opts.size(); I != E; ++I) {
306 const Record &R = *Opts[I];
307 if (isa<UnsetInit>(R.getValueInit("ValuesCode")))
308 continue;
309 OS << "{\n";
Yuka Takahashi45244ed2017-08-29 02:01:56 +0000310 OS << "bool ValuesWereAdded;\n";
Yuka Takahashi24bc6a42017-08-29 00:09:31 +0000311 OS << R.getValueAsString("ValuesCode");
312 OS << "\n";
313 for (const std::string &Pref : R.getValueAsListOfStrings("Prefixes")) {
Yuka Takahashi45244ed2017-08-29 02:01:56 +0000314 OS << "ValuesWereAdded = Opt.addValues(";
Yuka Takahashi24bc6a42017-08-29 00:09:31 +0000315 std::string S = (Pref + R.getValueAsString("Name")).str();
316 write_cstring(OS, S);
317 OS << ", Values);\n";
318 OS << "(void)ValuesWereAdded;\n";
319 OS << "assert(ValuesWereAdded && \"Couldn't add values to "
320 "OptTable!\");\n";
321 }
322 OS << "}\n";
323 }
324 OS << "\n";
325 OS << "#endif // OPTTABLE_ARG_INIT\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000326}
327} // end namespace llvm