LLVM  16.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) {
123  Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
124  assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
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 (char C : Prefix)
154  if (!is_contained(PrefixChars, C))
155  PrefixChars.push_back(C);
156  }
157 }
158 
159 OptTable::~OptTable() = default;
160 
162  unsigned id = Opt.getID();
163  if (id == 0)
164  return Option(nullptr, nullptr);
165  assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
166  return Option(&getInfo(id), this);
167 }
168 
169 static bool isInput(const StringSet<> &Prefixes, StringRef Arg) {
170  if (Arg == "-")
171  return true;
172  for (StringSet<>::const_iterator I = Prefixes.begin(),
173  E = Prefixes.end(); I != E; ++I)
174  if (Arg.startswith(I->getKey()))
175  return false;
176  return true;
177 }
178 
179 /// \returns Matched size. 0 means no match.
180 static unsigned matchOption(const OptTable::Info *I, StringRef Str,
181  bool IgnoreCase) {
182  for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) {
183  StringRef Prefix(*Pre);
184  if (Str.startswith(Prefix)) {
185  StringRef Rest = Str.substr(Prefix.size());
186  bool Matched = IgnoreCase ? Rest.startswith_insensitive(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 
195 // Returns true if one of the Prefixes + In.Names matches Option
197  if (In.Prefixes) {
198  StringRef InName(In.Name);
199  for (size_t I = 0; In.Prefixes[I]; I++)
200  if (Option.endswith(InName))
201  if (Option.slice(0, Option.size() - InName.size()) == In.Prefixes[I])
202  return true;
203  }
204  return false;
205 }
206 
207 // This function is for flag value completion.
208 // Eg. When "-stdlib=" and "l" was passed to this function, it will return
209 // appropiriate values for stdlib, which starts with l.
210 std::vector<std::string>
212  // Search all options and return possible values.
213  for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
214  const Info &In = OptionInfos[I];
215  if (!In.Values || !optionMatches(In, Option))
216  continue;
217 
218  SmallVector<StringRef, 8> Candidates;
219  StringRef(In.Values).split(Candidates, ",", -1, false);
220 
221  std::vector<std::string> Result;
222  for (StringRef Val : Candidates)
223  if (Val.startswith(Arg) && Arg.compare(Val))
224  Result.push_back(std::string(Val));
225  return Result;
226  }
227  return {};
228 }
229 
230 std::vector<std::string>
231 OptTable::findByPrefix(StringRef Cur, unsigned int DisableFlags) const {
232  std::vector<std::string> Ret;
233  for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
234  const Info &In = OptionInfos[I];
235  if (!In.Prefixes || (!In.HelpText && !In.GroupID))
236  continue;
237  if (In.Flags & DisableFlags)
238  continue;
239 
240  for (int I = 0; In.Prefixes[I]; I++) {
241  std::string S = std::string(In.Prefixes[I]) + std::string(In.Name) + "\t";
242  if (In.HelpText)
243  S += In.HelpText;
244  if (StringRef(S).startswith(Cur) && S != std::string(Cur) + "\t")
245  Ret.push_back(S);
246  }
247  }
248  return Ret;
249 }
250 
251 unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
252  unsigned FlagsToInclude, unsigned FlagsToExclude,
253  unsigned MinimumLength) const {
254  assert(!Option.empty());
255 
256  // Consider each [option prefix + option name] pair as a candidate, finding
257  // the closest match.
258  unsigned BestDistance = UINT_MAX;
259  for (const Info &CandidateInfo :
260  ArrayRef<Info>(OptionInfos).drop_front(FirstSearchableIndex)) {
261  StringRef CandidateName = CandidateInfo.Name;
262 
263  // We can eliminate some option prefix/name pairs as candidates right away:
264  // * Ignore option candidates with empty names, such as "--", or names
265  // that do not meet the minimum length.
266  if (CandidateName.empty() || CandidateName.size() < MinimumLength)
267  continue;
268 
269  // * If FlagsToInclude were specified, ignore options that don't include
270  // those flags.
271  if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
272  continue;
273  // * Ignore options that contain the FlagsToExclude.
274  if (CandidateInfo.Flags & FlagsToExclude)
275  continue;
276 
277  // * Ignore positional argument option candidates (which do not
278  // have prefixes).
279  if (!CandidateInfo.Prefixes)
280  continue;
281 
282  // Now check if the candidate ends with a character commonly used when
283  // delimiting an option from its value, such as '=' or ':'. If it does,
284  // attempt to split the given option based on that delimiter.
285  StringRef LHS, RHS;
286  char Last = CandidateName.back();
287  bool CandidateHasDelimiter = Last == '=' || Last == ':';
288  std::string NormalizedName = std::string(Option);
289  if (CandidateHasDelimiter) {
290  std::tie(LHS, RHS) = Option.split(Last);
291  NormalizedName = std::string(LHS);
292  if (Option.find(Last) == LHS.size())
293  NormalizedName += Last;
294  }
295 
296  // Consider each possible prefix for each candidate to find the most
297  // appropriate one. For example, if a user asks for "--helm", suggest
298  // "--help" over "-help".
299  for (int P = 0;
300  const char *const CandidatePrefix = CandidateInfo.Prefixes[P]; P++) {
301  std::string Candidate = (CandidatePrefix + CandidateName).str();
302  StringRef CandidateRef = Candidate;
303  unsigned Distance =
304  CandidateRef.edit_distance(NormalizedName, /*AllowReplacements=*/true,
305  /*MaxEditDistance=*/BestDistance);
306  if (RHS.empty() && CandidateHasDelimiter) {
307  // The Candidate ends with a = or : delimiter, but the option passed in
308  // didn't contain the delimiter (or doesn't have anything after it).
309  // In that case, penalize the correction: `-nodefaultlibs` is more
310  // likely to be a spello for `-nodefaultlib` than `-nodefaultlib:` even
311  // though both have an unmodified editing distance of 1, since the
312  // latter would need an argument.
313  ++Distance;
314  }
315  if (Distance < BestDistance) {
316  BestDistance = Distance;
317  NearestString = (Candidate + RHS).str();
318  }
319  }
320  }
321  return BestDistance;
322 }
323 
324 bool OptTable::addValues(const char *Option, const char *Values) {
325  for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
326  Info &In = OptionInfos[I];
327  if (optionMatches(In, Option)) {
328  In.Values = Values;
329  return true;
330  }
331  }
332  return false;
333 }
334 
335 // Parse a single argument, return the new argument, and update Index. If
336 // GroupedShortOptions is true, -a matches "-abc" and the argument in Args will
337 // be updated to "-bc". This overload does not support
338 // FlagsToInclude/FlagsToExclude or case insensitive options.
339 std::unique_ptr<Arg> OptTable::parseOneArgGrouped(InputArgList &Args,
340  unsigned &Index) const {
341  // Anything that doesn't start with PrefixesUnion is an input, as is '-'
342  // itself.
343  const char *CStr = Args.getArgString(Index);
344  StringRef Str(CStr);
345  if (isInput(PrefixesUnion, Str))
346  return std::make_unique<Arg>(getOption(InputOptionID), Str, Index++, CStr);
347 
348  const Info *End = OptionInfos.data() + OptionInfos.size();
349  StringRef Name = Str.ltrim(PrefixChars);
350  const Info *Start = std::lower_bound(
351  OptionInfos.data() + FirstSearchableIndex, End, Name.data());
352  const Info *Fallback = nullptr;
353  unsigned Prev = Index;
354 
355  // Search for the option which matches Str.
356  for (; Start != End; ++Start) {
357  unsigned ArgSize = matchOption(Start, Str, IgnoreCase);
358  if (!ArgSize)
359  continue;
360 
361  Option Opt(Start, this);
362  if (std::unique_ptr<Arg> A =
363  Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
364  /*GroupedShortOption=*/false, Index))
365  return A;
366 
367  // If Opt is a Flag of length 2 (e.g. "-a"), we know it is a prefix of
368  // the current argument (e.g. "-abc"). Match it as a fallback if no longer
369  // option (e.g. "-ab") exists.
370  if (ArgSize == 2 && Opt.getKind() == Option::FlagClass)
371  Fallback = Start;
372 
373  // Otherwise, see if the argument is missing.
374  if (Prev != Index)
375  return nullptr;
376  }
377  if (Fallback) {
378  Option Opt(Fallback, this);
379  // Check that the last option isn't a flag wrongly given an argument.
380  if (Str[2] == '=')
381  return std::make_unique<Arg>(getOption(UnknownOptionID), Str, Index++,
382  CStr);
383 
384  if (std::unique_ptr<Arg> A = Opt.accept(
385  Args, Str.substr(0, 2), /*GroupedShortOption=*/true, Index)) {
386  Args.replaceArgString(Index, Twine('-') + Str.substr(2));
387  return A;
388  }
389  }
390 
391  // In the case of an incorrect short option extract the character and move to
392  // the next one.
393  if (Str[1] != '-') {
394  CStr = Args.MakeArgString(Str.substr(0, 2));
395  Args.replaceArgString(Index, Twine('-') + Str.substr(2));
396  return std::make_unique<Arg>(getOption(UnknownOptionID), CStr, Index, CStr);
397  }
398 
399  return std::make_unique<Arg>(getOption(UnknownOptionID), Str, Index++, CStr);
400 }
401 
402 std::unique_ptr<Arg> OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
403  unsigned FlagsToInclude,
404  unsigned FlagsToExclude) const {
405  unsigned Prev = Index;
406  const char *Str = Args.getArgString(Index);
407 
408  // Anything that doesn't start with PrefixesUnion is an input, as is '-'
409  // itself.
410  if (isInput(PrefixesUnion, Str))
411  return std::make_unique<Arg>(getOption(InputOptionID), Str, Index++, Str);
412 
413  const Info *Start = OptionInfos.data() + FirstSearchableIndex;
414  const Info *End = OptionInfos.data() + OptionInfos.size();
415  StringRef Name = StringRef(Str).ltrim(PrefixChars);
416 
417  // Search for the first next option which could be a prefix.
418  Start = std::lower_bound(Start, End, Name.data());
419 
420  // Options are stored in sorted order, with '\0' at the end of the
421  // alphabet. Since the only options which can accept a string must
422  // prefix it, we iteratively search for the next option which could
423  // be a prefix.
424  //
425  // FIXME: This is searching much more than necessary, but I am
426  // blanking on the simplest way to make it fast. We can solve this
427  // problem when we move to TableGen.
428  for (; Start != End; ++Start) {
429  unsigned ArgSize = 0;
430  // Scan for first option which is a proper prefix.
431  for (; Start != End; ++Start)
432  if ((ArgSize = matchOption(Start, Str, IgnoreCase)))
433  break;
434  if (Start == End)
435  break;
436 
437  Option Opt(Start, this);
438 
439  if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
440  continue;
441  if (Opt.hasFlag(FlagsToExclude))
442  continue;
443 
444  // See if this option matches.
445  if (std::unique_ptr<Arg> A =
446  Opt.accept(Args, StringRef(Args.getArgString(Index), ArgSize),
447  /*GroupedShortOption=*/false, Index))
448  return A;
449 
450  // Otherwise, see if this argument was missing values.
451  if (Prev != Index)
452  return nullptr;
453  }
454 
455  // If we failed to find an option and this arg started with /, then it's
456  // probably an input path.
457  if (Str[0] == '/')
458  return std::make_unique<Arg>(getOption(InputOptionID), Str, Index++, Str);
459 
460  return std::make_unique<Arg>(getOption(UnknownOptionID), Str, Index++, Str);
461 }
462 
464  unsigned &MissingArgIndex,
465  unsigned &MissingArgCount,
466  unsigned FlagsToInclude,
467  unsigned FlagsToExclude) const {
468  InputArgList Args(ArgArr.begin(), ArgArr.end());
469 
470  // FIXME: Handle '@' args (or at least error on them).
471 
472  MissingArgIndex = MissingArgCount = 0;
473  unsigned Index = 0, End = ArgArr.size();
474  while (Index < End) {
475  // Ingore nullptrs, they are response file's EOL markers
476  if (Args.getArgString(Index) == nullptr) {
477  ++Index;
478  continue;
479  }
480  // Ignore empty arguments (other things may still take them as arguments).
481  StringRef Str = Args.getArgString(Index);
482  if (Str == "") {
483  ++Index;
484  continue;
485  }
486 
487  unsigned Prev = Index;
488  std::unique_ptr<Arg> A = GroupedShortOptions
489  ? parseOneArgGrouped(Args, Index)
490  : ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude);
491  assert((Index > Prev || GroupedShortOptions) &&
492  "Parser failed to consume argument.");
493 
494  // Check for missing argument error.
495  if (!A) {
496  assert(Index >= End && "Unexpected parser error.");
497  assert(Index - Prev - 1 && "No missing arguments!");
498  MissingArgIndex = Prev;
499  MissingArgCount = Index - Prev - 1;
500  break;
501  }
502 
503  Args.append(A.release());
504  }
505 
506  return Args;
507 }
508 
509 InputArgList OptTable::parseArgs(int Argc, char *const *Argv,
510  OptSpecifier Unknown, StringSaver &Saver,
511  function_ref<void(StringRef)> ErrorFn) const {
513  // The environment variable specifies initial options which can be overridden
514  // by commnad line options.
515  cl::expandResponseFiles(Argc, Argv, EnvVar, Saver, NewArgv);
516 
517  unsigned MAI, MAC;
518  opt::InputArgList Args = ParseArgs(makeArrayRef(NewArgv), MAI, MAC);
519  if (MAC)
520  ErrorFn((Twine(Args.getArgString(MAI)) + ": missing argument").str());
521 
522  // For each unknwon option, call ErrorFn with a formatted error message. The
523  // message includes a suggested alternative option spelling if available.
524  std::string Nearest;
525  for (const opt::Arg *A : Args.filtered(Unknown)) {
526  std::string Spelling = A->getAsString(Args);
527  if (findNearest(Spelling, Nearest) > 1)
528  ErrorFn("unknown argument '" + A->getAsString(Args) + "'");
529  else
530  ErrorFn("unknown argument '" + A->getAsString(Args) +
531  "', did you mean '" + Nearest + "'?");
532  }
533  return Args;
534 }
535 
536 static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
537  const Option O = Opts.getOption(Id);
538  std::string Name = O.getPrefixedName();
539 
540  // Add metavar, if used.
541  switch (O.getKind()) {
543  llvm_unreachable("Invalid option with help text.");
544 
546  if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) {
547  // For MultiArgs, metavar is full list of all argument names.
548  Name += ' ';
549  Name += MetaVarName;
550  }
551  else {
552  // For MultiArgs<N>, if metavar not supplied, print <value> N times.
553  for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) {
554  Name += " <value>";
555  }
556  }
557  break;
558 
559  case Option::FlagClass:
560  break;
561 
562  case Option::ValuesClass:
563  break;
564 
567  Name += ' ';
568  [[fallthrough]];
571  if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
572  Name += MetaVarName;
573  else
574  Name += "<value>";
575  break;
576  }
577 
578  return Name;
579 }
580 
581 namespace {
582 struct OptionInfo {
583  std::string Name;
584  StringRef HelpText;
585 };
586 } // namespace
587 
589  std::vector<OptionInfo> &OptionHelp) {
590  OS << Title << ":\n";
591 
592  // Find the maximum option length.
593  unsigned OptionFieldWidth = 0;
594  for (const OptionInfo &Opt : OptionHelp) {
595  // Limit the amount of padding we are willing to give up for alignment.
596  unsigned Length = Opt.Name.size();
597  if (Length <= 23)
598  OptionFieldWidth = std::max(OptionFieldWidth, Length);
599  }
600 
601  const unsigned InitialPad = 2;
602  for (const OptionInfo &Opt : OptionHelp) {
603  const std::string &Option = Opt.Name;
604  int Pad = OptionFieldWidth - int(Option.size());
605  OS.indent(InitialPad) << Option;
606 
607  // Break on long option names.
608  if (Pad < 0) {
609  OS << "\n";
610  Pad = OptionFieldWidth + InitialPad;
611  }
612  OS.indent(Pad + 1) << Opt.HelpText << '\n';
613  }
614 }
615 
616 static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
617  unsigned GroupID = Opts.getOptionGroupID(Id);
618 
619  // If not in a group, return the default help group.
620  if (!GroupID)
621  return "OPTIONS";
622 
623  // Abuse the help text of the option groups to store the "help group"
624  // name.
625  //
626  // FIXME: Split out option groups.
627  if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
628  return GroupHelp;
629 
630  // Otherwise keep looking.
631  return getOptionHelpGroup(Opts, GroupID);
632 }
633 
634 void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
635  bool ShowHidden, bool ShowAllAliases) const {
636  printHelp(OS, Usage, Title, /*Include*/ 0, /*Exclude*/
637  (ShowHidden ? 0 : HelpHidden), ShowAllAliases);
638 }
639 
640 void OptTable::printHelp(raw_ostream &OS, const char *Usage, const char *Title,
641  unsigned FlagsToInclude, unsigned FlagsToExclude,
642  bool ShowAllAliases) const {
643  OS << "OVERVIEW: " << Title << "\n\n";
644  OS << "USAGE: " << Usage << "\n\n";
645 
646  // Render help text into a map of group-name to a list of (option, help)
647  // pairs.
648  std::map<std::string, std::vector<OptionInfo>> GroupedOptionHelp;
649 
650  for (unsigned Id = 1, e = getNumOptions() + 1; Id != e; ++Id) {
651  // FIXME: Split out option groups.
653  continue;
654 
655  unsigned Flags = getInfo(Id).Flags;
656  if (FlagsToInclude && !(Flags & FlagsToInclude))
657  continue;
658  if (Flags & FlagsToExclude)
659  continue;
660 
661  // If an alias doesn't have a help text, show a help text for the aliased
662  // option instead.
663  const char *HelpText = getOptionHelpText(Id);
664  if (!HelpText && ShowAllAliases) {
665  const Option Alias = getOption(Id).getAlias();
666  if (Alias.isValid())
667  HelpText = getOptionHelpText(Alias.getID());
668  }
669 
670  if (HelpText && (strlen(HelpText) != 0)) {
671  const char *HelpGroup = getOptionHelpGroup(*this, Id);
672  const std::string &OptName = getOptionHelpName(*this, Id);
673  GroupedOptionHelp[HelpGroup].push_back({OptName, HelpText});
674  }
675  }
676 
677  for (auto& OptionGroup : GroupedOptionHelp) {
678  if (OptionGroup.first != GroupedOptionHelp.begin()->first)
679  OS << "\n";
680  PrintHelpOptionList(OS, OptionGroup.first, OptionGroup.second);
681  }
682 
683  OS.flush();
684 }
matchOption
static unsigned matchOption(const OptTable::Info *I, StringRef Str, bool IgnoreCase)
Definition: OptTable.cpp:180
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
Option.h
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:161
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::opt::Option::MultiArgClass
@ MultiArgClass
Definition: Option.h:64
llvm::opt::InputArgList
Definition: ArgList.h:383
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:161
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::PseudoProbeReservedId::Last
@ Last
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:1922
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:1199
llvm::StringRef::startswith_insensitive
bool startswith_insensitive(StringRef Prefix) const
Definition: StringRef.h:266
ErrorHandling.h
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:640
llvm::StringMap::end
iterator end()
Definition: StringMap.h:204
startswith
static bool startswith(StringRef Magic, const char(&S)[N])
Definition: Magic.cpp:28
optionMatches
static bool optionMatches(const OptTable::Info &In, StringRef Option)
Definition: OptTable.cpp:196
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:509
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:251
llvm::max
Expected< ExpressionValue > max(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:337
llvm::opt::Option::InputClass
@ InputClass
Definition: Option.h:55
llvm::StringSet::insert
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:34
llvm::MipsISD::Ret
@ Ret
Definition: MipsISelLowering.h:119
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
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
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:231
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:616
CommandLine.h
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
PrintHelpOptionList
static void PrintHelpOptionList(raw_ostream &OS, StringRef Title, std::vector< OptionInfo > &OptionHelp)
Definition: OptTable.cpp:588
llvm::StringRef::startswith
bool startswith(StringRef Prefix) const
Definition: StringRef.h:260
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::opt::OptSpecifier::OptSpecifier
OptSpecifier()=default
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:26
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
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
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::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
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: STLFunctionalExtras.h:36
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
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:324
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:264
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:402
Index
uint32_t Index
Definition: ELFObjHandler.cpp:83
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:53
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::StringRef::ltrim
StringRef ltrim(char Char) const
Return string with consecutive Char characters starting from the the left removed.
Definition: StringRef.h:784
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:1868
llvm::StringMap::begin
iterator begin()
Definition: StringMap.h:203
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:211
llvm::opt::OptTable::suggestValueCompletions
std::vector< std::string > suggestValueCompletions(StringRef Option, StringRef Arg) const
Find possible value for given flags.
Definition: OptTable.cpp:211
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:536
llvm::StringSet
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
OptSpecifier.h
Fallback
@ Fallback
Definition: WholeProgramDevirt.cpp:180
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:50
llvm::cl::expandResponseFiles
bool expandResponseFiles(int Argc, const char *const *Argv, const char *EnvVar, SmallVectorImpl< const char * > &NewArgv)
A convenience helper which concatenates the options specified by the environment variable EnvVar and ...
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
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::StringRef::size
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
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:463
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::opt::OptTable::~OptTable
~OptTable()
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
ArgList.h
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:152
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:169
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:475
llvm::raw_ostream::indent
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Definition: raw_ostream.cpp:494
N
#define N
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::StringRef::back
char back() const
back - Get the last character in the string.
Definition: StringRef.h:146
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
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:394
llvm::opt::Option::getAlias
const Option getAlias() const
Definition: Option.h:109
raw_ostream.h
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:241
llvm::opt::Option::JoinedClass
@ JoinedClass
Definition: Option.h:58
llvm::StringRef::split
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:693
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:153
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