Rephrase find loop to use std::find_if
authorDavid Blaikie <dblaikie@gmail.com>
Thu, 12 Mar 2015 19:46:21 +0000 (19:46 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Thu, 12 Mar 2015 19:46:21 +0000 (19:46 +0000)
Avoids the need for an assert-only variable, among other benefits.

llvm-svn: 232088

lld/include/lld/Core/File.h
lld/lib/ReaderWriter/MachO/StubsPass.cpp

index 5792ccf..41966e8 100644 (file)
@@ -126,7 +126,7 @@ public:
   /// object. There are four kinds of Atoms, so this iterator is templated on
   /// the four base Atom kinds.
   template <typename T>
-  class atom_iterator {
+  class atom_iterator : public std::iterator<std::forward_iterator_tag, T> {
   public:
     atom_iterator(const atom_collection<T> &c, const void *it)
               : _collection(c), _it(it) { }
@@ -139,8 +139,12 @@ public:
       return _collection.deref(_it);
     }
 
-    bool operator!=(const atom_iterator<T> &other) const {
-      return (this->_it != other._it);
+    friend bool operator==(const atom_iterator<T> &lhs, const atom_iterator<T> &rhs)  {
+      return lhs._it == rhs._it;
+    }
+
+    friend bool operator!=(const atom_iterator<T> &lhs, const atom_iterator<T> &rhs)  {
+      return !(lhs == rhs);
     }
 
     atom_iterator<T> &operator++() {
@@ -152,7 +156,6 @@ public:
     const void               *_it;
   };
 
-
   /// \brief Must be implemented to return the atom_collection object for
   /// all DefinedAtoms in this File.
   virtual const atom_collection<DefinedAtom> &defined() const = 0;
index e320ea5..bc4d9c2 100644 (file)
@@ -264,17 +264,13 @@ public:
     mergedFile->addAtom(*helperCacheNLPAtom);
 
     // Add reference to dyld_stub_binder in libSystem.dylib
-    bool binderFound = false;
-    for (const SharedLibraryAtom *atom : mergedFile->sharedLibrary()) {
-      if (atom->name().equals(_stubInfo.binderSymbolName)) {
-        addReference(helperBinderNLPAtom,
-                     _stubInfo.nonLazyPointerReferenceToBinder, atom);
-        binderFound = true;
-        break;
-      }
-    }
-    assert(binderFound && "dyld_stub_binder not found");
-    (void)binderFound;
+    auto I = std::find_if(
+        mergedFile->sharedLibrary().begin(), mergedFile->sharedLibrary().end(),
+        [&](const SharedLibraryAtom *atom) {
+          return atom->name().equals(_stubInfo.binderSymbolName);
+        });
+    assert(I != mergedFile->sharedLibrary().end() && "dyld_stub_binder not found");
+    addReference(helperBinderNLPAtom, _stubInfo.nonLazyPointerReferenceToBinder, *I);
 
     // Sort targets by name, so stubs and lazy pointers are consistent
     std::vector<const Atom *> targetsNeedingStubs;