Option parsing: support case-insensitive option matching.
[oota-llvm.git] / utils / TableGen / OptParserEmitter.cpp
1 //===- 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"
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"
16 #include <cstring>
17 #include <map>
18 #include <strings.h>
19
20 using namespace llvm;
21
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   size_t I = strlen(A);
26   size_t J = strlen(B);
27   if (I == J) {
28     if (int N = strcasecmp(A, B))
29       return N;
30     return strcmp(A, B);
31   }
32   if (I < J)
33     return strncasecmp(A, B, I) < 0 ? -1 : 1;
34   return strncasecmp(A, B, J) <= 0 ? -1 : 1;
35 }
36
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;
40
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");
44   if (ASent != BSent)
45     return ASent ? -1 : 1;
46
47   // Compare options by name, unless they are sentinels.
48   if (!ASent)
49     if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
50                                    B->getValueAsString("Name").c_str()))
51       return Cmp;
52
53   if (!ASent) {
54     std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
55     std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes");
56
57     for (std::vector<std::string>::const_iterator APre = APrefixes.begin(),
58                                                   AEPre = APrefixes.end(),
59                                                   BPre = BPrefixes.begin(),
60                                                   BEPre = BPrefixes.end();
61                                                   APre != AEPre &&
62                                                   BPre != BEPre;
63                                                   ++APre, ++BPre) {
64       if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str()))
65         return Cmp;
66     }
67   }
68
69   // Then by the kind precedence;
70   int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
71   int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
72   if (APrec == BPrec &&
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.");
78   }
79   return APrec < BPrec ? -1 : 1;
80 }
81
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")))
85     return R.getName();
86
87   return R.getValueAsString("EnumName");
88 }
89
90 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
91   OS << '"';
92   OS.write_escaped(Str);
93   OS << '"';
94   return OS;
95 }
96
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.
100 namespace llvm {
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");
106
107   emitSourceFileHeader("Option Parsing Definitions", OS);
108
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;
113   PrefixesT Prefixes;
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;
124   }
125
126   // Dump prefixes.
127
128   OS << "/////////\n";
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();
133                                   I != E; ++I) {
134     OS << "PREFIX(";
135
136     // Prefix name.
137     OS << I->second;
138
139     // Prefix values.
140     OS << ", {";
141     for (PrefixKeyT::const_iterator PI = I->first.begin(),
142                                     PE = I->first.end(); PI != PE; ++PI) {
143       OS << "\"" << *PI << "\" COMMA ";
144     }
145     OS << "0})\n";
146   }
147   OS << "#undef COMMA\n";
148   OS << "#endif\n\n";
149
150   OS << "/////////\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];
155
156     // Start a single option entry.
157     OS << "OPTION(";
158
159     // The option prefix;
160     OS << "0";
161
162     // The option string.
163     OS << ", \"" << R.getValueAsString("Name") << '"';
164
165     // The option identifier name.
166     OS  << ", "<< getOptionName(R);
167
168     // The option kind.
169     OS << ", Group";
170
171     // The containing option group (if any).
172     OS << ", ";
173     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
174       OS << getOptionName(*DI->getDef());
175     else
176       OS << "INVALID";
177
178     // The other option arguments (unused for groups).
179     OS << ", INVALID, 0, 0, 0";
180
181     // The option help text.
182     if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
183       OS << ",\n";
184       OS << "       ";
185       write_cstring(OS, R.getValueAsString("HelpText"));
186     } else
187       OS << ", 0";
188
189     // The option meta-variable name (unused).
190     OS << ", 0)\n";
191   }
192   OS << "\n";
193
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];
198
199     // Start a single option entry.
200     OS << "OPTION(";
201
202     // The option prefix;
203     std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
204     OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
205
206     // The option string.
207     write_cstring(OS, R.getValueAsString("Name"));
208
209     // The option identifier name.
210     OS  << ", "<< getOptionName(R);
211
212     // The option kind.
213     OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
214
215     // The containing option group (if any).
216     OS << ", ";
217     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
218       OS << getOptionName(*DI->getDef());
219     else
220       OS << "INVALID";
221
222     // The option alias (if any).
223     OS << ", ";
224     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
225       OS << getOptionName(*DI->getDef());
226     else
227       OS << "INVALID";
228
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.
233     OS << ", ";
234     std::vector<std::string> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
235     if (AliasArgs.size() == 0) {
236       OS << "0";
237     } else {
238       OS << "\"";
239       for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
240         OS << AliasArgs[i] << "\\0";
241       OS << "\"";
242     }
243
244     // The option flags.
245     const ListInit *LI = R.getValueAsListInit("Flags");
246     if (LI->empty()) {
247       OS << ", 0";
248     } else {
249       OS << ", ";
250       for (unsigned i = 0, e = LI->size(); i != e; ++i) {
251         if (i)
252           OS << " | ";
253         OS << cast<DefInit>(LI->getElement(i))->getDef()->getName();
254       }
255     }
256
257     // The option parameter field.
258     OS << ", " << R.getValueAsInt("NumArgs");
259
260     // The option help text.
261     if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
262       OS << ",\n";
263       OS << "       ";
264       write_cstring(OS, R.getValueAsString("HelpText"));
265     } else
266       OS << ", 0";
267
268     // The option meta-variable name.
269     OS << ", ";
270     if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
271       write_cstring(OS, R.getValueAsString("MetaVarName"));
272     else
273       OS << "0";
274
275     OS << ")\n";
276   }
277   OS << "#endif\n";
278 }
279 } // end namespace llvm