Skip to content

Commit 732bccb

Browse files
Use StringRef::{starts,ends}_with (NFC)
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 66e0159 commit 732bccb

File tree

14 files changed

+41
-41
lines changed

14 files changed

+41
-41
lines changed

clang-tools-extra/clang-change-namespace/ChangeNamespace.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -827,10 +827,10 @@ void ChangeNamespaceTool::replaceQualifiedSymbolInDeclContext(
827827
// "IsVisibleInNewNs" matcher.
828828
if (AliasQualifiedName != AliasName) {
829829
// The alias is defined in some namespace.
830-
assert(StringRef(AliasQualifiedName).endswith("::" + AliasName));
830+
assert(StringRef(AliasQualifiedName).ends_with("::" + AliasName));
831831
llvm::StringRef AliasNs =
832832
StringRef(AliasQualifiedName).drop_back(AliasName.size() + 2);
833-
if (!llvm::StringRef(OldNs).startswith(AliasNs))
833+
if (!llvm::StringRef(OldNs).starts_with(AliasNs))
834834
continue;
835835
}
836836
std::string NameWithAliasNamespace =
@@ -862,7 +862,7 @@ void ChangeNamespaceTool::replaceQualifiedSymbolInDeclContext(
862862
// If the new nested name in the new namespace is the same as it was in the
863863
// old namespace, we don't create replacement unless there can be ambiguity.
864864
if ((NestedName == ReplaceName && !Conflict) ||
865-
(NestedName.startswith("::") && NestedName.drop_front(2) == ReplaceName))
865+
(NestedName.starts_with("::") && NestedName.drop_front(2) == ReplaceName))
866866
return;
867867
// If the reference need to be fully-qualified, add a leading "::" unless
868868
// NewNamespace is the global namespace.
@@ -891,7 +891,7 @@ void ChangeNamespaceTool::fixTypeLoc(
891891
// a typedef type, we need to use the typedef type instead.
892892
auto IsInMovedNs = [&](const NamedDecl *D) {
893893
if (!llvm::StringRef(D->getQualifiedNameAsString())
894-
.startswith(OldNamespace + "::"))
894+
.starts_with(OldNamespace + "::"))
895895
return false;
896896
auto ExpansionLoc = Result.SourceManager->getExpansionLoc(D->getBeginLoc());
897897
if (ExpansionLoc.isInvalid())

clang-tools-extra/clang-include-fixer/IncludeFixerContext.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ std::string createQualifiedNameForReplacement(
2828
const find_all_symbols::SymbolInfo &MatchedSymbol) {
2929
// No need to add missing qualifiers if SymbolIdentifier has a global scope
3030
// operator "::".
31-
if (RawSymbolName.startswith("::"))
31+
if (RawSymbolName.starts_with("::"))
3232
return std::string(RawSymbolName);
3333

3434
std::string QualifiedName = MatchedSymbol.getQualifiedName();
@@ -42,7 +42,7 @@ std::string createQualifiedNameForReplacement(
4242
auto SymbolQualifiers = SplitQualifiers(RawSymbolName);
4343
std::string StrippedQualifiers;
4444
while (!SymbolQualifiers.empty() &&
45-
!llvm::StringRef(QualifiedName).endswith(SymbolQualifiers.back())) {
45+
!llvm::StringRef(QualifiedName).ends_with(SymbolQualifiers.back())) {
4646
StrippedQualifiers =
4747
"::" + SymbolQualifiers.back().str() + StrippedQualifiers;
4848
SymbolQualifiers.pop_back();

clang-tools-extra/clang-include-fixer/SymbolIndexManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ SymbolIndexManager::search(llvm::StringRef Identifier,
8282
Identifier.split(Names, "::");
8383

8484
bool IsFullyQualified = false;
85-
if (Identifier.startswith("::")) {
85+
if (Identifier.starts_with("::")) {
8686
Names.erase(Names.begin()); // Drop first (empty) element.
8787
IsFullyQualified = true;
8888
}

clang-tools-extra/clang-include-fixer/find-all-symbols/PathConfig.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ std::string getIncludePath(const SourceManager &SM, SourceLocation Loc,
2424
FilePath = SM.getFilename(Loc);
2525
if (FilePath.empty())
2626
return "";
27-
if (!FilePath.endswith(".inc"))
27+
if (!FilePath.ends_with(".inc"))
2828
break;
2929
FileID ID = SM.getFileID(Loc);
3030
Loc = SM.getIncludeLoc(ID);

clang-tools-extra/clang-include-fixer/plugin/IncludeFixerPlugin.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class ClangIncludeFixerPluginAction : public PluginASTAction {
5454
// Parse the extra command line args.
5555
// FIXME: This is very limited at the moment.
5656
for (StringRef Arg : Args) {
57-
if (Arg.startswith("-db="))
57+
if (Arg.starts_with("-db="))
5858
DB = Arg.substr(strlen("-db="));
59-
else if (Arg.startswith("-input="))
59+
else if (Arg.starts_with("-input="))
6060
Input = Arg.substr(strlen("-input="));
6161
}
6262

clang-tools-extra/include-cleaner/lib/Analysis.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ analyze(llvm::ArrayRef<Decl *> ASTRoots,
126126
// Since most private -> public mappings happen in a verbatim way, we
127127
// check textually here. This might go wrong in presence of symlinks or
128128
// header mappings. But that's not different than rest of the places.
129-
if (MainFile->tryGetRealPathName().endswith(PHeader))
129+
if (MainFile->tryGetRealPathName().ends_with(PHeader))
130130
continue;
131131
}
132132
}

clang-tools-extra/include-cleaner/lib/Record.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class PragmaIncludes::RecordPragma : public PPCallbacks, public CommentHandler {
277277
int CommentLine = SM.getLineNumber(CommentFID, CommentOffset);
278278

279279
if (InMainFile) {
280-
if (Pragma->startswith("keep")) {
280+
if (Pragma->starts_with("keep")) {
281281
KeepStack.push_back({CommentLine, false});
282282
} else if (Pragma->starts_with("begin_keep")) {
283283
KeepStack.push_back({CommentLine, true});
@@ -300,9 +300,10 @@ class PragmaIncludes::RecordPragma : public PPCallbacks, public CommentHandler {
300300
StringRef PublicHeader;
301301
if (Pragma->consume_front(", include ")) {
302302
// We always insert using the spelling from the pragma.
303-
PublicHeader = save(Pragma->startswith("<") || Pragma->startswith("\"")
304-
? (*Pragma)
305-
: ("\"" + *Pragma + "\"").str());
303+
PublicHeader =
304+
save(Pragma->starts_with("<") || Pragma->starts_with("\"")
305+
? (*Pragma)
306+
: ("\"" + *Pragma + "\"").str());
306307
}
307308
Out->IWYUPublic.insert({CommentUID, PublicHeader});
308309
return false;
@@ -313,11 +314,11 @@ class PragmaIncludes::RecordPragma : public PPCallbacks, public CommentHandler {
313314
}
314315
auto Filename = FE->getName();
315316
// Record export pragma.
316-
if (Pragma->startswith("export")) {
317+
if (Pragma->starts_with("export")) {
317318
ExportStack.push_back({CommentLine, CommentFID, save(Filename), false});
318-
} else if (Pragma->startswith("begin_exports")) {
319+
} else if (Pragma->starts_with("begin_exports")) {
319320
ExportStack.push_back({CommentLine, CommentFID, save(Filename), true});
320-
} else if (Pragma->startswith("end_exports")) {
321+
} else if (Pragma->starts_with("end_exports")) {
321322
// FIXME: be robust on unmatching cases. We should only pop the stack if
322323
// the begin_exports and end_exports is in the same file.
323324
if (!ExportStack.empty()) {

clang-tools-extra/modularize/CoverageChecker.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ void CoverageChecker::collectUmbrellaHeaderHeader(StringRef HeaderName) {
302302
sys::fs::current_path(PathBuf);
303303
// HeaderName will have an absolute path, so if it's the module map
304304
// directory, we remove it, also skipping trailing separator.
305-
if (HeaderName.startswith(PathBuf))
305+
if (HeaderName.starts_with(PathBuf))
306306
HeaderName = HeaderName.substr(PathBuf.size() + 1);
307307
// Save header name.
308308
ModuleMapHeadersSet.insert(ModularizeUtilities::getCanonicalPath(HeaderName));
@@ -356,8 +356,8 @@ bool CoverageChecker::collectFileSystemHeaders(StringRef IncludePath) {
356356
sys::path::append(Directory, IncludePath);
357357
if (Directory.size() == 0)
358358
Directory = ".";
359-
if (IncludePath.startswith("/") || IncludePath.startswith("\\") ||
360-
((IncludePath.size() >= 2) && (IncludePath[1] == ':'))) {
359+
if (IncludePath.starts_with("/") || IncludePath.starts_with("\\") ||
360+
((IncludePath.size() >= 2) && (IncludePath[1] == ':'))) {
361361
llvm::errs() << "error: Include path \"" << IncludePath
362362
<< "\" is not relative to the module map file.\n";
363363
return false;

clang-tools-extra/modularize/ModularizeUtilities.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ std::error_code ModularizeUtilities::loadAllHeaderListsAndDependencies() {
7575
for (auto I = InputFilePaths.begin(), E = InputFilePaths.end(); I != E; ++I) {
7676
llvm::StringRef InputPath = *I;
7777
// If it's a module map.
78-
if (InputPath.endswith(".modulemap")) {
78+
if (InputPath.ends_with(".modulemap")) {
7979
// Load the module map.
8080
if (std::error_code EC = loadModuleMap(InputPath))
8181
return EC;
82-
}
83-
else {
82+
} else {
8483
// Else we assume it's a header list and load it.
8584
if (std::error_code EC = loadSingleHeaderListsAndDependencies(InputPath)) {
8685
errs() << "modularize: error: Unable to get header list '" << InputPath
@@ -276,7 +275,7 @@ std::error_code ModularizeUtilities::loadModuleMap(
276275
StringRef DirName(Dir.getName());
277276
if (llvm::sys::path::filename(DirName) == "Modules") {
278277
DirName = llvm::sys::path::parent_path(DirName);
279-
if (DirName.endswith(".framework")) {
278+
if (DirName.ends_with(".framework")) {
280279
auto FrameworkDirOrErr = FileMgr->getDirectoryRef(DirName);
281280
if (!FrameworkDirOrErr) {
282281
// This can happen if there's a race between the above check and the
@@ -444,7 +443,7 @@ static std::string replaceDotDot(StringRef Path) {
444443
llvm::sys::path::append(Buffer, *B);
445444
++B;
446445
}
447-
if (Path.endswith("/") || Path.endswith("\\"))
446+
if (Path.ends_with("/") || Path.ends_with("\\"))
448447
Buffer.append(1, Path.back());
449448
return Buffer.c_str();
450449
}
@@ -457,7 +456,7 @@ std::string ModularizeUtilities::getCanonicalPath(StringRef FilePath) {
457456
std::string Tmp(replaceDotDot(FilePath));
458457
std::replace(Tmp.begin(), Tmp.end(), '\\', '/');
459458
StringRef Tmp2(Tmp);
460-
if (Tmp2.startswith("./"))
459+
if (Tmp2.starts_with("./"))
461460
Tmp = std::string(Tmp2.substr(2));
462461
return Tmp;
463462
}

clang-tools-extra/modularize/PreprocessorTracker.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ class PreprocessorTrackerImpl : public PreprocessorTracker {
883883
// Handle entering a header source file.
884884
void handleHeaderEntry(clang::Preprocessor &PP, llvm::StringRef HeaderPath) {
885885
// Ignore <built-in> and <command-line> to reduce message clutter.
886-
if (HeaderPath.startswith("<"))
886+
if (HeaderPath.starts_with("<"))
887887
return;
888888
HeaderHandle H = addHeader(HeaderPath);
889889
if (H != getCurrentHeaderHandle())
@@ -896,7 +896,7 @@ class PreprocessorTrackerImpl : public PreprocessorTracker {
896896
// Handle exiting a header source file.
897897
void handleHeaderExit(llvm::StringRef HeaderPath) {
898898
// Ignore <built-in> and <command-line> to reduce message clutter.
899-
if (HeaderPath.startswith("<"))
899+
if (HeaderPath.starts_with("<"))
900900
return;
901901
HeaderHandle H = findHeaderHandle(HeaderPath);
902902
HeaderHandle TH;

clang-tools-extra/pseudo/lib/cxx/CXX.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ static const char *CXXBNF =
2828

2929
// User-defined string literals look like `""suffix`.
3030
bool isStringUserDefined(const Token &Tok) {
31-
return !Tok.text().endswith("\"");
31+
return !Tok.text().ends_with("\"");
3232
}
33-
bool isCharUserDefined(const Token &Tok) { return !Tok.text().endswith("'"); }
33+
bool isCharUserDefined(const Token &Tok) { return !Tok.text().ends_with("'"); }
3434

3535
// Combinable flags describing numbers.
3636
// Clang has just one numeric_token kind, the grammar has 4.

clang-tools-extra/pseudo/lib/grammar/GrammarBNF.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class GrammarBuilder {
3333

3434
assert(llvm::all_of(Specs,
3535
[](const RuleSpec &R) {
36-
if (R.Target.endswith(OptSuffix))
36+
if (R.Target.ends_with(OptSuffix))
3737
return false;
3838
return llvm::all_of(
3939
R.Sequence, [](const RuleSpec::Element &E) {
40-
return !E.Symbol.endswith(OptSuffix);
40+
return !E.Symbol.ends_with(OptSuffix);
4141
});
4242
}) &&
4343
"Optional symbols should be eliminated!");
@@ -225,7 +225,7 @@ class GrammarBuilder {
225225
Chunk = Chunk.trim();
226226
if (Chunk.empty())
227227
continue; // skip empty
228-
if (Chunk.startswith("[") && Chunk.endswith("]")) {
228+
if (Chunk.starts_with("[") && Chunk.ends_with("]")) {
229229
if (Out.Sequence.empty())
230230
continue;
231231

@@ -241,7 +241,7 @@ class GrammarBuilder {
241241
bool parseAttributes(
242242
llvm::StringRef Content,
243243
std::vector<std::pair<llvm::StringRef, llvm::StringRef>> &Out) {
244-
assert(Content.startswith("[") && Content.endswith("]"));
244+
assert(Content.starts_with("[") && Content.ends_with("]"));
245245
auto KV = Content.drop_front().drop_back().split('=');
246246
Out.push_back({KV.first, KV.second.trim()});
247247

@@ -299,7 +299,7 @@ class GrammarBuilder {
299299
if (Elements.empty())
300300
return CB();
301301
auto Front = Elements.front();
302-
if (!Front.Symbol.endswith(OptSuffix)) {
302+
if (!Front.Symbol.ends_with(OptSuffix)) {
303303
Result.push_back(std::move(Front));
304304
eliminateOptionalTail(Elements.drop_front(1), Result, CB);
305305
Result.pop_back();

clang-tools-extra/unittests/clang-tidy/GoogleModuleTest.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class GlobalNamesInHeadersCheckTest : public ::testing::Test {
6666
"#define SOME_MACRO(x) using x\n";
6767
std::vector<ClangTidyError> Errors;
6868
std::vector<std::string> Args;
69-
if (!StringRef(Filename).endswith(".cpp")) {
69+
if (!StringRef(Filename).ends_with(".cpp")) {
7070
Args.emplace_back("-xc++-header");
7171
}
7272
test::runCheckOnCode<readability::GlobalNamesInHeadersCheck>(

lld/ELF/Arch/ARM.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -948,15 +948,15 @@ int64_t ARM::getImplicitAddend(const uint8_t *buf, RelType type) const {
948948
}
949949

950950
static bool isArmMapSymbol(const Symbol *b) {
951-
return b->getName() == "$a" || b->getName().startswith("$a.");
951+
return b->getName() == "$a" || b->getName().starts_with("$a.");
952952
}
953953

954954
static bool isThumbMapSymbol(const Symbol *s) {
955-
return s->getName() == "$t" || s->getName().startswith("$t.");
955+
return s->getName() == "$t" || s->getName().starts_with("$t.");
956956
}
957957

958958
static bool isDataMapSymbol(const Symbol *b) {
959-
return b->getName() == "$d" || b->getName().startswith("$d.");
959+
return b->getName() == "$d" || b->getName().starts_with("$d.");
960960
}
961961

962962
void elf::sortArmMappingSymbols() {
@@ -1189,7 +1189,7 @@ void elf::processArmCmseSymbols() {
11891189
// Only symbols with external linkage end up in symtab, so no need to do
11901190
// linkage checks. Only check symbol type.
11911191
for (Symbol *acleSeSym : symtab.getSymbols()) {
1192-
if (!acleSeSym->getName().startswith(ACLESESYM_PREFIX))
1192+
if (!acleSeSym->getName().starts_with(ACLESESYM_PREFIX))
11931193
continue;
11941194
// If input object build attributes do not support CMSE, error and disable
11951195
// further scanning for <sym>, __acle_se_<sym> pairs.

0 commit comments

Comments
 (0)