[BOLT] 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.
This commit is contained in:
Kazu Hirata
2023-12-13 23:34:49 -08:00
parent d5953e3e30
commit ad8fd5b185
12 changed files with 42 additions and 41 deletions

View File

@@ -900,8 +900,8 @@ public:
/// Return true if \p SymbolName was generated internally and was not present
/// in the input binary.
bool isInternalSymbolName(const StringRef Name) {
return Name.startswith("SYMBOLat") || Name.startswith("DATAat") ||
Name.startswith("HOLEat");
return Name.starts_with("SYMBOLat") || Name.starts_with("DATAat") ||
Name.starts_with("HOLEat");
}
MCSymbol *getHotTextStartSymbol() const {

View File

@@ -1086,7 +1086,7 @@ void BinaryContext::generateSymbolHashes() {
auto isPadding = [](const BinaryData &BD) {
StringRef Contents = BD.getSection().getContents();
StringRef SymData = Contents.substr(BD.getOffset(), BD.getSize());
return (BD.getName().startswith("HOLEat") ||
return (BD.getName().starts_with("HOLEat") ||
SymData.find_first_not_of(0) == StringRef::npos);
};
@@ -1326,8 +1326,8 @@ void BinaryContext::postProcessSymbolTable() {
bool Valid = true;
for (auto &Entry : BinaryDataMap) {
BinaryData *BD = Entry.second;
if ((BD->getName().startswith("SYMBOLat") ||
BD->getName().startswith("DATAat")) &&
if ((BD->getName().starts_with("SYMBOLat") ||
BD->getName().starts_with("DATAat")) &&
!BD->getParent() && !BD->getSize() && !BD->isAbsolute() &&
BD->getSection()) {
errs() << "BOLT-WARNING: zero-sized top level symbol: " << *BD << "\n";
@@ -1410,9 +1410,9 @@ void BinaryContext::fixBinaryDataHoles() {
auto isNotHole = [&Section](const binary_data_iterator &Itr) {
BinaryData *BD = Itr->second;
bool isHole = (!BD->getParent() && !BD->getSize() && BD->isObject() &&
(BD->getName().startswith("SYMBOLat0x") ||
BD->getName().startswith("DATAat0x") ||
BD->getName().startswith("ANONYMOUS")));
(BD->getName().starts_with("SYMBOLat0x") ||
BD->getName().starts_with("DATAat0x") ||
BD->getName().starts_with("ANONYMOUS")));
return !isHole && BD->getSection() == Section && !BD->getParent();
};
@@ -1818,14 +1818,14 @@ MarkerSymType BinaryContext::getMarkerType(const SymbolRef &Symbol) const {
if (*TypeOrError != SymbolRef::ST_Unknown)
return MarkerSymType::NONE;
if (*NameOrError == "$x" || NameOrError->startswith("$x."))
if (*NameOrError == "$x" || NameOrError->starts_with("$x."))
return MarkerSymType::CODE;
// $x<ISA>
if (isRISCV() && NameOrError->startswith("$x"))
if (isRISCV() && NameOrError->starts_with("$x"))
return MarkerSymType::CODE;
if (*NameOrError == "$d" || NameOrError->startswith("$d."))
if (*NameOrError == "$d" || NameOrError->starts_with("$d."))
return MarkerSymType::DATA;
return MarkerSymType::NONE;

View File

@@ -65,7 +65,7 @@ bool BinaryData::hasNameRegex(StringRef NameRegex) const {
bool BinaryData::nameStartsWith(StringRef Prefix) const {
for (const MCSymbol *Symbol : Symbols)
if (Symbol->getName().startswith(Prefix))
if (Symbol->getName().starts_with(Prefix))
return true;
return false;
}

View File

@@ -460,7 +460,7 @@ IndirectCallPromotion::maybeGetHotJumpTableTargets(BinaryBasicBlock &BB,
if (AccessInfo.MemoryObject) {
// Deal with bad/stale data
if (!AccessInfo.MemoryObject->getName().startswith(
if (!AccessInfo.MemoryObject->getName().starts_with(
"JUMP_TABLE/" + Function.getOneName().str()))
return JumpTableInfoType();
Index =

View File

@@ -408,14 +408,14 @@ bool ReorderData::markUnmoveableSymbols(BinaryContext &BC,
// suffix in another.
auto isPrivate = [&](const BinaryData *BD) {
auto Prefix = std::string("PG") + BC.AsmInfo->getPrivateGlobalPrefix();
return BD->getName().startswith(Prefix.str());
return BD->getName().starts_with(Prefix.str());
};
auto Range = BC.getBinaryDataForSection(Section);
bool FoundUnmoveable = false;
for (auto Itr = Range.begin(); Itr != Range.end(); ++Itr) {
BinaryData *Next =
std::next(Itr) != Range.end() ? std::next(Itr)->second : nullptr;
if (Itr->second->getName().startswith("PG.")) {
if (Itr->second->getName().starts_with("PG.")) {
BinaryData *Prev =
Itr != Range.begin() ? std::prev(Itr)->second : nullptr;
bool PrevIsPrivate = Prev && isPrivate(Prev);

View File

@@ -1416,12 +1416,12 @@ bool ShrinkWrapping::foldIdenticalSplitEdges() {
bool Changed = false;
for (auto Iter = BF.begin(); Iter != BF.end(); ++Iter) {
BinaryBasicBlock &BB = *Iter;
if (!BB.getName().startswith(".LSplitEdge"))
if (!BB.getName().starts_with(".LSplitEdge"))
continue;
for (BinaryBasicBlock &RBB : llvm::reverse(BF)) {
if (&RBB == &BB)
break;
if (!RBB.getName().startswith(".LSplitEdge") || !RBB.isValid() ||
if (!RBB.getName().starts_with(".LSplitEdge") || !RBB.isValid() ||
!isIdenticalSplitEdgeBB(BC, *Iter, RBB))
continue;
assert(RBB.pred_size() == 1 && "Invalid split edge BB");

View File

@@ -1915,7 +1915,7 @@ DataAggregator::parseMMapEvent() {
// PERF_RECORD_MMAP2 <pid>/<tid>: [<hexbase>(<hexsize>) .*]: .* <file_name>
StringRef FileName = Line.rsplit(FieldSeparator).second;
if (FileName.startswith("//") || FileName.startswith("[")) {
if (FileName.starts_with("//") || FileName.starts_with("[")) {
consumeRestOfLine();
return std::make_pair(StringRef(), ParsedInfo);
}
@@ -2168,7 +2168,7 @@ DataAggregator::getFileNameForBuildID(StringRef FileBuildID) {
continue;
}
if (IDPair->second.startswith(FileBuildID)) {
if (IDPair->second.starts_with(FileBuildID)) {
FileName = sys::path::filename(IDPair->first);
break;
}

View File

@@ -55,7 +55,7 @@ bool hasVolatileName(const BinaryFunction &BF) {
/// Return standard escaped name of the function possibly renamed by BOLT.
std::string normalizeName(StringRef NameRef) {
// Strip "PG." prefix used for globalized locals.
NameRef = NameRef.startswith("PG.") ? NameRef.substr(2) : NameRef;
NameRef = NameRef.starts_with("PG.") ? NameRef.substr(2) : NameRef;
return getEscapedName(NameRef);
}

View File

@@ -39,7 +39,7 @@ namespace bolt {
bool YAMLProfileReader::isYAML(const StringRef Filename) {
if (auto MB = MemoryBuffer::getFileOrSTDIN(Filename)) {
StringRef Buffer = (*MB)->getBuffer();
return Buffer.startswith("---\n");
return Buffer.starts_with("---\n");
} else {
report_error(Filename, MB.getError());
}

View File

@@ -120,7 +120,7 @@ void ExecutableFileMemoryManager::updateSection(
}
if (!IsCode && (SectionName == ".strtab" || SectionName == ".symtab" ||
SectionName == "" || SectionName.startswith(".rela.")))
SectionName == "" || SectionName.starts_with(".rela.")))
return;
SmallVector<char, 256> Buf;
@@ -139,7 +139,7 @@ void ExecutableFileMemoryManager::updateSection(
}
BinarySection *Section = nullptr;
if (!OrgSecPrefix.empty() && SectionName.startswith(OrgSecPrefix)) {
if (!OrgSecPrefix.empty() && SectionName.starts_with(OrgSecPrefix)) {
// Update the original section contents.
ErrorOr<BinarySection &> OrgSection =
BC.getUniqueSectionByName(SectionName.substr(OrgSecPrefix.length()));

View File

@@ -538,7 +538,7 @@ Error RewriteInstance::discoverStorage() {
if (!opts::HeatmapMode &&
!(opts::AggregateOnly && BAT->enabledFor(InputFile)) &&
(SectionName.startswith(getOrgSecPrefix()) ||
(SectionName.starts_with(getOrgSecPrefix()) ||
SectionName == getBOLTTextSectionName()))
return createStringError(
errc::function_not_supported,
@@ -778,12 +778,12 @@ void RewriteInstance::discoverFileObjects() {
std::unordered_map<SymbolRef, StringRef, SymbolRefHash> SymbolToFileName;
for (const ELFSymbolRef &Symbol : InputFile->symbols()) {
Expected<StringRef> NameOrError = Symbol.getName();
if (NameOrError && NameOrError->startswith("__asan_init")) {
if (NameOrError && NameOrError->starts_with("__asan_init")) {
errs() << "BOLT-ERROR: input file was compiled or linked with sanitizer "
"support. Cannot optimize.\n";
exit(1);
}
if (NameOrError && NameOrError->startswith("__llvm_coverage_mapping")) {
if (NameOrError && NameOrError->starts_with("__llvm_coverage_mapping")) {
errs() << "BOLT-ERROR: input file was compiled or linked with coverage "
"support. Cannot optimize.\n";
exit(1);
@@ -938,9 +938,10 @@ void RewriteInstance::discoverFileObjects() {
/// It is possible we are seeing a globalized local. LLVM might treat it as
/// a local if it has a "private global" prefix, e.g. ".L". Thus we have to
/// change the prefix to enforce global scope of the symbol.
std::string Name = SymName.startswith(BC->AsmInfo->getPrivateGlobalPrefix())
? "PG" + std::string(SymName)
: std::string(SymName);
std::string Name =
SymName.starts_with(BC->AsmInfo->getPrivateGlobalPrefix())
? "PG" + std::string(SymName)
: std::string(SymName);
// Disambiguate all local symbols before adding to symbol table.
// Since we don't know if we will see a global with the same name,
@@ -2723,8 +2724,8 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection,
BD->nameStartsWith(SymbolName) ||
BD->nameStartsWith("PG" + SymbolName) ||
(BD->nameStartsWith("ANONYMOUS") &&
(BD->getSectionName().startswith(".plt") ||
BD->getSectionName().endswith(".plt")))) &&
(BD->getSectionName().starts_with(".plt") ||
BD->getSectionName().ends_with(".plt")))) &&
"BOLT symbol names of all non-section relocations must match up "
"with symbol names referenced in the relocation");
@@ -2740,7 +2741,7 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection,
// in relocation sections can get through here too, from .plt.
assert(
(IsAArch64 || BC->isRISCV() || IsSectionRelocation ||
BC->getSectionNameForAddress(SymbolAddress)->startswith(".plt")) &&
BC->getSectionNameForAddress(SymbolAddress)->starts_with(".plt")) &&
"known symbols should not resolve to anonymous locals");
if (IsSectionRelocation) {
@@ -2757,7 +2758,7 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection,
Name = SymbolName;
} else {
if (StringRef(SymbolName)
.startswith(BC->AsmInfo->getPrivateGlobalPrefix()))
.starts_with(BC->AsmInfo->getPrivateGlobalPrefix()))
Name = NR.uniquify("PG" + SymbolName);
else
Name = NR.uniquify(SymbolName);
@@ -3464,8 +3465,8 @@ std::vector<BinarySection *> RewriteInstance::getCodeSections() {
// ".text.cold.T", ".text.cold.T-1", ... ".text.cold.1", ".text.cold"
// - if opts::HotFunctionsAtEnd is false, we want order
// ".text.cold", ".text.cold.1", ... ".text.cold.T-1", ".text.cold.T"
if (A->getName().startswith(BC->getColdCodeSectionName()) &&
B->getName().startswith(BC->getColdCodeSectionName())) {
if (A->getName().starts_with(BC->getColdCodeSectionName()) &&
B->getName().starts_with(BC->getColdCodeSectionName())) {
if (A->getName().size() != B->getName().size())
return (opts::HotFunctionsAtEnd)
? (A->getName().size() > B->getName().size())
@@ -5653,16 +5654,16 @@ bool RewriteInstance::willOverwriteSection(StringRef SectionName) {
}
bool RewriteInstance::isDebugSection(StringRef SectionName) {
if (SectionName.startswith(".debug_") || SectionName.startswith(".zdebug_") ||
SectionName == ".gdb_index" || SectionName == ".stab" ||
SectionName == ".stabstr")
if (SectionName.starts_with(".debug_") ||
SectionName.starts_with(".zdebug_") || SectionName == ".gdb_index" ||
SectionName == ".stab" || SectionName == ".stabstr")
return true;
return false;
}
bool RewriteInstance::isKSymtabSection(StringRef SectionName) {
if (SectionName.startswith("__ksymtab"))
if (SectionName.starts_with("__ksymtab"))
return true;
return false;

View File

@@ -253,7 +253,7 @@ bool isYAML(const StringRef Filename) {
if (std::error_code EC = MB.getError())
report_error(Filename, EC);
StringRef Buffer = MB.get()->getBuffer();
if (Buffer.startswith("---\n"))
if (Buffer.starts_with("---\n"))
return true;
return false;
}
@@ -279,7 +279,7 @@ void mergeLegacyProfiles(const SmallVectorImpl<std::string> &Filenames) {
{
std::lock_guard<std::mutex> Lock(BoltedCollectionMutex);
// Check if the string "boltedcollection" is in the first line
if (Buf.startswith("boltedcollection\n")) {
if (Buf.starts_with("boltedcollection\n")) {
if (!BoltedCollection.value_or(true))
report_error(
Filename,