llvm.org GIT mirror llvm / 034f865
StringRef-ify some Option APIs Patch by Eugene Kosov! Differential Revision: http://reviews.llvm.org/D14711 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253360 91177308-0d34-0410-b5e6-96231b3b80d8 David Blaikie 4 years ago
4 changed file(s) with 45 addition(s) and 49 deletion(s). Raw diff Collapse all Expand all
204204 unsigned AdditionalVals; // Greater than 0 for multi-valued option.
205205
206206 public:
207 const char *ArgStr; // The argument string itself (ex: "help", "o")
208 const char *HelpStr; // The descriptive text message for -help
209 const char *ValueStr; // String describing what the value of this option is
207 StringRef ArgStr; // The argument string itself (ex: "help", "o")
208 StringRef HelpStr; // The descriptive text message for -help
209 StringRef ValueStr; // String describing what the value of this option is
210210 OptionCategory *Category; // The Category this option belongs to
211211 bool FullyInitialized; // Has addArguemnt been called?
212212
227227 inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
228228
229229 // hasArgStr - Return true if the argstr != ""
230 bool hasArgStr() const { return ArgStr[0] != 0; }
230 bool hasArgStr() const { return !ArgStr.empty(); }
231231
232232 //-------------------------------------------------------------------------===
233233 // Accessor functions set by OptionModifiers
234234 //
235 void setArgStr(const char *S);
236 void setDescription(const char *S) { HelpStr = S; }
237 void setValueStr(const char *S) { ValueStr = S; }
235 void setArgStr(StringRef S);
236 void setDescription(StringRef S) { HelpStr = S; }
237 void setValueStr(StringRef S) { ValueStr = S; }
238238 void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; }
239239 void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; }
240240 void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; }
274274
275275 virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
276276
277 virtual void getExtraOptionNames(SmallVectorImpl<const char *> &) {}
277 virtual void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
278278
279279 // addOccurrence - Wrapper around handleOccurrence that enforces Flags.
280280 //
604604
605605 void initialize() {}
606606
607 void getExtraOptionNames(SmallVectorImpl<const char *> &OptionNames) {
607 void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) {
608608 // If there has been no argstr specified, that means that we need to add an
609609 // argument for every possible option. This ensures that our options are
610610 // vectored to us.
720720 return ValueRequired;
721721 }
722722
723 void getExtraOptionNames(SmallVectorImpl<const char *> &) {}
723 void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
724724
725725 void initialize() {}
726726
12041204 enum ValueExpected getValueExpectedFlagDefault() const override {
12051205 return Parser.getValueExpectedFlagDefault();
12061206 }
1207 void
1208 getExtraOptionNames(SmallVectorImpl> &OptionNames) override {
1207 void getExtraOptionNames(SmallVectorImpl> &OptionNames) override {
12091208 return Parser.getExtraOptionNames(OptionNames);
12101209 }
12111210
13661365 enum ValueExpected getValueExpectedFlagDefault() const override {
13671366 return Parser.getValueExpectedFlagDefault();
13681367 }
1369 void
1370 getExtraOptionNames(SmallVectorImpl> &OptionNames) override {
1368 void getExtraOptionNames(SmallVectorImpl> &OptionNames) override {
13711369 return Parser.getExtraOptionNames(OptionNames);
13721370 }
13731371
15061504 enum ValueExpected getValueExpectedFlagDefault() const override {
15071505 return Parser.getValueExpectedFlagDefault();
15081506 }
1509 void
1510 getExtraOptionNames(SmallVectorImpl> &OptionNames) override {
1507 void getExtraOptionNames(SmallVectorImpl> &OptionNames) override {
15111508 return Parser.getExtraOptionNames(OptionNames);
15121509 }
15131510
119119
120120 void addOption(Option *O) {
121121 bool HadErrors = false;
122 if (O->ArgStr[0]) {
122 if (O->hasArgStr()) {
123123 // Add argument to the argument map!
124124 if (!OptionsMap.insert(std::make_pair(O->ArgStr, O)).second) {
125125 errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
150150 }
151151
152152 void removeOption(Option *O) {
153 SmallVector<const char *, 16> OptionNames;
153 SmallVector<StringRef, 16> OptionNames;
154154 O->getExtraOptionNames(OptionNames);
155 if (O->ArgStr[0])
155 if (O->hasArgStr())
156156 OptionNames.push_back(O->ArgStr);
157157 for (auto Name : OptionNames)
158 OptionsMap.erase(StringRef(Name));
158 OptionsMap.erase(Name);
159159
160160 if (O->getFormattingFlag() == cl::Positional)
161161 for (auto Opt = PositionalOpts.begin(); Opt != PositionalOpts.end();
181181 nullptr != ConsumeAfterOpt);
182182 }
183183
184 void updateArgStr(Option *O, const char *NewName) {
184 void updateArgStr(Option *O, StringRef NewName) {
185185 if (!OptionsMap.insert(std::make_pair(NewName, O)).second) {
186186 errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
187187 << "' registered more than once!\n";
188188 report_fatal_error("inconsistency in registered CommandLine options");
189189 }
190 OptionsMap.erase(StringRef(O->ArgStr));
190 OptionsMap.erase(O->ArgStr);
191191 }
192192
193193 void printOptionValues();
226226
227227 void Option::removeArgument() { GlobalParser->removeOption(this); }
228228
229 void Option::setArgStr(const char *S) {
229 void Option::setArgStr(StringRef S) {
230230 if (FullyInitialized)
231231 GlobalParser->updateArgStr(this, S);
232232 ArgStr = S;
295295 ie = OptionsMap.end();
296296 it != ie; ++it) {
297297 Option *O = it->second;
298 SmallVector<const char *, 16> OptionNames;
298 SmallVector<StringRef, 16> OptionNames;
299299 O->getExtraOptionNames(OptionNames);
300 if (O->ArgStr[0])
300 if (O->hasArgStr())
301301 OptionNames.push_back(O->ArgStr);
302302
303303 bool PermitValue = O->getValueExpectedFlag() != cl::ValueDisallowed;
304304 StringRef Flag = PermitValue ? LHS : Arg;
305 for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
306 StringRef Name = OptionNames[i];
305 for (auto Name : OptionNames) {
307306 unsigned Distance = StringRef(Name).edit_distance(
308307 Flag, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
309308 if (!Best || Distance < BestDistance) {
310309 Best = O;
311310 BestDistance = Distance;
312311 if (RHS.empty() || !PermitValue)
313 NearestString = OptionNames[i];
312 NearestString = Name;
314313 else
315 NearestString = (Twine(OptionNames[i]) + "=" + RHS).str();
314 NearestString = (Twine(Name) + "=" + RHS).str();
316315 }
317316 }
318317 }
858857 "error - this positional option will never be matched, "
859858 "because it does not Require a value, and a "
860859 "cl::ConsumeAfter option is active!");
861 } else if (UnboundedFound && !Opt->ArgStr[0]) {
860 } else if (UnboundedFound && !Opt->hasArgStr()) {
862861 // This option does not "require" a value... Make sure this option is
863862 // not specified after an option that eats all extra arguments, or this
864863 // one will never get any!
11431142 // getValueStr - Get the value description string, using "DefaultMsg" if nothing
11441143 // has been specified yet.
11451144 //
1146 static const char *getValueStr(const Option &O, const char *DefaultMsg) {
1147 if (O.ValueStr[0] == 0)
1145 static StringRef getValueStr(const Option &O, StringRef DefaultMsg) {
1146 if (O.ValueStr.empty())
11481147 return DefaultMsg;
11491148 return O.ValueStr;
11501149 }
11541153 //
11551154
11561155 // Return the width of the option tag for printing...
1157 size_t alias::getOptionWidth() const { return std::strlen(ArgStr) + 6; }
1156 size_t alias::getOptionWidth() const { return ArgStr.size() + 6; }
11581157
11591158 static void printHelpStr(StringRef HelpStr, size_t Indent,
11601159 size_t FirstLineIndentedBy) {
11691168 // Print out the option for the alias.
11701169 void alias::printOptionInfo(size_t GlobalWidth) const {
11711170 outs() << " -" << ArgStr;
1172 printHelpStr(HelpStr, GlobalWidth, std::strlen(ArgStr) + 6);
1171 printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
11731172 }
11741173
11751174 //===----------------------------------------------------------------------===//
11811180
11821181 // Return the width of the option tag for printing...
11831182 size_t basic_parser_impl::getOptionWidth(const Option &O) const {
1184 size_t Len = std::strlen(O.ArgStr);
1183 size_t Len = O.ArgStr.size();
11851184 if (const char *ValName = getValueName())
1186 Len += std::strlen(getValueStr(O, ValName)) + 3;
1185 Len += getValueStr(O, ValName).size() + 3;
11871186
11881187 return Len + 6;
11891188 }
12041203 void basic_parser_impl::printOptionName(const Option &O,
12051204 size_t GlobalWidth) const {
12061205 outs() << " -" << O.ArgStr;
1207 outs().indent(GlobalWidth - std::strlen(O.ArgStr));
1206 outs().indent(GlobalWidth - O.ArgStr.size());
12081207 }
12091208
12101209 // parser implementation
13181317 // Return the width of the option tag for printing...
13191318 size_t generic_parser_base::getOptionWidth(const Option &O) const {
13201319 if (O.hasArgStr()) {
1321 size_t Size = std::strlen(O.ArgStr) + 6;
1320 size_t Size = O.ArgStr.size() + 6;
13221321 for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
13231322 Size = std::max(Size, std::strlen(getOption(i)) + 8);
13241323 return Size;
13371336 size_t GlobalWidth) const {
13381337 if (O.hasArgStr()) {
13391338 outs() << " -" << O.ArgStr;
1340 printHelpStr(O.HelpStr, GlobalWidth, std::strlen(O.ArgStr) + 6);
1339 printHelpStr(O.HelpStr, GlobalWidth, O.ArgStr.size() + 6);
13411340
13421341 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
13431342 size_t NumSpaces = GlobalWidth - strlen(getOption(i)) - 8;
13451344 outs().indent(NumSpaces) << " - " << getDescription(i) << '\n';
13461345 }
13471346 } else {
1348 if (O.HelpStr[0])
1347 if (!O.HelpStr.empty())
13491348 outs() << " " << O.HelpStr << '\n';
13501349 for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
13511350 const char *Option = getOption(i);
13641363 const Option &O, const GenericOptionValue &Value,
13651364 const GenericOptionValue &Default, size_t GlobalWidth) const {
13661365 outs() << " -" << O.ArgStr;
1367 outs().indent(GlobalWidth - std::strlen(O.ArgStr));
1366 outs().indent(GlobalWidth - O.ArgStr.size());
13681367
13691368 unsigned NumOpts = getNumOptions();
13701369 for (unsigned i = 0; i != NumOpts; ++i) {
15071506 outs() << "USAGE: " << GlobalParser->ProgramName << " [options]";
15081507
15091508 for (auto Opt : GlobalParser->PositionalOpts) {
1510 if (Opt->ArgStr[0])
1509 if (Opt->hasArgStr())
15111510 outs() << " --" << Opt->ArgStr;
15121511 outs() << " " << Opt->HelpStr;
15131512 }
9393 "Failed to modify option's option category.";
9494
9595 Retrieved->setDescription(Description);
96 ASSERT_STREQ(Retrieved->HelpStr, Description) <<
97 "Changing option description failed.";
96 ASSERT_STREQ(Retrieved->HelpStr.data(), Description)
97 << "Changing option description failed.";
9898
9999 Retrieved->setArgStr(ArgString);
100 ASSERT_STREQ(ArgString, Retrieved->ArgStr) <<
101 "Failed to modify option's Argument string.";
100 ASSERT_STREQ(ArgString, Retrieved->ArgStr.data())
101 << "Failed to modify option's Argument string.";
102102
103103 Retrieved->setValueStr(ValueString);
104 ASSERT_STREQ(Retrieved->ValueStr, ValueString) <<
105 "Failed to modify option's Value string.";
104 ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString)
105 << "Failed to modify option's Value string.";
106106
107107 Retrieved->setHiddenFlag(cl::Hidden);
108108 ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
856856 for (const auto &PatternString : ImplicitCheckNot) {
857857 // Create a buffer with fake command line content in order to display the
858858 // command line option responsible for the specific implicit CHECK-NOT.
859 std::string Prefix = std::string("-") + ImplicitCheckNot.ArgStr + "='";
859 std::string Prefix = (Twine("-") + ImplicitCheckNot.ArgStr + "='").str();
860860 std::string Suffix = "'";
861861 std::unique_ptr CmdLine = MemoryBuffer::getMemBufferCopy(
862862 Prefix + PatternString + Suffix, "command line");