Skip to content

Commit deffae5

Browse files
[clang] Use StringRef::operator== instead of StringRef::equals (NFC) (#91844)
I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator==/!= outnumber StringRef::equals by a factor of 24 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 379b777 commit deffae5

39 files changed

+68
-73
lines changed

clang/include/clang/Basic/IdentifierTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ class IdentifierTable {
738738
II->Entry = &Entry;
739739

740740
// If this is the 'import' contextual keyword, mark it as such.
741-
if (Name.equals("import"))
741+
if (Name == "import")
742742
II->setModulesImport(true);
743743

744744
return *II;

clang/include/clang/Basic/SourceManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
15041504
if (Presumed.isInvalid())
15051505
return false;
15061506
StringRef Filename(Presumed.getFilename());
1507-
return Filename.equals("<built-in>");
1507+
return Filename == "<built-in>";
15081508
}
15091509

15101510
/// Returns whether \p Loc is located in a <command line> file.
@@ -1513,7 +1513,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
15131513
if (Presumed.isInvalid())
15141514
return false;
15151515
StringRef Filename(Presumed.getFilename());
1516-
return Filename.equals("<command line>");
1516+
return Filename == "<command line>";
15171517
}
15181518

15191519
/// Returns whether \p Loc is located in a <scratch space> file.
@@ -1522,7 +1522,7 @@ class SourceManager : public RefCountedBase<SourceManager> {
15221522
if (Presumed.isInvalid())
15231523
return false;
15241524
StringRef Filename(Presumed.getFilename());
1525-
return Filename.equals("<scratch space>");
1525+
return Filename == "<scratch space>";
15261526
}
15271527

15281528
/// Returns if a SourceLocation is in a system header.

