Simplify the handling of iterators in ObjectFile.
None of the object file formats reported error on iterator increment. In
retrospect, that is not too surprising: no object format stores symbols or
sections in a linked list or other structure that requires chasing pointers.
As a consequence, all error checking can be done on begin() and end().
This reduces the text segment of bin/llvm-readobj in my machine from 521233 to
518526 bytes.
llvm-svn: 200442
diff --git a/llvm/lib/Object/MachOObjectFile.cpp b/llvm/lib/Object/MachOObjectFile.cpp
index 2409314..49e3148 100644
--- a/llvm/lib/Object/MachOObjectFile.cpp
+++ b/llvm/lib/Object/MachOObjectFile.cpp
@@ -275,18 +275,9 @@
// Helper to advance a section or symbol iterator multiple increments at a time.
template<class T>
-static error_code advance(T &it, size_t Val) {
- error_code ec;
- while (Val--) {
- it.increment(ec);
- }
- return ec;
-}
-
-template<class T>
-static void advanceTo(T &it, size_t Val) {
- if (error_code ec = advance(it, Val))
- report_fatal_error(ec.message());
+static void advance(T &it, size_t Val) {
+ while (Val--)
+ ++it;
}
static unsigned getCPUType(const MachOObjectFile *O) {
@@ -305,11 +296,9 @@
if (IsScattered) {
uint32_t Val = O->getPlainRelocationSymbolNum(RE);
- error_code ec;
for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols();
- SI != SE; SI.increment(ec)) {
- if (ec) report_fatal_error(ec.message());
-
+ SI != SE; ++SI) {
+ error_code ec;
uint64_t Addr;
StringRef Name;
@@ -325,9 +314,8 @@
// If we couldn't find a symbol that this relocation refers to, try
// to find a section beginning instead.
for (section_iterator SI = O->begin_sections(), SE = O->end_sections();
- SI != SE; SI.increment(ec)) {
- if (ec) report_fatal_error(ec.message());
-
+ SI != SE; ++SI) {
+ error_code ec;
uint64_t Addr;
StringRef Name;
@@ -350,12 +338,12 @@
if (isExtern) {
symbol_iterator SI = O->begin_symbols();
- advanceTo(SI, Val);
+ advance(SI, Val);
SI->getName(S);
} else {
section_iterator SI = O->begin_sections();
// Adjust for the fact that sections are 1-indexed.
- advanceTo(SI, Val - 1);
+ advance(SI, Val - 1);
SI->getName(S);
}
@@ -454,14 +442,11 @@
}
}
-error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
- SymbolRef &Res) const {
+void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
unsigned SymbolTableEntrySize = is64Bit() ?
sizeof(MachO::nlist_64) :
sizeof(MachO::nlist);
Symb.p += SymbolTableEntrySize;
- Res = SymbolRef(Symb, this);
- return object_error::success;
}
error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
@@ -545,9 +530,7 @@
}
// Unfortunately symbols are unsorted so we need to touch all
// symbols from load command
- error_code ec;
- for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
- I.increment(ec)) {
+ for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E; ++I) {
DataRefImpl DRI = I->getRawDataRefImpl();
Entry = getSymbolTableEntryBase(this, DRI);
getSymbolAddress(DRI, Value);
@@ -648,11 +631,8 @@
report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
}
-error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
- SectionRef &Res) const {
+void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
Sec.d.a++;
- Res = SectionRef(Sec, this);
- return object_error::success;
}
error_code
@@ -834,13 +814,10 @@
return relocation_iterator(RelocationRef(Ret, this));
}
-error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
- RelocationRef &Res) const {
+void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
const MachO::any_relocation_info *P =
reinterpret_cast<const MachO::any_relocation_info *>(Rel.p);
Rel.p = reinterpret_cast<uintptr_t>(P + 1);
- Res = RelocationRef(Rel, this);
- return object_error::success;
}
error_code