Skip to content

Commit 8d85c96

Browse files
committed
[lld] StringRef::{starts,ends}with => {starts,ends}_with. NFC
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.
1 parent fffa05a commit 8d85c96

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+143
-143
lines changed

lld/COFF/Chunks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ ArrayRef<uint8_t> SectionChunk::consumeDebugMagic(ArrayRef<uint8_t> data,
703703
if (data.size() < 4)
704704
fatal("the section is too short: " + sectionName);
705705

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

709709
uint32_t magic = support::endian::read32le(data.data());

lld/COFF/Chunks.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,12 @@ class SectionChunk final : public Chunk {
261261
// True if this is a codeview debug info chunk. These will not be laid out in
262262
// the image. Instead they will end up in the PDB, if one is requested.
263263
bool isCodeView() const {
264-
return getSectionName() == ".debug" || getSectionName().startswith(".debug$");
264+
return getSectionName() == ".debug" || getSectionName().starts_with(".debug$");
265265
}
266266

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

272272
// Allow iteration over the bodies of this chunk's relocated symbols.

lld/COFF/Driver.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ static std::string getOutputPath(StringRef path, bool isDll, bool isDriver) {
113113

114114
// Returns true if S matches /crtend.?\.o$/.
115115
static bool isCrtend(StringRef s) {
116-
if (!s.endswith(".o"))
116+
if (!s.ends_with(".o"))
117117
return false;
118118
s = s.drop_back(2);
119-
if (s.endswith("crtend"))
119+
if (s.ends_with("crtend"))
120120
return true;
121-
return !s.empty() && s.drop_back().endswith("crtend");
121+
return !s.empty() && s.drop_back().ends_with("crtend");
122122
}
123123

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

356356
bool LinkerDriver::isDecorated(StringRef sym) {
357-
return sym.startswith("@") || sym.contains("@@") || sym.startswith("?") ||
357+
return sym.starts_with("@") || sym.contains("@@") || sym.starts_with("?") ||
358358
(!ctx.config.mingw && sym.contains('@'));
359359
}
360360

@@ -1085,7 +1085,7 @@ bool LinkerDriver::run() {
10851085
void LinkerDriver::parseOrderFile(StringRef arg) {
10861086
// For some reason, the MSVC linker requires a filename to be
10871087
// preceded by "@".
1088-
if (!arg.startswith("@")) {
1088+
if (!arg.starts_with("@")) {
10891089
error("malformed /order option: '@' missing");
10901090
return;
10911091
}
@@ -1778,7 +1778,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
17781778
doGC = true;
17791779
} else if (s == "noref") {
17801780
doGC = false;
1781-
} else if (s == "icf" || s.startswith("icf=")) {
1781+
} else if (s == "icf" || s.starts_with("icf=")) {
17821782
icfLevel = ICFLevel::All;
17831783
} else if (s == "safeicf") {
17841784
icfLevel = ICFLevel::Safe;

lld/COFF/DriverUtils.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ void LinkerDriver::parseSwaprun(StringRef arg) {
322322
else
323323
error("/swaprun: invalid argument: " + swaprun);
324324
// To catch trailing commas, e.g. `/spawrun:cd,`
325-
if (newArg.empty() && arg.endswith(","))
325+
if (newArg.empty() && arg.ends_with(","))
326326
error("/swaprun: missing argument");
327327
arg = newArg;
328328
} while (!arg.empty());
@@ -592,7 +592,7 @@ Export LinkerDriver::parseExport(StringRef arg) {
592592
e.isPrivate = true;
593593
continue;
594594
}
595-
if (tok.startswith("@")) {
595+
if (tok.starts_with("@")) {
596596
int32_t ord;
597597
if (tok.substr(1).getAsInteger(0, ord))
598598
goto err;
@@ -616,9 +616,9 @@ static StringRef undecorate(COFFLinkerContext &ctx, StringRef sym) {
616616
// as-is with the leading underscore (with type IMPORT_NAME).
617617
// In MinGW mode, a decorated stdcall function gets the underscore
618618
// removed, just like normal cdecl functions.
619-
if (sym.startswith("_") && sym.contains('@') && !ctx.config.mingw)
619+
if (sym.starts_with("_") && sym.contains('@') && !ctx.config.mingw)
620620
return sym;
621-
return sym.startswith("_") ? sym.substr(1) : sym;
621+
return sym.starts_with("_") ? sym.substr(1) : sym;
622622
}
623623

624624
// Convert stdcall/fastcall style symbols into unsuffixed symbols,
@@ -628,8 +628,8 @@ static StringRef killAt(StringRef sym, bool prefix) {
628628
return sym;
629629
// Strip any trailing stdcall suffix
630630
sym = sym.substr(0, sym.find('@', 1));
631-
if (!sym.startswith("@")) {
632-
if (prefix && !sym.startswith("_"))
631+
if (!sym.starts_with("@")) {
632+
if (prefix && !sym.starts_with("_"))
633633
return saver().save("_" + sym);
634634
return sym;
635635
}

lld/COFF/ICF.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ bool ICF::isEligible(SectionChunk *c) {
9393
return true;
9494

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

9999
// Anything else not in an address-significance table is eligible.
@@ -132,7 +132,7 @@ bool ICF::assocEquals(const SectionChunk *a, const SectionChunk *b) {
132132
// debug info and CFGuard metadata.
133133
auto considerForICF = [](const SectionChunk &assoc) {
134134
StringRef Name = assoc.getSectionName();
135-
return !(Name.startswith(".debug") || Name == ".gfids$y" ||
135+
return !(Name.starts_with(".debug") || Name == ".gfids$y" ||
136136
Name == ".giats$y" || Name == ".gljmp$y");
137137
};
138138
auto ra = make_filter_range(a->children(), considerForICF);

lld/COFF/InputFiles.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
237237
// and then write it to a separate .pdb file.
238238

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

243243
if (sec->Characteristics & llvm::COFF::IMAGE_SCN_LNK_REMOVE)
@@ -261,12 +261,12 @@ SectionChunk *ObjFile::readSection(uint32_t sectionNumber,
261261
else if (name == ".sxdata")
262262
sxDataChunks.push_back(c);
263263
else if (ctx.config.tailMerge && sec->NumberOfRelocations == 0 &&
264-
name == ".rdata" && leaderName.startswith("??_C@"))
264+
name == ".rdata" && leaderName.starts_with("??_C@"))
265265
// COFF sections that look like string literal sections (i.e. no
266266
// relocations, in .rdata, leader symbol name matches the MSVC name mangling
267267
// for string literals) are subject to string tail merging.
268268
MergeChunk::addSection(ctx, c);
269-
else if (name == ".rsrc" || name.startswith(".rsrc$"))
269+
else if (name == ".rsrc" || name.starts_with(".rsrc$"))
270270
resourceChunks.push_back(c);
271271
else
272272
chunks.push_back(c);
@@ -366,7 +366,7 @@ Symbol *ObjFile::createRegular(COFFSymbolRef sym) {
366366
// everything should be fine. If something actually refers to the symbol
367367
// (e.g. the undefined weak alias), linking will fail due to undefined
368368
// references at the end.
369-
if (ctx.config.mingw && name.startswith(".weak."))
369+
if (ctx.config.mingw && name.starts_with(".weak."))
370370
return nullptr;
371371
return ctx.symtab.addUndefined(name, this, false);
372372
}

lld/COFF/MinGW.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ bool AutoExporter::shouldExport(Defined *sym) const {
144144
return false;
145145

146146
for (StringRef prefix : excludeSymbolPrefixes.keys())
147-
if (sym->getName().startswith(prefix))
147+
if (sym->getName().starts_with(prefix))
148148
return false;
149149
for (StringRef suffix : excludeSymbolSuffixes.keys())
150-
if (sym->getName().endswith(suffix))
150+
if (sym->getName().ends_with(suffix))
151151
return false;
152152

153153
// If a corresponding __imp_ symbol exists and is defined, don't export it.

lld/COFF/PDB.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,8 +1213,8 @@ void PDBLinker::addPublicsToPDB() {
12131213
// Drop the '_' prefix for x86.
12141214
if (ctx.config.machine == I386)
12151215
name = name.drop_front(1);
1216-
if (name.startswith("__profd_") || name.startswith("__profc_") ||
1217-
name.startswith("__covrec_")) {
1216+
if (name.starts_with("__profd_") || name.starts_with("__profc_") ||
1217+
name.starts_with("__covrec_")) {
12181218
return;
12191219
}
12201220
}
@@ -1469,7 +1469,7 @@ static void addLinkerModuleCoffGroup(PartialSection *sec,
14691469
// Somehow .idata sections & sections groups in the debug symbol stream have
14701470
// the "write" flag set. However the section header for the corresponding
14711471
// .idata section doesn't have it.
1472-
if (cgs.Name.startswith(".idata"))
1472+
if (cgs.Name.starts_with(".idata"))
14731473
cgs.Characteristics |= llvm::COFF::IMAGE_SCN_MEM_WRITE;
14741474

14751475
mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(

lld/COFF/SymbolTable.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void SymbolTable::loadMinGWSymbols() {
317317
}
318318

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

335335
Defined *SymbolTable::impSymbol(StringRef name) {
336-
if (name.startswith("__imp_"))
336+
if (name.starts_with("__imp_"))
337337
return nullptr;
338338
return dyn_cast_or_null<Defined>(find(("__imp_" + name).str()));
339339
}
@@ -456,7 +456,7 @@ void SymbolTable::reportUnresolvable() {
456456
if (undef->getWeakAlias())
457457
continue;
458458
StringRef name = undef->getName();
459-
if (name.startswith("__imp_")) {
459+
if (name.starts_with("__imp_")) {
460460
Symbol *imp = find(name.substr(strlen("__imp_")));
461461
if (imp && isa<Defined>(imp))
462462
continue;
@@ -504,7 +504,7 @@ void SymbolTable::resolveRemainingUndefines() {
504504

505505
// If we can resolve a symbol by removing __imp_ prefix, do that.
506506
// This odd rule is for compatibility with MSVC linker.
507-
if (name.startswith("__imp_")) {
507+
if (name.starts_with("__imp_")) {
508508
Symbol *imp = find(name.substr(strlen("__imp_")));
509509
if (imp && isa<Defined>(imp)) {
510510
auto *d = cast<Defined>(imp);
@@ -816,9 +816,9 @@ std::vector<Symbol *> SymbolTable::getSymsWithPrefix(StringRef prefix) {
816816
std::vector<Symbol *> syms;
817817
for (auto pair : symMap) {
818818
StringRef name = pair.first.val();
819-
if (name.startswith(prefix) || name.startswith(prefix.drop_front()) ||
820-
name.drop_front().startswith(prefix) ||
821-
name.drop_front().startswith(prefix.drop_front())) {
819+
if (name.starts_with(prefix) || name.starts_with(prefix.drop_front()) ||
820+
name.drop_front().starts_with(prefix) ||
821+
name.drop_front().starts_with(prefix.drop_front())) {
822822
syms.push_back(pair.second);
823823
}
824824
}
@@ -846,7 +846,7 @@ Symbol *SymbolTable::findMangle(StringRef name) {
846846
auto findByPrefix = [&syms](const Twine &t) -> Symbol * {
847847
std::string prefix = t.str();
848848
for (auto *s : syms)
849-
if (s->getName().startswith(prefix))
849+
if (s->getName().starts_with(prefix))
850850
return s;
851851
return nullptr;
852852
};
@@ -855,7 +855,7 @@ Symbol *SymbolTable::findMangle(StringRef name) {
855855
if (ctx.config.machine != I386)
856856
return findByPrefix("?" + name + "@@Y");
857857

858-
if (!name.startswith("_"))
858+
if (!name.starts_with("_"))
859859
return nullptr;
860860
// Search for x86 stdcall function.
861861
if (Symbol *s = findByPrefix(name + "@"))

lld/COFF/Writer.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ void Writer::fixPartialSectionChars(StringRef name, uint32_t chars) {
736736
PartialSection *pSec = it.second;
737737
StringRef curName = pSec->name;
738738
if (!curName.consume_front(name) ||
739-
(!curName.empty() && !curName.startswith("$")))
739+
(!curName.empty() && !curName.starts_with("$")))
740740
continue;
741741
if (pSec->characteristics == chars)
742742
continue;
@@ -769,7 +769,7 @@ bool Writer::fixGnuImportChunks() {
769769
// with alphabetical ordering of the object files within a library.
770770
for (auto it : partialSections) {
771771
PartialSection *pSec = it.second;
772-
if (!pSec->name.startswith(".idata"))
772+
if (!pSec->name.starts_with(".idata"))
773773
continue;
774774

775775
if (!pSec->chunks.empty())
@@ -857,9 +857,9 @@ static bool shouldStripSectionSuffix(SectionChunk *sc, StringRef name,
857857
return false;
858858
if (!sc || !sc->isCOMDAT())
859859
return false;
860-
return name.startswith(".text$") || name.startswith(".data$") ||
861-
name.startswith(".rdata$") || name.startswith(".pdata$") ||
862-
name.startswith(".xdata$") || name.startswith(".eh_frame$");
860+
return name.starts_with(".text$") || name.starts_with(".data$") ||
861+
name.starts_with(".rdata$") || name.starts_with(".pdata$") ||
862+
name.starts_with(".xdata$") || name.starts_with(".eh_frame$");
863863
}
864864

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

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

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

0 commit comments

Comments
 (0)