clang/lib/ARCMigrate/ObjCMT.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ static void rewriteToObjCProperty(const ObjCMethodDecl *Getter,
484484

485485
// Short circuit 'delegate' properties that contain the name "delegate" or
486486
// "dataSource", or have exact name "target" to have 'assign' attribute.
487-
if (PropertyName.equals("target") || PropertyName.contains("delegate") ||
487+
if (PropertyName == "target" || PropertyName.contains("delegate") ||
488488
PropertyName.contains("dataSource")) {
489489
QualType QT = Getter->getReturnType();
490490
if (!QT->isRealType())

clang/lib/AST/PrintfFormatString.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H,
146146
if (Warn && (Size == 0 || Size > 8))
147147
H.handleInvalidMaskType(MaskType);
148148
FS.setMaskType(MaskType);
149-
} else if (MatchedStr.equals("sensitive"))
149+
} else if (MatchedStr == "sensitive")
150150
PrivacyFlags = clang::analyze_os_log::OSLogBufferItem::IsSensitive;
151151
else if (PrivacyFlags !=
152-
clang::analyze_os_log::OSLogBufferItem::IsSensitive &&
153-
MatchedStr.equals("private"))
152+
clang::analyze_os_log::OSLogBufferItem::IsSensitive &&
153+
MatchedStr == "private")
154154
PrivacyFlags = clang::analyze_os_log::OSLogBufferItem::IsPrivate;
155-
else if (PrivacyFlags == 0 && MatchedStr.equals("public"))
155+
else if (PrivacyFlags == 0 && MatchedStr == "public")
156156
PrivacyFlags = clang::analyze_os_log::OSLogBufferItem::IsPublic;
157157
} else {
158158
size_t CommaOrBracePos =

clang/lib/ASTMatchers/Dynamic/Marshallers.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ getBestGuess(llvm::StringRef Search, llvm::ArrayRef<llvm::StringRef> Allowed,
2121
llvm::StringRef Res;
2222
for (const llvm::StringRef &Item : Allowed) {
2323
if (Item.equals_insensitive(Search)) {
24-
assert(!Item.equals(Search) && "This should be handled earlier on.");
24+
assert(Item != Search && "This should be handled earlier on.");
2525
MaxEditDistance = 1;
2626
Res = Item;
2727
continue;
@@ -41,7 +41,7 @@ getBestGuess(llvm::StringRef Search, llvm::ArrayRef<llvm::StringRef> Allowed,
4141
if (!NoPrefix.consume_front(DropPrefix))
4242
continue;
4343
if (NoPrefix.equals_insensitive(Search)) {
44-
if (NoPrefix.equals(Search))
44+
if (NoPrefix == Search)
4545
return Item.str();
4646
MaxEditDistance = 1;
4747
Res = Item;

clang/lib/Basic/Builtins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ bool Builtin::Context::isBuiltinFunc(llvm::StringRef FuncName) {
6464
bool InStdNamespace = FuncName.consume_front("std-");
6565
for (unsigned i = Builtin::NotBuiltin + 1; i != Builtin::FirstTSBuiltin;
6666
++i) {
67-
if (FuncName.equals(BuiltinInfo[i].Name) &&
67+
if (FuncName == BuiltinInfo[i].Name &&
6868
(bool)strchr(BuiltinInfo[i].Attributes, 'z') == InStdNamespace)
6969
return strchr(BuiltinInfo[i].Attributes, 'f') != nullptr;
7070
}

clang/lib/Basic/Diagnostic.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,8 +851,7 @@ FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
851851
// When the diagnostic string is only "%0", the entire string is being given
852852
// by an outside source. Remove unprintable characters from this string
853853
// and skip all the other string processing.
854-
if (DiagEnd - DiagStr == 2 &&
855-
StringRef(DiagStr, DiagEnd - DiagStr).equals("%0") &&
854+
if (DiagEnd - DiagStr == 2 && StringRef(DiagStr, DiagEnd - DiagStr) == "%0" &&
856855
getArgKind(0) == DiagnosticsEngine::ak_std_string) {
857856
const std::string &S = getArgStdStr(0);
858857
EscapeStringForDiagnostic(S, OutStr);

clang/lib/Basic/LangOptions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void LangOptions::resetNonModularOptions() {
4848

4949
bool LangOptions::isNoBuiltinFunc(StringRef FuncName) const {
5050
for (unsigned i = 0, e = NoBuiltinFuncs.size(); i != e; ++i)
51-
if (FuncName.equals(NoBuiltinFuncs[i]))
51+
if (FuncName == NoBuiltinFuncs[i])
5252
return true;
5353
return false;
5454
}

clang/lib/Basic/Targets/ARM.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ bool ARMTargetInfo::supportsThumb() const {
173173
}
174174

175175
bool ARMTargetInfo::supportsThumb2() const {
176-
return CPUAttr.equals("6T2") ||
177-
(ArchVersion >= 7 && !CPUAttr.equals("8M_BASE"));
176+
return CPUAttr == "6T2" || (ArchVersion >= 7 && CPUAttr != "8M_BASE");
178177
}
179178

180179
StringRef ARMTargetInfo::getCPUAttr() const {
@@ -1162,7 +1161,7 @@ bool ARMTargetInfo::validateAsmConstraint(
11621161
return true;
11631162
case 'j': // An immediate integer between 0 and 65535 (valid for MOVW)
11641163
// only available in ARMv6T2 and above
1165-
if (CPUAttr.equals("6T2") || ArchVersion >= 7) {
1164+
if (CPUAttr == "6T2" || ArchVersion >= 7) {
11661165
Info.setRequiresImmediate(0, 65535);
11671166
return true;
11681167
}

clang/lib/Basic/Targets/PPC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ class LLVM_LIBRARY_VISIBILITY PPCTargetInfo : public TargetInfo {
360360
bool hasBitIntType() const override { return true; }
361361

362362
bool isSPRegName(StringRef RegName) const override {
363-
return RegName.equals("r1") || RegName.equals("x1");
363+
return RegName == "r1" || RegName == "x1";
364364
}
365365

366366
// We support __builtin_cpu_supports/__builtin_cpu_is on targets that

0 commit comments

Comments
 (0)