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 "OptParserEmitter.h" 11#include "llvm/TableGen/Record.h" 12#include "llvm/ADT/STLExtras.h" 13using namespace llvm; 14 15static int StrCmpOptionName(const char *A, const char *B) { 16 char a = *A, b = *B; 17 while (a == b) { 18 if (a == '\0') 19 return 0; 20 21 a = *++A; 22 b = *++B; 23 } 24 25 if (a == '\0') // A is a prefix of B. 26 return 1; 27 if (b == '\0') // B is a prefix of A. 28 return -1; 29 30 // Otherwise lexicographic. 31 return (a < b) ? -1 : 1; 32} 33 34static int CompareOptionRecords(const void *Av, const void *Bv) { 35 const Record *A = *(Record**) Av; 36 const Record *B = *(Record**) Bv; 37 38 // Sentinel options precede all others and are only ordered by precedence. 39 bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel"); 40 bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel"); 41 if (ASent != BSent) 42 return ASent ? -1 : 1; 43 44 // Compare options by name, unless they are sentinels. 45 if (!ASent) 46 if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").c_str(), 47 B->getValueAsString("Name").c_str())) 48 return Cmp; 49 50 // Then by the kind precedence; 51 int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence"); 52 int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence"); 53 assert(APrec != BPrec && "Options are equivalent!"); 54 return APrec < BPrec ? -1 : 1; 55} 56 57static const std::string getOptionName(const Record &R) { 58 // Use the record name unless EnumName is defined. 59 if (dynamic_cast<UnsetInit*>(R.getValueInit("EnumName"))) 60 return R.getName(); 61 62 return R.getValueAsString("EnumName"); 63} 64 65static raw_ostream &write_cstring(raw_ostream &OS, llvm::StringRef Str) { 66 OS << '"'; 67 OS.write_escaped(Str); 68 OS << '"'; 69 return OS; 70} 71 72void OptParserEmitter::run(raw_ostream &OS) { 73 // Get the option groups and options. 74 const std::vector<Record*> &Groups = 75 Records.getAllDerivedDefinitions("OptionGroup"); 76 std::vector<Record*> Opts = Records.getAllDerivedDefinitions("Option"); 77 78 if (GenDefs) 79 EmitSourceFileHeader("Option Parsing Definitions", OS); 80 else 81 EmitSourceFileHeader("Option Parsing Table", OS); 82 83 array_pod_sort(Opts.begin(), Opts.end(), CompareOptionRecords); 84 if (GenDefs) { 85 OS << "#ifndef OPTION\n"; 86 OS << "#error \"Define OPTION prior to including this file!\"\n"; 87 OS << "#endif\n\n"; 88 89 OS << "/////////\n"; 90 OS << "// Groups\n\n"; 91 for (unsigned i = 0, e = Groups.size(); i != e; ++i) { 92 const Record &R = *Groups[i]; 93 94 // Start a single option entry. 95 OS << "OPTION("; 96 97 // The option string. 98 OS << '"' << R.getValueAsString("Name") << '"'; 99 100 // The option identifier name. 101 OS << ", "<< getOptionName(R); 102 103 // The option kind. 104 OS << ", Group"; 105 106 // The containing option group (if any). 107 OS << ", "; 108 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) 109 OS << getOptionName(*DI->getDef()); 110 else 111 OS << "INVALID"; 112 113 // The other option arguments (unused for groups). 114 OS << ", INVALID, 0, 0"; 115 116 // The option help text. 117 if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) { 118 OS << ",\n"; 119 OS << " "; 120 write_cstring(OS, R.getValueAsString("HelpText")); 121 } else 122 OS << ", 0"; 123 124 // The option meta-variable name (unused). 125 OS << ", 0)\n"; 126 } 127 OS << "\n"; 128 129 OS << "//////////\n"; 130 OS << "// Options\n\n"; 131 for (unsigned i = 0, e = Opts.size(); i != e; ++i) { 132 const Record &R = *Opts[i]; 133 134 // Start a single option entry. 135 OS << "OPTION("; 136 137 // The option string. 138 write_cstring(OS, R.getValueAsString("Name")); 139 140 // The option identifier name. 141 OS << ", "<< getOptionName(R); 142 143 // The option kind. 144 OS << ", " << R.getValueAsDef("Kind")->getValueAsString("Name"); 145 146 // The containing option group (if any). 147 OS << ", "; 148 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) 149 OS << getOptionName(*DI->getDef()); 150 else 151 OS << "INVALID"; 152 153 // The option alias (if any). 154 OS << ", "; 155 if (const DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Alias"))) 156 OS << getOptionName(*DI->getDef()); 157 else 158 OS << "INVALID"; 159 160 // The option flags. 161 const ListInit *LI = R.getValueAsListInit("Flags"); 162 if (LI->empty()) { 163 OS << ", 0"; 164 } else { 165 OS << ", "; 166 for (unsigned i = 0, e = LI->size(); i != e; ++i) { 167 if (i) 168 OS << " | "; 169 OS << dynamic_cast<DefInit*>(LI->getElement(i))->getDef()->getName(); 170 } 171 } 172 173 // The option parameter field. 174 OS << ", " << R.getValueAsInt("NumArgs"); 175 176 // The option help text. 177 if (!dynamic_cast<UnsetInit*>(R.getValueInit("HelpText"))) { 178 OS << ",\n"; 179 OS << " "; 180 write_cstring(OS, R.getValueAsString("HelpText")); 181 } else 182 OS << ", 0"; 183 184 // The option meta-variable name. 185 OS << ", "; 186 if (!dynamic_cast<UnsetInit*>(R.getValueInit("MetaVarName"))) 187 write_cstring(OS, R.getValueAsString("MetaVarName")); 188 else 189 OS << "0"; 190 191 OS << ")\n"; 192 } 193 } 194} 195