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 static int StrCmpOptionName(const char *A, const char *B) {
25 const char *X = A, *Y = B;
26 char a = tolower(*A), b = tolower(*B);
35 if (a == '\0') // A is a prefix of B.
37 if (b == '\0') // B is a prefix of A.
40 // Otherwise lexicographic.
41 return (a < b) ? -1 : 1;
44 static int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
45 const Record *A = *Av;
46 const Record *B = *Bv;
48 // Sentinel options precede all others and are only ordered by precedence.
49 bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
50 bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
52 return ASent ? -1 : 1;
54 // Compare options by name, unless they are sentinels.
56 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
57 B->getValueAsString("Name").c_str()))
61 std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
62 std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes");
64 for (std::vector<std::string>::const_iterator APre = APrefixes.begin(),
65 AEPre = APrefixes.end(),
66 BPre = BPrefixes.begin(),
67 BEPre = BPrefixes.end();
71 if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str()))
76 // Then by the kind precedence;
77 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
78 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
80 A->getValueAsListOfStrings("Prefixes") ==
81 B->getValueAsListOfStrings("Prefixes")) {
82 PrintError(A->getLoc(), Twine("Option is equivalent to"));
83 PrintError(B->getLoc(), Twine("Other defined here"));
84 PrintFatalError("Equivalent Options found.");
86 return APrec < BPrec ? -1 : 1;
89 static const std::string getOptionName(const Record &R) {
90 // Use the record name unless EnumName is defined.
91 if (isa<UnsetInit>(R.getValueInit("EnumName")))
94 return R.getValueAsString("EnumName");
97 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
99 OS.write_escaped(Str);
104 /// OptParserEmitter - This tablegen backend takes an input .td file
105 /// describing a list of options and emits a data structure for parsing and
106 /// working with those options when given an input command line.
108 void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
109 // Get the option groups and options.
110 const std::vector<Record*> &Groups =
111 Records.getAllDerivedDefinitions("OptionGroup");
112 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
114 emitSourceFileHeader("Option Parsing Definitions", OS);
116 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
117 // Generate prefix groups.
118 typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
119 typedef std::map<PrefixKeyT, std::string> PrefixesT;
121 Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
122 unsigned CurPrefix = 0;
123 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
124 const Record &R = *Opts[i];
125 std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
126 PrefixKeyT prfkey(prf.begin(), prf.end());
127 unsigned NewPrefix = CurPrefix + 1;
128 if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
129 Twine(NewPrefix)).str())).second)
130 CurPrefix = NewPrefix;
136 OS << "// Prefixes\n\n";
137 OS << "#ifdef PREFIX\n";
138 OS << "#define COMMA ,\n";
139 for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
148 for (PrefixKeyT::const_iterator PI = I->first.begin(),
149 PE = I->first.end(); PI != PE; ++PI) {
150 OS << "\"" << *PI << "\" COMMA ";
154 OS << "#undef COMMA\n";
158 OS << "// Groups\n\n";
159 OS << "#ifdef OPTION\n";
160 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
161 const Record &R = *Groups[i];
163 // Start a single option entry.
166 // The option prefix;
169 // The option string.
170 OS << ", \"" << R.getValueAsString("Name") << '"';
172 // The option identifier name.
173 OS << ", "<< getOptionName(R);
178 // The containing option group (if any).
180 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
181 OS << getOptionName(*DI->getDef());
185 // The other option arguments (unused for groups).
186 OS << ", INVALID, 0, 0, 0";
188 // The option help text.
189 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
192 write_cstring(OS, R.getValueAsString("HelpText"));
196 // The option meta-variable name (unused).
201 OS << "//////////\n";
202 OS << "// Options\n\n";
203 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
204 const Record &R = *Opts[i];
206 // Start a single option entry.
209 // The option prefix;
210 std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
211 OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
213 // The option string.
214 write_cstring(OS, R.getValueAsString("Name"));
216 // The option identifier name.
217 OS << ", "<< getOptionName(R);
220 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
222 // The containing option group (if any).
224 const ListInit *GroupFlags = nullptr;
225 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
226 GroupFlags = DI->getDef()->getValueAsListInit("Flags");
227 OS << getOptionName(*DI->getDef());
231 // The option alias (if any).
233 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
234 OS << getOptionName(*DI->getDef());
238 // The option alias arguments (if any).
239 // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
240 // would become "foo\0bar\0". Note that the compiler adds an implicit
241 // terminating \0 at the end.
243 std::vector<std::string> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
244 if (AliasArgs.size() == 0) {
248 for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
249 OS << AliasArgs[i] << "\\0";
256 const ListInit *LI = R.getValueAsListInit("Flags");
258 OS << (NumFlags++ ? " | " : "")
259 << cast<DefInit>(I)->getDef()->getName();
261 for (Init *I : *GroupFlags)
262 OS << (NumFlags++ ? " | " : "")
263 << cast<DefInit>(I)->getDef()->getName();
268 // The option parameter field.
269 OS << ", " << R.getValueAsInt("NumArgs");
271 // The option help text.
272 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
275 write_cstring(OS, R.getValueAsString("HelpText"));
279 // The option meta-variable name.
281 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
282 write_cstring(OS, R.getValueAsString("MetaVarName"));
290 } // end namespace llvm