OptTable.cpp revision 280031
1//===--- OptTable.cpp - Option Table Implementation -----------------------===// 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/Option/OptTable.h" 11#include "llvm/Option/Arg.h" 12#include "llvm/Option/ArgList.h" 13#include "llvm/Option/Option.h" 14#include "llvm/Support/ErrorHandling.h" 15#include "llvm/Support/raw_ostream.h" 16#include <algorithm> 17#include <cctype> 18#include <map> 19 20using namespace llvm; 21using namespace llvm::opt; 22 23namespace llvm { 24namespace opt { 25 26// Ordering on Info. The ordering is *almost* case-insensitive lexicographic, 27// with an exceptions. '\0' comes at the end of the alphabet instead of the 28// beginning (thus options precede any other options which prefix them). 29static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) { 30 const char *X = A, *Y = B; 31 char a = tolower(*A), b = tolower(*B); 32 while (a == b) { 33 if (a == '\0') 34 return 0; 35 36 a = tolower(*++X); 37 b = tolower(*++Y); 38 } 39 40 if (a == '\0') // A is a prefix of B. 41 return 1; 42 if (b == '\0') // B is a prefix of A. 43 return -1; 44 45 // Otherwise lexicographic. 46 return (a < b) ? -1 : 1; 47} 48 49#ifndef NDEBUG 50static int StrCmpOptionName(const char *A, const char *B) { 51 if (int N = StrCmpOptionNameIgnoreCase(A, B)) 52 return N; 53 return strcmp(A, B); 54} 55 56static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { 57 if (&A == &B) 58 return false; 59 60 if (int N = StrCmpOptionName(A.Name, B.Name)) 61 return N < 0; 62 63 for (const char * const *APre = A.Prefixes, 64 * const *BPre = B.Prefixes; 65 *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){ 66 if (int N = StrCmpOptionName(*APre, *BPre)) 67 return N < 0; 68 } 69 70 // Names are the same, check that classes are in order; exactly one 71 // should be joined, and it should succeed the other. 72 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) && 73 "Unexpected classes for options with same name."); 74 return B.Kind == Option::JoinedClass; 75} 76#endif 77 78// Support lower_bound between info and an option name. 79static inline bool operator<(const OptTable::Info &I, const char *Name) { 80 return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0; 81} 82} 83} 84 85OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {} 86 87OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos, 88 bool _IgnoreCase) 89 : OptionInfos(_OptionInfos), 90 NumOptionInfos(_NumOptionInfos), 91 IgnoreCase(_IgnoreCase), 92 TheInputOptionID(0), 93 TheUnknownOptionID(0), 94 FirstSearchableIndex(0) 95{ 96 // Explicitly zero initialize the error to work around a bug in array 97 // value-initialization on MinGW with gcc 4.3.5. 98 99 // Find start of normal options. 100 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { 101 unsigned Kind = getInfo(i + 1).Kind; 102 if (Kind == Option::InputClass) { 103 assert(!TheInputOptionID && "Cannot have multiple input options!"); 104 TheInputOptionID = getInfo(i + 1).ID; 105 } else if (Kind == Option::UnknownClass) { 106 assert(!TheUnknownOptionID && "Cannot have multiple unknown options!"); 107 TheUnknownOptionID = getInfo(i + 1).ID; 108 } else if (Kind != Option::GroupClass) { 109 FirstSearchableIndex = i; 110 break; 111 } 112 } 113 assert(FirstSearchableIndex != 0 && "No searchable options?"); 114 115#ifndef NDEBUG 116 // Check that everything after the first searchable option is a 117 // regular option class. 118 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) { 119 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind; 120 assert((Kind != Option::InputClass && Kind != Option::UnknownClass && 121 Kind != Option::GroupClass) && 122 "Special options should be defined first!"); 123 } 124 125 // Check that options are in order. 126 for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){ 127 if (!(getInfo(i) < getInfo(i + 1))) { 128 getOption(i).dump(); 129 getOption(i + 1).dump(); 130 llvm_unreachable("Options are not in order!"); 131 } 132 } 133#endif 134 135 // Build prefixes. 136 for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1; 137 i != e; ++i) { 138 if (const char *const *P = getInfo(i).Prefixes) { 139 for (; *P != nullptr; ++P) { 140 PrefixesUnion.insert(*P); 141 } 142 } 143 } 144 145 // Build prefix chars. 146 for (llvm::StringSet<>::const_iterator I = PrefixesUnion.begin(), 147 E = PrefixesUnion.end(); I != E; ++I) { 148 StringRef Prefix = I->getKey(); 149 for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end(); 150 C != CE; ++C) 151 if (std::find(PrefixChars.begin(), PrefixChars.end(), *C) 152 == PrefixChars.end()) 153 PrefixChars.push_back(*C); 154 } 155} 156 157OptTable::~OptTable() { 158} 159 160const Option OptTable::getOption(OptSpecifier Opt) const { 161 unsigned id = Opt.getID(); 162 if (id == 0) 163 return Option(nullptr, nullptr); 164 assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID."); 165 return Option(&getInfo(id), this); 166} 167 168static bool isInput(const llvm::StringSet<> &Prefixes, StringRef Arg) { 169 if (Arg == "-") 170 return true; 171 for (llvm::StringSet<>::const_iterator I = Prefixes.begin(), 172 E = Prefixes.end(); I != E; ++I) 173 if (Arg.startswith(I->getKey())) 174 return false; 175 return true; 176} 177 178/// \returns Matched size. 0 means no match. 179static unsigned matchOption(const OptTable::Info *I, StringRef Str, 180 bool IgnoreCase) { 181 for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) { 182 StringRef Prefix(*Pre); 183 if (Str.startswith(Prefix)) { 184 StringRef Rest = Str.substr(Prefix.size()); 185 bool Matched = IgnoreCase 186 ? Rest.startswith_lower(I->Name) 187 : Rest.startswith(I->Name); 188 if (Matched) 189 return Prefix.size() + StringRef(I->Name).size(); 190 } 191 } 192 return 0; 193} 194 195Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index, 196 unsigned FlagsToInclude, 197 unsigned FlagsToExclude) const { 198 unsigned Prev = Index; 199 const char *Str = Args.getArgString(Index); 200 201 // Anything that doesn't start with PrefixesUnion is an input, as is '-' 202 // itself. 203 if (isInput(PrefixesUnion, Str)) 204 return new Arg(getOption(TheInputOptionID), Str, Index++, Str); 205 206 const Info *Start = OptionInfos + FirstSearchableIndex; 207 const Info *End = OptionInfos + getNumOptions(); 208 StringRef Name = StringRef(Str).ltrim(PrefixChars); 209 210 // Search for the first next option which could be a prefix. 211 Start = std::lower_bound(Start, End, Name.data()); 212 213 // Options are stored in sorted order, with '\0' at the end of the 214 // alphabet. Since the only options which can accept a string must 215 // prefix it, we iteratively search for the next option which could 216 // be a prefix. 217 // 218 // FIXME: This is searching much more than necessary, but I am 219 // blanking on the simplest way to make it fast. We can solve this 220 // problem when we move to TableGen. 221 for (; Start != End; ++Start) { 222 unsigned ArgSize = 0; 223 // Scan for first option which is a proper prefix. 224 for (; Start != End; ++Start) 225 if ((ArgSize = matchOption(Start, Str, IgnoreCase))) 226 break; 227 if (Start == End) 228 break; 229 230 Option Opt(Start, this); 231 232 if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude)) 233 continue; 234 if (Opt.hasFlag(FlagsToExclude)) 235 continue; 236 237 // See if this option matches. 238 if (Arg *A = Opt.accept(Args, Index, ArgSize)) 239 return A; 240 241 // Otherwise, see if this argument was missing values. 242 if (Prev != Index) 243 return nullptr; 244 } 245 246 // If we failed to find an option and this arg started with /, then it's 247 // probably an input path. 248 if (Str[0] == '/') 249 return new Arg(getOption(TheInputOptionID), Str, Index++, Str); 250 251 return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str); 252} 253 254InputArgList *OptTable::ParseArgs(const char *const *ArgBegin, 255 const char *const *ArgEnd, 256 unsigned &MissingArgIndex, 257 unsigned &MissingArgCount, 258 unsigned FlagsToInclude, 259 unsigned FlagsToExclude) const { 260 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd); 261 262 // FIXME: Handle '@' args (or at least error on them). 263 264 MissingArgIndex = MissingArgCount = 0; 265 unsigned Index = 0, End = ArgEnd - ArgBegin; 266 while (Index < End) { 267 // Ingore nullptrs, they are response file's EOL markers 268 if (Args->getArgString(Index) == nullptr) { 269 ++Index; 270 continue; 271 } 272 // Ignore empty arguments (other things may still take them as arguments). 273 StringRef Str = Args->getArgString(Index); 274 if (Str == "") { 275 ++Index; 276 continue; 277 } 278 279 unsigned Prev = Index; 280 Arg *A = ParseOneArg(*Args, Index, FlagsToInclude, FlagsToExclude); 281 assert(Index > Prev && "Parser failed to consume argument."); 282 283 // Check for missing argument error. 284 if (!A) { 285 assert(Index >= End && "Unexpected parser error."); 286 assert(Index - Prev - 1 && "No missing arguments!"); 287 MissingArgIndex = Prev; 288 MissingArgCount = Index - Prev - 1; 289 break; 290 } 291 292 Args->append(A); 293 } 294 295 return Args; 296} 297 298static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) { 299 const Option O = Opts.getOption(Id); 300 std::string Name = O.getPrefixedName(); 301 302 // Add metavar, if used. 303 switch (O.getKind()) { 304 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: 305 llvm_unreachable("Invalid option with help text."); 306 307 case Option::MultiArgClass: 308 if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) { 309 // For MultiArgs, metavar is full list of all argument names. 310 Name += ' '; 311 Name += MetaVarName; 312 } 313 else { 314 // For MultiArgs<N>, if metavar not supplied, print <value> N times. 315 for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) { 316 Name += " <value>"; 317 } 318 } 319 break; 320 321 case Option::FlagClass: 322 break; 323 324 case Option::SeparateClass: case Option::JoinedOrSeparateClass: 325 case Option::RemainingArgsClass: 326 Name += ' '; 327 // FALLTHROUGH 328 case Option::JoinedClass: case Option::CommaJoinedClass: 329 case Option::JoinedAndSeparateClass: 330 if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) 331 Name += MetaVarName; 332 else 333 Name += "<value>"; 334 break; 335 } 336 337 return Name; 338} 339 340static void PrintHelpOptionList(raw_ostream &OS, StringRef Title, 341 std::vector<std::pair<std::string, 342 const char*> > &OptionHelp) { 343 OS << Title << ":\n"; 344 345 // Find the maximum option length. 346 unsigned OptionFieldWidth = 0; 347 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 348 // Skip titles. 349 if (!OptionHelp[i].second) 350 continue; 351 352 // Limit the amount of padding we are willing to give up for alignment. 353 unsigned Length = OptionHelp[i].first.size(); 354 if (Length <= 23) 355 OptionFieldWidth = std::max(OptionFieldWidth, Length); 356 } 357 358 const unsigned InitialPad = 2; 359 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 360 const std::string &Option = OptionHelp[i].first; 361 int Pad = OptionFieldWidth - int(Option.size()); 362 OS.indent(InitialPad) << Option; 363 364 // Break on long option names. 365 if (Pad < 0) { 366 OS << "\n"; 367 Pad = OptionFieldWidth + InitialPad; 368 } 369 OS.indent(Pad + 1) << OptionHelp[i].second << '\n'; 370 } 371} 372 373static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) { 374 unsigned GroupID = Opts.getOptionGroupID(Id); 375 376 // If not in a group, return the default help group. 377 if (!GroupID) 378 return "OPTIONS"; 379 380 // Abuse the help text of the option groups to store the "help group" 381 // name. 382 // 383 // FIXME: Split out option groups. 384 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID)) 385 return GroupHelp; 386 387 // Otherwise keep looking. 388 return getOptionHelpGroup(Opts, GroupID); 389} 390 391void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title, 392 bool ShowHidden) const { 393 PrintHelp(OS, Name, Title, /*Include*/ 0, /*Exclude*/ 394 (ShowHidden ? 0 : HelpHidden)); 395} 396 397 398void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title, 399 unsigned FlagsToInclude, 400 unsigned FlagsToExclude) const { 401 OS << "OVERVIEW: " << Title << "\n"; 402 OS << '\n'; 403 OS << "USAGE: " << Name << " [options] <inputs>\n"; 404 OS << '\n'; 405 406 // Render help text into a map of group-name to a list of (option, help) 407 // pairs. 408 typedef std::map<std::string, 409 std::vector<std::pair<std::string, const char*> > > helpmap_ty; 410 helpmap_ty GroupedOptionHelp; 411 412 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { 413 unsigned Id = i + 1; 414 415 // FIXME: Split out option groups. 416 if (getOptionKind(Id) == Option::GroupClass) 417 continue; 418 419 unsigned Flags = getInfo(Id).Flags; 420 if (FlagsToInclude && !(Flags & FlagsToInclude)) 421 continue; 422 if (Flags & FlagsToExclude) 423 continue; 424 425 if (const char *Text = getOptionHelpText(Id)) { 426 const char *HelpGroup = getOptionHelpGroup(*this, Id); 427 const std::string &OptName = getOptionHelpName(*this, Id); 428 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text)); 429 } 430 } 431 432 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(), 433 ie = GroupedOptionHelp.end(); it != ie; ++it) { 434 if (it != GroupedOptionHelp .begin()) 435 OS << "\n"; 436 PrintHelpOptionList(OS, it->first, it->second); 437 } 438 439 OS.flush(); 440} 441