[llvm] r204031 - [C++11] Introduce ObjectFile::symbols() to use range-based loops.

Alexey Samsonov samsonov at google.com
Mon Mar 17 00:28:21 PDT 2014


Author: samsonov
Date: Mon Mar 17 02:28:19 2014
New Revision: 204031

URL: http://llvm.org/viewvc/llvm-project?rev=204031&view=rev
Log:
[C++11] Introduce ObjectFile::symbols() to use range-based loops.

Reviewers: rafael

Reviewed By: rafael

CC: llvm-commits

Differential Revision: http://llvm-reviews.chandlerc.com/D3081

Modified:
    llvm/trunk/include/llvm/Object/ObjectFile.h
    llvm/trunk/lib/MC/MCObjectDisassembler.cpp
    llvm/trunk/lib/MC/MCObjectSymbolizer.cpp
    llvm/trunk/lib/Object/MachOObjectFile.cpp
    llvm/trunk/tools/llvm-objdump/MachODump.cpp
    llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
    llvm/trunk/tools/llvm-readobj/MachODumper.cpp
    llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp
    llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h
    llvm/trunk/tools/macho-dump/macho-dump.cpp

Modified: llvm/trunk/include/llvm/Object/ObjectFile.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Object/ObjectFile.h?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Object/ObjectFile.h (original)
+++ llvm/trunk/include/llvm/Object/ObjectFile.h Mon Mar 17 02:28:19 2014
@@ -286,6 +286,10 @@ protected:
   virtual error_code getLibraryPath(DataRefImpl Lib, StringRef &Res) const = 0;
 
 public:
+  typedef iterator_range<symbol_iterator> symbol_iterator_range;
+  symbol_iterator_range symbols() const {
+    return symbol_iterator_range(symbol_begin(), symbol_end());
+  }
 
   virtual section_iterator section_begin() const = 0;
   virtual section_iterator section_end() const = 0;

