124d58133SDimitry Andric //===- OptTable.cpp - Option Table Implementation -------------------------===//
2139f7f9bSDimitry Andric //
3139f7f9bSDimitry Andric // The LLVM Compiler Infrastructure
4139f7f9bSDimitry Andric //
5139f7f9bSDimitry Andric // This file is distributed under the University of Illinois Open Source
6139f7f9bSDimitry Andric // License. See LICENSE.TXT for details.
7139f7f9bSDimitry Andric //
8139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
9139f7f9bSDimitry Andric
10d88c1a5aSDimitry Andric #include "llvm/ADT/STLExtras.h"
1124d58133SDimitry Andric #include "llvm/ADT/StringRef.h"
1224d58133SDimitry Andric #include "llvm/ADT/StringSet.h"
13139f7f9bSDimitry Andric #include "llvm/Option/Arg.h"
14139f7f9bSDimitry Andric #include "llvm/Option/ArgList.h"
15139f7f9bSDimitry Andric #include "llvm/Option/Option.h"
1624d58133SDimitry Andric #include "llvm/Option/OptSpecifier.h"
1724d58133SDimitry Andric #include "llvm/Option/OptTable.h"
1824d58133SDimitry Andric #include "llvm/Support/Compiler.h"
19139f7f9bSDimitry Andric #include "llvm/Support/ErrorHandling.h"
20139f7f9bSDimitry Andric #include "llvm/Support/raw_ostream.h"
21139f7f9bSDimitry Andric #include <algorithm>
2224d58133SDimitry Andric #include <cassert>
23f785676fSDimitry Andric #include <cctype>
2424d58133SDimitry Andric #include <cstring>
25139f7f9bSDimitry Andric #include <map>
2624d58133SDimitry Andric #include <string>
2724d58133SDimitry Andric #include <utility>
2824d58133SDimitry Andric #include <vector>
29139f7f9bSDimitry Andric
30139f7f9bSDimitry Andric using namespace llvm;
31139f7f9bSDimitry Andric using namespace llvm::opt;
32139f7f9bSDimitry Andric
33f785676fSDimitry Andric namespace llvm {
34f785676fSDimitry Andric namespace opt {
35139f7f9bSDimitry Andric
36f785676fSDimitry Andric // Ordering on Info. The ordering is *almost* case-insensitive lexicographic,
372cab237bSDimitry Andric // with an exception. '\0' comes at the end of the alphabet instead of the
38f785676fSDimitry Andric // beginning (thus options precede any other options which prefix them).
StrCmpOptionNameIgnoreCase(const char * A,const char * B)39f785676fSDimitry Andric static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) {
40f785676fSDimitry Andric const char *X = A, *Y = B;
41f785676fSDimitry Andric char a = tolower(*A), b = tolower(*B);
42139f7f9bSDimitry Andric while (a == b) {
43139f7f9bSDimitry Andric if (a == '\0')
44139f7f9bSDimitry Andric return 0;
45139f7f9bSDimitry Andric
46f785676fSDimitry Andric a = tolower(*++X);
47f785676fSDimitry Andric b = tolower(*++Y);
48139f7f9bSDimitry Andric }
49139f7f9bSDimitry Andric
50139f7f9bSDimitry Andric if (a == '\0') // A is a prefix of B.
51139f7f9bSDimitry Andric return 1;
52139f7f9bSDimitry Andric if (b == '\0') // B is a prefix of A.
53139f7f9bSDimitry Andric return -1;
54139f7f9bSDimitry Andric
55139f7f9bSDimitry Andric // Otherwise lexicographic.
56139f7f9bSDimitry Andric return (a < b) ? -1 : 1;
57139f7f9bSDimitry Andric }
58139f7f9bSDimitry Andric
59f785676fSDimitry Andric #ifndef NDEBUG
StrCmpOptionName(const char * A,const char * B)60f785676fSDimitry Andric static int StrCmpOptionName(const char *A, const char *B) {
61f785676fSDimitry Andric if (int N = StrCmpOptionNameIgnoreCase(A, B))
62f785676fSDimitry Andric return N;
63f785676fSDimitry Andric return strcmp(A, B);
64f785676fSDimitry Andric }
65139f7f9bSDimitry Andric
operator <(const OptTable::Info & A,const OptTable::Info & B)66139f7f9bSDimitry Andric static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
67139f7f9bSDimitry Andric if (&A == &B)
68139f7f9bSDimitry Andric return false;
69139f7f9bSDimitry Andric
70139f7f9bSDimitry Andric if (int N = StrCmpOptionName(A.Name, B.Name))
71f785676fSDimitry Andric return N < 0;
72139f7f9bSDimitry Andric
73139f7f9bSDimitry Andric for (const char * const *APre = A.Prefixes,
74139f7f9bSDimitry Andric * const *BPre = B.Prefixes;
7591bc56edSDimitry Andric *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){
76139f7f9bSDimitry Andric if (int N = StrCmpOptionName(*APre, *BPre))
77f785676fSDimitry Andric return N < 0;
78139f7f9bSDimitry Andric }
79139f7f9bSDimitry Andric
80139f7f9bSDimitry Andric // Names are the same, check that classes are in order; exactly one
81139f7f9bSDimitry Andric // should be joined, and it should succeed the other.
82139f7f9bSDimitry Andric assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
83139f7f9bSDimitry Andric "Unexpected classes for options with same name.");
84139f7f9bSDimitry Andric return B.Kind == Option::JoinedClass;
85139f7f9bSDimitry Andric }
86f785676fSDimitry Andric #endif
87139f7f9bSDimitry Andric
88139f7f9bSDimitry Andric // Support lower_bound between info and an option name.
operator <(const OptTable::Info & I,const char * Name)89139f7f9bSDimitry Andric static inline bool operator<(const OptTable::Info &I, const char *Name) {
90f785676fSDimitry Andric return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0;
91139f7f9bSDimitry Andric }
9224d58133SDimitry Andric
9324d58133SDimitry Andric } // end namespace opt
9424d58133SDimitry Andric } // end namespace llvm
95139f7f9bSDimitry Andric
OptSpecifier(const Option * Opt)96139f7f9bSDimitry Andric OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
97139f7f9bSDimitry Andric
OptTable(ArrayRef<Info> OptionInfos,bool IgnoreCase)987d523365SDimitry Andric OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase)
9924d58133SDimitry Andric : OptionInfos(OptionInfos), IgnoreCase(IgnoreCase) {
100139f7f9bSDimitry Andric // Explicitly zero initialize the error to work around a bug in array
101139f7f9bSDimitry Andric // value-initialization on MinGW with gcc 4.3.5.
102139f7f9bSDimitry Andric
103139f7f9bSDimitry Andric // Find start of normal options.
104139f7f9bSDimitry Andric for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
105139f7f9bSDimitry Andric unsigned Kind = getInfo(i + 1).Kind;
106139f7f9bSDimitry Andric if (Kind == Option::InputClass) {
107139f7f9bSDimitry Andric assert(!TheInputOptionID && "Cannot have multiple input options!");
108139f7f9bSDimitry Andric TheInputOptionID = getInfo(i + 1).ID;
109139f7f9bSDimitry Andric } else if (Kind == Option::UnknownClass) {
110139f7f9bSDimitry Andric assert(!TheUnknownOptionID && "Cannot have multiple unknown options!");
111139f7f9bSDimitry Andric TheUnknownOptionID = getInfo(i + 1).ID;
112139f7f9bSDimitry Andric } else if (Kind != Option::GroupClass) {
113139f7f9bSDimitry Andric FirstSearchableIndex = i;
114139f7f9bSDimitry Andric break;
115139f7f9bSDimitry Andric }
116139f7f9bSDimitry Andric }
117139f7f9bSDimitry Andric assert(FirstSearchableIndex != 0 && "No searchable options?");
118139f7f9bSDimitry Andric
119139f7f9bSDimitry Andric #ifndef NDEBUG
120139f7f9bSDimitry Andric // Check that everything after the first searchable option is a
121139f7f9bSDimitry Andric // regular option class.
122139f7f9bSDimitry Andric for (unsigned i = FirstSearchableIndex, e = getNumOptions(); i != e; ++i) {
123139f7f9bSDimitry Andric Option::OptionClass Kind = (Option::OptionClass) getInfo(i + 1).Kind;
124139f7f9bSDimitry Andric assert((Kind != Option::InputClass && Kind != Option::UnknownClass &&
125139f7f9bSDimitry Andric Kind != Option::GroupClass) &&
126139f7f9bSDimitry Andric "Special options should be defined first!");
127139f7f9bSDimitry Andric }
128139f7f9bSDimitry Andric
129139f7f9bSDimitry Andric // Check that options are in order.
130139f7f9bSDimitry Andric for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){
131139f7f9bSDimitry Andric if (!(getInfo(i) < getInfo(i + 1))) {
132139f7f9bSDimitry Andric getOption(i).dump();
133139f7f9bSDimitry Andric getOption(i + 1).dump();
134139f7f9bSDimitry Andric llvm_unreachable("Options are not in order!");
135139f7f9bSDimitry Andric }
136139f7f9bSDimitry Andric }
137139f7f9bSDimitry Andric #endif
138139f7f9bSDimitry Andric
139139f7f9bSDimitry Andric // Build prefixes.
140139f7f9bSDimitry Andric for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions() + 1;
141139f7f9bSDimitry Andric i != e; ++i) {
142139f7f9bSDimitry Andric if (const char *const *P = getInfo(i).Prefixes) {
14391bc56edSDimitry Andric for (; *P != nullptr; ++P) {
144139f7f9bSDimitry Andric PrefixesUnion.insert(*P);
145139f7f9bSDimitry Andric }
146139f7f9bSDimitry Andric }
147139f7f9bSDimitry Andric }
148139f7f9bSDimitry Andric
149139f7f9bSDimitry Andric // Build prefix chars.
15024d58133SDimitry Andric for (StringSet<>::const_iterator I = PrefixesUnion.begin(),
151139f7f9bSDimitry Andric E = PrefixesUnion.end(); I != E; ++I) {
152139f7f9bSDimitry Andric StringRef Prefix = I->getKey();
153139f7f9bSDimitry Andric for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end();
154139f7f9bSDimitry Andric C != CE; ++C)
155d88c1a5aSDimitry Andric if (!is_contained(PrefixChars, *C))
156139f7f9bSDimitry Andric PrefixChars.push_back(*C);
157139f7f9bSDimitry Andric }
158139f7f9bSDimitry Andric }
159139f7f9bSDimitry Andric
16024d58133SDimitry Andric OptTable::~OptTable() = default;
161139f7f9bSDimitry Andric
getOption(OptSpecifier Opt) const162139f7f9bSDimitry Andric const Option OptTable::getOption(OptSpecifier Opt) const {
163139f7f9bSDimitry Andric unsigned id = Opt.getID();
164139f7f9bSDimitry Andric if (id == 0)
16591bc56edSDimitry Andric return Option(nullptr, nullptr);
166139f7f9bSDimitry Andric assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
167139f7f9bSDimitry Andric return Option(&getInfo(id), this);
168139f7f9bSDimitry Andric }
169139f7f9bSDimitry Andric
isInput(const StringSet<> & Prefixes,StringRef Arg)17024d58133SDimitry Andric static bool isInput(const StringSet<> &Prefixes, StringRef Arg) {
171139f7f9bSDimitry Andric if (Arg == "-")
172139f7f9bSDimitry Andric return true;
17324d58133SDimitry Andric for (StringSet<>::const_iterator I = Prefixes.begin(),
174139f7f9bSDimitry Andric E = Prefixes.end(); I != E; ++I)
175139f7f9bSDimitry Andric if (Arg.startswith(I->getKey()))
176139f7f9bSDimitry Andric return false;
177139f7f9bSDimitry Andric return true;
178139f7f9bSDimitry Andric }
179139f7f9bSDimitry Andric
180139f7f9bSDimitry Andric /// \returns Matched size. 0 means no match.
matchOption(const OptTable::Info * I,StringRef Str,bool IgnoreCase)181f785676fSDimitry Andric static unsigned matchOption(const OptTable::Info *I, StringRef Str,
182f785676fSDimitry Andric bool IgnoreCase) {
18391bc56edSDimitry Andric for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) {
184139f7f9bSDimitry Andric StringRef Prefix(*Pre);
185f785676fSDimitry Andric if (Str.startswith(Prefix)) {
186f785676fSDimitry Andric StringRef Rest = Str.substr(Prefix.size());
187f785676fSDimitry Andric bool Matched = IgnoreCase
188f785676fSDimitry Andric ? Rest.startswith_lower(I->Name)
189f785676fSDimitry Andric : Rest.startswith(I->Name);
190f785676fSDimitry Andric if (Matched)
191139f7f9bSDimitry Andric return Prefix.size() + StringRef(I->Name).size();
192139f7f9bSDimitry Andric }
193f785676fSDimitry Andric }
194139f7f9bSDimitry Andric return 0;
195139f7f9bSDimitry Andric }
196139f7f9bSDimitry Andric
197edd7eaddSDimitry Andric // Returns true if one of the Prefixes + In.Names matches Option
optionMatches(const OptTable::Info & In,StringRef Option)198edd7eaddSDimitry Andric static bool optionMatches(const OptTable::Info &In, StringRef Option) {
1992cab237bSDimitry Andric if (In.Prefixes)
200edd7eaddSDimitry Andric for (size_t I = 0; In.Prefixes[I]; I++)
201edd7eaddSDimitry Andric if (Option == std::string(In.Prefixes[I]) + In.Name)
202edd7eaddSDimitry Andric return true;
203edd7eaddSDimitry Andric return false;
204edd7eaddSDimitry Andric }
205edd7eaddSDimitry Andric
206edd7eaddSDimitry Andric // This function is for flag value completion.
207edd7eaddSDimitry Andric // Eg. When "-stdlib=" and "l" was passed to this function, it will return
208edd7eaddSDimitry Andric // appropiriate values for stdlib, which starts with l.
209edd7eaddSDimitry Andric std::vector<std::string>
suggestValueCompletions(StringRef Option,StringRef Arg) const210edd7eaddSDimitry Andric OptTable::suggestValueCompletions(StringRef Option, StringRef Arg) const {
211edd7eaddSDimitry Andric // Search all options and return possible values.
2122cab237bSDimitry Andric for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
2132cab237bSDimitry Andric const Info &In = OptionInfos[I];
2142cab237bSDimitry Andric if (!In.Values || !optionMatches(In, Option))
215edd7eaddSDimitry Andric continue;
216edd7eaddSDimitry Andric
217edd7eaddSDimitry Andric SmallVector<StringRef, 8> Candidates;
218edd7eaddSDimitry Andric StringRef(In.Values).split(Candidates, ",", -1, false);
219edd7eaddSDimitry Andric
220edd7eaddSDimitry Andric std::vector<std::string> Result;
221edd7eaddSDimitry Andric for (StringRef Val : Candidates)
2224ba319b5SDimitry Andric if (Val.startswith(Arg) && Arg.compare(Val))
223edd7eaddSDimitry Andric Result.push_back(Val);
224edd7eaddSDimitry Andric return Result;
225edd7eaddSDimitry Andric }
226edd7eaddSDimitry Andric return {};
227edd7eaddSDimitry Andric }
228edd7eaddSDimitry Andric
229c4394386SDimitry Andric std::vector<std::string>
findByPrefix(StringRef Cur,unsigned short DisableFlags) const230c4394386SDimitry Andric OptTable::findByPrefix(StringRef Cur, unsigned short DisableFlags) const {
231302affcbSDimitry Andric std::vector<std::string> Ret;
2322cab237bSDimitry Andric for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
2332cab237bSDimitry Andric const Info &In = OptionInfos[I];
234c4394386SDimitry Andric if (!In.Prefixes || (!In.HelpText && !In.GroupID))
235302affcbSDimitry Andric continue;
236c4394386SDimitry Andric if (In.Flags & DisableFlags)
237c4394386SDimitry Andric continue;
238c4394386SDimitry Andric
239302affcbSDimitry Andric for (int I = 0; In.Prefixes[I]; I++) {
24037cd60a3SDimitry Andric std::string S = std::string(In.Prefixes[I]) + std::string(In.Name) + "\t";
24137cd60a3SDimitry Andric if (In.HelpText)
24237cd60a3SDimitry Andric S += In.HelpText;
2434ba319b5SDimitry Andric if (StringRef(S).startswith(Cur) && S.compare(std::string(Cur) + "\t"))
244302affcbSDimitry Andric Ret.push_back(S);
245302affcbSDimitry Andric }
246302affcbSDimitry Andric }
247302affcbSDimitry Andric return Ret;
248302affcbSDimitry Andric }
249302affcbSDimitry Andric
findNearest(StringRef Option,std::string & NearestString,unsigned FlagsToInclude,unsigned FlagsToExclude,unsigned MinimumLength) const2504ba319b5SDimitry Andric unsigned OptTable::findNearest(StringRef Option, std::string &NearestString,
2514ba319b5SDimitry Andric unsigned FlagsToInclude, unsigned FlagsToExclude,
2524ba319b5SDimitry Andric unsigned MinimumLength) const {
2534ba319b5SDimitry Andric assert(!Option.empty());
2544ba319b5SDimitry Andric
2554ba319b5SDimitry Andric // Consider each option as a candidate, finding the closest match.
2564ba319b5SDimitry Andric unsigned BestDistance = UINT_MAX;
2574ba319b5SDimitry Andric for (const Info &CandidateInfo :
2584ba319b5SDimitry Andric ArrayRef<Info>(OptionInfos).drop_front(FirstSearchableIndex)) {
2594ba319b5SDimitry Andric StringRef CandidateName = CandidateInfo.Name;
2604ba319b5SDimitry Andric
2614ba319b5SDimitry Andric // Ignore option candidates with empty names, such as "--", or names
2624ba319b5SDimitry Andric // that do not meet the minimum length.
2634ba319b5SDimitry Andric if (CandidateName.empty() || CandidateName.size() < MinimumLength)
2644ba319b5SDimitry Andric continue;
2654ba319b5SDimitry Andric
2664ba319b5SDimitry Andric // If FlagsToInclude were specified, ignore options that don't include
2674ba319b5SDimitry Andric // those flags.
2684ba319b5SDimitry Andric if (FlagsToInclude && !(CandidateInfo.Flags & FlagsToInclude))
2694ba319b5SDimitry Andric continue;
2704ba319b5SDimitry Andric // Ignore options that contain the FlagsToExclude.
2714ba319b5SDimitry Andric if (CandidateInfo.Flags & FlagsToExclude)
2724ba319b5SDimitry Andric continue;
2734ba319b5SDimitry Andric
2744ba319b5SDimitry Andric // Ignore positional argument option candidates (which do not
2754ba319b5SDimitry Andric // have prefixes).
2764ba319b5SDimitry Andric if (!CandidateInfo.Prefixes)
2774ba319b5SDimitry Andric continue;
2784ba319b5SDimitry Andric // Find the most appropriate prefix. For example, if a user asks for
2794ba319b5SDimitry Andric // "--helm", suggest "--help" over "-help".
2804ba319b5SDimitry Andric StringRef Prefix = CandidateInfo.Prefixes[0];
2814ba319b5SDimitry Andric for (int P = 1; CandidateInfo.Prefixes[P]; P++) {
2824ba319b5SDimitry Andric if (Option.startswith(CandidateInfo.Prefixes[P]))
2834ba319b5SDimitry Andric Prefix = CandidateInfo.Prefixes[P];
2844ba319b5SDimitry Andric }
2854ba319b5SDimitry Andric
2864ba319b5SDimitry Andric // Check if the candidate ends with a character commonly used when
2874ba319b5SDimitry Andric // delimiting an option from its value, such as '=' or ':'. If it does,
2884ba319b5SDimitry Andric // attempt to split the given option based on that delimiter.
2894ba319b5SDimitry Andric std::string Delimiter = "";
2904ba319b5SDimitry Andric char Last = CandidateName.back();
2914ba319b5SDimitry Andric if (Last == '=' || Last == ':')
2924ba319b5SDimitry Andric Delimiter = std::string(1, Last);
2934ba319b5SDimitry Andric
2944ba319b5SDimitry Andric StringRef LHS, RHS;
2954ba319b5SDimitry Andric if (Delimiter.empty())
2964ba319b5SDimitry Andric LHS = Option;
2974ba319b5SDimitry Andric else
2984ba319b5SDimitry Andric std::tie(LHS, RHS) = Option.split(Last);
2994ba319b5SDimitry Andric
3004ba319b5SDimitry Andric std::string NormalizedName =
3014ba319b5SDimitry Andric (LHS.drop_front(Prefix.size()) + Delimiter).str();
3024ba319b5SDimitry Andric unsigned Distance =
3034ba319b5SDimitry Andric CandidateName.edit_distance(NormalizedName, /*AllowReplacements=*/true,
3044ba319b5SDimitry Andric /*MaxEditDistance=*/BestDistance);
3054ba319b5SDimitry Andric if (Distance < BestDistance) {
3064ba319b5SDimitry Andric BestDistance = Distance;
3074ba319b5SDimitry Andric NearestString = (Prefix + CandidateName + RHS).str();
3084ba319b5SDimitry Andric }
3094ba319b5SDimitry Andric }
3104ba319b5SDimitry Andric return BestDistance;
3114ba319b5SDimitry Andric }
3124ba319b5SDimitry Andric
addValues(const char * Option,const char * Values)3132cab237bSDimitry Andric bool OptTable::addValues(const char *Option, const char *Values) {
3142cab237bSDimitry Andric for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
3152cab237bSDimitry Andric Info &In = OptionInfos[I];
3162cab237bSDimitry Andric if (optionMatches(In, Option)) {
3172cab237bSDimitry Andric In.Values = Values;
3182cab237bSDimitry Andric return true;
3192cab237bSDimitry Andric }
3202cab237bSDimitry Andric }
3212cab237bSDimitry Andric return false;
3222cab237bSDimitry Andric }
3232cab237bSDimitry Andric
ParseOneArg(const ArgList & Args,unsigned & Index,unsigned FlagsToInclude,unsigned FlagsToExclude) const324f785676fSDimitry Andric Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
325f785676fSDimitry Andric unsigned FlagsToInclude,
326f785676fSDimitry Andric unsigned FlagsToExclude) const {
327139f7f9bSDimitry Andric unsigned Prev = Index;
328139f7f9bSDimitry Andric const char *Str = Args.getArgString(Index);
329139f7f9bSDimitry Andric
330139f7f9bSDimitry Andric // Anything that doesn't start with PrefixesUnion is an input, as is '-'
331139f7f9bSDimitry Andric // itself.
332139f7f9bSDimitry Andric if (isInput(PrefixesUnion, Str))
333139f7f9bSDimitry Andric return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
334139f7f9bSDimitry Andric
3352cab237bSDimitry Andric const Info *Start = OptionInfos.data() + FirstSearchableIndex;
3362cab237bSDimitry Andric const Info *End = OptionInfos.data() + OptionInfos.size();
337139f7f9bSDimitry Andric StringRef Name = StringRef(Str).ltrim(PrefixChars);
338139f7f9bSDimitry Andric
339139f7f9bSDimitry Andric // Search for the first next option which could be a prefix.
340139f7f9bSDimitry Andric Start = std::lower_bound(Start, End, Name.data());
341139f7f9bSDimitry Andric
342139f7f9bSDimitry Andric // Options are stored in sorted order, with '\0' at the end of the
343139f7f9bSDimitry Andric // alphabet. Since the only options which can accept a string must
344139f7f9bSDimitry Andric // prefix it, we iteratively search for the next option which could
345139f7f9bSDimitry Andric // be a prefix.
346139f7f9bSDimitry Andric //
347139f7f9bSDimitry Andric // FIXME: This is searching much more than necessary, but I am
348139f7f9bSDimitry Andric // blanking on the simplest way to make it fast. We can solve this
349139f7f9bSDimitry Andric // problem when we move to TableGen.
350139f7f9bSDimitry Andric for (; Start != End; ++Start) {
351139f7f9bSDimitry Andric unsigned ArgSize = 0;
352139f7f9bSDimitry Andric // Scan for first option which is a proper prefix.
353139f7f9bSDimitry Andric for (; Start != End; ++Start)
354f785676fSDimitry Andric if ((ArgSize = matchOption(Start, Str, IgnoreCase)))
355139f7f9bSDimitry Andric break;
356139f7f9bSDimitry Andric if (Start == End)
357139f7f9bSDimitry Andric break;
358139f7f9bSDimitry Andric
359f785676fSDimitry Andric Option Opt(Start, this);
360f785676fSDimitry Andric
361f785676fSDimitry Andric if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
362f785676fSDimitry Andric continue;
363f785676fSDimitry Andric if (Opt.hasFlag(FlagsToExclude))
364f785676fSDimitry Andric continue;
365f785676fSDimitry Andric
366139f7f9bSDimitry Andric // See if this option matches.
367f785676fSDimitry Andric if (Arg *A = Opt.accept(Args, Index, ArgSize))
368139f7f9bSDimitry Andric return A;
369139f7f9bSDimitry Andric
370139f7f9bSDimitry Andric // Otherwise, see if this argument was missing values.
371139f7f9bSDimitry Andric if (Prev != Index)
37291bc56edSDimitry Andric return nullptr;
373139f7f9bSDimitry Andric }
374139f7f9bSDimitry Andric
375f785676fSDimitry Andric // If we failed to find an option and this arg started with /, then it's
376f785676fSDimitry Andric // probably an input path.
377f785676fSDimitry Andric if (Str[0] == '/')
378f785676fSDimitry Andric return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
379f785676fSDimitry Andric
380139f7f9bSDimitry Andric return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
381139f7f9bSDimitry Andric }
382139f7f9bSDimitry Andric
ParseArgs(ArrayRef<const char * > ArgArr,unsigned & MissingArgIndex,unsigned & MissingArgCount,unsigned FlagsToInclude,unsigned FlagsToExclude) const3833dac3a9bSDimitry Andric InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr,
384139f7f9bSDimitry Andric unsigned &MissingArgIndex,
385f785676fSDimitry Andric unsigned &MissingArgCount,
386f785676fSDimitry Andric unsigned FlagsToInclude,
387f785676fSDimitry Andric unsigned FlagsToExclude) const {
3883dac3a9bSDimitry Andric InputArgList Args(ArgArr.begin(), ArgArr.end());
389139f7f9bSDimitry Andric
390139f7f9bSDimitry Andric // FIXME: Handle '@' args (or at least error on them).
391139f7f9bSDimitry Andric
392139f7f9bSDimitry Andric MissingArgIndex = MissingArgCount = 0;
3933dac3a9bSDimitry Andric unsigned Index = 0, End = ArgArr.size();
394139f7f9bSDimitry Andric while (Index < End) {
39539d628a0SDimitry Andric // Ingore nullptrs, they are response file's EOL markers
3963dac3a9bSDimitry Andric if (Args.getArgString(Index) == nullptr) {
39739d628a0SDimitry Andric ++Index;
39839d628a0SDimitry Andric continue;
39939d628a0SDimitry Andric }
400139f7f9bSDimitry Andric // Ignore empty arguments (other things may still take them as arguments).
4013dac3a9bSDimitry Andric StringRef Str = Args.getArgString(Index);
402f785676fSDimitry Andric if (Str == "") {
403139f7f9bSDimitry Andric ++Index;
404139f7f9bSDimitry Andric continue;
405139f7f9bSDimitry Andric }
406139f7f9bSDimitry Andric
407139f7f9bSDimitry Andric unsigned Prev = Index;
4083dac3a9bSDimitry Andric Arg *A = ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude);
409139f7f9bSDimitry Andric assert(Index > Prev && "Parser failed to consume argument.");
410139f7f9bSDimitry Andric
411139f7f9bSDimitry Andric // Check for missing argument error.
412139f7f9bSDimitry Andric if (!A) {
413139f7f9bSDimitry Andric assert(Index >= End && "Unexpected parser error.");
414139f7f9bSDimitry Andric assert(Index - Prev - 1 && "No missing arguments!");
415139f7f9bSDimitry Andric MissingArgIndex = Prev;
416139f7f9bSDimitry Andric MissingArgCount = Index - Prev - 1;
417139f7f9bSDimitry Andric break;
418139f7f9bSDimitry Andric }
419139f7f9bSDimitry Andric
4203dac3a9bSDimitry Andric Args.append(A);
421139f7f9bSDimitry Andric }
422139f7f9bSDimitry Andric
423139f7f9bSDimitry Andric return Args;
424139f7f9bSDimitry Andric }
425139f7f9bSDimitry Andric
getOptionHelpName(const OptTable & Opts,OptSpecifier Id)426139f7f9bSDimitry Andric static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
427139f7f9bSDimitry Andric const Option O = Opts.getOption(Id);
428139f7f9bSDimitry Andric std::string Name = O.getPrefixedName();
429139f7f9bSDimitry Andric
430139f7f9bSDimitry Andric // Add metavar, if used.
431139f7f9bSDimitry Andric switch (O.getKind()) {
432139f7f9bSDimitry Andric case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
433139f7f9bSDimitry Andric llvm_unreachable("Invalid option with help text.");
434139f7f9bSDimitry Andric
435139f7f9bSDimitry Andric case Option::MultiArgClass:
43639d628a0SDimitry Andric if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) {
43739d628a0SDimitry Andric // For MultiArgs, metavar is full list of all argument names.
43839d628a0SDimitry Andric Name += ' ';
43939d628a0SDimitry Andric Name += MetaVarName;
44039d628a0SDimitry Andric }
44139d628a0SDimitry Andric else {
44239d628a0SDimitry Andric // For MultiArgs<N>, if metavar not supplied, print <value> N times.
44339d628a0SDimitry Andric for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) {
44439d628a0SDimitry Andric Name += " <value>";
44539d628a0SDimitry Andric }
44639d628a0SDimitry Andric }
44739d628a0SDimitry Andric break;
448139f7f9bSDimitry Andric
449139f7f9bSDimitry Andric case Option::FlagClass:
450139f7f9bSDimitry Andric break;
451139f7f9bSDimitry Andric
452edd7eaddSDimitry Andric case Option::ValuesClass:
453edd7eaddSDimitry Andric break;
454edd7eaddSDimitry Andric
455139f7f9bSDimitry Andric case Option::SeparateClass: case Option::JoinedOrSeparateClass:
4563ca95b02SDimitry Andric case Option::RemainingArgsClass: case Option::RemainingArgsJoinedClass:
457139f7f9bSDimitry Andric Name += ' ';
458d88c1a5aSDimitry Andric LLVM_FALLTHROUGH;
459139f7f9bSDimitry Andric case Option::JoinedClass: case Option::CommaJoinedClass:
460139f7f9bSDimitry Andric case Option::JoinedAndSeparateClass:
461139f7f9bSDimitry Andric if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
462139f7f9bSDimitry Andric Name += MetaVarName;
463139f7f9bSDimitry Andric else
464139f7f9bSDimitry Andric Name += "<value>";
465139f7f9bSDimitry Andric break;
466139f7f9bSDimitry Andric }
467139f7f9bSDimitry Andric
468139f7f9bSDimitry Andric return Name;
469139f7f9bSDimitry Andric }
470139f7f9bSDimitry Andric
471b40b48b8SDimitry Andric namespace {
472b40b48b8SDimitry Andric struct OptionInfo {
473b40b48b8SDimitry Andric std::string Name;
474b40b48b8SDimitry Andric StringRef HelpText;
475b40b48b8SDimitry Andric };
476b40b48b8SDimitry Andric } // namespace
477b40b48b8SDimitry Andric
PrintHelpOptionList(raw_ostream & OS,StringRef Title,std::vector<OptionInfo> & OptionHelp)478139f7f9bSDimitry Andric static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
479b40b48b8SDimitry Andric std::vector<OptionInfo> &OptionHelp) {
480139f7f9bSDimitry Andric OS << Title << ":\n";
481139f7f9bSDimitry Andric
482139f7f9bSDimitry Andric // Find the maximum option length.
483139f7f9bSDimitry Andric unsigned OptionFieldWidth = 0;
484139f7f9bSDimitry Andric for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
485139f7f9bSDimitry Andric // Limit the amount of padding we are willing to give up for alignment.
486b40b48b8SDimitry Andric unsigned Length = OptionHelp[i].Name.size();
487139f7f9bSDimitry Andric if (Length <= 23)
488139f7f9bSDimitry Andric OptionFieldWidth = std::max(OptionFieldWidth, Length);
489139f7f9bSDimitry Andric }
490139f7f9bSDimitry Andric
491139f7f9bSDimitry Andric const unsigned InitialPad = 2;
492139f7f9bSDimitry Andric for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
493b40b48b8SDimitry Andric const std::string &Option = OptionHelp[i].Name;
494139f7f9bSDimitry Andric int Pad = OptionFieldWidth - int(Option.size());
495139f7f9bSDimitry Andric OS.indent(InitialPad) << Option;
496139f7f9bSDimitry Andric
497139f7f9bSDimitry Andric // Break on long option names.
498139f7f9bSDimitry Andric if (Pad < 0) {
499139f7f9bSDimitry Andric OS << "\n";
500139f7f9bSDimitry Andric Pad = OptionFieldWidth + InitialPad;
501139f7f9bSDimitry Andric }
502b40b48b8SDimitry Andric OS.indent(Pad + 1) << OptionHelp[i].HelpText << '\n';
503139f7f9bSDimitry Andric }
504139f7f9bSDimitry Andric }
505139f7f9bSDimitry Andric
getOptionHelpGroup(const OptTable & Opts,OptSpecifier Id)506139f7f9bSDimitry Andric static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
507139f7f9bSDimitry Andric unsigned GroupID = Opts.getOptionGroupID(Id);
508139f7f9bSDimitry Andric
509139f7f9bSDimitry Andric // If not in a group, return the default help group.
510139f7f9bSDimitry Andric if (!GroupID)
511139f7f9bSDimitry Andric return "OPTIONS";
512139f7f9bSDimitry Andric
513139f7f9bSDimitry Andric // Abuse the help text of the option groups to store the "help group"
514139f7f9bSDimitry Andric // name.
515139f7f9bSDimitry Andric //
516139f7f9bSDimitry Andric // FIXME: Split out option groups.
517139f7f9bSDimitry Andric if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
518139f7f9bSDimitry Andric return GroupHelp;
519139f7f9bSDimitry Andric
520139f7f9bSDimitry Andric // Otherwise keep looking.
521139f7f9bSDimitry Andric return getOptionHelpGroup(Opts, GroupID);
522139f7f9bSDimitry Andric }
523139f7f9bSDimitry Andric
PrintHelp(raw_ostream & OS,const char * Usage,const char * Title,bool ShowHidden,bool ShowAllAliases) const524*b5893f02SDimitry Andric void OptTable::PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
5252cab237bSDimitry Andric bool ShowHidden, bool ShowAllAliases) const {
526*b5893f02SDimitry Andric PrintHelp(OS, Usage, Title, /*Include*/ 0, /*Exclude*/
5272cab237bSDimitry Andric (ShowHidden ? 0 : HelpHidden), ShowAllAliases);
528f785676fSDimitry Andric }
529f785676fSDimitry Andric
PrintHelp(raw_ostream & OS,const char * Usage,const char * Title,unsigned FlagsToInclude,unsigned FlagsToExclude,bool ShowAllAliases) const530*b5893f02SDimitry Andric void OptTable::PrintHelp(raw_ostream &OS, const char *Usage, const char *Title,
5312cab237bSDimitry Andric unsigned FlagsToInclude, unsigned FlagsToExclude,
5322cab237bSDimitry Andric bool ShowAllAliases) const {
533*b5893f02SDimitry Andric OS << "OVERVIEW: " << Title << "\n\n";
534*b5893f02SDimitry Andric OS << "USAGE: " << Usage << "\n\n";
535139f7f9bSDimitry Andric
536139f7f9bSDimitry Andric // Render help text into a map of group-name to a list of (option, help)
537139f7f9bSDimitry Andric // pairs.
5384ba319b5SDimitry Andric std::map<std::string, std::vector<OptionInfo>> GroupedOptionHelp;
539139f7f9bSDimitry Andric
5404ba319b5SDimitry Andric for (unsigned Id = 1, e = getNumOptions() + 1; Id != e; ++Id) {
541139f7f9bSDimitry Andric // FIXME: Split out option groups.
542139f7f9bSDimitry Andric if (getOptionKind(Id) == Option::GroupClass)
543139f7f9bSDimitry Andric continue;
544139f7f9bSDimitry Andric
545f785676fSDimitry Andric unsigned Flags = getInfo(Id).Flags;
546f785676fSDimitry Andric if (FlagsToInclude && !(Flags & FlagsToInclude))
547f785676fSDimitry Andric continue;
548f785676fSDimitry Andric if (Flags & FlagsToExclude)
549139f7f9bSDimitry Andric continue;
550139f7f9bSDimitry Andric
5512cab237bSDimitry Andric // If an alias doesn't have a help text, show a help text for the aliased
5522cab237bSDimitry Andric // option instead.
5532cab237bSDimitry Andric const char *HelpText = getOptionHelpText(Id);
5542cab237bSDimitry Andric if (!HelpText && ShowAllAliases) {
5552cab237bSDimitry Andric const Option Alias = getOption(Id).getAlias();
5562cab237bSDimitry Andric if (Alias.isValid())
5572cab237bSDimitry Andric HelpText = getOptionHelpText(Alias.getID());
5582cab237bSDimitry Andric }
5592cab237bSDimitry Andric
5602cab237bSDimitry Andric if (HelpText) {
561139f7f9bSDimitry Andric const char *HelpGroup = getOptionHelpGroup(*this, Id);
562139f7f9bSDimitry Andric const std::string &OptName = getOptionHelpName(*this, Id);
5632cab237bSDimitry Andric GroupedOptionHelp[HelpGroup].push_back({OptName, HelpText});
564139f7f9bSDimitry Andric }
565139f7f9bSDimitry Andric }
566139f7f9bSDimitry Andric
5674ba319b5SDimitry Andric for (auto& OptionGroup : GroupedOptionHelp) {
5684ba319b5SDimitry Andric if (OptionGroup.first != GroupedOptionHelp.begin()->first)
569139f7f9bSDimitry Andric OS << "\n";
5704ba319b5SDimitry Andric PrintHelpOptionList(OS, OptionGroup.first, OptionGroup.second);
571139f7f9bSDimitry Andric }
572139f7f9bSDimitry Andric
573139f7f9bSDimitry Andric OS.flush();
574139f7f9bSDimitry Andric }
575