Use StringRef::operator== instead of StringRef::equals (NFC) (#91864)

I'm planning to remove StringRef::equals in favor of
StringRef::operator==.

- StringRef::operator==/!= outnumber StringRef::equals by a factor of
  276 under llvm-project/ 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".
This commit is contained in:
Kazu Hirata 2024-05-12 23:08:40 -07:00 committed by GitHub
parent e74a34b693
commit f841ca0c35
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 30 additions and 32 deletions

View File

@ -2000,7 +2000,7 @@ std::error_code DataAggregator::parseMMapEvents() {
std::pair<StringRef, MMapInfo> FileMMapInfo = FileMMapInfoRes.get();
if (FileMMapInfo.second.PID == -1)
continue;
if (FileMMapInfo.first.equals("(deleted)"))
if (FileMMapInfo.first == "(deleted)")
continue;
// Consider only the first mapping of the file for any given PID

View File

@ -1205,8 +1205,7 @@ std::error_code DataReader::parse() {
// Add entry data for branches to another function or branches
// to entry points (including recursive calls)
if (BI.To.IsSymbol &&
(!BI.From.Name.equals(BI.To.Name) || BI.To.Offset == 0)) {
if (BI.To.IsSymbol && (BI.From.Name != BI.To.Name || BI.To.Offset == 0)) {
I = GetOrCreateFuncEntry(BI.To.Name);
I->second.EntryData.emplace_back(std::move(BI));
}

View File

@ -1550,7 +1550,7 @@ CUOffsetMap DWARFRewriter::finalizeTypeSections(DIEBuilder &DIEBlder,
for (const SectionRef &Section : Obj->sections()) {
StringRef Contents = cantFail(Section.getContents());
StringRef Name = cantFail(Section.getName());
if (Name.equals(".debug_types"))
if (Name == ".debug_types")
BC.registerOrUpdateNoteSection(".debug_types", copyByteArray(Contents),
Contents.size());
}
@ -1633,10 +1633,10 @@ void DWARFRewriter::finalizeDebugSections(
for (const SectionRef &Secs : Obj->sections()) {
StringRef Contents = cantFail(Secs.getContents());
StringRef Name = cantFail(Secs.getName());
if (Name.equals(".debug_abbrev")) {
if (Name == ".debug_abbrev") {
BC.registerOrUpdateNoteSection(".debug_abbrev", copyByteArray(Contents),
Contents.size());
} else if (Name.equals(".debug_info")) {
} else if (Name == ".debug_info") {
BC.registerOrUpdateNoteSection(".debug_info", copyByteArray(Contents),
Contents.size());
}
@ -1771,7 +1771,7 @@ std::optional<StringRef> updateDebugData(
};
switch (SectionIter->second.second) {
default: {
if (!SectionName.equals("debug_str.dwo"))
if (SectionName != "debug_str.dwo")
errs() << "BOLT-WARNING: unsupported debug section: " << SectionName
<< "\n";
return SectionContents;
@ -1959,7 +1959,7 @@ void DWARFRewriter::updateDWP(DWARFUnit &CU,
continue;
}
if (SectionName.equals("debug_str.dwo")) {
if (SectionName == "debug_str.dwo") {
CurStrSection = OutData;
} else {
// Since handleDebugDataPatching returned true, we already know this is

View File

@ -87,7 +87,7 @@ void SDTRewriter::readSection() {
StringRef Name = DE.getCStr(&Offset);
if (!Name.equals("stapsdt"))
if (Name != "stapsdt")
errs() << "BOLT-WARNING: SDT note name \"" << Name
<< "\" is not expected\n";

View File

@ -171,7 +171,7 @@ std::optional<int64_t> ClangTidyCheck::OptionsView::getEnumInt(
if (IgnoreCase) {
if (Value.equals_insensitive(NameAndEnum.second))
return NameAndEnum.first;
} else if (Value.equals(NameAndEnum.second)) {
} else if (Value == NameAndEnum.second) {
return NameAndEnum.first;
} else if (Value.equals_insensitive(NameAndEnum.second)) {
Closest = NameAndEnum.second;

View File

@ -25,8 +25,8 @@ AST_MATCHER(QualType, isEnableIf) {
const NamedDecl *TypeDecl =
Spec->getTemplateName().getAsTemplateDecl()->getTemplatedDecl();
return TypeDecl->isInStdNamespace() &&
(TypeDecl->getName().equals("enable_if") ||
TypeDecl->getName().equals("enable_if_t"));
(TypeDecl->getName() == "enable_if" ||
TypeDecl->getName() == "enable_if_t");
};
const Type *BaseType = Node.getTypePtr();
// Case: pointer or reference to enable_if.

View File

@ -421,7 +421,7 @@ getContainerFromBeginEndCall(const Expr *Init, bool IsBegin, bool *IsArrow,
return {};
if (IsReverse && !Call->Name.consume_back("r"))
return {};
if (!Call->Name.empty() && !Call->Name.equals("c"))
if (!Call->Name.empty() && Call->Name != "c")
return {};
return std::make_pair(Call->Container, Call->CallKind);
}

View File

@ -1358,7 +1358,7 @@ IdentifierNamingCheck::getFailureInfo(
std::replace(KindName.begin(), KindName.end(), '_', ' ');
std::string Fixup = fixupWithStyle(Type, Name, Style, HNOption, ND);
if (StringRef(Fixup).equals(Name)) {
if (StringRef(Fixup) == Name) {
if (!IgnoreFailedSplit) {
LLVM_DEBUG(Location.print(llvm::dbgs(), SM);
llvm::dbgs()

View File

@ -138,11 +138,11 @@ static bool applyAbbreviationHeuristic(
const llvm::StringMap<std::string> &AbbreviationDictionary, StringRef Arg,
StringRef Param) {
if (AbbreviationDictionary.contains(Arg) &&
Param.equals(AbbreviationDictionary.lookup(Arg)))
Param == AbbreviationDictionary.lookup(Arg))
return true;
if (AbbreviationDictionary.contains(Param) &&
Arg.equals(AbbreviationDictionary.lookup(Param)))
Arg == AbbreviationDictionary.lookup(Param))
return true;
return false;

View File

@ -88,8 +88,7 @@ determineIncludeKind(StringRef CanonicalFile, StringRef IncludeFile,
if (FileCopy.consume_front(Parts.first) &&
FileCopy.consume_back(Parts.second)) {
// Determine the kind of this inclusion.
if (FileCopy.equals("/internal/") ||
FileCopy.equals("/proto/")) {
if (FileCopy == "/internal/" || FileCopy == "/proto/") {
return IncludeSorter::IK_MainTUInclude;
}
}

View File

@ -86,7 +86,7 @@ static const NamedDecl *findDecl(const RecordDecl &RecDecl,
StringRef DeclName) {
for (const Decl *D : RecDecl.decls()) {
if (const auto *ND = dyn_cast<NamedDecl>(D)) {
if (ND->getDeclName().isIdentifier() && ND->getName().equals(DeclName))
if (ND->getDeclName().isIdentifier() && ND->getName() == DeclName)
return ND;
}
}

View File

@ -883,7 +883,7 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
// -x cuda
auto language = args.getLastArgValue(clang::driver::options::OPT_x);
if (language.equals("cuda")) {
if (language == "cuda") {
res.getFrontendOpts().features.Enable(
Fortran::common::LanguageFeature::CUDA);
}
@ -986,7 +986,7 @@ static bool parseDialectArgs(CompilerInvocation &res, llvm::opt::ArgList &args,
if (args.hasArg(clang::driver::options::OPT_std_EQ)) {
auto standard = args.getLastArgValue(clang::driver::options::OPT_std_EQ);
// We only allow f2018 as the given standard
if (standard.equals("f2018")) {
if (standard == "f2018") {
res.setEnableConformanceChecks();
} else {
const unsigned diagID =

View File

@ -1607,7 +1607,7 @@ static bool isIntrinsicModuleProcedure(llvm::StringRef name) {
static bool isCoarrayIntrinsic(llvm::StringRef name) {
return name.starts_with("atomic_") || name.starts_with("co_") ||
name.contains("image") || name.ends_with("cobound") ||
name.equals("team_number");
name == "team_number";
}
/// Return the generic name of an intrinsic module procedure specific name.

View File

@ -3497,7 +3497,7 @@ public:
rewriter.startOpModification(op);
auto callee = op.getCallee();
if (callee)
if (callee->equals("hypotf"))
if (*callee == "hypotf")
op.setCalleeAttr(mlir::SymbolRefAttr::get(op.getContext(), "_hypotf"));
rewriter.finalizeOpModification(op);
@ -3514,7 +3514,7 @@ public:
matchAndRewrite(mlir::LLVM::LLVMFuncOp op,
mlir::PatternRewriter &rewriter) const override {
rewriter.startOpModification(op);
if (op.getSymName().equals("hypotf"))
if (op.getSymName() == "hypotf")
op.setSymNameAttr(rewriter.getStringAttr("_hypotf"));
rewriter.finalizeOpModification(op);
return mlir::success();
@ -3629,11 +3629,11 @@ public:
auto callee = op.getCallee();
if (!callee)
return true;
return !callee->equals("hypotf");
return *callee != "hypotf";
});
target.addDynamicallyLegalOp<mlir::LLVM::LLVMFuncOp>(
[](mlir::LLVM::LLVMFuncOp op) {
return !op.getSymName().equals("hypotf");
return op.getSymName() != "hypotf";
});
}

View File

@ -465,7 +465,7 @@ static bool equalsPath(StringRef path1, StringRef path2) {
#if defined(_WIN32)
return path1.equals_insensitive(path2);
#else
return path1.equals(path2);
return path1 == path2;
#endif
}

View File

@ -1128,7 +1128,7 @@ void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf,
for (Relocation &rel : relocs()) {
// Ignore calls into the split-stack api.
if (rel.sym->getName().starts_with("__morestack")) {
if (rel.sym->getName().equals("__morestack"))
if (rel.sym->getName() == "__morestack")
morestackCalls.push_back(&rel);
continue;
}

View File

@ -577,7 +577,7 @@ static bool isRelroSection(const OutputSection *sec) {
// for accessing .got as well, .got and .toc need to be close enough in the
// virtual address space. Usually, .toc comes just after .got. Since we place
// .got into RELRO, .toc needs to be placed into RELRO too.
if (sec->name.equals(".toc"))
if (sec->name == ".toc")
return true;
// .got.plt contains pointers to external function symbols. They are

View File

@ -1507,7 +1507,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
StringRef sep = sys::path::get_separator();
// real_path removes trailing slashes as part of the normalization, but
// these are meaningful for our text based stripping
if (config->osoPrefix.equals(".") || config->osoPrefix.ends_with(sep))
if (config->osoPrefix == "." || config->osoPrefix.ends_with(sep))
expanded += sep;
config->osoPrefix = saver().save(expanded.str());
}

View File

@ -519,8 +519,8 @@ uint64_t InputSection::getTombstoneForSection(StringRef name) {
// If they occur in DWARF debug symbols, we want to change the pc of the
// function to -1 to avoid overlapping with a valid range. However for the
// debug_ranges and debug_loc sections that would conflict with the existing
// meaning of -1 so we use -2.
if (name.equals(".debug_ranges") || name.equals(".debug_loc"))
// meaning of -1 so we use -2.
if (name == ".debug_ranges" || name == ".debug_loc")
return UINT64_C(-2);
if (name.starts_with(".debug_"))
return UINT64_C(-1);