1 //===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "OptEmitter.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/TableGen/Record.h"
14 #include "llvm/TableGen/TableGenBackend.h"
21 static const std::string getOptionName(const Record &R) {
22 // Use the record name unless EnumName is defined.
23 if (isa<UnsetInit>(R.getValueInit("EnumName")))
26 return R.getValueAsString("EnumName");
29 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
31 OS.write_escaped(Str);
36 /// OptParserEmitter - This tablegen backend takes an input .td file
37 /// describing a list of options and emits a data structure for parsing and
38 /// working with those options when given an input command line.
40 void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
41 // Get the option groups and options.
42 const std::vector<Record*> &Groups =
43 Records.getAllDerivedDefinitions("OptionGroup");
44 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
46 emitSourceFileHeader("Option Parsing Definitions", OS);
48 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
49 // Generate prefix groups.
50 typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
51 typedef std::map<PrefixKeyT, std::string> PrefixesT;
53 Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
54 unsigned CurPrefix = 0;
55 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
56 const Record &R = *Opts[i];
57 std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
58 PrefixKeyT prfkey(prf.begin(), prf.end());
59 unsigned NewPrefix = CurPrefix + 1;
60 if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
61 Twine(NewPrefix)).str())).second)
62 CurPrefix = NewPrefix;
68 OS << "// Prefixes\n\n";
69 OS << "#ifdef PREFIX\n";
70 OS << "#define COMMA ,\n";
71 for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
80 for (PrefixKeyT::const_iterator PI = I->first.begin(),
81 PE = I->first.end(); PI != PE; ++PI) {
82 OS << "\"" << *PI << "\" COMMA ";
86 OS << "#undef COMMA\n";
87 OS << "#endif // PREFIX\n\n";
90 OS << "// Groups\n\n";
91 OS << "#ifdef OPTION\n";
92 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
93 const Record &R = *Groups[i];
95 // Start a single option entry.
101 // The option string.
102 OS << ", \"" << R.getValueAsString("Name") << '"';
104 // The option identifier name.
105 OS << ", "<< getOptionName(R);
110 // The containing option group (if any).
112 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
113 OS << getOptionName(*DI->getDef());
117 // The other option arguments (unused for groups).
118 OS << ", INVALID, nullptr, 0, 0";
120 // The option help text.
121 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
124 write_cstring(OS, R.getValueAsString("HelpText"));
128 // The option meta-variable name (unused).
131 // The option Values (unused for groups).
132 OS << ", nullptr)\n";
136 OS << "//////////\n";
137 OS << "// Options\n\n";
138 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
139 const Record &R = *Opts[i];
141 // Start a single option entry.
144 // The option prefix;
145 std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
146 OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
148 // The option string.
149 write_cstring(OS, R.getValueAsString("Name"));
151 // The option identifier name.
152 OS << ", "<< getOptionName(R);
155 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
157 // The containing option group (if any).
159 const ListInit *GroupFlags = nullptr;
160 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
161 GroupFlags = DI->getDef()->getValueAsListInit("Flags");
162 OS << getOptionName(*DI->getDef());
166 // The option alias (if any).
168 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
169 OS << getOptionName(*DI->getDef());
173 // The option alias arguments (if any).
174 // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
175 // would become "foo\0bar\0". Note that the compiler adds an implicit
176 // terminating \0 at the end.
178 std::vector<StringRef> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
179 if (AliasArgs.size() == 0) {
183 for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
184 OS << AliasArgs[i] << "\\0";
191 const ListInit *LI = R.getValueAsListInit("Flags");
193 OS << (NumFlags++ ? " | " : "")
194 << cast<DefInit>(I)->getDef()->getName();
196 for (Init *I : *GroupFlags)
197 OS << (NumFlags++ ? " | " : "")
198 << cast<DefInit>(I)->getDef()->getName();
203 // The option parameter field.
204 OS << ", " << R.getValueAsInt("NumArgs");
206 // The option help text.
207 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
210 write_cstring(OS, R.getValueAsString("HelpText"));
214 // The option meta-variable name.
216 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
217 write_cstring(OS, R.getValueAsString("MetaVarName"));
221 // The option Values. Used for shell autocompletion.
223 if (!isa<UnsetInit>(R.getValueInit("Values")))
224 write_cstring(OS, R.getValueAsString("Values"));
230 OS << "#endif // OPTION\n";
233 OS << "#ifdef OPTTABLE_ARG_INIT\n";
234 OS << "//////////\n";
235 OS << "// Option Values\n\n";
236 for (unsigned I = 0, E = Opts.size(); I != E; ++I) {
237 const Record &R = *Opts[I];
238 if (isa<UnsetInit>(R.getValueInit("ValuesCode")))
241 OS << "bool ValuesWereAdded;\n";
242 OS << R.getValueAsString("ValuesCode");
244 for (std::string S : R.getValueAsListOfStrings("Prefixes")) {
245 OS << "ValuesWereAdded = Opt.addValues(";
246 S += R.getValueAsString("Name");
247 write_cstring(OS, S);
248 OS << ", Values);\n";
249 OS << "(void)ValuesWereAdded;\n";
250 OS << "assert(ValuesWereAdded && \"Couldn't add values to "
256 OS << "#endif // OPTTABLE_ARG_INIT\n";
258 } // end namespace llvm