Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 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 | |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 10 | #include "llvm/TableGen/Error.h" |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 11 | #include "llvm/TableGen/Record.h" |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 12 | #include "llvm/TableGen/TableGenBackend.h" |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/STLExtras.h" |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
| 15 | #include "llvm/ADT/Twine.h" |
| 16 | |
| 17 | #include <map> |
| 18 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | static 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 | |
| 40 | static int CompareOptionRecords(const void *Av, const void *Bv) { |
Roman Divacky | 31ba613 | 2012-09-06 15:59:27 +0000 | [diff] [blame] | 41 | const Record *A = *(const Record*const*) Av; |
| 42 | const Record *B = *(const Record*const*) Bv; |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 43 | |
| 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. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 56 | 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 Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 72 | // Then by the kind precedence; |
| 73 | int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence"); |
| 74 | int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence"); |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 75 | 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 Sonnenberger | 38859ee | 2012-10-25 16:37:08 +0000 | [diff] [blame^] | 80 | PrintFatalError("Equivalent Options found."); |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 81 | } |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 82 | return APrec < BPrec ? -1 : 1; |
| 83 | } |
| 84 | |
| 85 | static const std::string getOptionName(const Record &R) { |
| 86 | // Use the record name unless EnumName is defined. |
Sean Silva | 1ab4632 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 87 | if (isa<UnsetInit>(R.getValueInit("EnumName"))) |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 88 | return R.getName(); |
| 89 | |
| 90 | return R.getValueAsString("EnumName"); |
| 91 | } |
| 92 | |
| 93 | static 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 Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 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. |
| 103 | namespace clang { |
| 104 | void EmitOptParser(RecordKeeper &Records, raw_ostream &OS, bool GenDefs) { |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 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"); |
| 109 | |
| 110 | if (GenDefs) |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 111 | emitSourceFileHeader("Option Parsing Definitions", OS); |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 112 | else |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 113 | emitSourceFileHeader("Option Parsing Table", OS); |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 114 | |
| 115 | array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords); |
| 116 | if (GenDefs) { |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 117 | // 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 Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 159 | 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. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 171 | // The option prefix; |
| 172 | OS << "0"; |
| 173 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 174 | // The option string. |
Michael J. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 175 | OS << ", \"" << R.getValueAsString("Name") << '"'; |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 176 | |
| 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 Silva | 1ab4632 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 185 | if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 186 | 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 Silva | 1ab4632 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 194 | if (!isa<UnsetInit>(R.getValueInit("HelpText"))) { |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 195 | 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. Spencer | c635710 | 2012-10-22 22:13:48 +0000 | [diff] [blame] | 214 | // The option prefix; |
| 215 | std::vector<std::string> prf = R.getValueAsListOfStrings("Prefixes"); |
| 216 | OS << Prefixes[PrefixKeyT(prf.begin(), prf.end())] << ", "; |
| 217 | |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 218 | // 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 Silva | 1ab4632 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 229 | if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Group"))) |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 230 | OS << getOptionName(*DI->getDef()); |
| 231 | else |
| 232 | OS << "INVALID"; |
| 233 | |
| 234 | // The option alias (if any). |
| 235 | OS << ", "; |
Sean Silva | 1ab4632 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 236 | if (const DefInit *DI = dyn_cast<DefInit>(R.getValueInit("Alias"))) |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 237 | 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 Silva | 1ab4632 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 250 | OS << cast<DefInit>(LI->getElement(i))->getDef()->getName(); |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | // The option parameter field. |
| 255 | OS << ", " << R.getValueAsInt("NumArgs"); |
| 256 | |
| 257 | // The option help text. |
Sean Silva | 1ab4632 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 258 | if (!isa<UnsetInit>(R.getValueInit("HelpText"))) { |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 259 | 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 Silva | 1ab4632 | 2012-10-10 20:25:43 +0000 | [diff] [blame] | 267 | if (!isa<UnsetInit>(R.getValueInit("MetaVarName"))) |
Peter Collingbourne | 51d7777 | 2011-10-06 13:03:08 +0000 | [diff] [blame] | 268 | write_cstring(OS, R.getValueAsString("MetaVarName")); |
| 269 | else |
| 270 | OS << "0"; |
| 271 | |
| 272 | OS << ")\n"; |
| 273 | } |
| 274 | } |
| 275 | } |
Jakob Stoklund Olesen | 3cc509b | 2012-06-13 05:12:41 +0000 | [diff] [blame] | 276 | } // end namespace clang |