Skip to content

Commit f3dcc23

Browse files
[clang] Use StringRef::{starts,ends}_with (NFC) (#75149)
This patch replaces uses of StringRef::{starts,ends}with with StringRef::{starts,ends}_with for consistency with std::{string,string_view}::{starts,ends}_with in C++20. I'm planning to deprecate and eventually remove StringRef::{starts,ends}with.
1 parent eaa1152 commit f3dcc23

File tree

155 files changed

+547
-541
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+547
-541
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,7 +2878,7 @@ def Target : InheritableAttr {
28782878

28792879
for (auto &Feature : AttrFeatures) {
28802880
Feature = Feature.trim();
2881-
if (Feature.startswith("arch="))
2881+
if (Feature.starts_with("arch="))
28822882
return Feature.drop_front(sizeof("arch=") - 1);
28832883
}
28842884
return "";
@@ -2896,8 +2896,8 @@ def Target : InheritableAttr {
28962896
for (auto &Feature : AttrFeatures) {
28972897
Feature = Feature.trim();
28982898

2899-
if (!Feature.startswith("no-") && !Feature.startswith("arch=") &&
2900-
!Feature.startswith("fpmath=") && !Feature.startswith("tune="))
2899+
if (!Feature.starts_with("no-") && !Feature.starts_with("arch=") &&
2900+
!Feature.starts_with("fpmath=") && !Feature.starts_with("tune="))
29012901
Out.push_back(Feature);
29022902
}
29032903
}

clang/include/clang/Basic/IdentifierTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo {
511511
/// function(<#int x#>);
512512
/// \endcode
513513
bool isEditorPlaceholder() const {
514-
return getName().startswith("<#") && getName().endswith("#>");
514+
return getName().starts_with("<#") && getName().ends_with("#>");
515515
}
516516

517517
/// Determine whether \p this is a name reserved for the implementation (C99

clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ AnalyzerOptions::getRegisteredCheckers(bool IncludeExperimental) {
409409
};
410410
std::vector<StringRef> Checkers;
411411
for (StringRef CheckerName : StaticAnalyzerCheckerNames) {
412-
if (!CheckerName.startswith("debug.") &&
413-
(IncludeExperimental || !CheckerName.startswith("alpha.")))
412+
if (!CheckerName.starts_with("debug.") &&
413+
(IncludeExperimental || !CheckerName.starts_with("alpha.")))
414414
Checkers.push_back(CheckerName);
415415
}
416416
return Checkers;

clang/lib/APINotes/APINotesManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static void checkPrivateAPINotesName(DiagnosticsEngine &Diags,
198198
StringRef RealFileName =
199199
llvm::sys::path::filename(File->tryGetRealPathName());
200200
StringRef RealStem = llvm::sys::path::stem(RealFileName);
201-
if (RealStem.endswith("_private"))
201+
if (RealStem.ends_with("_private"))
202202
return;
203203

204204
unsigned DiagID = diag::warn_apinotes_private_case;

clang/lib/APINotes/APINotesYAMLCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ class YAMLConverter {
745745
convertCommonEntity(M, MI, M.Selector);
746746

747747
// Check if the selector ends with ':' to determine if it takes arguments.
748-
bool takesArguments = M.Selector.endswith(":");
748+
bool takesArguments = M.Selector.ends_with(":");
749749

750750
// Split the selector into pieces.
751751
llvm::SmallVector<StringRef, 4> Args;

clang/lib/ARCMigrate/ARCMT.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ createInvocationForMigration(CompilerInvocation &origCI,
201201
for (std::vector<std::string>::iterator
202202
I = CInvok->getDiagnosticOpts().Warnings.begin(),
203203
E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
204-
if (!StringRef(*I).startswith("error"))
204+
if (!StringRef(*I).starts_with("error"))
205205
WarnOpts.push_back(*I);
206206
}
207207
WarnOpts.push_back("error=arc-unsafe-retained-assign");

clang/lib/ARCMigrate/ObjCMT.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ static void rewriteToObjCProperty(const ObjCMethodDecl *Getter,
562562
static bool IsCategoryNameWithDeprecatedSuffix(ObjCContainerDecl *D) {
563563
if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D)) {
564564
StringRef Name = CatDecl->getName();
565-
return Name.endswith("Deprecated");
565+
return Name.ends_with("Deprecated");
566566
}
567567
return false;
568568
}
@@ -1176,12 +1176,12 @@ bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx,
11761176
if (!SetterMethod) {
11771177
// try a different naming convention for getter: isXxxxx
11781178
StringRef getterNameString = getterName->getName();
1179-
bool IsPrefix = getterNameString.startswith("is");
1179+
bool IsPrefix = getterNameString.starts_with("is");
11801180
// Note that we don't want to change an isXXX method of retainable object
11811181
// type to property (readonly or otherwise).
11821182
if (IsPrefix && GRT->isObjCRetainableType())
11831183
return false;
1184-
if (IsPrefix || getterNameString.startswith("get")) {
1184+
if (IsPrefix || getterNameString.starts_with("get")) {
11851185
LengthOfPrefix = (IsPrefix ? 2 : 3);
11861186
const char *CGetterName = getterNameString.data() + LengthOfPrefix;
11871187
// Make sure that first character after "is" or "get" prefix can
@@ -1320,11 +1320,11 @@ void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
13201320
if (OIT_Family == OIT_Singleton || OIT_Family == OIT_ReturnsSelf) {
13211321
StringRef STRefMethodName(MethodName);
13221322
size_t len = 0;
1323-
if (STRefMethodName.startswith("standard"))
1323+
if (STRefMethodName.starts_with("standard"))
13241324
len = strlen("standard");
1325-
else if (STRefMethodName.startswith("shared"))
1325+
else if (STRefMethodName.starts_with("shared"))
13261326
len = strlen("shared");
1327-
else if (STRefMethodName.startswith("default"))
1327+
else if (STRefMethodName.starts_with("default"))
13281328
len = strlen("default");
13291329
else
13301330
return;
@@ -1341,7 +1341,7 @@ void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
13411341
StringRef LoweredMethodName(MethodName);
13421342
std::string StringLoweredMethodName = LoweredMethodName.lower();
13431343
LoweredMethodName = StringLoweredMethodName;
1344-
if (!LoweredMethodName.startswith(ClassNamePostfix))
1344+
if (!LoweredMethodName.starts_with(ClassNamePostfix))
13451345
return;
13461346
if (OIT_Family == OIT_ReturnsSelf)
13471347
ReplaceWithClasstype(*this, OM);

clang/lib/ARCMigrate/TransUnbridgedCasts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class UnbridgedCastRewriter : public RecursiveASTVisitor<UnbridgedCastRewriter>{
146146
ento::cocoa::isRefType(E->getSubExpr()->getType(), "CF",
147147
FD->getIdentifier()->getName())) {
148148
StringRef fname = FD->getIdentifier()->getName();
149-
if (fname.endswith("Retain") || fname.contains("Create") ||
149+
if (fname.ends_with("Retain") || fname.contains("Create") ||
150150
fname.contains("Copy")) {
151151
// Do not migrate to couple of bridge transfer casts which
152152
// cancel each other out. Leave it unchanged so error gets user

clang/lib/ARCMigrate/TransformActions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ bool TransformActionsImpl::canReplaceText(SourceLocation loc, StringRef text) {
431431
if (invalidTemp)
432432
return false;
433433

434-
return file.substr(locInfo.second).startswith(text);
434+
return file.substr(locInfo.second).starts_with(text);
435435
}
436436

437437
void TransformActionsImpl::commitInsert(SourceLocation loc, StringRef text) {

clang/lib/ARCMigrate/Transforms.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ bool trans::isPlusOne(const Expr *E) {
9595
ento::cocoa::isRefType(callE->getType(), "CF",
9696
FD->getIdentifier()->getName())) {
9797
StringRef fname = FD->getIdentifier()->getName();
98-
if (fname.endswith("Retain") || fname.contains("Create") ||
98+
if (fname.ends_with("Retain") || fname.contains("Create") ||
9999
fname.contains("Copy"))
100100
return true;
101101
}

0 commit comments

Comments
 (0)