From e3fcf2e06fbf8273f68a6985f1309136dae59eb1 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Tue, 9 Aug 2022 21:52:08 -0700 Subject: [PATCH] [ELF] Simplify llvm::enumerate with structured binding. NFC --- lld/ELF/Arch/RISCV.cpp | 10 ++++------ lld/ELF/DWARF.cpp | 5 ++--- lld/ELF/InputFiles.cpp | 35 +++++++++++++++-------------------- lld/ELF/Relocations.cpp | 6 +++--- lld/ELF/SyntheticSections.cpp | 4 ++-- 5 files changed, 26 insertions(+), 34 deletions(-) diff --git a/lld/ELF/Arch/RISCV.cpp b/lld/ELF/Arch/RISCV.cpp index 97cc94d..a863e79 100644 --- a/lld/ELF/Arch/RISCV.cpp +++ b/lld/ELF/Arch/RISCV.cpp @@ -616,11 +616,11 @@ static bool relax(InputSection &sec) { DenseMap valueDelta; ArrayRef sa = makeArrayRef(aux.anchors); uint32_t delta = 0; - for (auto it : llvm::enumerate(sec.relocations)) { - for (; sa.size() && sa[0].offset <= it.value().offset; sa = sa.slice(1)) + for (auto [i, r] : llvm::enumerate(sec.relocations)) { + for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(1)) if (!sa[0].end) valueDelta[sa[0].d] = delta; - delta = aux.relocDeltas[it.index()]; + delta = aux.relocDeltas[i]; } for (const SymbolAnchor &sa : sa) if (!sa.end) @@ -630,9 +630,7 @@ static bool relax(InputSection &sec) { std::fill_n(aux.relocTypes.get(), sec.relocations.size(), R_RISCV_NONE); aux.writes.clear(); - for (auto it : llvm::enumerate(sec.relocations)) { - Relocation &r = it.value(); - const size_t i = it.index(); + for (auto [i, r] : llvm::enumerate(sec.relocations)) { const uint64_t loc = secAddr + r.offset - delta; uint32_t &cur = aux.relocDeltas[i], remove = 0; switch (r.type) { diff --git a/lld/ELF/DWARF.cpp b/lld/ELF/DWARF.cpp index 7c21c0a..7e03c6d 100644 --- a/lld/ELF/DWARF.cpp +++ b/lld/ELF/DWARF.cpp @@ -29,8 +29,7 @@ template LLDDwarfObj::LLDDwarfObj(ObjFile *obj) { // Get the ELF sections to retrieve sh_flags. See the SHF_GROUP comment below. ArrayRef objSections = obj->template getELFShdrs(); assert(objSections.size() == obj->getSections().size()); - for (auto it : llvm::enumerate(obj->getSections())) { - InputSectionBase *sec = it.value(); + for (auto [i, sec] : llvm::enumerate(obj->getSections())) { if (!sec) continue; @@ -57,7 +56,7 @@ template LLDDwarfObj::LLDDwarfObj(ObjFile *obj) { else if (sec->name == ".debug_line_str") lineStrSection = toStringRef(sec->data()); else if (sec->name == ".debug_info" && - !(objSections[it.index()].sh_flags & ELF::SHF_GROUP)) { + !(objSections[i].sh_flags & ELF::SHF_GROUP)) { // In DWARF v5, -fdebug-types-section places type units in .debug_info // sections in COMDAT groups. They are not compile units and thus should // be ignored for .gdb_index/diagnostics purposes. diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index c2522fd..1c642e9 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -1673,16 +1673,12 @@ void BitcodeFile::parse() { symbols.resize(obj->symbols().size()); // Process defined symbols first. See the comment in // ObjFile::initializeSymbols. - for (auto it : llvm::enumerate(obj->symbols())) - if (!it.value().isUndefined()) { - Symbol *&sym = symbols[it.index()]; - createBitcodeSymbol(sym, keptComdats, it.value(), *this); - } - for (auto it : llvm::enumerate(obj->symbols())) - if (it.value().isUndefined()) { - Symbol *&sym = symbols[it.index()]; - createBitcodeSymbol(sym, keptComdats, it.value(), *this); - } + for (auto [i, irSym] : llvm::enumerate(obj->symbols())) + if (!irSym.isUndefined()) + createBitcodeSymbol(symbols[i], keptComdats, irSym, *this); + for (auto [i, irSym] : llvm::enumerate(obj->symbols())) + if (irSym.isUndefined()) + createBitcodeSymbol(symbols[i], keptComdats, irSym, *this); for (auto l : obj->getDependentLibraries()) addDependentLibrary(l, this); @@ -1691,22 +1687,21 @@ void BitcodeFile::parse() { void BitcodeFile::parseLazy() { SymbolTable &symtab = *elf::symtab; symbols.resize(obj->symbols().size()); - for (auto it : llvm::enumerate(obj->symbols())) - if (!it.value().isUndefined()) { - auto *sym = symtab.insert(saver().save(it.value().getName())); + for (auto [i, irSym] : llvm::enumerate(obj->symbols())) + if (!irSym.isUndefined()) { + auto *sym = symtab.insert(saver().save(irSym.getName())); sym->resolve(LazyObject{*this}); - symbols[it.index()] = sym; + symbols[i] = sym; } } void BitcodeFile::postParse() { - for (auto it : llvm::enumerate(obj->symbols())) { - const Symbol &sym = *symbols[it.index()]; - const auto &objSym = it.value(); - if (sym.file == this || !sym.isDefined() || objSym.isUndefined() || - objSym.isCommon() || objSym.isWeak()) + for (auto [i, irSym] : llvm::enumerate(obj->symbols())) { + const Symbol &sym = *symbols[i]; + if (sym.file == this || !sym.isDefined() || irSym.isUndefined() || + irSym.isCommon() || irSym.isWeak()) continue; - int c = objSym.getComdatIndex(); + int c = irSym.getComdatIndex(); if (c != -1 && !keptComdats[c]) continue; reportDuplicate(sym, this, nullptr, 0); diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp index b2e6499..5d54527 100644 --- a/lld/ELF/Relocations.cpp +++ b/lld/ELF/Relocations.cpp @@ -812,9 +812,9 @@ void elf::reportUndefinedSymbols() { } // Enable spell corrector for the first 2 diagnostics. - for (auto it : enumerate(undefs)) - if (!it.value().locs.empty()) - reportUndefinedSymbol(it.value(), it.index() < 2); + for (const auto &[i, undef] : llvm::enumerate(undefs)) + if (!undef.locs.empty()) + reportUndefinedSymbol(undef, i < 2); undefs.clear(); } diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp index 9ec9912..82a57b1 100644 --- a/lld/ELF/SyntheticSections.cpp +++ b/lld/ELF/SyntheticSections.cpp @@ -2007,8 +2007,8 @@ template bool RelrSection::updateAllocSize() { // Get offsets for all relative relocations and sort them. std::unique_ptr offsets(new uint64_t[relocs.size()]); - for (auto it : llvm::enumerate(relocs)) - offsets[it.index()] = it.value().getOffset(); + for (auto [i, r] : llvm::enumerate(relocs)) + offsets[i] = r.getOffset(); llvm::sort(offsets.get(), offsets.get() + relocs.size()); // For each leading relocation, find following ones that can be folded -- 2.7.4