Index: include/llvm/IR/LegacyPassNameParser.h =================================================================== --- include/llvm/IR/LegacyPassNameParser.h +++ include/llvm/IR/LegacyPassNameParser.h @@ -89,7 +89,7 @@ // ValLessThan - Provide a sorting comparator for Values elements... static int ValLessThan(const PassNameParser::OptionInfo *VT1, const PassNameParser::OptionInfo *VT2) { - return std::strcmp(VT1->Name, VT2->Name); + return VT1->Name.compare(VT2->Name); } }; Index: include/llvm/Support/CommandLine.h =================================================================== --- include/llvm/Support/CommandLine.h +++ include/llvm/Support/CommandLine.h @@ -44,14 +44,14 @@ // ParseCommandLineOptions - Command line option processing entry point. // void ParseCommandLineOptions(int argc, const char *const *argv, - const char *Overview = nullptr); + StringRef Overview = StringRef()); //===----------------------------------------------------------------------===// // ParseEnvironmentOptions - Environment variable option processing alternate // entry point. // -void ParseEnvironmentOptions(const char *progName, const char *envvar, - const char *Overview = nullptr); +void ParseEnvironmentOptions(StringRef progName, StringRef envvar, + StringRef Overview = StringRef()); ///===---------------------------------------------------------------------===// /// SetVersionPrinter - Override the default (LLVM specific) version printer @@ -85,7 +85,7 @@ /// /// Literal options are used by some parsers to register special option values. /// This is how the PassNameParser registers pass names for opt. -void AddLiteralOption(Option &O, const char *Name); +void AddLiteralOption(Option &O, StringRef Name); //===----------------------------------------------------------------------===// // Flags permitted to be passed to command line arguments @@ -153,18 +153,17 @@ // class OptionCategory { private: - const char *const Name; - const char *const Description; + StringRef Name; + StringRef Description; void registerCategory(); public: - OptionCategory(const char *const Name, - const char *const Description = nullptr) + OptionCategory(StringRef Name, StringRef Description = StringRef()) : Name(Name), Description(Description) { registerCategory(); } - const char *getName() const { return Name; } - const char *getDescription() const { return Description; } + StringRef getName() const { return Name; } + StringRef getDescription() const { return Description; } }; // The general Option Category (used as default category). @@ -184,7 +183,7 @@ virtual bool handleOccurrence(unsigned pos, StringRef ArgName, StringRef Arg) = 0; - virtual enum ValueExpected getValueExpectedFlagDefault() const { + virtual ValueExpected getValueExpectedFlagDefault() const { return ValueOptional; } @@ -204,53 +203,56 @@ unsigned Position; // Position of last occurrence of the option unsigned AdditionalVals; // Greater than 0 for multi-valued option. + StringRef ArgStr; // The argument string itself (ex: "help", "o") + StringRef Description; // The descriptive text message for -help + StringRef ValueStr; // String describing what the value of this option is + public: - const char *ArgStr; // The argument string itself (ex: "help", "o") - const char *HelpStr; // The descriptive text message for -help - const char *ValueStr; // String describing what the value of this option is OptionCategory *Category; // The Category this option belongs to bool FullyInitialized; // Has addArguemnt been called? - inline enum NumOccurrencesFlag getNumOccurrencesFlag() const { - return (enum NumOccurrencesFlag)Occurrences; - } - inline enum ValueExpected getValueExpectedFlag() const { - return Value ? ((enum ValueExpected)Value) : getValueExpectedFlagDefault(); + NumOccurrencesFlag getNumOccurrencesFlag() const { + return (NumOccurrencesFlag)Occurrences; } - inline enum OptionHidden getOptionHiddenFlag() const { - return (enum OptionHidden)HiddenFlag; + ValueExpected getValueExpectedFlag() const { + return Value ? ((ValueExpected)Value) : getValueExpectedFlagDefault(); } - inline enum FormattingFlags getFormattingFlag() const { - return (enum FormattingFlags)Formatting; + OptionHidden getOptionHiddenFlag() const { return (OptionHidden)HiddenFlag; } + FormattingFlags getFormattingFlag() const { + return (FormattingFlags)Formatting; } - inline unsigned getMiscFlags() const { return Misc; } - inline unsigned getPosition() const { return Position; } - inline unsigned getNumAdditionalVals() const { return AdditionalVals; } + unsigned getMiscFlags() const { return Misc; } + unsigned getPosition() const { return Position; } + unsigned getNumAdditionalVals() const { return AdditionalVals; } // hasArgStr - Return true if the argstr != "" - bool hasArgStr() const { return ArgStr[0] != 0; } + bool hasArgStr() const { return ArgStr.empty() == false; } + bool hasDescriptionStr() const { return Description.empty() == false; } + bool hasValueStr() const { return ValueStr.empty() == false; } //-------------------------------------------------------------------------=== // Accessor functions set by OptionModifiers // - void setArgStr(const char *S); - void setDescription(const char *S) { HelpStr = S; } - void setValueStr(const char *S) { ValueStr = S; } - void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; } - void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; } - void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; } - void setFormattingFlag(enum FormattingFlags V) { Formatting = V; } - void setMiscFlag(enum MiscFlags M) { Misc |= M; } + StringRef getArgStr() const { return ArgStr; } + StringRef getDescription() const { return Description; } + StringRef getValueStr() const { return ValueStr; } + void setArgStr(StringRef S); + void setDescription(StringRef S) { Description = S; } + void setValueStr(StringRef S) { ValueStr = S; } + void setNumOccurrencesFlag(NumOccurrencesFlag Val) { Occurrences = Val; } + void setValueExpectedFlag(ValueExpected Val) { Value = Val; } + void setHiddenFlag(OptionHidden Val) { HiddenFlag = Val; } + void setFormattingFlag(FormattingFlags V) { Formatting = V; } + void setMiscFlag(MiscFlags M) { Misc |= M; } void setPosition(unsigned pos) { Position = pos; } void setCategory(OptionCategory &C) { Category = &C; } protected: - explicit Option(enum NumOccurrencesFlag OccurrencesFlag, - enum OptionHidden Hidden) + explicit Option(NumOccurrencesFlag OccurrencesFlag, OptionHidden Hidden) : NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0), HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0), Position(0), - AdditionalVals(0), ArgStr(""), HelpStr(""), ValueStr(""), - Category(&GeneralCategory), FullyInitialized(false) {} + AdditionalVals(0), Category(&GeneralCategory), FullyInitialized(false) { + } inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; } @@ -275,7 +277,7 @@ virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0; - virtual void getExtraOptionNames(SmallVectorImpl &) {} + virtual void getExtraOptionNames(SmallVectorImpl &) {} // addOccurrence - Wrapper around handleOccurrence that enforces Flags. // @@ -297,16 +299,16 @@ // desc - Modifier to set the description shown in the -help output... struct desc { - const char *Desc; - desc(const char *Str) : Desc(Str) {} + StringRef Desc; + desc(StringRef Str) : Desc(Str) {} void apply(Option &O) const { O.setDescription(Desc); } }; // value_desc - Modifier to set the value description shown in the -help // output... struct value_desc { - const char *Desc; - value_desc(const char *Str) : Desc(Str) {} + StringRef Desc; + value_desc(StringRef Str) : Desc(Str) {} void apply(Option &O) const { O.setValueStr(Desc); } }; @@ -505,11 +507,11 @@ // Use a vector instead of a map, because the lists should be short, // the overhead is less, and most importantly, it keeps them in the order // inserted so we can print our option out nicely. - SmallVector>, 4> Values; + SmallVector>, 4> Values; void processValues(va_list Vals); public: - ValuesClass(const char *EnumName, DataType Val, const char *Desc, + ValuesClass(StringRef EnumName, DataType Val, StringRef Desc, va_list ValueArgs) { // Insert the first value, which is required. Values.push_back(std::make_pair(EnumName, std::make_pair(Val, Desc))); @@ -517,22 +519,21 @@ // Process the varargs portion of the values... while (const char *enumName = va_arg(ValueArgs, const char *)) { DataType EnumVal = static_cast(va_arg(ValueArgs, int)); - const char *EnumDesc = va_arg(ValueArgs, const char *); + StringRef EnumDesc = va_arg(ValueArgs, const char *); Values.push_back(std::make_pair(enumName, // Add value to value map std::make_pair(EnumVal, EnumDesc))); } } template void apply(Opt &O) const { - for (size_t i = 0, e = Values.size(); i != e; ++i) - O.getParser().addLiteralOption(Values[i].first, Values[i].second.first, - Values[i].second.second); + for (const auto &I : Values) + O.getParser().addLiteralOption(I.first, I.second.first, I.second.second); } }; template ValuesClass LLVM_END_WITH_NULL -values(const char *Arg, DataType Val, const char *Desc, ...) { +values(StringRef Arg, DataType Val, const char *Desc, ...) { va_list ValueArgs; va_start(ValueArgs, Desc); ValuesClass Vals(Arg, Val, Desc, ValueArgs); @@ -556,10 +557,10 @@ protected: class GenericOptionInfo { public: - GenericOptionInfo(const char *name, const char *helpStr) + GenericOptionInfo(StringRef name, StringRef helpStr) : Name(name), HelpStr(helpStr) {} - const char *Name; - const char *HelpStr; + StringRef Name; + StringRef HelpStr; }; public: @@ -573,10 +574,10 @@ virtual unsigned getNumOptions() const = 0; // getOption - Return option name N. - virtual const char *getOption(unsigned N) const = 0; + virtual StringRef getOption(unsigned N) const = 0; // getDescription - Return description N - virtual const char *getDescription(unsigned N) const = 0; + virtual StringRef getDescription(unsigned N) const = 0; // Return the width of the option tag for printing... virtual size_t getOptionWidth(const Option &O) const; @@ -605,7 +606,7 @@ void initialize() {} - void getExtraOptionNames(SmallVectorImpl &OptionNames) { + void getExtraOptionNames(SmallVectorImpl &OptionNames) { // If there has been no argstr specified, that means that we need to add an // argument for every possible option. This ensures that our options are // vectored to us. @@ -614,7 +615,7 @@ OptionNames.push_back(getOption(i)); } - enum ValueExpected getValueExpectedFlagDefault() const { + ValueExpected getValueExpectedFlagDefault() const { // If there is an ArgStr specified, then we are of the form: // // -opt=O2 or -opt O2 or -optO2 @@ -635,7 +636,7 @@ // findOption - Return the option number corresponding to the specified // argument string. If the option is not found, getNumOptions() is returned. // - unsigned findOption(const char *Name); + unsigned findOption(StringRef Name); protected: Option &Owner; @@ -651,7 +652,7 @@ protected: class OptionInfo : public GenericOptionInfo { public: - OptionInfo(const char *name, DataType v, const char *helpStr) + OptionInfo(StringRef name, DataType v, StringRef helpStr) : GenericOptionInfo(name, helpStr), V(v) {} OptionValue V; }; @@ -663,8 +664,8 @@ // Implement virtual functions needed by generic_parser_base unsigned getNumOptions() const override { return unsigned(Values.size()); } - const char *getOption(unsigned N) const override { return Values[N].Name; } - const char *getDescription(unsigned N) const override { + StringRef getOption(unsigned N) const override { return Values[N].Name; } + StringRef getDescription(unsigned N) const override { return Values[N].HelpStr; } @@ -681,9 +682,9 @@ else ArgVal = ArgName; - for (size_t i = 0, e = Values.size(); i != e; ++i) - if (Values[i].Name == ArgVal) { - V = Values[i].V.getValue(); + for (const auto &I : Values) + if (I.Name == ArgVal) { + V = I.V.getValue(); return false; } @@ -693,7 +694,7 @@ /// addLiteralOption - Add an entry to the mapping table. /// template - void addLiteralOption(const char *Name, const DT &V, const char *HelpStr) { + void addLiteralOption(StringRef Name, const DT &V, StringRef HelpStr) { assert(findOption(Name) == Values.size() && "Option already exists!"); OptionInfo X(Name, static_cast(V), HelpStr); Values.push_back(X); @@ -702,7 +703,7 @@ /// removeLiteralOption - Remove the specified option. /// - void removeLiteralOption(const char *Name) { + void removeLiteralOption(StringRef Name) { unsigned N = findOption(Name); assert(N != Values.size() && "Option not found!"); Values.erase(Values.begin() + N); @@ -716,12 +717,9 @@ public: basic_parser_impl(Option &O) {} + ValueExpected getValueExpectedFlagDefault() const { return ValueRequired; } - enum ValueExpected getValueExpectedFlagDefault() const { - return ValueRequired; - } - - void getExtraOptionNames(SmallVectorImpl &) {} + void getExtraOptionNames(SmallVectorImpl &) {} void initialize() {} @@ -738,7 +736,7 @@ void printOptionNoValue(const Option &O, size_t GlobalWidth) const; // getValueName - Overload in subclass to provide a better default value. - virtual const char *getValueName() const { return "value"; } + virtual StringRef getValueName() const { return "value"; } // An out-of-line virtual method to provide a 'home' for this class. virtual void anchor(); @@ -775,12 +773,10 @@ void initialize() {} - enum ValueExpected getValueExpectedFlagDefault() const { - return ValueOptional; - } + ValueExpected getValueExpectedFlagDefault() const { return ValueOptional; } // getValueName - Do not print = at all. - const char *getValueName() const override { return nullptr; } + StringRef getValueName() const override { return StringRef(); } void printOptionDiff(const Option &O, bool V, OptVal Default, size_t GlobalWidth) const; @@ -801,12 +797,10 @@ // parse - Return true on error. bool parse(Option &O, StringRef ArgName, StringRef Arg, boolOrDefault &Val); - enum ValueExpected getValueExpectedFlagDefault() const { - return ValueOptional; - } + ValueExpected getValueExpectedFlagDefault() const { return ValueOptional; } // getValueName - Do not print = at all. - const char *getValueName() const override { return nullptr; } + StringRef getValueName() const override { return StringRef(); } void printOptionDiff(const Option &O, boolOrDefault V, OptVal Default, size_t GlobalWidth) const; @@ -828,7 +822,7 @@ bool parse(Option &O, StringRef ArgName, StringRef Arg, int &Val); // getValueName - Overload in subclass to provide a better default value. - const char *getValueName() const override { return "int"; } + StringRef getValueName() const override { return "int"; } void printOptionDiff(const Option &O, int V, OptVal Default, size_t GlobalWidth) const; @@ -850,7 +844,7 @@ bool parse(Option &O, StringRef ArgName, StringRef Arg, unsigned &Val); // getValueName - Overload in subclass to provide a better default value. - const char *getValueName() const override { return "uint"; } + StringRef getValueName() const override { return "uint"; } void printOptionDiff(const Option &O, unsigned V, OptVal Default, size_t GlobalWidth) const; @@ -875,7 +869,7 @@ unsigned long long &Val); // getValueName - Overload in subclass to provide a better default value. - const char *getValueName() const override { return "uint"; } + StringRef getValueName() const override { return "uint"; } void printOptionDiff(const Option &O, unsigned long long V, OptVal Default, size_t GlobalWidth) const; @@ -897,7 +891,7 @@ bool parse(Option &O, StringRef ArgName, StringRef Arg, double &Val); // getValueName - Overload in subclass to provide a better default value. - const char *getValueName() const override { return "number"; } + StringRef getValueName() const override { return "number"; } void printOptionDiff(const Option &O, double V, OptVal Default, size_t GlobalWidth) const; @@ -919,7 +913,7 @@ bool parse(Option &O, StringRef ArgName, StringRef Arg, float &Val); // getValueName - Overload in subclass to provide a better default value. - const char *getValueName() const override { return "number"; } + StringRef getValueName() const override { return "number"; } void printOptionDiff(const Option &O, float V, OptVal Default, size_t GlobalWidth) const; @@ -944,7 +938,7 @@ } // getValueName - Overload in subclass to provide a better default value. - const char *getValueName() const override { return "string"; } + StringRef getValueName() const override { return "string"; } void printOptionDiff(const Option &O, StringRef V, OptVal Default, size_t GlobalWidth) const; @@ -969,7 +963,7 @@ } // getValueName - Overload in subclass to provide a better default value. - const char *getValueName() const override { return "char"; } + StringRef getValueName() const override { return "char"; } void printOptionDiff(const Option &O, char V, OptVal Default, size_t GlobalWidth) const; @@ -1202,11 +1196,10 @@ return false; } - enum ValueExpected getValueExpectedFlagDefault() const override { + ValueExpected getValueExpectedFlagDefault() const override { return Parser.getValueExpectedFlagDefault(); } - void - getExtraOptionNames(SmallVectorImpl &OptionNames) override { + void getExtraOptionNames(SmallVectorImpl &OptionNames) override { return Parser.getExtraOptionNames(OptionNames); } @@ -1364,11 +1357,10 @@ std::vector Positions; ParserClass Parser; - enum ValueExpected getValueExpectedFlagDefault() const override { + ValueExpected getValueExpectedFlagDefault() const override { return Parser.getValueExpectedFlagDefault(); } - void - getExtraOptionNames(SmallVectorImpl &OptionNames) override { + void getExtraOptionNames(SmallVectorImpl &OptionNames) override { return Parser.getExtraOptionNames(OptionNames); } @@ -1504,11 +1496,10 @@ std::vector Positions; ParserClass Parser; - enum ValueExpected getValueExpectedFlagDefault() const override { + ValueExpected getValueExpectedFlagDefault() const override { return Parser.getValueExpectedFlagDefault(); } - void - getExtraOptionNames(SmallVectorImpl &OptionNames) override { + void getExtraOptionNames(SmallVectorImpl &OptionNames) override { return Parser.getExtraOptionNames(OptionNames); } @@ -1626,8 +1617,8 @@ // printed to stderr at the end of the regular help, just before // exit is called. struct extrahelp { - const char *morehelp; - explicit extrahelp(const char *help); + StringRef morehelp; + explicit extrahelp(StringRef help); }; void PrintVersionMessage(); Index: lib/Support/CommandLine.cpp =================================================================== --- lib/Support/CommandLine.cpp +++ lib/Support/CommandLine.cpp @@ -89,10 +89,10 @@ // Globals for name and overview of program. Program name is not a string to // avoid static ctor/dtor issues. std::string ProgramName; - const char *ProgramOverview; + StringRef ProgramOverview; // This collects additional help to be printed. - std::vector MoreHelp; + std::vector MoreHelp; SmallVector