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) {
28 if (int N = strcasecmp(A, B))
33 return strncasecmp(A, B, I) < 0 ? -1 : 1;
34 return strncasecmp(A, B, J) <= 0 ? -1 : 1;
37 static int CompareOptionRecords(const void *Av, const void *Bv) {
38 const Record *A = *(const Record*const*) Av;
39 const Record *B = *(const Record*const*) Bv;
41 // Sentinel options precede all others and are only ordered by precedence.
42 bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
43 bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
45 return ASent ? -1 : 1;
47 // Compare options by name, unless they are sentinels.
49 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
50 B->getValueAsString("Name").c_str()))
54 std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
55 std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes");
57 for (std::vector<std::string>::const_iterator APre = APrefixes.begin(),
58 AEPre = APrefixes.end(),
59 BPre = BPrefixes.begin(),
60 BEPre = BPrefixes.end();
64 if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str()))
69 // Then by the kind precedence;
70 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
71 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
73 A->getValueAsListOfStrings("Prefixes") ==
74 B->getValueAsListOfStrings("Prefixes")) {
75 PrintError(A->getLoc(), Twine("Option is equivilent to"));
76 PrintError(B->getLoc(), Twine("Other defined here"));
77 PrintFatalError("Equivalent Options found.");
79 return APrec < BPrec ? -1 : 1;
82 static const std::string getOptionName(const Record &R) {
83 // Use the record name unless EnumName is defined.
84 if (isa<UnsetInit>(R.getValueInit("EnumName")))
87 return R.getValueAsString("EnumName");
90 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
92 OS.write_escaped(Str);
97 /// OptParserEmitter - This tablegen backend takes an input .td file
98 /// describing a list of options and emits a data structure for parsing and
99 /// working with those options when given an input command line.
101 void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
102 // Get the option groups and options.
103 const std::vector<Record*> &Groups =
104 Records.getAllDerivedDefinitions("OptionGroup");
105 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
107 emitSourceFileHeader("Option Parsing Definitions", OS);
109 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
110 // Generate prefix groups.
111 typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
112 typedef std::map<PrefixKeyT, std::string> PrefixesT;
114 Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
115 unsigned CurPrefix = 0;
116 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
117 const Record &R = *Opts[i];
118 std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
119 PrefixKeyT prfkey(prf.begin(), prf.end());
120 unsigned NewPrefix = CurPrefix + 1;
121 if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
122 Twine(NewPrefix)).str())).second)
123 CurPrefix = NewPrefix;
129 OS << "// Prefixes\n\n";
130 OS << "#ifdef PREFIX\n";
131 OS << "#define COMMA ,\n";
132 for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
141 for (PrefixKeyT::const_iterator PI = I->first.begin(),
142 PE = I->first.end(); PI != PE; ++PI) {
143 OS << "\"" << *PI << "\" COMMA ";
147 OS << "#undef COMMA\n";
151 OS << "// Groups\n\n";
152 OS << "#ifdef OPTION\n";
153 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
154 const Record &R = *Groups[i];
156 // Start a single option entry.
159 // The option prefix;
162 // The option string.
163 OS << ", \"" << R.getValueAsString("Name") << '"';
165 // The option identifier name.
166 OS << ", "<< getOptionName(R);
171 // The containing option group (if any).
173 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
174 OS << getOptionName(*DI->getDef());
178 // The other option arguments (unused for groups).
179 OS << ", INVALID, 0, 0, 0";
181 // The option help text.
182 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
185 write_cstring(OS, R.getValueAsString("HelpText"));
189 // The option meta-variable name (unused).
194 OS << "//////////\n";
195 OS << "// Options\n\n";
196 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
197 const Record &R = *Opts[i];
199 // Start a single option entry.
202 // The option prefix;
203 std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
204 OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
206 // The option string.
207 write_cstring(OS, R.getValueAsString("Name"));
209 // The option identifier name.
210 OS << ", "<< getOptionName(R);
213 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
215 // The containing option group (if any).
217 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
218 OS << getOptionName(*DI->getDef());
222 // The option alias (if any).
224 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
225 OS << getOptionName(*DI->getDef());
229 // The option alias arguments (if any).
230 // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
231 // would become "foo\0bar\0". Note that the compiler adds an implicit
232 // terminating \0 at the end.
234 std::vector<std::string> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
235 if (AliasArgs.size() == 0) {
239 for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
240 OS << AliasArgs[i] << "\\0";
245 const ListInit *LI = R.getValueAsListInit("Flags");
250 for (unsigned i = 0, e = LI->size(); i != e; ++i) {
253 OS << cast<DefInit>(LI->getElement(i))->getDef()->getName();
257 // The option parameter field.
258 OS << ", " << R.getValueAsInt("NumArgs");
260 // The option help text.
261 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
264 write_cstring(OS, R.getValueAsString("HelpText"));
268 // The option meta-variable name.
270 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
271 write_cstring(OS, R.getValueAsString("MetaVarName"));
279 } // end namespace llvm