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/TableGen/Record.h"
12 #include "llvm/TableGen/TableGenBackend.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
21 static int StrCmpOptionName(const char *A, const char *B) {
31 if (a == '\0') // A is a prefix of B.
33 if (b == '\0') // B is a prefix of A.
36 // Otherwise lexicographic.
37 return (a < b) ? -1 : 1;
40 static int CompareOptionRecords(const void *Av, const void *Bv) {
41 const Record *A = *(const Record*const*) Av;
42 const Record *B = *(const Record*const*) Bv;
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");
48 return ASent ? -1 : 1;
50 // Compare options by name, unless they are sentinels.
52 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(),
53 B->getValueAsString("Name").c_str()))
57 std::vector<std::string> APrefixes = A->getValueAsListOfStrings("Prefixes");
58 std::vector<std::string> BPrefixes = B->getValueAsListOfStrings("Prefixes");
60 for (std::vector<std::string>::const_iterator APre = APrefixes.begin(),
61 AEPre = APrefixes.end(),
62 BPre = BPrefixes.begin(),
63 BEPre = BPrefixes.end();
67 if (int Cmp = StrCmpOptionName(APre->c_str(), BPre->c_str()))
72 // Then by the kind precedence;
73 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");
74 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");
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"));
80 PrintFatalError("Equivalent Options found.");
82 return APrec < BPrec ? -1 : 1;
85 static const std::string getOptionName(const Record &R) {
86 // Use the record name unless EnumName is defined.
87 if (isa<UnsetInit>(R.getValueInit("EnumName")))
90 return R.getValueAsString("EnumName");
93 static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) {
95 OS.write_escaped(Str);
100 /// 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.
104 void EmitOptParser(RecordKeeper &Records, raw_ostream &OS, bool GenDefs) {
105 // Get the option groups and options.
106 const std::vector<Record*> &Groups =
107 Records.getAllDerivedDefinitions("OptionGroup");
108 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option");
111 emitSourceFileHeader("Option Parsing Definitions", OS);
113 emitSourceFileHeader("Option Parsing Table", OS);
115 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords);
117 // Generate prefix groups.
118 typedef SmallVector<SmallString<2>, 2> PrefixKeyT;
119 typedef std::map<PrefixKeyT, std::string> PrefixesT;
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;
133 OS << "#ifndef PREFIX\n";
134 OS << "#error \"Define PREFIX prior to including this file!\"\n";
139 OS << "// Prefixes\n\n";
140 OS << "#define COMMA ,\n";
141 for (PrefixesT::const_iterator I = Prefixes.begin(), E = Prefixes.end();
150 for (PrefixKeyT::const_iterator PI = I->first.begin(),
151 PE = I->first.end(); PI != PE; ++PI) {
152 OS << "\"" << *PI << "\" COMMA ";
156 OS << "#undef COMMA\n";
159 OS << "#ifndef OPTION\n";
160 OS << "#error \"Define OPTION prior to including this file!\"\n";
164 OS << "// Groups\n\n";
165 for (unsigned i = 0, e = Groups.size(); i != e; ++i) {
166 const Record &R = *Groups[i];
168 // Start a single option entry.
171 // The option prefix;
174 // The option string.
175 OS << ", \"" << R.getValueAsString("Name") << '"';
177 // The option identifier name.
178 OS << ", "<< getOptionName(R);
183 // The containing option group (if any).
185 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
186 OS << getOptionName(*DI->getDef());
190 // The other option arguments (unused for groups).
191 OS << ", INVALID, 0, 0";
193 // The option help text.
194 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
197 write_cstring(OS, R.getValueAsString("HelpText"));
201 // The option meta-variable name (unused).
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];
211 // Start a single option entry.
214 // The option prefix;
215 std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes");
216 OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", ";
218 // The option string.
219 write_cstring(OS, R.getValueAsString("Name"));
221 // The option identifier name.
222 OS << ", "<< getOptionName(R);
225 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name");
227 // The containing option group (if any).
229 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group")))
230 OS << getOptionName(*DI->getDef());
234 // The option alias (if any).
236 if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias")))
237 OS << getOptionName(*DI->getDef());
242 const ListInit *LI = R.getValueAsListInit("Flags");
247 for (unsigned i = 0, e = LI->size(); i != e; ++i) {
250 OS << cast<DefInit>(LI->getElement(i))->getDef()->getName();
254 // The option parameter field.
255 OS << ", " << R.getValueAsInt("NumArgs");
257 // The option help text.
258 if (!isa<UnsetInit>(R.getValueInit("HelpText"))) {
261 write_cstring(OS, R.getValueAsString("HelpText"));
265 // The option meta-variable name.
267 if (!isa<UnsetInit>(R.getValueInit("MetaVarName")))
268 write_cstring(OS, R.getValueAsString("MetaVarName"));
276 } // end namespace clang