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() {
168     registerSubCommand(&SubCommand::getTopLevel());
169     registerSubCommand(&SubCommand::getAll());
170   }
171 
172   void ResetAllOptionOccurrences();
173 
174   bool ParseCommandLineOptions(int argc, const char *const *argv,
175                                StringRef Overview, raw_ostream *Errs = nullptr,
176                                bool LongOptionsUseDoubleDash = false);
177 
addLiteralOption(Option & Opt,SubCommand * SC,StringRef Name)178   void addLiteralOption(Option &Opt, SubCommand *SC, StringRef Name) {
179     if (Opt.hasArgStr())
180       return;
181     if (!SC->OptionsMap.insert(std::make_pair(Name, &Opt)).second) {
182       errs() << ProgramName << ": CommandLine Error: Option '" << Name
183              << "' registered more than once!\n";
184       report_fatal_error("inconsistency in registered CommandLine options");
185     }
186 
187     // If we're adding this to all sub-commands, add it to the ones that have
188     // already been registered.
189     if (SC == &SubCommand::getAll()) {
190       for (auto *Sub : RegisteredSubCommands) {
191         if (SC == Sub)
192           continue;
193         addLiteralOption(Opt, Sub, Name);
194       }
195     }
196   }
197 
addLiteralOption(Option & Opt,StringRef Name)198   void addLiteralOption(Option &Opt, StringRef Name) {
199     if (Opt.Subs.empty())
200       addLiteralOption(Opt, &SubCommand::getTopLevel(), Name);
201     else {
202       for (auto *SC : Opt.Subs)
203         addLiteralOption(Opt, SC, Name);
204     }
205   }
206 
addOption(Option * O,SubCommand * SC)207   void addOption(Option *O, SubCommand *SC) {
208     bool HadErrors = false;
209     if (O->hasArgStr()) {
210       // If it's a DefaultOption, check to make sure it isn't already there.
211       if (O->isDefaultOption() &&
212           SC->OptionsMap.find(O->ArgStr) != SC->OptionsMap.end())
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     // If we're adding this to all sub-commands, add it to the ones that have
244     // already been registered.
245     if (SC == &SubCommand::getAll()) {
246       for (auto *Sub : RegisteredSubCommands) {
247         if (SC == Sub)
248           continue;
249         addOption(O, Sub);
250       }
251     }
252   }
253 
addOption(Option * O,bool ProcessDefaultOption=false)254   void addOption(Option *O, bool ProcessDefaultOption = false) {
255     if (!ProcessDefaultOption && O->isDefaultOption()) {
256       DefaultOptions.push_back(O);
257       return;
258     }
259 
260     if (O->Subs.empty()) {
261       addOption(O, &SubCommand::getTopLevel());
262     } else {
263       for (auto *SC : O->Subs)
264         addOption(O, SC);
265     }
266   }
267 
removeOption(Option * O,SubCommand * SC)268   void removeOption(Option *O, SubCommand *SC) {
269     SmallVector<StringRef, 16> OptionNames;
270     O->getExtraOptionNames(OptionNames);
271     if (O->hasArgStr())
272       OptionNames.push_back(O->ArgStr);
273 
274     SubCommand &Sub = *SC;
275     auto End = Sub.OptionsMap.end();
276     for (auto Name : OptionNames) {
277       auto I = Sub.OptionsMap.find(Name);
278       if (I != End && I->getValue() == O)
279         Sub.OptionsMap.erase(I);
280     }
281 
282     if (O->getFormattingFlag() == cl::Positional)
283       for (auto *Opt = Sub.PositionalOpts.begin();
284            Opt != Sub.PositionalOpts.end(); ++Opt) {
285         if (*Opt == O) {
286           Sub.PositionalOpts.erase(Opt);
287           break;
288         }
289       }
290     else if (O->getMiscFlags() & cl::Sink)
291       for (auto *Opt = Sub.SinkOpts.begin(); Opt != Sub.SinkOpts.end(); ++Opt) {
292         if (*Opt == O) {
293           Sub.SinkOpts.erase(Opt);
294           break;
295         }
296       }
297     else if (O == Sub.ConsumeAfterOpt)
298       Sub.ConsumeAfterOpt = nullptr;
299   }
300 
removeOption(Option * O)301   void removeOption(Option *O) {
302     if (O->Subs.empty())
303       removeOption(O, &SubCommand::getTopLevel());
304     else {
305       if (O->isInAllSubCommands()) {
306         for (auto *SC : RegisteredSubCommands)
307           removeOption(O, SC);
308       } else {
309         for (auto *SC : O->Subs)
310           removeOption(O, SC);
311       }
312     }
313   }
314 
hasOptions(const SubCommand & Sub) const315   bool hasOptions(const SubCommand &Sub) const {
316     return (!Sub.OptionsMap.empty() || !Sub.PositionalOpts.empty() ||
317             nullptr != Sub.ConsumeAfterOpt);
318   }
319 
hasOptions() const320   bool hasOptions() const {
321     for (const auto *S : RegisteredSubCommands) {
322       if (hasOptions(*S))
323         return true;
324     }
325     return false;
326   }
327 
getActiveSubCommand()328   SubCommand *getActiveSubCommand() { return ActiveSubCommand; }
329 
updateArgStr(Option * O,StringRef NewName,SubCommand * SC)330   void updateArgStr(Option *O, StringRef NewName, SubCommand *SC) {
331     SubCommand &Sub = *SC;
332     if (!Sub.OptionsMap.insert(std::make_pair(NewName, O)).second) {
333       errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
334              << "' registered more than once!\n";
335       report_fatal_error("inconsistency in registered CommandLine options");
336     }
337     Sub.OptionsMap.erase(O->ArgStr);
338   }
339 
updateArgStr(Option * O,StringRef NewName)340   void updateArgStr(Option *O, StringRef NewName) {
341     if (O->Subs.empty())
342       updateArgStr(O, NewName, &SubCommand::getTopLevel());
343     else {
344       if (O->isInAllSubCommands()) {
345         for (auto *SC : RegisteredSubCommands)
346           updateArgStr(O, NewName, SC);
347       } else {
348         for (auto *SC : O->Subs)
349           updateArgStr(O, NewName, SC);
350       }
351     }
352   }
353 
354   void printOptionValues();
355 
registerCategory(OptionCategory * cat)356   void registerCategory(OptionCategory *cat) {
357     assert(count_if(RegisteredOptionCategories,
358                     [cat](const OptionCategory *Category) {
359              return cat->getName() == Category->getName();
360            }) == 0 &&
361            "Duplicate option categories");
362 
363     RegisteredOptionCategories.insert(cat);
364   }
365 
registerSubCommand(SubCommand * sub)366   void registerSubCommand(SubCommand *sub) {
367     assert(count_if(RegisteredSubCommands,
368                     [sub](const SubCommand *Sub) {
369                       return (!sub->getName().empty()) &&
370                              (Sub->getName() == sub->getName());
371                     }) == 0 &&
372            "Duplicate subcommands");
373     RegisteredSubCommands.insert(sub);
374 
375     // For all options that have been registered for all subcommands, add the
376     // option to this subcommand now.
377     if (sub != &SubCommand::getAll()) {
378       for (auto &E : SubCommand::getAll().OptionsMap) {
379         Option *O = E.second;
380         if ((O->isPositional() || O->isSink() || O->isConsumeAfter()) ||
381             O->hasArgStr())
382           addOption(O, sub);
383         else
384           addLiteralOption(*O, sub, E.first());
385       }
386     }
387   }
388 
unregisterSubCommand(SubCommand * sub)389   void unregisterSubCommand(SubCommand *sub) {
390     RegisteredSubCommands.erase(sub);
391   }
392 
393   iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator>
getRegisteredSubcommands()394   getRegisteredSubcommands() {
395     return make_range(RegisteredSubCommands.begin(),
396                       RegisteredSubCommands.end());
397   }
398 
reset()399   void reset() {
400     ActiveSubCommand = nullptr;
401     ProgramName.clear();
402     ProgramOverview = StringRef();
403 
404     MoreHelp.clear();
405     RegisteredOptionCategories.clear();
406 
407     ResetAllOptionOccurrences();
408     RegisteredSubCommands.clear();
409 
410     SubCommand::getTopLevel().reset();
411     SubCommand::getAll().reset();
412     registerSubCommand(&SubCommand::getTopLevel());
413     registerSubCommand(&SubCommand::getAll());
414 
415     DefaultOptions.clear();
416   }
417 
418 private:
419   SubCommand *ActiveSubCommand = nullptr;
420 
421   Option *LookupOption(SubCommand &Sub, StringRef &Arg, StringRef &Value);
LookupLongOption(SubCommand & Sub,StringRef & Arg,StringRef & Value,bool LongOptionsUseDoubleDash,bool HaveDoubleDash)422   Option *LookupLongOption(SubCommand &Sub, StringRef &Arg, StringRef &Value,
423                            bool LongOptionsUseDoubleDash, bool HaveDoubleDash) {
424     Option *Opt = LookupOption(Sub, Arg, Value);
425     if (Opt && LongOptionsUseDoubleDash && !HaveDoubleDash && !isGrouping(Opt))
426       return nullptr;
427     return Opt;
428   }
429   SubCommand *LookupSubCommand(StringRef Name);
430 };
431 
432 } // namespace
433 
434 static ManagedStatic<CommandLineParser> GlobalParser;
435 
AddLiteralOption(Option & O,StringRef Name)436 void cl::AddLiteralOption(Option &O, StringRef Name) {
437   GlobalParser->addLiteralOption(O, Name);
438 }
439 
extrahelp(StringRef Help)440 extrahelp::extrahelp(StringRef Help) : morehelp(Help) {
441   GlobalParser->MoreHelp.push_back(Help);
442 }
443 
addArgument()444 void Option::addArgument() {
445   GlobalParser->addOption(this);
446   FullyInitialized = true;
447 }
448 
removeArgument()449 void Option::removeArgument() { GlobalParser->removeOption(this); }
450 
setArgStr(StringRef S)451 void Option::setArgStr(StringRef S) {
452   if (FullyInitialized)
453     GlobalParser->updateArgStr(this, S);
454   assert((S.empty() || S[0] != '-') && "Option can't start with '-");
455   ArgStr = S;
456   if (ArgStr.size() == 1)
457     setMiscFlag(Grouping);
458 }
459 
addCategory(OptionCategory & C)460 void Option::addCategory(OptionCategory &C) {
461   assert(!Categories.empty() && "Categories cannot be empty.");
462   // Maintain backward compatibility by replacing the default GeneralCategory
463   // if it's still set.  Otherwise, just add the new one.  The GeneralCategory
464   // must be explicitly added if you want multiple categories that include it.
465   if (&C != &getGeneralCategory() && Categories[0] == &getGeneralCategory())
466     Categories[0] = &C;
467   else if (!is_contained(Categories, &C))
468     Categories.push_back(&C);
469 }
470 
reset()471 void Option::reset() {
472   NumOccurrences = 0;
473   setDefault();
474   if (isDefaultOption())
475     removeArgument();
476 }
477 
registerCategory()478 void OptionCategory::registerCategory() {
479   GlobalParser->registerCategory(this);
480 }
481 
482 // A special subcommand representing no subcommand. It is particularly important
483 // that this ManagedStatic uses constant initailization and not dynamic
484 // initialization because it is referenced from cl::opt constructors, which run
485 // dynamically in an arbitrary order.
486 LLVM_REQUIRE_CONSTANT_INITIALIZATION
487 ManagedStatic<SubCommand> llvm::cl::TopLevelSubCommand;
488 
489 // A special subcommand that can be used to put an option into all subcommands.
490 ManagedStatic<SubCommand> llvm::cl::AllSubCommands;
491 
getTopLevel()492 SubCommand &SubCommand::getTopLevel() { return *TopLevelSubCommand; }
493 
getAll()494 SubCommand &SubCommand::getAll() { return *AllSubCommands; }
495 
registerSubCommand()496 void SubCommand::registerSubCommand() {
497   GlobalParser->registerSubCommand(this);
498 }
499 
unregisterSubCommand()500 void SubCommand::unregisterSubCommand() {
501   GlobalParser->unregisterSubCommand(this);
502 }
503 
reset()504 void SubCommand::reset() {
505   PositionalOpts.clear();
506   SinkOpts.clear();
507   OptionsMap.clear();
508 
509   ConsumeAfterOpt = nullptr;
510 }
511 
operator bool() const512 SubCommand::operator bool() const {
513   return (GlobalParser->getActiveSubCommand() == this);
514 }
515 
516 //===----------------------------------------------------------------------===//
517 // Basic, shared command line option processing machinery.
518 //
519 
520 /// LookupOption - Lookup the option specified by the specified option on the
521 /// command line.  If there is a value specified (after an equal sign) return
522 /// that as well.  This assumes that leading dashes have already been stripped.
LookupOption(SubCommand & Sub,StringRef & Arg,StringRef & Value)523 Option *CommandLineParser::LookupOption(SubCommand &Sub, StringRef &Arg,
524                                         StringRef &Value) {
525   // Reject all dashes.
526   if (Arg.empty())
527     return nullptr;
528   assert(&Sub != &SubCommand::getAll());
529 
530   size_t EqualPos = Arg.find('=');
531 
532   // If we have an equals sign, remember the value.
533   if (EqualPos == StringRef::npos) {
534     // Look up the option.
535     return Sub.OptionsMap.lookup(Arg);
536   }
537 
538   // If the argument before the = is a valid option name and the option allows
539   // non-prefix form (ie is not AlwaysPrefix), we match.  If not, signal match
540   // failure by returning nullptr.
541   auto I = Sub.OptionsMap.find(Arg.substr(0, EqualPos));
542   if (I == Sub.OptionsMap.end())
543     return nullptr;
544 
545   auto *O = I->second;
546   if (O->getFormattingFlag() == cl::AlwaysPrefix)
547     return nullptr;
548 
549   Value = Arg.substr(EqualPos + 1);
550   Arg = Arg.substr(0, EqualPos);
551   return I->second;
552 }
553 
LookupSubCommand(StringRef Name)554 SubCommand *CommandLineParser::LookupSubCommand(StringRef Name) {
555   if (Name.empty())
556     return &SubCommand::getTopLevel();
557   for (auto *S : RegisteredSubCommands) {
558     if (S == &SubCommand::getAll())
559       continue;
560     if (S->getName().empty())
561       continue;
562 
563     if (StringRef(S->getName()) == StringRef(Name))
564       return S;
565   }
566   return &SubCommand::getTopLevel();
567 }
568 
569 /// LookupNearestOption - Lookup the closest match to the option specified by
570 /// the specified option on the command line.  If there is a value specified
571 /// (after an equal sign) return that as well.  This assumes that leading dashes
572 /// have already been stripped.
LookupNearestOption(StringRef Arg,const StringMap<Option * > & OptionsMap,std::string & NearestString)573 static Option *LookupNearestOption(StringRef Arg,
574                                    const StringMap<Option *> &OptionsMap,
575                                    std::string &NearestString) {
576   // Reject all dashes.
577   if (Arg.empty())
578     return nullptr;
579 
580   // Split on any equal sign.
581   std::pair<StringRef, StringRef> SplitArg = Arg.split('=');
582   StringRef &LHS = SplitArg.first; // LHS == Arg when no '=' is present.
583   StringRef &RHS = SplitArg.second;
584 
585   // Find the closest match.
586   Option *Best = nullptr;
587   unsigned BestDistance = 0;
588   for (StringMap<Option *>::const_iterator it = OptionsMap.begin(),
589                                            ie = OptionsMap.end();
590        it != ie; ++it) {
591     Option *O = it->second;
592     // Do not suggest really hidden options (not shown in any help).
593     if (O->getOptionHiddenFlag() == ReallyHidden)
594       continue;
595 
596     SmallVector<StringRef, 16> OptionNames;
597     O->getExtraOptionNames(OptionNames);
598     if (O->hasArgStr())
599       OptionNames.push_back(O->ArgStr);
600 
601     bool PermitValue = O->getValueExpectedFlag() != cl::ValueDisallowed;
602     StringRef Flag = PermitValue ? LHS : Arg;
603     for (const auto &Name : OptionNames) {
604       unsigned Distance = StringRef(Name).edit_distance(
605           Flag, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
606       if (!Best || Distance < BestDistance) {
607         Best = O;
608         BestDistance = Distance;
609         if (RHS.empty() || !PermitValue)
610           NearestString = std::string(Name);
611         else
612           NearestString = (Twine(Name) + "=" + RHS).str();
613       }
614     }
615   }
616 
617   return Best;
618 }
619 
620 /// CommaSeparateAndAddOccurrence - A wrapper around Handler->addOccurrence()
621 /// that does special handling of cl::CommaSeparated options.
CommaSeparateAndAddOccurrence(Option * Handler,unsigned pos,StringRef ArgName,StringRef Value,bool MultiArg=false)622 static bool CommaSeparateAndAddOccurrence(Option *Handler, unsigned pos,
623                                           StringRef ArgName, StringRef Value,
624                                           bool MultiArg = false) {
625   // Check to see if this option accepts a comma separated list of values.  If
626   // it does, we have to split up the value into multiple values.
627   if (Handler->getMiscFlags() & CommaSeparated) {
628     StringRef Val(Value);
629     StringRef::size_type Pos = Val.find(',');
630 
631     while (Pos != StringRef::npos) {
632       // Process the portion before the comma.
633       if (Handler->addOccurrence(pos, ArgName, Val.substr(0, Pos), MultiArg))
634         return true;
635       // Erase the portion before the comma, AND the comma.
636       Val = Val.substr(Pos + 1);
637       // Check for another comma.
638       Pos = Val.find(',');
639     }
640 
641     Value = Val;
642   }
643 
644   return Handler->addOccurrence(pos, ArgName, Value, MultiArg);
645 }
646 
647 /// ProvideOption - For Value, this differentiates between an empty value ("")
648 /// and a null value (StringRef()).  The later is accepted for arguments that
649 /// 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)650 static inline bool ProvideOption(Option *Handler, StringRef ArgName,
651                                  StringRef Value, int argc,
652                                  const char *const *argv, int &i) {
653   // Is this a multi-argument option?
654   unsigned NumAdditionalVals = Handler->getNumAdditionalVals();
655 
656   // Enforce value requirements
657   switch (Handler->getValueExpectedFlag()) {
658   case ValueRequired:
659     if (!Value.data()) { // No value specified?
660       // If no other argument or the option only supports prefix form, we
661       // cannot look at the next argument.
662       if (i + 1 >= argc || Handler->getFormattingFlag() == cl::AlwaysPrefix)
663         return Handler->error("requires a value!");
664       // Steal the next argument, like for '-o filename'
665       assert(argv && "null check");
666       Value = StringRef(argv[++i]);
667     }
668     break;
669   case ValueDisallowed:
670     if (NumAdditionalVals > 0)
671       return Handler->error("multi-valued option specified"
672                             " with ValueDisallowed modifier!");
673 
674     if (Value.data())
675       return Handler->error("does not allow a value! '" + Twine(Value) +
676                             "' specified.");
677     break;
678   case ValueOptional:
679     break;
680   }
681 
682   // If this isn't a multi-arg option, just run the handler.
683   if (NumAdditionalVals == 0)
684     return CommaSeparateAndAddOccurrence(Handler, i, ArgName, Value);
685 
686   // If it is, run the handle several times.
687   bool MultiArg = false;
688 
689   if (Value.data()) {
690     if (CommaSeparateAndAddOccurrence(Handler, i, ArgName, Value, MultiArg))
691       return true;
692     --NumAdditionalVals;
693     MultiArg = true;
694   }
695 
696   while (NumAdditionalVals > 0) {
697     if (i + 1 >= argc)
698       return Handler->error("not enough values!");
699     assert(argv && "null check");
700     Value = StringRef(argv[++i]);
701 
702     if (CommaSeparateAndAddOccurrence(Handler, i, ArgName, Value, MultiArg))
703       return true;
704     MultiArg = true;
705     --NumAdditionalVals;
706   }
707   return false;
708 }
709 
ProvidePositionalOption(Option * Handler,StringRef Arg,int i)710 bool llvm::cl::ProvidePositionalOption(Option *Handler, StringRef Arg, int i) {
711   int Dummy = i;
712   return ProvideOption(Handler, Handler->ArgStr, Arg, 0, nullptr, Dummy);
713 }
714 
715 // getOptionPred - Check to see if there are any options that satisfy the
716 // specified predicate with names that are the prefixes in Name.  This is
717 // checked by progressively stripping characters off of the name, checking to
718 // see if there options that satisfy the predicate.  If we find one, return it,
719 // otherwise return null.
720 //
getOptionPred(StringRef Name,size_t & Length,bool (* Pred)(const Option *),const StringMap<Option * > & OptionsMap)721 static Option *getOptionPred(StringRef Name, size_t &Length,
722                              bool (*Pred)(const Option *),
723                              const StringMap<Option *> &OptionsMap) {
724   StringMap<Option *>::const_iterator OMI = OptionsMap.find(Name);
725   if (OMI != OptionsMap.end() && !Pred(OMI->getValue()))
726     OMI = OptionsMap.end();
727 
728   // Loop while we haven't found an option and Name still has at least two
729   // characters in it (so that the next iteration will not be the empty
730   // string.
731   while (OMI == OptionsMap.end() && Name.size() > 1) {
732     Name = Name.substr(0, Name.size() - 1); // Chop off the last character.
733     OMI = OptionsMap.find(Name);
734     if (OMI != OptionsMap.end() && !Pred(OMI->getValue()))
735       OMI = OptionsMap.end();
736   }
737 
738   if (OMI != OptionsMap.end() && Pred(OMI->second)) {
739     Length = Name.size();
740     return OMI->second; // Found one!
741   }
742   return nullptr; // No option found!
743 }
744 
745 /// HandlePrefixedOrGroupedOption - The specified argument string (which started
746 /// with at least one '-') does not fully match an available option.  Check to
747 /// see if this is a prefix or grouped option.  If so, split arg into output an
748 /// Arg/Value pair and return the Option to parse it with.
749 static Option *
HandlePrefixedOrGroupedOption(StringRef & Arg,StringRef & Value,bool & ErrorParsing,const StringMap<Option * > & OptionsMap)750 HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value,
751                               bool &ErrorParsing,
752                               const StringMap<Option *> &OptionsMap) {
753   if (Arg.size() == 1)
754     return nullptr;
755 
756   // Do the lookup!
757   size_t Length = 0;
758   Option *PGOpt = getOptionPred(Arg, Length, isPrefixedOrGrouping, OptionsMap);
759   if (!PGOpt)
760     return nullptr;
761 
762   do {
763     StringRef MaybeValue =
764         (Length < Arg.size()) ? Arg.substr(Length) : StringRef();
765     Arg = Arg.substr(0, Length);
766     assert(OptionsMap.count(Arg) && OptionsMap.find(Arg)->second == PGOpt);
767 
768     // cl::Prefix options do not preserve '=' when used separately.
769     // The behavior for them with grouped options should be the same.
770     if (MaybeValue.empty() || PGOpt->getFormattingFlag() == cl::AlwaysPrefix ||
771         (PGOpt->getFormattingFlag() == cl::Prefix && MaybeValue[0] != '=')) {
772       Value = MaybeValue;
773       return PGOpt;
774     }
775 
776     if (MaybeValue[0] == '=') {
777       Value = MaybeValue.substr(1);
778       return PGOpt;
779     }
780 
781     // This must be a grouped option.
782     assert(isGrouping(PGOpt) && "Broken getOptionPred!");
783 
784     // Grouping options inside a group can't have values.
785     if (PGOpt->getValueExpectedFlag() == cl::ValueRequired) {
786       ErrorParsing |= PGOpt->error("may not occur within a group!");
787       return nullptr;
788     }
789 
790     // Because the value for the option is not required, we don't need to pass
791     // argc/argv in.
792     int Dummy = 0;
793     ErrorParsing |= ProvideOption(PGOpt, Arg, StringRef(), 0, nullptr, Dummy);
794 
795     // Get the next grouping option.
796     Arg = MaybeValue;
797     PGOpt = getOptionPred(Arg, Length, isGrouping, OptionsMap);
798   } while (PGOpt);
799 
800   // We could not find a grouping option in the remainder of Arg.
801   return nullptr;
802 }
803 
RequiresValue(const Option * O)804 static bool RequiresValue(const Option *O) {
805   return O->getNumOccurrencesFlag() == cl::Required ||
806          O->getNumOccurrencesFlag() == cl::OneOrMore;
807 }
808 
EatsUnboundedNumberOfValues(const Option * O)809 static bool EatsUnboundedNumberOfValues(const Option *O) {
810   return O->getNumOccurrencesFlag() == cl::ZeroOrMore ||
811          O->getNumOccurrencesFlag() == cl::OneOrMore;
812 }
813 
isWhitespace(char C)814 static bool isWhitespace(char C) {
815   return C == ' ' || C == '\t' || C == '\r' || C == '\n';
816 }
817 
isWhitespaceOrNull(char C)818 static bool isWhitespaceOrNull(char C) {
819   return isWhitespace(C) || C == '\0';
820 }
821 
isQuote(char C)822 static bool isQuote(char C) { return C == '\"' || C == '\''; }
823 
TokenizeGNUCommandLine(StringRef Src,StringSaver & Saver,SmallVectorImpl<const char * > & NewArgv,bool MarkEOLs)824 void cl::TokenizeGNUCommandLine(StringRef Src, StringSaver &Saver,
825                                 SmallVectorImpl<const char *> &NewArgv,
826                                 bool MarkEOLs) {
827   SmallString<128> Token;
828   for (size_t I = 0, E = Src.size(); I != E; ++I) {
829     // Consume runs of whitespace.
830     if (Token.empty()) {
831       while (I != E && isWhitespace(Src[I])) {
832         // Mark the end of lines in response files.
833         if (MarkEOLs && Src[I] == '\n')
834           NewArgv.push_back(nullptr);
835         ++I;
836       }
837       if (I == E)
838         break;
839     }
840 
841     char C = Src[I];
842 
843     // Backslash escapes the next character.
844     if (I + 1 < E && C == '\\') {
845       ++I; // Skip the escape.
846       Token.push_back(Src[I]);
847       continue;
848     }
849 
850     // Consume a quoted string.
851     if (isQuote(C)) {
852       ++I;
853       while (I != E && Src[I] != C) {
854         // Backslash escapes the next character.
855         if (Src[I] == '\\' && I + 1 != E)
856           ++I;
857         Token.push_back(Src[I]);
858         ++I;
859       }
860       if (I == E)
861         break;
862       continue;
863     }
864 
865     // End the token if this is whitespace.
866     if (isWhitespace(C)) {
867       if (!Token.empty())
868         NewArgv.push_back(Saver.save(Token.str()).data());
869       // Mark the end of lines in response files.
870       if (MarkEOLs && C == '\n')
871         NewArgv.push_back(nullptr);
872       Token.clear();
873       continue;
874     }
875 
876     // This is a normal character.  Append it.
877     Token.push_back(C);
878   }
879 
880   // Append the last token after hitting EOF with no whitespace.
881   if (!Token.empty())
882     NewArgv.push_back(Saver.save(Token.str()).data());
883 }
884 
885 /// Backslashes are interpreted in a rather complicated way in the Windows-style
886 /// command line, because backslashes are used both to separate path and to
887 /// escape double quote. This method consumes runs of backslashes as well as the
888 /// following double quote if it's escaped.
889 ///
890 ///  * If an even number of backslashes is followed by a double quote, one
891 ///    backslash is output for every pair of backslashes, and the last double
892 ///    quote remains unconsumed. The double quote will later be interpreted as
893 ///    the start or end of a quoted string in the main loop outside of this
894 ///    function.
895 ///
896 ///  * If an odd number of backslashes is followed by a double quote, one
897 ///    backslash is output for every pair of backslashes, and a double quote is
898 ///    output for the last pair of backslash-double quote. The double quote is
899 ///    consumed in this case.
900 ///
901 ///  * Otherwise, backslashes are interpreted literally.
parseBackslash(StringRef Src,size_t I,SmallString<128> & Token)902 static size_t parseBackslash(StringRef Src, size_t I, SmallString<128> &Token) {
903   size_t E = Src.size();
904   int BackslashCount = 0;
905   // Skip the backslashes.
906   do {
907     ++I;
908     ++BackslashCount;
909   } while (I != E && Src[I] == '\\');
910 
911   bool FollowedByDoubleQuote = (I != E && Src[I] == '"');
912   if (FollowedByDoubleQuote) {
913     Token.append(BackslashCount / 2, '\\');
914     if (BackslashCount % 2 == 0)
915       return I - 1;
916     Token.push_back('"');
917     return I;
918   }
919   Token.append(BackslashCount, '\\');
920   return I - 1;
921 }
922 
923 // Windows treats whitespace, double quotes, and backslashes specially, except
924 // when parsing the first token of a full command line, in which case
925 // backslashes are not special.
isWindowsSpecialChar(char C)926 static bool isWindowsSpecialChar(char C) {
927   return isWhitespaceOrNull(C) || C == '\\' || C == '\"';
928 }
isWindowsSpecialCharInCommandName(char C)929 static bool isWindowsSpecialCharInCommandName(char C) {
930   return isWhitespaceOrNull(C) || C == '\"';
931 }
932 
933 // Windows tokenization implementation. The implementation is designed to be
934 // 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)935 static inline void tokenizeWindowsCommandLineImpl(
936     StringRef Src, StringSaver &Saver, function_ref<void(StringRef)> AddToken,
937     bool AlwaysCopy, function_ref<void()> MarkEOL, bool InitialCommandName) {
938   SmallString<128> Token;
939 
940   // Sometimes, this function will be handling a full command line including an
941   // executable pathname at the start. In that situation, the initial pathname
942   // needs different handling from the following arguments, because when
943   // CreateProcess or cmd.exe scans the pathname, it doesn't treat \ as
944   // escaping the quote character, whereas when libc scans the rest of the
945   // command line, it does.
946   bool CommandName = InitialCommandName;
947 
948   // Try to do as much work inside the state machine as possible.
949   enum { INIT, UNQUOTED, QUOTED } State = INIT;
950 
951   for (size_t I = 0, E = Src.size(); I < E; ++I) {
952     switch (State) {
953     case INIT: {
954       assert(Token.empty() && "token should be empty in initial state");
955       // Eat whitespace before a token.
956       while (I < E && isWhitespaceOrNull(Src[I])) {
957         if (Src[I] == '\n')
958           MarkEOL();
959         ++I;
960       }
961       // Stop if this was trailing whitespace.
962       if (I >= E)
963         break;
964       size_t Start = I;
965       if (CommandName) {
966         while (I < E && !isWindowsSpecialCharInCommandName(Src[I]))
967           ++I;
968       } else {
969         while (I < E && !isWindowsSpecialChar(Src[I]))
970           ++I;
971       }
972       StringRef NormalChars = Src.slice(Start, I);
973       if (I >= E || isWhitespaceOrNull(Src[I])) {
974         // No special characters: slice out the substring and start the next
975         // token. Copy the string if the caller asks us to.
976         AddToken(AlwaysCopy ? Saver.save(NormalChars) : NormalChars);
977         if (I < E && Src[I] == '\n') {
978           MarkEOL();
979           CommandName = InitialCommandName;
980         } else {
981           CommandName = false;
982         }
983       } else if (Src[I] == '\"') {
984         Token += NormalChars;
985         State = QUOTED;
986       } else if (Src[I] == '\\') {
987         assert(!CommandName && "or else we'd have treated it as a normal char");
988         Token += NormalChars;
989         I = parseBackslash(Src, I, Token);
990         State = UNQUOTED;
991       } else {
992         llvm_unreachable("unexpected special character");
993       }
994       break;
995     }
996 
997     case UNQUOTED:
998       if (isWhitespaceOrNull(Src[I])) {
999         // Whitespace means the end of the token. If we are in this state, the
1000         // token must have contained a special character, so we must copy the
1001         // token.
1002         AddToken(Saver.save(Token.str()));
1003         Token.clear();
1004         if (Src[I] == '\n') {
1005           CommandName = InitialCommandName;
1006           MarkEOL();
1007         } else {
1008           CommandName = false;
1009         }
1010         State = INIT;
1011       } else if (Src[I] == '\"') {
1012         State = QUOTED;
1013       } else if (Src[I] == '\\' && !CommandName) {
1014         I = parseBackslash(Src, I, Token);
1015       } else {
1016         Token.push_back(Src[I]);
1017       }
1018       break;
1019 
1020     case QUOTED:
1021       if (Src[I] == '\"') {
1022         if (I < (E - 1) && Src[I + 1] == '"') {
1023           // Consecutive double-quotes inside a quoted string implies one
1024           // double-quote.
1025           Token.push_back('"');
1026           ++I;
1027         } else {
1028           // Otherwise, end the quoted portion and return to the unquoted state.
1029           State = UNQUOTED;
1030         }
1031       } else if (Src[I] == '\\' && !CommandName) {
1032         I = parseBackslash(Src, I, Token);
1033       } else {
1034         Token.push_back(Src[I]);
1035       }
1036       break;
1037     }
1038   }
1039 
1040   if (State != INIT)
1041     AddToken(Saver.save(Token.str()));
1042 }
1043 
TokenizeWindowsCommandLine(StringRef Src,StringSaver & Saver,SmallVectorImpl<const char * > & NewArgv,bool MarkEOLs)1044 void cl::TokenizeWindowsCommandLine(StringRef Src, StringSaver &Saver,
1045                                     SmallVectorImpl<const char *> &NewArgv,
1046                                     bool MarkEOLs) {
1047   auto AddToken = [&](StringRef Tok) { NewArgv.push_back(Tok.data()); };
1048   auto OnEOL = [&]() {
1049     if (MarkEOLs)
1050       NewArgv.push_back(nullptr);
1051   };
1052   tokenizeWindowsCommandLineImpl(Src, Saver, AddToken,
1053                                  /*AlwaysCopy=*/true, OnEOL, false);
1054 }
1055 
TokenizeWindowsCommandLineNoCopy(StringRef Src,StringSaver & Saver,SmallVectorImpl<StringRef> & NewArgv)1056 void cl::TokenizeWindowsCommandLineNoCopy(StringRef Src, StringSaver &Saver,
1057                                           SmallVectorImpl<StringRef> &NewArgv) {
1058   auto AddToken = [&](StringRef Tok) { NewArgv.push_back(Tok); };
1059   auto OnEOL = []() {};
1060   tokenizeWindowsCommandLineImpl(Src, Saver, AddToken, /*AlwaysCopy=*/false,
1061                                  OnEOL, false);
1062 }
1063 
TokenizeWindowsCommandLineFull(StringRef Src,StringSaver & Saver,SmallVectorImpl<const char * > & NewArgv,bool MarkEOLs)1064 void cl::TokenizeWindowsCommandLineFull(StringRef Src, StringSaver &Saver,
1065                                         SmallVectorImpl<const char *> &NewArgv,
1066                                         bool MarkEOLs) {
1067   auto AddToken = [&](StringRef Tok) { NewArgv.push_back(Tok.data()); };
1068   auto OnEOL = [&]() {
1069     if (MarkEOLs)
1070       NewArgv.push_back(nullptr);
1071   };
1072   tokenizeWindowsCommandLineImpl(Src, Saver, AddToken,
1073                                  /*AlwaysCopy=*/true, OnEOL, true);
1074 }
1075 
tokenizeConfigFile(StringRef Source,StringSaver & Saver,SmallVectorImpl<const char * > & NewArgv,bool MarkEOLs)1076 void cl::tokenizeConfigFile(StringRef Source, StringSaver &Saver,
1077                             SmallVectorImpl<const char *> &NewArgv,
1078                             bool MarkEOLs) {
1079   for (const char *Cur = Source.begin(); Cur != Source.end();) {
1080     SmallString<128> Line;
1081     // Check for comment line.
1082     if (isWhitespace(*Cur)) {
1083       while (Cur != Source.end() && isWhitespace(*Cur))
1084         ++Cur;
1085       continue;
1086     }
1087     if (*Cur == '#') {
1088       while (Cur != Source.end() && *Cur != '\n')
1089         ++Cur;
1090       continue;
1091     }
1092     // Find end of the current line.
1093     const char *Start = Cur;
1094     for (const char *End = Source.end(); Cur != End; ++Cur) {
1095       if (*Cur == '\\') {
1096         if (Cur + 1 != End) {
1097           ++Cur;
1098           if (*Cur == '\n' ||
1099               (*Cur == '\r' && (Cur + 1 != End) && Cur[1] == '\n')) {
1100             Line.append(Start, Cur - 1);
1101             if (*Cur == '\r')
1102               ++Cur;
1103             Start = Cur + 1;
1104           }
1105         }
1106       } else if (*Cur == '\n')
1107         break;
1108     }
1109     // Tokenize line.
1110     Line.append(Start, Cur);
1111     cl::TokenizeGNUCommandLine(Line, Saver, NewArgv, MarkEOLs);
1112   }
1113 }
1114 
1115 // It is called byte order marker but the UTF-8 BOM is actually not affected
1116 // by the host system's endianness.
hasUTF8ByteOrderMark(ArrayRef<char> S)1117 static bool hasUTF8ByteOrderMark(ArrayRef<char> S) {
1118   return (S.size() >= 3 && S[0] == '\xef' && S[1] == '\xbb' && S[2] == '\xbf');
1119 }
1120 
1121 // Substitute <CFGDIR> with the file's base path.
ExpandBasePaths(StringRef BasePath,StringSaver & Saver,const char * & Arg)1122 static void ExpandBasePaths(StringRef BasePath, StringSaver &Saver,
1123                             const char *&Arg) {
1124   assert(sys::path::is_absolute(BasePath));
1125   constexpr StringLiteral Token("<CFGDIR>");
1126   const StringRef ArgString(Arg);
1127 
1128   SmallString<128> ResponseFile;
1129   StringRef::size_type StartPos = 0;
1130   for (StringRef::size_type TokenPos = ArgString.find(Token);
1131        TokenPos != StringRef::npos;
1132        TokenPos = ArgString.find(Token, StartPos)) {
1133     // Token may appear more than once per arg (e.g. comma-separated linker
1134     // args). Support by using path-append on any subsequent appearances.
1135     const StringRef LHS = ArgString.substr(StartPos, TokenPos - StartPos);
1136     if (ResponseFile.empty())
1137       ResponseFile = LHS;
1138     else
1139       llvm::sys::path::append(ResponseFile, LHS);
1140     ResponseFile.append(BasePath);
1141     StartPos = TokenPos + Token.size();
1142   }
1143 
1144   if (!ResponseFile.empty()) {
1145     // Path-append the remaining arg substring if at least one token appeared.
1146     const StringRef Remaining = ArgString.substr(StartPos);
1147     if (!Remaining.empty())
1148       llvm::sys::path::append(ResponseFile, Remaining);
1149     Arg = Saver.save(ResponseFile.str()).data();
1150   }
1151 }
1152 
1153 // FName must be an absolute path.
expandResponseFile(StringRef FName,SmallVectorImpl<const char * > & NewArgv)1154 Error ExpansionContext::expandResponseFile(
1155     StringRef FName, SmallVectorImpl<const char *> &NewArgv) {
1156   assert(sys::path::is_absolute(FName));
1157   llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
1158       FS->getBufferForFile(FName);
1159   if (!MemBufOrErr) {
1160     std::error_code EC = MemBufOrErr.getError();
1161     return llvm::createStringError(EC, Twine("cannot not open file '") + FName +
1162                                            "': " + EC.message());
1163   }
1164   MemoryBuffer &MemBuf = *MemBufOrErr.get();
1165   StringRef Str(MemBuf.getBufferStart(), MemBuf.getBufferSize());
1166 
1167   // If we have a UTF-16 byte order mark, convert to UTF-8 for parsing.
1168   ArrayRef<char> BufRef(MemBuf.getBufferStart(), MemBuf.getBufferEnd());
1169   std::string UTF8Buf;
1170   if (hasUTF16ByteOrderMark(BufRef)) {
1171     if (!convertUTF16ToUTF8String(BufRef, UTF8Buf))
1172       return llvm::createStringError(std::errc::illegal_byte_sequence,
1173                                      "Could not convert UTF16 to UTF8");
1174     Str = StringRef(UTF8Buf);
1175   }
1176   // If we see UTF-8 BOM sequence at the beginning of a file, we shall remove
1177   // these bytes before parsing.
1178   // Reference: http://en.wikipedia.org/wiki/UTF-8#Byte_order_mark
1179   else if (hasUTF8ByteOrderMark(BufRef))
1180     Str = StringRef(BufRef.data() + 3, BufRef.size() - 3);
1181 
1182   // Tokenize the contents into NewArgv.
1183   Tokenizer(Str, Saver, NewArgv, MarkEOLs);
1184 
1185   // Expanded file content may require additional transformations, like using
1186   // absolute paths instead of relative in '@file' constructs or expanding
1187   // macros.
1188   if (!RelativeNames && !InConfigFile)
1189     return Error::success();
1190 
1191   StringRef BasePath = llvm::sys::path::parent_path(FName);
1192   for (const char *&Arg : NewArgv) {
1193     if (!Arg)
1194       continue;
1195 
1196     // Substitute <CFGDIR> with the file's base path.
1197     if (InConfigFile)
1198       ExpandBasePaths(BasePath, Saver, Arg);
1199 
1200     // Discover the case, when argument should be transformed into '@file' and
1201     // evaluate 'file' for it.
1202     StringRef ArgStr(Arg);
1203     StringRef FileName;
1204     bool ConfigInclusion = false;
1205     if (ArgStr.consume_front("@")) {
1206       FileName = ArgStr;
1207       if (!llvm::sys::path::is_relative(FileName))
1208         continue;
1209     } else if (ArgStr.consume_front("--config=")) {
1210       FileName = ArgStr;
1211       ConfigInclusion = true;
1212     } else {
1213       continue;
1214     }
1215 
1216     // Update expansion construct.
1217     SmallString<128> ResponseFile;
1218     ResponseFile.push_back('@');
1219     if (ConfigInclusion && !llvm::sys::path::has_parent_path(FileName)) {
1220       SmallString<128> FilePath;
1221       if (!findConfigFile(FileName, FilePath))
1222         return createStringError(
1223             std::make_error_code(std::errc::no_such_file_or_directory),
1224             "cannot not find configuration file: " + FileName);
1225       ResponseFile.append(FilePath);
1226     } else {
1227       ResponseFile.append(BasePath);
1228       llvm::sys::path::append(ResponseFile, FileName);
1229     }
1230     Arg = Saver.save(ResponseFile.str()).data();
1231   }
1232   return Error::success();
1233 }
1234 
1235 /// Expand response files on a command line recursively using the given
1236 /// StringSaver and tokenization strategy.
expandResponseFiles(SmallVectorImpl<const char * > & Argv)1237 Error ExpansionContext::expandResponseFiles(
1238     SmallVectorImpl<const char *> &Argv) {
1239   struct ResponseFileRecord {
1240     std::string File;
1241     size_t End;
1242   };
1243 
1244   // To detect recursive response files, we maintain a stack of files and the
1245   // position of the last argument in the file. This position is updated
1246   // dynamically as we recursively expand files.
1247   SmallVector<ResponseFileRecord, 3> FileStack;
1248 
1249   // Push a dummy entry that represents the initial command line, removing
1250   // the need to check for an empty list.
1251   FileStack.push_back({"", Argv.size()});
1252 
1253   // Don't cache Argv.size() because it can change.
1254   for (unsigned I = 0; I != Argv.size();) {
1255     while (I == FileStack.back().End) {
1256       // Passing the end of a file's argument list, so we can remove it from the
1257       // stack.
1258       FileStack.pop_back();
1259     }
1260 
1261     const char *Arg = Argv[I];
1262     // Check if it is an EOL marker
1263     if (Arg == nullptr) {
1264       ++I;
1265       continue;
1266     }
1267 
1268     if (Arg[0] != '@') {
1269       ++I;
1270       continue;
1271     }
1272 
1273     const char *FName = Arg + 1;
1274     // Note that CurrentDir is only used for top-level rsp files, the rest will
1275     // always have an absolute path deduced from the containing file.
1276     SmallString<128> CurrDir;
1277     if (llvm::sys::path::is_relative(FName)) {
1278       if (CurrentDir.empty()) {
1279         if (auto CWD = FS->getCurrentWorkingDirectory()) {
1280           CurrDir = *CWD;
1281         } else {
1282           return createStringError(
1283               CWD.getError(), Twine("cannot get absolute path for: ") + FName);
1284         }
1285       } else {
1286         CurrDir = CurrentDir;
1287       }
1288       llvm::sys::path::append(CurrDir, FName);
1289       FName = CurrDir.c_str();
1290     }
1291 
1292     ErrorOr<llvm::vfs::Status> Res = FS->status(FName);
1293     if (!Res || !Res->exists()) {
1294       std::error_code EC = Res.getError();
1295       if (!InConfigFile) {
1296         // If the specified file does not exist, leave '@file' unexpanded, as
1297         // libiberty does.
1298         if (!EC || EC == llvm::errc::no_such_file_or_directory) {
1299           ++I;
1300           continue;
1301         }
1302       }
1303       if (!EC)
1304         EC = llvm::errc::no_such_file_or_directory;
1305       return createStringError(EC, Twine("cannot not open file '") + FName +
1306                                        "': " + EC.message());
1307     }
1308     const llvm::vfs::Status &FileStatus = Res.get();
1309 
1310     auto IsEquivalent =
1311         [FileStatus, this](const ResponseFileRecord &RFile) -> ErrorOr<bool> {
1312       ErrorOr<llvm::vfs::Status> RHS = FS->status(RFile.File);
1313       if (!RHS)
1314         return RHS.getError();
1315       return FileStatus.equivalent(*RHS);
1316     };
1317 
1318     // Check for recursive response files.
1319     for (const auto &F : drop_begin(FileStack)) {
1320       if (ErrorOr<bool> R = IsEquivalent(F)) {
1321         if (R.get())
1322           return createStringError(
1323               R.getError(), Twine("recursive expansion of: '") + F.File + "'");
1324       } else {
1325         return createStringError(R.getError(),
1326                                  Twine("cannot open file: ") + F.File);
1327       }
1328     }
1329 
1330     // Replace this response file argument with the tokenization of its
1331     // contents.  Nested response files are expanded in subsequent iterations.
1332     SmallVector<const char *, 0> ExpandedArgv;
1333     if (Error Err = expandResponseFile(FName, ExpandedArgv))
1334       return Err;
1335 
1336     for (ResponseFileRecord &Record : FileStack) {
1337       // Increase the end of all active records by the number of newly expanded
1338       // arguments, minus the response file itself.
1339       Record.End += ExpandedArgv.size() - 1;
1340     }
1341 
1342     FileStack.push_back({FName, I + ExpandedArgv.size()});
1343     Argv.erase(Argv.begin() + I);
1344     Argv.insert(Argv.begin() + I, ExpandedArgv.begin(), ExpandedArgv.end());
1345   }
1346 
1347   // If successful, the top of the file stack will mark the end of the Argv
1348   // stream. A failure here indicates a bug in the stack popping logic above.
1349   // Note that FileStack may have more than one element at this point because we
1350   // don't have a chance to pop the stack when encountering recursive files at
1351   // the end of the stream, so seeing that doesn't indicate a bug.
1352   assert(FileStack.size() > 0 && Argv.size() == FileStack.back().End);
1353   return Error::success();
1354 }
1355 
expandResponseFiles(int Argc,const char * const * Argv,const char * EnvVar,StringSaver & Saver,SmallVectorImpl<const char * > & NewArgv)1356 bool cl::expandResponseFiles(int Argc, const char *const *Argv,
1357                              const char *EnvVar, StringSaver &Saver,
1358                              SmallVectorImpl<const char *> &NewArgv) {
1359 #ifdef _WIN32
1360   auto Tokenize = cl::TokenizeWindowsCommandLine;
1361 #else
1362   auto Tokenize = cl::TokenizeGNUCommandLine;
1363 #endif
1364   // The environment variable specifies initial options.
1365   if (EnvVar)
1366     if (std::optional<std::string> EnvValue = sys::Process::GetEnv(EnvVar))
1367       Tokenize(*EnvValue, Saver, NewArgv, /*MarkEOLs=*/false);
1368 
1369   // Command line options can override the environment variable.
1370   NewArgv.append(Argv + 1, Argv + Argc);
1371   ExpansionContext ECtx(Saver.getAllocator(), Tokenize);
1372   if (Error Err = ECtx.expandResponseFiles(NewArgv)) {
1373     errs() << toString(std::move(Err)) << '\n';
1374     return false;
1375   }
1376   return true;
1377 }
1378 
ExpandResponseFiles(StringSaver & Saver,TokenizerCallback Tokenizer,SmallVectorImpl<const char * > & Argv)1379 bool cl::ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer,
1380                              SmallVectorImpl<const char *> &Argv) {
1381   ExpansionContext ECtx(Saver.getAllocator(), Tokenizer);
1382   if (Error Err = ECtx.expandResponseFiles(Argv)) {
1383     errs() << toString(std::move(Err)) << '\n';
1384     return false;
1385   }
1386   return true;
1387 }
1388 
ExpansionContext(BumpPtrAllocator & A,TokenizerCallback T)1389 ExpansionContext::ExpansionContext(BumpPtrAllocator &A, TokenizerCallback T)
1390     : Saver(A), Tokenizer(T), FS(vfs::getRealFileSystem().get()) {}
1391 
findConfigFile(StringRef FileName,SmallVectorImpl<char> & FilePath)1392 bool ExpansionContext::findConfigFile(StringRef FileName,
1393                                       SmallVectorImpl<char> &FilePath) {
1394   SmallString<128> CfgFilePath;
1395   const auto FileExists = [this](SmallString<128> Path) -> bool {
1396     auto Status = FS->status(Path);
1397     return Status &&
1398            Status->getType() == llvm::sys::fs::file_type::regular_file;
1399   };
1400 
1401   // If file name contains directory separator, treat it as a path to
1402   // configuration file.
1403   if (llvm::sys::path::has_parent_path(FileName)) {
1404     CfgFilePath = FileName;
1405     if (llvm::sys::path::is_relative(FileName) && FS->makeAbsolute(CfgFilePath))
1406       return false;
1407     if (!FileExists(CfgFilePath))
1408       return false;
1409     FilePath.assign(CfgFilePath.begin(), CfgFilePath.end());
1410     return true;
1411   }
1412 
1413   // Look for the file in search directories.
1414   for (const StringRef &Dir : SearchDirs) {
1415     if (Dir.empty())
1416       continue;
1417     CfgFilePath.assign(Dir);
1418     llvm::sys::path::append(CfgFilePath, FileName);
1419     llvm::sys::path::native(CfgFilePath);
1420     if (FileExists(CfgFilePath)) {
1421       FilePath.assign(CfgFilePath.begin(), CfgFilePath.end());
1422       return true;
1423     }
1424   }
1425 
1426   return false;
1427 }
1428 
readConfigFile(StringRef CfgFile,SmallVectorImpl<const char * > & Argv)1429 Error ExpansionContext::readConfigFile(StringRef CfgFile,
1430                                        SmallVectorImpl<const char *> &Argv) {
1431   SmallString<128> AbsPath;
1432   if (sys::path::is_relative(CfgFile)) {
1433     AbsPath.assign(CfgFile);
1434     if (std::error_code EC = FS->makeAbsolute(AbsPath))
1435       return make_error<StringError>(
1436           EC, Twine("cannot get absolute path for " + CfgFile));
1437     CfgFile = AbsPath.str();
1438   }
1439   InConfigFile = true;
1440   RelativeNames = true;
1441   if (Error Err = expandResponseFile(CfgFile, Argv))
1442     return Err;
1443   return expandResponseFiles(Argv);
1444 }
1445 
1446 static void initCommonOptions();
ParseCommandLineOptions(int argc,const char * const * argv,StringRef Overview,raw_ostream * Errs,const char * EnvVar,bool LongOptionsUseDoubleDash)1447 bool cl::ParseCommandLineOptions(int argc, const char *const *argv,
1448                                  StringRef Overview, raw_ostream *Errs,
1449                                  const char *EnvVar,
1450                                  bool LongOptionsUseDoubleDash) {
1451   initCommonOptions();
1452   SmallVector<const char *, 20> NewArgv;
1453   BumpPtrAllocator A;
1454   StringSaver Saver(A);
1455   NewArgv.push_back(argv[0]);
1456 
1457   // Parse options from environment variable.
1458   if (EnvVar) {
1459     if (std::optional<std::string> EnvValue =
1460             sys::Process::GetEnv(StringRef(EnvVar)))
1461       TokenizeGNUCommandLine(*EnvValue, Saver, NewArgv);
1462   }
1463 
1464   // Append options from command line.
1465   for (int I = 1; I < argc; ++I)
1466     NewArgv.push_back(argv[I]);
1467   int NewArgc = static_cast<int>(NewArgv.size());
1468 
1469   // Parse all options.
1470   return GlobalParser->ParseCommandLineOptions(NewArgc, &NewArgv[0], Overview,
1471                                                Errs, LongOptionsUseDoubleDash);
1472 }
1473 
1474 /// Reset all options at least once, so that we can parse different options.
ResetAllOptionOccurrences()1475 void CommandLineParser::ResetAllOptionOccurrences() {
1476   // Reset all option values to look like they have never been seen before.
1477   // Options might be reset twice (they can be reference in both OptionsMap
1478   // and one of the other members), but that does not harm.
1479   for (auto *SC : RegisteredSubCommands) {
1480     for (auto &O : SC->OptionsMap)
1481       O.second->reset();
1482     for (Option *O : SC->PositionalOpts)
1483       O->reset();
1484     for (Option *O : SC->SinkOpts)
1485       O->reset();
1486     if (SC->ConsumeAfterOpt)
1487       SC->ConsumeAfterOpt->reset();
1488   }
1489 }
1490 
ParseCommandLineOptions(int argc,const char * const * argv,StringRef Overview,raw_ostream * Errs,bool LongOptionsUseDoubleDash)1491 bool CommandLineParser::ParseCommandLineOptions(int argc,
1492                                                 const char *const *argv,
1493                                                 StringRef Overview,
1494                                                 raw_ostream *Errs,
1495                                                 bool LongOptionsUseDoubleDash) {
1496   assert(hasOptions() && "No options specified!");
1497 
1498   ProgramOverview = Overview;
1499   bool IgnoreErrors = Errs;
1500   if (!Errs)
1501     Errs = &errs();
1502   bool ErrorParsing = false;
1503 
1504   // Expand response files.
1505   SmallVector<const char *, 20> newArgv(argv, argv + argc);
1506   BumpPtrAllocator A;
1507 #ifdef _WIN32
1508   auto Tokenize = cl::TokenizeWindowsCommandLine;
1509 #else
1510   auto Tokenize = cl::TokenizeGNUCommandLine;
1511 #endif
1512   ExpansionContext ECtx(A, Tokenize);
1513   if (Error Err = ECtx.expandResponseFiles(newArgv)) {
1514     *Errs << toString(std::move(Err)) << '\n';
1515     return false;
1516   }
1517   argv = &newArgv[0];
1518   argc = static_cast<int>(newArgv.size());
1519 
1520   // Copy the program name into ProgName, making sure not to overflow it.
1521   ProgramName = std::string(sys::path::filename(StringRef(argv[0])));
1522 
1523   // Check out the positional arguments to collect information about them.
1524   unsigned NumPositionalRequired = 0;
1525 
1526   // Determine whether or not there are an unlimited number of positionals
1527   bool HasUnlimitedPositionals = false;
1528 
1529   int FirstArg = 1;
1530   SubCommand *ChosenSubCommand = &SubCommand::getTopLevel();
1531   if (argc >= 2 && argv[FirstArg][0] != '-') {
1532     // If the first argument specifies a valid subcommand, start processing
1533     // options from the second argument.
1534     ChosenSubCommand = LookupSubCommand(StringRef(argv[FirstArg]));
1535     if (ChosenSubCommand != &SubCommand::getTopLevel())
1536       FirstArg = 2;
1537   }
1538   GlobalParser->ActiveSubCommand = ChosenSubCommand;
1539 
1540   assert(ChosenSubCommand);
1541   auto &ConsumeAfterOpt = ChosenSubCommand->ConsumeAfterOpt;
1542   auto &PositionalOpts = ChosenSubCommand->PositionalOpts;
1543   auto &SinkOpts = ChosenSubCommand->SinkOpts;
1544   auto &OptionsMap = ChosenSubCommand->OptionsMap;
1545 
1546   for (auto *O: DefaultOptions) {
1547     addOption(O, true);
1548   }
1549 
1550   if (ConsumeAfterOpt) {
1551     assert(PositionalOpts.size() > 0 &&
1552            "Cannot specify cl::ConsumeAfter without a positional argument!");
1553   }
1554   if (!PositionalOpts.empty()) {
1555 
1556     // Calculate how many positional values are _required_.
1557     bool UnboundedFound = false;
1558     for (size_t i = 0, e = PositionalOpts.size(); i != e; ++i) {
1559       Option *Opt = PositionalOpts[i];
1560       if (RequiresValue(Opt))
1561         ++NumPositionalRequired;
1562       else if (ConsumeAfterOpt) {
1563         // ConsumeAfter cannot be combined with "optional" positional options
1564         // unless there is only one positional argument...
1565         if (PositionalOpts.size() > 1) {
1566           if (!IgnoreErrors)
1567             Opt->error("error - this positional option will never be matched, "
1568                        "because it does not Require a value, and a "
1569                        "cl::ConsumeAfter option is active!");
1570           ErrorParsing = true;
1571         }
1572       } else if (UnboundedFound && !Opt->hasArgStr()) {
1573         // This option does not "require" a value...  Make sure this option is
1574         // not specified after an option that eats all extra arguments, or this
1575         // one will never get any!
1576         //
1577         if (!IgnoreErrors)
1578           Opt->error("error - option can never match, because "
1579                      "another positional argument will match an "
1580                      "unbounded number of values, and this option"
1581                      " does not require a value!");
1582         *Errs << ProgramName << ": CommandLine Error: Option '" << Opt->ArgStr
1583               << "' is all messed up!\n";
1584         *Errs << PositionalOpts.size();
1585         ErrorParsing = true;
1586       }
1587       UnboundedFound |= EatsUnboundedNumberOfValues(Opt);
1588     }
1589     HasUnlimitedPositionals = UnboundedFound || ConsumeAfterOpt;
1590   }
1591 
1592   // PositionalVals - A vector of "positional" arguments we accumulate into
1593   // the process at the end.
1594   //
1595   SmallVector<std::pair<StringRef, unsigned>, 4> PositionalVals;
1596 
1597   // If the program has named positional arguments, and the name has been run
1598   // across, keep track of which positional argument was named.  Otherwise put
1599   // the positional args into the PositionalVals list...
1600   Option *ActivePositionalArg = nullptr;
1601 
1602   // Loop over all of the arguments... processing them.
1603   bool DashDashFound = false; // Have we read '--'?
1604   for (int i = FirstArg; i < argc; ++i) {
1605     Option *Handler = nullptr;
1606     Option *NearestHandler = nullptr;
1607     std::string NearestHandlerString;
1608     StringRef Value;
1609     StringRef ArgName = "";
1610     bool HaveDoubleDash = false;
1611 
1612     // Check to see if this is a positional argument.  This argument is
1613     // considered to be positional if it doesn't start with '-', if it is "-"
1614     // itself, or if we have seen "--" already.
1615     //
1616     if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
1617       // Positional argument!
1618       if (ActivePositionalArg) {
1619         ProvidePositionalOption(ActivePositionalArg, StringRef(argv[i]), i);
1620         continue; // We are done!
1621       }
1622 
1623       if (!PositionalOpts.empty()) {
1624         PositionalVals.push_back(std::make_pair(StringRef(argv[i]), i));
1625 
1626         // All of the positional arguments have been fulfulled, give the rest to
1627         // the consume after option... if it's specified...
1628         //
1629         if (PositionalVals.size() >= NumPositionalRequired && ConsumeAfterOpt) {
1630           for (++i; i < argc; ++i)
1631             PositionalVals.push_back(std::make_pair(StringRef(argv[i]), i));
1632           break; // Handle outside of the argument processing loop...
1633         }
1634 
1635         // Delay processing positional arguments until the end...
1636         continue;
1637       }
1638     } else if (argv[i][0] == '-' && argv[i][1] == '-' && argv[i][2] == 0 &&
1639                !DashDashFound) {
1640       DashDashFound = true; // This is the mythical "--"?
1641       continue;             // Don't try to process it as an argument itself.
1642     } else if (ActivePositionalArg &&
1643                (ActivePositionalArg->getMiscFlags() & PositionalEatsArgs)) {
1644       // If there is a positional argument eating options, check to see if this
1645       // option is another positional argument.  If so, treat it as an argument,
1646       // otherwise feed it to the eating positional.
1647       ArgName = StringRef(argv[i] + 1);
1648       // Eat second dash.
1649       if (!ArgName.empty() && ArgName[0] == '-') {
1650         HaveDoubleDash = true;
1651         ArgName = ArgName.substr(1);
1652       }
1653 
1654       Handler = LookupLongOption(*ChosenSubCommand, ArgName, Value,
1655                                  LongOptionsUseDoubleDash, HaveDoubleDash);
1656       if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
1657         ProvidePositionalOption(ActivePositionalArg, StringRef(argv[i]), i);
1658         continue; // We are done!
1659       }
1660     } else { // We start with a '-', must be an argument.
1661       ArgName = StringRef(argv[i] + 1);
1662       // Eat second dash.
1663       if (!ArgName.empty() && ArgName[0] == '-') {
1664         HaveDoubleDash = true;
1665         ArgName = ArgName.substr(1);
1666       }
1667 
1668       Handler = LookupLongOption(*ChosenSubCommand, ArgName, Value,
1669                                  LongOptionsUseDoubleDash, HaveDoubleDash);
1670 
1671       // Check to see if this "option" is really a prefixed or grouped argument.
1672       if (!Handler && !(LongOptionsUseDoubleDash && HaveDoubleDash))
1673         Handler = HandlePrefixedOrGroupedOption(ArgName, Value, ErrorParsing,
1674                                                 OptionsMap);
1675 
1676       // Otherwise, look for the closest available option to report to the user
1677       // in the upcoming error.
1678       if (!Handler && SinkOpts.empty())
1679         NearestHandler =
1680             LookupNearestOption(ArgName, OptionsMap, NearestHandlerString);
1681     }
1682 
1683     if (!Handler) {
1684       if (SinkOpts.empty()) {
1685         *Errs << ProgramName << ": Unknown command line argument '" << argv[i]
1686               << "'.  Try: '" << argv[0] << " --help'\n";
1687 
1688         if (NearestHandler) {
1689           // If we know a near match, report it as well.
1690           *Errs << ProgramName << ": Did you mean '"
1691                 << PrintArg(NearestHandlerString, 0) << "'?\n";
1692         }
1693 
1694         ErrorParsing = true;
1695       } else {
1696         for (Option *SinkOpt : SinkOpts)
1697           SinkOpt->addOccurrence(i, "", StringRef(argv[i]));
1698       }
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.size() > 2)
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       bool IsEmptyCategory = CategoryOptions.empty();
2471       if (!ShowHidden && IsEmptyCategory)
2472         continue;
2473 
2474       // Print category information.
2475       outs() << "\n";
2476       outs() << Category->getName() << ":\n";
2477 
2478       // Check if description is set.
2479       if (!Category->getDescription().empty())
2480         outs() << Category->getDescription() << "\n\n";
2481       else
2482         outs() << "\n";
2483 
2484       // When using --help-hidden explicitly state if the category has no
2485       // options associated with it.
2486       if (IsEmptyCategory) {
2487         outs() << "  This option category has no options.\n";
2488         continue;
2489       }
2490       // Loop over the options in the category and print.
2491       for (const Option *Opt : CategoryOptions)
2492         Opt->printOptionInfo(MaxArgLen);
2493     }
2494   }
2495 };
2496 
2497 // This wraps the Uncategorizing and Categorizing printers and decides
2498 // at run time which should be invoked.
2499 class HelpPrinterWrapper {
2500 private:
2501   HelpPrinter &UncategorizedPrinter;
2502   CategorizedHelpPrinter &CategorizedPrinter;
2503 
2504 public:
HelpPrinterWrapper(HelpPrinter & UncategorizedPrinter,CategorizedHelpPrinter & CategorizedPrinter)2505   explicit HelpPrinterWrapper(HelpPrinter &UncategorizedPrinter,
2506                               CategorizedHelpPrinter &CategorizedPrinter)
2507       : UncategorizedPrinter(UncategorizedPrinter),
2508         CategorizedPrinter(CategorizedPrinter) {}
2509 
2510   // Invoke the printer.
2511   void operator=(bool Value);
2512 };
2513 
2514 } // End anonymous namespace
2515 
2516 #if defined(__GNUC__)
2517 // GCC and GCC-compatible compilers define __OPTIMIZE__ when optimizations are
2518 // enabled.
2519 # if defined(__OPTIMIZE__)
2520 #  define LLVM_IS_DEBUG_BUILD 0
2521 # else
2522 #  define LLVM_IS_DEBUG_BUILD 1
2523 # endif
2524 #elif defined(_MSC_VER)
2525 // MSVC doesn't have a predefined macro indicating if optimizations are enabled.
2526 // Use _DEBUG instead. This macro actually corresponds to the choice between
2527 // debug and release CRTs, but it is a reasonable proxy.
2528 # if defined(_DEBUG)
2529 #  define LLVM_IS_DEBUG_BUILD 1
2530 # else
2531 #  define LLVM_IS_DEBUG_BUILD 0
2532 # endif
2533 #else
2534 // Otherwise, for an unknown compiler, assume this is an optimized build.
2535 # define LLVM_IS_DEBUG_BUILD 0
2536 #endif
2537 
2538 namespace {
2539 class VersionPrinter {
2540 public:
print(std::vector<VersionPrinterTy> ExtraPrinters={})2541   void print(std::vector<VersionPrinterTy> ExtraPrinters = {}) {
2542     raw_ostream &OS = outs();
2543 #ifdef PACKAGE_VENDOR
2544     OS << PACKAGE_VENDOR << " ";
2545 #else
2546     OS << "LLVM (http://llvm.org/):\n  ";
2547 #endif
2548     OS << PACKAGE_NAME << " version " << PACKAGE_VERSION << "\n  ";
2549 #if LLVM_IS_DEBUG_BUILD
2550     OS << "DEBUG build";
2551 #else
2552     OS << "Optimized build";
2553 #endif
2554 #ifndef NDEBUG
2555     OS << " with assertions";
2556 #endif
2557     OS << ".\n";
2558 
2559     // Iterate over any registered extra printers and call them to add further
2560     // information.
2561     if (!ExtraPrinters.empty()) {
2562       for (const auto &I : ExtraPrinters)
2563         I(outs());
2564     }
2565   }
2566   void operator=(bool OptionWasSpecified);
2567 };
2568 
2569 struct CommandLineCommonOptions {
2570   // Declare the four HelpPrinter instances that are used to print out help, or
2571   // help-hidden as an uncategorized list or in categories.
2572   HelpPrinter UncategorizedNormalPrinter{false};
2573   HelpPrinter UncategorizedHiddenPrinter{true};
2574   CategorizedHelpPrinter CategorizedNormalPrinter{false};
2575   CategorizedHelpPrinter CategorizedHiddenPrinter{true};
2576   // Declare HelpPrinter wrappers that will decide whether or not to invoke
2577   // a categorizing help printer
2578   HelpPrinterWrapper WrappedNormalPrinter{UncategorizedNormalPrinter,
2579                                           CategorizedNormalPrinter};
2580   HelpPrinterWrapper WrappedHiddenPrinter{UncategorizedHiddenPrinter,
2581                                           CategorizedHiddenPrinter};
2582   // Define a category for generic options that all tools should have.
2583   cl::OptionCategory GenericCategory{"Generic Options"};
2584 
2585   // Define uncategorized help printers.
2586   // --help-list is hidden by default because if Option categories are being
2587   // used then --help behaves the same as --help-list.
2588   cl::opt<HelpPrinter, true, parser<bool>> HLOp{
2589       "help-list",
2590       cl::desc(
2591           "Display list of available options (--help-list-hidden for more)"),
2592       cl::location(UncategorizedNormalPrinter),
2593       cl::Hidden,
2594       cl::ValueDisallowed,
2595       cl::cat(GenericCategory),
2596       cl::sub(SubCommand::getAll())};
2597 
2598   cl::opt<HelpPrinter, true, parser<bool>> HLHOp{
2599       "help-list-hidden",
2600       cl::desc("Display list of all available options"),
2601       cl::location(UncategorizedHiddenPrinter),
2602       cl::Hidden,
2603       cl::ValueDisallowed,
2604       cl::cat(GenericCategory),
2605       cl::sub(SubCommand::getAll())};
2606 
2607   // Define uncategorized/categorized help printers. These printers change their
2608   // behaviour at runtime depending on whether one or more Option categories
2609   // have been declared.
2610   cl::opt<HelpPrinterWrapper, true, parser<bool>> HOp{
2611       "help",
2612       cl::desc("Display available options (--help-hidden for more)"),
2613       cl::location(WrappedNormalPrinter),
2614       cl::ValueDisallowed,
2615       cl::cat(GenericCategory),
2616       cl::sub(SubCommand::getAll())};
2617 
2618   cl::alias HOpA{"h", cl::desc("Alias for --help"), cl::aliasopt(HOp),
2619                  cl::DefaultOption};
2620 
2621   cl::opt<HelpPrinterWrapper, true, parser<bool>> HHOp{
2622       "help-hidden",
2623       cl::desc("Display all available options"),
2624       cl::location(WrappedHiddenPrinter),
2625       cl::Hidden,
2626       cl::ValueDisallowed,
2627       cl::cat(GenericCategory),
2628       cl::sub(SubCommand::getAll())};
2629 
2630   cl::opt<bool> PrintOptions{
2631       "print-options",
2632       cl::desc("Print non-default options after command line parsing"),
2633       cl::Hidden,
2634       cl::init(false),
2635       cl::cat(GenericCategory),
2636       cl::sub(SubCommand::getAll())};
2637 
2638   cl::opt<bool> PrintAllOptions{
2639       "print-all-options",
2640       cl::desc("Print all option values after command line parsing"),
2641       cl::Hidden,
2642       cl::init(false),
2643       cl::cat(GenericCategory),
2644       cl::sub(SubCommand::getAll())};
2645 
2646   VersionPrinterTy OverrideVersionPrinter = nullptr;
2647 
2648   std::vector<VersionPrinterTy> ExtraVersionPrinters;
2649 
2650   // Define the --version option that prints out the LLVM version for the tool
2651   VersionPrinter VersionPrinterInstance;
2652 
2653   cl::opt<VersionPrinter, true, parser<bool>> VersOp{
2654       "version", cl::desc("Display the version of this program"),
2655       cl::location(VersionPrinterInstance), cl::ValueDisallowed,
2656       cl::cat(GenericCategory)};
2657 };
2658 } // End anonymous namespace
2659 
2660 // Lazy-initialized global instance of options controlling the command-line
2661 // parser and general handling.
2662 static ManagedStatic<CommandLineCommonOptions> CommonOptions;
2663 
initCommonOptions()2664 static void initCommonOptions() {
2665   *CommonOptions;
2666   initDebugCounterOptions();
2667   initGraphWriterOptions();
2668   initSignalsOptions();
2669   initStatisticOptions();
2670   initTimerOptions();
2671   initTypeSizeOptions();
2672   initWithColorOptions();
2673   initDebugOptions();
2674   initRandomSeedOptions();
2675 }
2676 
getGeneralCategory()2677 OptionCategory &cl::getGeneralCategory() {
2678   // Initialise the general option category.
2679   static OptionCategory GeneralCategory{"General options"};
2680   return GeneralCategory;
2681 }
2682 
operator =(bool OptionWasSpecified)2683 void VersionPrinter::operator=(bool OptionWasSpecified) {
2684   if (!OptionWasSpecified)
2685     return;
2686 
2687   if (CommonOptions->OverrideVersionPrinter != nullptr) {
2688     CommonOptions->OverrideVersionPrinter(outs());
2689     exit(0);
2690   }
2691   print(CommonOptions->ExtraVersionPrinters);
2692 
2693   exit(0);
2694 }
2695 
operator =(bool Value)2696 void HelpPrinterWrapper::operator=(bool Value) {
2697   if (!Value)
2698     return;
2699 
2700   // Decide which printer to invoke. If more than one option category is
2701   // registered then it is useful to show the categorized help instead of
2702   // uncategorized help.
2703   if (GlobalParser->RegisteredOptionCategories.size() > 1) {
2704     // unhide --help-list option so user can have uncategorized output if they
2705     // want it.
2706     CommonOptions->HLOp.setHiddenFlag(NotHidden);
2707 
2708     CategorizedPrinter = true; // Invoke categorized printer
2709   } else
2710     UncategorizedPrinter = true; // Invoke uncategorized printer
2711 }
2712 
2713 // Print the value of each option.
PrintOptionValues()2714 void cl::PrintOptionValues() { GlobalParser->printOptionValues(); }
2715 
printOptionValues()2716 void CommandLineParser::printOptionValues() {
2717   if (!CommonOptions->PrintOptions && !CommonOptions->PrintAllOptions)
2718     return;
2719 
2720   SmallVector<std::pair<const char *, Option *>, 128> Opts;
2721   sortOpts(ActiveSubCommand->OptionsMap, Opts, /*ShowHidden*/ true);
2722 
2723   // Compute the maximum argument length...
2724   size_t MaxArgLen = 0;
2725   for (size_t i = 0, e = Opts.size(); i != e; ++i)
2726     MaxArgLen = std::max(MaxArgLen, Opts[i].second->getOptionWidth());
2727 
2728   for (size_t i = 0, e = Opts.size(); i != e; ++i)
2729     Opts[i].second->printOptionValue(MaxArgLen, CommonOptions->PrintAllOptions);
2730 }
2731 
2732 // Utility function for printing the help message.
PrintHelpMessage(bool Hidden,bool Categorized)2733 void cl::PrintHelpMessage(bool Hidden, bool Categorized) {
2734   if (!Hidden && !Categorized)
2735     CommonOptions->UncategorizedNormalPrinter.printHelp();
2736   else if (!Hidden && Categorized)
2737     CommonOptions->CategorizedNormalPrinter.printHelp();
2738   else if (Hidden && !Categorized)
2739     CommonOptions->UncategorizedHiddenPrinter.printHelp();
2740   else
2741     CommonOptions->CategorizedHiddenPrinter.printHelp();
2742 }
2743 
2744 /// Utility function for printing version number.
PrintVersionMessage()2745 void cl::PrintVersionMessage() {
2746   CommonOptions->VersionPrinterInstance.print(CommonOptions->ExtraVersionPrinters);
2747 }
2748 
SetVersionPrinter(VersionPrinterTy func)2749 void cl::SetVersionPrinter(VersionPrinterTy func) {
2750   CommonOptions->OverrideVersionPrinter = func;
2751 }
2752 
AddExtraVersionPrinter(VersionPrinterTy func)2753 void cl::AddExtraVersionPrinter(VersionPrinterTy func) {
2754   CommonOptions->ExtraVersionPrinters.push_back(func);
2755 }
2756 
getRegisteredOptions(SubCommand & Sub)2757 StringMap<Option *> &cl::getRegisteredOptions(SubCommand &Sub) {
2758   initCommonOptions();
2759   auto &Subs = GlobalParser->RegisteredSubCommands;
2760   (void)Subs;
2761   assert(is_contained(Subs, &Sub));
2762   return Sub.OptionsMap;
2763 }
2764 
2765 iterator_range<typename SmallPtrSet<SubCommand *, 4>::iterator>
getRegisteredSubcommands()2766 cl::getRegisteredSubcommands() {
2767   return GlobalParser->getRegisteredSubcommands();
2768 }
2769 
HideUnrelatedOptions(cl::OptionCategory & Category,SubCommand & Sub)2770 void cl::HideUnrelatedOptions(cl::OptionCategory &Category, SubCommand &Sub) {
2771   initCommonOptions();
2772   for (auto &I : Sub.OptionsMap) {
2773     bool Unrelated = true;
2774     for (auto &Cat : I.second->Categories) {
2775       if (Cat == &Category || Cat == &CommonOptions->GenericCategory)
2776         Unrelated = false;
2777     }
2778     if (Unrelated)
2779       I.second->setHiddenFlag(cl::ReallyHidden);
2780   }
2781 }
2782 
HideUnrelatedOptions(ArrayRef<const cl::OptionCategory * > Categories,SubCommand & Sub)2783 void cl::HideUnrelatedOptions(ArrayRef<const cl::OptionCategory *> Categories,
2784                               SubCommand &Sub) {
2785   initCommonOptions();
2786   for (auto &I : Sub.OptionsMap) {
2787     bool Unrelated = true;
2788     for (auto &Cat : I.second->Categories) {
2789       if (is_contained(Categories, Cat) ||
2790           Cat == &CommonOptions->GenericCategory)
2791         Unrelated = false;
2792     }
2793     if (Unrelated)
2794       I.second->setHiddenFlag(cl::ReallyHidden);
2795   }
2796 }
2797 
ResetCommandLineParser()2798 void cl::ResetCommandLineParser() { GlobalParser->reset(); }
ResetAllOptionOccurrences()2799 void cl::ResetAllOptionOccurrences() {
2800   GlobalParser->ResetAllOptionOccurrences();
2801 }
2802 
LLVMParseCommandLineOptions(int argc,const char * const * argv,const char * Overview)2803 void LLVMParseCommandLineOptions(int argc, const char *const *argv,
2804                                  const char *Overview) {
2805   llvm::cl::ParseCommandLineOptions(argc, argv, StringRef(Overview),
2806                                     &llvm::nulls());
2807 }
2808