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 "llvm/TableGen/Error.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 // Ordering on Info. The logic should match with the consumer-side function in
22 // llvm/Option/OptTable.h.
23 // FIXME: Mmake this take StringRefs instead of null terminated strings to
25 static int StrCmpOptionName(const char *A
, const char *B
) {
26 const char *X
= A
, *Y
= B
;
27 char a
= tolower(*A
), b
= tolower(*B
);
36 if (a
== '\0') // A is a prefix of B.
38 if (b
== '\0') // B is a prefix of A.
41 // Otherwise lexicographic.
42 return (a
< b
) ? -1 : 1;
45 static int CompareOptionRecords(Record
*const *Av
, Record
*const *Bv
) {
46 const Record
*A
= *Av
;
47 const Record
*B
= *Bv
;
49 // Sentinel options precede all others and are only ordered by precedence.
50 bool ASent
= A
->getValueAsDef("Kind")->getValueAsBit("Sentinel");
51 bool BSent
= B
->getValueAsDef("Kind")->getValueAsBit("Sentinel");
53 return ASent
? -1 : 1;
55 // Compare options by name, unless they are sentinels.
57 if (int Cmp
= StrCmpOptionName(A
->getValueAsString("Name").str().c_str(),
58 B
->getValueAsString("Name").str().c_str()))
62 std::vector
<StringRef
> APrefixes
= A
->getValueAsListOfStrings("Prefixes");
63 std::vector
<StringRef
> BPrefixes
= B
->getValueAsListOfStrings("Prefixes");
65 for (std::vector
<StringRef
>::const_iterator APre
= APrefixes
.begin(),
66 AEPre
= APrefixes
.end(),
67 BPre
= BPrefixes
.begin(),
68 BEPre
= BPrefixes
.end();
72 if (int Cmp
= StrCmpOptionName(APre
->str().c_str(), BPre
->str().c_str()))
77 // Then by the kind precedence;
78 int APrec
= A
->getValueAsDef("Kind")->getValueAsInt("Precedence");
79 int BPrec
= B
->getValueAsDef("Kind")->getValueAsInt("Precedence");
81 A
->getValueAsListOfStrings("Prefixes") ==
82 B
->getValueAsListOfStrings("Prefixes")) {
83 PrintError(A
->getLoc(), Twine("Option is equivalent to"));
84 PrintError(B
->getLoc(), Twine("Other defined here"));
85 PrintFatalError("Equivalent Options found.");
87 return APrec
< BPrec
? -1 : 1;
90 static const std::string
getOptionName(const Record
&R
) {
91 // Use the record name unless EnumName is defined.
92 if (isa
<UnsetInit
>(R
.getValueInit("EnumName")))
95 return R
.getValueAsString("EnumName");
98 static raw_ostream
&write_cstring(raw_ostream
&OS
, llvm::StringRef Str
) {
100 OS
.write_escaped(Str
);
105 /// OptParserEmitter - This tablegen backend takes an input .td file
106 /// describing a list of options and emits a data structure for parsing and
107 /// working with those options when given an input command line.
109 void EmitOptParser(RecordKeeper
&Records
, raw_ostream
&OS
) {
110 // Get the option groups and options.
111 const std::vector
<Record
*> &Groups
=
112 Records
.getAllDerivedDefinitions("OptionGroup");
113 std::vector
<Record
*> Opts
= Records
.getAllDerivedDefinitions("Option");
115 emitSourceFileHeader("Option Parsing Definitions", OS
);
117 array_pod_sort(Opts
.begin(), Opts
.end(), CompareOptionRecords
);
118 // Generate prefix groups.
119 typedef SmallVector
<SmallString
<2>, 2> PrefixKeyT
;
120 typedef std::map
<PrefixKeyT
, std::string
> PrefixesT
;
122 Prefixes
.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
123 unsigned CurPrefix
= 0;
124 for (unsigned i
= 0, e
= Opts
.size(); i
!= e
; ++i
) {
125 const Record
&R
= *Opts
[i
];
126 std::vector
<StringRef
> prf
= R
.getValueAsListOfStrings("Prefixes");
127 PrefixKeyT
prfkey(prf
.begin(), prf
.end());
128 unsigned NewPrefix
= CurPrefix
+ 1;
129 if (Prefixes
.insert(std::make_pair(prfkey
, (Twine("prefix_") +
130 Twine(NewPrefix
)).str())).second
)
131 CurPrefix
= NewPrefix
;
137 OS
<< "// Prefixes\n\n";
138 OS
<< "#ifdef PREFIX\n";
139 OS
<< "#define COMMA ,\n";
140 for (PrefixesT::const_iterator I
= Prefixes
.begin(), E
= Prefixes
.end();
149 for (PrefixKeyT::const_iterator PI
= I
->first
.begin(),
150 PE
= I
->first
.end(); PI
!= PE
; ++PI
) {
151 OS
<< "\"" << *PI
<< "\" COMMA ";
155 OS
<< "#undef COMMA\n";
156 OS
<< "#endif // PREFIX\n\n";
159 OS
<< "// Groups\n\n";
160 OS
<< "#ifdef OPTION\n";
161 for (unsigned i
= 0, e
= Groups
.size(); i
!= e
; ++i
) {
162 const Record
&R
= *Groups
[i
];
164 // Start a single option entry.
167 // The option prefix;
170 // The option string.
171 OS
<< ", \"" << R
.getValueAsString("Name") << '"';
173 // The option identifier name.
174 OS
<< ", "<< getOptionName(R
);
179 // The containing option group (if any).
181 if (const DefInit
*DI
= dyn_cast
<DefInit
>(R
.getValueInit("Group")))
182 OS
<< getOptionName(*DI
->getDef());
186 // The other option arguments (unused for groups).
187 OS
<< ", INVALID, nullptr, 0, 0";
189 // The option help text.
190 if (!isa
<UnsetInit
>(R
.getValueInit("HelpText"))) {
193 write_cstring(OS
, R
.getValueAsString("HelpText"));
197 // The option meta-variable name (unused).
200 // The option Values (unused for groups).
201 OS
<< ", nullptr)\n";
205 OS
<< "//////////\n";
206 OS
<< "// Options\n\n";
207 for (unsigned i
= 0, e
= Opts
.size(); i
!= e
; ++i
) {
208 const Record
&R
= *Opts
[i
];
210 // Start a single option entry.
213 // The option prefix;
214 std::vector
<StringRef
> prf
= R
.getValueAsListOfStrings("Prefixes");
215 OS
<< Prefixes
[PrefixKeyT(prf
.begin(), prf
.end())] << ", ";
217 // The option string.
218 write_cstring(OS
, R
.getValueAsString("Name"));
220 // The option identifier name.
221 OS
<< ", "<< getOptionName(R
);
224 OS
<< ", " << R
.getValueAsDef("Kind")->getValueAsString("Name");
226 // The containing option group (if any).
228 const ListInit
*GroupFlags
= nullptr;
229 if (const DefInit
*DI
= dyn_cast
<DefInit
>(R
.getValueInit("Group"))) {
230 GroupFlags
= DI
->getDef()->getValueAsListInit("Flags");
231 OS
<< getOptionName(*DI
->getDef());
235 // The option alias (if any).
237 if (const DefInit
*DI
= dyn_cast
<DefInit
>(R
.getValueInit("Alias")))
238 OS
<< getOptionName(*DI
->getDef());
242 // The option alias arguments (if any).
243 // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
244 // would become "foo\0bar\0". Note that the compiler adds an implicit
245 // terminating \0 at the end.
247 std::vector
<StringRef
> AliasArgs
= R
.getValueAsListOfStrings("AliasArgs");
248 if (AliasArgs
.size() == 0) {
252 for (size_t i
= 0, e
= AliasArgs
.size(); i
!= e
; ++i
)
253 OS
<< AliasArgs
[i
] << "\\0";
260 const ListInit
*LI
= R
.getValueAsListInit("Flags");
262 OS
<< (NumFlags
++ ? " | " : "")
263 << cast
<DefInit
>(I
)->getDef()->getName();
265 for (Init
*I
: *GroupFlags
)
266 OS
<< (NumFlags
++ ? " | " : "")
267 << cast
<DefInit
>(I
)->getDef()->getName();
272 // The option parameter field.
273 OS
<< ", " << R
.getValueAsInt("NumArgs");
275 // The option help text.
276 if (!isa
<UnsetInit
>(R
.getValueInit("HelpText"))) {
279 write_cstring(OS
, R
.getValueAsString("HelpText"));
283 // The option meta-variable name.
285 if (!isa
<UnsetInit
>(R
.getValueInit("MetaVarName")))
286 write_cstring(OS
, R
.getValueAsString("MetaVarName"));
290 // The option Values. Used for shell autocompletion.
292 if (!isa
<UnsetInit
>(R
.getValueInit("Values")))
293 write_cstring(OS
, R
.getValueAsString("Values"));
299 OS
<< "#endif // OPTION\n";
302 OS
<< "#ifdef OPTTABLE_ARG_INIT\n";
303 OS
<< "//////////\n";
304 OS
<< "// Option Values\n\n";
305 for (unsigned I
= 0, E
= Opts
.size(); I
!= E
; ++I
) {
306 const Record
&R
= *Opts
[I
];
307 if (isa
<UnsetInit
>(R
.getValueInit("ValuesCode")))
310 OS
<< "bool ValuesWereAdded;\n";
311 OS
<< R
.getValueAsString("ValuesCode");
313 for (const std::string
&Pref
: R
.getValueAsListOfStrings("Prefixes")) {
314 OS
<< "ValuesWereAdded = Opt.addValues(";
315 std::string S
= (Pref
+ R
.getValueAsString("Name")).str();
316 write_cstring(OS
, S
);
317 OS
<< ", Values);\n";
318 OS
<< "(void)ValuesWereAdded;\n";
319 OS
<< "assert(ValuesWereAdded && \"Couldn't add values to "
325 OS
<< "#endif // OPTTABLE_ARG_INIT\n";
327 } // end namespace llvm