[llvm] b75453b - MCAssembler: Remove unneeded non-const iterators for Sections and misleading size()

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 5 15:42:43 PDT 2024


Author: Fangrui Song
Date: 2024-07-05T15:42:38-07:00
New Revision: b75453bc07dabe8e0dc0efb0766a4238e3df6712

URL: https://github.com/llvm/llvm-project/commit/b75453bc07dabe8e0dc0efb0766a4238e3df6712
DIFF: https://github.com/llvm/llvm-project/commit/b75453bc07dabe8e0dc0efb0766a4238e3df6712.diff

LOG: MCAssembler: Remove unneeded non-const iterators for Sections and misleading size()

The pointers cannot be mutated even if the dereferenced MCSection can.

Added: 
    

Modified: 
    llvm/include/llvm/MC/MCAssembler.h
    llvm/lib/MC/MCAssembler.cpp
    llvm/lib/MC/MachObjectWriter.cpp
    llvm/lib/MC/WinCOFFObjectWriter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/MC/MCAssembler.h b/llvm/include/llvm/MC/MCAssembler.h
index 8a8f0d4c1ea08..4b08d50de9e22 100644
--- a/llvm/include/llvm/MC/MCAssembler.h
+++ b/llvm/include/llvm/MC/MCAssembler.h
@@ -56,10 +56,8 @@ class MCValue;
 
 class MCAssembler {
 public:
-  using SectionListType = std::vector<MCSection *>;
-
+  using SectionListType = SmallVector<MCSection *, 0>;
   using const_iterator = pointee_iterator<SectionListType::const_iterator>;
-  using iterator = pointee_iterator<SectionListType::iterator>;
 
   /// MachO specific deployment target version info.
   // A Major version of 0 indicates that no version information was supplied
@@ -326,17 +324,9 @@ class MCAssembler {
     BundleAlignSize = Size;
   }
 
-  /// \name Section List Access
-  /// @{
-
-  iterator begin() { return Sections.begin(); }
   const_iterator begin() const { return Sections.begin(); }
-
-  iterator end() { return Sections.end(); }
   const_iterator end() const { return Sections.end(); }
 
-  size_t size() const { return Sections.size(); }
-
   iterator_range<pointee_iterator<
       typename SmallVector<const MCSymbol *, 0>::const_iterator>>
   symbols() const {

diff  --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp
index 3c777791472bf..c8d12eb5dcf64 100644
--- a/llvm/lib/MC/MCAssembler.cpp
+++ b/llvm/lib/MC/MCAssembler.cpp
@@ -1339,14 +1339,18 @@ LLVM_DUMP_METHOD void MCAssembler::dump() const{
 
   OS << "<MCAssembler\n";
   OS << "  Sections:[\n    ";
-  for (const_iterator it = begin(), ie = end(); it != ie; ++it) {
-    if (it != begin()) OS << ",\n    ";
-    it->dump();
+  bool First = true;
+  for (const MCSection &Sec : *this) {
+    if (First)
+      First = false;
+    else
+      OS << ",\n    ";
+    Sec.dump();
   }
   OS << "],\n";
   OS << "  Symbols:[";
 
-  bool First = true;
+  First = true;
   for (const MCSymbol &Sym : symbols()) {
     if (First)
       First = false;

diff  --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 14f7f0d5c1871..53eed0092a5b4 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -572,9 +572,8 @@ void MachObjectWriter::computeSymbolTable(
   // Build section lookup table.
   DenseMap<const MCSection*, uint8_t> SectionIndexMap;
   unsigned Index = 1;
-  for (MCAssembler::iterator it = Asm.begin(),
-         ie = Asm.end(); it != ie; ++it, ++Index)
-    SectionIndexMap[&*it] = Index;
+  for (MCSection &Sec : Asm)
+    SectionIndexMap[&Sec] = Index++;
   assert(Index <= 256 && "Too many sections!");
 
   // Build the string table.
@@ -798,7 +797,7 @@ uint64_t MachObjectWriter::writeObject(MCAssembler &Asm) {
     }
   }
 
-  unsigned NumSections = Asm.size();
+  unsigned NumSections = Asm.end() - Asm.begin();
   const MCAssembler::VersionInfoType &VersionInfo = Asm.getVersionInfo();
 
   // The section data starts after the header, the segment load command (and

diff  --git a/llvm/lib/MC/WinCOFFObjectWriter.cpp b/llvm/lib/MC/WinCOFFObjectWriter.cpp
index c0bad192eb982..7ba38be7edba9 100644
--- a/llvm/lib/MC/WinCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/WinCOFFObjectWriter.cpp
@@ -1139,8 +1139,8 @@ uint64_t WinCOFFWriter::writeObject(MCAssembler &Asm) {
 #ifndef NDEBUG
   sections::iterator I = Sections.begin();
   sections::iterator IE = Sections.end();
-  MCAssembler::iterator J = Asm.begin();
-  MCAssembler::iterator JE = Asm.end();
+  auto J = Asm.begin();
+  auto JE = Asm.end();
   for (; I != IE && J != JE; ++I, ++J) {
     while (J != JE && ((Mode == NonDwoOnly && isDwoSection(*J)) ||
                        (Mode == DwoOnly && !isDwoSection(*J))))


        


More information about the llvm-commits mailing list