[lld] [lld][ELF/MachO] Use `.contains` rather than `.count` for set membership (PR #177404)

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 22 09:14:15 PST 2026


https://github.com/sbc100 created https://github.com/llvm/llvm-project/pull/177404

This matches the usage in the other linker backends.

See #176610, #177067

>From 605f89c19e0affc3d62ff09c3e238cad8bbe7507 Mon Sep 17 00:00:00 2001
From: Sam Clegg <sbc at chromium.org>
Date: Tue, 20 Jan 2026 17:17:31 -0800
Subject: [PATCH] [lld][ELF/MachO] Use `.contains` rather than `.count` for set
 membership

This matches the usage in the other linker backends.

See #176610, #177067
---
 lld/ELF/Arch/ARM.cpp                            |  8 ++++----
 lld/ELF/Arch/PPC64.cpp                          |  2 +-
 lld/ELF/Driver.cpp                              |  8 ++++----
 lld/ELF/LTO.cpp                                 |  2 +-
 lld/ELF/LinkerScript.cpp                        |  4 ++--
 lld/ELF/ScriptParser.cpp                        |  8 ++++----
 lld/ELF/SyntheticSections.cpp                   |  4 ++--
 lld/ELF/Writer.cpp                              | 10 +++++-----
 lld/MachO/InputFiles.cpp                        |  2 +-
 lld/MachO/ObjC.cpp                              |  4 ++--
 lld/MachO/SymbolTable.cpp                       |  2 +-
 lld/include/lld/Common/BPSectionOrdererBase.inc |  2 +-
 12 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/lld/ELF/Arch/ARM.cpp b/lld/ELF/Arch/ARM.cpp
index 6c4290ff1e448..3c183f71635ba 100644
--- a/lld/ELF/Arch/ARM.cpp
+++ b/lld/ELF/Arch/ARM.cpp
@@ -1229,7 +1229,7 @@ template <class ELFT> void ObjFile<ELFT>::importCmseSymbols() {
       continue;
     }
 
-    if (ctx.symtab->cmseImportLib.count(sym->getName())) {
+    if (ctx.symtab->cmseImportLib.contains(sym->getName())) {
       Err(ctx) << "CMSE symbol '" << sym->getName()
                << "' is multiply defined in import library '" << this << "'";
       continue;
@@ -1343,7 +1343,7 @@ ArmCmseSGSection::ArmCmseSGSection(Ctx &ctx)
     addSGVeneer(cast<Defined>(entryFunc.acleSeSym),
                 cast<Defined>(entryFunc.sym));
   for (auto &[_, sym] : ctx.symtab->cmseImportLib) {
-    if (!ctx.symtab->inCMSEOutImpLib.count(sym->getName()))
+    if (!ctx.symtab->inCMSEOutImpLib.contains(sym->getName()))
       Warn(ctx)
           << "entry function '" << sym->getName()
           << "' from CMSE import library is not present in secure application";
@@ -1352,7 +1352,7 @@ ArmCmseSGSection::ArmCmseSGSection(Ctx &ctx)
   if (!ctx.symtab->cmseImportLib.empty() && ctx.arg.cmseOutputLib.empty()) {
     for (auto &[_, entryFunc] : ctx.symtab->cmseSymMap) {
       Symbol *sym = entryFunc.sym;
-      if (!ctx.symtab->inCMSEOutImpLib.count(sym->getName()))
+      if (!ctx.symtab->inCMSEOutImpLib.contains(sym->getName()))
         Warn(ctx) << "new entry function '" << sym->getName()
                   << "' introduced but no output import library specified";
     }
@@ -1361,7 +1361,7 @@ ArmCmseSGSection::ArmCmseSGSection(Ctx &ctx)
 
 void ArmCmseSGSection::addSGVeneer(Symbol *acleSeSym, Symbol *sym) {
   entries.emplace_back(acleSeSym, sym);
-  if (ctx.symtab->cmseImportLib.count(sym->getName()))
+  if (ctx.symtab->cmseImportLib.contains(sym->getName()))
     ctx.symtab->inCMSEOutImpLib[sym->getName()] = true;
   // Symbol addresses different, nothing to do.
   if (acleSeSym->file != sym->file ||
diff --git a/lld/ELF/Arch/PPC64.cpp b/lld/ELF/Arch/PPC64.cpp
index 6528cc51accc5..6cdccd86bec37 100644
--- a/lld/ELF/Arch/PPC64.cpp
+++ b/lld/ELF/Arch/PPC64.cpp
@@ -1719,7 +1719,7 @@ void PPC64::relocateAlloc(InputSection &sec, uint8_t *buf) const {
       // entry, there may be R_PPC64_TOC16_HA not paired with
       // R_PPC64_TOC16_LO_DS. Don't relax. This loses some relaxation
       // opportunities but is safe.
-      if (ctx.ppc64noTocRelax.count({rel.sym, rel.addend}) ||
+      if (ctx.ppc64noTocRelax.contains({rel.sym, rel.addend}) ||
           !tryRelaxPPC64TocIndirection(ctx, rel, loc))
         relocate(loc, rel, val);
       break;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 8647752be31fe..c8c16bbde63a8 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -1155,7 +1155,7 @@ static void ltoValidateAllVtablesHaveTypeInfos(Ctx &ctx,
 
   SmallSetVector<StringRef, 0> vtableSymbolsWithNoRTTI;
   for (StringRef s : vtableSymbols)
-    if (!typeInfoSymbols.count(s))
+    if (!typeInfoSymbols.contains(s))
       vtableSymbolsWithNoRTTI.insert(s);
 
   // Remove known safe symbols.
@@ -2315,11 +2315,11 @@ static DenseSet<StringRef> getExcludeLibs(opt::InputArgList &args) {
 // This is not a popular option, but some programs such as bionic libc use it.
 static void excludeLibs(Ctx &ctx, opt::InputArgList &args) {
   DenseSet<StringRef> libs = getExcludeLibs(args);
-  bool all = libs.count("ALL");
+  bool all = libs.contains("ALL");
 
   auto visit = [&](InputFile *file) {
     if (file->archiveName.empty() ||
-        !(all || libs.count(path::filename(file->archiveName))))
+        !(all || libs.contains(path::filename(file->archiveName))))
       return;
     ArrayRef<Symbol *> symbols = file->getSymbols();
     if (isa<ELFFileBase>(file))
@@ -2687,7 +2687,7 @@ static void markBuffersAsDontNeed(Ctx &ctx, bool skipLinkedOutput) {
   for (BitcodeFile *file : ctx.lazyBitcodeFiles)
     bufs.insert(file->mb.getBufferStart());
   for (MemoryBuffer &mb : llvm::make_pointee_range(ctx.memoryBuffers))
-    if (bufs.count(mb.getBufferStart()))
+    if (bufs.contains(mb.getBufferStart()))
       mb.dontNeedIfMmap();
 }
 
diff --git a/lld/ELF/LTO.cpp b/lld/ELF/LTO.cpp
index 44a679498ed1d..fa39444408ee2 100644
--- a/lld/ELF/LTO.cpp
+++ b/lld/ELF/LTO.cpp
@@ -258,7 +258,7 @@ void BitcodeCompiler::add(BitcodeFile &f) {
     r.VisibleToRegularObj = ctx.arg.relocatable || sym->isUsedInRegularObj ||
                             sym->referencedAfterWrap ||
                             (r.Prevailing && sym->isExported) ||
-                            usedStartStop.count(objSym.getSectionName());
+                            usedStartStop.contains(objSym.getSectionName());
     // Identify symbols exported dynamically, and that therefore could be
     // referenced by a shared library not visible to the linker.
     r.ExportDynamic = sym->computeBinding(ctx) != STB_LOCAL &&
diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp
index 218c9d3a86184..8982094c3e530 100644
--- a/lld/ELF/LinkerScript.cpp
+++ b/lld/ELF/LinkerScript.cpp
@@ -1093,7 +1093,7 @@ void LinkerScript::diagnoseMissingSGSectionAddress() const {
     return;
 
   OutputSection *sec = findByName(sectionCommands, ".gnu.sgstubs");
-  if (sec && !sec->addrExpr && !ctx.arg.sectionStartMap.count(".gnu.sgstubs"))
+  if (sec && !sec->addrExpr && !ctx.arg.sectionStartMap.contains(".gnu.sgstubs"))
     ErrAlways(ctx) << "no address assigned to the veneers output section "
                    << sec->name;
 }
@@ -1904,5 +1904,5 @@ bool LinkerScript::shouldAddProvideSym(StringRef symName) {
     unusedProvideSyms.insert(sym);
     return false;
   }
-  return !unusedProvideSyms.count(sym);
+  return !unusedProvideSyms.contains(sym);
 }
diff --git a/lld/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp
index 07f3f786d7cec..0300b956e6071 100644
--- a/lld/ELF/ScriptParser.cpp
+++ b/lld/ELF/ScriptParser.cpp
@@ -541,9 +541,9 @@ void ScriptParser::readRegionAlias() {
   StringRef name = readName();
   expect(")");
 
-  if (ctx.script->memoryRegions.count(alias))
+  if (ctx.script->memoryRegions.contains(alias))
     setError("redefinition of memory region '" + alias + "'");
-  if (!ctx.script->memoryRegions.count(name))
+  if (!ctx.script->memoryRegions.contains(name))
     setError("memory region '" + name + "' is not defined");
   ctx.script->memoryRegions.insert({alias, ctx.script->memoryRegions[name]});
 }
@@ -1590,7 +1590,7 @@ Expr ScriptParser::readPrimary() {
   }
   if (tok == "LENGTH") {
     StringRef name = readParenName();
-    if (ctx.script->memoryRegions.count(name) == 0) {
+    if (!ctx.script->memoryRegions.contains(name)) {
       setError("memory region not defined: " + name);
       return [] { return 0; };
     }
@@ -1626,7 +1626,7 @@ Expr ScriptParser::readPrimary() {
   }
   if (tok == "ORIGIN") {
     StringRef name = readParenName();
-    if (ctx.script->memoryRegions.count(name) == 0) {
+    if (!ctx.script->memoryRegions.contains(name)) {
       setError("memory region not defined: " + name);
       return [] { return 0; };
     }
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 1c3ae0ac48e9c..40f97c45c5c49 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -943,7 +943,7 @@ void MipsGotSection::build() {
   // using 32-bit value at the end of 16-bit entries.
   for (FileGot &got : gots) {
     got.relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {
-      return got.global.count(p.first);
+      return got.global.contains(p.first);
     });
     set_union(got.local16, got.local32);
     got.local32.clear();
@@ -1005,7 +1005,7 @@ void MipsGotSection::build() {
   // by subtracting "global" entries in the primary GOT.
   primGot = &gots.front();
   primGot->relocs.remove_if([&](const std::pair<Symbol *, size_t> &p) {
-    return primGot->global.count(p.first);
+    return primGot->global.contains(p.first);
   });
 
   // Calculate indexes for each GOT entry.
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 57202f42cce5b..81fea1920871c 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -117,7 +117,7 @@ removeEmptyPTLoad(Ctx &ctx, SmallVector<std::unique_ptr<PhdrEntry>, 0> &phdrs) {
   for (auto it2 = it; it2 != phdrs.end(); ++it2)
     removed.insert(it2->get());
   for (OutputSection *sec : ctx.outputSections)
-    if (removed.count(sec->ptLoad))
+    if (removed.contains(sec->ptLoad))
       sec->ptLoad = nullptr;
   phdrs.erase(it, phdrs.end());
 }
@@ -682,7 +682,7 @@ unsigned elf::getSectionRank(Ctx &ctx, OutputSection &osec) {
 
   // We want to put section specified by -T option first, so we
   // can start assigning VA starting from them later.
-  if (ctx.arg.sectionStartMap.count(osec.name))
+  if (ctx.arg.sectionStartMap.contains(osec.name))
     return rank;
   rank |= RF_NOT_ADDR_SET;
 
@@ -1799,10 +1799,10 @@ static void removeUnusedSyntheticSections(Ctx &ctx) {
       for (SectionCommand *cmd : osec->commands)
         if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
           llvm::erase_if(isd->sections, [&](InputSection *isec) {
-            return unused.count(isec);
+            return unused.contains(isec);
           });
   llvm::erase_if(ctx.script->orphanSections, [&](const InputSectionBase *sec) {
-    return unused.count(sec);
+    return unused.contains(sec);
   });
 }
 
@@ -1943,7 +1943,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
       for (SharedFile *file : ctx.sharedFiles) {
         bool allNeededIsKnown =
             llvm::all_of(file->dtNeeded, [&](StringRef needed) {
-              return ctx.symtab->soNames.count(CachedHashStringRef(needed));
+              return ctx.symtab->soNames.contains(CachedHashStringRef(needed));
             });
         if (!allNeededIsKnown)
           continue;
diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp
index 681f502ad2cb6..cc7eae51175bc 100644
--- a/lld/MachO/InputFiles.cpp
+++ b/lld/MachO/InputFiles.cpp
@@ -1451,7 +1451,7 @@ void ObjFile::registerEhFrames(Section &ehFrameSection) {
       continue;
     }
 
-    assert(cieMap.count(cieIsec));
+    assert(cieMap.contains(cieIsec));
     const CIE &cie = cieMap[cieIsec];
     // Offset of the function address within the EH frame.
     const size_t funcAddrOff = dataOff;
diff --git a/lld/MachO/ObjC.cpp b/lld/MachO/ObjC.cpp
index 2540bffecf2aa..4522c574c137f 100644
--- a/lld/MachO/ObjC.cpp
+++ b/lld/MachO/ObjC.cpp
@@ -264,7 +264,7 @@ void ObjcCategoryChecker::parseCategory(const ConcatInputSection *catIsec) {
 
   auto *classSym = cast<Symbol *>(classReloc->referent);
   if (auto *d = dyn_cast<Defined>(classSym))
-    if (!classMap.count(d))
+    if (!classMap.contains(d))
       parseClass(d);
 
   if (const auto *r = catIsec->getRelocAt(catLayout.classMethodsOffset)) {
@@ -1179,7 +1179,7 @@ void ObjcCategoryMerger::collectAndValidateCategoriesData() {
       assert(categorySym &&
              "Failed to get a valid category at __objc_catlit offset");
 
-      if (nlCategories.count(categorySym))
+      if (nlCategories.contains(categorySym))
         continue;
 
       auto *catBodyIsec = dyn_cast<ConcatInputSection>(categorySym->isec());
diff --git a/lld/MachO/SymbolTable.cpp b/lld/MachO/SymbolTable.cpp
index a7db5a3ac96e3..84d3e45d64396 100644
--- a/lld/MachO/SymbolTable.cpp
+++ b/lld/MachO/SymbolTable.cpp
@@ -433,7 +433,7 @@ static bool recoverFromUndefinedSymbol(const Undefined &sym) {
     return true;
 
   // Handle -U.
-  if (config->explicitDynamicLookups.count(sym.getName())) {
+  if (config->explicitDynamicLookups.contains(sym.getName())) {
     symtab->addDynamicLookup(sym.getName());
     return true;
   }
diff --git a/lld/include/lld/Common/BPSectionOrdererBase.inc b/lld/include/lld/Common/BPSectionOrdererBase.inc
index 00c9dc7159ddd..43de0f722a30e 100644
--- a/lld/include/lld/Common/BPSectionOrdererBase.inc
+++ b/lld/include/lld/Common/BPSectionOrdererBase.inc
@@ -224,7 +224,7 @@ auto BPOrderer<D>::computeOrder(
   SmallVector<unsigned> sectionIdxsForFunctionCompression,
       sectionIdxsForDataCompression;
   for (unsigned sectionIdx = 0; sectionIdx < sections.size(); sectionIdx++) {
-    if (startupSectionIdxUNs.count(sectionIdx))
+    if (startupSectionIdxUNs.contains(sectionIdx))
       continue;
     const auto *isec = sections[sectionIdx];
     if (D::isCodeSection(*isec)) {



More information about the llvm-commits mailing list