blob: 674c89af9f992fcbc2a8168e553c2fc733817287 [file] [log] [blame]
Peter Collingbourne51d77772011-10-06 13:03:08 +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
Michael J. Spencerc6357102012-10-22 22:13:48 +000010#include "llvm/TableGen/Error.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000011#include "llvm/TableGen/Record.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000012#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000013#include "llvm/ADT/STLExtras.h"
Michael J. Spencerc6357102012-10-22 22:13:48 +000014#include "llvm/ADT/SmallString.h"
15#include "llvm/ADT/Twine.h"
16
17#include <map>
18
Peter Collingbourne51d77772011-10-06 13:03:08 +000019using namespace llvm;
20
21static int StrCmpOptionName(const char *A, const char *B) {
22 char a = *A, b = *B;
23 while (a == b) {
24 if (a == '\0')
25 return 0;
26
27 a = *++A;
28 b = *++B;
29 }
30
31 if (a == '\0') // A is a prefix of B.
32 return 1;
33 if (b == '\0') // B is a prefix of A.
34 return -1;
35
36 // Otherwise lexicographic.
37 return (a < b) ? -1 : 1;
38}
39
40static int CompareOptionRecords(const void *Av, const void *Bv) {
Roman Divacky31ba6132012-09-06 15:59:27 +000041 const Record *A = *(const Record*const*) Av;
42 const Record *B = *(const Record*const*) Bv;
Peter Collingbourne51d77772011-10-06 13:03:08 +000043
44 // Sentinel options precede all others and are only ordered by precedence.
45 bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");
46 bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");
47 if (ASent != BSent)
48 return ASent ? -1 : 1;
49
50 // Compare options by name, unless they are sentinels.
51 if (!ASent)
52 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
53 B->getValueAsString("Name").c_str()))
54 return Cmp;
55
Michael J. Spencerc6357102012-10-22 22:13:48 +000056 if (!ASent) {
57 std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
58 std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes");
59
60 for (std::vector<std::string>::const_iterator APre = APrefixes.begin(),
61 AEPre = APrefixes.end(),
62 BPre = BPrefixes.begin(),
63 BEPre = BPrefixes.end();
64 APre != AEPre &&
65 BPre != BEPre;
66 ++APre, ++BPre) {
67 if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str()))
68 return Cmp;
69 }
70 }
71
Peter Collingbourne51d77772011-10-06 13:03:08 +000072 // Then by the kind precedence;
73 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
74 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
Michael J. Spencerc6357102012-10-22 22:13:48 +000075 if (APrec == BPrec &&
76 A->getValueAsListOfStrings("Prefixes") ==
77 B->getValueAsListOfStrings("Prefixes")) {
78 PrintError(A->getLoc(), Twine("Option is equivilent to"));
79 PrintError(B->getLoc(), Twine("Other defined here"));
Joerg Sonnenberger38859ee2012-10-25 16:37:08 +000080 PrintFatalError("Equivalent Options found.");
Michael J. Spencerc6357102012-10-22 22:13:48 +000081 }
Peter Collingbourne51d77772011-10-06 13:03:08 +000082 return APrec < BPrec ? -1 : 1;
83}
84
85static const std::string getOptionName(const Record &R) {
86 // Use the record name unless EnumName is defined.
Sean Silva1ab46322012-10-10 20:25:43 +000087 if (isa<UnsetInit>(R.getValueInit("EnumName")))
Peter Collingbourne51d77772011-10-06 13:03:08 +000088 return R.getName();
89
90 return R.getValueAsString("EnumName");
91}
92
93static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
94 OS << '"';
95 OS.write_escaped(Str);
96 OS << '"';
97 return OS;
98}
99
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000100/// OptParserEmitter - This tablegen backend takes an input .td file
101/// describing a list of options and emits a data structure for parsing and
102/// working with those options when given an input command line.
103namespace clang {
104void EmitOptParser(RecordKeeper &Records, raw_ostream &OS, bool GenDefs) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000105 // Get the option groups and options.
106 const std::vector<Record*> &Groups =
107 Records.getAllDerivedDefinitions("OptionGroup");
108 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
109
110 if (GenDefs)
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000111 emitSourceFileHeader("Option Parsing Definitions", OS);
Peter Collingbourne51d77772011-10-06 13:03:08 +0000112 else
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000113 emitSourceFileHeader("Option Parsing Table", OS);
Peter Collingbourne51d77772011-10-06 13:03:08 +0000114
115 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
116 if (GenDefs) {
Michael J. Spencerc6357102012-10-22 22:13:48 +0000117 // 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 OS << "#ifndef PREFIX\n";
134 OS << "#error \"Define PREFIX prior to including this file!\"\n";
135 OS << "#endif\n\n";
136
137 // Dump prefixes.
138 OS << "/////////\n";
139 OS << "// Prefixes\n\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 << "0})\n";
155 }
156 OS << "#undef COMMA\n";
157 OS << "\n";
158
Peter Collingbourne51d77772011-10-06 13:03:08 +0000159 OS << "#ifndef OPTION\n";
160 OS << "#error \"Define OPTION prior to including this file!\"\n";
161 OS << "#endif\n\n";
162
163 OS << "/////////\n";
164 OS << "// Groups\n\n";
165 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
166 const Record &R = *Groups[i];
167
168 // Start a single option entry.
169 OS << "OPTION(";
170
Michael J. Spencerc6357102012-10-22 22:13:48 +0000171 // The option prefix;
172 OS << "0";
173
Peter Collingbourne51d77772011-10-06 13:03:08 +0000174 // The option string.
Michael J. Spencerc6357102012-10-22 22:13:48 +0000175 OS << ", \"" << R.getValueAsString("Name") << '"';
Peter Collingbourne51d77772011-10-06 13:03:08 +0000176
177 // The option identifier name.
178 OS << ", "<< getOptionName(R);
179
180 // The option kind.
181 OS << ", Group";
182
183 // The containing option group (if any).
184 OS << ", ";
Sean Silva1ab46322012-10-10 20:25:43 +0000185 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000186 OS << getOptionName(*DI->getDef());
187 else
188 OS << "INVALID";
189
190 // The other option arguments (unused for groups).
191 OS << ", INVALID, 0, 0";
192
193 // The option help text.
Sean Silva1ab46322012-10-10 20:25:43 +0000194 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000195 OS << ",\n";
196 OS << " ";
197 write_cstring(OS, R.getValueAsString("HelpText"));
198 } else
199 OS << ", 0";
200
201 // The option meta-variable name (unused).
202 OS << ", 0)\n";
203 }
204 OS << "\n";
205
206 OS << "//////////\n";
207 OS << "// Options\n\n";
208 for (unsigned i = 0, e = Opts.size(); i != e; ++i) {
209 const Record &R = *Opts[i];
210
211 // Start a single option entry.
212 OS << "OPTION(";
213
Michael J. Spencerc6357102012-10-22 22:13:48 +0000214 // The option prefix;
215 std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
216 OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
217
Peter Collingbourne51d77772011-10-06 13:03:08 +0000218 // The option string.
219 write_cstring(OS, R.getValueAsString("Name"));
220
221 // The option identifier name.
222 OS << ", "<< getOptionName(R);
223
224 // The option kind.
225 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
226
227 // The containing option group (if any).
228 OS << ", ";
Sean Silva1ab46322012-10-10 20:25:43 +0000229 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000230 OS << getOptionName(*DI->getDef());
231 else
232 OS << "INVALID";
233
234 // The option alias (if any).
235 OS << ", ";
Sean Silva1ab46322012-10-10 20:25:43 +0000236 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000237 OS << getOptionName(*DI->getDef());
238 else
239 OS << "INVALID";
240
241 // The option flags.
242 const ListInit *LI = R.getValueAsListInit("Flags");
243 if (LI->empty()) {
244 OS << ", 0";
245 } else {
246 OS << ", ";
247 for (unsigned i = 0, e = LI->size(); i != e; ++i) {
248 if (i)
249 OS << " | ";
Sean Silva1ab46322012-10-10 20:25:43 +0000250 OS << cast<DefInit>(LI->getElement(i))->getDef()->getName();
Peter Collingbourne51d77772011-10-06 13:03:08 +0000251 }
252 }
253
254 // The option parameter field.
255 OS << ", " << R.getValueAsInt("NumArgs");
256
257 // The option help text.
Sean Silva1ab46322012-10-10 20:25:43 +0000258 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000259 OS << ",\n";
260 OS << " ";
261 write_cstring(OS, R.getValueAsString("HelpText"));
262 } else
263 OS << ", 0";
264
265 // The option meta-variable name.
266 OS << ", ";
Sean Silva1ab46322012-10-10 20:25:43 +0000267 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
Peter Collingbourne51d77772011-10-06 13:03:08 +0000268 write_cstring(OS, R.getValueAsString("MetaVarName"));
269 else
270 OS << "0";
271
272 OS << ")\n";
273 }
274 }
275}
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000276} // end namespace clang