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