Skip to content

Commit

Permalink
[ADT] Rename SmallString::{starts,ends}with to {starts,ends}_with (#7…
Browse files Browse the repository at this point in the history
…4916)

This patch renames {starts,ends}with to {starts,ends}_with for
consistency with std::{string,string_view}::{starts,ends}_with in
C++20.  Since there are only a handful of occurrences, this patch
skips the deprecation phase and simply renames them.
  • Loading branch information
kazutakahirata committed Dec 9, 2023
1 parent 5c8755f commit cc4ecfd
Show file tree
Hide file tree
Showing 21 changed files with 33 additions and 37 deletions.
2 changes: 1 addition & 1 deletion clang-tools-extra/clang-doc/Mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
.getPresumedLoc(D->getBeginLoc())
.getFilename());
IsFileInRootDir = false;
if (RootDir.empty() || !File.startswith(RootDir))
if (RootDir.empty() || !File.starts_with(RootDir))
return File;
IsFileInRootDir = true;
llvm::SmallString<128> Prefix(RootDir);
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/modularize/ModuleAssistant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ static bool addModuleDescription(Module *RootModule,
llvm::SmallString<256> NativePath, NativePrefix;
llvm::sys::path::native(HeaderFilePath, NativePath);
llvm::sys::path::native(HeaderPrefix, NativePrefix);
if (NativePath.startswith(NativePrefix))
if (NativePath.starts_with(NativePrefix))
FilePath = std::string(NativePath.substr(NativePrefix.size() + 1));
else
FilePath = std::string(HeaderFilePath);
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/MicrosoftMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3809,14 +3809,14 @@ void MicrosoftMangleContextImpl::mangleCXXRTTICompleteObjectLocator(
llvm::raw_svector_ostream Stream(VFTableMangling);
mangleCXXVFTable(Derived, BasePath, Stream);

if (VFTableMangling.startswith("??@")) {
assert(VFTableMangling.endswith("@"));
if (VFTableMangling.starts_with("??@")) {
assert(VFTableMangling.ends_with("@"));
Out << VFTableMangling << "??_R4@";
return;
}

assert(VFTableMangling.startswith("??_7") ||
VFTableMangling.startswith("??_S"));
assert(VFTableMangling.starts_with("??_7") ||
VFTableMangling.starts_with("??_S"));

Out << "??_R4" << VFTableMangling.str().drop_front(4);
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Basic/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
// where both are valid examples of the same platform+environment but in the
// variant (2) the simulator is hardcoded as part of the platform name. Both
// forms above should match for "iossimulator" requirement.
if (Target.getTriple().isOSDarwin() && PlatformEnv.endswith("simulator"))
if (Target.getTriple().isOSDarwin() && PlatformEnv.ends_with("simulator"))
return PlatformEnv == Feature || CmpPlatformEnv(PlatformEnv, Feature);

return PlatformEnv == Feature;
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CrossTU/CrossTranslationUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ CrossTranslationUnitContext::ASTLoader::load(StringRef Identifier) {
// Normalize by removing relative path components.
llvm::sys::path::remove_dots(Path, /*remove_dot_dot*/ true, PathStyle);

if (Path.endswith(".ast"))
if (Path.ends_with(".ast"))
return loadFromDump(Path);
else
return loadFromSource(Path);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename,
// (or /Library/Logs/DiagnosticReports for root) and has the filename pattern
// clang-<VERSION>_<YYYY-MM-DD-HHMMSS>_<hostname>.crash.
path::home_directory(CrashDiagDir);
if (CrashDiagDir.startswith("/var/root"))
if (CrashDiagDir.starts_with("/var/root"))
CrashDiagDir = "/";
path::append(CrashDiagDir, "Library/Logs/DiagnosticReports");
int PID =
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Driver/ToolChains/Darwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
// rpaths. This is currently true from this place, but we need to be
// careful if this function is ever called before user's rpaths are emitted.
if (Opts & RLO_AddRPath) {
assert(DarwinLibName.endswith(".dylib") && "must be a dynamic library");
assert(DarwinLibName.ends_with(".dylib") && "must be a dynamic library");

// Add @executable_path to rpath to support having the dylib copied with
// the executable.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Lex/ModuleMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,7 @@ void ModuleMapParser::diagnosePrivateModules(SourceLocation ExplicitLoc,
continue;

SmallString<128> FullName(ActiveModule->getFullModuleName());
if (!FullName.startswith(M->Name) && !FullName.endswith("Private"))
if (!FullName.starts_with(M->Name) && !FullName.ends_with("Private"))
continue;
SmallString<128> FixedPrivModDecl;
SmallString<128> Canonical(M->Name);
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaCodeComplete.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10112,7 +10112,7 @@ void Sema::CodeCompleteIncludedFile(llvm::StringRef Dir, bool Angled) {
const StringRef &Dirname = llvm::sys::path::filename(Dir);
const bool isQt = Dirname.startswith("Qt") || Dirname == "ActiveQt";
const bool ExtensionlessHeaders =
IsSystem || isQt || Dir.endswith(".framework/Headers");
IsSystem || isQt || Dir.ends_with(".framework/Headers");
std::error_code EC;
unsigned Count = 0;
for (auto It = FS.dir_begin(Dir, EC);
Expand Down
2 changes: 1 addition & 1 deletion lld/COFF/PDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ void PDBLinker::pdbMakeAbsolute(SmallVectorImpl<char> &fileName) {
// decide that it's a unix path if we're fairly certain. Specifically, if
// it starts with a forward slash.
SmallString<128> absoluteFileName = ctx.config.pdbSourcePath;
sys::path::Style guessedStyle = absoluteFileName.startswith("/")
sys::path::Style guessedStyle = absoluteFileName.starts_with("/")
? sys::path::Style::posix
: sys::path::Style::windows;
sys::path::append(absoluteFileName, guessedStyle, fileName);
Expand Down
2 changes: 1 addition & 1 deletion lld/MachO/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ std::string ObjFile::sourceFile() const {
StringRef sep = sys::path::get_separator();
// We don't use `path::append` here because we want an empty `dir` to result
// in an absolute path. `append` would give us a relative path for that case.
if (!dir.endswith(sep))
if (!dir.ends_with(sep))
dir += sep;
return (dir + unitName).str();
}
Expand Down
2 changes: 1 addition & 1 deletion lldb/source/Commands/CommandCompletions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ static void DiskFilesOrDirectories(const llvm::Twine &partial_name,
llvm::StringRef SearchDir;
llvm::StringRef PartialItem;

if (CompletionBuffer.startswith("~")) {
if (CompletionBuffer.starts_with("~")) {
llvm::StringRef Buffer = CompletionBuffer;
size_t FirstSep =
Buffer.find_if([](char c) { return path::is_separator(c); });
Expand Down
12 changes: 4 additions & 8 deletions llvm/include/llvm/ADT/SmallString.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,11 @@ class SmallString : public SmallVector<char, InternalLen> {
/// @name String Predicates
/// @{

/// startswith - Check if this string starts with the given \p Prefix.
bool startswith(StringRef Prefix) const {
return str().startswith(Prefix);
}
/// starts_with - Check if this string starts with the given \p Prefix.
bool starts_with(StringRef Prefix) const { return str().starts_with(Prefix); }

/// endswith - Check if this string ends with the given \p Suffix.
bool endswith(StringRef Suffix) const {
return str().endswith(Suffix);
}
/// ends_with - Check if this string ends with the given \p Suffix.
bool ends_with(StringRef Suffix) const { return str().ends_with(Suffix); }

/// @}
/// @name String Searching
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ MCSection *TargetLoweringObjectFileELF::getSectionForMachineBasicBlock(
} else {
Name += FunctionSectionName;
if (TM.getUniqueBasicBlockSectionNames()) {
if (!Name.endswith("."))
if (!Name.ends_with("."))
Name += ".";
Name += MBB.getSymbol()->getName();
} else {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Support/Windows/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16,

// If the path is a long path, mangled into forward slashes, normalize
// back to backslashes here.
if (Path8Str.startswith("//?/"))
if (Path8Str.starts_with("//?/"))
llvm::sys::path::native(Path8Str, path::Style::windows_backslash);

if (std::error_code EC = UTF8ToUTF16(Path8Str, Path16))
Expand All @@ -96,7 +96,7 @@ std::error_code widenPath(const Twine &Path8, SmallVectorImpl<wchar_t> &Path16,
const char *const LongPathPrefix = "\\\\?\\";

if ((Path16.size() + CurPathLen) < MaxPathLen ||
Path8Str.startswith(LongPathPrefix))
Path8Str.starts_with(LongPathPrefix))
return std::error_code();

if (!IsAbsolute) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/dsymutil/DebugMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ MappingTraits<dsymutil::DebugMapObject>::YamlDMO::denormalize(IO &IO) {
}

uint8_t Type = MachO::N_OSO;
if (Path.endswith(".dylib")) {
if (Path.ends_with(".dylib")) {
// FIXME: find a more resilient way
Type = MachO::N_LIB;
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-cov/CodeCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ void CodeCoverageTool::remapPathNames(const CoverageMapping &Coverage) {
SmallString<128> NativeFilename;
sys::path::native(Filename, NativeFilename);
sys::path::remove_dots(NativeFilename, true);
if (NativeFilename.startswith(RemapFrom)) {
if (NativeFilename.starts_with(RemapFrom)) {
RemappedFilenames[Filename] =
RemapTo + NativeFilename.substr(RemapFrom.size()).str();
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-cov/CoverageReport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void CoverageReport::render(const FileCoverageSummary &File,
sys::path::native(FileName);

// remove_dots will remove trailing slash, so we need to check before it.
auto IsDir = FileName.endswith(sys::path::get_separator());
auto IsDir = FileName.ends_with(sys::path::get_separator());
sys::path::remove_dots(FileName, /*remove_dot_dot=*/true);
if (IsDir)
FileName += sys::path::get_separator();
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-cov/SourceCoverageViewHTML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ struct CoveragePrinterHTMLDirectory::Reporter : public DirectoryCoverageReport {
sys::path::native(LinkTextStr);

// remove_dots will remove trailing slash, so we need to check before it.
auto IsDir = LinkTextStr.endswith(sys::path::get_separator());
auto IsDir = LinkTextStr.ends_with(sys::path::get_separator());
sys::path::remove_dots(LinkTextStr, /*remove_dot_dot=*/true);

SmallString<128> LinkTargetStr(LinkTextStr);
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-ml/llvm-ml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static Triple GetTriple(StringRef ProgName, opt::InputArgList &Args) {
StringRef DefaultBitness = "32";
SmallString<255> Program = ProgName;
sys::path::replace_extension(Program, "");
if (Program.endswith("ml64"))
if (Program.ends_with("ml64"))
DefaultBitness = "64";

StringRef TripleName =
Expand Down
12 changes: 6 additions & 6 deletions llvm/unittests/Support/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ TEST_F(FileSystemTest, TempFiles) {
int FD2;
SmallString<64> TempPath2;
ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD2, TempPath2));
ASSERT_TRUE(TempPath2.endswith(".temp"));
ASSERT_TRUE(TempPath2.ends_with(".temp"));
ASSERT_NE(TempPath.str(), TempPath2.str());

fs::file_status A, B;
Expand All @@ -908,7 +908,7 @@ TEST_F(FileSystemTest, TempFiles) {

SmallString<64> TempPath3;
ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "", TempPath3));
ASSERT_FALSE(TempPath3.endswith("."));
ASSERT_FALSE(TempPath3.ends_with("."));
FileRemover Cleanup3(TempPath3);

// Create a hard link to Temp1.
Expand Down Expand Up @@ -1515,13 +1515,13 @@ TEST(Support, NormalizePath) {
const char *Path7a = "~/aaa";
SmallString<64> Path7(Path7a);
path::native(Path7, path::Style::windows_backslash);
EXPECT_TRUE(Path7.endswith("\\aaa"));
EXPECT_TRUE(Path7.startswith(PathHome));
EXPECT_TRUE(Path7.ends_with("\\aaa"));
EXPECT_TRUE(Path7.starts_with(PathHome));
EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1));
Path7 = Path7a;
path::native(Path7, path::Style::windows_slash);
EXPECT_TRUE(Path7.endswith("/aaa"));
EXPECT_TRUE(Path7.startswith(PathHome));
EXPECT_TRUE(Path7.ends_with("/aaa"));
EXPECT_TRUE(Path7.starts_with(PathHome));
EXPECT_EQ(Path7.size(), PathHome.size() + strlen(Path7a + 1));

const char *Path8a = "~";
Expand Down

0 comments on commit cc4ecfd

Please sign in to comment.