blob: c1bb05e817f0a842503483c0a5ebf0fc51f7ca05 [file] [log] [blame]
Eugene Zelenkoaf615892017-06-16 00:43:26 +00001//===- OptTable.cpp - Option Table Implementation -------------------------===//
Michael J. Spencer41ee0412012-12-05 00:29:32 +00002//
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
David Majnemer0d955d02016-08-11 22:21:41 +000010#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoaf615892017-06-16 00:43:26 +000011#include "llvm/ADT/StringRef.h"
12#include "llvm/ADT/StringSet.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000013#include "llvm/Option/Arg.h"
14#include "llvm/Option/ArgList.h"
15#include "llvm/Option/Option.h"
Eugene Zelenkoaf615892017-06-16 00:43:26 +000016#include "llvm/Option/OptSpecifier.h"
17#include "llvm/Option/OptTable.h"
18#include "llvm/Support/Compiler.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000019#include "llvm/Support/ErrorHandling.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000020#include "llvm/Support/raw_ostream.h"
Michael J. Spencer41ee0412012-12-05 00:29:32 +000021#include <algorithm>
Eugene Zelenkoaf615892017-06-16 00:43:26 +000022#include <cassert>
Rui Ueyama8fb5a912013-08-28 20:04:31 +000023#include <cctype>
Eugene Zelenkoaf615892017-06-16 00:43:26 +000024#include <cstring>
Michael J. Spencer41ee0412012-12-05 00:29:32 +000025#include <map>
Eugene Zelenkoaf615892017-06-16 00:43:26 +000026#include <string>
27#include <utility>
28#include <vector>
Michael J. Spencer41ee0412012-12-05 00:29:32 +000029
30using namespace llvm;
31using namespace llvm::opt;
32
Rui Ueyama8fb5a912013-08-28 20:04:31 +000033namespace llvm {
34namespace opt {
Rui Ueyama7159bd92013-08-27 23:47:01 +000035
Rui Ueyama8fb5a912013-08-28 20:04:31 +000036// Ordering on Info. The ordering is *almost* case-insensitive lexicographic,
Nathan Hawes9b656ff2017-08-24 21:20:41 +000037// with an exception. '\0' comes at the end of the alphabet instead of the
Rui Ueyama8fb5a912013-08-28 20:04:31 +000038// beginning (thus options precede any other options which prefix them).
39static int StrCmpOptionNameIgnoreCase(const char *A, const char *B) {
40 const char *X = A, *Y = B;
41 char a = tolower(*A), b = tolower(*B);
Rui Ueyamac3779ff2013-08-28 00:02:06 +000042 while (a == b) {
43 if (a == '\0')
44 return 0;
45
Rui Ueyama8fb5a912013-08-28 20:04:31 +000046 a = tolower(*++X);
47 b = tolower(*++Y);
Rui Ueyamac3779ff2013-08-28 00:02:06 +000048 }
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;
Rui Ueyama7159bd92013-08-27 23:47:01 +000057}
58
Eli Friedman3e7dca62013-09-10 23:22:56 +000059#ifndef NDEBUG
60static 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
66static 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;
Craig Topper2617dcc2014-04-15 06:32:26 +000075 *APre != nullptr && *BPre != nullptr; ++APre, ++BPre){
Eli Friedman3e7dca62013-09-10 23:22:56 +000076 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
Michael J. Spencer41ee0412012-12-05 00:29:32 +000088// Support lower_bound between info and an option name.
89static inline bool operator<(const OptTable::Info &I, const char *Name) {
Rui Ueyama8fb5a912013-08-28 20:04:31 +000090 return StrCmpOptionNameIgnoreCase(I.Name, Name) < 0;
Michael J. Spencer41ee0412012-12-05 00:29:32 +000091}
Eugene Zelenkoaf615892017-06-16 00:43:26 +000092
93} // end namespace opt
94} // end namespace llvm
Michael J. Spencer41ee0412012-12-05 00:29:32 +000095
96OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
97
Craig Topper8ea23902015-10-21 16:30:42 +000098OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase)
Eugene Zelenkoaf615892017-06-16 00:43:26 +000099 : OptionInfos(OptionInfos), IgnoreCase(IgnoreCase) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000100 // 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(!TheInputOptionID && "Cannot have multiple input options!");
108 TheInputOptionID = getInfo(i + 1).ID;
109 } else if (Kind == Option::UnknownClass) {
110 assert(!TheUnknownOptionID && "Cannot have multiple unknown options!");
111 TheUnknownOptionID = 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) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000143 for (; *P != nullptr; ++P) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000144 PrefixesUnion.insert(*P);
145 }
146 }
147 }
148
149 // Build prefix chars.
Eugene Zelenkoaf615892017-06-16 00:43:26 +0000150 for (StringSet<>::const_iterator I = PrefixesUnion.begin(),
151 E = PrefixesUnion.end(); I != E; ++I) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000152 StringRef Prefix = I->getKey();
153 for (StringRef::const_iterator C = Prefix.begin(), CE = Prefix.end();
154 C != CE; ++C)
David Majnemer0d955d02016-08-11 22:21:41 +0000155 if (!is_contained(PrefixChars, *C))
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000156 PrefixChars.push_back(*C);
157 }
158}
159
Eugene Zelenkoaf615892017-06-16 00:43:26 +0000160OptTable::~OptTable() = default;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000161
162const Option OptTable::getOption(OptSpecifier Opt) const {
163 unsigned id = Opt.getID();
164 if (id == 0)
Craig Topper2617dcc2014-04-15 06:32:26 +0000165 return Option(nullptr, nullptr);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000166 assert((unsigned) (id - 1) < getNumOptions() && "Invalid ID.");
167 return Option(&getInfo(id), this);
168}
169
Eugene Zelenkoaf615892017-06-16 00:43:26 +0000170static bool isInput(const StringSet<> &Prefixes, StringRef Arg) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000171 if (Arg == "-")
172 return true;
Eugene Zelenkoaf615892017-06-16 00:43:26 +0000173 for (StringSet<>::const_iterator I = Prefixes.begin(),
174 E = Prefixes.end(); I != E; ++I)
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000175 if (Arg.startswith(I->getKey()))
176 return false;
177 return true;
178}
179
180/// \returns Matched size. 0 means no match.
Rui Ueyama8fb5a912013-08-28 20:04:31 +0000181static unsigned matchOption(const OptTable::Info *I, StringRef Str,
182 bool IgnoreCase) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000183 for (const char * const *Pre = I->Prefixes; *Pre != nullptr; ++Pre) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000184 StringRef Prefix(*Pre);
Rui Ueyama8fb5a912013-08-28 20:04:31 +0000185 if (Str.startswith(Prefix)) {
186 StringRef Rest = Str.substr(Prefix.size());
187 bool Matched = IgnoreCase
Jakub Staszakcfcfee02013-11-04 19:22:50 +0000188 ? Rest.startswith_lower(I->Name)
Rui Ueyama8fb5a912013-08-28 20:04:31 +0000189 : Rest.startswith(I->Name);
190 if (Matched)
191 return Prefix.size() + StringRef(I->Name).size();
192 }
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000193 }
194 return 0;
195}
196
Yuka Takahashiba5d4af2017-06-20 16:31:31 +0000197// Returns true if one of the Prefixes + In.Names matches Option
198static bool optionMatches(const OptTable::Info &In, StringRef Option) {
Yuka Takahashi24bc6a42017-08-29 00:09:31 +0000199 if (In.Prefixes)
Yuka Takahashiba5d4af2017-06-20 16:31:31 +0000200 for (size_t I = 0; In.Prefixes[I]; I++)
201 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.
209std::vector<std::string>
210OptTable::suggestValueCompletions(StringRef Option, StringRef Arg) const {
211 // Search all options and return possible values.
Yuka Takahashi24bc6a42017-08-29 00:09:31 +0000212 for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
213 const Info &In = OptionInfos[I];
214 if (!In.Values || !optionMatches(In, Option))
Yuka Takahashiba5d4af2017-06-20 16:31:31 +0000215 continue;
216
217 SmallVector<StringRef, 8> Candidates;
218 StringRef(In.Values).split(Candidates, ",", -1, false);
219
220 std::vector<std::string> Result;
221 for (StringRef Val : Candidates)
222 if (Val.startswith(Arg))
223 Result.push_back(Val);
224 return Result;
225 }
226 return {};
227}
228
Yuka Takahashi33cf63b2017-07-08 17:48:59 +0000229std::vector<std::string>
230OptTable::findByPrefix(StringRef Cur, unsigned short DisableFlags) const {
Yuka Takahashic8068db2017-05-23 18:39:08 +0000231 std::vector<std::string> Ret;
Yuka Takahashi24bc6a42017-08-29 00:09:31 +0000232 for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
233 const Info &In = OptionInfos[I];
Yuka Takahashi34a7c3b2017-07-05 02:36:32 +0000234 if (!In.Prefixes || (!In.HelpText && !In.GroupID))
Yuka Takahashic8068db2017-05-23 18:39:08 +0000235 continue;
Yuka Takahashi33cf63b2017-07-08 17:48:59 +0000236 if (In.Flags & DisableFlags)
237 continue;
238
Yuka Takahashic8068db2017-05-23 18:39:08 +0000239 for (int I = 0; In.Prefixes[I]; I++) {
Yuka Takahashi66256902017-07-26 13:36:58 +0000240 std::string S = std::string(In.Prefixes[I]) + std::string(In.Name) + "\t";
241 if (In.HelpText)
242 S += In.HelpText;
Yuka Takahashic8068db2017-05-23 18:39:08 +0000243 if (StringRef(S).startswith(Cur))
244 Ret.push_back(S);
245 }
246 }
247 return Ret;
248}
249
Yuka Takahashi24bc6a42017-08-29 00:09:31 +0000250bool OptTable::addValues(const char *Option, const char *Values) {
251 for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
252 Info &In = OptionInfos[I];
253 if (optionMatches(In, Option)) {
254 In.Values = Values;
255 return true;
256 }
257 }
258 return false;
259}
260
Reid Klecknereadb7652013-07-19 18:04:57 +0000261Arg *OptTable::ParseOneArg(const ArgList &Args, unsigned &Index,
262 unsigned FlagsToInclude,
263 unsigned FlagsToExclude) const {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000264 unsigned Prev = Index;
265 const char *Str = Args.getArgString(Index);
266
267 // Anything that doesn't start with PrefixesUnion is an input, as is '-'
268 // itself.
269 if (isInput(PrefixesUnion, Str))
270 return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
271
Yuka Takahashi24bc6a42017-08-29 00:09:31 +0000272 const Info *Start = OptionInfos.data() + FirstSearchableIndex;
273 const Info *End = OptionInfos.data() + OptionInfos.size();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000274 StringRef Name = StringRef(Str).ltrim(PrefixChars);
275
276 // Search for the first next option which could be a prefix.
277 Start = std::lower_bound(Start, End, Name.data());
278
279 // Options are stored in sorted order, with '\0' at the end of the
280 // alphabet. Since the only options which can accept a string must
281 // prefix it, we iteratively search for the next option which could
282 // be a prefix.
283 //
284 // FIXME: This is searching much more than necessary, but I am
285 // blanking on the simplest way to make it fast. We can solve this
286 // problem when we move to TableGen.
287 for (; Start != End; ++Start) {
288 unsigned ArgSize = 0;
289 // Scan for first option which is a proper prefix.
290 for (; Start != End; ++Start)
Rui Ueyama8fb5a912013-08-28 20:04:31 +0000291 if ((ArgSize = matchOption(Start, Str, IgnoreCase)))
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000292 break;
293 if (Start == End)
294 break;
295
Reid Klecknereadb7652013-07-19 18:04:57 +0000296 Option Opt(Start, this);
297
298 if (FlagsToInclude && !Opt.hasFlag(FlagsToInclude))
299 continue;
300 if (Opt.hasFlag(FlagsToExclude))
301 continue;
302
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000303 // See if this option matches.
Reid Klecknereadb7652013-07-19 18:04:57 +0000304 if (Arg *A = Opt.accept(Args, Index, ArgSize))
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000305 return A;
306
307 // Otherwise, see if this argument was missing values.
308 if (Prev != Index)
Craig Topper2617dcc2014-04-15 06:32:26 +0000309 return nullptr;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000310 }
311
Reid Klecknereadb7652013-07-19 18:04:57 +0000312 // If we failed to find an option and this arg started with /, then it's
313 // probably an input path.
314 if (Str[0] == '/')
315 return new Arg(getOption(TheInputOptionID), Str, Index++, Str);
316
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000317 return new Arg(getOption(TheUnknownOptionID), Str, Index++, Str);
318}
319
David Blaikiedb3d31d2015-06-22 22:06:37 +0000320InputArgList OptTable::ParseArgs(ArrayRef<const char *> ArgArr,
321 unsigned &MissingArgIndex,
322 unsigned &MissingArgCount,
323 unsigned FlagsToInclude,
324 unsigned FlagsToExclude) const {
325 InputArgList Args(ArgArr.begin(), ArgArr.end());
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000326
327 // FIXME: Handle '@' args (or at least error on them).
328
329 MissingArgIndex = MissingArgCount = 0;
David Blaikie259f61d2015-06-21 06:31:53 +0000330 unsigned Index = 0, End = ArgArr.size();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000331 while (Index < End) {
Reid Klecknere3f146d2014-08-22 19:29:17 +0000332 // Ingore nullptrs, they are response file's EOL markers
David Blaikiedb3d31d2015-06-22 22:06:37 +0000333 if (Args.getArgString(Index) == nullptr) {
Reid Klecknere3f146d2014-08-22 19:29:17 +0000334 ++Index;
335 continue;
336 }
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000337 // Ignore empty arguments (other things may still take them as arguments).
David Blaikiedb3d31d2015-06-22 22:06:37 +0000338 StringRef Str = Args.getArgString(Index);
Hans Wennborgb8f34202013-08-02 21:20:27 +0000339 if (Str == "") {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000340 ++Index;
341 continue;
342 }
343
344 unsigned Prev = Index;
David Blaikiedb3d31d2015-06-22 22:06:37 +0000345 Arg *A = ParseOneArg(Args, Index, FlagsToInclude, FlagsToExclude);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000346 assert(Index > Prev && "Parser failed to consume argument.");
347
348 // Check for missing argument error.
349 if (!A) {
350 assert(Index >= End && "Unexpected parser error.");
351 assert(Index - Prev - 1 && "No missing arguments!");
352 MissingArgIndex = Prev;
353 MissingArgCount = Index - Prev - 1;
354 break;
355 }
356
David Blaikiedb3d31d2015-06-22 22:06:37 +0000357 Args.append(A);
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000358 }
359
Logan Chien9d5891f2015-06-22 23:16:02 +0000360 return Args;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000361}
362
363static std::string getOptionHelpName(const OptTable &Opts, OptSpecifier Id) {
364 const Option O = Opts.getOption(Id);
365 std::string Name = O.getPrefixedName();
366
367 // Add metavar, if used.
368 switch (O.getKind()) {
369 case Option::GroupClass: case Option::InputClass: case Option::UnknownClass:
370 llvm_unreachable("Invalid option with help text.");
371
372 case Option::MultiArgClass:
Nick Kledzikbcd6e2a2014-08-15 21:35:07 +0000373 if (const char *MetaVarName = Opts.getOptionMetaVar(Id)) {
374 // For MultiArgs, metavar is full list of all argument names.
375 Name += ' ';
376 Name += MetaVarName;
377 }
378 else {
379 // For MultiArgs<N>, if metavar not supplied, print <value> N times.
380 for (unsigned i=0, e=O.getNumArgs(); i< e; ++i) {
381 Name += " <value>";
382 }
383 }
384 break;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000385
386 case Option::FlagClass:
387 break;
388
Yuka Takahashiba5d4af2017-06-20 16:31:31 +0000389 case Option::ValuesClass:
390 break;
391
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000392 case Option::SeparateClass: case Option::JoinedOrSeparateClass:
Hans Wennborg40cfde32016-04-15 00:23:30 +0000393 case Option::RemainingArgsClass: case Option::RemainingArgsJoinedClass:
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000394 Name += ' ';
Justin Bognerb03fd122016-08-17 05:10:15 +0000395 LLVM_FALLTHROUGH;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000396 case Option::JoinedClass: case Option::CommaJoinedClass:
397 case Option::JoinedAndSeparateClass:
398 if (const char *MetaVarName = Opts.getOptionMetaVar(Id))
399 Name += MetaVarName;
400 else
401 Name += "<value>";
402 break;
403 }
404
405 return Name;
406}
407
George Rimarb4e76f62017-07-18 10:59:30 +0000408namespace {
409struct OptionInfo {
410 std::string Name;
411 StringRef HelpText;
412};
413} // namespace
414
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000415static void PrintHelpOptionList(raw_ostream &OS, StringRef Title,
George Rimarb4e76f62017-07-18 10:59:30 +0000416 std::vector<OptionInfo> &OptionHelp) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000417 OS << Title << ":\n";
418
419 // Find the maximum option length.
420 unsigned OptionFieldWidth = 0;
421 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000422 // Limit the amount of padding we are willing to give up for alignment.
George Rimarb4e76f62017-07-18 10:59:30 +0000423 unsigned Length = OptionHelp[i].Name.size();
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000424 if (Length <= 23)
425 OptionFieldWidth = std::max(OptionFieldWidth, Length);
426 }
427
428 const unsigned InitialPad = 2;
429 for (unsigned i = 0, e = OptionHelp.size(); i != e; ++i) {
George Rimarb4e76f62017-07-18 10:59:30 +0000430 const std::string &Option = OptionHelp[i].Name;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000431 int Pad = OptionFieldWidth - int(Option.size());
432 OS.indent(InitialPad) << Option;
433
434 // Break on long option names.
435 if (Pad < 0) {
436 OS << "\n";
437 Pad = OptionFieldWidth + InitialPad;
438 }
George Rimarb4e76f62017-07-18 10:59:30 +0000439 OS.indent(Pad + 1) << OptionHelp[i].HelpText << '\n';
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000440 }
441}
442
443static const char *getOptionHelpGroup(const OptTable &Opts, OptSpecifier Id) {
444 unsigned GroupID = Opts.getOptionGroupID(Id);
445
446 // If not in a group, return the default help group.
447 if (!GroupID)
448 return "OPTIONS";
449
450 // Abuse the help text of the option groups to store the "help group"
451 // name.
452 //
453 // FIXME: Split out option groups.
454 if (const char *GroupHelp = Opts.getOptionHelpText(GroupID))
455 return GroupHelp;
456
457 // Otherwise keep looking.
458 return getOptionHelpGroup(Opts, GroupID);
459}
460
Reid Kleckner12e03322013-06-13 18:12:12 +0000461void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
George Rimar4530dff2017-07-26 09:09:56 +0000462 bool ShowHidden, bool ShowAllAliases) const {
Reid Kleckner12e03322013-06-13 18:12:12 +0000463 PrintHelp(OS, Name, Title, /*Include*/ 0, /*Exclude*/
George Rimar4530dff2017-07-26 09:09:56 +0000464 (ShowHidden ? 0 : HelpHidden), ShowAllAliases);
Reid Kleckner12e03322013-06-13 18:12:12 +0000465}
466
Reid Kleckner12e03322013-06-13 18:12:12 +0000467void OptTable::PrintHelp(raw_ostream &OS, const char *Name, const char *Title,
George Rimar4530dff2017-07-26 09:09:56 +0000468 unsigned FlagsToInclude, unsigned FlagsToExclude,
469 bool ShowAllAliases) const {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000470 OS << "OVERVIEW: " << Title << "\n";
471 OS << '\n';
472 OS << "USAGE: " << Name << " [options] <inputs>\n";
473 OS << '\n';
474
475 // Render help text into a map of group-name to a list of (option, help)
476 // pairs.
George Rimarb4e76f62017-07-18 10:59:30 +0000477 using helpmap_ty = std::map<std::string, std::vector<OptionInfo>>;
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000478 helpmap_ty GroupedOptionHelp;
479
480 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
481 unsigned Id = i + 1;
482
483 // FIXME: Split out option groups.
484 if (getOptionKind(Id) == Option::GroupClass)
485 continue;
486
Reid Kleckner12e03322013-06-13 18:12:12 +0000487 unsigned Flags = getInfo(Id).Flags;
488 if (FlagsToInclude && !(Flags & FlagsToInclude))
489 continue;
490 if (Flags & FlagsToExclude)
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000491 continue;
492
George Rimar4530dff2017-07-26 09:09:56 +0000493 // If an alias doesn't have a help text, show a help text for the aliased
494 // option instead.
495 const char *HelpText = getOptionHelpText(Id);
496 if (!HelpText && ShowAllAliases) {
497 const Option Alias = getOption(Id).getAlias();
498 if (Alias.isValid())
499 HelpText = getOptionHelpText(Alias.getID());
500 }
501
502 if (HelpText) {
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000503 const char *HelpGroup = getOptionHelpGroup(*this, Id);
504 const std::string &OptName = getOptionHelpName(*this, Id);
George Rimar4530dff2017-07-26 09:09:56 +0000505 GroupedOptionHelp[HelpGroup].push_back({OptName, HelpText});
Michael J. Spencer41ee0412012-12-05 00:29:32 +0000506 }
507 }
508
509 for (helpmap_ty::iterator it = GroupedOptionHelp .begin(),
510 ie = GroupedOptionHelp.end(); it != ie; ++it) {
511 if (it != GroupedOptionHelp .begin())
512 OS << "\n";
513 PrintHelpOptionList(OS, it->first, it->second);
514 }
515
516 OS.flush();
517}