Skip to content

Commit

Permalink
[lld] StringRef::{starts,ends}with => {starts,ends}_with. NFC
Browse files Browse the repository at this point in the history
The latter form is now preferred to be similar to C++20 starts_with.
This replacement also removes one function call when startswith is not inlined.
  • Loading branch information
MaskRay committed Jun 5, 2023
1 parent fffa05a commit 8d85c96
Show file tree
Hide file tree
Showing 45 changed files with 143 additions and 143 deletions.
2 changes: 1 addition & 1 deletion lld/COFF/Chunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ ArrayRef<uint8_t> SectionChunk::consumeDebugMagic(ArrayRef<uint8_t> data,
if (data.size() < 4)
fatal("the section is too short: " + sectionName);

if (!sectionName.startswith(".debug$"))
if (!sectionName.starts_with(".debug$"))
fatal("invalid section: " + sectionName);

uint32_t magic = support::endian::read32le(data.data());
Expand Down
4 changes: 2 additions & 2 deletions lld/COFF/Chunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,12 @@ class SectionChunk final : public Chunk {
// True if this is a codeview debug info chunk. These will not be laid out in
// the image. Instead they will end up in the PDB, if one is requested.
bool isCodeView() const {
return getSectionName() == ".debug" || getSectionName().startswith(".debug$");
return getSectionName() == ".debug" || getSectionName().starts_with(".debug$");
}

// True if this is a DWARF debug info or exception handling chunk.
bool isDWARF() const {
return getSectionName().startswith(".debug_") || getSectionName() == ".eh_frame";
return getSectionName().starts_with(".debug_") || getSectionName() == ".eh_frame";
}

// Allow iteration over the bodies of this chunk's relocated symbols.
Expand Down
12 changes: 6 additions & 6 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ static std::string getOutputPath(StringRef path, bool isDll, bool isDriver) {

// Returns true if S matches /crtend.?\.o$/.
static bool isCrtend(StringRef s) {
if (!s.endswith(".o"))
if (!s.ends_with(".o"))
return false;
s = s.drop_back(2);
if (s.endswith("crtend"))
if (s.ends_with("crtend"))
return true;
return !s.empty() && s.drop_back().endswith("crtend");
return !s.empty() && s.drop_back().ends_with("crtend");
}

// ErrorOr is not default constructible, so it cannot be used as the type
Expand Down Expand Up @@ -354,7 +354,7 @@ void LinkerDriver::enqueueArchiveMember(const Archive::Child &c,
}

bool LinkerDriver::isDecorated(StringRef sym) {
return sym.startswith("@") || sym.contains("@@") || sym.startswith("?") ||
return sym.starts_with("@") || sym.contains("@@") || sym.starts_with("?") ||
(!ctx.config.mingw && sym.contains('@'));
}

Expand Down Expand Up @@ -1085,7 +1085,7 @@ bool LinkerDriver::run() {
void LinkerDriver::parseOrderFile(StringRef arg) {
// For some reason, the MSVC linker requires a filename to be
// preceded by "@".
if (!arg.startswith("@")) {
if (!arg.starts_with("@")) {
error("malformed /order option: '@' missing");
return;
}
Expand Down Expand Up @@ -1778,7 +1778,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
doGC = true;
} else if (s == "noref") {
doGC = false;
} else if (s == "icf" || s.startswith("icf=")) {
} else if (s == "icf" || s.starts_with("icf=")) {
icfLevel = ICFLevel::All;
} else if (s == "safeicf") {
icfLevel = ICFLevel::Safe;
Expand Down
12 changes: 6 additions & 6 deletions lld/COFF/DriverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void LinkerDriver::parseSwaprun(StringRef arg) {
else
error("/swaprun: invalid argument: " + swaprun);
// To catch trailing commas, e.g. `/spawrun:cd,`
if (newArg.empty() && arg.endswith(","))
if (newArg.empty() && arg.ends_with(","))
error("/swaprun: missing argument");
arg = newArg;
} while (!arg.empty());
Expand Down Expand Up @@ -592,7 +592,7 @@ Export LinkerDriver::parseExport(StringRef arg) {
e.isPrivate = true;
continue;
}
if (tok.startswith("@")) {
if (tok.starts_with("@")) {
int32_t ord;
if (tok.substr(1).getAsInteger(0, ord))
goto err;
Expand All @@ -616,9 +616,9 @@ static StringRef undecorate(COFFLinkerContext &ctx, StringRef sym) {
// as-is with the leading underscore (with type IMPORT_NAME).
// In MinGW mode, a decorated stdcall function gets the underscore
// removed, just like normal cdecl functions.
if (sym.startswith("_") && sym.contains('@') && !ctx.config.mingw)
if (sym.starts_with("_") && sym.contains('@') && !ctx.config.mingw)
return sym;
return sym.startswith("_") ? sym.substr(1) : sym;
return sym.starts_with("_") ? sym.substr(1) : sym;
}

// Convert stdcall/fastcall style symbols into unsuffixed symbols,
Expand All @@ -628,8 +628,8 @@ static StringRef killAt(StringRef sym, bool prefix) {
return sym;
// Strip any trailing stdcall suffix
sym = sym.substr(0, sym.find('@', 1));
if (!sym.startswith("@")) {
if (prefix && !sym.startswith("_"))
if (!sym.starts_with("@")) {
if (prefix && !sym.starts_with("_"))
return saver().save("_" + sym);
return sym;
}
Expand Down
4 changes: 2 additions & 2 deletions lld/COFF/ICF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bool ICF::isEligible(SectionChunk *c) {
return true;

// So are vtables.
if (c->sym && c->sym->getName().startswith("??_7"))
if (c->sym && c->sym->getName().starts_with("??_7"))
return true;

// Anything else not in an address-significance table is eligible.
Expand Down Expand Up @@ -132,7 +132,7 @@ bool ICF::assocEquals(const SectionChunk *a, const SectionChunk *b) {
// debug info and CFGuard metadata.
auto considerForICF = [](const SectionChunk &assoc) {
StringRef Name = assoc.getSectionName();
return !(Name.startswith(".debug") || Name == ".gfids$y" ||
return !(Name.starts_with(".debug") || Name == ".gfids$y" ||
Name == ".giats$y" || Name == ".gljmp$y");
};
auto ra = make_filter_range(a->children(), considerForICF);
Expand Down
8 changes: 4 additions & 4 deletions lld/COFF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
// and then write it to a separate .pdb file.

// Ignore DWARF debug info unless /debug is given.
if (!ctx.config.debug && name.startswith(".debug_"))
if (!ctx.config.debug && name.starts_with(".debug_"))
return nullptr;

if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
Expand All @@ -261,12 +261,12 @@ SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
else if (name == ".sxdata")
sxDataChunks.push_back(c);
else if (ctx.config.tailMerge && sec->NumberOfRelocations == 0 &&
name == ".rdata" && leaderName.startswith("??_C@"))
name == ".rdata" && leaderName.starts_with("??_C@"))
// COFF sections that look like string literal sections (i.e. no
// relocations, in .rdata, leader symbol name matches the MSVC name mangling
// for string literals) are subject to string tail merging.
MergeChunk::addSection(ctx, c);
else if (name == ".rsrc" || name.startswith(".rsrc$"))
else if (name == ".rsrc" || name.starts_with(".rsrc$"))
resourceChunks.push_back(c);
else
chunks.push_back(c);
Expand Down Expand Up @@ -366,7 +366,7 @@ Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
// everything should be fine. If something actually refers to the symbol
// (e.g. the undefined weak alias), linking will fail due to undefined
// references at the end.
if (ctx.config.mingw && name.startswith(".weak."))
if (ctx.config.mingw && name.starts_with(".weak."))
return nullptr;
return ctx.symtab.addUndefined(name, this, false);
}
Expand Down
4 changes: 2 additions & 2 deletions lld/COFF/MinGW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ bool AutoExporter::shouldExport(Defined *sym) const {
return false;

for (StringRef prefix : excludeSymbolPrefixes.keys())
if (sym->getName().startswith(prefix))
if (sym->getName().starts_with(prefix))
return false;
for (StringRef suffix : excludeSymbolSuffixes.keys())
if (sym->getName().endswith(suffix))
if (sym->getName().ends_with(suffix))
return false;

// If a corresponding __imp_ symbol exists and is defined, don't export it.
Expand Down
6 changes: 3 additions & 3 deletions lld/COFF/PDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1213,8 +1213,8 @@ void PDBLinker::addPublicsToPDB() {
// Drop the '_' prefix for x86.
if (ctx.config.machine == I386)
name = name.drop_front(1);
if (name.startswith("__profd_") || name.startswith("__profc_") ||
name.startswith("__covrec_")) {
if (name.starts_with("__profd_") || name.starts_with("__profc_") ||
name.starts_with("__covrec_")) {
return;
}
}
Expand Down Expand Up @@ -1469,7 +1469,7 @@ static void addLinkerModuleCoffGroup(PartialSection *sec,
// Somehow .idata sections & sections groups in the debug symbol stream have
// the "write" flag set. However the section header for the corresponding
// .idata section doesn't have it.
if (cgs.Name.startswith(".idata"))
if (cgs.Name.starts_with(".idata"))
cgs.Characteristics |= llvm::COFF::IMAGE_SCN_MEM_WRITE;

mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
Expand Down
18 changes: 9 additions & 9 deletions lld/COFF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ void SymbolTable::loadMinGWSymbols() {
}

if (ctx.config.autoImport) {
if (name.startswith("__imp_"))
if (name.starts_with("__imp_"))
continue;
// If we have an undefined symbol, but we have a lazy symbol we could
// load, load it.
Expand All @@ -333,7 +333,7 @@ void SymbolTable::loadMinGWSymbols() {
}

Defined *SymbolTable::impSymbol(StringRef name) {
if (name.startswith("__imp_"))
if (name.starts_with("__imp_"))
return nullptr;
return dyn_cast_or_null<Defined>(find(("__imp_" + name).str()));
}
Expand Down Expand Up @@ -456,7 +456,7 @@ void SymbolTable::reportUnresolvable() {
if (undef->getWeakAlias())
continue;
StringRef name = undef->getName();
if (name.startswith("__imp_")) {
if (name.starts_with("__imp_")) {
Symbol *imp = find(name.substr(strlen("__imp_")));
if (imp && isa<Defined>(imp))
continue;
Expand Down Expand Up @@ -504,7 +504,7 @@ void SymbolTable::resolveRemainingUndefines() {

// If we can resolve a symbol by removing __imp_ prefix, do that.
// This odd rule is for compatibility with MSVC linker.
if (name.startswith("__imp_")) {
if (name.starts_with("__imp_")) {
Symbol *imp = find(name.substr(strlen("__imp_")));
if (imp && isa<Defined>(imp)) {
auto *d = cast<Defined>(imp);
Expand Down Expand Up @@ -816,9 +816,9 @@ std::vector<Symbol *> SymbolTable::getSymsWithPrefix(StringRef prefix) {
std::vector<Symbol *> syms;
for (auto pair : symMap) {
StringRef name = pair.first.val();
if (name.startswith(prefix) || name.startswith(prefix.drop_front()) ||
name.drop_front().startswith(prefix) ||
name.drop_front().startswith(prefix.drop_front())) {
if (name.starts_with(prefix) || name.starts_with(prefix.drop_front()) ||
name.drop_front().starts_with(prefix) ||
name.drop_front().starts_with(prefix.drop_front())) {
syms.push_back(pair.second);
}
}
Expand Down Expand Up @@ -846,7 +846,7 @@ Symbol *SymbolTable::findMangle(StringRef name) {
auto findByPrefix = [&syms](const Twine &t) -> Symbol * {
std::string prefix = t.str();
for (auto *s : syms)
if (s->getName().startswith(prefix))
if (s->getName().starts_with(prefix))
return s;
return nullptr;
};
Expand All @@ -855,7 +855,7 @@ Symbol *SymbolTable::findMangle(StringRef name) {
if (ctx.config.machine != I386)
return findByPrefix("?" + name + "@@Y");

if (!name.startswith("_"))
if (!name.starts_with("_"))
return nullptr;
// Search for x86 stdcall function.
if (Symbol *s = findByPrefix(name + "@"))
Expand Down
16 changes: 8 additions & 8 deletions lld/COFF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ void Writer::fixPartialSectionChars(StringRef name, uint32_t chars) {
PartialSection *pSec = it.second;
StringRef curName = pSec->name;
if (!curName.consume_front(name) ||
(!curName.empty() && !curName.startswith("$")))
(!curName.empty() && !curName.starts_with("$")))
continue;
if (pSec->characteristics == chars)
continue;
Expand Down Expand Up @@ -769,7 +769,7 @@ bool Writer::fixGnuImportChunks() {
// with alphabetical ordering of the object files within a library.
for (auto it : partialSections) {
PartialSection *pSec = it.second;
if (!pSec->name.startswith(".idata"))
if (!pSec->name.starts_with(".idata"))
continue;

if (!pSec->chunks.empty())
Expand Down Expand Up @@ -857,9 +857,9 @@ static bool shouldStripSectionSuffix(SectionChunk *sc, StringRef name,
return false;
if (!sc || !sc->isCOMDAT())
return false;
return name.startswith(".text$") || name.startswith(".data$") ||
name.startswith(".rdata$") || name.startswith(".pdata$") ||
name.startswith(".xdata$") || name.startswith(".eh_frame$");
return name.starts_with(".text$") || name.starts_with(".data$") ||
name.starts_with(".rdata$") || name.starts_with(".pdata$") ||
name.starts_with(".xdata$") || name.starts_with(".eh_frame$");
}

void Writer::sortSections() {
Expand Down Expand Up @@ -924,7 +924,7 @@ void Writer::createSections() {
if (shouldStripSectionSuffix(sc, name, ctx.config.mingw))
name = name.split('$').first;

if (name.startswith(".tls"))
if (name.starts_with(".tls"))
tlsAlignment = std::max(tlsAlignment, c->getAlignment());

PartialSection *pSec = createPartialSection(name,
Expand Down Expand Up @@ -985,7 +985,7 @@ void Writer::createSections() {
// Move discardable sections named .debug_ to the end, after other
// discardable sections. Stripping only removes the sections named
// .debug_* - thus try to avoid leaving holes after stripping.
if (s->name.startswith(".debug_"))
if (s->name.starts_with(".debug_"))
return 3;
return 2;
}
Expand Down Expand Up @@ -1143,7 +1143,7 @@ void Writer::createExportTable() {
}
// Warn on exported deleting destructor.
for (auto e : ctx.config.exports)
if (e.sym && e.sym->getName().startswith("??_G"))
if (e.sym && e.sym->getName().starts_with("??_G"))
warn("export of deleting dtor: " + toString(ctx, *e.sym));
}

Expand Down
2 changes: 1 addition & 1 deletion lld/Common/Args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static int64_t getInteger(opt::InputArgList &args, unsigned key,

int64_t v;
StringRef s = a->getValue();
if (base == 16 && (s.startswith("0x") || s.startswith("0X")))
if (base == 16 && (s.starts_with("0x") || s.starts_with("0X")))
s = s.drop_front(2);
if (to_integer(s, v, base))
return v;
Expand Down
4 changes: 2 additions & 2 deletions lld/Common/Reproduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ std::string lld::relativeToRoot(StringRef path) {
// of the result.
SmallString<128> res;
StringRef root = path::root_name(abs);
if (root.endswith(":"))
if (root.ends_with(":"))
res = root.drop_back();
else if (root.startswith("//"))
else if (root.starts_with("//"))
res = root.substr(2);

path::append(res, path::relative_path(abs));
Expand Down
4 changes: 2 additions & 2 deletions lld/Common/Strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ using namespace llvm;
using namespace lld;

SingleStringMatcher::SingleStringMatcher(StringRef Pattern) {
if (Pattern.size() > 2 && Pattern.startswith("\"") &&
Pattern.endswith("\"")) {
if (Pattern.size() > 2 && Pattern.starts_with("\"") &&
Pattern.ends_with("\"")) {
ExactMatch = true;
ExactPattern = Pattern.substr(1, Pattern.size() - 2);
} else {
Expand Down
4 changes: 2 additions & 2 deletions lld/ELF/AArch64ErrataFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,10 @@ void AArch64Err843419Patcher::init() {
// [Symbol Value, End of section). The type, code or data, is determined by
// the mapping symbol name, $x for code, $d for data.
auto isCodeMapSymbol = [](const Symbol *b) {
return b->getName() == "$x" || b->getName().startswith("$x.");
return b->getName() == "$x" || b->getName().starts_with("$x.");
};
auto isDataMapSymbol = [](const Symbol *b) {
return b->getName() == "$d" || b->getName().startswith("$d.");
return b->getName() == "$d" || b->getName().starts_with("$d.");
};

// Collect mapping symbols for every executable InputSection.
Expand Down
6 changes: 3 additions & 3 deletions lld/ELF/ARMErrataFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,13 @@ void ARMErr657417Patcher::init() {
// [Symbol Value, End of section). The type, code or data, is determined by
// the mapping symbol name, $a for Arm code, $t for Thumb code, $d for data.
auto isArmMapSymbol = [](const Symbol *s) {
return s->getName() == "$a" || s->getName().startswith("$a.");
return s->getName() == "$a" || s->getName().starts_with("$a.");
};
auto isThumbMapSymbol = [](const Symbol *s) {
return s->getName() == "$t" || s->getName().startswith("$t.");
return s->getName() == "$t" || s->getName().starts_with("$t.");
};
auto isDataMapSymbol = [](const Symbol *s) {
return s->getName() == "$d" || s->getName().startswith("$d.");
return s->getName() == "$d" || s->getName().starts_with("$d.");
};

// Collect mapping symbols for every executable InputSection.
Expand Down

1 comment on commit 8d85c96

@kazutakahirata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Fangrui!

Please sign in to comment.