Skip to content

[clang] Use StringRef::{starts,ends}_with (NFC) #75149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -2878,7 +2878,7 @@ def Target : InheritableAttr {

for (auto &Feature : AttrFeatures) {
Feature = Feature.trim();
if (Feature.startswith("arch="))
if (Feature.starts_with("arch="))
return Feature.drop_front(sizeof("arch=") - 1);
}
return "";
Expand All @@ -2896,8 +2896,8 @@ def Target : InheritableAttr {
for (auto &Feature : AttrFeatures) {
Feature = Feature.trim();

if (!Feature.startswith("no-") && !Feature.startswith("arch=") &&
!Feature.startswith("fpmath=") && !Feature.startswith("tune="))
if (!Feature.starts_with("no-") && !Feature.starts_with("arch=") &&
!Feature.starts_with("fpmath=") && !Feature.starts_with("tune="))
Out.push_back(Feature);
}
}
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/IdentifierTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ class alignas(IdentifierInfoAlignment) IdentifierInfo {
/// function(<#int x#>);
/// \endcode
bool isEditorPlaceholder() const {
return getName().startswith("<#") && getName().endswith("#>");
return getName().starts_with("<#") && getName().ends_with("#>");
}

/// Determine whether \p this is a name reserved for the implementation (C99
Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/StaticAnalyzer/Core/AnalyzerOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,8 @@ AnalyzerOptions::getRegisteredCheckers(bool IncludeExperimental) {
};
std::vector<StringRef> Checkers;
for (StringRef CheckerName : StaticAnalyzerCheckerNames) {
if (!CheckerName.startswith("debug.") &&
(IncludeExperimental || !CheckerName.startswith("alpha.")))
if (!CheckerName.starts_with("debug.") &&
(IncludeExperimental || !CheckerName.starts_with("alpha.")))
Checkers.push_back(CheckerName);
}
return Checkers;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/APINotes/APINotesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ static void checkPrivateAPINotesName(DiagnosticsEngine &Diags,
StringRef RealFileName =
llvm::sys::path::filename(File->tryGetRealPathName());
StringRef RealStem = llvm::sys::path::stem(RealFileName);
if (RealStem.endswith("_private"))
if (RealStem.ends_with("_private"))
return;

unsigned DiagID = diag::warn_apinotes_private_case;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/APINotes/APINotesYAMLCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ class YAMLConverter {
convertCommonEntity(M, MI, M.Selector);

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

// Split the selector into pieces.
llvm::SmallVector<StringRef, 4> Args;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/ARCMigrate/ARCMT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ createInvocationForMigration(CompilerInvocation &origCI,
for (std::vector<std::string>::iterator
I = CInvok->getDiagnosticOpts().Warnings.begin(),
E = CInvok->getDiagnosticOpts().Warnings.end(); I != E; ++I) {
if (!StringRef(*I).startswith("error"))
if (!StringRef(*I).starts_with("error"))
WarnOpts.push_back(*I);
}
WarnOpts.push_back("error=arc-unsafe-retained-assign");
Expand Down
14 changes: 7 additions & 7 deletions clang/lib/ARCMigrate/ObjCMT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ static void rewriteToObjCProperty(const ObjCMethodDecl *Getter,
static bool IsCategoryNameWithDeprecatedSuffix(ObjCContainerDecl *D) {
if (ObjCCategoryDecl *CatDecl = dyn_cast<ObjCCategoryDecl>(D)) {
StringRef Name = CatDecl->getName();
return Name.endswith("Deprecated");
return Name.ends_with("Deprecated");
}
return false;
}
Expand Down Expand Up @@ -1176,12 +1176,12 @@ bool ObjCMigrateASTConsumer::migrateProperty(ASTContext &Ctx,
if (!SetterMethod) {
// try a different naming convention for getter: isXxxxx
StringRef getterNameString = getterName->getName();
bool IsPrefix = getterNameString.startswith("is");
bool IsPrefix = getterNameString.starts_with("is");
// Note that we don't want to change an isXXX method of retainable object
// type to property (readonly or otherwise).
if (IsPrefix && GRT->isObjCRetainableType())
return false;
if (IsPrefix || getterNameString.startswith("get")) {
if (IsPrefix || getterNameString.starts_with("get")) {
LengthOfPrefix = (IsPrefix ? 2 : 3);
const char *CGetterName = getterNameString.data() + LengthOfPrefix;
// Make sure that first character after "is" or "get" prefix can
Expand Down Expand Up @@ -1320,11 +1320,11 @@ void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
if (OIT_Family == OIT_Singleton || OIT_Family == OIT_ReturnsSelf) {
StringRef STRefMethodName(MethodName);
size_t len = 0;
if (STRefMethodName.startswith("standard"))
if (STRefMethodName.starts_with("standard"))
len = strlen("standard");
else if (STRefMethodName.startswith("shared"))
else if (STRefMethodName.starts_with("shared"))
len = strlen("shared");
else if (STRefMethodName.startswith("default"))
else if (STRefMethodName.starts_with("default"))
len = strlen("default");
else
return;
Expand All @@ -1341,7 +1341,7 @@ void ObjCMigrateASTConsumer::migrateFactoryMethod(ASTContext &Ctx,
StringRef LoweredMethodName(MethodName);
std::string StringLoweredMethodName = LoweredMethodName.lower();
LoweredMethodName = StringLoweredMethodName;
if (!LoweredMethodName.startswith(ClassNamePostfix))
if (!LoweredMethodName.starts_with(ClassNamePostfix))
return;
if (OIT_Family == OIT_ReturnsSelf)
ReplaceWithClasstype(*this, OM);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/ARCMigrate/TransUnbridgedCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class UnbridgedCastRewriter : public RecursiveASTVisitor<UnbridgedCastRewriter>{
ento::cocoa::isRefType(E->getSubExpr()->getType(), "CF",
FD->getIdentifier()->getName())) {
StringRef fname = FD->getIdentifier()->getName();
if (fname.endswith("Retain") || fname.contains("Create") ||
if (fname.ends_with("Retain") || fname.contains("Create") ||
fname.contains("Copy")) {
// Do not migrate to couple of bridge transfer casts which
// cancel each other out. Leave it unchanged so error gets user
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/ARCMigrate/TransformActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ bool TransformActionsImpl::canReplaceText(SourceLocation loc, StringRef text) {
if (invalidTemp)
return false;

return file.substr(locInfo.second).startswith(text);
return file.substr(locInfo.second).starts_with(text);
}

void TransformActionsImpl::commitInsert(SourceLocation loc, StringRef text) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/ARCMigrate/Transforms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ bool trans::isPlusOne(const Expr *E) {
ento::cocoa::isRefType(callE->getType(), "CF",
FD->getIdentifier()->getName())) {
StringRef fname = FD->getIdentifier()->getName();
if (fname.endswith("Retain") || fname.contains("Create") ||
if (fname.ends_with("Retain") || fname.contains("Create") ||
fname.contains("Copy"))
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8223,7 +8223,7 @@ void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string &S,
// Another legacy compatibility encoding. Some ObjC qualifier and type
// combinations need to be rearranged.
// Rewrite "in const" from "nr" to "rn"
if (StringRef(S).endswith("nr"))
if (StringRef(S).ends_with("nr"))
S.replace(S.end()-2, S.end(), "rn");
}

Expand Down Expand Up @@ -13519,7 +13519,7 @@ void ASTContext::getFunctionFeatureMap(llvm::StringMap<bool> &FeatureMap,
Target->getTargetOpts().FeaturesAsWritten.begin(),
Target->getTargetOpts().FeaturesAsWritten.end());
} else {
if (VersionStr.startswith("arch="))
if (VersionStr.starts_with("arch="))
TargetCPU = VersionStr.drop_front(sizeof("arch=") - 1);
else if (VersionStr != "default")
Features.push_back((StringRef{"+"} + VersionStr).str());
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/DeclPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,7 @@ void DeclPrinter::VisitObjCPropertyDecl(ObjCPropertyDecl *PDecl) {
std::string TypeStr = PDecl->getASTContext().getUnqualifiedObjCPointerType(T).
getAsString(Policy);
Out << ' ' << TypeStr;
if (!StringRef(TypeStr).endswith("*"))
if (!StringRef(TypeStr).ends_with("*"))
Out << ' ';
Out << *PDecl;
if (Policy.PolishForDeclaration)
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/Mangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ void MangleContext::mangleName(GlobalDecl GD, raw_ostream &Out) {

// If the label isn't literal, or if this is an alias for an LLVM intrinsic,
// do not add a "\01" prefix.
if (!ALA->getIsLiteralLabel() || ALA->getLabel().startswith("llvm.")) {
if (!ALA->getIsLiteralLabel() || ALA->getLabel().starts_with("llvm.")) {
Out << ALA->getLabel();
return;
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/MicrosoftMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct msvc_hashing_ostream : public llvm::raw_svector_ostream {
: llvm::raw_svector_ostream(Buffer), OS(OS) {}
~msvc_hashing_ostream() override {
StringRef MangledName = str();
bool StartsWithEscape = MangledName.startswith("\01");
bool StartsWithEscape = MangledName.starts_with("\01");
if (StartsWithEscape)
MangledName = MangledName.drop_front(1);
if (MangledName.size() < 4096) {
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/PrintfFormatString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H,
// Set the privacy flag if the privacy annotation in the
// comma-delimited segment is at least as strict as the privacy
// annotations in previous comma-delimited segments.
if (MatchedStr.startswith("mask")) {
if (MatchedStr.starts_with("mask")) {
StringRef MaskType = MatchedStr.substr(sizeof("mask.") - 1);
unsigned Size = MaskType.size();
if (Warn && (Size == 0 || Size > 8))
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/AST/RawCommentList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ RawComment::RawComment(const SourceManager &SourceMgr, SourceRange SR,
Kind = K.first;
IsTrailingComment |= K.second;

IsAlmostTrailingComment = RawText.startswith("//<") ||
RawText.startswith("/*<");
IsAlmostTrailingComment =
RawText.starts_with("//<") || RawText.starts_with("/*<");
} else {
Kind = RCK_Merged;
IsTrailingComment =
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/AST/Stmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,11 +811,12 @@ std::string MSAsmStmt::generateAsmString(const ASTContext &C) const {
StringRef Instruction = Pieces[I];
// For vex/vex2/vex3/evex masm style prefix, convert it to att style
// since we don't support masm style prefix in backend.
if (Instruction.startswith("vex "))
if (Instruction.starts_with("vex "))
MSAsmString += '{' + Instruction.substr(0, 3).str() + '}' +
Instruction.substr(3).str();
else if (Instruction.startswith("vex2 ") ||
Instruction.startswith("vex3 ") || Instruction.startswith("evex "))
else if (Instruction.starts_with("vex2 ") ||
Instruction.starts_with("vex3 ") ||
Instruction.starts_with("evex "))
MSAsmString += '{' + Instruction.substr(0, 4).str() + '}' +
Instruction.substr(4).str();
else
Expand Down
12 changes: 6 additions & 6 deletions clang/lib/ASTMatchers/ASTMatchersInternal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,11 @@ HasNameMatcher::HasNameMatcher(std::vector<std::string> N)

static bool consumeNameSuffix(StringRef &FullName, StringRef Suffix) {
StringRef Name = FullName;
if (!Name.endswith(Suffix))
if (!Name.ends_with(Suffix))
return false;
Name = Name.drop_back(Suffix.size());
if (!Name.empty()) {
if (!Name.endswith("::"))
if (!Name.ends_with("::"))
return false;
Name = Name.drop_back(2);
}
Expand Down Expand Up @@ -530,7 +530,7 @@ class PatternSet {
PatternSet(ArrayRef<std::string> Names) {
Patterns.reserve(Names.size());
for (StringRef Name : Names)
Patterns.push_back({Name, Name.startswith("::")});
Patterns.push_back({Name, Name.starts_with("::")});
}

/// Consumes the name suffix from each pattern in the set and removes the ones
Expand Down Expand Up @@ -652,11 +652,11 @@ bool HasNameMatcher::matchesNodeFullSlow(const NamedDecl &Node) const {
const StringRef FullName = OS.str();

for (const StringRef Pattern : Names) {
if (Pattern.startswith("::")) {
if (Pattern.starts_with("::")) {
if (FullName == Pattern)
return true;
} else if (FullName.endswith(Pattern) &&
FullName.drop_back(Pattern.size()).endswith("::")) {
} else if (FullName.ends_with(Pattern) &&
FullName.drop_back(Pattern.size()).ends_with("::")) {
return true;
}
}
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/ASTMatchers/Dynamic/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ class Parser::CodeTokenizer {
break;
++TokenLength;
}
if (TokenLength == 4 && Code.startswith("true")) {
if (TokenLength == 4 && Code.starts_with("true")) {
Result.Kind = TokenInfo::TK_Literal;
Result.Value = true;
} else if (TokenLength == 5 && Code.startswith("false")) {
} else if (TokenLength == 5 && Code.starts_with("false")) {
Result.Kind = TokenInfo::TK_Literal;
Result.Value = false;
} else {
Expand Down Expand Up @@ -737,7 +737,7 @@ bool Parser::parseMatcherExpressionImpl(const TokenInfo &NameToken,
// Completions minus the prefix.
void Parser::addCompletion(const TokenInfo &CompToken,
const MatcherCompletion& Completion) {
if (StringRef(Completion.TypedText).startswith(CompToken.Text) &&
if (StringRef(Completion.TypedText).starts_with(CompToken.Text) &&
Completion.Specificity > 0) {
Completions.emplace_back(Completion.TypedText.substr(CompToken.Text.size()),
Completion.MatcherDecl, Completion.Specificity);
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Analysis/BodyFarm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,8 @@ Stmt *BodyFarm::getBody(const FunctionDecl *D) {
FF = nullptr;
break;
}
} else if (Name.startswith("OSAtomicCompareAndSwap") ||
Name.startswith("objc_atomicCompareAndSwap")) {
} else if (Name.starts_with("OSAtomicCompareAndSwap") ||
Name.starts_with("objc_atomicCompareAndSwap")) {
FF = create_OSAtomicCompareAndSwap;
} else if (Name == "call_once" && D->getDeclContext()->isStdNamespace()) {
FF = create_call_once;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Analysis/CallGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ bool CallGraph::includeCalleeInGraph(const Decl *D) {
return false;

IdentifierInfo *II = FD->getIdentifier();
if (II && II->getName().startswith("__inline"))
if (II && II->getName().starts_with("__inline"))
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Analysis/CalledOnceCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ class CalledOnceChecker : public ConstStmtVisitor<CalledOnceChecker> {
/// Return true if the given name has conventional suffixes.
static bool hasConventionalSuffix(llvm::StringRef Name) {
return llvm::any_of(CONVENTIONAL_SUFFIXES, [Name](llvm::StringRef Suffix) {
return Name.endswith(Suffix);
return Name.ends_with(Suffix);
});
}

Expand Down
11 changes: 5 additions & 6 deletions clang/lib/Analysis/CocoaConventions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
// Recursively walk the typedef stack, allowing typedefs of reference types.
while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
StringRef TDName = TD->getDecl()->getIdentifier()->getName();
if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
if (TDName.starts_with(Prefix) && TDName.ends_with("Ref"))
return true;
// XPC unfortunately uses CF-style function names, but aren't CF types.
if (TDName.startswith("xpc_"))
if (TDName.starts_with("xpc_"))
return false;
RetTy = TD->getDecl()->getUnderlyingType();
}
Expand All @@ -43,7 +43,7 @@ bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
return false;

// Does the name start with the prefix?
return Name.startswith(Prefix);
return Name.starts_with(Prefix);
}

/// Returns true when the passed-in type is a CF-style reference-counted
Expand Down Expand Up @@ -127,10 +127,9 @@ bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
// Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
// character.
StringRef suffix = functionName.substr(it - start);
if (suffix.startswith("reate")) {
if (suffix.starts_with("reate")) {
it += 5;
}
else if (suffix.startswith("opy")) {
} else if (suffix.starts_with("opy")) {
it += 3;
} else {
// Keep scanning.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bool isCheckLikeMethod(llvm::SmallDenseSet<const CXXMethodDecl *> &CheckDecls,
return false;

for (const CXXMethodDecl *M : ParentClass->methods())
if (M->getDeclName().isIdentifier() && M->getName().endswith("Check"))
if (M->getDeclName().isIdentifier() && M->getName().ends_with("Check"))
CheckDecls.insert(M);
}

Expand Down
Loading