1139f7f9bSDimitry Andric //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
2139f7f9bSDimitry Andric //
3139f7f9bSDimitry Andric // The LLVM Compiler Infrastructure
4139f7f9bSDimitry Andric //
5139f7f9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
6139f7f9bSDimitry Andric // License. See LICENSE.TXT for details.
7139f7f9bSDimitry Andric //
8139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
9139f7f9bSDimitry Andric
10139f7f9bSDimitry Andric #include "llvm/TableGen/Error.h"
11139f7f9bSDimitry Andric #include "llvm/ADT/STLExtras.h"
12139f7f9bSDimitry Andric #include "llvm/ADT/SmallString.h"
13139f7f9bSDimitry Andric #include "llvm/ADT/Twine.h"
14139f7f9bSDimitry Andric #include "llvm/TableGen/Record.h"
15139f7f9bSDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
16f785676fSDimitry Andric #include <cctype>
1791bc56edSDimitry Andric #include <cstring>
18139f7f9bSDimitry Andric #include <map>
19139f7f9bSDimitry Andric
20139f7f9bSDimitry Andric using namespace llvm;
21139f7f9bSDimitry Andric
22f785676fSDimitry Andric // Ordering on Info. The logic should match with the consumer-side function in
23f785676fSDimitry Andric // llvm/Option/OptTable.h.
24f9448bf3SDimitry Andric // FIXME: Mmake this take StringRefs instead of null terminated strings to
25f9448bf3SDimitry Andric // simplify callers.
StrCmpOptionName(const char * A,const char * B)26139f7f9bSDimitry Andric static int StrCmpOptionName(const char *A, const char *B) {
27f785676fSDimitry Andric const char *X = A, *Y = B;
28f785676fSDimitry Andric char a = tolower(*A), b = tolower(*B);
29139f7f9bSDimitry Andric while (a == b) {
30139f7f9bSDimitry Andric if (a == '\0')
31f785676fSDimitry Andric return strcmp(A, B);
32139f7f9bSDimitry Andric
33f785676fSDimitry Andric a = tolower(*++X);
34f785676fSDimitry Andric b = tolower(*++Y);
35139f7f9bSDimitry Andric }
36139f7f9bSDimitry Andric
37139f7f9bSDimitry Andric if (a == '\0') // A is a prefix of B.
38139f7f9bSDimitry Andric return 1;
39139f7f9bSDimitry Andric if (b == '\0') // B is a prefix of A.
40139f7f9bSDimitry Andric return -1;
41139f7f9bSDimitry Andric
42139f7f9bSDimitry Andric // Otherwise lexicographic.
43139f7f9bSDimitry Andric return (a < b) ? -1 : 1;
44139f7f9bSDimitry Andric }
45139f7f9bSDimitry Andric
CompareOptionRecords(Record * const * Av,Record * const * Bv)46f785676fSDimitry Andric static int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
47f785676fSDimitry Andric const Record *A = *Av;
48f785676fSDimitry Andric const Record *B = *Bv;
49139f7f9bSDimitry Andric
50139f7f9bSDimitry Andric // Sentinel options precede all others and are only ordered by precedence.
51139f7f9bSDimitry Andric bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
52139f7f9bSDimitry Andric bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
53139f7f9bSDimitry Andric if (ASent != BSent)
54139f7f9bSDimitry Andric return ASent ? -1 : 1;
55139f7f9bSDimitry Andric
56139f7f9bSDimitry Andric // Compare options by name, unless they are sentinels.
57139f7f9bSDimitry Andric if (!ASent)
58f9448bf3SDimitry Andric if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").str().c_str(),
59f9448bf3SDimitry Andric B->getValueAsString("Name").str().c_str()))
60139f7f9bSDimitry Andric return Cmp;
61139f7f9bSDimitry Andric
62139f7f9bSDimitry Andric if (!ASent) {
63f9448bf3SDimitry Andric std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes");
64f9448bf3SDimitry Andric std::vector<StringRef> BPrefixes = B->getValueAsListOfStrings("Prefixes");
65139f7f9bSDimitry Andric
66f9448bf3SDimitry Andric for (std::vector<StringRef>::const_iterator APre = APrefixes.begin(),
67139f7f9bSDimitry Andric AEPre = APrefixes.end(),
68139f7f9bSDimitry Andric BPre = BPrefixes.begin(),
69139f7f9bSDimitry Andric BEPre = BPrefixes.end();
70139f7f9bSDimitry Andric APre != AEPre &&
71139f7f9bSDimitry Andric BPre != BEPre;
72139f7f9bSDimitry Andric ++APre, ++BPre) {
73f9448bf3SDimitry Andric if (int Cmp = StrCmpOptionName(APre->str().c_str(), BPre->str().c_str()))
74139f7f9bSDimitry Andric return Cmp;
75139f7f9bSDimitry Andric }
76139f7f9bSDimitry Andric }
77139f7f9bSDimitry Andric
78139f7f9bSDimitry Andric // Then by the kind precedence;
79139f7f9bSDimitry Andric int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
80139f7f9bSDimitry Andric int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
81139f7f9bSDimitry Andric if (APrec == BPrec &&
82139f7f9bSDimitry Andric A->getValueAsListOfStrings("Prefixes") ==
83139f7f9bSDimitry Andric B->getValueAsListOfStrings("Prefixes")) {
84f785676fSDimitry Andric PrintError(A->getLoc(), Twine("Option is equivalent to"));
85139f7f9bSDimitry Andric PrintError(B->getLoc(), Twine("Other defined here"));
86139f7f9bSDimitry Andric PrintFatalError("Equivalent Options found.");
87139f7f9bSDimitry Andric }
88139f7f9bSDimitry Andric return APrec < BPrec ? -1 : 1;
89139f7f9bSDimitry Andric }
90139f7f9bSDimitry Andric
getOptionName(const Record & R)91139f7f9bSDimitry Andric static const std::string getOptionName(const Record &R) {
92139f7f9bSDimitry Andric // Use the record name unless EnumName is defined.
93139f7f9bSDimitry Andric if (isa<UnsetInit>(R.getValueInit("EnumName")))
94139f7f9bSDimitry Andric return R.getName();
95139f7f9bSDimitry Andric
96139f7f9bSDimitry Andric return R.getValueAsString("EnumName");
97139f7f9bSDimitry Andric }
98139f7f9bSDimitry Andric
write_cstring(raw_ostream & OS,llvm::StringRef Str)99139f7f9bSDimitry Andric static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
100139f7f9bSDimitry Andric OS << '"';
101139f7f9bSDimitry Andric OS.write_escaped(Str);
102139f7f9bSDimitry Andric OS << '"';
103139f7f9bSDimitry Andric return OS;
104139f7f9bSDimitry Andric }
105139f7f9bSDimitry Andric
106139f7f9bSDimitry Andric /// OptParserEmitter - This tablegen backend takes an input .td file
107139f7f9bSDimitry Andric /// describing a list of options and emits a data structure for parsing and
108139f7f9bSDimitry Andric /// working with those options when given an input command line.
109139f7f9bSDimitry Andric namespace llvm {
EmitOptParser(RecordKeeper & Records,raw_ostream & OS)110139f7f9bSDimitry Andric void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
111139f7f9bSDimitry Andric // Get the option groups and options.
112139f7f9bSDimitry Andric const std::vector<Record*> &Groups =
113139f7f9bSDimitry Andric Records.getAllDerivedDefinitions("OptionGroup");
114139f7f9bSDimitry Andric std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
115139f7f9bSDimitry Andric
116139f7f9bSDimitry Andric emitSourceFileHeader("Option Parsing Definitions", OS);
117139f7f9bSDimitry Andric
118139f7f9bSDimitry Andric array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
119139f7f9bSDimitry Andric // Generate prefix groups.
120139f7f9bSDimitry Andric typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
121139f7f9bSDimitry Andric typedef std::map<PrefixKeyT, std::string> PrefixesT;
122139f7f9bSDimitry Andric PrefixesT Prefixes;
123139f7f9bSDimitry Andric Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
124139f7f9bSDimitry Andric unsigned CurPrefix = 0;
125139f7f9bSDimitry Andric for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
126139f7f9bSDimitry Andric const Record &R = *Opts[i];
127f9448bf3SDimitry Andric std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
128139f7f9bSDimitry Andric PrefixKeyT prfkey(prf.begin(), prf.end());
129139f7f9bSDimitry Andric unsigned NewPrefix = CurPrefix + 1;
130139f7f9bSDimitry Andric if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
131139f7f9bSDimitry Andric Twine(NewPrefix)).str())).second)
132139f7f9bSDimitry Andric CurPrefix = NewPrefix;
133139f7f9bSDimitry Andric }
134139f7f9bSDimitry Andric
135139f7f9bSDimitry Andric // Dump prefixes.
136139f7f9bSDimitry Andric
137139f7f9bSDimitry Andric OS << "/////////\n";
138139f7f9bSDimitry Andric OS << "// Prefixes\n\n";
139139f7f9bSDimitry Andric OS << "#ifdef PREFIX\n";
140139f7f9bSDimitry Andric OS << "#define COMMA ,\n";
141139f7f9bSDimitry Andric for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
142139f7f9bSDimitry Andric I != E; ++I) {
143139f7f9bSDimitry Andric OS << "PREFIX(";
144139f7f9bSDimitry Andric
145139f7f9bSDimitry Andric // Prefix name.
146139f7f9bSDimitry Andric OS << I->second;
147139f7f9bSDimitry Andric
148139f7f9bSDimitry Andric // Prefix values.
149139f7f9bSDimitry Andric OS << ", {";
150139f7f9bSDimitry Andric for (PrefixKeyT::const_iterator PI = I->first.begin(),
151139f7f9bSDimitry Andric PE = I->first.end(); PI != PE; ++PI) {
152139f7f9bSDimitry Andric OS << "\"" << *PI << "\" COMMA ";
153139f7f9bSDimitry Andric }
1547d523365SDimitry Andric OS << "nullptr})\n";
155139f7f9bSDimitry Andric }
156139f7f9bSDimitry Andric OS << "#undef COMMA\n";
1577d523365SDimitry Andric OS << "#endif // PREFIX\n\n";
158139f7f9bSDimitry Andric
159139f7f9bSDimitry Andric OS << "/////////\n";
160139f7f9bSDimitry Andric OS << "// Groups\n\n";
161139f7f9bSDimitry Andric OS << "#ifdef OPTION\n";
162139f7f9bSDimitry Andric for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
163139f7f9bSDimitry Andric const Record &R = *Groups[i];
164139f7f9bSDimitry Andric
165139f7f9bSDimitry Andric // Start a single option entry.
166139f7f9bSDimitry Andric OS << "OPTION(";
167139f7f9bSDimitry Andric
168139f7f9bSDimitry Andric // The option prefix;
1697d523365SDimitry Andric OS << "nullptr";
170139f7f9bSDimitry Andric
171139f7f9bSDimitry Andric // The option string.
172139f7f9bSDimitry Andric OS << ", \"" << R.getValueAsString("Name") << '"';
173139f7f9bSDimitry Andric
174139f7f9bSDimitry Andric // The option identifier name.
175139f7f9bSDimitry Andric OS << ", "<< getOptionName(R);
176139f7f9bSDimitry Andric
177139f7f9bSDimitry Andric // The option kind.
178139f7f9bSDimitry Andric OS << ", Group";
179139f7f9bSDimitry Andric
180139f7f9bSDimitry Andric // The containing option group (if any).
181139f7f9bSDimitry Andric OS << ", ";
182139f7f9bSDimitry Andric if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
183139f7f9bSDimitry Andric OS << getOptionName(*DI->getDef());
184139f7f9bSDimitry Andric else
185139f7f9bSDimitry Andric OS << "INVALID";
186139f7f9bSDimitry Andric
187139f7f9bSDimitry Andric // The other option arguments (unused for groups).
1887d523365SDimitry Andric OS << ", INVALID, nullptr, 0, 0";
189139f7f9bSDimitry Andric
190139f7f9bSDimitry Andric // The option help text.
191139f7f9bSDimitry Andric if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
192139f7f9bSDimitry Andric OS << ",\n";
193139f7f9bSDimitry Andric OS << " ";
194139f7f9bSDimitry Andric write_cstring(OS, R.getValueAsString("HelpText"));
195139f7f9bSDimitry Andric } else
1967d523365SDimitry Andric OS << ", nullptr";
197139f7f9bSDimitry Andric
198139f7f9bSDimitry Andric // The option meta-variable name (unused).
199edd7eaddSDimitry Andric OS << ", nullptr";
200edd7eaddSDimitry Andric
201edd7eaddSDimitry Andric // The option Values (unused for groups).
2027d523365SDimitry Andric OS << ", nullptr)\n";
203139f7f9bSDimitry Andric }
204139f7f9bSDimitry Andric OS << "\n";
205139f7f9bSDimitry Andric
206139f7f9bSDimitry Andric OS << "//////////\n";
207139f7f9bSDimitry Andric OS << "// Options\n\n";
208139f7f9bSDimitry Andric for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
209139f7f9bSDimitry Andric const Record &R = *Opts[i];
210139f7f9bSDimitry Andric
211139f7f9bSDimitry Andric // Start a single option entry.
212139f7f9bSDimitry Andric OS << "OPTION(";
213139f7f9bSDimitry Andric
214139f7f9bSDimitry Andric // The option prefix;
215f9448bf3SDimitry Andric std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
216139f7f9bSDimitry Andric OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
217139f7f9bSDimitry Andric
218139f7f9bSDimitry Andric // The option string.
219139f7f9bSDimitry Andric write_cstring(OS, R.getValueAsString("Name"));
220139f7f9bSDimitry Andric
221139f7f9bSDimitry Andric // The option identifier name.
222139f7f9bSDimitry Andric OS << ", "<< getOptionName(R);
223139f7f9bSDimitry Andric
224139f7f9bSDimitry Andric // The option kind.
225139f7f9bSDimitry Andric OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
226139f7f9bSDimitry Andric
227139f7f9bSDimitry Andric // The containing option group (if any).
228139f7f9bSDimitry Andric OS << ", ";
22991bc56edSDimitry Andric const ListInit *GroupFlags = nullptr;
23091bc56edSDimitry Andric if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
23191bc56edSDimitry Andric GroupFlags = DI->getDef()->getValueAsListInit("Flags");
232139f7f9bSDimitry Andric OS << getOptionName(*DI->getDef());
23391bc56edSDimitry Andric } else
234139f7f9bSDimitry Andric OS << "INVALID";
235139f7f9bSDimitry Andric
236139f7f9bSDimitry Andric // The option alias (if any).
237139f7f9bSDimitry Andric OS << ", ";
238139f7f9bSDimitry Andric if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
239139f7f9bSDimitry Andric OS << getOptionName(*DI->getDef());
240139f7f9bSDimitry Andric else
241139f7f9bSDimitry Andric OS << "INVALID";
242139f7f9bSDimitry Andric
243f785676fSDimitry Andric // The option alias arguments (if any).
244f785676fSDimitry Andric // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
245f785676fSDimitry Andric // would become "foo\0bar\0". Note that the compiler adds an implicit
246f785676fSDimitry Andric // terminating \0 at the end.
247f785676fSDimitry Andric OS << ", ";
248f9448bf3SDimitry Andric std::vector<StringRef> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
249f785676fSDimitry Andric if (AliasArgs.size() == 0) {
2507d523365SDimitry Andric OS << "nullptr";
251f785676fSDimitry Andric } else {
252f785676fSDimitry Andric OS << "\"";
253f785676fSDimitry Andric for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
254f785676fSDimitry Andric OS << AliasArgs[i] << "\\0";
255f785676fSDimitry Andric OS << "\"";
256f785676fSDimitry Andric }
257f785676fSDimitry Andric
258139f7f9bSDimitry Andric // The option flags.
259139f7f9bSDimitry Andric OS << ", ";
26091bc56edSDimitry Andric int NumFlags = 0;
26191bc56edSDimitry Andric const ListInit *LI = R.getValueAsListInit("Flags");
26291bc56edSDimitry Andric for (Init *I : *LI)
26391bc56edSDimitry Andric OS << (NumFlags++ ? " | " : "")
26491bc56edSDimitry Andric << cast<DefInit>(I)->getDef()->getName();
26591bc56edSDimitry Andric if (GroupFlags) {
26691bc56edSDimitry Andric for (Init *I : *GroupFlags)
26791bc56edSDimitry Andric OS << (NumFlags++ ? " | " : "")
26891bc56edSDimitry Andric << cast<DefInit>(I)->getDef()->getName();
269139f7f9bSDimitry Andric }
27091bc56edSDimitry Andric if (NumFlags == 0)
27191bc56edSDimitry Andric OS << '0';
272139f7f9bSDimitry Andric
273139f7f9bSDimitry Andric // The option parameter field.
274139f7f9bSDimitry Andric OS << ", " << R.getValueAsInt("NumArgs");
275139f7f9bSDimitry Andric
276139f7f9bSDimitry Andric // The option help text.
277139f7f9bSDimitry Andric if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
278139f7f9bSDimitry Andric OS << ",\n";
279139f7f9bSDimitry Andric OS << " ";
280139f7f9bSDimitry Andric write_cstring(OS, R.getValueAsString("HelpText"));
281139f7f9bSDimitry Andric } else
2827d523365SDimitry Andric OS << ", nullptr";
283139f7f9bSDimitry Andric
284139f7f9bSDimitry Andric // The option meta-variable name.
285139f7f9bSDimitry Andric OS << ", ";
286139f7f9bSDimitry Andric if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
287139f7f9bSDimitry Andric write_cstring(OS, R.getValueAsString("MetaVarName"));
288139f7f9bSDimitry Andric else
2897d523365SDimitry Andric OS << "nullptr";
290139f7f9bSDimitry Andric
291edd7eaddSDimitry Andric // The option Values. Used for shell autocompletion.
292edd7eaddSDimitry Andric OS << ", ";
293edd7eaddSDimitry Andric if (!isa<UnsetInit>(R.getValueInit("Values")))
294edd7eaddSDimitry Andric write_cstring(OS, R.getValueAsString("Values"));
295edd7eaddSDimitry Andric else
296edd7eaddSDimitry Andric OS << "nullptr";
297edd7eaddSDimitry Andric
298139f7f9bSDimitry Andric OS << ")\n";
299139f7f9bSDimitry Andric }
3007d523365SDimitry Andric OS << "#endif // OPTION\n";
301*2cab237bSDimitry Andric
302*2cab237bSDimitry Andric OS << "\n";
303*2cab237bSDimitry Andric OS << "#ifdef OPTTABLE_ARG_INIT\n";
304*2cab237bSDimitry Andric OS << "//////////\n";
305*2cab237bSDimitry Andric OS << "// Option Values\n\n";
306*2cab237bSDimitry Andric for (unsigned I = 0, E = Opts.size(); I != E; ++I) {
307*2cab237bSDimitry Andric const Record &R = *Opts[I];
308*2cab237bSDimitry Andric if (isa<UnsetInit>(R.getValueInit("ValuesCode")))
309*2cab237bSDimitry Andric continue;
310*2cab237bSDimitry Andric OS << "{\n";
311*2cab237bSDimitry Andric OS << "bool ValuesWereAdded;\n";
312*2cab237bSDimitry Andric OS << R.getValueAsString("ValuesCode");
313*2cab237bSDimitry Andric OS << "\n";
314*2cab237bSDimitry Andric for (const std::string &Pref : R.getValueAsListOfStrings("Prefixes")) {
315*2cab237bSDimitry Andric OS << "ValuesWereAdded = Opt.addValues(";
316*2cab237bSDimitry Andric std::string S = (Pref + R.getValueAsString("Name")).str();
317*2cab237bSDimitry Andric write_cstring(OS, S);
318*2cab237bSDimitry Andric OS << ", Values);\n";
319*2cab237bSDimitry Andric OS << "(void)ValuesWereAdded;\n";
320*2cab237bSDimitry Andric OS << "assert(ValuesWereAdded && \"Couldn't add values to "
321*2cab237bSDimitry Andric "OptTable!\");\n";
322*2cab237bSDimitry Andric }
323*2cab237bSDimitry Andric OS << "}\n";
324*2cab237bSDimitry Andric }
325*2cab237bSDimitry Andric OS << "\n";
326*2cab237bSDimitry Andric OS << "#endif // OPTTABLE_ARG_INIT\n";
327139f7f9bSDimitry Andric }
328139f7f9bSDimitry Andric } // end namespace llvm
329