[clang] [Driver] Use StringRef::operator== instead of StringRef::equals (NFC) (PR #91698)
Kazu Hirata via cfe-commits
cfe-commits at lists.llvm.org
Thu May 9 22:14:31 PDT 2024
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/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".
>From 5a5154a06a8df048b0edc0d6413c28c565de0867 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Thu, 9 May 2024 00:51:45 -0700
Subject: [PATCH] [Driver] Use StringRef::operator== instead of
StringRef::equals (NFC)
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".
---
clang/lib/Driver/Driver.cpp | 6 +-
clang/lib/Driver/ToolChains/AIX.cpp | 4 +-
clang/lib/Driver/ToolChains/AMDGPU.cpp | 2 +-
clang/lib/Driver/ToolChains/Clang.cpp | 66 +++++++++++-----------
clang/lib/Driver/ToolChains/CommonArgs.cpp | 2 +-
clang/lib/Driver/ToolChains/Flang.cpp | 13 ++---
6 files changed, 45 insertions(+), 48 deletions(-)
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 114320f5d3146..7b36d8e5084cf 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -564,9 +564,9 @@ static llvm::Triple computeTargetTriple(const Driver &D,
StringRef ObjectMode = *ObjectModeValue;
llvm::Triple::ArchType AT = llvm::Triple::UnknownArch;
- if (ObjectMode.equals("64")) {
+ if (ObjectMode == "64") {
AT = Target.get64BitArchVariant().getArch();
- } else if (ObjectMode.equals("32")) {
+ } else if (ObjectMode == "32") {
AT = Target.get32BitArchVariant().getArch();
} else {
D.Diag(diag::err_drv_invalid_object_mode) << ObjectMode;
@@ -6694,7 +6694,7 @@ llvm::StringRef clang::driver::getDriverMode(StringRef ProgName,
return Opt.consume_front(OptName) ? Opt : "";
}
-bool driver::IsClangCL(StringRef DriverMode) { return DriverMode.equals("cl"); }
+bool driver::IsClangCL(StringRef DriverMode) { return DriverMode == "cl"; }
llvm::Error driver::expandResponseFiles(SmallVectorImpl<const char *> &Args,
bool ClangCLMode,
diff --git a/clang/lib/Driver/ToolChains/AIX.cpp b/clang/lib/Driver/ToolChains/AIX.cpp
index aab98506adb96..85825e1ea65b1 100644
--- a/clang/lib/Driver/ToolChains/AIX.cpp
+++ b/clang/lib/Driver/ToolChains/AIX.cpp
@@ -481,8 +481,8 @@ static void addTocDataOptions(const llvm::opt::ArgList &Args,
// Currently only supported for small code model.
if (TOCDataGloballyinEffect &&
- (Args.getLastArgValue(options::OPT_mcmodel_EQ).equals("large") ||
- Args.getLastArgValue(options::OPT_mcmodel_EQ).equals("medium"))) {
+ (Args.getLastArgValue(options::OPT_mcmodel_EQ) == "large" ||
+ Args.getLastArgValue(options::OPT_mcmodel_EQ) == "medium")) {
D.Diag(clang::diag::warn_drv_unsupported_tocdata);
return;
}
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 07965b487ea79..9ffea57b005de 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -732,7 +732,7 @@ AMDGPUToolChain::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
checkTargetID(*DAL);
- if (!Args.getLastArgValue(options::OPT_x).equals("cl"))
+ if (Args.getLastArgValue(options::OPT_x) != "cl")
return DAL;
// Phase 1 (.cl -> .bc)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 775dc249999e1..449eb9b2a965a 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1526,7 +1526,7 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, const ArgList &Args,
CmdArgs.push_back(
Args.MakeArgString(Twine("-msign-return-address=") + Scope));
- if (!Scope.equals("none"))
+ if (Scope != "none")
CmdArgs.push_back(
Args.MakeArgString(Twine("-msign-return-address-key=") + Key));
if (BranchProtectionPAuthLR)
@@ -1719,10 +1719,9 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,
if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) {
StringRef Val = A->getValue();
const Driver &D = getToolChain().getDriver();
- if (Val.equals("128") || Val.equals("256") || Val.equals("512") ||
- Val.equals("1024") || Val.equals("2048") || Val.equals("128+") ||
- Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") ||
- Val.equals("2048+")) {
+ if (Val == "128" || Val == "256" || Val == "512" || Val == "1024" ||
+ Val == "2048" || Val == "128+" || Val == "256+" || Val == "512+" ||
+ Val == "1024+" || Val == "2048+") {
unsigned Bits = 0;
if (!Val.consume_back("+")) {
bool Invalid = Val.getAsInteger(10, Bits); (void)Invalid;
@@ -1736,7 +1735,7 @@ void Clang::AddAArch64TargetArgs(const ArgList &Args,
CmdArgs.push_back(
Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128)));
// Silently drop requests for vector-length agnostic code as it's implied.
- } else if (!Val.equals("scalable"))
+ } else if (Val != "scalable")
// Handle the unsupported values passed to msve-vector-bits.
D.Diag(diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << Val;
@@ -2098,7 +2097,7 @@ void Clang::AddRISCVTargetArgs(const ArgList &Args,
// If the value is "zvl", use MinVLen from march. Otherwise, try to parse
// as integer as long as we have a MinVLen.
unsigned Bits = 0;
- if (Val.equals("zvl") && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
+ if (Val == "zvl" && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
Bits = MinVLen;
} else if (!Val.getAsInteger(10, Bits)) {
// Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that
@@ -2115,7 +2114,7 @@ void Clang::AddRISCVTargetArgs(const ArgList &Args,
Args.MakeArgString("-mvscale-max=" + llvm::Twine(VScaleMin)));
CmdArgs.push_back(
Args.MakeArgString("-mvscale-min=" + llvm::Twine(VScaleMin)));
- } else if (!Val.equals("scalable")) {
+ } else if (Val != "scalable") {
// Handle the unsupported values passed to mrvv-vector-bits.
D.Diag(diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << Val;
@@ -2865,13 +2864,13 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
case options::OPT_fcomplex_arithmetic_EQ: {
LangOptions::ComplexRangeKind RangeVal;
StringRef Val = A->getValue();
- if (Val.equals("full"))
+ if (Val == "full")
RangeVal = LangOptions::ComplexRangeKind::CX_Full;
- else if (Val.equals("improved"))
+ else if (Val == "improved")
RangeVal = LangOptions::ComplexRangeKind::CX_Improved;
- else if (Val.equals("promoted"))
+ else if (Val == "promoted")
RangeVal = LangOptions::ComplexRangeKind::CX_Promoted;
- else if (Val.equals("basic"))
+ else if (Val == "basic")
RangeVal = LangOptions::ComplexRangeKind::CX_Basic;
else {
D.Diag(diag::err_drv_unsupported_option_argument)
@@ -2910,24 +2909,24 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
FPContract = "on";
StringRef Val = A->getValue();
- if (OFastEnabled && !Val.equals("fast")) {
- // Only -ffp-model=fast is compatible with OFast, ignore.
+ if (OFastEnabled && Val != "fast") {
+ // Only -ffp-model=fast is compatible with OFast, ignore.
D.Diag(clang::diag::warn_drv_overriding_option)
<< Args.MakeArgString("-ffp-model=" + Val) << "-Ofast";
break;
}
StrictFPModel = false;
- if (!FPModel.empty() && !FPModel.equals(Val))
+ if (!FPModel.empty() && FPModel != Val)
D.Diag(clang::diag::warn_drv_overriding_option)
<< Args.MakeArgString("-ffp-model=" + FPModel)
<< Args.MakeArgString("-ffp-model=" + Val);
- if (Val.equals("fast")) {
+ if (Val == "fast") {
FPModel = Val;
applyFastMath();
- } else if (Val.equals("precise")) {
+ } else if (Val == "precise") {
FPModel = Val;
FPContract = "on";
- } else if (Val.equals("strict")) {
+ } else if (Val == "strict") {
StrictFPModel = true;
FPExceptionBehavior = "strict";
FPModel = Val;
@@ -2957,7 +2956,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
case options::OPT_fno_signed_zeros: SignedZeros = false; break;
case options::OPT_ftrapping_math:
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
- !FPExceptionBehavior.equals("strict"))
+ FPExceptionBehavior != "strict")
// Warn that previous value of option is overridden.
D.Diag(clang::diag::warn_drv_overriding_option)
<< Args.MakeArgString("-ffp-exception-behavior=" +
@@ -2969,7 +2968,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
break;
case options::OPT_fno_trapping_math:
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
- !FPExceptionBehavior.equals("ignore"))
+ FPExceptionBehavior != "ignore")
// Warn that previous value of option is overridden.
D.Diag(clang::diag::warn_drv_overriding_option)
<< Args.MakeArgString("-ffp-exception-behavior=" +
@@ -3008,8 +3007,8 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
// Validate and pass through -ffp-contract option.
case options::OPT_ffp_contract: {
StringRef Val = A->getValue();
- if (Val.equals("fast") || Val.equals("on") || Val.equals("off") ||
- Val.equals("fast-honor-pragmas")) {
+ if (Val == "fast" || Val == "on" || Val == "off" ||
+ Val == "fast-honor-pragmas") {
FPContract = Val;
LastSeenFfpContractOption = Val;
} else
@@ -3022,16 +3021,16 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
case options::OPT_ffp_exception_behavior_EQ: {
StringRef Val = A->getValue();
if (!TrappingMathPresent && !FPExceptionBehavior.empty() &&
- !FPExceptionBehavior.equals(Val))
+ FPExceptionBehavior != Val)
// Warn that previous value of option is overridden.
D.Diag(clang::diag::warn_drv_overriding_option)
<< Args.MakeArgString("-ffp-exception-behavior=" +
FPExceptionBehavior)
<< Args.MakeArgString("-ffp-exception-behavior=" + Val);
TrappingMath = TrappingMathPresent = false;
- if (Val.equals("ignore") || Val.equals("maytrap"))
+ if (Val == "ignore" || Val == "maytrap")
FPExceptionBehavior = Val;
- else if (Val.equals("strict")) {
+ else if (Val == "strict") {
FPExceptionBehavior = Val;
TrappingMath = TrappingMathPresent = true;
} else
@@ -3043,8 +3042,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
// Validate and pass through -ffp-eval-method option.
case options::OPT_ffp_eval_method_EQ: {
StringRef Val = A->getValue();
- if (Val.equals("double") || Val.equals("extended") ||
- Val.equals("source"))
+ if (Val == "double" || Val == "extended" || Val == "source")
FPEvalMethod = Val;
else
D.Diag(diag::err_drv_unsupported_option_argument)
@@ -3056,18 +3054,18 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
StringRef Val = A->getValue();
const llvm::Triple::ArchType Arch = TC.getArch();
if (Arch == llvm::Triple::x86 || Arch == llvm::Triple::x86_64) {
- if (Val.equals("standard") || Val.equals("fast"))
+ if (Val == "standard" || Val == "fast")
Float16ExcessPrecision = Val;
// To make it GCC compatible, allow the value of "16" which
// means disable excess precision, the same meaning than clang's
// equivalent value "none".
- else if (Val.equals("16"))
+ else if (Val == "16")
Float16ExcessPrecision = "none";
else
D.Diag(diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << Val;
} else {
- if (!(Val.equals("standard") || Val.equals("fast")))
+ if (!(Val == "standard" || Val == "fast"))
D.Diag(diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << Val;
}
@@ -3149,7 +3147,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
// subsequent options conflict then emit warning diagnostic.
if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath &&
SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc &&
- FPContract.equals("off"))
+ FPContract == "off")
// OK: Current Arg doesn't conflict with -ffp-model=strict
;
else {
@@ -3195,7 +3193,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
if (TrappingMath) {
// FP Exception Behavior is also set to strict
- assert(FPExceptionBehavior.equals("strict"));
+ assert(FPExceptionBehavior == "strict");
}
// The default is IEEE.
@@ -3244,8 +3242,8 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc &&
ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath) {
CmdArgs.push_back("-ffast-math");
- if (FPModel.equals("fast")) {
- if (FPContract.equals("fast"))
+ if (FPModel == "fast") {
+ if (FPContract == "fast")
// All set, do nothing.
;
else if (FPContract.empty())
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 6796b43a15502..71e993119436a 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -346,7 +346,7 @@ void tools::addDirectoryList(const ArgList &Args, ArgStringList &CmdArgs,
return; // Nothing to do.
StringRef Name(ArgName);
- if (Name.equals("-I") || Name.equals("-L") || Name.empty())
+ if (Name == "-I" || Name == "-L" || Name.empty())
CombinedArg = true;
StringRef Dirs(DirList);
diff --git a/clang/lib/Driver/ToolChains/Flang.cpp b/clang/lib/Driver/ToolChains/Flang.cpp
index 436a9c418a5f9..d275528b69051 100644
--- a/clang/lib/Driver/ToolChains/Flang.cpp
+++ b/clang/lib/Driver/ToolChains/Flang.cpp
@@ -170,10 +170,9 @@ void Flang::AddAArch64TargetArgs(const ArgList &Args,
if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) {
StringRef Val = A->getValue();
const Driver &D = getToolChain().getDriver();
- if (Val.equals("128") || Val.equals("256") || Val.equals("512") ||
- Val.equals("1024") || Val.equals("2048") || Val.equals("128+") ||
- Val.equals("256+") || Val.equals("512+") || Val.equals("1024+") ||
- Val.equals("2048+")) {
+ if (Val == "128" || Val == "256" || Val == "512" || Val == "1024" ||
+ Val == "2048" || Val == "128+" || Val == "256+" || Val == "512+" ||
+ Val == "1024+" || Val == "2048+") {
unsigned Bits = 0;
if (!Val.consume_back("+")) {
[[maybe_unused]] bool Invalid = Val.getAsInteger(10, Bits);
@@ -187,7 +186,7 @@ void Flang::AddAArch64TargetArgs(const ArgList &Args,
CmdArgs.push_back(
Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128)));
// Silently drop requests for vector-length agnostic code as it's implied.
- } else if (!Val.equals("scalable"))
+ } else if (Val != "scalable")
// Handle the unsupported values passed to msve-vector-bits.
D.Diag(diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << Val;
@@ -214,7 +213,7 @@ void Flang::AddRISCVTargetArgs(const ArgList &Args,
// If the value is "zvl", use MinVLen from march. Otherwise, try to parse
// as integer as long as we have a MinVLen.
unsigned Bits = 0;
- if (Val.equals("zvl") && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
+ if (Val == "zvl" && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
Bits = MinVLen;
} else if (!Val.getAsInteger(10, Bits)) {
// Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that
@@ -231,7 +230,7 @@ void Flang::AddRISCVTargetArgs(const ArgList &Args,
Args.MakeArgString("-mvscale-max=" + llvm::Twine(VScaleMin)));
CmdArgs.push_back(
Args.MakeArgString("-mvscale-min=" + llvm::Twine(VScaleMin)));
- } else if (!Val.equals("scalable")) {
+ } else if (Val != "scalable") {
// Handle the unsupported values passed to mrvv-vector-bits.
D.Diag(diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << Val;
More information about the cfe-commits
mailing list