LLVM 19.0.0git
CommandLine.cpp
Go to the documentation of this file.
1//===-- CommandLine.cpp - Command line parser 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// This class implements a command line argument processor that is useful when
10// creating a tool. It provides a simple, minimalistic interface that is easily
11// extensible and supports nonlocal (library) command line options.
12//
13// Note that rather than trying to figure out what this code does, you could try
14// reading the library documentation located in docs/CommandLine.html
15//
16//===----------------------------------------------------------------------===//
17
19
20#include "DebugOptions.h"
21
22#include "llvm-c/Support.h"
23#include "llvm/ADT/ArrayRef.h"
28#include "llvm/ADT/StringMap.h"
29#include "llvm/ADT/StringRef.h"
30#include "llvm/ADT/Twine.h"
31#include "llvm/Config/config.h"
33#include "llvm/Support/Debug.h"
34#include "llvm/Support/Error.h"
39#include "llvm/Support/Path.h"
44#include <cstdlib>
45#include <optional>
46#include <string>
47using namespace llvm;
48using namespace cl;
49
50#define DEBUG_TYPE "commandline"
51
52//===----------------------------------------------------------------------===//
53// Template instantiations and anchors.
54//
55namespace llvm {
56namespace cl {
57template class basic_parser<bool>;
58template class basic_parser<boolOrDefault>;
59template class basic_parser<int>;
60template class basic_parser<long>;
61template class basic_parser<long long>;
62template class basic_parser<unsigned>;
63template class basic_parser<unsigned long>;
65template class basic_parser<double>;
66template class basic_parser<float>;
67template class basic_parser<std::string>;
68template class basic_parser<char>;
69
70template class opt<unsigned>;
71template class opt<int>;
72template class opt<std::string>;
73template class opt<char>;
74template class opt<bool>;
75} // namespace cl
76} // namespace llvm
77
78// Pin the vtables to this file.
79void GenericOptionValue::anchor() {}
82void Option::anchor() {}
86void parser<int>::anchor() {}
96
97//===----------------------------------------------------------------------===//
98
99const static size_t DefaultPad = 2;
100
101static StringRef ArgPrefix = "-";
104
105static size_t argPlusPrefixesSize(StringRef ArgName, size_t Pad = DefaultPad) {
106 size_t Len = ArgName.size();
107 if (Len == 1)
108 return Len + Pad + ArgPrefix.size() + ArgHelpPrefix.size();
109 return Len + Pad + ArgPrefixLong.size() + ArgHelpPrefix.size();
110}
111
112static SmallString<8> argPrefix(StringRef ArgName, size_t Pad = DefaultPad) {
114 for (size_t I = 0; I < Pad; ++I) {
115 Prefix.push_back(' ');
116 }
117 Prefix.append(ArgName.size() > 1 ? ArgPrefixLong : ArgPrefix);
118 return Prefix;
119}
120
121// Option predicates...
122static inline bool isGrouping(const Option *O) {
123 return O->getMiscFlags() & cl::Grouping;
124}
125static inline bool isPrefixedOrGrouping(const Option *O) {
126 return isGrouping(O) || O->getFormattingFlag() == cl::Prefix ||
127 O->getFormattingFlag() == cl::AlwaysPrefix;
128}
129
130
131namespace {
132
133class PrintArg {
134 StringRef ArgName;
135 size_t Pad;
136public:
137 PrintArg(StringRef ArgName, size_t Pad = DefaultPad) : ArgName(ArgName), Pad(Pad) {}
138 friend raw_ostream &operator<<(raw_ostream &OS, const PrintArg &);
139};
140
141raw_ostream &operator<<(raw_ostream &OS, const PrintArg& Arg) {
142 OS << argPrefix(Arg.ArgName, Arg.Pad) << Arg.ArgName;
143 return OS;
144}
145
146class CommandLineParser {
147public:
148 // Globals for name and overview of program. Program name is not a string to
149 // avoid static ctor/dtor issues.
150 std::string ProgramName;
151 StringRef ProgramOverview;
152
153 // This collects additional help to be printed.
154 std::vector<StringRef> MoreHelp;
155
156 // This collects Options added with the cl::DefaultOption flag. Since they can
157 // be overridden, they are not added to the appropriate SubCommands until
158 // ParseCommandLineOptions actually runs.
159 SmallVector<Option*, 4> DefaultOptions;
160
161 // This collects the different option categories that have been registered.
162 SmallPtrSet<OptionCategory *, 16> RegisteredOptionCategories;
163
164 // This collects the different subcommands that have been registered.
165 SmallPtrSet<SubCommand *, 4> RegisteredSubCommands;
166
167 CommandLineParser() { registerSubCommand(&SubCommand::getTopLevel()); }
168
170
171 bool ParseCommandLineOptions(int argc, const char *const *argv,
172 StringRef Overview, raw_ostream *Errs = nullptr,
173 bool LongOptionsUseDoubleDash = false);
174
175 void forEachSubCommand(Option &Opt, function_ref<void(SubCommand &)> Action) {
176 if (Opt.Subs.empty()) {
178 return;
179 }
180 if (Opt.Subs.size() == 1 && *Opt.Subs.begin() == &SubCommand::getAll()) {
181 for (auto *SC : RegisteredSubCommands)
182 Action(*SC);
184 return;
185 }
186 for (auto *SC : Opt.Subs) {
187 assert(SC != &SubCommand::getAll() &&
188 "SubCommand::getAll() should not be used with other subcommands");
189 Action(*SC);
190 }
191 }
192
193 void addLiteralOption(Option &Opt, SubCommand *SC, StringRef Name) {
194 if (Opt.hasArgStr())
195 return;
196 if (!SC->OptionsMap.insert(std::make_pair(Name, &Opt)).second) {
197 errs() << ProgramName << ": CommandLine Error: Option '" << Name
198 << "' registered more than once!\n";
199 report_fatal_error("inconsistency in registered CommandLine options");
200 }
201 }
202
203 void addLiteralOption(Option &Opt, StringRef Name) {
204 forEachSubCommand(
205 Opt, [&](SubCommand &SC) { addLiteralOption(Opt, &SC, Name); });
206 }
207
208 void addOption(Option *O, SubCommand *SC) {
209 bool HadErrors = false;
210 if (O->hasArgStr()) {
211 // If it's a DefaultOption, check to make sure it isn't already there.
212 if (O->isDefaultOption() && SC->OptionsMap.contains(O->ArgStr))
213 return;
214
215 // Add argument to the argument map!
216 if (!SC->OptionsMap.insert(std::make_pair(O->ArgStr, O)).second) {
217 errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
218 << "' registered more than once!\n";
219 HadErrors = true;
220 }
221 }
222
223 // Remember information about positional options.
224 if (O->getFormattingFlag() == cl::Positional)
225 SC->PositionalOpts.push_back(O);
226 else if (O->getMiscFlags() & cl::Sink) // Remember sink options
227 SC->SinkOpts.push_back(O);
228 else if (O->getNumOccurrencesFlag() == cl::ConsumeAfter) {
229 if (SC->ConsumeAfterOpt) {
230 O->error("Cannot specify more than one option with cl::ConsumeAfter!");
231 HadErrors = true;
232 }
233 SC->ConsumeAfterOpt = O;
234 }
235
236 // Fail hard if there were errors. These are strictly unrecoverable and
237 // indicate serious issues such as conflicting option names or an
238 // incorrectly
239 // linked LLVM distribution.
240 if (HadErrors)
241 report_fatal_error("inconsistency in registered CommandLine options");
242 }
243
244 void addOption(Option *O, bool ProcessDefaultOption = false) {
245 if (!ProcessDefaultOption && O->isDefaultOption()) {
246 DefaultOptions.push_back(O);
247 return;
248 }
249 forEachSubCommand(*O, [&](SubCommand &SC) { addOption(O, &SC); });
250 }
251
252 void removeOption(Option *O, SubCommand *SC) {
253 SmallVector<StringRef, 16> OptionNames;
254 O->getExtraOptionNames(OptionNames);
255 if (O->hasArgStr())
256 OptionNames.push_back(O->ArgStr);
257
258 SubCommand &Sub = *SC;
259 auto End = Sub.OptionsMap.end();
260 for (auto Name : OptionNames) {
261 auto I = Sub.OptionsMap.find(Name);
262 if (I != End && I->getValue() == O)
263 Sub.OptionsMap.erase(I);
264 }
265
266 if (O->getFormattingFlag() == cl::Positional)
267 for (auto *Opt = Sub.PositionalOpts.begin();
268 Opt != Sub.PositionalOpts.end(); ++Opt) {
269 if (*Opt == O) {
270 Sub.PositionalOpts.erase(Opt);
271 break;
272 }
273 }
274 else if (O->getMiscFlags() & cl::Sink)
275 for (auto *Opt = Sub.SinkOpts.begin(); Opt != Sub.SinkOpts.end(); ++Opt) {
276 if (*Opt == O) {
277 Sub.SinkOpts.erase(Opt);
278 break;
279 }
280 }
281 else if (O == Sub.ConsumeAfterOpt)
282 Sub.ConsumeAfterOpt = nullptr;
283 }
284
285 void removeOption(Option *O) {
286 forEachSubCommand(*O, [&](SubCommand &SC) { removeOption(O, &SC); });
287 }
288
289 bool hasOptions(const SubCommand &Sub) const {
290 return (!Sub.OptionsMap.empty() || !Sub.PositionalOpts.empty() ||
291 nullptr != Sub.ConsumeAfterOpt);
292 }
293
294 bool hasOptions() const {
295 for (const auto *S : RegisteredSubCommands) {
296 if (hasOptions(*S))
297 return true;
298 }
299 return false;
300 }
301
302 bool hasNamedSubCommands() const {
303 for (const auto *S : RegisteredSubCommands)
304 if (!S->getName().empty())
305 return true;
306 return false;
307 }
308
309 SubCommand *getActiveSubCommand() { return ActiveSubCommand; }
310
311 void updateArgStr(Option *O, StringRef NewName, SubCommand *SC) {
312 SubCommand &Sub = *SC;
313 if (!Sub.OptionsMap.insert(std::make_pair(NewName, O)).second) {
314 errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
315 << "' registered more than once!\n";
316 report_fatal_error("inconsistency in registered CommandLine options");
317 }
318 Sub.OptionsMap.erase(O->ArgStr);
319 }
320
321 void updateArgStr(Option *O, StringRef NewName) {
322 forEachSubCommand(*O,
323 [&](SubCommand &SC) { updateArgStr(O, NewName, &SC); });
324 }
325
326 void printOptionValues();
327
328 void registerCategory(OptionCategory *cat) {
329 assert(count_if(RegisteredOptionCategories,
330 [cat](const OptionCategory *Category) {
331 return cat->getName() == Category->getName();
332 }) == 0 &&
333 "Duplicate option categories");
334
335 RegisteredOptionCategories.insert(cat);
336 }
337
338 void registerSubCommand(SubCommand *sub) {
339 assert(count_if(RegisteredSubCommands,
340 [sub](const SubCommand *Sub) {
341 return (!sub->getName().empty()) &&
342 (Sub->getName() == sub->getName());
343 }) == 0 &&
344 "Duplicate subcommands");
345 RegisteredSubCommands.insert(sub);
346
347 // For all options that have been registered for all subcommands, add the
348 // option to this subcommand now.
350 "SubCommand::getAll() should not be registered");
351 for (auto &E : SubCommand::getAll().OptionsMap) {
352 Option *O = E.second;
353 if ((O->isPositional() || O->isSink() || O->isConsumeAfter()) ||
354 O->hasArgStr())
355 addOption(O, sub);
356 else
357 addLiteralOption(*O, sub, E.first());
358 }
359 }
360
361 void unregisterSubCommand(SubCommand *sub) {
362 RegisteredSubCommands.erase(sub);
363 }
364
367 return make_range(RegisteredSubCommands.begin(),
368 RegisteredSubCommands.end());
369 }
370
371 void reset() {
372 ActiveSubCommand = nullptr;
373 ProgramName.clear();
374 ProgramOverview = StringRef();
375
376 MoreHelp.clear();
377 RegisteredOptionCategories.clear();
378
380 RegisteredSubCommands.clear();
381
384 registerSubCommand(&SubCommand::getTopLevel());
385
386 DefaultOptions.clear();
387 }
388
389private:
390 SubCommand *ActiveSubCommand = nullptr;
391
392 Option *LookupOption(SubCommand &Sub, StringRef &Arg, StringRef &Value);
393 Option *LookupLongOption(SubCommand &Sub, StringRef &Arg, StringRef &Value,
394 bool LongOptionsUseDoubleDash, bool HaveDoubleDash) {
395 Option *Opt = LookupOption(Sub, Arg, Value);
396 if (Opt && LongOptionsUseDoubleDash && !HaveDoubleDash && !isGrouping(Opt))
397 return nullptr;
398 return Opt;
399 }
400 SubCommand *LookupSubCommand(StringRef Name, std::string &NearestString);
401};
402
403} // namespace
404
406
408 GlobalParser->addLiteralOption(O, Name);
409}
410
412 GlobalParser->MoreHelp.push_back(Help);
413}
414
416 GlobalParser->addOption(this);
417 FullyInitialized = true;
418}
419
420void Option::removeArgument() { GlobalParser->removeOption(this); }
421
423 if (FullyInitialized)
424 GlobalParser->updateArgStr(this, S);
425 assert(!S.starts_with("-") && "Option can't start with '-");
426 ArgStr = S;
427 if (ArgStr.size() == 1)
429}
430
432 assert(!Categories.empty() && "Categories cannot be empty.");
433 // Maintain backward compatibility by replacing the default GeneralCategory
434 // if it's still set. Otherwise, just add the new one. The GeneralCategory
435 // must be explicitly added if you want multiple categories that include it.
436 if (&C != &getGeneralCategory() && Categories[0] == &getGeneralCategory())
437 Categories[0] = &C;
438 else if (!is_contained(Categories, &C))
439 Categories.push_back(&C);
440}
441
443 NumOccurrences = 0;
444 setDefault();
445 if (isDefaultOption())
447}
448
449void OptionCategory::registerCategory() {
450 GlobalParser->registerCategory(this);
451}
452
453// A special subcommand representing no subcommand. It is particularly important
454// that this ManagedStatic uses constant initailization and not dynamic
455// initialization because it is referenced from cl::opt constructors, which run
456// dynamically in an arbitrary order.
459
460// A special subcommand that can be used to put an option into all subcommands.
462
464
466
468 GlobalParser->registerSubCommand(this);
469}
470
472 GlobalParser->unregisterSubCommand(this);
473}
474
476 PositionalOpts.clear();
477 SinkOpts.clear();
478 OptionsMap.clear();
479
480 ConsumeAfterOpt = nullptr;
481}
482
483SubCommand::operator bool() const {
484 return (GlobalParser->getActiveSubCommand() == this);
485}
486
487//===----------------------------------------------------------------------===//
488// Basic, shared command line option processing machinery.
489//
490
491/// LookupOption - Lookup the option specified by the specified option on the
492/// command line. If there is a value specified (after an equal sign) return
493/// that as well. This assumes that leading dashes have already been stripped.
494Option *CommandLineParser::LookupOption(SubCommand &Sub, StringRef &Arg,
495 StringRef &Value) {
496 // Reject all dashes.
497 if (Arg.empty())
498 return nullptr;
499 assert(&Sub != &SubCommand::getAll());
500
501 size_t EqualPos = Arg.find('=');
502
503 // If we have an equals sign, remember the value.
504 if (EqualPos == StringRef::npos) {
505 // Look up the option.
506 return Sub.OptionsMap.lookup(Arg);
507 }
508
509 // If the argument before the = is a valid option name and the option allows
510 // non-prefix form (ie is not AlwaysPrefix), we match. If not, signal match
511 // failure by returning nullptr.
512 auto I = Sub.OptionsMap.find(Arg.substr(0, EqualPos));
513 if (I == Sub.OptionsMap.end())
514 return nullptr;
515
516 auto *O = I->second;
517 if (O->getFormattingFlag() == cl::AlwaysPrefix)
518 return nullptr;
519
520 Value = Arg.substr(EqualPos + 1);
521 Arg = Arg.substr(0, EqualPos);
522 return I->second;
523}
524
525SubCommand *CommandLineParser::LookupSubCommand(StringRef Name,
526 std::string &NearestString) {
527 if (Name.empty())
528 return &SubCommand::getTopLevel();
529 // Find a subcommand with the edit distance == 1.
530 SubCommand *NearestMatch = nullptr;
531 for (auto *S : RegisteredSubCommands) {
532 assert(S != &SubCommand::getAll() &&
533 "SubCommand::getAll() is not expected in RegisteredSubCommands");
534 if (S->getName().empty())
535 continue;
536
537 if (StringRef(S->getName()) == StringRef(Name))
538 return S;
539
540 if (!NearestMatch && S->getName().edit_distance(Name) < 2)
541 NearestMatch = S;
542 }
543
544 if (NearestMatch)
545 NearestString = NearestMatch->getName();
546
547 return &SubCommand::getTopLevel();
548}
549
550/// LookupNearestOption - Lookup the closest match to the option specified by
551/// the specified option on the command line. If there is a value specified
552/// (after an equal sign) return that as well. This assumes that leading dashes
553/// have already been stripped.
555 const StringMap<Option *> &OptionsMap,
556 std::string &NearestString) {
557 // Reject all dashes.
558 if (Arg.empty())
559 return nullptr;
560
561 // Split on any equal sign.
562 std::pair<StringRef, StringRef> SplitArg = Arg.split('=');
563 StringRef &LHS = SplitArg.first; // LHS == Arg when no '=' is present.
564 StringRef &RHS = SplitArg.second;
565
566 // Find the closest match.
567 Option *Best = nullptr;
568 unsigned BestDistance = 0;
569 for (StringMap<Option *>::const_iterator it = OptionsMap.begin(),
570 ie = OptionsMap.end();
571 it != ie; ++it) {
572 Option *O = it->second;
573 // Do not suggest really hidden options (not shown in any help).
574 if (O->getOptionHiddenFlag() == ReallyHidden)
575 continue;
576
577 SmallVector<StringRef, 16> OptionNames;
578 O->getExtraOptionNames(OptionNames);
579 if (O->hasArgStr())
580 OptionNames.push_back(O->ArgStr);
581
582 bool PermitValue = O->getValueExpectedFlag() != cl::ValueDisallowed;
583 StringRef Flag = PermitValue ? LHS : Arg;
584 for (const auto &Name : OptionNames) {
585 unsigned Distance = StringRef(Name).edit_distance(
586 Flag, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
587 if (!Best || Distance < BestDistance) {
588 Best = O;
589 BestDistance = Distance;
590 if (RHS.empty() || !PermitValue)
591 NearestString = std::string(Name);
592 else
593 NearestString = (Twine(Name) + "=" + RHS).str();
594 }
595 }
596 }
597
598 return Best;
599}
600
601/// CommaSeparateAndAddOccurrence - A wrapper around Handler->addOccurrence()
602/// that does special handling of cl::CommaSeparated options.
603static bool CommaSeparateAndAddOccurrence(Option *Handler, unsigned pos,
604 StringRef ArgName, StringRef Value,
605 bool MultiArg = false) {
606 // Check to see if this option accepts a comma separated list of values. If
607 // it does, we have to split up the value into multiple values.
608 if (Handler->getMiscFlags() & CommaSeparated) {
609 StringRef Val(Value);
610 StringRef::size_type Pos = Val.find(',');
611
612 while (Pos != StringRef::npos) {
613 // Process the portion before the comma.
614 if (Handler->addOccurrence(pos, ArgName, Val.substr(0, Pos), MultiArg))
615 return true;
616 // Erase the portion before the comma, AND the comma.
617 Val = Val.substr(Pos + 1);
618 // Check for another comma.
619 Pos = Val.find(',');
620 }
621
622 Value = Val;
623 }
624
625 return Handler->addOccurrence(pos, ArgName, Value, MultiArg);
626}
627
628/// ProvideOption - For Value, this differentiates between an empty value ("")
629/// and a null value (StringRef()). The later is accepted for arguments that
630/// don't allow a value (-foo) the former is rejected (-foo=).
631static inline bool ProvideOption(Option *Handler, StringRef ArgName,
632 StringRef Value, int argc,
633 const char *const *argv, int &i) {
634 // Is this a multi-argument option?
635 unsigned NumAdditionalVals = Handler->getNumAdditionalVals();
636
637 // Enforce value requirements
638 switch (Handler->getValueExpectedFlag()) {
639 case ValueRequired:
640 if (!Value.data()) { // No value specified?
641 // If no other argument or the option only supports prefix form, we
642 // cannot look at the next argument.
643 if (i + 1 >= argc || Handler->getFormattingFlag() == cl::AlwaysPrefix)
644 return Handler->error("requires a value!");
645 // Steal the next argument, like for '-o filename'
646 assert(argv && "null check");
647 Value = StringRef(argv[++i]);
648 }
649 break;
650 case ValueDisallowed:
651 if (NumAdditionalVals > 0)
652 return Handler->error("multi-valued option specified"
653 " with ValueDisallowed modifier!");
654
655 if (Value.data())
656 return Handler->error("does not allow a value! '" + Twine(Value) +
657 "' specified.");
658 break;
659 case ValueOptional:
660 break;
661 }
662
663 // If this isn't a multi-arg option, just run the handler.
664 if (NumAdditionalVals == 0)
665 return CommaSeparateAndAddOccurrence(Handler, i, ArgName, Value);
666
667 // If it is, run the handle several times.
668 bool MultiArg = false;
669
670 if (Value.data()) {
671 if (CommaSeparateAndAddOccurrence(Handler, i, ArgName, Value, MultiArg))
672 return true;
673 --NumAdditionalVals;
674 MultiArg = true;
675 }
676
677 while (NumAdditionalVals > 0) {
678 if (i + 1 >= argc)
679 return Handler->error("not enough values!");
680 assert(argv && "null check");
681 Value = StringRef(argv[++i]);
682
683 if (CommaSeparateAndAddOccurrence(Handler, i, ArgName, Value, MultiArg))
684 return true;
685 MultiArg = true;
686 --NumAdditionalVals;
687 }
688 return false;
689}
690
692 int Dummy = i;
693 return ProvideOption(Handler, Handler->ArgStr, Arg, 0, nullptr, Dummy);
694}
695
696// getOptionPred - Check to see if there are any options that satisfy the
697// specified predicate with names that are the prefixes in Name. This is
698// checked by progressively stripping characters off of the name, checking to
699// see if there options that satisfy the predicate. If we find one, return it,
700// otherwise return null.
701//
703 bool (*Pred)(const Option *),
704 const StringMap<Option *> &OptionsMap) {
706 if (OMI != OptionsMap.end() && !Pred(OMI->getValue()))
707 OMI = OptionsMap.end();
708
709 // Loop while we haven't found an option and Name still has at least two
710 // characters in it (so that the next iteration will not be the empty
711 // string.
712 while (OMI == OptionsMap.end() && Name.size() > 1) {
713 Name = Name.substr(0, Name.size() - 1); // Chop off the last character.
714 OMI = OptionsMap.find(Name);
715 if (OMI != OptionsMap.end() && !Pred(OMI->getValue()))
716 OMI = OptionsMap.end();
717 }
718
719 if (OMI != OptionsMap.end() && Pred(OMI->second)) {
720 Length = Name.size();
721 return OMI->second; // Found one!
722 }
723 return nullptr; // No option found!
724}
725
726/// HandlePrefixedOrGroupedOption - The specified argument string (which started
727/// with at least one '-') does not fully match an available option. Check to
728/// see if this is a prefix or grouped option. If so, split arg into output an
729/// Arg/Value pair and return the Option to parse it with.
730static Option *
732 bool &ErrorParsing,
733 const StringMap<Option *> &OptionsMap) {
734 if (Arg.size() == 1)
735 return nullptr;
736
737 // Do the lookup!
738 size_t Length = 0;
739 Option *PGOpt = getOptionPred(Arg, Length, isPrefixedOrGrouping, OptionsMap);
740 if (!PGOpt)
741 return nullptr;
742
743 do {
744 StringRef MaybeValue =
745 (Length < Arg.size()) ? Arg.substr(Length) : StringRef();
746 Arg = Arg.substr(0, Length);
747 assert(OptionsMap.count(Arg) && OptionsMap.find(Arg)->second == PGOpt);
748
749 // cl::Prefix options do not preserve '=' when used separately.
750 // The behavior for them with grouped options should be the same.
751 if (MaybeValue.empty() || PGOpt->getFormattingFlag() == cl::AlwaysPrefix ||
752 (PGOpt->getFormattingFlag() == cl::Prefix && MaybeValue[0] != '=')) {
753 Value = MaybeValue;
754 return PGOpt;
755 }
756
757 if (MaybeValue[0] == '=') {
758 Value = MaybeValue.substr(1);
759 return PGOpt;
760 }
761
762 // This must be a grouped option.
763 assert(isGrouping(PGOpt) && "Broken getOptionPred!");
764
765 // Grouping options inside a group can't have values.
766 if (PGOpt->getValueExpectedFlag() == cl::ValueRequired) {
767 ErrorParsing |= PGOpt->error("may not occur within a group!");
768 return nullptr;
769 }
770
771 // Because the value for the option is not required, we don't need to pass
772 // argc/argv in.
773 int Dummy = 0;
774 ErrorParsing |= ProvideOption(PGOpt, Arg, StringRef(), 0, nullptr, Dummy);
775
776 // Get the next grouping option.
777 Arg = MaybeValue;
778 PGOpt = getOptionPred(Arg, Length, isGrouping, OptionsMap);
779 } while (PGOpt);
780
781 // We could not find a grouping option in the remainder of Arg.
782 return nullptr;
783}
784
785static bool RequiresValue(const Option *O) {
786 return O->getNumOccurrencesFlag() == cl::Required ||
787 O->getNumOccurrencesFlag() == cl::OneOrMore;
788}
789
790static bool EatsUnboundedNumberOfValues(const Option *O) {
791 return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
792 O->getNumOccurrencesFlag() == cl::OneOrMore;
793}
794
795static bool isWhitespace(char C) {
796 return C == ' ' || C == '\t' || C == '\r' || C == '\n';
797}
798
799static bool isWhitespaceOrNull(char C) {
800 return isWhitespace(C) || C == '\0';
801}
802
803static bool isQuote(char C) { return C == '\"' || C == '\''; }
804
807 bool MarkEOLs) {
808 SmallString<128> Token;
809 for (size_t I = 0, E = Src.size(); I != E; ++I) {
810 // Consume runs of whitespace.
811 if (Token.empty()) {
812 while (I != E && isWhitespace(Src[I])) {
813 // Mark the end of lines in response files.
814 if (MarkEOLs && Src[I] == '\n')
815 NewArgv.push_back(nullptr);
816 ++I;
817 }
818 if (I == E)
819 break;
820 }
821
822 char C = Src[I];
823
824 // Backslash escapes the next character.
825 if (I + 1 < E && C == '\\') {
826 ++I; // Skip the escape.
827 Token.push_back(Src[I]);
828 continue;
829 }
830
831 // Consume a quoted string.
832 if (isQuote(C)) {
833 ++I;
834 while (I != E && Src[I] != C) {
835 // Backslash escapes the next character.
836 if (Src[I] == '\\' && I + 1 != E)
837 ++I;
838 Token.push_back(Src[I]);
839 ++I;
840 }
841 if (I == E)
842 break;
843 continue;
844 }
845
846 // End the token if this is whitespace.
847 if (isWhitespace(C)) {
848 if (!Token.empty())
849 NewArgv.push_back(Saver.save(Token.str()).data());
850 // Mark the end of lines in response files.
851 if (MarkEOLs && C == '\n')
852 NewArgv.push_back(nullptr);
853 Token.clear();
854 continue;
855 }
856
857 // This is a normal character. Append it.
858 Token.push_back(C);
859 }
860
861 // Append the last token after hitting EOF with no whitespace.
862 if (!Token.empty())
863 NewArgv.push_back(Saver.save(Token.str()).data());
864}
865
866/// Backslashes are interpreted in a rather complicated way in the Windows-style
867/// command line, because backslashes are used both to separate path and to
868/// escape double quote. This method consumes runs of backslashes as well as the
869/// following double quote if it's escaped.
870///
871/// * If an even number of backslashes is followed by a double quote, one
872/// backslash is output for every pair of backslashes, and the last double
873/// quote remains unconsumed. The double quote will later be interpreted as
874/// the start or end of a quoted string in the main loop outside of this
875/// function.
876///
877/// * If an odd number of backslashes is followed by a double quote, one
878/// backslash is output for every pair of backslashes, and a double quote is
879/// output for the last pair of backslash-double quote. The double quote is
880/// consumed in this case.
881///
882/// * Otherwise, backslashes are interpreted literally.
883static size_t parseBackslash(StringRef Src, size_t I, SmallString<128> &Token) {
884 size_t E = Src.size();
885 int BackslashCount = 0;
886 // Skip the backslashes.
887 do {
888 ++I;
889 ++BackslashCount;
890 } while (I != E && Src[I] == '\\');
891
892 bool FollowedByDoubleQuote = (I != E && Src[I] == '"');
893 if (FollowedByDoubleQuote) {
894 Token.append(BackslashCount / 2, '\\');
895 if (BackslashCount % 2 == 0)
896 return I - 1;
897 Token.push_back('"');
898 return I;
899 }
900 Token.append(BackslashCount, '\\');
901 return I - 1;
902}
903
904// Windows treats whitespace, double quotes, and backslashes specially, except
905// when parsing the first token of a full command line, in which case
906// backslashes are not special.
907static bool isWindowsSpecialChar(char C) {
908 return isWhitespaceOrNull(C) || C == '\\' || C == '\"';
909}
911 return isWhitespaceOrNull(C) || C == '\"';
912}
913
914// Windows tokenization implementation. The implementation is designed to be
915// inlined and specialized for the two user entry points.
917 StringRef Src, StringSaver &Saver, function_ref<void(StringRef)> AddToken,
918 bool AlwaysCopy, function_ref<void()> MarkEOL, bool InitialCommandName) {
919 SmallString<128> Token;
920
921 // Sometimes, this function will be handling a full command line including an
922 // executable pathname at the start. In that situation, the initial pathname
923 // needs different handling from the following arguments, because when
924 // CreateProcess or cmd.exe scans the pathname, it doesn't treat \ as
925 // escaping the quote character, whereas when libc scans the rest of the
926 // command line, it does.
927 bool CommandName = InitialCommandName;
928
929 // Try to do as much work inside the state machine as possible.
930 enum { INIT, UNQUOTED, QUOTED } State = INIT;
931
932 for (size_t I = 0, E = Src.size(); I < E; ++I) {
933 switch (State) {
934 case INIT: {
935 assert(Token.empty() && "token should be empty in initial state");
936 // Eat whitespace before a token.
937 while (I < E && isWhitespaceOrNull(Src[I])) {
938 if (Src[I] == '\n')
939 MarkEOL();
940 ++I;
941 }
942 // Stop if this was trailing whitespace.
943 if (I >= E)
944 break;
945 size_t Start = I;
946 if (CommandName) {
947 while (I < E && !isWindowsSpecialCharInCommandName(Src[I]))
948 ++I;
949 } else {
950 while (I < E && !isWindowsSpecialChar(Src[I]))
951 ++I;
952 }
953 StringRef NormalChars = Src.slice(Start, I);
954 if (I >= E || isWhitespaceOrNull(Src[I])) {
955 // No special characters: slice out the substring and start the next
956 // token. Copy the string if the caller asks us to.
957 AddToken(AlwaysCopy ? Saver.save(NormalChars) : NormalChars);
958 if (I < E && Src[I] == '\n') {
959 MarkEOL();
960 CommandName = InitialCommandName;
961 } else {
962 CommandName = false;
963 }
964 } else if (Src[I] == '\"') {
965 Token += NormalChars;
966 State = QUOTED;
967 } else if (Src[I] == '\\') {
968 assert(!CommandName && "or else we'd have treated it as a normal char");
969 Token += NormalChars;
970 I = parseBackslash(Src, I, Token);
971 State = UNQUOTED;
972 } else {
973 llvm_unreachable("unexpected special character");
974 }
975 break;
976 }
977
978 case UNQUOTED:
979 if (isWhitespaceOrNull(Src[I])) {
980 // Whitespace means the end of the token. If we are in this state, the
981 // token must have contained a special character, so we must copy the
982 // token.
983 AddToken(Saver.save(Token.str()));
984 Token.clear();
985 if (Src[I] == '\n') {
986 CommandName = InitialCommandName;
987 MarkEOL();
988 } else {
989 CommandName = false;
990 }
991 State = INIT;
992 } else if (Src[I] == '\"') {
993 State = QUOTED;
994 } else if (Src[I] == '\\' && !CommandName) {
995 I = parseBackslash(Src, I, Token);
996 } else {
997 Token.push_back(Src[I]);
998 }
999 break;
1000
1001 case QUOTED:
1002 if (Src[I] == '\"') {
1003 if (I < (E - 1) && Src[I + 1] == '"') {
1004 // Consecutive double-quotes inside a quoted string implies one
1005 // double-quote.
1006 Token.push_back('"');
1007 ++I;
1008 } else {
1009 // Otherwise, end the quoted portion and return to the unquoted state.
1010 State = UNQUOTED;
1011 }
1012 } else if (Src[I] == '\\' && !CommandName) {
1013 I = parseBackslash(Src, I, Token);
1014 } else {
1015 Token.push_back(Src[I]);
1016 }
1017 break;
1018 }
1019 }
1020
1021 if (State != INIT)
1022 AddToken(Saver.save(Token.str()));
1023}
1024
1027 bool MarkEOLs) {
1028 auto AddToken = [&](StringRef Tok) { NewArgv.push_back(Tok.data()); };
1029 auto OnEOL = [&]() {
1030 if (MarkEOLs)
1031 NewArgv.push_back(nullptr);
1032 };
1033 tokenizeWindowsCommandLineImpl(Src, Saver, AddToken,
1034 /*AlwaysCopy=*/true, OnEOL, false);
1035}
1036
1038 SmallVectorImpl<StringRef> &NewArgv) {
1039 auto AddToken = [&](StringRef Tok) { NewArgv.push_back(Tok); };
1040 auto OnEOL = []() {};
1041 tokenizeWindowsCommandLineImpl(Src, Saver, AddToken, /*AlwaysCopy=*/false,
1042 OnEOL, false);
1043}
1044
1047 bool MarkEOLs) {
1048 auto AddToken = [&](StringRef Tok) { NewArgv.push_back(Tok.data()); };
1049 auto OnEOL = [&]() {
1050 if (MarkEOLs)
1051 NewArgv.push_back(nullptr);
1052 };
1053 tokenizeWindowsCommandLineImpl(Src, Saver, AddToken,
1054 /*AlwaysCopy=*/true, OnEOL, true);
1055}
1056
1059 bool MarkEOLs) {
1060 for (const char *Cur = Source.begin(); Cur != Source.end();) {
1061 SmallString<128> Line;
1062 // Check for comment line.
1063 if (isWhitespace(*Cur)) {
1064 while (Cur != Source.end() && isWhitespace(*Cur))
1065 ++Cur;
1066 continue;
1067 }
1068 if (*Cur == '#') {
1069 while (Cur != Source.end() && *Cur != '\n')
1070 ++Cur;
1071 continue;
1072 }
1073 // Find end of the current line.
1074 const char *Start = Cur;
1075 for (const char *End = Source.end(); Cur != End; ++Cur) {
1076 if (*Cur == '\\') {
1077 if (Cur + 1 != End) {
1078 ++Cur;
1079 if (*Cur == '\n' ||
1080 (*Cur == '\r' && (Cur + 1 != End) && Cur[1] == '\n')) {
1081 Line.append(Start, Cur - 1);
1082 if (*Cur == '\r')
1083 ++Cur;
1084 Start = Cur + 1;
1085 }
1086 }
1087 } else if (*Cur == '\n')
1088 break;
1089 }
1090 // Tokenize line.
1091 Line.append(Start, Cur);
1092 cl::TokenizeGNUCommandLine(Line, Saver, NewArgv, MarkEOLs);
1093 }
1094}
1095
1096// It is called byte order marker but the UTF-8 BOM is actually not affected
1097// by the host system's endianness.
1099 return (S.size() >= 3 && S[0] == '\xef' && S[1] == '\xbb' && S[2] == '\xbf');
1100}
1101
1102// Substitute <CFGDIR> with the file's base path.
1103static void ExpandBasePaths(StringRef BasePath, StringSaver &Saver,
1104 const char *&Arg) {
1105 assert(sys::path::is_absolute(BasePath));
1106 constexpr StringLiteral Token("<CFGDIR>");
1107 const StringRef ArgString(Arg);
1108
1109 SmallString<128> ResponseFile;
1110 StringRef::size_type StartPos = 0;
1111 for (StringRef::size_type TokenPos = ArgString.find(Token);
1112 TokenPos != StringRef::npos;
1113 TokenPos = ArgString.find(Token, StartPos)) {
1114 // Token may appear more than once per arg (e.g. comma-separated linker
1115 // args). Support by using path-append on any subsequent appearances.
1116 const StringRef LHS = ArgString.substr(StartPos, TokenPos - StartPos);
1117 if (ResponseFile.empty())
1118 ResponseFile = LHS;
1119 else
1120 llvm::sys::path::append(ResponseFile, LHS);
1121 ResponseFile.append(BasePath);
1122 StartPos = TokenPos + Token.size();
1123 }
1124
1125 if (!ResponseFile.empty()) {
1126 // Path-append the remaining arg substring if at least one token appeared.
1127 const StringRef Remaining = ArgString.substr(StartPos);
1128 if (!Remaining.empty())
1129 llvm::sys::path::append(ResponseFile, Remaining);
1130 Arg = Saver.save(ResponseFile.str()).data();
1131 }
1132}
1133
1134// FName must be an absolute path.
1135Error ExpansionContext::expandResponseFile(
1136 StringRef FName, SmallVectorImpl<const char *> &NewArgv) {
1139 FS->getBufferForFile(FName);
1140 if (!MemBufOrErr) {
1141 std::error_code EC = MemBufOrErr.getError();
1142 return llvm::createStringError(EC, Twine("cannot not open file '") + FName +
1143 "': " + EC.message());
1144 }
1145 MemoryBuffer &MemBuf = *MemBufOrErr.get();
1146 StringRef Str(MemBuf.getBufferStart(), MemBuf.getBufferSize());
1147
1148 // If we have a UTF-16 byte order mark, convert to UTF-8 for parsing.
1149 ArrayRef<char> BufRef(MemBuf.getBufferStart(), MemBuf.getBufferEnd());
1150 std::string UTF8Buf;
1151 if (hasUTF16ByteOrderMark(BufRef)) {
1152 if (!convertUTF16ToUTF8String(BufRef, UTF8Buf))
1153 return llvm::createStringError(std::errc::illegal_byte_sequence,
1154 "Could not convert UTF16 to UTF8");
1155 Str = StringRef(UTF8Buf);
1156 }
1157 // If we see UTF-8 BOM sequence at the beginning of a file, we shall remove
1158 // these bytes before parsing.
1159 // Reference: http://en.wikipedia.org/wiki/UTF-8#Byte_order_mark
1160 else if (hasUTF8ByteOrderMark(BufRef))
1161 Str = StringRef(BufRef.data() + 3, BufRef.size() - 3);
1162
1163 // Tokenize the contents into NewArgv.
1164 Tokenizer(Str, Saver, NewArgv, MarkEOLs);
1165
1166 // Expanded file content may require additional transformations, like using
1167 // absolute paths instead of relative in '@file' constructs or expanding
1168 // macros.
1169 if (!RelativeNames && !InConfigFile)
1170 return Error::success();
1171
1172 StringRef BasePath = llvm::sys::path::parent_path(FName);
1173 for (const char *&Arg : NewArgv) {
1174 if (!Arg)
1175 continue;
1176
1177 // Substitute <CFGDIR> with the file's base path.
1178 if (InConfigFile)
1179 ExpandBasePaths(BasePath, Saver, Arg);
1180
1181 // Discover the case, when argument should be transformed into '@file' and
1182 // evaluate 'file' for it.
1183 StringRef ArgStr(Arg);
1184 StringRef FileName;
1185 bool ConfigInclusion = false;
1186 if (ArgStr.consume_front("@")) {
1187 FileName = ArgStr;
1188 if (!llvm::sys::path::is_relative(FileName))
1189 continue;
1190 } else if (ArgStr.consume_front("--config=")) {
1191 FileName = ArgStr;
1192 ConfigInclusion = true;
1193 } else {
1194 continue;
1195 }
1196
1197 // Update expansion construct.
1198 SmallString<128> ResponseFile;
1199 ResponseFile.push_back('@');
1200 if (ConfigInclusion && !llvm::sys::path::has_parent_path(FileName)) {
1201 SmallString<128> FilePath;
1202 if (!findConfigFile(FileName, FilePath))
1203 return createStringError(
1204 std::make_error_code(std::errc::no_such_file_or_directory),
1205 "cannot not find configuration file: " + FileName);
1206 ResponseFile.append(FilePath);
1207 } else {
1208 ResponseFile.append(BasePath);
1209 llvm::sys::path::append(ResponseFile, FileName);
1210 }
1211 Arg = Saver.save(ResponseFile.str()).data();
1212 }
1213 return Error::success();
1214}
1215
1216/// Expand response files on a command line recursively using the given
1217/// StringSaver and tokenization strategy.
1220 struct ResponseFileRecord {
1221 std::string File;
1222 size_t End;
1223 };
1224
1225 // To detect recursive response files, we maintain a stack of files and the
1226 // position of the last argument in the file. This position is updated
1227 // dynamically as we recursively expand files.
1229
1230 // Push a dummy entry that represents the initial command line, removing
1231 // the need to check for an empty list.
1232 FileStack.push_back({"", Argv.size()});
1233
1234 // Don't cache Argv.size() because it can change.
1235 for (unsigned I = 0; I != Argv.size();) {
1236 while (I == FileStack.back().End) {
1237 // Passing the end of a file's argument list, so we can remove it from the
1238 // stack.
1239 FileStack.pop_back();
1240 }
1241
1242 const char *Arg = Argv[I];
1243 // Check if it is an EOL marker
1244 if (Arg == nullptr) {
1245 ++I;
1246 continue;
1247 }
1248
1249 if (Arg[0] != '@') {
1250 ++I;
1251 continue;
1252 }
1253
1254 const char *FName = Arg + 1;
1255 // Note that CurrentDir is only used for top-level rsp files, the rest will
1256 // always have an absolute path deduced from the containing file.
1257 SmallString<128> CurrDir;
1258 if (llvm::sys::path::is_relative(FName)) {
1259 if (CurrentDir.empty()) {
1260 if (auto CWD = FS->getCurrentWorkingDirectory()) {
1261 CurrDir = *CWD;
1262 } else {
1263 return createStringError(
1264 CWD.getError(), Twine("cannot get absolute path for: ") + FName);
1265 }
1266 } else {
1267 CurrDir = CurrentDir;
1268 }
1269 llvm::sys::path::append(CurrDir, FName);
1270 FName = CurrDir.c_str();
1271 }
1272
1273 ErrorOr<llvm::vfs::Status> Res = FS->status(FName);
1274 if (!Res || !Res->exists()) {
1275 std::error_code EC = Res.getError();
1276 if (!InConfigFile) {
1277 // If the specified file does not exist, leave '@file' unexpanded, as
1278 // libiberty does.
1279 if (!EC || EC == llvm::errc::no_such_file_or_directory) {
1280 ++I;
1281 continue;
1282 }
1283 }
1284 if (!EC)
1286 return createStringError(EC, Twine("cannot not open file '") + FName +
1287 "': " + EC.message());
1288 }
1289 const llvm::vfs::Status &FileStatus = Res.get();
1290
1291 auto IsEquivalent =
1292 [FileStatus, this](const ResponseFileRecord &RFile) -> ErrorOr<bool> {
1293 ErrorOr<llvm::vfs::Status> RHS = FS->status(RFile.File);
1294 if (!RHS)
1295 return RHS.getError();
1296 return FileStatus.equivalent(*RHS);
1297 };
1298
1299 // Check for recursive response files.
1300 for (const auto &F : drop_begin(FileStack)) {
1301 if (ErrorOr<bool> R = IsEquivalent(F)) {
1302 if (R.get())
1303 return createStringError(
1304 R.getError(), Twine("recursive expansion of: '") + F.File + "'");
1305 } else {
1306 return createStringError(R.getError(),
1307 Twine("cannot open file: ") + F.File);
1308 }
1309 }
1310
1311 // Replace this response file argument with the tokenization of its
1312 // contents. Nested response files are expanded in subsequent iterations.
1313 SmallVector<const char *, 0> ExpandedArgv;
1314 if (Error Err = expandResponseFile(FName, ExpandedArgv))
1315 return Err;
1316
1317 for (ResponseFileRecord &Record : FileStack) {
1318 // Increase the end of all active records by the number of newly expanded
1319 // arguments, minus the response file itself.
1320 Record.End += ExpandedArgv.size() - 1;
1321 }
1322
1323 FileStack.push_back({FName, I + ExpandedArgv.size()});
1324 Argv.erase(Argv.begin() + I);
1325 Argv.insert(Argv.begin() + I, ExpandedArgv.begin(), ExpandedArgv.end());
1326 }
1327
1328 // If successful, the top of the file stack will mark the end of the Argv
1329 // stream. A failure here indicates a bug in the stack popping logic above.
1330 // Note that FileStack may have more than one element at this point because we
1331 // don't have a chance to pop the stack when encountering recursive files at
1332 // the end of the stream, so seeing that doesn't indicate a bug.
1333 assert(FileStack.size() > 0 && Argv.size() == FileStack.back().End);
1334 return Error::success();
1335}
1336
1337bool cl::expandResponseFiles(int Argc, const char *const *Argv,
1338 const char *EnvVar, StringSaver &Saver,
1340#ifdef _WIN32
1341 auto Tokenize = cl::TokenizeWindowsCommandLine;
1342#else
1343 auto Tokenize = cl::TokenizeGNUCommandLine;
1344#endif
1345 // The environment variable specifies initial options.
1346 if (EnvVar)
1347 if (std::optional<std::string> EnvValue = sys::Process::GetEnv(EnvVar))
1348 Tokenize(*EnvValue, Saver, NewArgv, /*MarkEOLs=*/false);
1349
1350 // Command line options can override the environment variable.
1351 NewArgv.append(Argv + 1, Argv + Argc);
1352 ExpansionContext ECtx(Saver.getAllocator(), Tokenize);
1353 if (Error Err = ECtx.expandResponseFiles(NewArgv)) {
1354 errs() << toString(std::move(Err)) << '\n';
1355 return false;
1356 }
1357 return true;
1358}
1359
1362 ExpansionContext ECtx(Saver.getAllocator(), Tokenizer);
1363 if (Error Err = ECtx.expandResponseFiles(Argv)) {
1364 errs() << toString(std::move(Err)) << '\n';
1365 return false;
1366 }
1367 return true;
1368}
1369
1371 : Saver(A), Tokenizer(T), FS(vfs::getRealFileSystem().get()) {}
1372
1374 SmallVectorImpl<char> &FilePath) {
1375 SmallString<128> CfgFilePath;
1376 const auto FileExists = [this](SmallString<128> Path) -> bool {
1377 auto Status = FS->status(Path);
1378 return Status &&
1380 };
1381
1382 // If file name contains directory separator, treat it as a path to
1383 // configuration file.
1384 if (llvm::sys::path::has_parent_path(FileName)) {
1385 CfgFilePath = FileName;
1386 if (llvm::sys::path::is_relative(FileName) && FS->makeAbsolute(CfgFilePath))
1387 return false;
1388 if (!FileExists(CfgFilePath))
1389 return false;
1390 FilePath.assign(CfgFilePath.begin(), CfgFilePath.end());
1391 return true;
1392 }
1393
1394 // Look for the file in search directories.
1395 for (const StringRef &Dir : SearchDirs) {
1396 if (Dir.empty())
1397 continue;
1398 CfgFilePath.assign(Dir);
1399 llvm::sys::path::append(CfgFilePath, FileName);
1400 llvm::sys::path::native(CfgFilePath);
1401 if (FileExists(CfgFilePath)) {
1402 FilePath.assign(CfgFilePath.begin(), CfgFilePath.end());
1403 return true;
1404 }
1405 }
1406
1407 return false;
1408}
1409
1412 SmallString<128> AbsPath;
1413 if (sys::path::is_relative(CfgFile)) {
1414 AbsPath.assign(CfgFile);
1415 if (std::error_code EC = FS->makeAbsolute(AbsPath))
1416 return make_error<StringError>(
1417 EC, Twine("cannot get absolute path for " + CfgFile));
1418 CfgFile = AbsPath.str();
1419 }
1420 InConfigFile = true;
1421 RelativeNames = true;
1422 if (Error Err = expandResponseFile(CfgFile, Argv))
1423 return Err;
1424 return expandResponseFiles(Argv);
1425}
1426
1427static void initCommonOptions();
1428bool cl::ParseCommandLineOptions(int argc, const char *const *argv,
1429 StringRef Overview, raw_ostream *Errs,
1430 const char *EnvVar,
1431 bool LongOptionsUseDoubleDash) {
1435 StringSaver Saver(A);
1436 NewArgv.push_back(argv[0]);
1437
1438 // Parse options from environment variable.
1439 if (EnvVar) {
1440 if (std::optional<std::string> EnvValue =
1442 TokenizeGNUCommandLine(*EnvValue, Saver, NewArgv);
1443 }
1444
1445 // Append options from command line.
1446 for (int I = 1; I < argc; ++I)
1447 NewArgv.push_back(argv[I]);
1448 int NewArgc = static_cast<int>(NewArgv.size());
1449
1450 // Parse all options.
1451 return GlobalParser->ParseCommandLineOptions(NewArgc, &NewArgv[0], Overview,
1452 Errs, LongOptionsUseDoubleDash);
1453}
1454
1455/// Reset all options at least once, so that we can parse different options.
1456void CommandLineParser::ResetAllOptionOccurrences() {
1457 // Reset all option values to look like they have never been seen before.
1458 // Options might be reset twice (they can be reference in both OptionsMap
1459 // and one of the other members), but that does not harm.
1460 for (auto *SC : RegisteredSubCommands) {
1461 for (auto &O : SC->OptionsMap)
1462 O.second->reset();
1463 for (Option *O : SC->PositionalOpts)
1464 O->reset();
1465 for (Option *O : SC->SinkOpts)
1466 O->reset();
1467 if (SC->ConsumeAfterOpt)
1468 SC->ConsumeAfterOpt->reset();
1469 }
1470}
1471
1472bool CommandLineParser::ParseCommandLineOptions(int argc,
1473 const char *const *argv,
1474 StringRef Overview,
1475 raw_ostream *Errs,
1476 bool LongOptionsUseDoubleDash) {
1477 assert(hasOptions() && "No options specified!");
1478
1479 ProgramOverview = Overview;
1480 bool IgnoreErrors = Errs;
1481 if (!Errs)
1482 Errs = &errs();
1483 bool ErrorParsing = false;
1484
1485 // Expand response files.
1486 SmallVector<const char *, 20> newArgv(argv, argv + argc);
1488#ifdef _WIN32
1489 auto Tokenize = cl::TokenizeWindowsCommandLine;
1490#else
1491 auto Tokenize = cl::TokenizeGNUCommandLine;
1492#endif
1493 ExpansionContext ECtx(A, Tokenize);
1494 if (Error Err = ECtx.expandResponseFiles(newArgv)) {
1495 *Errs << toString(std::move(Err)) << '\n';
1496 return false;
1497 }
1498 argv = &newArgv[0];
1499 argc = static_cast<int>(newArgv.size());
1500
1501 // Copy the program name into ProgName, making sure not to overflow it.
1502 ProgramName = std::string(sys::path::filename(StringRef(argv[0])));
1503
1504 // Check out the positional arguments to collect information about them.
1505 unsigned NumPositionalRequired = 0;
1506
1507 // Determine whether or not there are an unlimited number of positionals
1508 bool HasUnlimitedPositionals = false;
1509
1510 int FirstArg = 1;
1511 SubCommand *ChosenSubCommand = &SubCommand::getTopLevel();
1512 std::string NearestSubCommandString;
1513 bool MaybeNamedSubCommand =
1514 argc >= 2 && argv[FirstArg][0] != '-' && hasNamedSubCommands();
1515 if (MaybeNamedSubCommand) {
1516 // If the first argument specifies a valid subcommand, start processing
1517 // options from the second argument.
1518 ChosenSubCommand =
1519 LookupSubCommand(StringRef(argv[FirstArg]), NearestSubCommandString);
1520 if (ChosenSubCommand != &SubCommand::getTopLevel())
1521 FirstArg = 2;
1522 }
1523 GlobalParser->ActiveSubCommand = ChosenSubCommand;
1524
1525 assert(ChosenSubCommand);
1526 auto &ConsumeAfterOpt = ChosenSubCommand->ConsumeAfterOpt;
1527 auto &PositionalOpts = ChosenSubCommand->PositionalOpts;
1528 auto &SinkOpts = ChosenSubCommand->SinkOpts;
1529 auto &OptionsMap = ChosenSubCommand->OptionsMap;
1530
1531 for (auto *O: DefaultOptions) {
1532 addOption(O, true);
1533 }
1534
1535 if (ConsumeAfterOpt) {
1536 assert(PositionalOpts.size() > 0 &&
1537 "Cannot specify cl::ConsumeAfter without a positional argument!");
1538 }
1539 if (!PositionalOpts.empty()) {
1540
1541 // Calculate how many positional values are _required_.
1542 bool UnboundedFound = false;
1543 for (size_t i = 0, e = PositionalOpts.size(); i != e; ++i) {
1544 Option *Opt = PositionalOpts[i];
1545 if (RequiresValue(Opt))
1546 ++NumPositionalRequired;
1547 else if (ConsumeAfterOpt) {
1548 // ConsumeAfter cannot be combined with "optional" positional options
1549 // unless there is only one positional argument...
1550 if (PositionalOpts.size() > 1) {
1551 if (!IgnoreErrors)
1552 Opt->error("error - this positional option will never be matched, "
1553 "because it does not Require a value, and a "
1554 "cl::ConsumeAfter option is active!");
1555 ErrorParsing = true;
1556 }
1557 } else if (UnboundedFound && !Opt->hasArgStr()) {
1558 // This option does not "require" a value... Make sure this option is
1559 // not specified after an option that eats all extra arguments, or this
1560 // one will never get any!
1561 //
1562 if (!IgnoreErrors)
1563 Opt->error("error - option can never match, because "
1564 "another positional argument will match an "
1565 "unbounded number of values, and this option"
1566 " does not require a value!");
1567 *Errs << ProgramName << ": CommandLine Error: Option '" << Opt->ArgStr
1568 << "' is all messed up!\n";
1569 *Errs << PositionalOpts.size();
1570 ErrorParsing = true;
1571 }
1572 UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
1573 }
1574 HasUnlimitedPositionals = UnboundedFound || ConsumeAfterOpt;
1575 }
1576
1577 // PositionalVals - A vector of "positional" arguments we accumulate into
1578 // the process at the end.
1579 //
1581
1582 // If the program has named positional arguments, and the name has been run
1583 // across, keep track of which positional argument was named. Otherwise put
1584 // the positional args into the PositionalVals list...
1585 Option *ActivePositionalArg = nullptr;
1586
1587 // Loop over all of the arguments... processing them.
1588 bool DashDashFound = false; // Have we read '--'?
1589 for (int i = FirstArg; i < argc; ++i) {
1590 Option *Handler = nullptr;
1591 std::string NearestHandlerString;
1593 StringRef ArgName = "";
1594 bool HaveDoubleDash = false;
1595
1596 // Check to see if this is a positional argument. This argument is
1597 // considered to be positional if it doesn't start with '-', if it is "-"
1598 // itself, or if we have seen "--" already.
1599 //
1600 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
1601 // Positional argument!
1602 if (ActivePositionalArg) {
1603 ProvidePositionalOption(ActivePositionalArg, StringRef(argv[i]), i);
1604 continue; // We are done!
1605 }
1606
1607 if (!PositionalOpts.empty()) {
1608 PositionalVals.push_back(std::make_pair(StringRef(argv[i]), i));
1609
1610 // All of the positional arguments have been fulfulled, give the rest to
1611 // the consume after option... if it's specified...
1612 //
1613 if (PositionalVals.size() >= NumPositionalRequired && ConsumeAfterOpt) {
1614 for (++i; i < argc; ++i)
1615 PositionalVals.push_back(std::make_pair(StringRef(argv[i]), i));
1616 break; // Handle outside of the argument processing loop...
1617 }
1618
1619 // Delay processing positional arguments until the end...
1620 continue;
1621 }
1622 } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
1623 !DashDashFound) {
1624 DashDashFound = true; // This is the mythical "--"?
1625 continue; // Don't try to process it as an argument itself.
1626 } else if (ActivePositionalArg &&
1627 (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
1628 // If there is a positional argument eating options, check to see if this
1629 // option is another positional argument. If so, treat it as an argument,
1630 // otherwise feed it to the eating positional.
1631 ArgName = StringRef(argv[i] + 1);
1632 // Eat second dash.
1633 if (ArgName.consume_front("-"))
1634 HaveDoubleDash = true;
1635
1636 Handler = LookupLongOption(*ChosenSubCommand, ArgName, Value,
1637 LongOptionsUseDoubleDash, HaveDoubleDash);
1638 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
1639 ProvidePositionalOption(ActivePositionalArg, StringRef(argv[i]), i);
1640 continue; // We are done!
1641 }
1642 } else { // We start with a '-', must be an argument.
1643 ArgName = StringRef(argv[i] + 1);
1644 // Eat second dash.
1645 if (ArgName.consume_front("-"))
1646 HaveDoubleDash = true;
1647
1648 Handler = LookupLongOption(*ChosenSubCommand, ArgName, Value,
1649 LongOptionsUseDoubleDash, HaveDoubleDash);
1650
1651 // If Handler is not found in a specialized subcommand, look up handler
1652 // in the top-level subcommand.
1653 // cl::opt without cl::sub belongs to top-level subcommand.
1654 if (!Handler && ChosenSubCommand != &SubCommand::getTopLevel())
1655 Handler = LookupLongOption(SubCommand::getTopLevel(), ArgName, Value,
1656 LongOptionsUseDoubleDash, HaveDoubleDash);
1657
1658 // Check to see if this "option" is really a prefixed or grouped argument.
1659 if (!Handler && !(LongOptionsUseDoubleDash && HaveDoubleDash))
1660 Handler = HandlePrefixedOrGroupedOption(ArgName, Value, ErrorParsing,
1661 OptionsMap);
1662
1663 // Otherwise, look for the closest available option to report to the user
1664 // in the upcoming error.
1665 if (!Handler && SinkOpts.empty())
1666 LookupNearestOption(ArgName, OptionsMap, NearestHandlerString);
1667 }
1668
1669 if (!Handler) {
1670 if (!SinkOpts.empty()) {
1671 for (Option *SinkOpt : SinkOpts)
1672 SinkOpt->addOccurrence(i, "", StringRef(argv[i]));
1673 continue;
1674 }
1675
1676 auto ReportUnknownArgument = [&](bool IsArg,
1677 StringRef NearestArgumentName) {
1678 *Errs << ProgramName << ": Unknown "
1679 << (IsArg ? "command line argument" : "subcommand") << " '"
1680 << argv[i] << "'. Try: '" << argv[0] << " --help'\n";
1681
1682 if (NearestArgumentName.empty())
1683 return;
1684
1685 *Errs << ProgramName << ": Did you mean '";
1686 if (IsArg)
1687 *Errs << PrintArg(NearestArgumentName, 0);
1688 else
1689 *Errs << NearestArgumentName;
1690 *Errs << "'?\n";
1691 };
1692
1693 if (i > 1 || !MaybeNamedSubCommand)
1694 ReportUnknownArgument(/*IsArg=*/true, NearestHandlerString);
1695 else
1696 ReportUnknownArgument(/*IsArg=*/false, NearestSubCommandString);
1697
1698 ErrorParsing = true;
1699 continue;
1700 }
1701
1702 // If this is a named positional argument, just remember that it is the
1703 // active one...
1704 if (Handler->getFormattingFlag() == cl::Positional) {
1705 if ((Handler->getMiscFlags() & PositionalEatsArgs) && !Value.empty()) {
1706 Handler->error("This argument does not take a value.\n"
1707 "\tInstead, it consumes any positional arguments until "
1708 "the next recognized option.", *Errs);
1709 ErrorParsing = true;
1710 }
1711 ActivePositionalArg = Handler;
1712 }
1713 else
1714 ErrorParsing |= ProvideOption(Handler, ArgName, Value, argc, argv, i);
1715 }
1716
1717 // Check and handle positional arguments now...
1718 if (NumPositionalRequired > PositionalVals.size()) {
1719 *Errs << ProgramName
1720 << ": Not enough positional command line arguments specified!\n"
1721 << "Must specify at least " << NumPositionalRequired
1722 << " positional argument" << (NumPositionalRequired > 1 ? "s" : "")
1723 << ": See: " << argv[0] << " --help\n";
1724
1725 ErrorParsing = true;
1726 } else if (!HasUnlimitedPositionals &&
1727 PositionalVals.size() > PositionalOpts.size()) {
1728 *Errs << ProgramName << ": Too many positional arguments specified!\n"
1729 << "Can specify at most " << PositionalOpts.size()
1730 << " positional arguments: See: " << argv[0] << " --help\n";
1731 ErrorParsing = true;
1732
1733 } else if (!ConsumeAfterOpt) {
1734 // Positional args have already been handled if ConsumeAfter is specified.
1735 unsigned ValNo = 0, NumVals = static_cast<unsigned>(PositionalVals.size());
1736 for (size_t i = 0, e = PositionalOpts.size(); i != e; ++i) {
1737 if (RequiresValue(PositionalOpts[i])) {
1738 ProvidePositionalOption(PositionalOpts[i], PositionalVals[ValNo].first,
1739 PositionalVals[ValNo].second);
1740 ValNo++;
1741 --NumPositionalRequired; // We fulfilled our duty...
1742 }
1743
1744 // If we _can_ give this option more arguments, do so now, as long as we
1745 // do not give it values that others need. 'Done' controls whether the
1746 // option even _WANTS_ any more.
1747 //
1748 bool Done = PositionalOpts[i]->getNumOccurrencesFlag() == cl::Required;
1749 while (NumVals - ValNo > NumPositionalRequired && !Done) {
1750 switch (PositionalOpts[i]->getNumOccurrencesFlag()) {
1751 case cl::Optional:
1752 Done = true; // Optional arguments want _at most_ one value
1753 [[fallthrough]];
1754 case cl::ZeroOrMore: // Zero or more will take all they can get...
1755 case cl::OneOrMore: // One or more will take all they can get...
1756 ProvidePositionalOption(PositionalOpts[i],
1757 PositionalVals[ValNo].first,
1758 PositionalVals[ValNo].second);
1759 ValNo++;
1760 break;
1761 default:
1762 llvm_unreachable("Internal error, unexpected NumOccurrences flag in "
1763 "positional argument processing!");
1764 }
1765 }
1766 }
1767 } else {
1768 assert(ConsumeAfterOpt && NumPositionalRequired <= PositionalVals.size());
1769 unsigned ValNo = 0;
1770 for (size_t J = 0, E = PositionalOpts.size(); J != E; ++J)
1771 if (RequiresValue(PositionalOpts[J])) {
1772 ErrorParsing |= ProvidePositionalOption(PositionalOpts[J],
1773 PositionalVals[ValNo].first,
1774 PositionalVals[ValNo].second);
1775 ValNo++;
1776 }
1777
1778 // Handle the case where there is just one positional option, and it's
1779 // optional. In this case, we want to give JUST THE FIRST option to the
1780 // positional option and keep the rest for the consume after. The above
1781 // loop would have assigned no values to positional options in this case.
1782 //
1783 if (PositionalOpts.size() == 1 && ValNo == 0 && !PositionalVals.empty()) {
1784 ErrorParsing |= ProvidePositionalOption(PositionalOpts[0],
1785 PositionalVals[ValNo].first,
1786 PositionalVals[ValNo].second);
1787 ValNo++;
1788 }
1789
1790 // Handle over all of the rest of the arguments to the
1791 // cl::ConsumeAfter command line option...
1792 for (; ValNo != PositionalVals.size(); ++ValNo)
1793 ErrorParsing |=
1794 ProvidePositionalOption(ConsumeAfterOpt, PositionalVals[ValNo].first,
1795 PositionalVals[ValNo].second);
1796 }
1797
1798 // Loop over args and make sure all required args are specified!
1799 for (const auto &Opt : OptionsMap) {
1800 switch (Opt.second->getNumOccurrencesFlag()) {
1801 case Required:
1802 case OneOrMore:
1803 if (Opt.second->getNumOccurrences() == 0) {
1804 Opt.second->error("must be specified at least once!");
1805 ErrorParsing = true;
1806 }
1807 [[fallthrough]];
1808 default:
1809 break;
1810 }
1811 }
1812
1813 // Now that we know if -debug is specified, we can use it.
1814 // Note that if ReadResponseFiles == true, this must be done before the
1815 // memory allocated for the expanded command line is free()d below.
1816 LLVM_DEBUG(dbgs() << "Args: ";
1817 for (int i = 0; i < argc; ++i) dbgs() << argv[i] << ' ';
1818 dbgs() << '\n';);
1819
1820 // Free all of the memory allocated to the map. Command line options may only
1821 // be processed once!
1822 MoreHelp.clear();
1823
1824 // If we had an error processing our arguments, don't let the program execute
1825 if (ErrorParsing) {
1826 if (!IgnoreErrors)
1827 exit(1);
1828 return false;
1829 }
1830 return true;
1831}
1832
1833//===----------------------------------------------------------------------===//
1834// Option Base class implementation
1835//
1836
1837bool Option::error(const Twine &Message, StringRef ArgName, raw_ostream &Errs) {
1838 if (!ArgName.data())
1839 ArgName = ArgStr;
1840 if (ArgName.empty())
1841 Errs << HelpStr; // Be nice for positional arguments
1842 else
1843 Errs << GlobalParser->ProgramName << ": for the " << PrintArg(ArgName, 0);
1844
1845 Errs << " option: " << Message << "\n";
1846 return true;
1847}
1848
1850 bool MultiArg) {
1851 if (!MultiArg)
1852 NumOccurrences++; // Increment the number of times we have been seen
1853
1854 return handleOccurrence(pos, ArgName, Value);
1855}
1856
1857// getValueStr - Get the value description string, using "DefaultMsg" if nothing
1858// has been specified yet.
1859//
1860static StringRef getValueStr(const Option &O, StringRef DefaultMsg) {
1861 if (O.ValueStr.empty())
1862 return DefaultMsg;
1863 return O.ValueStr;
1864}
1865
1866//===----------------------------------------------------------------------===//
1867// cl::alias class implementation
1868//
1869
1870// Return the width of the option tag for printing...
1871size_t alias::getOptionWidth() const {
1873}
1874
1875void Option::printHelpStr(StringRef HelpStr, size_t Indent,
1876 size_t FirstLineIndentedBy) {
1877 assert(Indent >= FirstLineIndentedBy);
1878 std::pair<StringRef, StringRef> Split = HelpStr.split('\n');
1879 outs().indent(Indent - FirstLineIndentedBy)
1880 << ArgHelpPrefix << Split.first << "\n";
1881 while (!Split.second.empty()) {
1882 Split = Split.second.split('\n');
1883 outs().indent(Indent) << Split.first << "\n";
1884 }
1885}
1886
1887void Option::printEnumValHelpStr(StringRef HelpStr, size_t BaseIndent,
1888 size_t FirstLineIndentedBy) {
1889 const StringRef ValHelpPrefix = " ";
1890 assert(BaseIndent >= FirstLineIndentedBy);
1891 std::pair<StringRef, StringRef> Split = HelpStr.split('\n');
1892 outs().indent(BaseIndent - FirstLineIndentedBy)
1893 << ArgHelpPrefix << ValHelpPrefix << Split.first << "\n";
1894 while (!Split.second.empty()) {
1895 Split = Split.second.split('\n');
1896 outs().indent(BaseIndent + ValHelpPrefix.size()) << Split.first << "\n";
1897 }
1898}
1899
1900// Print out the option for the alias.
1901void alias::printOptionInfo(size_t GlobalWidth) const {
1902 outs() << PrintArg(ArgStr);
1904}
1905
1906//===----------------------------------------------------------------------===//
1907// Parser Implementation code...
1908//
1909
1910// basic_parser implementation
1911//
1912
1913// Return the width of the option tag for printing...
1915 size_t Len = argPlusPrefixesSize(O.ArgStr);
1916 auto ValName = getValueName();
1917 if (!ValName.empty()) {
1918 size_t FormattingLen = 3;
1919 if (O.getMiscFlags() & PositionalEatsArgs)
1920 FormattingLen = 6;
1921 Len += getValueStr(O, ValName).size() + FormattingLen;
1922 }
1923
1924 return Len;
1925}
1926
1927// printOptionInfo - Print out information about this option. The
1928// to-be-maintained width is specified.
1929//
1931 size_t GlobalWidth) const {
1932 outs() << PrintArg(O.ArgStr);
1933
1934 auto ValName = getValueName();
1935 if (!ValName.empty()) {
1936 if (O.getMiscFlags() & PositionalEatsArgs) {
1937 outs() << " <" << getValueStr(O, ValName) << ">...";
1938 } else if (O.getValueExpectedFlag() == ValueOptional)
1939 outs() << "[=<" << getValueStr(O, ValName) << ">]";
1940 else {
1941 outs() << (O.ArgStr.size() == 1 ? " <" : "=<") << getValueStr(O, ValName)
1942 << '>';
1943 }
1944 }
1945
1946 Option::printHelpStr(O.HelpStr, GlobalWidth, getOptionWidth(O));
1947}
1948
1950 size_t GlobalWidth) const {
1951 outs() << PrintArg(O.ArgStr);
1952 outs().indent(GlobalWidth - O.ArgStr.size());
1953}
1954
1955// parser<bool> implementation
1956//
1957bool parser<bool>::parse(Option &O, StringRef ArgName, StringRef Arg,
1958 bool &Value) {
1959 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
1960 Arg == "1") {
1961 Value = true;
1962 return false;
1963 }
1964
1965 if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
1966 Value = false;
1967 return false;
1968 }
1969 return O.error("'" + Arg +
1970 "' is invalid value for boolean argument! Try 0 or 1");
1971}
1972
1973// parser<boolOrDefault> implementation
1974//
1977 if (Arg == "" || Arg == "true" || Arg == "TRUE" || Arg == "True" ||
1978 Arg == "1") {
1979 Value = BOU_TRUE;
1980 return false;
1981 }
1982 if (Arg == "false" || Arg == "FALSE" || Arg == "False" || Arg == "0") {
1983 Value = BOU_FALSE;
1984 return false;
1985 }
1986
1987 return O.error("'" + Arg +
1988 "' is invalid value for boolean argument! Try 0 or 1");
1989}
1990
1991// parser<int> implementation
1992//
1993bool parser<int>::parse(Option &O, StringRef ArgName, StringRef Arg,
1994 int &Value) {
1995 if (Arg.getAsInteger(0, Value))
1996 return O.error("'" + Arg + "' value invalid for integer argument!");
1997 return false;
1998}
1999
2000// parser<long> implementation
2001//
2002bool parser<long>::parse(Option &O, StringRef ArgName, StringRef Arg,
2003 long &Value) {
2004 if (Arg.getAsInteger(0, Value))
2005 return O.error("'" + Arg + "' value invalid for long argument!");
2006 return false;
2007}
2008
2009// parser<long long> implementation
2010//
2012 long long &Value) {
2013 if (Arg.getAsInteger(0, Value))
2014 return O.error("'" + Arg + "' value invalid for llong argument!");
2015 return false;
2016}
2017
2018// parser<unsigned> implementation
2019//
2021 unsigned &Value) {
2022
2023 if (Arg.getAsInteger(0, Value))
2024 return O.error("'" + Arg + "' value invalid for uint argument!");
2025 return false;
2026}
2027
2028// parser<unsigned long> implementation
2029//
2031 unsigned long &Value) {
2032
2033 if (Arg.getAsInteger(0, Value))
2034 return O.error("'" + Arg + "' value invalid for ulong argument!");
2035 return false;
2036}
2037
2038// parser<unsigned long long> implementation
2039//
2041 StringRef Arg,
2042 unsigned long long &Value) {
2043
2044 if (Arg.getAsInteger(0, Value))
2045 return O.error("'" + Arg + "' value invalid for ullong argument!");
2046 return false;
2047}
2048
2049// parser<double>/parser<float> implementation
2050//
2051static bool parseDouble(Option &O, StringRef Arg, double &Value) {
2052 if (to_float(Arg, Value))
2053 return false;
2054 return O.error("'" + Arg + "' value invalid for floating point argument!");
2055}
2056
2057bool parser<double>::parse(Option &O, StringRef ArgName, StringRef Arg,
2058 double &Val) {
2059 return parseDouble(O, Arg, Val);
2060}
2061
2062bool parser<float>::parse(Option &O, StringRef ArgName, StringRef Arg,
2063 float &Val) {
2064 double dVal;
2065 if (parseDouble(O, Arg, dVal))
2066 return true;
2067 Val = (float)dVal;
2068 return false;
2069}
2070
2071// generic_parser_base implementation
2072//
2073
2074// findOption - Return the option number corresponding to the specified
2075// argument string. If the option is not found, getNumOptions() is returned.
2076//
2078 unsigned e = getNumOptions();
2079
2080 for (unsigned i = 0; i != e; ++i) {
2081 if (getOption(i) == Name)
2082 return i;
2083 }
2084 return e;
2085}
2086
2087static StringRef EqValue = "=<value>";
2088static StringRef EmptyOption = "<empty>";
2090static size_t getOptionPrefixesSize() {
2091 return OptionPrefix.size() + ArgHelpPrefix.size();
2092}
2093
2095 const Option &O) {
2096 return O.getValueExpectedFlag() != ValueOptional || !Name.empty() ||
2097 !Description.empty();
2098}
2099
2100// Return the width of the option tag for printing...
2102 if (O.hasArgStr()) {
2103 size_t Size =
2104 argPlusPrefixesSize(O.ArgStr) + EqValue.size();
2105 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
2108 continue;
2109 size_t NameSize = Name.empty() ? EmptyOption.size() : Name.size();
2110 Size = std::max(Size, NameSize + getOptionPrefixesSize());
2111 }
2112 return Size;
2113 } else {
2114 size_t BaseSize = 0;
2115 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
2116 BaseSize = std::max(BaseSize, getOption(i).size() + 8);
2117 return BaseSize;
2118 }
2119}
2120
2121// printOptionInfo - Print out information about this option. The
2122// to-be-maintained width is specified.
2123//
2125 size_t GlobalWidth) const {
2126 if (O.hasArgStr()) {
2127 // When the value is optional, first print a line just describing the
2128 // option without values.
2129 if (O.getValueExpectedFlag() == ValueOptional) {
2130 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
2131 if (getOption(i).empty()) {
2132 outs() << PrintArg(O.ArgStr);
2133 Option::printHelpStr(O.HelpStr, GlobalWidth,
2134 argPlusPrefixesSize(O.ArgStr));
2135 break;
2136 }
2137 }
2138 }
2139
2140 outs() << PrintArg(O.ArgStr) << EqValue;
2141 Option::printHelpStr(O.HelpStr, GlobalWidth,
2142 EqValue.size() +
2143 argPlusPrefixesSize(O.ArgStr));
2144 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
2145 StringRef OptionName = getOption(i);
2146 StringRef Description = getDescription(i);
2147 if (!shouldPrintOption(OptionName, Description, O))
2148 continue;
2149 size_t FirstLineIndent = OptionName.size() + getOptionPrefixesSize();
2150 outs() << OptionPrefix << OptionName;
2151 if (OptionName.empty()) {
2152 outs() << EmptyOption;
2153 assert(FirstLineIndent >= EmptyOption.size());
2154 FirstLineIndent += EmptyOption.size();
2155 }
2156 if (!Description.empty())
2157 Option::printEnumValHelpStr(Description, GlobalWidth, FirstLineIndent);
2158 else
2159 outs() << '\n';
2160 }
2161 } else {
2162 if (!O.HelpStr.empty())
2163 outs() << " " << O.HelpStr << '\n';
2164 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
2166 outs() << " " << PrintArg(Option);
2167 Option::printHelpStr(getDescription(i), GlobalWidth, Option.size() + 8);
2168 }
2169 }
2170}
2171
2172static const size_t MaxOptWidth = 8; // arbitrary spacing for printOptionDiff
2173
2174// printGenericOptionDiff - Print the value of this option and it's default.
2175//
2176// "Generic" options have each value mapped to a name.
2178 const Option &O, const GenericOptionValue &Value,
2179 const GenericOptionValue &Default, size_t GlobalWidth) const {
2180 outs() << " " << PrintArg(O.ArgStr);
2181 outs().indent(GlobalWidth - O.ArgStr.size());
2182
2183 unsigned NumOpts = getNumOptions();
2184 for (unsigned i = 0; i != NumOpts; ++i) {
2185 if (!Value.compare(getOptionValue(i)))
2186 continue;
2187
2188 outs() << "= " << getOption(i);
2189 size_t L = getOption(i).size();
2190 size_t NumSpaces = MaxOptWidth > L ? MaxOptWidth - L : 0;
2191 outs().indent(NumSpaces) << " (default: ";
2192 for (unsigned j = 0; j != NumOpts; ++j) {
2193 if (!Default.compare(getOptionValue(j)))
2194 continue;
2195 outs() << getOption(j);
2196 break;
2197 }
2198 outs() << ")\n";
2199 return;
2200 }
2201 outs() << "= *unknown option value*\n";
2202}
2203
2204// printOptionDiff - Specializations for printing basic value types.
2205//
2206#define PRINT_OPT_DIFF(T) \
2207 void parser<T>::printOptionDiff(const Option &O, T V, OptionValue<T> D, \
2208 size_t GlobalWidth) const { \
2209 printOptionName(O, GlobalWidth); \
2210 std::string Str; \
2211 { \
2212 raw_string_ostream SS(Str); \
2213 SS << V; \
2214 } \
2215 outs() << "= " << Str; \
2216 size_t NumSpaces = \
2217 MaxOptWidth > Str.size() ? MaxOptWidth - Str.size() : 0; \
2218 outs().indent(NumSpaces) << " (default: "; \
2219 if (D.hasValue()) \
2220 outs() << D.getValue(); \
2221 else \
2222 outs() << "*no default*"; \
2223 outs() << ")\n"; \
2224 }
2225
2226PRINT_OPT_DIFF(bool)
2228PRINT_OPT_DIFF(int)
2229PRINT_OPT_DIFF(long)
2230PRINT_OPT_DIFF(long long)
2231PRINT_OPT_DIFF(unsigned)
2232PRINT_OPT_DIFF(unsigned long)
2233PRINT_OPT_DIFF(unsigned long long)
2234PRINT_OPT_DIFF(double)
2235PRINT_OPT_DIFF(float)
2236PRINT_OPT_DIFF(char)
2237
2240 size_t GlobalWidth) const {
2241 printOptionName(O, GlobalWidth);
2242 outs() << "= " << V;
2243 size_t NumSpaces = MaxOptWidth > V.size() ? MaxOptWidth - V.size() : 0;
2244 outs().indent(NumSpaces) << " (default: ";
2245 if (D.hasValue())
2246 outs() << D.getValue();
2247 else
2248 outs() << "*no default*";
2249 outs() << ")\n";
2250}
2251
2252// Print a placeholder for options that don't yet support printOptionDiff().
2254 size_t GlobalWidth) const {
2255 printOptionName(O, GlobalWidth);
2256 outs() << "= *cannot print option value*\n";
2257}
2258
2259//===----------------------------------------------------------------------===//
2260// -help and -help-hidden option implementation
2261//
2262
2263static int OptNameCompare(const std::pair<const char *, Option *> *LHS,
2264 const std::pair<const char *, Option *> *RHS) {
2265 return strcmp(LHS->first, RHS->first);
2266}
2267
2268static int SubNameCompare(const std::pair<const char *, SubCommand *> *LHS,
2269 const std::pair<const char *, SubCommand *> *RHS) {
2270 return strcmp(LHS->first, RHS->first);
2271}
2272
2273// Copy Options into a vector so we can sort them as we like.
2274static void sortOpts(StringMap<Option *> &OptMap,
2275 SmallVectorImpl<std::pair<const char *, Option *>> &Opts,
2276 bool ShowHidden) {
2277 SmallPtrSet<Option *, 32> OptionSet; // Duplicate option detection.
2278
2279 for (StringMap<Option *>::iterator I = OptMap.begin(), E = OptMap.end();
2280 I != E; ++I) {
2281 // Ignore really-hidden options.
2282 if (I->second->getOptionHiddenFlag() == ReallyHidden)
2283 continue;
2284
2285 // Unless showhidden is set, ignore hidden flags.
2286 if (I->second->getOptionHiddenFlag() == Hidden && !ShowHidden)
2287 continue;
2288
2289 // If we've already seen this option, don't add it to the list again.
2290 if (!OptionSet.insert(I->second).second)
2291 continue;
2292
2293 Opts.push_back(
2294 std::pair<const char *, Option *>(I->getKey().data(), I->second));
2295 }
2296
2297 // Sort the options list alphabetically.
2298 array_pod_sort(Opts.begin(), Opts.end(), OptNameCompare);
2299}
2300
2301static void
2303 SmallVectorImpl<std::pair<const char *, SubCommand *>> &Subs) {
2304 for (auto *S : SubMap) {
2305 if (S->getName().empty())
2306 continue;
2307 Subs.push_back(std::make_pair(S->getName().data(), S));
2308 }
2309 array_pod_sort(Subs.begin(), Subs.end(), SubNameCompare);
2310}
2311
2312namespace {
2313
2314class HelpPrinter {
2315protected:
2316 const bool ShowHidden;
2318 StrOptionPairVector;
2320 StrSubCommandPairVector;
2321 // Print the options. Opts is assumed to be alphabetically sorted.
2322 virtual void printOptions(StrOptionPairVector &Opts, size_t MaxArgLen) {
2323 for (size_t i = 0, e = Opts.size(); i != e; ++i)
2324 Opts[i].second->printOptionInfo(MaxArgLen);
2325 }
2326
2327 void printSubCommands(StrSubCommandPairVector &Subs, size_t MaxSubLen) {
2328 for (const auto &S : Subs) {
2329 outs() << " " << S.first;
2330 if (!S.second->getDescription().empty()) {
2331 outs().indent(MaxSubLen - strlen(S.first));
2332 outs() << " - " << S.second->getDescription();
2333 }
2334 outs() << "\n";
2335 }
2336 }
2337
2338public:
2339 explicit HelpPrinter(bool showHidden) : ShowHidden(showHidden) {}
2340 virtual ~HelpPrinter() = default;
2341
2342 // Invoke the printer.
2343 void operator=(bool Value) {
2344 if (!Value)
2345 return;
2346 printHelp();
2347
2348 // Halt the program since help information was printed
2349 exit(0);
2350 }
2351
2352 void printHelp() {
2353 SubCommand *Sub = GlobalParser->getActiveSubCommand();
2354 auto &OptionsMap = Sub->OptionsMap;
2355 auto &PositionalOpts = Sub->PositionalOpts;
2356 auto &ConsumeAfterOpt = Sub->ConsumeAfterOpt;
2357
2358 StrOptionPairVector Opts;
2359 sortOpts(OptionsMap, Opts, ShowHidden);
2360
2361 StrSubCommandPairVector Subs;
2362 sortSubCommands(GlobalParser->RegisteredSubCommands, Subs);
2363
2364 if (!GlobalParser->ProgramOverview.empty())
2365 outs() << "OVERVIEW: " << GlobalParser->ProgramOverview << "\n";
2366
2367 if (Sub == &SubCommand::getTopLevel()) {
2368 outs() << "USAGE: " << GlobalParser->ProgramName;
2369 if (!Subs.empty())
2370 outs() << " [subcommand]";
2371 outs() << " [options]";
2372 } else {
2373 if (!Sub->getDescription().empty()) {
2374 outs() << "SUBCOMMAND '" << Sub->getName()
2375 << "': " << Sub->getDescription() << "\n\n";
2376 }
2377 outs() << "USAGE: " << GlobalParser->ProgramName << " " << Sub->getName()
2378 << " [options]";
2379 }
2380
2381 for (auto *Opt : PositionalOpts) {
2382 if (Opt->hasArgStr())
2383 outs() << " --" << Opt->ArgStr;
2384 outs() << " " << Opt->HelpStr;
2385 }
2386
2387 // Print the consume after option info if it exists...
2388 if (ConsumeAfterOpt)
2389 outs() << " " << ConsumeAfterOpt->HelpStr;
2390
2391 if (Sub == &SubCommand::getTopLevel() && !Subs.empty()) {
2392 // Compute the maximum subcommand length...
2393 size_t MaxSubLen = 0;
2394 for (size_t i = 0, e = Subs.size(); i != e; ++i)
2395 MaxSubLen = std::max(MaxSubLen, strlen(Subs[i].first));
2396
2397 outs() << "\n\n";
2398 outs() << "SUBCOMMANDS:\n\n";
2399 printSubCommands(Subs, MaxSubLen);
2400 outs() << "\n";
2401 outs() << " Type \"" << GlobalParser->ProgramName
2402 << " <subcommand> --help\" to get more help on a specific "
2403 "subcommand";
2404 }
2405
2406 outs() << "\n\n";
2407
2408 // Compute the maximum argument length...
2409 size_t MaxArgLen = 0;
2410 for (size_t i = 0, e = Opts.size(); i != e; ++i)
2411 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
2412
2413 outs() << "OPTIONS:\n";
2414 printOptions(Opts, MaxArgLen);
2415
2416 // Print any extra help the user has declared.
2417 for (const auto &I : GlobalParser->MoreHelp)
2418 outs() << I;
2419 GlobalParser->MoreHelp.clear();
2420 }
2421};
2422
2423class CategorizedHelpPrinter : public HelpPrinter {
2424public:
2425 explicit CategorizedHelpPrinter(bool showHidden) : HelpPrinter(showHidden) {}
2426
2427 // Helper function for printOptions().
2428 // It shall return a negative value if A's name should be lexicographically
2429 // ordered before B's name. It returns a value greater than zero if B's name
2430 // should be ordered before A's name, and it returns 0 otherwise.
2431 static int OptionCategoryCompare(OptionCategory *const *A,
2432 OptionCategory *const *B) {
2433 return (*A)->getName().compare((*B)->getName());
2434 }
2435
2436 // Make sure we inherit our base class's operator=()
2437 using HelpPrinter::operator=;
2438
2439protected:
2440 void printOptions(StrOptionPairVector &Opts, size_t MaxArgLen) override {
2441 std::vector<OptionCategory *> SortedCategories;
2443
2444 // Collect registered option categories into vector in preparation for
2445 // sorting.
2446 for (OptionCategory *Category : GlobalParser->RegisteredOptionCategories)
2447 SortedCategories.push_back(Category);
2448
2449 // Sort the different option categories alphabetically.
2450 assert(SortedCategories.size() > 0 && "No option categories registered!");
2451 array_pod_sort(SortedCategories.begin(), SortedCategories.end(),
2452 OptionCategoryCompare);
2453
2454 // Walk through pre-sorted options and assign into categories.
2455 // Because the options are already alphabetically sorted the
2456 // options within categories will also be alphabetically sorted.
2457 for (size_t I = 0, E = Opts.size(); I != E; ++I) {
2458 Option *Opt = Opts[I].second;
2459 for (auto &Cat : Opt->Categories) {
2460 assert(llvm::is_contained(SortedCategories, Cat) &&
2461 "Option has an unregistered category");
2462 CategorizedOptions[Cat].push_back(Opt);
2463 }
2464 }
2465
2466 // Now do printing.
2467 for (OptionCategory *Category : SortedCategories) {
2468 // Hide empty categories for --help, but show for --help-hidden.
2469 const auto &CategoryOptions = CategorizedOptions[Category];
2470 if (CategoryOptions.empty())
2471 continue;
2472
2473 // Print category information.
2474 outs() << "\n";
2475 outs() << Category->getName() << ":\n";
2476
2477 // Check if description is set.
2478 if (!Category->getDescription().empty())
2479 outs() << Category->getDescription() << "\n\n";
2480 else
2481 outs() << "\n";
2482
2483 // Loop over the options in the category and print.
2484 for (const Option *Opt : CategoryOptions)
2485 Opt->printOptionInfo(MaxArgLen);
2486 }
2487 }
2488};
2489
2490// This wraps the Uncategorizing and Categorizing printers and decides
2491// at run time which should be invoked.
2492class HelpPrinterWrapper {
2493private:
2494 HelpPrinter &UncategorizedPrinter;
2495 CategorizedHelpPrinter &CategorizedPrinter;
2496
2497public:
2498 explicit HelpPrinterWrapper(HelpPrinter &UncategorizedPrinter,
2499 CategorizedHelpPrinter &CategorizedPrinter)
2500 : UncategorizedPrinter(UncategorizedPrinter),
2501 CategorizedPrinter(CategorizedPrinter) {}
2502
2503 // Invoke the printer.
2504 void operator=(bool Value);
2505};
2506
2507} // End anonymous namespace
2508
2509#if defined(__GNUC__)
2510// GCC and GCC-compatible compilers define __OPTIMIZE__ when optimizations are
2511// enabled.
2512# if defined(__OPTIMIZE__)
2513# define LLVM_IS_DEBUG_BUILD 0
2514# else
2515# define LLVM_IS_DEBUG_BUILD 1
2516# endif
2517#elif defined(_MSC_VER)
2518// MSVC doesn't have a predefined macro indicating if optimizations are enabled.
2519// Use _DEBUG instead. This macro actually corresponds to the choice between
2520// debug and release CRTs, but it is a reasonable proxy.
2521# if defined(_DEBUG)
2522# define LLVM_IS_DEBUG_BUILD 1
2523# else
2524# define LLVM_IS_DEBUG_BUILD 0
2525# endif
2526#else
2527// Otherwise, for an unknown compiler, assume this is an optimized build.
2528# define LLVM_IS_DEBUG_BUILD 0
2529#endif
2530
2531namespace {
2532class VersionPrinter {
2533public:
2534 void print(std::vector<VersionPrinterTy> ExtraPrinters = {}) {
2535 raw_ostream &OS = outs();
2536#ifdef PACKAGE_VENDOR
2537 OS << PACKAGE_VENDOR << " ";
2538#else
2539 OS << "LLVM (http://llvm.org/):\n ";
2540#endif
2541 OS << PACKAGE_NAME << " version " << PACKAGE_VERSION << "\n ";
2542#if LLVM_IS_DEBUG_BUILD
2543 OS << "DEBUG build";
2544#else
2545 OS << "Optimized build";
2546#endif
2547#ifndef NDEBUG
2548 OS << " with assertions";
2549#endif
2550 OS << ".\n";
2551
2552 // Iterate over any registered extra printers and call them to add further
2553 // information.
2554 if (!ExtraPrinters.empty()) {
2555 for (const auto &I : ExtraPrinters)
2556 I(outs());
2557 }
2558 }
2559 void operator=(bool OptionWasSpecified);
2560};
2561
2562struct CommandLineCommonOptions {
2563 // Declare the four HelpPrinter instances that are used to print out help, or
2564 // help-hidden as an uncategorized list or in categories.
2565 HelpPrinter UncategorizedNormalPrinter{false};
2566 HelpPrinter UncategorizedHiddenPrinter{true};
2567 CategorizedHelpPrinter CategorizedNormalPrinter{false};
2568 CategorizedHelpPrinter CategorizedHiddenPrinter{true};
2569 // Declare HelpPrinter wrappers that will decide whether or not to invoke
2570 // a categorizing help printer
2571 HelpPrinterWrapper WrappedNormalPrinter{UncategorizedNormalPrinter,
2572 CategorizedNormalPrinter};
2573 HelpPrinterWrapper WrappedHiddenPrinter{UncategorizedHiddenPrinter,
2574 CategorizedHiddenPrinter};
2575 // Define a category for generic options that all tools should have.
2576 cl::OptionCategory GenericCategory{"Generic Options"};
2577
2578 // Define uncategorized help printers.
2579 // --help-list is hidden by default because if Option categories are being
2580 // used then --help behaves the same as --help-list.
2582 "help-list",
2583 cl::desc(
2584 "Display list of available options (--help-list-hidden for more)"),
2585 cl::location(UncategorizedNormalPrinter),
2586 cl::Hidden,
2588 cl::cat(GenericCategory),
2590
2592 "help-list-hidden",
2593 cl::desc("Display list of all available options"),
2594 cl::location(UncategorizedHiddenPrinter),
2595 cl::Hidden,
2597 cl::cat(GenericCategory),
2599
2600 // Define uncategorized/categorized help printers. These printers change their
2601 // behaviour at runtime depending on whether one or more Option categories
2602 // have been declared.
2604 "help",
2605 cl::desc("Display available options (--help-hidden for more)"),
2606 cl::location(WrappedNormalPrinter),
2608 cl::cat(GenericCategory),
2610
2611 cl::alias HOpA{"h", cl::desc("Alias for --help"), cl::aliasopt(HOp),
2613
2615 "help-hidden",
2616 cl::desc("Display all available options"),
2617 cl::location(WrappedHiddenPrinter),
2618 cl::Hidden,
2620 cl::cat(GenericCategory),
2622
2623 cl::opt<bool> PrintOptions{
2624 "print-options",
2625 cl::desc("Print non-default options after command line parsing"),
2626 cl::Hidden,
2627 cl::init(false),
2628 cl::cat(GenericCategory),
2630
2631 cl::opt<bool> PrintAllOptions{
2632 "print-all-options",
2633 cl::desc("Print all option values after command line parsing"),
2634 cl::Hidden,
2635 cl::init(false),
2636 cl::cat(GenericCategory),
2638
2639 VersionPrinterTy OverrideVersionPrinter = nullptr;
2640
2641 std::vector<VersionPrinterTy> ExtraVersionPrinters;
2642
2643 // Define the --version option that prints out the LLVM version for the tool
2644 VersionPrinter VersionPrinterInstance;
2645
2647 "version", cl::desc("Display the version of this program"),
2648 cl::location(VersionPrinterInstance), cl::ValueDisallowed,
2649 cl::cat(GenericCategory)};
2650};
2651} // End anonymous namespace
2652
2653// Lazy-initialized global instance of options controlling the command-line
2654// parser and general handling.
2656
2657static void initCommonOptions() {
2668}
2669
2671 // Initialise the general option category.
2672 static OptionCategory GeneralCategory{"General options"};
2673 return GeneralCategory;
2674}
2675
2676void VersionPrinter::operator=(bool OptionWasSpecified) {
2677 if (!OptionWasSpecified)
2678 return;
2679
2680 if (CommonOptions->OverrideVersionPrinter != nullptr) {
2681 CommonOptions->OverrideVersionPrinter(outs());
2682 exit(0);
2683 }
2684 print(CommonOptions->ExtraVersionPrinters);
2685
2686 exit(0);
2687}
2688
2689void HelpPrinterWrapper::operator=(bool Value) {
2690 if (!Value)
2691 return;
2692
2693 // Decide which printer to invoke. If more than one option category is
2694 // registered then it is useful to show the categorized help instead of
2695 // uncategorized help.
2696 if (GlobalParser->RegisteredOptionCategories.size() > 1) {
2697 // unhide --help-list option so user can have uncategorized output if they
2698 // want it.
2699 CommonOptions->HLOp.setHiddenFlag(NotHidden);
2700
2701 CategorizedPrinter = true; // Invoke categorized printer
2702 } else
2703 UncategorizedPrinter = true; // Invoke uncategorized printer
2704}
2705
2706// Print the value of each option.
2707void cl::PrintOptionValues() { GlobalParser->printOptionValues(); }
2708
2709void CommandLineParser::printOptionValues() {
2710 if (!CommonOptions->PrintOptions && !CommonOptions->PrintAllOptions)
2711 return;
2712
2714 sortOpts(ActiveSubCommand->OptionsMap, Opts, /*ShowHidden*/ true);
2715
2716 // Compute the maximum argument length...
2717 size_t MaxArgLen = 0;
2718 for (size_t i = 0, e = Opts.size(); i != e; ++i)
2719 MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
2720
2721 for (size_t i = 0, e = Opts.size(); i != e; ++i)
2722 Opts[i].second->printOptionValue(MaxArgLen, CommonOptions->PrintAllOptions);
2723}
2724
2725// Utility function for printing the help message.
2726void cl::PrintHelpMessage(bool Hidden, bool Categorized) {
2727 if (!Hidden && !Categorized)
2728 CommonOptions->UncategorizedNormalPrinter.printHelp();
2729 else if (!Hidden && Categorized)
2730 CommonOptions->CategorizedNormalPrinter.printHelp();
2731 else if (Hidden && !Categorized)
2732 CommonOptions->UncategorizedHiddenPrinter.printHelp();
2733 else
2734 CommonOptions->CategorizedHiddenPrinter.printHelp();
2735}
2736
2738 static const StringRef Config[] = {
2739 // Placeholder to ensure the array always has elements, since it's an
2740 // error to have a zero-sized array. Slice this off before returning.
2741 "",
2742 // Actual compiler build config feature list:
2743#if LLVM_IS_DEBUG_BUILD
2744 "+unoptimized",
2745#endif
2746#ifndef NDEBUG
2747 "+assertions",
2748#endif
2749#ifdef EXPENSIVE_CHECKS
2750 "+expensive-checks",
2751#endif
2752#if __has_feature(address_sanitizer)
2753 "+asan",
2754#endif
2755#if __has_feature(dataflow_sanitizer)
2756 "+dfsan",
2757#endif
2758#if __has_feature(hwaddress_sanitizer)
2759 "+hwasan",
2760#endif
2761#if __has_feature(memory_sanitizer)
2762 "+msan",
2763#endif
2764#if __has_feature(thread_sanitizer)
2765 "+tsan",
2766#endif
2767#if __has_feature(undefined_behavior_sanitizer)
2768 "+ubsan",
2769#endif
2770 };
2771 return ArrayRef(Config).drop_front(1);
2772}
2773
2774// Utility function for printing the build config.
2776#if LLVM_VERSION_PRINTER_SHOW_BUILD_CONFIG
2777 OS << "Build config: ";
2779 OS << '\n';
2780#endif
2781}
2782
2783/// Utility function for printing version number.
2785 CommonOptions->VersionPrinterInstance.print(CommonOptions->ExtraVersionPrinters);
2786}
2787
2789 CommonOptions->OverrideVersionPrinter = func;
2790}
2791
2793 CommonOptions->ExtraVersionPrinters.push_back(func);
2794}
2795
2798 auto &Subs = GlobalParser->RegisteredSubCommands;
2799 (void)Subs;
2800 assert(Subs.contains(&Sub));
2801 return Sub.OptionsMap;
2802}
2803
2806 return GlobalParser->getRegisteredSubcommands();
2807}
2808
2811 for (auto &I : Sub.OptionsMap) {
2812 bool Unrelated = true;
2813 for (auto &Cat : I.second->Categories) {
2814 if (Cat == &Category || Cat == &CommonOptions->GenericCategory)
2815 Unrelated = false;
2816 }
2817 if (Unrelated)
2818 I.second->setHiddenFlag(cl::ReallyHidden);
2819 }
2820}
2821
2823 SubCommand &Sub) {
2825 for (auto &I : Sub.OptionsMap) {
2826 bool Unrelated = true;
2827 for (auto &Cat : I.second->Categories) {
2828 if (is_contained(Categories, Cat) ||
2829 Cat == &CommonOptions->GenericCategory)
2830 Unrelated = false;
2831 }
2832 if (Unrelated)
2833 I.second->setHiddenFlag(cl::ReallyHidden);
2834 }
2835}
2836
2839 GlobalParser->ResetAllOptionOccurrences();
2840}
2841
2842void LLVMParseCommandLineOptions(int argc, const char *const *argv,
2843 const char *Overview) {
2844 llvm::cl::ParseCommandLineOptions(argc, argv, StringRef(Overview),
2845 &llvm::nulls());
2846}
This file defines the StringMap class.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static bool CommaSeparateAndAddOccurrence(Option *Handler, unsigned pos, StringRef ArgName, StringRef Value, bool MultiArg=false)
CommaSeparateAndAddOccurrence - A wrapper around Handler->addOccurrence() that does special handling ...
static StringRef OptionPrefix
static bool RequiresValue(const Option *O)
static int SubNameCompare(const std::pair< const char *, SubCommand * > *LHS, const std::pair< const char *, SubCommand * > *RHS)
static size_t argPlusPrefixesSize(StringRef ArgName, size_t Pad=DefaultPad)
static bool isPrefixedOrGrouping(const Option *O)
static bool shouldPrintOption(StringRef Name, StringRef Description, const Option &O)
static Option * HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value, bool &ErrorParsing, const StringMap< Option * > &OptionsMap)
HandlePrefixedOrGroupedOption - The specified argument string (which started with at least one '-') d...
static bool parseDouble(Option &O, StringRef Arg, double &Value)
static const size_t DefaultPad
Definition: CommandLine.cpp:99
static StringRef EmptyOption
static bool hasUTF8ByteOrderMark(ArrayRef< char > S)
static ManagedStatic< CommandLineParser > GlobalParser
static void ExpandBasePaths(StringRef BasePath, StringSaver &Saver, const char *&Arg)
static SmallString< 8 > argPrefix(StringRef ArgName, size_t Pad=DefaultPad)
static StringRef ArgHelpPrefix
static bool isWindowsSpecialCharInCommandName(char C)
static Option * getOptionPred(StringRef Name, size_t &Length, bool(*Pred)(const Option *), const StringMap< Option * > &OptionsMap)
static StringRef getValueStr(const Option &O, StringRef DefaultMsg)
static size_t getOptionPrefixesSize()
static bool ProvideOption(Option *Handler, StringRef ArgName, StringRef Value, int argc, const char *const *argv, int &i)
ProvideOption - For Value, this differentiates between an empty value ("") and a null value (StringRe...
static bool isQuote(char C)
static ManagedStatic< CommandLineCommonOptions > CommonOptions
static void initCommonOptions()
static void tokenizeWindowsCommandLineImpl(StringRef Src, StringSaver &Saver, function_ref< void(StringRef)> AddToken, bool AlwaysCopy, function_ref< void()> MarkEOL, bool InitialCommandName)
static bool isWhitespace(char C)
static size_t parseBackslash(StringRef Src, size_t I, SmallString< 128 > &Token)
Backslashes are interpreted in a rather complicated way in the Windows-style command line,...
static StringRef ArgPrefixLong
static void sortSubCommands(const SmallPtrSetImpl< SubCommand * > &SubMap, SmallVectorImpl< std::pair< const char *, SubCommand * > > &Subs)
#define PRINT_OPT_DIFF(T)
static bool isWhitespaceOrNull(char C)
static StringRef EqValue
static const size_t MaxOptWidth
static Option * LookupNearestOption(StringRef Arg, const StringMap< Option * > &OptionsMap, std::string &NearestString)
LookupNearestOption - Lookup the closest match to the option specified by the specified option on the...
static bool EatsUnboundedNumberOfValues(const Option *O)
static int OptNameCompare(const std::pair< const char *, Option * > *LHS, const std::pair< const char *, Option * > *RHS)
static void sortOpts(StringMap< Option * > &OptMap, SmallVectorImpl< std::pair< const char *, Option * > > &Opts, bool ShowHidden)
static StringRef ArgPrefix
static bool isWindowsSpecialChar(char C)
static bool isGrouping(const Option *O)
#define LLVM_REQUIRE_CONSTANT_INITIALIZATION
LLVM_REQUIRE_CONSTANT_INITIALIZATION - Apply this to globals to ensure that they are constant initial...
Definition: Compiler.h:310
#define LLVM_DEBUG(X)
Definition: Debug.h:101
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
RelaxConfig Config
Definition: ELF_riscv.cpp:506
static void Help(ArrayRef< SubtargetSubTypeKV > CPUTable, ArrayRef< SubtargetFeatureKV > FeatTable)
Display help for feature and mcpu choices.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Provides a library for accessing information about this process and other processes on the operating ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallPtrSet class.
This file defines the SmallString class.
This file contains some functions that are useful when dealing with strings.
Defines the virtual file system interface vfs::FileSystem.
Value * RHS
Value * LHS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:204
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
Represents either an error or a value T.
Definition: ErrorOr.h:56
reference get()
Definition: ErrorOr.h:149
std::error_code getError() const
Definition: ErrorOr.h:152
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:83
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
size_t getBufferSize() const
Definition: MemoryBuffer.h:68
const char * getBufferEnd() const
Definition: MemoryBuffer.h:67
const char * getBufferStart() const
Definition: MemoryBuffer.h:66
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:321
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
void assign(StringRef RHS)
Assign from a StringRef.
Definition: SmallString.h:51
void append(StringRef RHS)
Append from a StringRef.
Definition: SmallString.h:68
const char * c_str()
Definition: SmallString.h:259
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:254
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void assign(size_type NumElts, ValueParamT Elt)
Definition: SmallVector.h:717
iterator erase(const_iterator CI)
Definition: SmallVector.h:750
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:818
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:849
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:127
iterator end()
Definition: StringMap.h:220
iterator begin()
Definition: StringMap.h:219
iterator find(StringRef Key)
Definition: StringMap.h:233
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition: StringMap.h:276
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:696
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:466
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:567
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:257
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
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
size_t size_type
Definition: StringRef.h:56
constexpr size_t size() const
size - Get the string size.
Definition: StringRef.h:137
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition: StringRef.h:631
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:293
static constexpr size_t npos
Definition: StringRef.h:52
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition: StringSaver.h:21
BumpPtrAllocator & getAllocator() const
Definition: StringSaver.h:27
StringRef save(const char *S)
Definition: StringSaver.h:30
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
LLVM Value Representation.
Definition: Value.h:74
Contains options that control response file expansion.
Definition: CommandLine.h:2153
bool findConfigFile(StringRef FileName, SmallVectorImpl< char > &FilePath)
Looks for the specified configuration file.
ExpansionContext(BumpPtrAllocator &A, TokenizerCallback T)
Error expandResponseFiles(SmallVectorImpl< const char * > &Argv)
Expands constructs "@file" in the provided array of arguments recursively.
Error readConfigFile(StringRef CfgFile, SmallVectorImpl< const char * > &Argv)
Reads command line options from the given configuration file.
StringRef getDescription() const
Definition: CommandLine.h:195
StringRef getName() const
Definition: CommandLine.h:194
SmallPtrSet< SubCommand *, 1 > Subs
Definition: CommandLine.h:294
StringRef ArgStr
Definition: CommandLine.h:289
int getNumOccurrences() const
Definition: CommandLine.h:406
enum ValueExpected getValueExpectedFlag() const
Definition: CommandLine.h:300
void addCategory(OptionCategory &C)
virtual bool addOccurrence(unsigned pos, StringRef ArgName, StringRef Value, bool MultiArg=false)
void setMiscFlag(enum MiscFlags M)
Definition: CommandLine.h:336
enum FormattingFlags getFormattingFlag() const
Definition: CommandLine.h:308
virtual void printOptionInfo(size_t GlobalWidth) const =0
enum NumOccurrencesFlag getNumOccurrencesFlag() const
Definition: CommandLine.h:296
SmallVector< OptionCategory *, 1 > Categories
Definition: CommandLine.h:293
bool error(const Twine &Message, StringRef ArgName=StringRef(), raw_ostream &Errs=llvm::errs())
void setArgStr(StringRef S)
bool hasArgStr() const
Definition: CommandLine.h:317
bool isDefaultOption() const
Definition: CommandLine.h:320
unsigned getMiscFlags() const
Definition: CommandLine.h:312
virtual void setDefault()=0
static void printEnumValHelpStr(StringRef HelpStr, size_t Indent, size_t FirstLineIndentedBy)
unsigned getNumAdditionalVals() const
Definition: CommandLine.h:314
void removeArgument()
Unregisters this option from the CommandLine system.
static void printHelpStr(StringRef HelpStr, size_t Indent, size_t FirstLineIndentedBy)
StringRef HelpStr
Definition: CommandLine.h:290
StringRef getName() const
Definition: CommandLine.h:230
SmallVector< Option *, 4 > SinkOpts
Definition: CommandLine.h:234
static SubCommand & getTopLevel()
static SubCommand & getAll()
Option * ConsumeAfterOpt
Definition: CommandLine.h:237
SmallVector< Option *, 4 > PositionalOpts
Definition: CommandLine.h:233
StringMap< Option * > OptionsMap
Definition: CommandLine.h:235
StringRef getDescription() const
Definition: CommandLine.h:231
void printOptionInfo(const Option &O, size_t GlobalWidth) const
virtual StringRef getValueName() const
Definition: CommandLine.h:926
void printOptionNoValue(const Option &O, size_t GlobalWidth) const
size_t getOptionWidth(const Option &O) const
void printOptionName(const Option &O, size_t GlobalWidth) const
virtual size_t getOptionWidth(const Option &O) const
virtual StringRef getDescription(unsigned N) const =0
virtual const GenericOptionValue & getOptionValue(unsigned N) const =0
virtual unsigned getNumOptions() const =0
virtual StringRef getOption(unsigned N) const =0
void printOptionDiff(const Option &O, const AnyOptionValue &V, const AnyOptionValue &Default, size_t GlobalWidth) const
Definition: CommandLine.h:780
void printGenericOptionDiff(const Option &O, const GenericOptionValue &V, const GenericOptionValue &Default, size_t GlobalWidth) const
virtual void printOptionInfo(const Option &O, size_t GlobalWidth) const
unsigned findOption(StringRef Name)
bool parse(Option &O, StringRef ArgName, StringRef Arg, DataType &V)
Definition: CommandLine.h:859
An efficient, type-erasing, non-owning reference to a callable.
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
static std::optional< std::string > GetEnv(StringRef name)
virtual llvm::ErrorOr< std::string > getCurrentWorkingDirectory() const =0
Get the working directory of this file system.
llvm::ErrorOr< std::unique_ptr< llvm::MemoryBuffer > > getBufferForFile(const Twine &Name, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
This is a convenience method that opens a file, gets its content and then closes the file.
virtual std::error_code makeAbsolute(SmallVectorImpl< char > &Path) const
Make Path an absolute path.
virtual llvm::ErrorOr< Status > status(const Twine &Path)=0
Get the status of the entry at Path, if one exists.
The result of a status operation.
bool equivalent(const Status &Other) const
void LLVMParseCommandLineOptions(int argc, const char *const *argv, const char *Overview)
This function parses the given arguments using the LLVM command line parser.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
ManagedStatic< cl::opt< FnT >, OptCreatorT > Action
constexpr size_t NameSize
Definition: XCOFF.h:29
std::function< void(raw_ostream &)> VersionPrinterTy
Definition: CommandLine.h:76
void(*)(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char * > &NewArgv, bool MarkEOLs) TokenizerCallback
String tokenization function type.
Definition: CommandLine.h:2137
void PrintVersionMessage()
Utility function for printing version number.
bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, SmallVectorImpl< const char * > &Argv)
A convenience helper which supports the typical use case of expansion function call.
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 ...
void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char * > &NewArgv, bool MarkEOLs=false)
Tokenizes a string of Windows command line arguments, which may contain quotes and escaped quotes.
OptionCategory & getGeneralCategory()
@ ConsumeAfter
Definition: CommandLine.h:126
@ ValueOptional
Definition: CommandLine.h:131
@ ValueDisallowed
Definition: CommandLine.h:133
@ ValueRequired
Definition: CommandLine.h:132
void ResetAllOptionOccurrences()
Reset all command line options to a state that looks as if they have never appeared on the command li...
void SetVersionPrinter(VersionPrinterTy func)
===------------------------------------------------------------------—===// Override the default (LLV...
void tokenizeConfigFile(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char * > &NewArgv, bool MarkEOLs=false)
Tokenizes content of configuration file.
@ ReallyHidden
Definition: CommandLine.h:139
StringMap< Option * > & getRegisteredOptions(SubCommand &Sub=SubCommand::getTopLevel())
Use this to get a StringMap to all registered named options (e.g.
void ResetCommandLineParser()
Reset the command line parser back to its initial state.
bool ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview="", raw_ostream *Errs=nullptr, const char *EnvVar=nullptr, bool LongOptionsUseDoubleDash=false)
ManagedStatic< SubCommand > TopLevelSubCommand
void PrintOptionValues()
iterator_range< typename SmallPtrSet< SubCommand *, 4 >::iterator > getRegisteredSubcommands()
Use this to get all registered SubCommands from the provided parser.
void AddLiteralOption(Option &O, StringRef Name)
Adds a new option for parsing and provides the option it refers to.
void TokenizeWindowsCommandLineNoCopy(StringRef Source, StringSaver &Saver, SmallVectorImpl< StringRef > &NewArgv)
Tokenizes a Windows command line while attempting to avoid copies.
void printBuildConfig(raw_ostream &OS)
Prints the compiler build configuration.
bool ProvidePositionalOption(Option *Handler, StringRef Arg, int i)
Parses Arg into the option handler Handler.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
@ CommaSeparated
Definition: CommandLine.h:164
@ DefaultOption
Definition: CommandLine.h:175
@ PositionalEatsArgs
Definition: CommandLine.h:165
ManagedStatic< SubCommand > AllSubCommands
ArrayRef< StringRef > getCompilerBuildConfig()
An array of optional enabled settings in the LLVM build configuration, which may be of interest to co...
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:470
void HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub=SubCommand::getTopLevel())
Mark all options not part of this category as cl::ReallyHidden.
void AddExtraVersionPrinter(VersionPrinterTy func)
===------------------------------------------------------------------—===// Add an extra printer to u...
void PrintHelpMessage(bool Hidden=false, bool Categorized=false)
This function just prints the help message, exactly the same way as if the -help or -help-hidden opti...
void TokenizeWindowsCommandLineFull(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char * > &NewArgv, bool MarkEOLs=false)
Tokenizes a Windows full command line, including command name at the start.
@ AlwaysPrefix
Definition: CommandLine.h:160
void TokenizeGNUCommandLine(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char * > &NewArgv, bool MarkEOLs=false)
Tokenizes a command line that can contain escapes and quotes.
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
constexpr double e
Definition: MathExtras.h:31
bool has_parent_path(const Twine &path, Style style=Style::native)
Has parent path?
Definition: Path.cpp:651
bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Definition: Path.cpp:700
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:578
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:468
bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition: Path.cpp:672
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:457
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
@ Length
Definition: DWP.cpp:456
void initWithColorOptions()
Definition: WithColor.cpp:34
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1680
@ Done
Definition: Threading.h:61
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void initDebugOptions()
Definition: Debug.cpp:145
void interleaveComma(const Container &c, StreamT &os, UnaryFunctor each_fn)
Definition: STLExtras.h:2165
bool hasUTF16ByteOrderMark(ArrayRef< char > SrcBytes)
Returns true if a blob of text starts with a UTF-16 big or little endian byte order mark.
void initDebugCounterOptions()
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1258
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
@ no_such_file_or_directory
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
bool convertUTF16ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
void initSignalsOptions()
Definition: Signals.cpp:64
void initTypeSizeOptions()
Definition: TypeSize.cpp:33
void initStatisticOptions()
Definition: Statistic.cpp:49
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
void initTimerOptions()
Definition: Timer.cpp:90
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void initRandomSeedOptions()
void initGraphWriterOptions()
Definition: GraphWriter.cpp:52
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:293
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition: STLExtras.h:1921
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:1607
@ Default
The result values are uniform if and only if all operands are uniform.
#define INIT(o, n)
Definition: regexec.c:120
extrahelp(StringRef help)