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