Skip to content

Commit 4c4a94a

Browse files
dwblaikiepow2clk
authored andcommitted
StringRef-ify some Option APIs
Patch by Eugene Kosov! Differential Revision: http://reviews.llvm.org/D14711 llvm-svn: 253360 (cherry picked from LLVM commit ff43d69ddfa9c3422639ed6112a5a947c553f9d3)
1 parent 4f398bf commit 4c4a94a

4 files changed

Lines changed: 42 additions & 46 deletions

File tree

include/llvm/Support/CommandLine.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ class Option {
206206
unsigned AdditionalVals; // Greater than 0 for multi-valued option.
207207

208208
public:
209-
const char *ArgStr; // The argument string itself (ex: "help", "o")
210-
const char *HelpStr; // The descriptive text message for -help
211-
const char *ValueStr; // String describing what the value of this option is
209+
StringRef ArgStr; // The argument string itself (ex: "help", "o")
210+
StringRef HelpStr; // The descriptive text message for -help
211+
StringRef ValueStr; // String describing what the value of this option is
212212
OptionCategory *Category; // The Category this option belongs to
213213
bool FullyInitialized; // Has addArguemnt been called?
214214

@@ -229,14 +229,14 @@ class Option {
229229
inline unsigned getNumAdditionalVals() const { return AdditionalVals; }
230230

231231
// hasArgStr - Return true if the argstr != ""
232-
bool hasArgStr() const { return ArgStr[0] != 0; }
232+
bool hasArgStr() const { return !ArgStr.empty(); }
233233

234234
//-------------------------------------------------------------------------===
235235
// Accessor functions set by OptionModifiers
236236
//
237-
void setArgStr(const char *S);
238-
void setDescription(const char *S) { HelpStr = S; }
239-
void setValueStr(const char *S) { ValueStr = S; }
237+
void setArgStr(StringRef S);
238+
void setDescription(StringRef S) { HelpStr = S; }
239+
void setValueStr(StringRef S) { ValueStr = S; }
240240
void setNumOccurrencesFlag(enum NumOccurrencesFlag Val) { Occurrences = Val; }
241241
void setValueExpectedFlag(enum ValueExpected Val) { Value = Val; }
242242
void setHiddenFlag(enum OptionHidden Val) { HiddenFlag = Val; }
@@ -276,7 +276,7 @@ class Option {
276276

277277
virtual void printOptionValue(size_t GlobalWidth, bool Force) const = 0;
278278

279-
virtual void getExtraOptionNames(SmallVectorImpl<const char *> &) {}
279+
virtual void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
280280

281281
// addOccurrence - Wrapper around handleOccurrence that enforces Flags.
282282
//
@@ -606,7 +606,7 @@ class generic_parser_base {
606606

607607
void initialize() {}
608608

609-
void getExtraOptionNames(SmallVectorImpl<const char *> &OptionNames) {
609+
void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) {
610610
// If there has been no argstr specified, that means that we need to add an
611611
// argument for every possible option. This ensures that our options are
612612
// vectored to us.
@@ -722,7 +722,7 @@ class basic_parser_impl { // non-template implementation of basic_parser<t>
722722
return ValueRequired;
723723
}
724724

725-
void getExtraOptionNames(SmallVectorImpl<const char *> &) {}
725+
void getExtraOptionNames(SmallVectorImpl<StringRef> &) {}
726726

727727
void initialize() {}
728728

@@ -1206,8 +1206,7 @@ class opt : public Option,
12061206
enum ValueExpected getValueExpectedFlagDefault() const override {
12071207
return Parser.getValueExpectedFlagDefault();
12081208
}
1209-
void
1210-
getExtraOptionNames(SmallVectorImpl<const char *> &OptionNames) override {
1209+
void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
12111210
return Parser.getExtraOptionNames(OptionNames);
12121211
}
12131212

@@ -1368,8 +1367,7 @@ class list : public Option, public list_storage<DataType, StorageClass> {
13681367
enum ValueExpected getValueExpectedFlagDefault() const override {
13691368
return Parser.getValueExpectedFlagDefault();
13701369
}
1371-
void
1372-
getExtraOptionNames(SmallVectorImpl<const char *> &OptionNames) override {
1370+
void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
13731371
return Parser.getExtraOptionNames(OptionNames);
13741372
}
13751373

@@ -1508,8 +1506,7 @@ class bits : public Option, public bits_storage<DataType, Storage> {
15081506
enum ValueExpected getValueExpectedFlagDefault() const override {
15091507
return Parser.getValueExpectedFlagDefault();
15101508
}
1511-
void
1512-
getExtraOptionNames(SmallVectorImpl<const char *> &OptionNames) override {
1509+
void getExtraOptionNames(SmallVectorImpl<StringRef> &OptionNames) override {
15131510
return Parser.getExtraOptionNames(OptionNames);
15141511
}
15151512

lib/Support/CommandLine.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,12 @@ class CommandLineParser {
151151
}
152152

153153
void removeOption(Option *O) {
154-
SmallVector<const char *, 16> OptionNames;
154+
SmallVector<StringRef, 16> OptionNames;
155155
O->getExtraOptionNames(OptionNames);
156156
if (O->hasArgStr())
157157
OptionNames.push_back(O->ArgStr);
158158
for (auto Name : OptionNames)
159-
OptionsMap.erase(StringRef(Name));
159+
OptionsMap.erase(Name);
160160

161161
if (O->getFormattingFlag() == cl::Positional)
162162
for (auto Opt = PositionalOpts.begin(); Opt != PositionalOpts.end();
@@ -182,13 +182,13 @@ class CommandLineParser {
182182
nullptr != ConsumeAfterOpt);
183183
}
184184

185-
void updateArgStr(Option *O, const char *NewName) {
185+
void updateArgStr(Option *O, StringRef NewName) {
186186
if (!OptionsMap.insert(std::make_pair(NewName, O)).second) {
187187
errs() << ProgramName << ": CommandLine Error: Option '" << O->ArgStr
188188
<< "' registered more than once!\n";
189189
report_fatal_error("inconsistency in registered CommandLine options");
190190
}
191-
OptionsMap.erase(StringRef(O->ArgStr));
191+
OptionsMap.erase(O->ArgStr);
192192
}
193193

194194
void printOptionValues();
@@ -227,7 +227,7 @@ void Option::addArgument() {
227227

228228
void Option::removeArgument() { GlobalParser->removeOption(this); }
229229

230-
void Option::setArgStr(const char *S) {
230+
void Option::setArgStr(StringRef S) {
231231
if (FullyInitialized)
232232
GlobalParser->updateArgStr(this, S);
233233
ArgStr = S;
@@ -296,24 +296,23 @@ static Option *LookupNearestOption(StringRef Arg,
296296
ie = OptionsMap.end();
297297
it != ie; ++it) {
298298
Option *O = it->second;
299-
SmallVector<const char *, 16> OptionNames;
299+
SmallVector<StringRef, 16> OptionNames;
300300
O->getExtraOptionNames(OptionNames);
301-
if (O->ArgStr[0])
301+
if (O->hasArgStr())
302302
OptionNames.push_back(O->ArgStr);
303303

304304
bool PermitValue = O->getValueExpectedFlag() != cl::ValueDisallowed;
305305
StringRef Flag = PermitValue ? LHS : Arg;
306-
for (size_t i = 0, e = OptionNames.size(); i != e; ++i) {
307-
StringRef Name = OptionNames[i];
306+
for (auto Name : OptionNames) {
308307
unsigned Distance = StringRef(Name).edit_distance(
309308
Flag, /*AllowReplacements=*/true, /*MaxEditDistance=*/BestDistance);
310309
if (!Best || Distance < BestDistance) {
311310
Best = O;
312311
BestDistance = Distance;
313312
if (RHS.empty() || !PermitValue)
314-
NearestString = OptionNames[i];
313+
NearestString = Name;
315314
else
316-
NearestString = (Twine(OptionNames[i]) + "=" + RHS).str();
315+
NearestString = (Twine(Name) + "=" + RHS).str();
317316
}
318317
}
319318
}
@@ -1157,8 +1156,8 @@ bool Option::addOccurrence(unsigned pos, StringRef ArgName, StringRef Value,
11571156
// getValueStr - Get the value description string, using "DefaultMsg" if nothing
11581157
// has been specified yet.
11591158
//
1160-
static const char *getValueStr(const Option &O, const char *DefaultMsg) {
1161-
if (O.ValueStr[0] == 0)
1159+
static StringRef getValueStr(const Option &O, StringRef DefaultMsg) {
1160+
if (O.ValueStr.empty())
11621161
return DefaultMsg;
11631162
return O.ValueStr;
11641163
}
@@ -1168,7 +1167,7 @@ static const char *getValueStr(const Option &O, const char *DefaultMsg) {
11681167
//
11691168

11701169
// Return the width of the option tag for printing...
1171-
size_t alias::getOptionWidth() const { return std::strlen(ArgStr) + 6; }
1170+
size_t alias::getOptionWidth() const { return ArgStr.size() + 6; }
11721171

11731172
static void printHelpStr(StringRef HelpStr, size_t Indent,
11741173
size_t FirstLineIndentedBy) {
@@ -1183,7 +1182,7 @@ static void printHelpStr(StringRef HelpStr, size_t Indent,
11831182
// Print out the option for the alias.
11841183
void alias::printOptionInfo(size_t GlobalWidth) const {
11851184
outs() << " -" << ArgStr;
1186-
printHelpStr(HelpStr, GlobalWidth, std::strlen(ArgStr) + 6);
1185+
printHelpStr(HelpStr, GlobalWidth, ArgStr.size() + 6);
11871186
}
11881187

11891188
//===----------------------------------------------------------------------===//
@@ -1195,9 +1194,9 @@ void alias::printOptionInfo(size_t GlobalWidth) const {
11951194

11961195
// Return the width of the option tag for printing...
11971196
size_t basic_parser_impl::getOptionWidth(const Option &O) const {
1198-
size_t Len = std::strlen(O.ArgStr);
1197+
size_t Len = O.ArgStr.size();
11991198
if (const char *ValName = getValueName())
1200-
Len += std::strlen(getValueStr(O, ValName)) + 3;
1199+
Len += getValueStr(O, ValName).size() + 3;
12011200

12021201
return Len + 6;
12031202
}
@@ -1218,7 +1217,7 @@ void basic_parser_impl::printOptionInfo(const Option &O,
12181217
void basic_parser_impl::printOptionName(const Option &O,
12191218
size_t GlobalWidth) const {
12201219
outs() << " -" << O.ArgStr;
1221-
outs().indent(GlobalWidth - std::strlen(O.ArgStr));
1220+
outs().indent(GlobalWidth - O.ArgStr.size());
12221221
}
12231222

12241223
// parser<bool> implementation
@@ -1332,7 +1331,7 @@ unsigned generic_parser_base::findOption(const char *Name) {
13321331
// Return the width of the option tag for printing...
13331332
size_t generic_parser_base::getOptionWidth(const Option &O) const {
13341333
if (O.hasArgStr()) {
1335-
size_t Size = std::strlen(O.ArgStr) + 6;
1334+
size_t Size = O.ArgStr.size() + 6;
13361335
for (unsigned i = 0, e = getNumOptions(); i != e; ++i)
13371336
Size = std::max(Size, std::strlen(getOption(i)) + 8);
13381337
return Size;
@@ -1351,15 +1350,15 @@ void generic_parser_base::printOptionInfo(const Option &O,
13511350
size_t GlobalWidth) const {
13521351
if (O.hasArgStr()) {
13531352
outs() << " -" << O.ArgStr;
1354-
printHelpStr(O.HelpStr, GlobalWidth, std::strlen(O.ArgStr) + 6);
1353+
printHelpStr(O.HelpStr, GlobalWidth, O.ArgStr.size() + 6);
13551354

13561355
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
13571356
size_t NumSpaces = GlobalWidth - strlen(getOption(i)) - 8;
13581357
outs() << " =" << getOption(i);
13591358
outs().indent(NumSpaces) << " - " << getDescription(i) << '\n';
13601359
}
13611360
} else {
1362-
if (O.HelpStr[0])
1361+
if (!O.HelpStr.empty())
13631362
outs() << " " << O.HelpStr << '\n';
13641363
for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
13651364
const char *Option = getOption(i);
@@ -1378,7 +1377,7 @@ void generic_parser_base::printGenericOptionDiff(
13781377
const Option &O, const GenericOptionValue &Value,
13791378
const GenericOptionValue &Default, size_t GlobalWidth) const {
13801379
outs() << " -" << O.ArgStr;
1381-
outs().indent(GlobalWidth - std::strlen(O.ArgStr));
1380+
outs().indent(GlobalWidth - O.ArgStr.size());
13821381

13831382
unsigned NumOpts = getNumOptions();
13841383
for (unsigned i = 0; i != NumOpts; ++i) {
@@ -1522,7 +1521,7 @@ class HelpPrinter {
15221521
outs() << "USAGE: " << GlobalParser->ProgramName << " [options]";
15231522

15241523
for (auto Opt : GlobalParser->PositionalOpts) {
1525-
if (Opt->ArgStr[0])
1524+
if (Opt->hasArgStr())
15261525
outs() << " --" << Opt->ArgStr;
15271526
outs() << " " << Opt->HelpStr;
15281527
}

unittests/Support/CommandLineTest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,16 @@ TEST(CommandLineTest, ModifyExisitingOption) {
9999
"Failed to modify option's option category.";
100100

101101
Retrieved->setDescription(Description);
102-
ASSERT_STREQ(Retrieved->HelpStr, Description) <<
103-
"Changing option description failed.";
102+
ASSERT_STREQ(Retrieved->HelpStr.data(), Description)
103+
<< "Changing option description failed.";
104104

105105
Retrieved->setArgStr(ArgString);
106-
ASSERT_STREQ(ArgString, Retrieved->ArgStr) <<
107-
"Failed to modify option's Argument string.";
106+
ASSERT_STREQ(ArgString, Retrieved->ArgStr.data())
107+
<< "Failed to modify option's Argument string.";
108108

109109
Retrieved->setValueStr(ValueString);
110-
ASSERT_STREQ(Retrieved->ValueStr, ValueString) <<
111-
"Failed to modify option's Value string.";
110+
ASSERT_STREQ(Retrieved->ValueStr.data(), ValueString)
111+
<< "Failed to modify option's Value string.";
112112

113113
Retrieved->setHiddenFlag(cl::Hidden);
114114
ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<

utils/FileCheck/FileCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,7 @@ static bool ReadCheckFile(SourceMgr &SM,
873873
for (const auto &PatternString : ImplicitCheckNot) {
874874
// Create a buffer with fake command line content in order to display the
875875
// command line option responsible for the specific implicit CHECK-NOT.
876-
std::string Prefix = std::string("-") + ImplicitCheckNot.ArgStr + "='";
876+
std::string Prefix = (Twine("-") + ImplicitCheckNot.ArgStr + "='").str();
877877
std::string Suffix = "'";
878878
std::unique_ptr<MemoryBuffer> CmdLine = MemoryBuffer::getMemBufferCopy(
879879
Prefix + PatternString + Suffix, "command line");

0 commit comments

Comments
 (0)