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 <cctype>
17 #include <cstring>
18 #include <map>
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 // FIXME: Mmake this take StringRefs instead of null terminated strings to
25 // simplify callers.
26 static int StrCmpOptionName(const char *A, const char *B) {
27   const char *X = A, *Y = B;
28   char a = tolower(*A), b = tolower(*B);
29   while (a == b) {
30     if (a == '\0')
31       return strcmp(A, B);
32 
33     a = tolower(*++X);
34     b = tolower(*++Y);
35   }
36 
37   if (a == '\0') // A is a prefix of B.
38     return 1;
39   if (b == '\0') // B is a prefix of A.
40     return -1;
41 
42   // Otherwise lexicographic.
43   return (a < b) ? -1 : 1;
44 }
45 
46 static int CompareOptionRecords(Record *const *Av, Record *const *Bv) {
47   const Record *A = *Av;
48   const Record *B = *Bv;
49 
50   // Sentinel options precede all others and are only ordered by precedence.
51   bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
52   bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
53   if (ASent != BSent)
54     return ASent ? -1 : 1;
55 
56   // Compare options by name, unless they are sentinels.
57   if (!ASent)
58     if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").str().c_str(),
59                                    B->getValueAsString("Name").str().c_str()))
60       return Cmp;
61 
62   if (!ASent) {
63     std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes");
64     std::vector<StringRef> BPrefixes = B->getValueAsListOfStrings("Prefixes");
65 
66     for (std::vector<StringRef>::const_iterator APre = APrefixes.begin(),
67                                                 AEPre = APrefixes.end(),
68                                                 BPre = BPrefixes.begin(),
69                                                 BEPre = BPrefixes.end();
70                                                 APre != AEPre &&
71                                                 BPre != BEPre;
72                                                 ++APre, ++BPre) {
73       if (int Cmp = StrCmpOptionName(APre->str().c_str(), BPre->str().c_str()))
74         return Cmp;
75     }
76   }
77 
78   // Then by the kind precedence;
79   int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
80   int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
81   if (APrec == BPrec &&
82       A->getValueAsListOfStrings("Prefixes") ==
83       B->getValueAsListOfStrings("Prefixes")) {
84     PrintError(A->getLoc(), Twine("Option is equivalent to"));
85     PrintError(B->getLoc(), Twine("Other defined here"));
86     PrintFatalError("Equivalent Options found.");
87   }
88   return APrec < BPrec ? -1 : 1;
89 }
90 
91 static const std::string getOptionName(const Record &R) {
92   // Use the record name unless EnumName is defined.
93   if (isa<UnsetInit>(R.getValueInit("EnumName")))
94     return R.getName();
95 
96   return R.getValueAsString("EnumName");
97 }
98 
99 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
100   OS << '"';
101   OS.write_escaped(Str);
102   OS << '"';
103   return OS;
104 }
105 
106 /// OptParserEmitter - This tablegen backend takes an input .td file
107 /// describing a list of options and emits a data structure for parsing and
108 /// working with those options when given an input command line.
109 namespace llvm {
110 void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
111   // Get the option groups and options.
112   const std::vector<Record*> &Groups =
113     Records.getAllDerivedDefinitions("OptionGroup");
114   std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
115 
116   emitSourceFileHeader("Option Parsing Definitions", OS);
117 
118   array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
119   // Generate prefix groups.
120   typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
121   typedef std::map<PrefixKeyT, std::string> PrefixesT;
122   PrefixesT Prefixes;
123   Prefixes.insert(std::make_pair(PrefixKeyT(), "prefix_0"));
124   unsigned CurPrefix = 0;
125   for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
126     const Record &R = *Opts[i];
127     std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
128     PrefixKeyT prfkey(prf.begin(), prf.end());
129     unsigned NewPrefix = CurPrefix + 1;
130     if (Prefixes.insert(std::make_pair(prfkey, (Twine("prefix_") +
131                                               Twine(NewPrefix)).str())).second)
132       CurPrefix = NewPrefix;
133   }
134 
135   // Dump prefixes.
136 
137   OS << "/////////\n";
138   OS << "// Prefixes\n\n";
139   OS << "#ifdef PREFIX\n";
140   OS << "#define COMMA ,\n";
141   for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
142                                   I != E; ++I) {
143     OS << "PREFIX(";
144 
145     // Prefix name.
146     OS << I->second;
147 
148     // Prefix values.
149     OS << ", {";
150     for (PrefixKeyT::const_iterator PI = I->first.begin(),
151                                     PE = I->first.end(); PI != PE; ++PI) {
152       OS << "\"" << *PI << "\" COMMA ";
153     }
154     OS << "nullptr})\n";
155   }
156   OS << "#undef COMMA\n";
157   OS << "#endif // PREFIX\n\n";
158 
159   OS << "/////////\n";
160   OS << "// Groups\n\n";
161   OS << "#ifdef OPTION\n";
162   for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
163     const Record &R = *Groups[i];
164 
165     // Start a single option entry.
166     OS << "OPTION(";
167 
168     // The option prefix;
169     OS << "nullptr";
170 
171     // The option string.
172     OS << ", \"" << R.getValueAsString("Name") << '"';
173 
174     // The option identifier name.
175     OS  << ", "<< getOptionName(R);
176 
177     // The option kind.
178     OS << ", Group";
179 
180     // The containing option group (if any).
181     OS << ", ";
182     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
183       OS << getOptionName(*DI->getDef());
184     else
185       OS << "INVALID";
186 
187     // The other option arguments (unused for groups).
188     OS << ", INVALID, nullptr, 0, 0";
189 
190     // The option help text.
191     if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
192       OS << ",\n";
193       OS << "       ";
194       write_cstring(OS, R.getValueAsString("HelpText"));
195     } else
196       OS << ", nullptr";
197 
198     // The option meta-variable name (unused).
199     OS << ", nullptr)\n";
200   }
201   OS << "\n";
202 
203   OS << "//////////\n";
204   OS << "// Options\n\n";
205   for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
206     const Record &R = *Opts[i];
207 
208     // Start a single option entry.
209     OS << "OPTION(";
210 
211     // The option prefix;
212     std::vector<StringRef> prf = R.getValueAsListOfStrings("Prefixes");
213     OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
214 
215     // The option string.
216     write_cstring(OS, R.getValueAsString("Name"));
217 
218     // The option identifier name.
219     OS  << ", "<< getOptionName(R);
220 
221     // The option kind.
222     OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
223 
224     // The containing option group (if any).
225     OS << ", ";
226     const ListInit *GroupFlags = nullptr;
227     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) {
228       GroupFlags = DI->getDef()->getValueAsListInit("Flags");
229       OS << getOptionName(*DI->getDef());
230     } else
231       OS << "INVALID";
232 
233     // The option alias (if any).
234     OS << ", ";
235     if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
236       OS << getOptionName(*DI->getDef());
237     else
238       OS << "INVALID";
239 
240     // The option alias arguments (if any).
241     // Emitted as a \0 separated list in a string, e.g. ["foo", "bar"]
242     // would become "foo\0bar\0". Note that the compiler adds an implicit
243     // terminating \0 at the end.
244     OS << ", ";
245     std::vector<StringRef> AliasArgs = R.getValueAsListOfStrings("AliasArgs");
246     if (AliasArgs.size() == 0) {
247       OS << "nullptr";
248     } else {
249       OS << "\"";
250       for (size_t i = 0, e = AliasArgs.size(); i != e; ++i)
251         OS << AliasArgs[i] << "\\0";
252       OS << "\"";
253     }
254 
255     // The option flags.
256     OS << ", ";
257     int NumFlags = 0;
258     const ListInit *LI = R.getValueAsListInit("Flags");
259     for (Init *I : *LI)
260       OS << (NumFlags++ ? " | " : "")
261          << cast<DefInit>(I)->getDef()->getName();
262     if (GroupFlags) {
263       for (Init *I : *GroupFlags)
264         OS << (NumFlags++ ? " | " : "")
265            << cast<DefInit>(I)->getDef()->getName();
266     }
267     if (NumFlags == 0)
268       OS << '0';
269 
270     // The option parameter field.
271     OS << ", " << R.getValueAsInt("NumArgs");
272 
273     // The option help text.
274     if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
275       OS << ",\n";
276       OS << "       ";
277       write_cstring(OS, R.getValueAsString("HelpText"));
278     } else
279       OS << ", nullptr";
280 
281     // The option meta-variable name.
282     OS << ", ";
283     if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
284       write_cstring(OS, R.getValueAsString("MetaVarName"));
285     else
286       OS << "nullptr";
287 
288     OS << ")\n";
289   }
290   OS << "#endif // OPTION\n";
291 }
292 } // end namespace llvm
293