Modified: llvm/trunk/lib/MC/MCObjectDisassembler.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCObjectDisassembler.cpp?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCObjectDisassembler.cpp (original)
+++ llvm/trunk/lib/MC/MCObjectDisassembler.cpp Mon Mar 17 02:28:19 2014
@@ -37,13 +37,12 @@ MCObjectDisassembler::MCObjectDisassembl
     : Obj(Obj), Dis(Dis), MIA(MIA), MOS(0) {}
 
 uint64_t MCObjectDisassembler::getEntrypoint() {
-  for (symbol_iterator SI = Obj.symbol_begin(), SE = Obj.symbol_end();
-       SI != SE; ++SI) {
+  for (const SymbolRef &Symbol : Obj.symbols()) {
     StringRef Name;
-    SI->getName(Name);
+    Symbol.getName(Name);
     if (Name == "main" || Name == "_main") {
       uint64_t Entrypoint;
-      SI->getAddress(Entrypoint);
+      Symbol.getAddress(Entrypoint);
       return getEffectiveLoadAddr(Entrypoint);
     }
   }
@@ -181,13 +180,12 @@ void MCObjectDisassembler::buildCFG(MCMo
   AddressSetTy Splits;
   AddressSetTy Calls;
 
-  for (symbol_iterator SI = Obj.symbol_begin(), SE = Obj.symbol_end();
-       SI != SE; ++SI) {
+  for (const SymbolRef &Symbol : Obj.symbols()) {
     SymbolRef::Type SymType;
-    SI->getType(SymType);
+    Symbol.getType(SymType);
     if (SymType == SymbolRef::ST_Function) {
       uint64_t SymAddr;
-      SI->getAddress(SymAddr);
+      Symbol.getAddress(SymAddr);
       SymAddr = getEffectiveLoadAddr(SymAddr);
       Calls.push_back(SymAddr);
       Splits.push_back(SymAddr);

Modified: llvm/trunk/lib/MC/MCObjectSymbolizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCObjectSymbolizer.cpp?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCObjectSymbolizer.cpp (original)
+++ llvm/trunk/lib/MC/MCObjectSymbolizer.cpp Mon Mar 17 02:28:19 2014
@@ -153,14 +153,17 @@ tryAddingSymbolicOperand(MCInst &MI, raw
     return false;
   uint64_t UValue = Value;
   // FIXME: map instead of looping each time?
-  for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end();
-       SI != SE; ++SI) {
-    uint64_t SymAddr; SI->getAddress(SymAddr);
-    uint64_t SymSize; SI->getSize(SymSize);
-    StringRef SymName; SI->getName(SymName);
-    SymbolRef::Type SymType; SI->getType(SymType);
-    if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize
-        || SymName.empty() || SymType != SymbolRef::ST_Function)
+  for (const SymbolRef &Symbol : Obj->symbols()) {
+    uint64_t SymAddr;
+    Symbol.getAddress(SymAddr);
+    uint64_t SymSize;
+    Symbol.getSize(SymSize);
+    StringRef SymName;
+    Symbol.getName(SymName);
+    SymbolRef::Type SymType;
+    Symbol.getType(SymType);
+    if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize ||
+        SymName.empty() || SymType != SymbolRef::ST_Function)
       continue;
 
     if ( SymAddr == UValue ||

Modified: llvm/trunk/lib/Object/MachOObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/MachOObjectFile.cpp?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/lib/Object/MachOObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/MachOObjectFile.cpp Mon Mar 17 02:28:19 2014
@@ -296,16 +296,16 @@ static void printRelocationTargetName(co
   if (IsScattered) {
     uint32_t Val = O->getPlainRelocationSymbolNum(RE);
 
-    for (symbol_iterator SI = O->symbol_begin(), SE = O->symbol_end();
-         SI != SE; ++SI) {
+    for (const SymbolRef &Symbol : O->symbols()) {
       error_code ec;
       uint64_t Addr;
       StringRef Name;
 
-      if ((ec = SI->getAddress(Addr)))
+      if ((ec = Symbol.getAddress(Addr)))
         report_fatal_error(ec.message());
-      if (Addr != Val) continue;
-      if ((ec = SI->getName(Name)))
+      if (Addr != Val)
+        continue;
+      if ((ec = Symbol.getName(Name)))
         report_fatal_error(ec.message());
       fmt << Name;
       return;
@@ -528,8 +528,8 @@ error_code MachOObjectFile::getSymbolSiz
   }
   // Unfortunately symbols are unsorted so we need to touch all
   // symbols from load command
-  for (symbol_iterator I = symbol_begin(), E = symbol_end(); I != E; ++I) {
-    DataRefImpl DRI = I->getRawDataRefImpl();
+  for (const SymbolRef &Symbol : symbols()) {
+    DataRefImpl DRI = Symbol.getRawDataRefImpl();
     Entry = getSymbolTableEntryBase(this, DRI);
     getSymbolAddress(DRI, Value);
     if (Entry.n_sect == SectionIndex && Value > BeginOffset)

Modified: llvm/trunk/tools/llvm-objdump/MachODump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/MachODump.cpp?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/MachODump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/MachODump.cpp Mon Mar 17 02:28:19 2014
@@ -146,17 +146,14 @@ static void DumpDataInCode(const char *b
   }
 }
 
-static void
-getSectionsAndSymbols(const MachO::mach_header Header,
-                      MachOObjectFile *MachOObj,
-                      std::vector<SectionRef> &Sections,
-                      std::vector<SymbolRef> &Symbols,
-                      SmallVectorImpl<uint64_t> &FoundFns,
-                      uint64_t &BaseSegmentAddress) {
-  for (symbol_iterator SI = MachOObj->symbol_begin(),
-                       SE = MachOObj->symbol_end();
-       SI != SE; ++SI)
-    Symbols.push_back(*SI);
+static void getSectionsAndSymbols(const MachO::mach_header Header,
+                                  MachOObjectFile *MachOObj,
+                                  std::vector<SectionRef> &Sections,
+                                  std::vector<SymbolRef> &Symbols,
+                                  SmallVectorImpl<uint64_t> &FoundFns,
+                                  uint64_t &BaseSegmentAddress) {
+  for (const SymbolRef &Symbol : MachOObj->symbols())
+    Symbols.push_back(Symbol);
 
   for (const SectionRef &Section : MachOObj->sections()) {
     StringRef SectName;

Modified: llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp (original)
+++ llvm/trunk/tools/llvm-objdump/llvm-objdump.cpp Mon Mar 17 02:28:19 2014
@@ -408,13 +408,12 @@ static void DisassembleObject(const Obje
       break;
 
     // Make a list of all the symbols in this section.
-    std::vector<std::pair<uint64_t, StringRef> > Symbols;
-    for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end();
-         SI != SE; ++SI) {
+    std::vector<std::pair<uint64_t, StringRef>> Symbols;
+    for (const SymbolRef &Symbol : Obj->symbols()) {
       bool contains;
-      if (!error(Section.containsSymbol(*SI, contains)) && contains) {
+      if (!error(Section.containsSymbol(Symbol, contains)) && contains) {
         uint64_t Address;
-        if (error(SI->getAddress(Address)))
+        if (error(Symbol.getAddress(Address)))
           break;
         if (Address == UnknownAddressOrSize)
           continue;
@@ -423,7 +422,7 @@ static void DisassembleObject(const Obje
           continue;
 
         StringRef Name;
-        if (error(SI->getName(Name)))
+        if (error(Symbol.getName(Name)))
           break;
         Symbols.push_back(std::make_pair(Address, Name));
       }
@@ -703,19 +702,23 @@ static void PrintSymbolTable(const Objec
   if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
     PrintCOFFSymbolTable(coff);
   else {
-    for (symbol_iterator si = o->symbol_begin(), se = o->symbol_end();
-         si != se; ++si) {
+    for (const SymbolRef &Symbol : o->symbols()) {
       StringRef Name;
       uint64_t Address;
       SymbolRef::Type Type;
       uint64_t Size;
-      uint32_t Flags = si->getFlags();
+      uint32_t Flags = Symbol.getFlags();
       section_iterator Section = o->section_end();
-      if (error(si->getName(Name))) continue;
-      if (error(si->getAddress(Address))) continue;
-      if (error(si->getType(Type))) continue;
-      if (error(si->getSize(Size))) continue;
-      if (error(si->getSection(Section))) continue;
+      if (error(Symbol.getName(Name)))
+        continue;
+      if (error(Symbol.getAddress(Address)))
+        continue;
+      if (error(Symbol.getType(Type)))
+        continue;
+      if (error(Symbol.getSize(Size)))
+        continue;
+      if (error(Symbol.getSection(Section)))
+        continue;
 
       bool Global = Flags & SymbolRef::SF_Global;
       bool Weak = Flags & SymbolRef::SF_Weak;

Modified: llvm/trunk/tools/llvm-readobj/MachODumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/MachODumper.cpp?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/MachODumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/MachODumper.cpp Mon Mar 17 02:28:19 2014
@@ -38,7 +38,7 @@ public:
   virtual void printUnwindInfo() override;
 
 private:
-  void printSymbol(symbol_iterator SymI);
+  void printSymbol(const SymbolRef &Symbol);
 
   void printRelocation(const RelocationRef &Reloc);
 
@@ -255,13 +255,12 @@ void MachODumper::printSections(const Ma
 
     if (opts::SectionSymbols) {
       ListScope D(W, "Symbols");
-      for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
-           SymI != SymE; ++SymI) {
+      for (const SymbolRef &Symbol : Obj->symbols()) {
         bool Contained = false;
-        if (Section.containsSymbol(*SymI, Contained) || !Contained)
+        if (Section.containsSymbol(Symbol, Contained) || !Contained)
           continue;
 
-        printSymbol(SymI);
+        printSymbol(Symbol);
       }
     }
 
@@ -354,9 +353,8 @@ void MachODumper::printRelocation(const
 void MachODumper::printSymbols() {
   ListScope Group(W, "Symbols");
 
-  for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
-       SymI != SymE; ++SymI) {
-    printSymbol(SymI);
+  for (const SymbolRef &Symbol : Obj->symbols()) {
+    printSymbol(Symbol);
   }
 }
 
@@ -364,38 +362,37 @@ void MachODumper::printDynamicSymbols()
   ListScope Group(W, "DynamicSymbols");
 }
 
-void MachODumper::printSymbol(symbol_iterator SymI) {
+void MachODumper::printSymbol(const SymbolRef &Symbol) {
   StringRef SymbolName;
-  if (SymI->getName(SymbolName))
+  if (Symbol.getName(SymbolName))
     SymbolName = "";
 
-  MachOSymbol Symbol;
-  getSymbol(Obj, SymI->getRawDataRefImpl(), Symbol);
+  MachOSymbol MOSymbol;
+  getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
 
   StringRef SectionName = "";
   section_iterator SecI(Obj->section_begin());
-  if (!error(SymI->getSection(SecI)) &&
-      SecI != Obj->section_end())
-      error(SecI->getName(SectionName));
+  if (!error(Symbol.getSection(SecI)) && SecI != Obj->section_end())
+    error(SecI->getName(SectionName));
 
   DictScope D(W, "Symbol");
-  W.printNumber("Name", SymbolName, Symbol.StringIndex);
-  if (Symbol.Type & MachO::N_STAB) {
-    W.printHex ("Type", "SymDebugTable", Symbol.Type);
+  W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
+  if (MOSymbol.Type & MachO::N_STAB) {
+    W.printHex("Type", "SymDebugTable", MOSymbol.Type);
   } else {
-    if (Symbol.Type & MachO::N_PEXT)
+    if (MOSymbol.Type & MachO::N_PEXT)
       W.startLine() << "PrivateExtern\n";
-    if (Symbol.Type & MachO::N_EXT)
+    if (MOSymbol.Type & MachO::N_EXT)
       W.startLine() << "Extern\n";
-    W.printEnum("Type", uint8_t(Symbol.Type & MachO::N_TYPE),
+    W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
                 makeArrayRef(MachOSymbolTypes));
   }
-  W.printHex   ("Section", SectionName, Symbol.SectionIndex);
-  W.printEnum  ("RefType", static_cast<uint16_t>(Symbol.Flags & 0xF),
-                  makeArrayRef(MachOSymbolRefTypes));
-  W.printFlags ("Flags", static_cast<uint16_t>(Symbol.Flags & ~0xF),
-                  makeArrayRef(MachOSymbolFlags));
-  W.printHex   ("Value", Symbol.Value);
+  W.printHex("Section", SectionName, MOSymbol.SectionIndex);
+  W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0xF),
+              makeArrayRef(MachOSymbolRefTypes));
+  W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0xF),
+               makeArrayRef(MachOSymbolFlags));
+  W.printHex("Value", MOSymbol.Value);
 }
 
 void MachODumper::printUnwindInfo() {

Modified: llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp (original)
+++ llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.cpp Mon Mar 17 02:28:19 2014
@@ -53,9 +53,8 @@ static void patchFunctionNameInDILineInf
 
 ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
     : Module(Obj), DebugInfoContext(DICtx) {
-  for (symbol_iterator si = Module->symbol_begin(), se = Module->symbol_end();
-       si != se; ++si) {
-    addSymbol(si);
+  for (const SymbolRef &Symbol : Module->symbols()) {
+    addSymbol(Symbol);
   }
   bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
   if (NoSymbolTable && Module->isELF()) {
@@ -63,20 +62,19 @@ ModuleInfo::ModuleInfo(ObjectFile *Obj,
     std::pair<symbol_iterator, symbol_iterator> IDyn =
         getELFDynamicSymbolIterators(Module);
     for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) {
-      addSymbol(si);
+      addSymbol(*si);
     }
   }
 }
 
-void ModuleInfo::addSymbol(const symbol_iterator &Sym) {
+void ModuleInfo::addSymbol(const SymbolRef &Symbol) {
   SymbolRef::Type SymbolType;
-  if (error(Sym->getType(SymbolType)))
+  if (error(Symbol.getType(SymbolType)))
     return;
-  if (SymbolType != SymbolRef::ST_Function &&
-      SymbolType != SymbolRef::ST_Data)
+  if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
     return;
   uint64_t SymbolAddress;
-  if (error(Sym->getAddress(SymbolAddress)) ||
+  if (error(Symbol.getAddress(SymbolAddress)) ||
       SymbolAddress == UnknownAddressOrSize)
     return;
   uint64_t SymbolSize;
@@ -84,11 +82,11 @@ void ModuleInfo::addSymbol(const symbol_
   // occupies the memory range up to the following symbol.
   if (isa<MachOObjectFile>(Module))
     SymbolSize = 0;
-  else if (error(Sym->getSize(SymbolSize)) ||
+  else if (error(Symbol.getSize(SymbolSize)) ||
            SymbolSize == UnknownAddressOrSize)
     return;
   StringRef SymbolName;
-  if (error(Sym->getName(SymbolName)))
+  if (error(Symbol.getName(SymbolName)))
     return;
   // Mach-O symbol table names have leading underscore, skip it.
   if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')

Modified: llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h (original)
+++ llvm/trunk/tools/llvm-symbolizer/LLVMSymbolize.h Mon Mar 17 02:28:19 2014
@@ -101,7 +101,7 @@ private:
   bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
                               std::string &Name, uint64_t &Addr,
                               uint64_t &Size) const;
-  void addSymbol(const symbol_iterator &Sym);
+  void addSymbol(const SymbolRef &Symbol);
   ObjectFile *Module;
   std::unique_ptr<DIContext> DebugInfoContext;
 

Modified: llvm/trunk/tools/macho-dump/macho-dump.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/macho-dump/macho-dump.cpp?rev=204031&r1=204030&r2=204031&view=diff
==============================================================================
--- llvm/trunk/tools/macho-dump/macho-dump.cpp (original)
+++ llvm/trunk/tools/macho-dump/macho-dump.cpp Mon Mar 17 02:28:19 2014
@@ -202,9 +202,8 @@ static int DumpSymtabCommand(const MachO
   // Dump the symbol table.
   outs() << "  ('_symbols', [\n";
   unsigned SymNum = 0;
-  for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
-       ++I, ++SymNum) {
-    DataRefImpl DRI = I->getRawDataRefImpl();
+  for (const SymbolRef &Symbol : Obj.symbols()) {
+    DataRefImpl DRI = Symbol.getRawDataRefImpl();
     if (Obj.is64Bit()) {
       MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI);
       DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
@@ -216,6 +215,7 @@ static int DumpSymtabCommand(const MachO
                                STE.n_sect, STE.n_desc, STE.n_value,
                                StringTable);
     }
+    SymNum++;
   }
   outs() << "  ])\n";
 





More information about the llvm-commits mailing list