Skip to content

Commit 135d92f

Browse files
[Driver] Use StringRef::operator== instead of StringRef::equals (NFC) (#91698)
I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator==/!= outnumber StringRef::equals by a factor of 13 under clang/ in terms of their usage. - The elimination of StringRef::equals brings StringRef closer to std::string_view, which has operator== but not equals. - S == "foo" is more readable than S.equals("foo"), especially for !Long.Expression.equals("str") vs Long.Expression != "str".
1 parent 06ad863 commit 135d92f

File tree

6 files changed

+45
-48
lines changed

6 files changed

+45
-48
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,9 @@ static llvm::Triple computeTargetTriple(const Driver &D,
564564
StringRef ObjectMode = *ObjectModeValue;
565565
llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
566566

567-
if (ObjectMode.equals("64")) {
567+
if (ObjectMode == "64") {
568568
AT = Target.get64BitArchVariant().getArch();
569-
} else if (ObjectMode.equals("32")) {
569+
} else if (ObjectMode == "32") {
570570
AT = Target.get32BitArchVariant().getArch();
571571
} else {
572572
D.Diag(diag::err_drv_invalid_object_mode) << ObjectMode;
@@ -6694,7 +6694,7 @@ llvm::StringRef clang::driver::getDriverMode(StringRef ProgName,
66946694
return Opt.consume_front(OptName) ? Opt : "";
66956695
}
66966696

6697-
bool driver::IsClangCL(StringRef DriverMode) { return DriverMode.equals("cl"); }
6697+
bool driver::IsClangCL(StringRef DriverMode) { return DriverMode == "cl"; }
66986698

66996699
llvm::Error driver::expandResponseFiles(SmallVectorImpl<const char *> &Args,
67006700
bool ClangCLMode,

clang/lib/Driver/ToolChains/AIX.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,8 @@ static void addTocDataOptions(const llvm::opt::ArgList &Args,
481481

482482
// Currently only supported for small code model.
483483
if (TOCDataGloballyinEffect &&
484-
(Args.getLastArgValue(options::OPT_mcmodel_EQ).equals("large") ||
485-
Args.getLastArgValue(options::OPT_mcmodel_EQ).equals("medium"))) {
484+
(Args.getLastArgValue(options::OPT_mcmodel_EQ) == "large" ||
485+
Args.getLastArgValue(options::OPT_mcmodel_EQ) == "medium")) {
486486
D.Diag(clang::diag::warn_drv_unsupported_tocdata);
487487
return;
488488
}

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
732732

733733
checkTargetID(*DAL);
734734

735-
if (!Args.getLastArgValue(options::OPT_x).equals("cl"))
735+
if (Args.getLastArgValue(options::OPT_x) != "cl")
736736
return DAL;
737737

738738
// Phase 1 (.cl -> .bc)

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,7 +1526,7 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
15261526

15271527
CmdArgs.push_back(
15281528
Args.MakeArgString(Twine("-msign-return-address=") + Scope));
1529-
if (!Scope.equals("none"))
1529+
if (Scope != "none")
15301530
CmdArgs.push_back(
15311531
Args.MakeArgString(Twine("-msign-return-address-key=") + Key));
15321532
if (BranchProtectionPAuthLR)
@@ -1719,10 +1719,9 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,
17191719
if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) {
17201720
StringRef Val = A->getValue();
17211721
const Driver &D = getToolChain().getDriver();
1722-
if (Val.equals("128") || Val.equals("256") || Val.equals("512") ||
1723-
Val.equals("1024") || Val.equals("2048") || Val.equals("128+") ||
1724-
Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") ||
1725-
Val.equals("2048+")) {
1722+
if (Val == "128" || Val == "256" || Val == "512" || Val == "1024" ||
1723+
Val == "2048" || Val == "128+" || Val == "256+" || Val == "512+" ||
1724+
Val == "1024+" || Val == "2048+") {
17261725
unsigned Bits = 0;
17271726
if (!Val.consume_back("+")) {
17281727
bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid;
@@ -1736,7 +1735,7 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,
17361735
CmdArgs.push_back(
17371736
Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128)));
17381737
// Silently drop requests for vector-length agnostic code as it's implied.
1739-
} else if (!Val.equals("scalable"))
1738+
} else if (Val != "scalable")
17401739
// Handle the unsupported values passed to msve-vector-bits.
17411740
D.Diag(diag::err_drv_unsupported_option_argument)
17421741
<< A->getSpelling() << Val;
@@ -2098,7 +2097,7 @@ void Clang::AddRISCVTargetArgs(const ArgList &Args,
20982097
// If the value is "zvl", use MinVLen from march. Otherwise, try to parse
20992098
// as integer as long as we have a MinVLen.
21002099
unsigned Bits = 0;
2101-
if (Val.equals("zvl") && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
2100+
if (Val == "zvl" && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
21022101
Bits = MinVLen;
21032102
} else if (!Val.getAsInteger(10, Bits)) {
21042103
// Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that
@@ -2115,7 +2114,7 @@ void Clang::AddRISCVTargetArgs(const ArgList &Args,
21152114
Args.MakeArgString("-mvscale-max=" + llvm::Twine(VScaleMin)));
21162115
CmdArgs.push_back(
21172116
Args.MakeArgString("-mvscale-min=" + llvm::Twine(VScaleMin)));
2118-
} else if (!Val.equals("scalable")) {
2117+
} else if (Val != "scalable") {
21192118
// Handle the unsupported values passed to mrvv-vector-bits.
21202119
D.Diag(diag::err_drv_unsupported_option_argument)
21212120
<< A->getSpelling() << Val;
@@ -2865,13 +2864,13 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
28652864
case options::OPT_fcomplex_arithmetic_EQ: {
28662865
LangOptions::ComplexRangeKind RangeVal;
28672866
StringRef Val = A->getValue();
2868-
if (Val.equals("full"))
2867+
if (Val == "full")
28692868
RangeVal = LangOptions::ComplexRangeKind::CX_Full;
2870-
else if (Val.equals("improved"))
2869+
else if (Val == "improved")
28712870
RangeVal = LangOptions::ComplexRangeKind::CX_Improved;
2872-
else if (Val.equals("promoted"))
2871+
else if (Val == "promoted")
28732872
RangeVal = LangOptions::ComplexRangeKind::CX_Promoted;
2874-
else if (Val.equals("basic"))
2873+
else if (Val == "basic")
28752874
RangeVal = LangOptions::ComplexRangeKind::CX_Basic;
28762875
else {
28772876
D.Diag(diag::err_drv_unsupported_option_argument)
@@ -2910,24 +2909,24 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29102909
FPContract = "on";
29112910

29122911
StringRef Val = A->getValue();
2913-
if (OFastEnabled && !Val.equals("fast")) {
2914-
// Only -ffp-model=fast is compatible with OFast, ignore.
2912+
if (OFastEnabled && Val != "fast") {
2913+
// Only -ffp-model=fast is compatible with OFast, ignore.
29152914
D.Diag(clang::diag::warn_drv_overriding_option)
29162915
<< Args.MakeArgString("-ffp-model=" + Val) << "-Ofast";
29172916
break;
29182917
}
29192918
StrictFPModel = false;
2920-
if (!FPModel.empty() && !FPModel.equals(Val))
2919+
if (!FPModel.empty() && FPModel != Val)
29212920
D.Diag(clang::diag::warn_drv_overriding_option)
29222921
<< Args.MakeArgString("-ffp-model=" + FPModel)
29232922
<< Args.MakeArgString("-ffp-model=" + Val);
2924-
if (Val.equals("fast")) {
2923+
if (Val == "fast") {
29252924
FPModel = Val;
29262925
applyFastMath();
2927-
} else if (Val.equals("precise")) {
2926+
} else if (Val == "precise") {
29282927
FPModel = Val;
29292928
FPContract = "on";
2930-
} else if (Val.equals("strict")) {
2929+
} else if (Val == "strict") {
29312930
StrictFPModel = true;
29322931
FPExceptionBehavior = "strict";
29332932
FPModel = Val;
@@ -2957,7 +2956,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29572956
case options::OPT_fno_signed_zeros: SignedZeros = false; break;
29582957
case options::OPT_ftrapping_math:
29592958
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
2960-
!FPExceptionBehavior.equals("strict"))
2959+
FPExceptionBehavior != "strict")
29612960
// Warn that previous value of option is overridden.
29622961
D.Diag(clang::diag::warn_drv_overriding_option)
29632962
<< Args.MakeArgString("-ffp-exception-behavior=" +
@@ -2969,7 +2968,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29692968
break;
29702969
case options::OPT_fno_trapping_math:
29712970
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
2972-
!FPExceptionBehavior.equals("ignore"))
2971+
FPExceptionBehavior != "ignore")
29732972
// Warn that previous value of option is overridden.
29742973
D.Diag(clang::diag::warn_drv_overriding_option)
29752974
<< Args.MakeArgString("-ffp-exception-behavior=" +
@@ -3008,8 +3007,8 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
30083007
// Validate and pass through -ffp-contract option.
30093008
case options::OPT_ffp_contract: {
30103009
StringRef Val = A->getValue();
3011-
if (Val.equals("fast") || Val.equals("on") || Val.equals("off") ||
3012-
Val.equals("fast-honor-pragmas")) {
3010+
if (Val == "fast" || Val == "on" || Val == "off" ||
3011+
Val == "fast-honor-pragmas") {
30133012
FPContract = Val;
30143013
LastSeenFfpContractOption = Val;
30153014
} else
@@ -3022,16 +3021,16 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
30223021
case options::OPT_ffp_exception_behavior_EQ: {
30233022
StringRef Val = A->getValue();
30243023
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
3025-
!FPExceptionBehavior.equals(Val))
3024+
FPExceptionBehavior != Val)
30263025
// Warn that previous value of option is overridden.
30273026
D.Diag(clang::diag::warn_drv_overriding_option)
30283027
<< Args.MakeArgString("-ffp-exception-behavior=" +
30293028
FPExceptionBehavior)
30303029
<< Args.MakeArgString("-ffp-exception-behavior=" + Val);
30313030
TrappingMath = TrappingMathPresent = false;
3032-
if (Val.equals("ignore") || Val.equals("maytrap"))
3031+
if (Val == "ignore" || Val == "maytrap")
30333032
FPExceptionBehavior = Val;
3034-
else if (Val.equals("strict")) {
3033+
else if (Val == "strict") {
30353034
FPExceptionBehavior = Val;
30363035
TrappingMath = TrappingMathPresent = true;
30373036
} else
@@ -3043,8 +3042,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
30433042
// Validate and pass through -ffp-eval-method option.
30443043
case options::OPT_ffp_eval_method_EQ: {
30453044
StringRef Val = A->getValue();
3046-
if (Val.equals("double") || Val.equals("extended") ||
3047-
Val.equals("source"))
3045+
if (Val == "double" || Val == "extended" || Val == "source")
30483046
FPEvalMethod = Val;
30493047
else
30503048
D.Diag(diag::err_drv_unsupported_option_argument)
@@ -3056,18 +3054,18 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
30563054
StringRef Val = A->getValue();
30573055
const llvm::Triple::ArchType Arch = TC.getArch();
30583056
if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
3059-
if (Val.equals("standard") || Val.equals("fast"))
3057+
if (Val == "standard" || Val == "fast")
30603058
Float16ExcessPrecision = Val;
30613059
// To make it GCC compatible, allow the value of "16" which
30623060
// means disable excess precision, the same meaning than clang's
30633061
// equivalent value "none".
3064-
else if (Val.equals("16"))
3062+
else if (Val == "16")
30653063
Float16ExcessPrecision = "none";
30663064
else
30673065
D.Diag(diag::err_drv_unsupported_option_argument)
30683066
<< A->getSpelling() << Val;
30693067
} else {
3070-
if (!(Val.equals("standard") || Val.equals("fast")))
3068+
if (!(Val == "standard" || Val == "fast"))
30713069
D.Diag(diag::err_drv_unsupported_option_argument)
30723070
<< A->getSpelling() << Val;
30733071
}
@@ -3149,7 +3147,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
31493147
// subsequent options conflict then emit warning diagnostic.
31503148
if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath &&
31513149
SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc &&
3152-
FPContract.equals("off"))
3150+
FPContract == "off")
31533151
// OK: Current Arg doesn't conflict with -ffp-model=strict
31543152
;
31553153
else {
@@ -3195,7 +3193,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
31953193

31963194
if (TrappingMath) {
31973195
// FP Exception Behavior is also set to strict
3198-
assert(FPExceptionBehavior.equals("strict"));
3196+
assert(FPExceptionBehavior == "strict");
31993197
}
32003198

32013199
// The default is IEEE.
@@ -3244,8 +3242,8 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
32443242
if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc &&
32453243
ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) {
32463244
CmdArgs.push_back("-ffast-math");
3247-
if (FPModel.equals("fast")) {
3248-
if (FPContract.equals("fast"))
3245+
if (FPModel == "fast") {
3246+
if (FPContract == "fast")
32493247
// All set, do nothing.
32503248
;
32513249
else if (FPContract.empty())

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ void tools::addDirectoryList(const ArgList &Args, ArgStringList &CmdArgs,
346346
return; // Nothing to do.
347347

348348
StringRef Name(ArgName);
349-
if (Name.equals("-I") || Name.equals("-L") || Name.empty())
349+
if (Name == "-I" || Name == "-L" || Name.empty())
350350
CombinedArg = true;
351351

352352
StringRef Dirs(DirList);

clang/lib/Driver/ToolChains/Flang.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,9 @@ void Flang::AddAArch64TargetArgs(const ArgList &Args,
170170
if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) {
171171
StringRef Val = A->getValue();
172172
const Driver &D = getToolChain().getDriver();
173-
if (Val.equals("128") || Val.equals("256") || Val.equals("512") ||
174-
Val.equals("1024") || Val.equals("2048") || Val.equals("128+") ||
175-
Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") ||
176-
Val.equals("2048+")) {
173+
if (Val == "128" || Val == "256" || Val == "512" || Val == "1024" ||
174+
Val == "2048" || Val == "128+" || Val == "256+" || Val == "512+" ||
175+
Val == "1024+" || Val == "2048+") {
177176
unsigned Bits = 0;
178177
if (!Val.consume_back("+")) {
179178
[[maybe_unused]] bool Invalid = Val.getAsInteger(10, Bits);
@@ -187,7 +186,7 @@ void Flang::AddAArch64TargetArgs(const ArgList &Args,
187186
CmdArgs.push_back(
188187
Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128)));
189188
// Silently drop requests for vector-length agnostic code as it's implied.
190-
} else if (!Val.equals("scalable"))
189+
} else if (Val != "scalable")
191190
// Handle the unsupported values passed to msve-vector-bits.
192191
D.Diag(diag::err_drv_unsupported_option_argument)
193192
<< A->getSpelling() << Val;
@@ -214,7 +213,7 @@ void Flang::AddRISCVTargetArgs(const ArgList &Args,
214213
// If the value is "zvl", use MinVLen from march. Otherwise, try to parse
215214
// as integer as long as we have a MinVLen.
216215
unsigned Bits = 0;
217-
if (Val.equals("zvl") && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
216+
if (Val == "zvl" && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
218217
Bits = MinVLen;
219218
} else if (!Val.getAsInteger(10, Bits)) {
220219
// Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that
@@ -231,7 +230,7 @@ void Flang::AddRISCVTargetArgs(const ArgList &Args,
231230
Args.MakeArgString("-mvscale-max=" + llvm::Twine(VScaleMin)));
232231
CmdArgs.push_back(
233232
Args.MakeArgString("-mvscale-min=" + llvm::Twine(VScaleMin)));
234-
} else if (!Val.equals("scalable")) {
233+
} else if (Val != "scalable") {
235234
// Handle the unsupported values passed to mrvv-vector-bits.
236235
D.Diag(diag::err_drv_unsupported_option_argument)
237236
<< A->getSpelling() << Val;

0 commit comments

Comments
 (0)