1 //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/TableGen/Error.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/TableGen/Record.h"
15 #include "llvm/TableGen/TableGenBackend.h"
22 // Ordering on Info. The logic should match with the consumer-side function in
23 // llvm/Option/OptTable.h.
24 // FIXME: Mmake this take StringRefs instead of null terminated strings to
26 static int StrCmpOptionName(const char *A, const char *B) {
27 const char *X = A, *Y = B;
28 char a = tolower(*A), b = tolower(*B);
37 if (a == '\0') // A is a prefix of B.
39 if (b == '\0') // B is a prefix of A.
42 // Otherwise lexicographic.
43 return (a < b) ? -1 : 1;
46 static int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
47 const Record *A = *Av;
48 const Record *B = *Bv;
50 // Sentinel options precede all others and are only ordered by precedence.
51 bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
52 bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
54 return ASent ? -1 : 1;
56 // Compare options by name, unless they are sentinels.
58 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").str().c_str(),
59 B->getValueAsString("Name").str().c_str()))
63 std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes");
64 std::vector<StringRef> BPrefixes = B->getValueAsListOfStrings("Prefixes");
66 for (std::vector<StringRef>::const_iterator APre = APrefixes.begin(),
67 AEPre = APrefixes.end(),
68 BPre = BPrefixes.begin(),
69 BEPre = BPrefixes.end();
73 if (int Cmp = StrCmpOptionName(APre->str().c_str(), BPre->str().c_str()))
78 // Then by the kind precedence;
79 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
80 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
82 A->getValueAsListOfStrings("Prefixes") ==
83 B->getValueAsListOfStrings("Prefixes")) {
84 PrintError(A->getLoc(), Twine("Option is equivalent to"));
85 PrintError(B->getLoc(), Twine("Other defined here"));
86 PrintFatalError("Equivalent Options found.");
88 return APrec < BPrec ? -1 : 1;
91 static const std::string getOptionName(const Record &R) {
92 // Use the record name unless EnumName is defined.
93 if (isa<UnsetInit>(R.getValueInit("EnumName")))
96 return R.getValueAsString("EnumName");
99 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
101 OS.write_escaped(Str);
106 /// OptParserEmitter - This tablegen backend takes an input .td file
107 /// describing a list of options and emits a data structure for parsing and
108 /// working with those options when given an input command line.
110 void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
111 // Get the option groups and options.
112 const std::vector<Record*> &Groups =
113 Records.getAllDerivedDefinitions("OptionGroup");
114 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
116 emitSourceFileHeader("Option Parsing Definitions", OS);
118 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
119 // Generate prefix groups.
120 typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
121 typedef std::map<PrefixKeyT, std::string> PrefixesT;
123 Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
124 unsigned CurPrefix = 0;
125 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
126 const Record &R = *Opts[i];
127 std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
128 PrefixKeyT prfkey(prf.begin(), prf.end());
129 unsigned NewPrefix = CurPrefix + 1;
130 if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
131 Twine(NewPrefix)).str())).second)
132 CurPrefix = NewPrefix;
138 OS << "// Prefixes\n\n";
139 OS << "#ifdef PREFIX\n";
140 OS << "#define COMMA ,\n";
141 for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
150 for (PrefixKeyT::const_iterator PI = I->first.begin(),
151 PE = I->first.end(); PI != PE; ++PI) {
152 OS << "\"" << *PI << "\" COMMA ";
156 OS << "#undef COMMA\n";
157 OS << "#endif // PREFIX\n\n";
160 OS << "// Groups\n\n";
161 OS << "#ifdef OPTION\n";
162 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
163 const Record &R = *Groups[i];
165 // Start a single option entry.
168 // The option prefix;
171 // The option string.
172 OS << ", \"" << R.getValueAsString("Name") << '"';
174 // The option identifier name.
175 OS << ", "<< getOptionName(R);
180 // The containing option group (if any).
182 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
183 OS << getOptionName(*DI->getDef());
187 // The other option arguments (unused for groups).
188 OS << ", INVALID, nullptr, 0, 0";
190 // The option help text.
191 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
194 write_cstring(OS, R.getValueAsString("HelpText"));
198 // The option meta-variable name (unused).
201 // The option Values (unused for groups).
202 OS << ", nullptr)\n";
206 OS << "//////////\n";
207 OS << "// Options\n\n";
208 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
209 const Record &R = *Opts[i];
211 // Start a single option entry.
214 // The option prefix;
215 std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
216 OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
218 // The option string.
219 write_cstring(OS, R.getValueAsString("Name"));
221 // The option identifier name.
222 OS << ", "<< getOptionName(R);
225 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
227 // The containing option group (if any).
229 const ListInit *GroupFlags = nullptr;
230 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
231 GroupFlags = DI->getDef()->getValueAsListInit("Flags");
232 OS << getOptionName(*DI->getDef());
236 // The option alias (if any).
238 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
239 OS << getOptionName(*DI->getDef());
243 // The option alias arguments (if any).
244 // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
245 // would become "foo\0bar\0". Note that the compiler adds an implicit
246 // terminating \0 at the end.
248 std::vector<StringRef> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
249 if (AliasArgs.size() == 0) {
253 for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
254 OS << AliasArgs[i] << "\\0";
261 const ListInit *LI = R.getValueAsListInit("Flags");
263 OS << (NumFlags++ ? " | " : "")
264 << cast<DefInit>(I)->getDef()->getName();
266 for (Init *I : *GroupFlags)
267 OS << (NumFlags++ ? " | " : "")
268 << cast<DefInit>(I)->getDef()->getName();
273 // The option parameter field.
274 OS << ", " << R.getValueAsInt("NumArgs");
276 // The option help text.
277 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
280 write_cstring(OS, R.getValueAsString("HelpText"));
284 // The option meta-variable name.
286 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
287 write_cstring(OS, R.getValueAsString("MetaVarName"));
291 // The option Values. Used for shell autocompletion.
293 if (!isa<UnsetInit>(R.getValueInit("Values")))
294 write_cstring(OS, R.getValueAsString("Values"));
300 OS << "#endif // OPTION\n";
303 OS << "#ifdef OPTTABLE_ARG_INIT\n";
304 OS << "//////////\n";
305 OS << "// Option Values\n\n";
306 for (unsigned I = 0, E = Opts.size(); I != E; ++I) {
307 const Record &R = *Opts[I];
308 if (isa<UnsetInit>(R.getValueInit("ValuesCode")))
311 OS << "bool ValuesWereAdded;\n";
312 OS << R.getValueAsString("ValuesCode");
314 for (const std::string &Pref : R.getValueAsListOfStrings("Prefixes")) {
315 OS << "ValuesWereAdded = Opt.addValues(";
316 std::string S = (Pref + R.getValueAsString("Name")).str();
317 write_cstring(OS, S);
318 OS << ", Values);\n";
319 OS << "(void)ValuesWereAdded;\n";
320 OS << "assert(ValuesWereAdded && \"Couldn't add values to "
326 OS << "#endif // OPTTABLE_ARG_INIT\n";
328 } // end namespace llvm