LLVM  14.0.0git
OptTable.cpp
Go to the documentation of this file.
1 //===- OptTable.cpp - Option Table Implementation -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Option/OptTable.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/StringSet.h"
13 #include "llvm/Option/Arg.h"
14 #include "llvm/Option/ArgList.h"
16 #include "llvm/Option/Option.h"
17 #include "llvm/Support/CommandLine.h" // for expandResponseFiles
18 #include "llvm/Support/Compiler.h"
21 #include <algorithm>
22 #include <cassert>
23 #include <cctype>
24 #include <cstring>
25 #include <map>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 using namespace llvm;
31 using namespace llvm::opt;
32 
33 namespace llvm {
34 namespace opt {
35 
36 // Ordering on Info. The ordering is *almost* case-insensitive lexicographic,
37 // with an exception. '\0' comes at the end of the alphabet instead of the
38 // beginning (thus options precede any other options which prefix them).
39 static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) {
40  const char *X = A, *Y = B;
41  char a = tolower(*A), b = tolower(*B);
42  while (a == b) {
43  if (a == '\0')
44  return 0;
45 
46  a = tolower(*++X);
47  b = tolower(*++Y);
48  }
49 
50  if (a == '\0') // A is a prefix of B.
51  return 1;
52  if (b == '\0') // B is a prefix of A.
53  return -1;
54 
55  // Otherwise lexicographic.
56  return (a < b) ? -1 : 1;
57 }
58 
59 #ifndef NDEBUG
60 static int StrCmpOptionName(const char *A, const char *B) {
61  if (int N = StrCmpOptionNameIgnoreCase(A, B))
62  return N;
63  return strcmp(A, B);
64 }
65 
66 static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
67  if (&A == &B)
68  return false;
69 
70  if (int N = StrCmpOptionName(A.Name, B.Name))
71  return N < 0;
72 
73  for (const char * const *APre = A.Prefixes,
74  * const *BPre = B.Prefixes;
75  *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){
76  if (int N = StrCmpOptionName(*APre, *BPre))
77  return N < 0;
78  }
79 
80  // Names are the same, check that classes are in order; exactly one
81  // should be joined, and it should succeed the other.
82  assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
83  "Unexpected classes for options with same name.");
84  return B.Kind == Option::JoinedClass;
85 }
86 #endif
87 
88 // Support lower_bound between info and an option name.
89 static inline bool operator<(const OptTable::Info &I, const char *Name) {
90  return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0;
91 }
92 
93 } // end namespace opt
94 } // end namespace llvm
95 
96 OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
97 
98 OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase)
99  : OptionInfos(OptionInfos), IgnoreCase(IgnoreCase) {
100  // Explicitly zero initialize the error to work around a bug in array
101  // value-initialization on MinGW with gcc 4.3.5.
102 
103  // Find start of normal options.
104  for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
105  unsigned Kind = getInfo(i + 1).Kind;
106  if (Kind == Option::InputClass) {
107  assert(!InputOptionID && "Cannot have multiple input options!");
108  InputOptionID = getInfo(i + 1).ID;
109  } else if (Kind == Option::UnknownClass) {
110  assert(!UnknownOptionID && "Cannot have multiple unknown options!");
111  UnknownOptionID = getInfo(i + 1).ID;
112  } else if (Kind != Option::GroupClass) {
113  FirstSearchableIndex = i;
114  break;
115  }
116  }
117  assert(FirstSearchableIndex != 0 && "No searchable options?");
118 
119 #ifndef NDEBUG
120  // Check that everything after the first searchable option is a
121  // regular option class.
122  for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
125  Kind != Option::GroupClass) &&
126  "Special options should be defined first!");
127  }
128 
129  // Check that options are in order.
130  for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){
131  if (!(getInfo(i) < getInfo(i + 1))) {
132  getOption(i).dump();
133  getOption(i + 1).dump();
134  llvm_unreachable("Options are not in order!");
135  }
136  }
137 #endif
138 
139  // Build prefixes.
140  for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1;
141  i != e; ++i) {
142  if (const char *const *P = getInfo(i).Prefixes) {
143  for (; *P != nullptr; ++P) {
144  PrefixesUnion.insert(*P);
145  }
146  }
147  }
148 
149  // Build prefix chars.
150  for (StringSet<>::const_iterator I = PrefixesUnion.begin(),
151  E = PrefixesUnion.end(); I != E; ++I) {
152  StringRef Prefix = I->getKey();
153  for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end();
154  C != CE; ++C)
155  if (!is_contained(PrefixChars, *C))
156  PrefixChars.push_back(*C);
157  }
158 }
159 
160 OptTable::~OptTable() = default;
161 
163  unsigned id = Opt.getID();
164  if (id == 0)
165  return Option(nullptr, nullptr);
166  assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
167  return Option(&getInfo(id), this);
168 }
169 
170 static bool isInput(const StringSet<> &Prefixes, StringRef Arg) {
171  if (Arg == "-")
172  return true;
173  for (StringSet<>::const_iterator I = Prefixes.begin(),
174  E = Prefixes.end(); I != E; ++I)
175  if (Arg.startswith(I->getKey()))
176  return false;
177  return true;
178 }
179 
180 /// \returns Matched size. 0 means no match.
181 static unsigned matchOption(const OptTable::Info *I, StringRef Str,
182  bool IgnoreCase) {
183  for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) {
184  StringRef Prefix(*Pre);
185  if (Str.startswith(Prefix)) {
186  StringRef Rest = Str.substr(Prefix.size());
187  bool Matched = IgnoreCase ? Rest.startswith_insensitive(I->Name)
188  : Rest.startswith(I->Name);
189  if (Matched)
190  return Prefix.size() + StringRef(I->Name).size();
191  }
192  }
193  return 0;
194 }
195 
196 // Returns true if one of the Prefixes + In.Names matches Option
198  if (In.Prefixes) {
199  StringRef InName(In.Name);
200  for (size_t I = 0; In.Prefixes[I]; I++)
201  if (Option.endswith(InName))
202  if (Option.slice(0, Option.size() - InName.size()) == In.Prefixes[I])
203  return true;
204  }
205  return false;
206 }
207 
208 // This function is for flag value completion.
209 // Eg. When "-stdlib=" and "l" was passed to this function, it will return
210 // appropiriate values for stdlib, which starts with l.
211 std::vector<std::string>
213  // Search all options and return possible values.
214  for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
215  const Info &In = OptionInfos[I];
216  if (!In.Values || !optionMatches(In, Option))
217  continue;
218 
219  SmallVector<StringRef, 8> Candidates;
220  StringRef(In.Values).split(Candidates, ",", -1, false);
221 
222  std::vector<std::string> Result;
223  for (StringRef Val : Candidates)
224  if (Val.startswith(Arg) && Arg.compare(Val))
225  Result.push_back(std::string(Val));
226  return Result;
227  }
228  return {};
229 }
230 
231 std::vector<std::string>
232 OptTable::findByPrefix(StringRef Cur, unsigned int DisableFlags) const {
233  std::vector<std::string> Ret;
234  for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
235  const Info &In = OptionInfos[I];
236  if (!In.Prefixes || (!In.HelpText && !In.GroupID))
237  continue;
238  if (In.Flags & DisableFlags)
239  continue;
240 
241  for (int I = 0; In.Prefixes[I]; I++) {
242  std::string S = std::string(In.Prefixes[I]) + std::string(In.Name) + "\t";
243  if (In.HelpText)
244  S += In.HelpText;
245  if (StringRef(S).startswith(Cur) && S != std::string(Cur) + "\t")
246  Ret.push_back(S);
247  }
248  }
249  return Ret;
250 }
251 
252 unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
253  unsigned FlagsToInclude, unsigned FlagsToExclude,
254  unsigned MinimumLength) const {
255  assert(!Option.empty());
256 
257  // Consider each [option prefix + option name] pair as a candidate, finding
258  // the closest match.
259  unsigned BestDistance = UINT_MAX;
260  for (const Info &CandidateInfo :
261  ArrayRef<Info>(OptionInfos).drop_front(FirstSearchableIndex)) {
262  StringRef CandidateName = CandidateInfo.Name;
263 
264  // We can eliminate some option prefix/name pairs as candidates right away:
265  // * Ignore option candidates with empty names, such as "--", or names
266  // that do not meet the minimum length.
267  if (CandidateName.empty() || CandidateName.size() < MinimumLength)
268  continue;
269 
270  // * If FlagsToInclude were specified, ignore options that don't include
271  // those flags.
272  if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
273  continue;
274  // * Ignore options that contain the FlagsToExclude.
275  if (CandidateInfo.Flags & FlagsToExclude)
276  continue;
277 
278  // * Ignore positional argument option candidates (which do not
279  // have prefixes).
280  if (!CandidateInfo.Prefixes)
281  continue;
282 
283  // Now check if the candidate ends with a character commonly used when
284  // delimiting an option from its value, such as '=' or ':'. If it does,
285  // attempt to split the given option based on that delimiter.
286  StringRef LHS, RHS;
287  char Last = CandidateName.back();
288  bool CandidateHasDelimiter = Last == '=' || Last == ':';
289  std::string NormalizedName = std::string(Option);
290  if (CandidateHasDelimiter) {
291  std::tie(LHS, RHS) = Option.split(Last);
292  NormalizedName = std::string(LHS);
293  if (Option.find(Last) == LHS.size())
294  NormalizedName += Last;
295  }
296 
297  // Consider each possible prefix for each candidate to find the most
298  // appropriate one. For example, if a user asks for "--helm", suggest
299  // "--help" over "-help".
300  for (int P = 0;
301  const char *const CandidatePrefix = CandidateInfo.Prefixes[P]; P++) {
302  std::string Candidate = (CandidatePrefix + CandidateName).str();
303  StringRef CandidateRef = Candidate;
304  unsigned Distance =
305  CandidateRef.edit_distance(NormalizedName, /*AllowReplacements=*/true,
306  /*MaxEditDistance=*/BestDistance);
307  if (RHS.empty() && CandidateHasDelimiter) {
308  // The Candidate ends with a = or : delimiter, but the option passed in
309  // didn't contain the delimiter (or doesn't have anything after it).
310  // In that case, penalize the correction: `-nodefaultlibs` is more
311  // likely to be a spello for `-nodefaultlib` than `-nodefaultlib:` even
312  // though both have an unmodified editing distance of 1, since the
313  // latter would need an argument.
314  ++Distance;
315  }
316  if (Distance < BestDistance) {
317  BestDistance = Distance;
318  NearestString = (Candidate + RHS).str();
319  }
320  }
321  }
322  return BestDistance;
323 }
324 
325 bool OptTable::addValues(const char *Option, const char *Values) {
326  for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
327  Info &In = OptionInfos[I];
328  if (optionMatches(In, Option)) {
329  In.Values = Values;
330  return true;
331  }
332  }
333  return false;
334 }
335 
336 // Parse a single argument, return the new argument, and update Index. If
337 // GroupedShortOptions is true, -a matches "-abc" and the argument in Args will
338 // be updated to "-bc". This overload does not support
339 // FlagsToInclude/FlagsToExclude or case insensitive options.
340 std::unique_ptr<Arg> OptTable::parseOneArgGrouped(InputArgList &Args,
341  unsigned &Index) const {
342  // Anything that doesn't start with PrefixesUnion is an input, as is '-'
343  // itself.
344  const char *CStr = Args.getArgString(Index);
345  StringRef Str(CStr);
346  if (isInput(PrefixesUnion, Str))
347  return std::make_unique<Arg>(getOption(InputOptionID), Str, Index++, CStr);
348 
349  const Info *End = OptionInfos.data() + OptionInfos.size();
350  StringRef Name = Str.ltrim(PrefixChars);
351  const Info *Start = std::lower_bound(
352  OptionInfos.data() + FirstSearchableIndex, End, Name.data());
353  const Info *Fallback = nullptr;
354  unsigned Prev = Index;
355 
356  // Search for the option which matches Str.
357  for (; Start != End; ++Start) {
358  unsigned ArgSize = matchOption(Start, Str, IgnoreCase);
359  if (!ArgSize)
360  continue;
361 
362  Option Opt(Start, this);
363  if (std::unique_ptr<Arg> A =
364  Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
365  /*GroupedShortOption=*/false, Index))
366  return A;
367 
368  // If Opt is a Flag of length 2 (e.g. "-a"), we know it is a prefix of
369  // the current argument (e.g. "-abc"). Match it as a fallback if no longer
370  // option (e.g. "-ab") exists.
371  if (ArgSize == 2 && Opt.getKind() == Option::FlagClass)
372  Fallback = Start;
373 
374  // Otherwise, see if the argument is missing.
375  if (Prev != Index)
376  return nullptr;
377  }
378  if (Fallback) {
379  Option Opt(Fallback, this);
380  // Check that the last option isn't a flag wrongly given an argument.
381  if (Str[2] == '=')
382  return std::make_unique<Arg>(getOption(UnknownOptionID), Str, Index++,
383  CStr);
384 
385  if (std::unique_ptr<Arg> A = Opt.accept(
386  Args, Str.substr(0, 2), /*GroupedShortOption=*/true, Index)) {
387  Args.replaceArgString(Index, Twine('-') + Str.substr(2));
388  return A;
389  }
390  }
391 
392  // In the case of an incorrect short option extract the character and move to
393  // the next one.
394  if (Str[1] != '-') {
395  CStr = Args.MakeArgString(Str.substr(0, 2));
396  Args.replaceArgString(Index, Twine('-') + Str.substr(2));
397  return std::make_unique<Arg>(getOption(UnknownOptionID), CStr, Index, CStr);
398  }
399 
400  return std::make_unique<Arg>(getOption(UnknownOptionID), Str, Index++, CStr);
401 }
402 
403 std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
404  unsigned FlagsToInclude,
405  unsigned FlagsToExclude) const {
406  unsigned Prev = Index;
407  const char *Str = Args.getArgString(Index);
408 
409  // Anything that doesn't start with PrefixesUnion is an input, as is '-'
410  // itself.
411  if (isInput(PrefixesUnion, Str))
412  return std::make_unique<Arg>(getOption(InputOptionID), Str, Index++, Str);
413 
414  const Info *Start = OptionInfos.data() + FirstSearchableIndex;
415  const Info *End = OptionInfos.data() + OptionInfos.size();
416  StringRef Name = StringRef(Str).ltrim(PrefixChars);
417 
418  // Search for the first next option which could be a prefix.
419  Start = std::lower_bound(Start, End, Name.data());
420 
421  // Options are stored in sorted order, with '\0' at the end of the
422  // alphabet. Since the only options which can accept a string must
423  // prefix it, we iteratively search for the next option which could
424  // be a prefix.
425  //
426  // FIXME: This is searching much more than necessary, but I am
427  // blanking on the simplest way to make it fast. We can solve this
428  // problem when we move to TableGen.
429  for (; Start != End; ++Start) {
430  unsigned ArgSize = 0;
431  // Scan for first option which is a proper prefix.
432  for (; Start != End; ++Start)
433  if ((ArgSize = matchOption(Start, Str, IgnoreCase)))
434  break;
435  if (Start == End)
436  break;
437 
438  Option Opt(Start, this);
439 
440  if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
441  continue;
442  if (Opt.hasFlag(FlagsToExclude))
443  continue;
444 
445  // See if this option matches.
446  if (std::unique_ptr<Arg> A =
447  Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
448  /*GroupedShortOption=*/false, Index))
449  return A;
450 
451  // Otherwise, see if this argument was missing values.
452  if (Prev != Index)
453  return nullptr;
454  }
455 
456  // If we failed to find an option and this arg started with /, then it's
457  // probably an input path.
458  if (Str[0] == '/')
459  return std::make_unique<Arg>(getOption(InputOptionID), Str, Index++, Str);
460 
461  return std::make_unique<Arg>(getOption(UnknownOptionID), Str, Index++, Str);
462 }
463 
465  unsigned &MissingArgIndex,
466  unsigned &MissingArgCount,
467  unsigned FlagsToInclude,
468  unsigned FlagsToExclude) const {
469  InputArgList Args(ArgArr.begin(), ArgArr.end());
470 
471  // FIXME: Handle '@' args (or at least error on them).
472 
473  MissingArgIndex = MissingArgCount = 0;
474  unsigned Index = 0, End = ArgArr.size();
475  while (Index < End) {
476  // Ingore nullptrs, they are response file's EOL markers
477  if (Args.getArgString(Index) == nullptr) {
478  ++Index;
479  continue;
480  }
481  // Ignore empty arguments (other things may still take them as arguments).
482  StringRef Str = Args.getArgString(Index);
483  if (Str == "") {
484  ++Index;
485  continue;
486  }
487 
488  unsigned Prev = Index;
489  std::unique_ptr<Arg> A = GroupedShortOptions
490  ? parseOneArgGrouped(Args, Index)
491  : ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude);
492  assert((Index > Prev || GroupedShortOptions) &&
493  "Parser failed to consume argument.");
494 
495  // Check for missing argument error.
496  if (!A) {
497  assert(Index >= End && "Unexpected parser error.");
498  assert(Index - Prev - 1 && "No missing arguments!");
499  MissingArgIndex = Prev;
500  MissingArgCount = Index - Prev - 1;
501  break;
502  }
503 
504  Args.append(A.release());
505  }
506 
507  return Args;
508 }
509 
510 InputArgList OptTable::parseArgs(int Argc, char *const *Argv,
511  OptSpecifier Unknown, StringSaver &Saver,
512  function_ref<void(StringRef)> ErrorFn) const {
514  // The environment variable specifies initial options which can be overridden
515  // by commnad line options.
516  cl::expandResponseFiles(Argc, Argv, EnvVar, Saver, NewArgv);
517 
518  unsigned MAI, MAC;
519  opt::InputArgList Args = ParseArgs(makeArrayRef(NewArgv), MAI, MAC);
520  if (MAC)
521  ErrorFn((Twine(Args.getArgString(MAI)) + ": missing argument").str());
522 
523  // For each unknwon option, call ErrorFn with a formatted error message. The
524  // message includes a suggested alternative option spelling if available.
525  std::string Nearest;
526  for (const opt::Arg *A : Args.filtered(Unknown)) {
527  std::string Spelling = A->getAsString(Args);
528  if (findNearest(Spelling, Nearest) > 1)
529  ErrorFn("unknown argument '" + A->getAsString(Args) + "'");
530  else
531  ErrorFn("unknown argument '" + A->getAsString(Args) +
532  "', did you mean '" + Nearest + "'?");
533  }
534  return Args;
535 }
536 
537 static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
538  const Option O = Opts.getOption(Id);
539  std::string Name = O.getPrefixedName();
540 
541  // Add metavar, if used.
542  switch (O.getKind()) {
544  llvm_unreachable("Invalid option with help text.");
545 
547  if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) {
548  // For MultiArgs, metavar is full list of all argument names.
549  Name += ' ';
550  Name += MetaVarName;
551  }
552  else {
553  // For MultiArgs<N>, if metavar not supplied, print <value> N times.
554  for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) {
555  Name += " <value>";
556  }
557  }
558  break;
559 
560  case Option::FlagClass:
561  break;
562 
563  case Option::ValuesClass:
564  break;
565 
568  Name += ' ';
572  if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
573  Name += MetaVarName;
574  else
575  Name += "<value>";
576  break;
577  }
578 
579  return Name;
580 }
581 
582 namespace {
583 struct OptionInfo {
584  std::string Name;
585  StringRef HelpText;
586 };
587 } // namespace
588 
590  std::vector<OptionInfo> &OptionHelp) {
591  OS << Title << ":\n";
592 
593  // Find the maximum option length.
594  unsigned OptionFieldWidth = 0;
595  for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
596  // Limit the amount of padding we are willing to give up for alignment.
597  unsigned Length = OptionHelp[i].Name.size();
598  if (Length <= 23)
599  OptionFieldWidth = std::max(OptionFieldWidth, Length);
600  }
601 
602  const unsigned InitialPad = 2;
603  for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
604  const std::string &Option = OptionHelp[i].Name;
605  int Pad = OptionFieldWidth - int(Option.size());
606  OS.indent(InitialPad) << Option;
607 
608  // Break on long option names.
609  if (Pad < 0) {
610  OS << "\n";
611  Pad = OptionFieldWidth + InitialPad;
612  }
613  OS.indent(Pad + 1) << OptionHelp[i].HelpText << '\n';
614  }
615 }
616 
617 static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
618  unsigned GroupID = Opts.getOptionGroupID(Id);
619 
620  // If not in a group, return the default help group.
621  if (!GroupID)
622  return "OPTIONS";
623 
624  // Abuse the help text of the option groups to store the "help group"
625  // name.
626  //
627  // FIXME: Split out option groups.
628  if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
629  return GroupHelp;
630 
631  // Otherwise keep looking.
632  return getOptionHelpGroup(Opts, GroupID);
633 }
634 
635 void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
636  bool ShowHidden, bool ShowAllAliases) const {
637  printHelp(OS, Usage, Title, /*Include*/ 0, /*Exclude*/
638  (ShowHidden ? 0 : HelpHidden), ShowAllAliases);
639 }
640 
641 void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
642  unsigned FlagsToInclude, unsigned FlagsToExclude,
643  bool ShowAllAliases) const {
644  OS << "OVERVIEW: " << Title << "\n\n";
645  OS << "USAGE: " << Usage << "\n\n";
646 
647  // Render help text into a map of group-name to a list of (option, help)
648  // pairs.
649  std::map<std::string, std::vector<OptionInfo>> GroupedOptionHelp;
650 
651  for (unsigned Id = 1, e = getNumOptions() + 1; Id != e; ++Id) {
652  // FIXME: Split out option groups.
654  continue;
655 
656  unsigned Flags = getInfo(Id).Flags;
657  if (FlagsToInclude && !(Flags & FlagsToInclude))
658  continue;
659  if (Flags & FlagsToExclude)
660  continue;
661 
662  // If an alias doesn't have a help text, show a help text for the aliased
663  // option instead.
664  const char *HelpText = getOptionHelpText(Id);
665  if (!HelpText && ShowAllAliases) {
666  const Option Alias = getOption(Id).getAlias();
667  if (Alias.isValid())
668  HelpText = getOptionHelpText(Alias.getID());
669  }
670 
671  if (HelpText && (strlen(HelpText) != 0)) {
672  const char *HelpGroup = getOptionHelpGroup(*this, Id);
673  const std::string &OptName = getOptionHelpName(*this, Id);
674  GroupedOptionHelp[HelpGroup].push_back({OptName, HelpText});
675  }
676  }
677 
678  for (auto& OptionGroup : GroupedOptionHelp) {
679  if (OptionGroup.first != GroupedOptionHelp.begin()->first)
680  OS << "\n";
681  PrintHelpOptionList(OS, OptionGroup.first, OptionGroup.second);
682  }
683 
684  OS.flush();
685 }
matchOption
static unsigned matchOption(const OptTable::Info *I, StringRef Str, bool IgnoreCase)
Definition: OptTable.cpp:181
llvm::opt::Option::dump
void dump() const
Definition: Option.cpp:90
i
i
Definition: README.txt:29
llvm::opt::OptTable
Provide access to the Option info table.
Definition: OptTable.h:40
llvm::StringRef::back
LLVM_NODISCARD char back() const
back - Get the last character in the string.
Definition: StringRef.h:168
Option.h
llvm::StringRef::startswith
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:286
llvm::opt::OptTable::getOption
const Option getOption(OptSpecifier Opt) const
Get the given Opt's Option instance, lazily creating it if necessary.
Definition: OptTable.cpp:162
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::opt::Option::MultiArgClass
@ MultiArgClass
Definition: Option.h:64
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
llvm::opt::InputArgList
Definition: ArgList.h:367
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:164
llvm::opt::Arg
A concrete instance of a particular driver option.
Definition: Arg.h:34
llvm::opt::Option::CommaJoinedClass
@ CommaJoinedClass
Definition: Option.h:63
llvm::lower_bound
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1661
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::opt::ArgList
ArgList - Ordered collection of driver arguments.
Definition: ArgList.h:116
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
ErrorHandling.h
llvm::StringRef::ltrim
LLVM_NODISCARD StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition: StringRef.h:842
llvm::opt::OptTable::printHelp
void printHelp(raw_ostream &OS, const char *Usage, const char *Title, unsigned FlagsToInclude, unsigned FlagsToExclude, bool ShowAllAliases) const
Render the help text for an option table.
Definition: OptTable.cpp:641
llvm::StringMap::end
iterator end()
Definition: StringMap.h:203
startswith
static bool startswith(StringRef Magic, const char(&S)[N])
Definition: Magic.cpp:30
optionMatches
static bool optionMatches(const OptTable::Info &In, StringRef Option)
Definition: OptTable.cpp:197
llvm::opt::OptTable::parseArgs
InputArgList parseArgs(int Argc, char *const *Argv, OptSpecifier Unknown, StringSaver &Saver, function_ref< void(StringRef)> ErrorFn) const
A convenience helper which handles optional initial options populated from an environment variable,...
Definition: OptTable.cpp:510
llvm::opt::OptTable::findNearest
unsigned findNearest(StringRef Option, std::string &NearestString, unsigned FlagsToInclude=0, unsigned FlagsToExclude=0, unsigned MinimumLength=4) const
Find the OptTable option that most closely matches the given string.
Definition: OptTable.cpp:252
llvm::cl::expandResponseFiles
bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar, StringSaver &Saver, SmallVectorImpl< const char * > &NewArgv)
A convenience helper which concatenates the options specified by the environment variable EnvVar and ...
Definition: CommandLine.cpp:1260
llvm::opt::Option::InputClass
@ InputClass
Definition: Option.h:55
llvm::StringSet::insert
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:33
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:116
STLExtras.h
llvm::opt::Option::accept
std::unique_ptr< Arg > accept(const ArgList &Args, StringRef CurArg, bool GroupedShortOption, unsigned &Index) const
Potentially accept the current argument, returning a new Arg instance, or 0 if the option does not ac...
Definition: Option.cpp:236
llvm::opt::OptTable::findByPrefix
std::vector< std::string > findByPrefix(StringRef Cur, unsigned int DisableFlags) const
Find flags from OptTable which starts with Cur.
Definition: OptTable.cpp:232
llvm::opt::OptTable::getOptionKind
unsigned getOptionKind(OptSpecifier id) const
Get the kind of the given option.
Definition: OptTable.h:110
llvm::opt::OptTable::getOptionGroupID
unsigned getOptionGroupID(OptSpecifier id) const
Get the group id for the given option.
Definition: OptTable.h:115
a
=0.0 ? 0.0 :(a > 0.0 ? 1.0 :-1.0) a
Definition: README.txt:489
llvm::opt::StrCmpOptionName
static int StrCmpOptionName(const char *A, const char *B)
Definition: OptTable.cpp:60
getOptionHelpGroup
static const char * getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id)
Definition: OptTable.cpp:617
CommandLine.h
PrintHelpOptionList
static void PrintHelpOptionList(raw_ostream &OS, StringRef Title, std::vector< OptionInfo > &OptionHelp)
Definition: OptTable.cpp:589
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::StringRef::split
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:749
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::opt::OptSpecifier::OptSpecifier
OptSpecifier()=default
llvm::StringRef::startswith_insensitive
LLVM_NODISCARD bool startswith_insensitive(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
Definition: StringRef.cpp:45
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
llvm::StringMapConstIterator
Definition: StringMap.h:24
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
llvm::opt::Option::RemainingArgsJoinedClass
@ RemainingArgsJoinedClass
Definition: Option.h:62
b
the resulting code requires compare and branches when and if the revised code is with conditional branches instead of More there is a byte word extend before each where there should be only and the condition codes are not remembered when the same two values are compared twice More LSR enhancements i8 and i32 load store addressing modes are identical int b
Definition: README.txt:418
llvm::opt::Option::getID
unsigned getID() const
Definition: Option.h:87
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
opt
arm prera ldst opt
Definition: ARMLoadStoreOptimizer.cpp:2191
llvm::opt::OptTable::Info::ID
unsigned ID
Definition: OptTable.h:50
llvm::opt
Definition: Arg.h:26
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::raw_ostream::flush
void flush()
Definition: raw_ostream.h:186
llvm::opt::Option::SeparateClass
@ SeparateClass
Definition: Option.h:60
llvm::opt::Option::JoinedAndSeparateClass
@ JoinedAndSeparateClass
Definition: Option.h:66
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::opt::OptSpecifier
OptSpecifier - Wrapper class for abstracting references to option IDs.
Definition: OptSpecifier.h:18
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::opt::Option::UnknownClass
@ UnknownClass
Definition: Option.h:56
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
llvm::tgtok::In
@ In
Definition: TGLexer.h:51
llvm::opt::OptTable::addValues
bool addValues(const char *Option, const char *Values)
Add Values to Option's Values class.
Definition: OptTable.cpp:325
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:197
llvm::opt::OptTable::ParseOneArg
std::unique_ptr< Arg > ParseOneArg(const ArgList &Args, unsigned &Index, unsigned FlagsToInclude=0, unsigned FlagsToExclude=0) const
Parse a single argument; returning the new argument and updating Index.
Definition: OptTable.cpp:403
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::opt::OptTable::getOptionMetaVar
const char * getOptionMetaVar(OptSpecifier id) const
Get the meta-variable name to use when describing this options values in the help text.
Definition: OptTable.h:126
OptTable.h
llvm::numbers::e
constexpr double e
Definition: MathExtras.h:57
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1616
llvm::StringMap::begin
iterator begin()
Definition: StringMap.h:202
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::opt::Option::isValid
bool isValid() const
Definition: Option.h:83
llvm::opt::Option::ValuesClass
@ ValuesClass
Definition: Option.h:59
PreferPredicateTy::Option
Option
Definition: LoopVectorize.cpp:212
llvm::opt::OptTable::suggestValueCompletions
std::vector< std::string > suggestValueCompletions(StringRef Option, StringRef Arg) const
Find possible value for given flags.
Definition: OptTable.cpp:212
llvm::opt::Option::GroupClass
@ GroupClass
Definition: Option.h:54
llvm::opt::HelpHidden
@ HelpHidden
Definition: Option.h:34
llvm::StringSaver
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition: StringSaver.h:21
getOptionHelpName
static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id)
Definition: OptTable.cpp:537
llvm::StringSet
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:22
OptSpecifier.h
Arg.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::opt::Option::RemainingArgsClass
@ RemainingArgsClass
Definition: Option.h:61
Compiler.h
llvm::opt::OptTable::getOptionHelpText
const char * getOptionHelpText(OptSpecifier id) const
Get the help text to use to describe this option.
Definition: OptTable.h:120
llvm::opt::operator<
static bool operator<(const OptTable::Info &A, const OptTable::Info &B)
Definition: OptTable.cpp:66
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::opt::StrCmpOptionNameIgnoreCase
static int StrCmpOptionNameIgnoreCase(const char *A, const char *B)
Definition: OptTable.cpp:39
LLVM_FALLTHROUGH
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:273
StringSet.h
llvm::opt::OptTable::ParseArgs
InputArgList ParseArgs(ArrayRef< const char * > Args, unsigned &MissingArgIndex, unsigned &MissingArgCount, unsigned FlagsToInclude=0, unsigned FlagsToExclude=0) const
Parse an list of arguments into an InputArgList.
Definition: OptTable.cpp:464
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::opt::OptTable::~OptTable
~OptTable()
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
ArgList.h
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:153
llvm::opt::OptTable::OptTable
OptTable(ArrayRef< Info > OptionInfos, bool IgnoreCase=false)
Definition: OptTable.cpp:98
llvm::opt::OptTable::Info::Kind
unsigned char Kind
Definition: OptTable.h:51
llvm::opt::OptTable::Info::Flags
unsigned int Flags
Definition: OptTable.h:53
llvm::opt::Option::hasFlag
bool hasFlag(unsigned Val) const
Test if this option has the flag Val.
Definition: Option.h:182
llvm::opt::Option
Option - Abstract representation for a single form of driver argument.
Definition: Option.h:51
llvm::opt::Option::JoinedOrSeparateClass
@ JoinedOrSeparateClass
Definition: Option.h:65
isInput
static bool isInput(const StringSet<> &Prefixes, StringRef Arg)
Definition: OptTable.cpp:170
llvm::opt::Option::FlagClass
@ FlagClass
Definition: Option.h:57
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:476
llvm::raw_ostream::indent
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Definition: raw_ostream.cpp:497
N
#define N
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::StringRef::const_iterator
const char * const_iterator
Definition: StringRef.h:63
llvm::opt::OptTable::Info
Entry for a single option instance in the option data table.
Definition: OptTable.h:43
llvm::opt::Option::OptionClass
OptionClass
Definition: Option.h:53
llvm::StringRef::edit_distance
LLVM_NODISCARD unsigned edit_distance(StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const
Determine the edit distance between this string and another string.
Definition: StringRef.cpp:92
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::opt::Option::getAlias
const Option getAlias() const
Definition: Option.h:109
raw_ostream.h
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:228
llvm::StringRef::size
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:157
llvm::opt::Option::JoinedClass
@ JoinedClass
Definition: Option.h:58
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:154
llvm::opt::OptTable::getNumOptions
unsigned getNumOptions() const
Return the total number of option classes.
Definition: OptTable.h:96
llvm::opt::OptSpecifier::getID
unsigned getID() const
Definition: OptSpecifier.h:29
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37