llvm.org GIT mirror llvm / b7d8ee4
Use StringRef in CommandLine Options handling (NFC) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@283007 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 2 years ago
6 changed file(s) with 140 addition(s) and 140 deletion(s). Raw diff Collapse all Expand all
131131 // Add existing passes to option.
132132 for (RegistryClass *Node = RegistryClass::getList();
133133 Node; Node = Node->getNext()) {
134 this->addLiteralOption(Node->getName().data(),
134 this->addLiteralOption(Node->getName(),
135135 (typename RegistryClass::FunctionPassCtor)Node->getCtor(),
136 Node->getDescription().data());
136 Node->getDescription());
137137 }
138138
139139 // Make sure we listen for list changes.
143143 // Implement the MachinePassRegistryListener callbacks.
144144 //
145145 void NotifyAdd(StringRef N, MachinePassCtor C, StringRef D) override {
146 this->addLiteralOption(N.data(),
147 (typename RegistryClass::FunctionPassCtor)C,
148 D.data());
146 this->addLiteralOption(N, (typename RegistryClass::FunctionPassCtor)C, D);
149147 }
150148 void NotifyRemove(StringRef N) override {
151 this->removeLiteralOption(N.data());
149 this->removeLiteralOption(N);
152150 }
153151 };
154152
4646 // ParseCommandLineOptions - Command line option processing entry point.
4747 //
4848 bool ParseCommandLineOptions(int argc, const char *const *argv,
49 const char *Overview = nullptr,
49 StringRef Overview = "",
5050 bool IgnoreErrors = false);
5151
5252 //===----------------------------------------------------------------------===//
5454 // entry point.
5555 //
5656 void ParseEnvironmentOptions(const char *progName, const char *envvar,
57 const char *Overview = nullptr);
57 const char *Overview = "");
5858
5959 ///===---------------------------------------------------------------------===//
6060 /// SetVersionPrinter - Override the default (LLVM specific) version printer
8888 ///
8989 /// Literal options are used by some parsers to register special option values.
9090 /// This is how the PassNameParser registers pass names for opt.
91 void AddLiteralOption(Option &O, const char *Name);
91 void AddLiteralOption(Option &O, StringRef Name);
9292
9393 //===----------------------------------------------------------------------===//
9494 // Flags permitted to be passed to command line arguments
156156 //
157157 class OptionCategory {
158158 private:
159 const char *const Name;
160 const char *const Description;
159 StringRef const Name;
160 StringRef const Description;
161161 void registerCategory();
162162
163163 public:
164 OptionCategory(const char *const Name,
165 const char *const Description = nullptr)
164 OptionCategory(StringRef const Name,
165 StringRef const Description = "")
166166 : Name(Name), Description(Description) {
167167 registerCategory();
168168 }
169 const char *getName() const { return Name; }
170 const char *getDescription() const { return Description; }
169 StringRef getName() const { return Name; }
170 StringRef getDescription() const { return Description; }
171171 };
172172
173173 // The general Option Category (used as default category).
178178 //
179179 class SubCommand {
180180 private:
181 const char *const Name = nullptr;
182 const char *const Description = nullptr;
181 StringRef Name = "";
182 StringRef Description = "";
183183
184184 protected:
185185 void registerSubCommand();
186186 void unregisterSubCommand();
187187
188188 public:
189 SubCommand(const char *const Name, const char *const Description = nullptr)
189 SubCommand(StringRef Name, StringRef Description = "")
190190 : Name(Name), Description(Description) {
191 registerSubCommand();
191 registerSubCommand();
192192 }
193193 SubCommand() {}
194194
196196
197197 operator bool() const;
198198
199 const char *getName() const { return Name; }
200 const char *getDescription() const { return Description; }
199 StringRef getName() const { return Name; }
200 StringRef getDescription() const { return Description; }
201201
202202 SmallVector
203203 SmallVector
352352
353353 // desc - Modifier to set the description shown in the -help output...
354354 struct desc {
355 const char *Desc;
356 desc(const char *Str) : Desc(Str) {}
355 StringRef Desc;
356 desc(StringRef Str) : Desc(Str) {}
357357 void apply(Option &O) const { O.setDescription(Desc); }
358358 };
359359
360360 // value_desc - Modifier to set the value description shown in the -help
361361 // output...
362362 struct value_desc {
363 const char *Desc;
364 value_desc(const char *Str) : Desc(Str) {}
363 StringRef Desc;
364 value_desc(StringRef Str) : Desc(Str) {}
365365 void apply(Option &O) const { O.setValueStr(Desc); }
366366 };
367367
568568 // Use a vector instead of a map, because the lists should be short,
569569 // the overhead is less, and most importantly, it keeps them in the order
570570 // inserted so we can print our option out nicely.
571 SmallVectorconst char *, std::pair>>, 4> Values;
571 SmallVectorStringRef , std::pair>>, 4> Values;
572572 void processValues(va_list Vals);
573573
574574 public:
575 ValuesClass(const char *EnumName, DataType Val, const char *Desc,
575 ValuesClass(StringRef EnumName, DataType Val, StringRef Desc,
576576 va_list ValueArgs) {
577577 // Insert the first value, which is required.
578578 Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc)));
579579
580580 // Process the varargs portion of the values...
581 while (const char *enumName = va_arg(ValueArgs, const char *)) {
581 while (const char *enumName = va_arg(ValueArgs, const char * )) {
582582 DataType EnumVal = static_cast(va_arg(ValueArgs, int));
583 const char *EnumDesc = va_arg(ValueArgs, const char *);
584 Values.push_back(std::make_pair(enumName, // Add value to value map
583 auto EnumDesc = StringRef(va_arg(ValueArgs, const char * ));
584 Values.push_back(std::make_pair(StringRef(enumName), // Add value to value map
585585 std::make_pair(EnumVal, EnumDesc)));
586586 }
587587 }
595595
596596 template
597597 ValuesClass LLVM_END_WITH_NULL
598 values(const char *Arg, DataType Val, const char *Desc, ...) {
598 values(StringRef Arg, DataType Val, StringRef Desc, ...) {
599599 va_list ValueArgs;
600600 va_start(ValueArgs, Desc);
601601 ValuesClass Vals(Arg, Val, Desc, ValueArgs);
619619 protected:
620620 class GenericOptionInfo {
621621 public:
622 GenericOptionInfo(const char *name, const char *helpStr)
622 GenericOptionInfo(StringRef name, StringRef helpStr)
623623 : Name(name), HelpStr(helpStr) {}
624 const char *Name;
625 const char *HelpStr;
624 StringRef Name;
625 StringRef HelpStr;
626626 };
627627
628628 public:
636636 virtual unsigned getNumOptions() const = 0;
637637
638638 // getOption - Return option name N.
639 virtual const char *getOption(unsigned N) const = 0;
639 virtual StringRef getOption(unsigned N) const = 0;
640640
641641 // getDescription - Return description N
642 virtual const char *getDescription(unsigned N) const = 0;
642 virtual StringRef getDescription(unsigned N) const = 0;
643643
644644 // Return the width of the option tag for printing...
645645 virtual size_t getOptionWidth(const Option &O) const;
698698 // findOption - Return the option number corresponding to the specified
699699 // argument string. If the option is not found, getNumOptions() is returned.
700700 //
701 unsigned findOption(const char *Name);
701 unsigned findOption(StringRef Name);
702702
703703 protected:
704704 Option &Owner;
714714 protected:
715715 class OptionInfo : public GenericOptionInfo {
716716 public:
717 OptionInfo(const char *name, DataType v, const char *helpStr)
717 OptionInfo(StringRef name, DataType v, StringRef helpStr)
718718 : GenericOptionInfo(name, helpStr), V(v) {}
719719 OptionValue V;
720720 };
726726
727727 // Implement virtual functions needed by generic_parser_base
728728 unsigned getNumOptions() const override { return unsigned(Values.size()); }
729 const char *getOption(unsigned N) const override { return Values[N].Name; }
730 const char *getDescription(unsigned N) const override {
729 StringRef getOption(unsigned N) const override { return Values[N].Name; }
730 StringRef getDescription(unsigned N) const override {
731731 return Values[N].HelpStr;
732732 }
733733
756756 /// addLiteralOption - Add an entry to the mapping table.
757757 ///
758758 template
759 void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) {
759 void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr) {
760760 assert(findOption(Name) == Values.size() && "Option already exists!");
761761 OptionInfo X(Name, static_cast(V), HelpStr);
762762 Values.push_back(X);
765765
766766 /// removeLiteralOption - Remove the specified option.
767767 ///
768 void removeLiteralOption(const char *Name) {
768 void removeLiteralOption(StringRef Name) {
769769 unsigned N = findOption(Name);
770770 assert(N != Values.size() && "Option not found!");
771771 Values.erase(Values.begin() + N);
801801 void printOptionNoValue(const Option &O, size_t GlobalWidth) const;
802802
803803 // getValueName - Overload in subclass to provide a better default value.
804 virtual const char *getValueName() const { return "value"; }
804 virtual StringRef getValueName() const { return "value"; }
805805
806806 // An out-of-line virtual method to provide a 'home' for this class.
807807 virtual void anchor();
843843 }
844844
845845 // getValueName - Do not print = at all.
846 const char *getValueName() const override { return nullptr; }
846 StringRef getValueName() const override { return StringRef(); }
847847
848848 void printOptionDiff(const Option &O, bool V, OptVal Default,
849849 size_t GlobalWidth) const;
869869 }
870870
871871 // getValueName - Do not print = at all.
872 const char *getValueName() const override { return nullptr; }
872 StringRef getValueName() const override { return StringRef(); }
873873
874874 void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default,
875875 size_t GlobalWidth) const;
891891 bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val);
892892
893893 // getValueName - Overload in subclass to provide a better default value.
894 const char *getValueName() const override { return "int"; }
894 StringRef getValueName() const override { return "int"; }
895895
896896 void printOptionDiff(const Option &O, int V, OptVal Default,
897897 size_t GlobalWidth) const;
913913 bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val);
914914
915915 // getValueName - Overload in subclass to provide a better default value.
916 const char *getValueName() const override { return "uint"; }
916 StringRef getValueName() const override { return "uint"; }
917917
918918 void printOptionDiff(const Option &O, unsigned V, OptVal Default,
919919 size_t GlobalWidth) const;
938938 unsigned long long &Val);
939939
940940 // getValueName - Overload in subclass to provide a better default value.
941 const char *getValueName() const override { return "uint"; }
941 StringRef getValueName() const override { return "uint"; }
942942
943943 void printOptionDiff(const Option &O, unsigned long long V, OptVal Default,
944944 size_t GlobalWidth) const;
960960 bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val);
961961
962962 // getValueName - Overload in subclass to provide a better default value.
963 const char *getValueName() const override { return "number"; }
963 StringRef getValueName() const override { return "number"; }
964964
965965 void printOptionDiff(const Option &O, double V, OptVal Default,
966966 size_t GlobalWidth) const;
982982 bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val);
983983
984984 // getValueName - Overload in subclass to provide a better default value.
985 const char *getValueName() const override { return "number"; }
985 StringRef getValueName() const override { return "number"; }
986986
987987 void printOptionDiff(const Option &O, float V, OptVal Default,
988988 size_t GlobalWidth) const;
10071007 }
10081008
10091009 // getValueName - Overload in subclass to provide a better default value.
1010 const char *getValueName() const override { return "string"; }
1010 StringRef getValueName() const override { return "string"; }
10111011
10121012 void printOptionDiff(const Option &O, StringRef V, const OptVal &Default,
10131013 size_t GlobalWidth) const;
10321032 }
10331033
10341034 // getValueName - Overload in subclass to provide a better default value.
1035 const char *getValueName() const override { return "char"; }
1035 StringRef getValueName() const override { return "char"; }
10361036
10371037 void printOptionDiff(const Option &O, char V, OptVal Default,
10381038 size_t GlobalWidth) const;
11001100
11011101 // Handle const char* as a special case...
11021102 template struct applicator {
1103 template static void opt(const char *Str, Opt &O) {
1103 template static void opt(StringRef Str, Opt &O) {
11041104 O.setArgStr(Str);
11051105 }
11061106 };
11071107 template struct applicator {
1108 template static void opt(const char *Str, Opt &O) {
1108 template static void opt(StringRef Str, Opt &O) {
11091109 O.setArgStr(Str);
11101110 }
11111111 };
1112 template <> struct applicator {
1113 template static void opt(const char *Str, Opt &O) {
1112 template <> struct applicator {
1113 template static void opt(StringRef Str, Opt &O) {
11141114 O.setArgStr(Str);
11151115 }
11161116 };
16871687 // printed to stderr at the end of the regular help, just before
16881688 // exit is called.
16891689 struct extrahelp {
1690 const char *morehelp;
1691 explicit extrahelp(const char *help);
1690 StringRef morehelp;
1691 explicit extrahelp(StringRef help);
16921692 };
16931693
16941694 void PrintVersionMessage();
9292 /// option stores (\p ValT), the class that will read the option (\p Base),
9393 /// and the member that the class will store the data into (\p Mem).
9494 template
95 static void registerOption(const char *ArgStr, const char *Desc,
95 static void registerOption(StringRef ArgStr, StringRef Desc,
9696 const ValT &InitValue) {
9797 cl::opt *Option = new cl::opt(ArgStr, cl::desc(Desc),
9898 cl::Hidden, cl::init(InitValue));
9191 // Globals for name and overview of program. Program name is not a string to
9292 // avoid static ctor/dtor issues.
9393 std::string ProgramName;
94 const char *ProgramOverview;
94 StringRef ProgramOverview;
9595
9696 // This collects additional help to be printed.
97 std::vector<const char *> MoreHelp;
97 std::vector<StringRef> MoreHelp;
9898
9999 // This collects the different option categories that have been registered.
100100 SmallPtrSet RegisteredOptionCategories;
102102 // This collects the different subcommands that have been registered.
103103 SmallPtrSet RegisteredSubCommands;
104104
105 CommandLineParser() : ProgramOverview(nullptr), ActiveSubCommand(nullptr) {
105 CommandLineParser() : ActiveSubCommand(nullptr) {
106106 registerSubCommand(&*TopLevelSubCommand);
107107 registerSubCommand(&*AllSubCommands);
108108 }
110110 void ResetAllOptionOccurrences();
111111
112112 bool ParseCommandLineOptions(int argc, const char *const *argv,
113 const char *Overview, bool IgnoreErrors);
114
115 void addLiteralOption(Option &Opt, SubCommand *SC, const char *Name) {
113 StringRef Overview, bool IgnoreErrors);
114
115 void addLiteralOption(Option &Opt, SubCommand *SC, StringRef Name) {
116116 if (Opt.hasArgStr())
117117 return;
118118 if (!SC->OptionsMap.insert(std::make_pair(Name, &Opt)).second) {
132132 }
133133 }
134134
135 void addLiteralOption(Option &Opt, const char *Name) {
135 void addLiteralOption(Option &Opt, StringRef Name) {
136136 if (Opt.Subs.empty())
137137 addLiteralOption(Opt, &*TopLevelSubCommand, Name);
138138 else {
284284 void registerSubCommand(SubCommand *sub) {
285285 assert(count_if(RegisteredSubCommands,
286286 [sub](const SubCommand *Sub) {
287 return (sub->getName() != nullptr) &&
287 return (!sub->getName().empty()) &&
288288 (Sub->getName() == sub->getName());
289289 }) == 0 &&
290290 "Duplicate subcommands");
299299 O->hasArgStr())
300300 addOption(O, sub);
301301 else
302 addLiteralOption(*O, sub, E.first().str().c_str());
302 addLiteralOption(*O, sub, E.first());
303303 }
304304 }
305305 }
311311 void reset() {
312312 ActiveSubCommand = nullptr;
313313 ProgramName.clear();
314 ProgramOverview = nullptr;
314 ProgramOverview = StringRef();
315315
316316 MoreHelp.clear();
317317 RegisteredOptionCategories.clear();
329329 SubCommand *ActiveSubCommand;
330330
331331 Option *LookupOption(SubCommand &Sub, StringRef &Arg, StringRef &Value);
332 SubCommand *LookupSubCommand(const char *Name);
332 SubCommand *LookupSubCommand(StringRef Name);
333333 };
334334
335335 } // namespace
336336
337337 static ManagedStatic GlobalParser;
338338
339 void cl::AddLiteralOption(Option &O, const char *Name) {
339 void cl::AddLiteralOption(Option &O, StringRef Name) {
340340 GlobalParser->addLiteralOption(O, Name);
341341 }
342342
343 extrahelp::extrahelp(const char *Help) : morehelp(Help) {
343 extrahelp::extrahelp(StringRef Help) : morehelp(Help) {
344344 GlobalParser->MoreHelp.push_back(Help);
345345 }
346346
427427 return I->second;
428428 }
429429
430 SubCommand *CommandLineParser::LookupSubCommand(const char *Name) {
431 if (Name == nullptr)
430 SubCommand *CommandLineParser::LookupSubCommand(StringRef Name) {
431 if (Name.empty())
432432 return &*TopLevelSubCommand;
433433 for (auto S : RegisteredSubCommands) {
434434 if (S == &*AllSubCommands)
435435 continue;
436 if (S->getName() == nullptr)
436 if (S->getName().empty())
437437 continue;
438438
439439 if (StringRef(S->getName()) == StringRef(Name))
533533 return Handler->error("requires a value!");
534534 // Steal the next argument, like for '-o filename'
535535 assert(argv && "null check");
536 Value = argv[++i];
536 Value = StringRef(argv[++i]);
537537 }
538538 break;
539539 case ValueDisallowed:
567567 if (i + 1 >= argc)
568568 return Handler->error("not enough values!");
569569 assert(argv && "null check");
570 Value = argv[++i];
570 Value = StringRef(argv[++i]);
571571
572572 if (CommaSeparateAndAddOccurrence(Handler, i, ArgName, Value, MultiArg))
573573 return true;
725725 // End the token if this is whitespace.
726726 if (isWhitespace(Src[I])) {
727727 if (!Token.empty())
728 NewArgv.push_back(Saver.save(Token.c_str()));
728 NewArgv.push_back(Saver.save(StringRef(Token)));
729729 Token.clear();
730730 continue;
731731 }
736736
737737 // Append the last token after hitting EOF with no whitespace.
738738 if (!Token.empty())
739 NewArgv.push_back(Saver.save(Token.c_str()));
739 NewArgv.push_back(Saver.save(StringRef(Token)));
740740 // Mark the end of response files
741741 if (MarkEOLs)
742742 NewArgv.push_back(nullptr);
817817 if (State == UNQUOTED) {
818818 // Whitespace means the end of the token.
819819 if (isWhitespace(Src[I])) {
820 NewArgv.push_back(Saver.save(Token.c_str()));
820 NewArgv.push_back(Saver.save(StringRef(Token)));
821821 Token.clear();
822822 State = INIT;
823823 // Mark the end of lines in response files
852852 }
853853 // Append the last token after hitting EOF with no whitespace.
854854 if (!Token.empty())
855 NewArgv.push_back(Saver.save(Token.c_str()));
855 NewArgv.push_back(Saver.save(StringRef(Token)));
856856 // Mark the end of response files
857857 if (MarkEOLs)
858858 NewArgv.push_back(nullptr);
952952 assert(envVar && "Environment variable name missing");
953953
954954 // Get the environment variable they want us to parse options out of.
955 llvm::Optional envValue = sys::Process::GetEnv(envVar);
955 llvm::Optional envValue = sys::Process::GetEnv(StringRef(envVar));
956956 if (!envValue)
957957 return;
958958
967967 // and hand it off to ParseCommandLineOptions().
968968 TokenizeGNUCommandLine(*envValue, Saver, newArgv);
969969 int newArgc = static_cast(newArgv.size());
970 ParseCommandLineOptions(newArgc, &newArgv[0], Overview);
970 ParseCommandLineOptions(newArgc, &newArgv[0], StringRef(Overview));
971971 }
972972
973973 bool cl::ParseCommandLineOptions(int argc, const char *const *argv,
974 const char *Overview, bool IgnoreErrors) {
974 StringRef Overview, bool IgnoreErrors) {
975975 return GlobalParser->ParseCommandLineOptions(argc, argv, Overview,
976976 IgnoreErrors);
977977 }
987987
988988 bool CommandLineParser::ParseCommandLineOptions(int argc,
989989 const char *const *argv,
990 const char *Overview,
990 StringRef Overview,
991991 bool IgnoreErrors) {
992992 assert(hasOptions() && "No options specified!");
993993
10001000 argc = static_cast(newArgv.size());
10011001
10021002 // Copy the program name into ProgName, making sure not to overflow it.
1003 ProgramName = sys::path::filename(argv[0]);
1003 ProgramName = sys::path::filename(StringRef(argv[0]));
10041004
10051005 ProgramOverview = Overview;
10061006 bool ErrorParsing = false;
10161016 if (argc >= 2 && argv[FirstArg][0] != '-') {
10171017 // If the first argument specifies a valid subcommand, start processing
10181018 // options from the second argument.
1019 ChosenSubCommand = LookupSubCommand(argv[FirstArg]);
1019 ChosenSubCommand = LookupSubCommand(StringRef(argv[FirstArg]));
10201020 if (ChosenSubCommand != &*TopLevelSubCommand)
10211021 FirstArg = 2;
10221022 }
10971097 if (argv[i][0] != '-' || argv[i][1] == 0 || DashDashFound) {
10981098 // Positional argument!
10991099 if (ActivePositionalArg) {
1100 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
1100 ProvidePositionalOption(ActivePositionalArg, StringRef(argv[i]), i);
11011101 continue; // We are done!
11021102 }
11031103
11041104 if (!PositionalOpts.empty()) {
1105 PositionalVals.push_back(std::make_pair(argv[i], i));
1105 PositionalVals.push_back(std::make_pair(StringRef(argv[i]), i));
11061106
11071107 // All of the positional arguments have been fulfulled, give the rest to
11081108 // the consume after option... if it's specified...
11091109 //
11101110 if (PositionalVals.size() >= NumPositionalRequired && ConsumeAfterOpt) {
11111111 for (++i; i < argc; ++i)
1112 PositionalVals.push_back(std::make_pair(argv[i], i));
1112 PositionalVals.push_back(std::make_pair(StringRef(argv[i]), i));
11131113 break; // Handle outside of the argument processing loop...
11141114 }
11151115
11251125 // If there is a positional argument eating options, check to see if this
11261126 // option is another positional argument. If so, treat it as an argument,
11271127 // otherwise feed it to the eating positional.
1128 ArgName = argv[i] + 1;
1128 ArgName = StringRef(argv[i] + 1);
11291129 // Eat leading dashes.
11301130 while (!ArgName.empty() && ArgName[0] == '-')
11311131 ArgName = ArgName.substr(1);
11321132
11331133 Handler = LookupOption(*ChosenSubCommand, ArgName, Value);
11341134 if (!Handler || Handler->getFormattingFlag() != cl::Positional) {
1135 ProvidePositionalOption(ActivePositionalArg, argv[i], i);
1135 ProvidePositionalOption(ActivePositionalArg, StringRef(argv[i]), i);
11361136 continue; // We are done!
11371137 }
11381138
11391139 } else { // We start with a '-', must be an argument.
1140 ArgName = argv[i] + 1;
1140 ArgName = StringRef(argv[i] + 1);
11411141 // Eat leading dashes.
11421142 while (!ArgName.empty() && ArgName[0] == '-')
11431143 ArgName = ArgName.substr(1);
11741174 for (SmallVectorImpl
11751175 E = SinkOpts.end();
11761176 I != E; ++I)
1177 (*I)->addOccurrence(i, "", argv[i]);
1177 (*I)->addOccurrence(i, "", StringRef(argv[i]));
11781178 }
11791179 continue;
11801180 }
13881388 // Return the width of the option tag for printing...
13891389 size_t basic_parser_impl::getOptionWidth(const Option &O) const {
13901390 size_t Len = O.ArgStr.size();
1391 if (const char *ValName = getValueName())
1391 auto ValName = getValueName();
1392 if (!ValName.empty())
13921393 Len += getValueStr(O, ValName).size() + 3;
13931394
13941395 return Len + 6;
14011402 size_t GlobalWidth) const {
14021403 outs() << " -" << O.ArgStr;
14031404
1404 if (const char *ValName = getValueName())
1405 auto ValName = getValueName();
1406 if (!ValName.empty())
14051407 outs() << "=<" << getValueStr(O, ValName) << '>';
14061408
14071409 printHelpStr(O.HelpStr, GlobalWidth, getOptionWidth(O));
15111513 // findOption - Return the option number corresponding to the specified
15121514 // argument string. If the option is not found, getNumOptions() is returned.
15131515 //
1514 unsigned generic_parser_base::findOption(const char *Name) {
1516 unsigned generic_parser_base::findOption(StringRef Name) {
15151517 unsigned e = getNumOptions();
15161518
15171519 for (unsigned i = 0; i != e; ++i) {
1518 if (strcmp(getOption(i), Name) == 0)
1520 if (getOption(i) == Name)
15191521 return i;
15201522 }
15211523 return e;
15261528 if (O.hasArgStr()) {
15271529 size_t Size = O.ArgStr.size() + 6;
15281530 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
1529 Size = std::max(Size, std::strlen(getOption(i)) + 8);
1531 Size = std::max(Size, getOption(i).size() + 8);
15301532 return Size;
15311533 } else {
15321534 size_t BaseSize = 0;
15331535 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
1534 BaseSize = std::max(BaseSize, std::strlen(getOption(i)) + 8);
1536 BaseSize = std::max(BaseSize, getOption(i).size() + 8);
15351537 return BaseSize;
15361538 }
15371539 }
15461548 printHelpStr(O.HelpStr, GlobalWidth, O.ArgStr.size() + 6);
15471549
15481550 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
1549 size_t NumSpaces = GlobalWidth - strlen(getOption(i)) - 8;
1551 size_t NumSpaces = GlobalWidth - getOption(i).size() - 8;
15501552 outs() << " =" << getOption(i);
15511553 outs().indent(NumSpaces) << " - " << getDescription(i) << '\n';
15521554 }
15541556 if (!O.HelpStr.empty())
15551557 outs() << " " << O.HelpStr << '\n';
15561558 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
1557 const char *Option = getOption(i);
1559 auto Option = getOption(i);
15581560 outs() << " -" << Option;
1559 printHelpStr(getDescription(i), GlobalWidth, std::strlen(Option) + 8);
1561 printHelpStr(getDescription(i), GlobalWidth, Option.size() + 8);
15601562 }
15611563 }
15621564 }
15781580 continue;
15791581
15801582 outs() << "= " << getOption(i);
1581 size_t L = std::strlen(getOption(i));
1583 size_t L = getOption(i).size();
15821584 size_t NumSpaces = MaxOptWidth > L ? MaxOptWidth - L : 0;
15831585 outs().indent(NumSpaces) << " (default: ";
15841586 for (unsigned j = 0; j != NumOpts; ++j) {
16911693 sortSubCommands(const SmallPtrSetImpl &SubMap,
16921694 SmallVectorImpl> &Subs) {
16931695 for (const auto &S : SubMap) {
1694 if (S->getName() == nullptr)
1696 if (S->getName().empty())
16951697 continue;
1696 Subs.push_back(std::make_pair(S->getName(), S));
1698 Subs.push_back(std::make_pair(S->getName().data(), S));
16971699 }
16981700 array_pod_sort(Subs.begin(), Subs.end(), SubNameCompare);
16991701 }
17161718 void printSubCommands(StrSubCommandPairVector &Subs, size_t MaxSubLen) {
17171719 for (const auto &S : Subs) {
17181720 outs() << " " << S.first;
1719 if (S.second->getDescription()) {
1721 if (!S.second->getDescription().empty()) {
17201722 outs().indent(MaxSubLen - strlen(S.first));
17211723 outs() << " - " << S.second->getDescription();
17221724 }
17441746 StrSubCommandPairVector Subs;
17451747 sortSubCommands(GlobalParser->RegisteredSubCommands, Subs);
17461748
1747 if (GlobalParser->ProgramOverview)
1749 if (!GlobalParser->ProgramOverview.empty())
17481750 outs() << "OVERVIEW: " << GlobalParser->ProgramOverview << "\n";
17491751
17501752 if (Sub == &*TopLevelSubCommand)
17511753 outs() << "USAGE: " << GlobalParser->ProgramName
17521754 << " [subcommand] [options]";
17531755 else {
1754 if (Sub->getDescription() != nullptr) {
1756 if (!Sub->getDescription().empty()) {
17551757 outs() << "SUBCOMMAND '" << Sub->getName()
17561758 << "': " << Sub->getDescription() << "\n\n";
17571759 }
18131815 // ordered before B's name. It returns a value greater equal zero otherwise.
18141816 static int OptionCategoryCompare(OptionCategory *const *A,
18151817 OptionCategory *const *B) {
1816 return strcmp((*A)->getName(), (*B)->getName());
1818 return (*A)->getName() == (*B)->getName();
18171819 }
18181820
18191821 // Make sure we inherit our base class's operator=()
18701872 outs() << (*Category)->getName() << ":\n";
18711873
18721874 // Check if description is set.
1873 if ((*Category)->getDescription() != nullptr)
1875 if (!(*Category)->getDescription().empty())
18741876 outs() << (*Category)->getDescription() << "\n\n";
18751877 else
18761878 outs() << "\n";
21312133
21322134 void LLVMParseCommandLineOptions(int argc, const char *const *argv,
21332135 const char *Overview) {
2134 llvm::cl::ParseCommandLineOptions(argc, argv, Overview, true);
2135 }
2136 llvm::cl::ParseCommandLineOptions(argc, argv, StringRef(Overview), true);
2137 }
6666 return false;
6767 }
6868
69 const char *getValueName() const override { return "IR scalar type"; }
69 StringRef getValueName() const override { return "IR scalar type"; }
7070 };
7171 }
7272
7575
7676 class StackSubCommand : public cl::SubCommand {
7777 public:
78 StackSubCommand(const char *const Name,
79 const char *const Description = nullptr)
78 StackSubCommand(StringRef Name,
79 StringRef Description = StringRef())
8080 : SubCommand(Name, Description) {}
8181
8282 StackSubCommand() : SubCommand() {}
167167 SmallVectorImpl &NewArgv,
168168 bool MarkEOLs);
169169
170 void testCommandLineTokenizer(ParserFunction *parse, const char *Input,
170 void testCommandLineTokenizer(ParserFunction *parse, StringRef Input,
171171 const char *const Output[], size_t OutputSize) {
172172 SmallVector Actual;
173173 BumpPtrAllocator A;
181181 }
182182
183183 TEST(CommandLineTest, TokenizeGNUCommandLine) {
184 const char *Input =
184 const char Input[] =
185185 "foo\\ bar \"foo bar\" \'foo bar\' 'foo\\\\bar' -DFOO=bar\\(\\) "
186186 "foo\"bar\"baz C:\\\\src\\\\foo.cpp \"C:\\src\\foo.cpp\"";
187187 const char *const Output[] = {
192192 }
193193
194194 TEST(CommandLineTest, TokenizeWindowsCommandLine) {
195 const char *Input = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
195 const char Input[] = "a\\b c\\\\d e\\\\\"f g\" h\\\"i j\\\\\\\"k \"lmn\" o pqr "
196196 "\"st \\\"u\" \\v";
197197 const char *const Output[] = { "a\\b", "c\\\\d", "e\\f g", "h\"i", "j\\\"k",
198198 "lmn", "o", "pqr", "st \"u", "\\v" };
298298 EXPECT_FALSE(SC1Opt);
299299 EXPECT_FALSE(SC2Opt);
300300 const char *args[] = {"prog", "-top-level"};
301 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
301 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
302302 EXPECT_TRUE(TopLevelOpt);
303303 EXPECT_FALSE(SC1Opt);
304304 EXPECT_FALSE(SC2Opt);
310310 EXPECT_FALSE(SC1Opt);
311311 EXPECT_FALSE(SC2Opt);
312312 const char *args2[] = {"prog", "sc1", "-sc1"};
313 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, nullptr, true));
313 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
314314 EXPECT_FALSE(TopLevelOpt);
315315 EXPECT_TRUE(SC1Opt);
316316 EXPECT_FALSE(SC2Opt);
322322 EXPECT_FALSE(SC1Opt);
323323 EXPECT_FALSE(SC2Opt);
324324 const char *args3[] = {"prog", "sc2", "-sc2"};
325 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, nullptr, true));
325 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), true));
326326 EXPECT_FALSE(TopLevelOpt);
327327 EXPECT_FALSE(SC1Opt);
328328 EXPECT_TRUE(SC2Opt);
338338 StackOption SC2Opt("sc2", cl::sub(SC2), cl::init(false));
339339
340340 const char *args[] = {"prog", "sc1", "-sc2"};
341 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, nullptr, true));
341 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), true));
342342 }
343343
344344 TEST(CommandLineTest, AddToAllSubCommands) {
354354 const char *args3[] = {"prog", "sc2", "-everywhere"};
355355
356356 EXPECT_FALSE(AllOpt);
357 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
357 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
358358 EXPECT_TRUE(AllOpt);
359359
360360 AllOpt = false;
361361
362362 cl::ResetAllOptionOccurrences();
363363 EXPECT_FALSE(AllOpt);
364 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, nullptr, true));
364 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
365365 EXPECT_TRUE(AllOpt);
366366
367367 AllOpt = false;
368368
369369 cl::ResetAllOptionOccurrences();
370370 EXPECT_FALSE(AllOpt);
371 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, nullptr, true));
371 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args3, StringRef(), true));
372372 EXPECT_TRUE(AllOpt);
373373 }
374374
381381 const char *args[] = {"prog", "-top-level"};
382382
383383 EXPECT_FALSE(TopLevelOpt);
384 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
384 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
385385 EXPECT_TRUE(TopLevelOpt);
386386
387387 TopLevelOpt = false;
388388
389389 cl::ResetAllOptionOccurrences();
390390 EXPECT_FALSE(TopLevelOpt);
391 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
391 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
392392 EXPECT_TRUE(TopLevelOpt);
393393 }
394394
402402 const char *args[] = {"prog", "sc", "-remove-option"};
403403
404404 EXPECT_FALSE(RemoveOption);
405 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, nullptr, true));
405 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args, StringRef(), true));
406406 EXPECT_TRUE(RemoveOption);
407407
408408 RemoveOption.removeArgument();
409409
410410 cl::ResetAllOptionOccurrences();
411 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, nullptr, true));
411 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args, StringRef(), true));
412412 }
413413
414414 TEST(CommandLineTest, RemoveFromTopLevelSubCommand) {
422422 const char *args[] = {"prog", "-top-level-remove"};
423423
424424 EXPECT_FALSE(TopLevelRemove);
425 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, nullptr, true));
425 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
426426 EXPECT_TRUE(TopLevelRemove);
427427
428428 TopLevelRemove.removeArgument();
429429
430430 cl::ResetAllOptionOccurrences();
431 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args, nullptr, true));
431 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args, StringRef(), true));
432432 }
433433
434434 TEST(CommandLineTest, RemoveFromAllSubCommands) {
447447
448448 // It should work for all subcommands including the top-level.
449449 EXPECT_FALSE(RemoveOption);
450 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, nullptr, true));
450 EXPECT_TRUE(cl::ParseCommandLineOptions(2, args0, StringRef(), true));
451451 EXPECT_TRUE(RemoveOption);
452452
453453 RemoveOption = false;
454454
455455 cl::ResetAllOptionOccurrences();
456456 EXPECT_FALSE(RemoveOption);
457 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args1, nullptr, true));
457 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args1, StringRef(), true));
458458 EXPECT_TRUE(RemoveOption);
459459
460460 RemoveOption = false;
461461
462462 cl::ResetAllOptionOccurrences();
463463 EXPECT_FALSE(RemoveOption);
464 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, nullptr, true));
464 EXPECT_TRUE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
465465 EXPECT_TRUE(RemoveOption);
466466
467467 RemoveOption.removeArgument();
468468
469469 // It should not work for any subcommands including the top-level.
470470 cl::ResetAllOptionOccurrences();
471 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args0, nullptr, true));
472 cl::ResetAllOptionOccurrences();
473 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args1, nullptr, true));
474 cl::ResetAllOptionOccurrences();
475 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, nullptr, true));
471 EXPECT_FALSE(cl::ParseCommandLineOptions(2, args0, StringRef(), true));
472 cl::ResetAllOptionOccurrences();
473 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args1, StringRef(), true));
474 cl::ResetAllOptionOccurrences();
475 EXPECT_FALSE(cl::ParseCommandLineOptions(3, args2, StringRef(), true));
476476 }
477477
478478 } // anonymous namespace