blob: c1b5e6510325ce2308524869094da38a1ac3be4a [file] [log] [blame]
Michael J. Spencer41ee0412012-12-05 00:29:32 +00001//===- OptParserEmitter.cpp - Table Driven Command Line Parsing -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/TableGen/Error.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000011#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/Twine.h"
Chandler Carruthb034cb72013-01-02 10:26:28 +000014#include "llvm/TableGen/Record.h"
15#include "llvm/TableGen/TableGenBackend.h"
Rui Ueyama8fb5a912013-08-28 20:04:31 +000016#include <cctype>
Chandler Carruth442f7842014-03-04 10:07:28 +000017#include <cstring>
Michael J. Spencer41ee0412012-12-05 00:29:32 +000018#include <map>
19
20using namespace llvm;
21
Rui Ueyama8fb5a912013-08-28 20:04:31 +000022// Ordering on Info. The logic should match with the consumer-side function in
23// llvm/Option/OptTable.h.
Michael J. Spencer41ee0412012-12-05 00:29:32 +000024static int StrCmpOptionName(const char *A, const char *B) {
Rui Ueyama8fb5a912013-08-28 20:04:31 +000025 const char *X = A, *Y = B;
26 char a = tolower(*A), b = tolower(*B);
Rui Ueyamac3779ff2013-08-28 00:02:06 +000027 while (a == b) {
28 if (a == '\0')
Rui Ueyama8fb5a912013-08-28 20:04:31 +000029 return strcmp(A, B);
Rui Ueyamac3779ff2013-08-28 00:02:06 +000030
Rui Ueyama8fb5a912013-08-28 20:04:31 +000031 a = tolower(*++X);
32 b = tolower(*++Y);
Michael J. Spencer41ee0412012-12-05 00:29:32 +000033 }
Rui Ueyamac3779ff2013-08-28 00:02:06 +000034
35 if (a == '\0') // A is a prefix of B.
36 return 1;
37 if (b == '\0') // B is a prefix of A.
38 return -1;
39
40 // Otherwise lexicographic.
41 return (a < b) ? -1 : 1;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000042}
43
Benjamin Kramer8817cca2013-09-22 14:09:50 +000044static int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
45 const Record *A = *Av;
46 const Record *B = *Bv;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000047
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");
51 if (ASent != BSent)
52 return ASent ? -1 : 1;
53
54 // Compare options by name, unless they are sentinels.
55 if (!ASent)
56 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
57 B->getValueAsString("Name").c_str()))
Rui Ueyama8fb5a912013-08-28 20:04:31 +000058 return Cmp;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000059
60 if (!ASent) {
61 std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
62 std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes");
63
64 for (std::vector<std::string>::const_iterator APre = APrefixes.begin(),
65 AEPre = APrefixes.end(),
66 BPre = BPrefixes.begin(),
67 BEPre = BPrefixes.end();
68 APre != AEPre &&
69 BPre != BEPre;
70 ++APre, ++BPre) {
71 if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str()))
72 return Cmp;
73 }
74 }
75
76 // Then by the kind precedence;
77 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
78 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
79 if (APrec == BPrec &&
80 A->getValueAsListOfStrings("Prefixes") ==
81 B->getValueAsListOfStrings("Prefixes")) {
Peter Collingbournea5906f02013-10-20 03:19:25 +000082 PrintError(A->getLoc(), Twine("Option is equivalent to"));
Michael J. Spencer41ee0412012-12-05 00:29:32 +000083 PrintError(B->getLoc(), Twine("Other defined here"));
84 PrintFatalError("Equivalent Options found.");
85 }
86 return APrec < BPrec ? -1 : 1;
87}
88
89static const std::string getOptionName(const Record &R) {
90 // Use the record name unless EnumName is defined.
91 if (isa<UnsetInit>(R.getValueInit("EnumName")))
92 return R.getName();
93
94 return R.getValueAsString("EnumName");
95}
96
97static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
98 OS << '"';
99 OS.write_escaped(Str);
100 OS << '"';
101 return OS;
102}
103
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.
107namespace llvm {
108void 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");
113
114 emitSourceFileHeader("Option Parsing Definitions", OS);
115
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;
120 PrefixesT Prefixes;
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;
131 }
132
133 // Dump prefixes.
134
135 OS << "/////////\n";
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();
140 I != E; ++I) {
141 OS << "PREFIX(";
142
143 // Prefix name.
144 OS << I->second;
145
146 // Prefix values.
147 OS << ", {";
148 for (PrefixKeyT::const_iterator PI = I->first.begin(),
149 PE = I->first.end(); PI != PE; ++PI) {
150 OS << "\"" << *PI << "\" COMMA ";
151 }
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000152 OS << "nullptr})\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000153 }
154 OS << "#undef COMMA\n";
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000155 OS << "#endif // PREFIX\n\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000156
157 OS << "/////////\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];
162
163 // Start a single option entry.
Hans Wennborg8669b972013-07-31 23:28:51 +0000164 OS << "OPTION(";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000165
166 // The option prefix;
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000167 OS << "nullptr";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000168
169 // The option string.
170 OS << ", \"" << R.getValueAsString("Name") << '"';
171
172 // The option identifier name.
173 OS << ", "<< getOptionName(R);
174
175 // The option kind.
176 OS << ", Group";
177
178 // The containing option group (if any).
179 OS << ", ";
180 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
181 OS << getOptionName(*DI->getDef());
182 else
183 OS << "INVALID";
184
185 // The other option arguments (unused for groups).
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000186 OS << ", INVALID, nullptr, 0, 0";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000187
188 // The option help text.
189 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
190 OS << ",\n";
191 OS << " ";
192 write_cstring(OS, R.getValueAsString("HelpText"));
193 } else
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000194 OS << ", nullptr";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000195
196 // The option meta-variable name (unused).
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000197 OS << ", nullptr)\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000198 }
199 OS << "\n";
200
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];
205
206 // Start a single option entry.
Hans Wennborg8669b972013-07-31 23:28:51 +0000207 OS << "OPTION(";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000208
209 // The option prefix;
210 std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
211 OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
212
213 // The option string.
214 write_cstring(OS, R.getValueAsString("Name"));
215
216 // The option identifier name.
217 OS << ", "<< getOptionName(R);
218
219 // The option kind.
220 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
221
222 // The containing option group (if any).
223 OS << ", ";
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000224 const ListInit *GroupFlags = nullptr;
225 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
226 GroupFlags = DI->getDef()->getValueAsListInit("Flags");
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000227 OS << getOptionName(*DI->getDef());
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000228 } else
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000229 OS << "INVALID";
230
231 // The option alias (if any).
232 OS << ", ";
233 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
234 OS << getOptionName(*DI->getDef());
235 else
236 OS << "INVALID";
237
Hans Wennborg5fdcf862013-07-31 22:44:41 +0000238 // 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.
242 OS << ", ";
243 std::vector<std::string> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
244 if (AliasArgs.size() == 0) {
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000245 OS << "nullptr";
Hans Wennborg5fdcf862013-07-31 22:44:41 +0000246 } else {
247 OS << "\"";
248 for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
249 OS << AliasArgs[i] << "\\0";
250 OS << "\"";
251 }
252
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000253 // The option flags.
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000254 OS << ", ";
255 int NumFlags = 0;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000256 const ListInit *LI = R.getValueAsListInit("Flags");
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000257 for (Init *I : *LI)
258 OS << (NumFlags++ ? " | " : "")
259 << cast<DefInit>(I)->getDef()->getName();
260 if (GroupFlags) {
261 for (Init *I : *GroupFlags)
262 OS << (NumFlags++ ? " | " : "")
263 << cast<DefInit>(I)->getDef()->getName();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000264 }
Reid Kleckner9ef63b22014-07-12 00:18:58 +0000265 if (NumFlags == 0)
266 OS << '0';
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000267
268 // The option parameter field.
269 OS << ", " << R.getValueAsInt("NumArgs");
270
271 // The option help text.
272 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
273 OS << ",\n";
274 OS << " ";
275 write_cstring(OS, R.getValueAsString("HelpText"));
276 } else
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000277 OS << ", nullptr";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000278
279 // The option meta-variable name.
280 OS << ", ";
281 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
282 write_cstring(OS, R.getValueAsString("MetaVarName"));
283 else
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000284 OS << "nullptr";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000285
286 OS << ")\n";
287 }
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000288 OS << "#endif // OPTION\n";
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000289}
290} // end namespace llvm