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 "clang/Driver/OptTable.h" 11#include "clang/Driver/Arg.h" 12#include "clang/Driver/ArgList.h" 13#include "clang/Driver/Option.h" 14#include "llvm/Support/raw_ostream.h" 15#include "llvm/Support/ErrorHandling.h" 16#include <algorithm> 17#include <map> 18using namespace clang::driver; 19using namespace clang::driver::options; 20using namespace clang; 21 22// Ordering on Info. The ordering is *almost* lexicographic, with two 23// exceptions. First, '\0' comes at the end of the alphabet instead of 24// the beginning (thus options precede any other options which prefix 25// them). Second, for options with the same name, the less permissive 26// version should come first; a Flag option should precede a Joined 27// option, for example. 28 29static int StrCmpOptionName(const char *A, const char *B) { 30 char a = *A, b = *B; 31 while (a == b) { 32 if (a == '\0') 33 return 0; 34 35 a = *++A; 36 b = *++B; 37 } 38 39 if (a == '\0') // A is a prefix of B. 40 return 1; 41 if (b == '\0') // B is a prefix of A. 42 return -1; 43 44 // Otherwise lexicographic. 45 return (a < b) ? -1 : 1; 46} 47 48namespace clang { 49namespace driver { 50static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) { 51 if (&A == &B) 52 return false; 53 54 if (int N = StrCmpOptionName(A.Name, B.Name)) 55 return N == -1; 56 57 // Names are the same, check that classes are in order; exactly one 58 // should be joined, and it should succeed the other. 59 assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) && 60 "Unexpected classes for options with same name."); 61 return B.Kind == Option::JoinedClass; 62} 63 64// Support lower_bound between info and an option name. 65static inline bool operator<(const OptTable::Info &I, const char *Name) { 66 return StrCmpOptionName(I.Name, Name) == -1; 67} 68static inline bool operator<(const char *Name, const OptTable::Info &I) { 69 return StrCmpOptionName(Name, I.Name) == -1; 70} 71} 72} 73 74// 75 76OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {} 77 78// 79 80OptTable::OptTable(const Info *_OptionInfos, unsigned _NumOptionInfos) 81 : OptionInfos(_OptionInfos), NumOptionInfos(_NumOptionInfos), 82 Options(new Option*[NumOptionInfos]), 83 TheInputOption(0), TheUnknownOption(0), FirstSearchableIndex(0) 84{ 85 // Explicitly zero initialize the error to work around a bug in array 86 // value-initialization on MinGW with gcc 4.3.5. 87 memset(Options, 0, sizeof(*Options) * NumOptionInfos); 88 89 // Find start of normal options. 90 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { 91 unsigned Kind = getInfo(i + 1).Kind; 92 if (Kind == Option::InputClass) { 93 assert(!TheInputOption && "Cannot have multiple input options!"); 94 TheInputOption = getOption(i + 1); 95 } else if (Kind == Option::UnknownClass) { 96 assert(!TheUnknownOption && "Cannot have multiple input options!"); 97 TheUnknownOption = getOption(i + 1); 98 } else if (Kind != Option::GroupClass) { 99 FirstSearchableIndex = i; 100 break; 101 } 102 } 103 assert(FirstSearchableIndex != 0 && "No searchable options?"); 104 105#ifndef NDEBUG 106 // Check that everything after the first searchable option is a 107 // regular option class. 108 for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) { 109 Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind; 110 assert((Kind != Option::InputClass && Kind != Option::UnknownClass && 111 Kind != Option::GroupClass) && 112 "Special options should be defined first!"); 113 } 114 115 // Check that options are in order. 116 for (unsigned i = FirstSearchableIndex+1, e = getNumOptions(); i != e; ++i) { 117 if (!(getInfo(i) < getInfo(i + 1))) { 118 getOption(i)->dump(); 119 getOption(i + 1)->dump(); 120 llvm_unreachable("Options are not in order!"); 121 } 122 } 123#endif 124} 125 126OptTable::~OptTable() { 127 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) 128 delete Options[i]; 129 delete[] Options; 130} 131 132Option *OptTable::CreateOption(unsigned id) const { 133 const Info &info = getInfo(id); 134 const OptionGroup *Group = 135 cast_or_null<OptionGroup>(getOption(info.GroupID)); 136 const Option *Alias = getOption(info.AliasID); 137 138 Option *Opt = 0; 139 switch (info.Kind) { 140 case Option::InputClass: 141 Opt = new InputOption(id); break; 142 case Option::UnknownClass: 143 Opt = new UnknownOption(id); break; 144 case Option::GroupClass: 145 Opt = new OptionGroup(id, info.Name, Group); break; 146 case Option::FlagClass: 147 Opt = new FlagOption(id, info.Name, Group, Alias); break; 148 case Option::JoinedClass: 149 Opt = new JoinedOption(id, info.Name, Group, Alias); break; 150 case Option::SeparateClass: 151 Opt = new SeparateOption(id, info.Name, Group, Alias); break; 152 case Option::CommaJoinedClass: 153 Opt = new CommaJoinedOption(id, info.Name, Group, Alias); break; 154 case Option::MultiArgClass: 155 Opt = new MultiArgOption(id, info.Name, Group, Alias, info.Param); break; 156 case Option::JoinedOrSeparateClass: 157 Opt = new JoinedOrSeparateOption(id, info.Name, Group, Alias); break; 158 case Option::JoinedAndSeparateClass: 159 Opt = new JoinedAndSeparateOption(id, info.Name, Group, Alias); break; 160 } 161 162 if (info.Flags & DriverOption) 163 Opt->setDriverOption(true); 164 if (info.Flags & LinkerInput) 165 Opt->setLinkerInput(true); 166 if (info.Flags & NoArgumentUnused) 167 Opt->setNoArgumentUnused(true); 168 if (info.Flags & NoForward) 169 Opt->setNoForward(true); 170 if (info.Flags & RenderAsInput) 171 Opt->setNoOptAsInput(true); 172 if (info.Flags & RenderJoined) { 173 assert((info.Kind == Option::JoinedOrSeparateClass || 174 info.Kind == Option::SeparateClass) && "Invalid option."); 175 Opt->setRenderStyle(Option::RenderJoinedStyle); 176 } 177 if (info.Flags & RenderSeparate) { 178 assert((info.Kind == Option::JoinedOrSeparateClass || 179 info.Kind == Option::JoinedClass) && "Invalid option."); 180 Opt->setRenderStyle(Option::RenderSeparateStyle); 181 } 182 if (info.Flags & Unsupported) 183 Opt->setUnsupported(true); 184 185 return Opt; 186} 187 188Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index) const { 189 unsigned Prev = Index; 190 const char *Str = Args.getArgString(Index); 191 192 // Anything that doesn't start with '-' is an input, as is '-' itself. 193 if (Str[0] != '-' || Str[1] == '\0') 194 return new Arg(TheInputOption, Index++, Str); 195 196 const Info *Start = OptionInfos + FirstSearchableIndex; 197 const Info *End = OptionInfos + getNumOptions(); 198 199 // Search for the first next option which could be a prefix. 200 Start = std::lower_bound(Start, End, Str); 201 202 // Options are stored in sorted order, with '\0' at the end of the 203 // alphabet. Since the only options which can accept a string must 204 // prefix it, we iteratively search for the next option which could 205 // be a prefix. 206 // 207 // FIXME: This is searching much more than necessary, but I am 208 // blanking on the simplest way to make it fast. We can solve this 209 // problem when we move to TableGen. 210 for (; Start != End; ++Start) { 211 // Scan for first option which is a proper prefix. 212 for (; Start != End; ++Start) 213 if (memcmp(Str, Start->Name, strlen(Start->Name)) == 0) 214 break; 215 if (Start == End) 216 break; 217 218 // See if this option matches. 219 if (Arg *A = getOption(Start - OptionInfos + 1)->accept(Args, Index)) 220 return A; 221 222 // Otherwise, see if this argument was missing values. 223 if (Prev != Index) 224 return 0; 225 } 226 227 return new Arg(TheUnknownOption, Index++, Str); 228} 229 230InputArgList *OptTable::ParseArgs(const char* const *ArgBegin, 231 const char* const *ArgEnd, 232 unsigned &MissingArgIndex, 233 unsigned &MissingArgCount) const { 234 InputArgList *Args = new InputArgList(ArgBegin, ArgEnd); 235 236 // FIXME: Handle '@' args (or at least error on them). 237 238 MissingArgIndex = MissingArgCount = 0; 239 unsigned Index = 0, End = ArgEnd - ArgBegin; 240 while (Index < End) { 241 // Ignore empty arguments (other things may still take them as arguments). 242 if (Args->getArgString(Index)[0] == '\0') { 243 ++Index; 244 continue; 245 } 246 247 unsigned Prev = Index; 248 Arg *A = ParseOneArg(*Args, Index); 249 assert(Index > Prev && "Parser failed to consume argument."); 250 251 // Check for missing argument error. 252 if (!A) { 253 assert(Index >= End && "Unexpected parser error."); 254 assert(Index - Prev - 1 && "No missing arguments!"); 255 MissingArgIndex = Prev; 256 MissingArgCount = Index - Prev - 1; 257 break; 258 } 259 260 Args->append(A); 261 } 262 263 return Args; 264} 265 266static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) { 267 std::string Name = Opts.getOptionName(Id); 268 269 // Add metavar, if used. 270 switch (Opts.getOptionKind(Id)) { 271 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass: 272 llvm_unreachable("Invalid option with help text."); 273 274 case Option::MultiArgClass: 275 llvm_unreachable("Cannot print metavar for this kind of option."); 276 277 case Option::FlagClass: 278 break; 279 280 case Option::SeparateClass: case Option::JoinedOrSeparateClass: 281 Name += ' '; 282 // FALLTHROUGH 283 case Option::JoinedClass: case Option::CommaJoinedClass: 284 case Option::JoinedAndSeparateClass: 285 if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) 286 Name += MetaVarName; 287 else 288 Name += "<value>"; 289 break; 290 } 291 292 return Name; 293} 294 295static void PrintHelpOptionList(raw_ostream &OS, StringRef Title, 296 std::vector<std::pair<std::string, 297 const char*> > &OptionHelp) { 298 OS << Title << ":\n"; 299 300 // Find the maximum option length. 301 unsigned OptionFieldWidth = 0; 302 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 303 // Skip titles. 304 if (!OptionHelp[i].second) 305 continue; 306 307 // Limit the amount of padding we are willing to give up for alignment. 308 unsigned Length = OptionHelp[i].first.size(); 309 if (Length <= 23) 310 OptionFieldWidth = std::max(OptionFieldWidth, Length); 311 } 312 313 const unsigned InitialPad = 2; 314 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) { 315 const std::string &Option = OptionHelp[i].first; 316 int Pad = OptionFieldWidth - int(Option.size()); 317 OS.indent(InitialPad) << Option; 318 319 // Break on long option names. 320 if (Pad < 0) { 321 OS << "\n"; 322 Pad = OptionFieldWidth + InitialPad; 323 } 324 OS.indent(Pad + 1) << OptionHelp[i].second << '\n'; 325 } 326} 327 328static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) { 329 unsigned GroupID = Opts.getOptionGroupID(Id); 330 331 // If not in a group, return the default help group. 332 if (!GroupID) 333 return "OPTIONS"; 334 335 // Abuse the help text of the option groups to store the "help group" 336 // name. 337 // 338 // FIXME: Split out option groups. 339 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID)) 340 return GroupHelp; 341 342 // Otherwise keep looking. 343 return getOptionHelpGroup(Opts, GroupID); 344} 345 346void OptTable::PrintHelp(raw_ostream &OS, const char *Name, 347 const char *Title, bool ShowHidden) const { 348 OS << "OVERVIEW: " << Title << "\n"; 349 OS << '\n'; 350 OS << "USAGE: " << Name << " [options] <inputs>\n"; 351 OS << '\n'; 352 353 // Render help text into a map of group-name to a list of (option, help) 354 // pairs. 355 typedef std::map<std::string, 356 std::vector<std::pair<std::string, const char*> > > helpmap_ty; 357 helpmap_ty GroupedOptionHelp; 358 359 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) { 360 unsigned Id = i + 1; 361 362 // FIXME: Split out option groups. 363 if (getOptionKind(Id) == Option::GroupClass) 364 continue; 365 366 if (!ShowHidden && isOptionHelpHidden(Id)) 367 continue; 368 369 if (const char *Text = getOptionHelpText(Id)) { 370 const char *HelpGroup = getOptionHelpGroup(*this, Id); 371 const std::string &OptName = getOptionHelpName(*this, Id); 372 GroupedOptionHelp[HelpGroup].push_back(std::make_pair(OptName, Text)); 373 } 374 } 375 376 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(), 377 ie = GroupedOptionHelp.end(); it != ie; ++it) { 378 if (it != GroupedOptionHelp .begin()) 379 OS << "\n"; 380 PrintHelpOptionList(OS, it->first, it->second); 381 } 382 383 OS.flush(); 384} 385