LCOV - code coverage report
Current view: top level - lib/Option - OptTable.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 203 207 98.1 %
Date: 2018-07-13 00:08:38 Functions: 18 18 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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/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"
      15             : #include "llvm/Option/Option.h"
      16             : #include "llvm/Option/OptSpecifier.h"
      17             : #include "llvm/Option/OptTable.h"
      18             : #include "llvm/Support/Compiler.h"
      19             : #include "llvm/Support/ErrorHandling.h"
      20             : #include "llvm/Support/raw_ostream.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    19287089 : static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) {
      40             :   const char *X = A, *Y = B;
      41    19287089 :   char a = tolower(*A), b = tolower(*B);
      42    75009393 :   while (a == b) {
      43    29481043 :     if (a == '\0')
      44             :       return 0;
      45             : 
      46    27861152 :     a = tolower(*++X);
      47    27861152 :     b = tolower(*++Y);
      48             :   }
      49             : 
      50    17667198 :   if (a == '\0') // A is a prefix of B.
      51             :     return 1;
      52    17517979 :   if (b == '\0') // B is a prefix of A.
      53             :     return -1;
      54             : 
      55             :   // Otherwise lexicographic.
      56    16095300 :   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    19287086 :   return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0;
      91             : }
      92             : 
      93             : } // end namespace opt
      94             : } // end namespace llvm
      95             : 
      96         396 : OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
      97             : 
      98       67620 : OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase)
      99      137368 :     : 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     3721111 :   for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
     105     7440094 :     unsigned Kind = getInfo(i + 1).Kind;
     106     3720047 :     if (Kind == Option::InputClass) {
     107             :       assert(!TheInputOptionID && "Cannot have multiple input options!");
     108       67620 :       TheInputOptionID = getInfo(i + 1).ID;
     109     3652427 :     } else if (Kind == Option::UnknownClass) {
     110             :       assert(!TheUnknownOptionID && "Cannot have multiple unknown options!");
     111       67620 :       TheUnknownOptionID = getInfo(i + 1).ID;
     112     3584807 :     } else if (Kind != Option::GroupClass) {
     113       67620 :       FirstSearchableIndex = i;
     114       67620 :       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   146402076 :   for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1;
     141   146402076 :                 i != e; ++i) {
     142   146334456 :     if (const char *const *P = getInfo(i).Prefixes) {
     143   471045627 :       for (; *P != nullptr; ++P) {
     144             :         PrefixesUnion.insert(*P);
     145             :       }
     146             :     }
     147             :   }
     148             : 
     149             :   // Build prefix chars.
     150       67620 :   for (StringSet<>::const_iterator I = PrefixesUnion.begin(),
     151      268532 :                                    E = PrefixesUnion.end(); I != E; ++I) {
     152             :     StringRef Prefix = I->getKey();
     153      269570 :     for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end();
     154      470482 :                                    C != CE; ++C)
     155      269570 :       if (!is_contained(PrefixChars, *C))
     156      132846 :         PrefixChars.push_back(*C);
     157             :   }
     158       67620 : }
     159             : 
     160             : OptTable::~OptTable() = default;
     161             : 
     162   122553616 : const Option OptTable::getOption(OptSpecifier Opt) const {
     163             :   unsigned id = Opt.getID();
     164   122553616 :   if (id == 0)
     165    84309745 :     return Option(nullptr, nullptr);
     166             :   assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
     167    38243871 :   return Option(&getInfo(id), this);
     168             : }
     169             : 
     170     1737575 : static bool isInput(const StringSet<> &Prefixes, StringRef Arg) {
     171             :   if (Arg == "-")
     172             :     return true;
     173     1737401 :   for (StringSet<>::const_iterator I = Prefixes.begin(),
     174     3557924 :                                    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   118617391 : static unsigned matchOption(const OptTable::Info *I, StringRef Str,
     182             :                             bool IgnoreCase) {
     183   246716375 :   for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) {
     184             :     StringRef Prefix(*Pre);
     185             :     if (Str.startswith(Prefix)) {
     186    22963151 :       StringRef Rest = Str.substr(Prefix.size());
     187             :       bool Matched = IgnoreCase
     188    33163916 :           ? Rest.startswith_lower(I->Name)
     189    22852710 :           : Rest.startswith(I->Name);
     190      110442 :       if (Matched)
     191     3334738 :         return Prefix.size() + StringRef(I->Name).size();
     192             :     }
     193             :   }
     194             :   return 0;
     195             : }
     196             : 
     197             : // Returns true if one of the Prefixes + In.Names matches Option
     198   267699779 : static bool optionMatches(const OptTable::Info &In, StringRef Option) {
     199   267699779 :   if (In.Prefixes)
     200   844118122 :     for (size_t I = 0; In.Prefixes[I]; I++)
     201   865211642 :       if (Option == std::string(In.Prefixes[I]) + In.Name)
     202             :         return true;
     203             :   return false;
     204             : }
     205             : 
     206             : // This function is for flag value completion.
     207             : // Eg. When "-stdlib=" and "l" was passed to this function, it will return
     208             : // appropiriate values for stdlib, which starts with l.
     209             : std::vector<std::string>
     210          36 : OptTable::suggestValueCompletions(StringRef Option, StringRef Arg) const {
     211             :   // Search all options and return possible values.
     212      120672 :   for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
     213       60319 :     const Info &In = OptionInfos[I];
     214       60319 :     if (!In.Values || !optionMatches(In, Option))
     215       60300 :       continue;
     216             : 
     217             :     SmallVector<StringRef, 8> Candidates;
     218          38 :     StringRef(In.Values).split(Candidates, ",", -1, false);
     219             : 
     220          19 :     std::vector<std::string> Result;
     221         661 :     for (StringRef Val : Candidates)
     222             :       if (Val.startswith(Arg) && Arg.compare(Val))
     223         285 :         Result.push_back(Val);
     224             :     return Result;
     225             :   }
     226             :   return {};
     227             : }
     228             : 
     229             : std::vector<std::string>
     230          13 : OptTable::findByPrefix(StringRef Cur, unsigned short DisableFlags) const {
     231             :   std::vector<std::string> Ret;
     232       59228 :   for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
     233       29601 :     const Info &In = OptionInfos[I];
     234       29601 :     if (!In.Prefixes || (!In.HelpText && !In.GroupID))
     235        3991 :       continue;
     236       25610 :     if (In.Flags & DisableFlags)
     237        5744 :       continue;
     238             : 
     239       65452 :     for (int I = 0; In.Prefixes[I]; I++) {
     240       91172 :       std::string S = std::string(In.Prefixes[I]) + std::string(In.Name) + "\t";
     241       22793 :       if (In.HelpText)
     242             :         S += In.HelpText;
     243       50513 :       if (StringRef(S).startswith(Cur) && S.compare(std::string(Cur) + "\t"))
     244        4927 :         Ret.push_back(S);
     245             :     }
     246             :   }
     247          13 :   return Ret;
     248             : }
     249             : 
     250          98 : unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
     251             :                                unsigned FlagsToInclude, unsigned FlagsToExclude,
     252             :                                unsigned MinimumLength) const {
     253             :   assert(!Option.empty());
     254             : 
     255             :   // Consider each option as a candidate, finding the closest match.
     256             :   unsigned BestDistance = UINT_MAX;
     257      198358 :   for (const Info &CandidateInfo :
     258      198554 :        ArrayRef<Info>(OptionInfos).drop_front(FirstSearchableIndex)) {
     259      198358 :     StringRef CandidateName = CandidateInfo.Name;
     260             : 
     261             :     // Ignore option candidates with empty names, such as "--", or names
     262             :     // that do not meet the minimum length.
     263      198358 :     if (CandidateName.empty() || CandidateName.size() < MinimumLength)
     264      122382 :       continue;
     265             : 
     266             :     // If FlagsToInclude were specified, ignore options that don't include
     267             :     // those flags.
     268      178992 :     if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
     269       64513 :       continue;
     270             :     // Ignore options that contain the FlagsToExclude.
     271      114479 :     if (CandidateInfo.Flags & FlagsToExclude)
     272       19137 :       continue;
     273             : 
     274             :     // Ignore positional argument option candidates (which do not
     275             :     // have prefixes).
     276       95342 :     if (!CandidateInfo.Prefixes)
     277           0 :       continue;
     278             :     // Find the most appropriate prefix. For example, if a user asks for
     279             :     // "--helm", suggest "--help" over "-help".
     280       95342 :     StringRef Prefix = CandidateInfo.Prefixes[0];
     281      103262 :     for (int P = 1; CandidateInfo.Prefixes[P]; P++) {
     282             :       if (Option.startswith(CandidateInfo.Prefixes[P]))
     283        2139 :         Prefix = CandidateInfo.Prefixes[P];
     284             :     }
     285             : 
     286             :     // Check if the candidate ends with a character commonly used when
     287             :     // delimiting an option from its value, such as '=' or ':'. If it does,
     288             :     // attempt to split the given option based on that delimiter.
     289       95342 :     std::string Delimiter = "";
     290             :     char Last = CandidateName.back();
     291       95342 :     if (Last == '=' || Last == ':')
     292       38877 :       Delimiter = std::string(1, Last);
     293             : 
     294       95342 :     StringRef LHS, RHS;
     295       95342 :     if (Delimiter.empty())
     296       82383 :       LHS = Option;
     297             :     else
     298       12959 :       std::tie(LHS, RHS) = Option.split(Last);
     299             : 
     300             :     std::string NormalizedName =
     301      190684 :         (LHS.drop_front(Prefix.size()) + Delimiter).str();
     302             :     unsigned Distance =
     303       95342 :         CandidateName.edit_distance(NormalizedName, /*AllowReplacements=*/true,
     304       95342 :                                     /*MaxEditDistance=*/BestDistance);
     305       95342 :     if (Distance < BestDistance) {
     306             :       BestDistance = Distance;
     307         814 :       NearestString = (Prefix + CandidateName + RHS).str();
     308             :     }
     309             :   }
     310          98 :   return BestDistance;
     311             : }
     312             : 
     313      191754 : bool OptTable::addValues(const char *Option, const char *Values) {
     314   535380196 :   for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
     315   267688805 :     Info &In = OptionInfos[I];
     316   267688805 :     if (optionMatches(In, Option)) {
     317      191754 :       In.Values = Values;
     318      191754 :       return true;
     319             :     }
     320             :   }
     321             :   return false;
     322             : }
     323             : 
     324     1737575 : Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
     325             :                            unsigned FlagsToInclude,
     326             :                            unsigned FlagsToExclude) const {
     327     1737575 :   unsigned Prev = Index;
     328     1737575 :   const char *Str = Args.getArgString(Index);
     329             : 
     330             :   // Anything that doesn't start with PrefixesUnion is an input, as is '-'
     331             :   // itself.
     332     1737575 :   if (isInput(PrefixesUnion, Str))
     333       71046 :     return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
     334             : 
     335     1713893 :   const Info *Start = OptionInfos.data() + FirstSearchableIndex;
     336     1713893 :   const Info *End = OptionInfos.data() + OptionInfos.size();
     337     1713893 :   StringRef Name = StringRef(Str).ltrim(PrefixChars);
     338             : 
     339             :   // Search for the first next option which could be a prefix.
     340     3427462 :   Start = std::lower_bound(Start, End, Name.data());
     341             : 
     342             :   // Options are stored in sorted order, with '\0' at the end of the
     343             :   // alphabet. Since the only options which can accept a string must
     344             :   // prefix it, we iteratively search for the next option which could
     345             :   // be a prefix.
     346             :   //
     347             :   // FIXME: This is searching much more than necessary, but I am
     348             :   // blanking on the simplest way to make it fast. We can solve this
     349             :   // problem when we move to TableGen.
     350     1716895 :   for (; Start != End; ++Start) {
     351             :     unsigned ArgSize = 0;
     352             :     // Scan for first option which is a proper prefix.
     353   235616382 :     for (; Start != End; ++Start)
     354   237233958 :       if ((ArgSize = matchOption(Start, Str, IgnoreCase)))
     355             :         break;
     356     1715550 :     if (Start == End)
     357             :       break;
     358             : 
     359     1667369 :     Option Opt(Start, this);
     360             : 
     361     2509261 :     if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
     362         457 :       continue;
     363     3334382 :     if (Opt.hasFlag(FlagsToExclude))
     364         101 :       continue;
     365             : 
     366             :     // See if this option matches.
     367     1667090 :     if (Arg *A = Opt.accept(Args, Index, ArgSize))
     368     1665706 :       return A;
     369             : 
     370             :     // Otherwise, see if this argument was missing values.
     371        1398 :     if (Prev != Index)
     372             :       return nullptr;
     373             :   }
     374             : 
     375             :   // If we failed to find an option and this arg started with /, then it's
     376             :   // probably an input path.
     377       48187 :   if (Str[0] == '/')
     378      144132 :     return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
     379             : 
     380         429 :   return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
     381             : }
     382             : 
     383       70463 : InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr,
     384             :                                  unsigned &MissingArgIndex,
     385             :                                  unsigned &MissingArgCount,
     386             :                                  unsigned FlagsToInclude,
     387             :                                  unsigned FlagsToExclude) const {
     388       70463 :   InputArgList Args(ArgArr.begin(), ArgArr.end());
     389             : 
     390             :   // FIXME: Handle '@' args (or at least error on them).
     391             : 
     392       70463 :   MissingArgIndex = MissingArgCount = 0;
     393       70463 :   unsigned Index = 0, End = ArgArr.size();
     394     1808183 :   while (Index < End) {
     395             :     // Ingore nullptrs, they are response file's EOL markers
     396     1737890 :     if (Args.getArgString(Index) == nullptr) {
     397         159 :       ++Index;
     398         159 :       continue;
     399             :     }
     400             :     // Ignore empty arguments (other things may still take them as arguments).
     401             :     StringRef Str = Args.getArgString(Index);
     402          32 :     if (Str == "") {
     403          32 :       ++Index;
     404          32 :       continue;
     405             :     }
     406             : 
     407             :     unsigned Prev = Index;
     408     1737540 :     Arg *A = ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude);
     409             :     assert(Index > Prev && "Parser failed to consume argument.");
     410             : 
     411             :     // Check for missing argument error.
     412     1737540 :     if (!A) {
     413             :       assert(Index >= End && "Unexpected parser error.");
     414             :       assert(Index - Prev - 1 && "No missing arguments!");
     415          11 :       MissingArgIndex = Prev;
     416          11 :       MissingArgCount = Index - Prev - 1;
     417             :       break;
     418             :     }
     419             : 
     420     1737529 :     Args.append(A);
     421             :   }
     422             : 
     423       70463 :   return Args;
     424             : }
     425             : 
     426        3737 : static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
     427        3737 :   const Option O = Opts.getOption(Id);
     428        3737 :   std::string Name = O.getPrefixedName();
     429             : 
     430             :   // Add metavar, if used.
     431        7474 :   switch (O.getKind()) {
     432           0 :   case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
     433           0 :     llvm_unreachable("Invalid option with help text.");
     434             : 
     435           2 :   case Option::MultiArgClass:
     436           2 :     if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) {
     437             :       // For MultiArgs, metavar is full list of all argument names.
     438             :       Name += ' ';
     439             :       Name += MetaVarName;
     440             :     }
     441             :     else {
     442             :       // For MultiArgs<N>, if metavar not supplied, print <value> N times.
     443           0 :       for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) {
     444             :         Name += " <value>";
     445             :       }
     446             :     }
     447             :     break;
     448             : 
     449             :   case Option::FlagClass:
     450             :     break;
     451             : 
     452             :   case Option::ValuesClass:
     453             :     break;
     454             : 
     455             :   case Option::SeparateClass: case Option::JoinedOrSeparateClass:
     456             :   case Option::RemainingArgsClass: case Option::RemainingArgsJoinedClass:
     457             :     Name += ' ';
     458             :     LLVM_FALLTHROUGH;
     459        1252 :   case Option::JoinedClass: case Option::CommaJoinedClass:
     460             :   case Option::JoinedAndSeparateClass:
     461        1252 :     if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
     462             :       Name += MetaVarName;
     463             :     else
     464             :       Name += "<value>";
     465             :     break;
     466             :   }
     467             : 
     468        3737 :   return Name;
     469             : }
     470             : 
     471             : namespace {
     472       29508 : struct OptionInfo {
     473             :   std::string Name;
     474             :   StringRef HelpText;
     475             : };
     476             : } // namespace
     477             : 
     478          34 : static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
     479             :                                 std::vector<OptionInfo> &OptionHelp) {
     480          34 :   OS << Title << ":\n";
     481             : 
     482             :   // Find the maximum option length.
     483          34 :   unsigned OptionFieldWidth = 0;
     484        3805 :   for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
     485             :     // Limit the amount of padding we are willing to give up for alignment.
     486        7474 :     unsigned Length = OptionHelp[i].Name.size();
     487        3737 :     if (Length <= 23)
     488        2740 :       OptionFieldWidth = std::max(OptionFieldWidth, Length);
     489             :   }
     490             : 
     491             :   const unsigned InitialPad = 2;
     492        7508 :   for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
     493        3737 :     const std::string &Option = OptionHelp[i].Name;
     494        3737 :     int Pad = OptionFieldWidth - int(Option.size());
     495        3737 :     OS.indent(InitialPad) << Option;
     496             : 
     497             :     // Break on long option names.
     498        3737 :     if (Pad < 0) {
     499         997 :       OS << "\n";
     500         997 :       Pad = OptionFieldWidth + InitialPad;
     501             :     }
     502        7474 :     OS.indent(Pad + 1) << OptionHelp[i].HelpText << '\n';
     503             :   }
     504          34 : }
     505             : 
     506        8131 : static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
     507             :   unsigned GroupID = Opts.getOptionGroupID(Id);
     508             : 
     509             :   // If not in a group, return the default help group.
     510        8131 :   if (!GroupID)
     511             :     return "OPTIONS";
     512             : 
     513             :   // Abuse the help text of the option groups to store the "help group"
     514             :   // name.
     515             :   //
     516             :   // FIXME: Split out option groups.
     517        4803 :   if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
     518             :     return GroupHelp;
     519             : 
     520             :   // Otherwise keep looking.
     521        4394 :   return getOptionHelpGroup(Opts, GroupID);
     522             : }
     523             : 
     524          15 : void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
     525             :                          bool ShowHidden, bool ShowAllAliases) const {
     526          15 :   PrintHelp(OS, Name, Title, /*Include*/ 0, /*Exclude*/
     527             :             (ShowHidden ? 0 : HelpHidden), ShowAllAliases);
     528          15 : }
     529             : 
     530          22 : void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
     531             :                          unsigned FlagsToInclude, unsigned FlagsToExclude,
     532             :                          bool ShowAllAliases) const {
     533          22 :   OS << "OVERVIEW: " << Title << "\n";
     534             :   OS << '\n';
     535          22 :   OS << "USAGE: " << Name << " [options] <inputs>\n";
     536             :   OS << '\n';
     537             : 
     538             :   // Render help text into a map of group-name to a list of (option, help)
     539             :   // pairs.
     540             :   std::map<std::string, std::vector<OptionInfo>> GroupedOptionHelp;
     541             : 
     542       17651 :   for (unsigned Id = 1, e = getNumOptions() + 1; Id != e; ++Id) {
     543             :     // FIXME: Split out option groups.
     544       17629 :     if (getOptionKind(Id) == Option::GroupClass)
     545         394 :       continue;
     546             : 
     547       17235 :     unsigned Flags = getInfo(Id).Flags;
     548       17235 :     if (FlagsToInclude && !(Flags & FlagsToInclude))
     549        6950 :       continue;
     550       10285 :     if (Flags & FlagsToExclude)
     551        1739 :       continue;
     552             : 
     553             :     // If an alias doesn't have a help text, show a help text for the aliased
     554             :     // option instead.
     555             :     const char *HelpText = getOptionHelpText(Id);
     556        8546 :     if (!HelpText && ShowAllAliases) {
     557         336 :       const Option Alias = getOption(Id).getAlias();
     558         168 :       if (Alias.isValid())
     559             :         HelpText = getOptionHelpText(Alias.getID());
     560             :     }
     561             : 
     562        8546 :     if (HelpText) {
     563        3737 :       const char *HelpGroup = getOptionHelpGroup(*this, Id);
     564        3737 :       const std::string &OptName = getOptionHelpName(*this, Id);
     565       11211 :       GroupedOptionHelp[HelpGroup].push_back({OptName, HelpText});
     566             :     }
     567             :   }
     568             : 
     569          56 :   for (auto& OptionGroup : GroupedOptionHelp) {
     570          68 :     if (OptionGroup.first != GroupedOptionHelp.begin()->first)
     571          12 :       OS << "\n";
     572          68 :     PrintHelpOptionList(OS, OptionGroup.first, OptionGroup.second);
     573             :   }
     574             : 
     575             :   OS.flush();
     576          22 : }

Generated by: LCOV version 1.13