Skip to content

Commit

Permalink
MCAssembler: Remove unneeded non-const iterators for Sections and mis…
Browse files Browse the repository at this point in the history
…leading size()

The pointers cannot be mutated even if the dereferenced MCSection can.
  • Loading branch information
MaskRay committed Jul 5, 2024
1 parent ceade83 commit b75453b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
12 changes: 1 addition & 11 deletions llvm/include/llvm/MC/MCAssembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/MC/MCAssembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions llvm/lib/MC/MachObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/MC/WinCOFFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
Expand Down

0 comments on commit b75453b

Please sign in to comment.