Make directory iterator sentinels free.
authorBenjamin Kramer <benny.kra@googlemail.com>
Fri, 9 Aug 2013 17:03:39 +0000 (17:03 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Fri, 9 Aug 2013 17:03:39 +0000 (17:03 +0000)
This trades some complexity in operator== for not introducing static objects
into any functions using recursive directory iterators.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@188081 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/FileSystem.h

index c130b478e3188d681cb404b8c2d6f3631d1945e6..fb0eaeb8dbcf0181b5c04c536bb2ab440b4df7fc 100644 (file)
@@ -869,7 +869,7 @@ public:
   }
 
   /// Construct end iterator.
-  directory_iterator() : State(new detail::DirIterState) {}
+  directory_iterator() : State(0) {}
 
   // No operator++ because we need error_code.
   directory_iterator &increment(error_code &ec) {
@@ -881,6 +881,12 @@ public:
   const directory_entry *operator->() const { return &State->CurrentEntry; }
 
   bool operator==(const directory_iterator &RHS) const {
+    if (State == RHS.State)
+      return true;
+    if (RHS.State == 0)
+      return State->CurrentEntry == directory_entry();
+    if (State == 0)
+      return RHS.State->CurrentEntry == directory_entry();
     return State->CurrentEntry == RHS.State->CurrentEntry;
   }
 
@@ -920,7 +926,7 @@ public:
   }
   // No operator++ because we need error_code.
   recursive_directory_iterator &increment(error_code &ec) {
-    static const directory_iterator end_itr;
+    const directory_iterator end_itr;
 
     if (State->HasNoPushRequest)
       State->HasNoPushRequest = false;
@@ -967,7 +973,7 @@ public:
     assert(State && "Cannot pop and end itertor!");
     assert(State->Level > 0 && "Cannot pop an iterator with level < 1");
 
-    static const directory_iterator end_itr;
+    const directory_iterator end_itr;
     error_code ec;
     do {
       if (ec)