[libcxx] r289735 - Fix PR31378 - std::list::remove should not require a default constructible allocator.

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 14 14:48:38 PST 2016


Author: ericwf
Date: Wed Dec 14 16:48:38 2016
New Revision: 289735

URL: http://llvm.org/viewvc/llvm-project?rev=289735&view=rev
Log:
Fix PR31378 - std::list::remove should not require a default constructible allocator.

In list::remove we collect the nodes we're removing in a seperate
list instance. However we construct this list using the default
constructor which default constructs the allocator. However allocators
are not required to be default constructible. This patch fixes the
construction of the second list.

Modified:
    libcxx/trunk/include/list
    libcxx/trunk/test/std/containers/sequences/list/list.ops/remove.pass.cpp
    libcxx/trunk/test/support/controlled_allocators.hpp
    libcxx/trunk/test/support/min_allocator.h

Modified: libcxx/trunk/include/list
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/list?rev=289735&r1=289734&r2=289735&view=diff
==============================================================================
--- libcxx/trunk/include/list (original)
+++ libcxx/trunk/include/list Wed Dec 14 16:48:38 2016
@@ -2091,7 +2091,7 @@ template <class _Tp, class _Alloc>
 void
 list<_Tp, _Alloc>::remove(const value_type& __x)
 {
-    list<_Tp, _Alloc> __deleted_nodes; // collect the nodes we're removing
+    list<_Tp, _Alloc> __deleted_nodes(get_allocator()); // collect the nodes we're removing
     for (const_iterator __i = begin(), __e = end(); __i != __e;)
     {
         if (*__i == __x)

Modified: libcxx/trunk/test/std/containers/sequences/list/list.ops/remove.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/containers/sequences/list/list.ops/remove.pass.cpp?rev=289735&r1=289734&r2=289735&view=diff
==============================================================================
--- libcxx/trunk/test/std/containers/sequences/list/list.ops/remove.pass.cpp (original)
+++ libcxx/trunk/test/std/containers/sequences/list/list.ops/remove.pass.cpp Wed Dec 14 16:48:38 2016
@@ -14,56 +14,70 @@
 #include <list>
 #include <cassert>
 
+#include "test_macros.h"
 #include "min_allocator.h"
 
 struct S {
-    S(int i) : i_(new int(i)) {}
-    S(const S &rhs) : i_(new int(*rhs.i_)) {}
-    S& operator = (const S &rhs) { *i_ = *rhs.i_; return *this; }
-    ~S () { delete i_; i_ = NULL; }
-    bool operator == (const S &rhs) const { return *i_ == *rhs.i_; }
-    int get () const { return *i_; }
-    int *i_;
-    };
+  S(int i) : i_(new int(i)) {}
+  S(const S &rhs) : i_(new int(*rhs.i_)) {}
+  S &operator=(const S &rhs) {
+    *i_ = *rhs.i_;
+    return *this;
+  }
+  ~S() {
+    delete i_;
+    i_ = NULL;
+  }
+  bool operator==(const S &rhs) const { return *i_ == *rhs.i_; }
+  int get() const { return *i_; }
+  int *i_;
+};
 
-
-int main()
-{
-    {
+int main() {
+  {
     int a1[] = {1, 2, 3, 4};
     int a2[] = {1, 2, 4};
-    std::list<int> c(a1, a1+4);
+    std::list<int> c(a1, a1 + 4);
     c.remove(3);
-    assert(c == std::list<int>(a2, a2+3));
-    }
-    {  // LWG issue #526
+    assert(c == std::list<int>(a2, a2 + 3));
+  }
+  { // LWG issue #526
     int a1[] = {1, 2, 1, 3, 5, 8, 11};
-    int a2[] = {   2,    3, 5, 8, 11};
-    std::list<int> c(a1, a1+7);
+    int a2[] = {2, 3, 5, 8, 11};
+    std::list<int> c(a1, a1 + 7);
     c.remove(c.front());
-    assert(c == std::list<int>(a2, a2+5));
-    }
-    {
+    assert(c == std::list<int>(a2, a2 + 5));
+  }
+  {
     int a1[] = {1, 2, 1, 3, 5, 8, 11, 1};
-    int a2[] = {   2,    3, 5, 8, 11   };
+    int a2[] = {2, 3, 5, 8, 11};
     std::list<S> c;
-    for(int *ip = a1; ip < a1+8; ++ip)
-        c.push_back(S(*ip));
+    for (int *ip = a1; ip < a1 + 8; ++ip)
+      c.push_back(S(*ip));
     c.remove(c.front());
     std::list<S>::const_iterator it = c.begin();
-    for(int *ip = a2; ip < a2+5; ++ip, ++it) {
-        assert ( it != c.end());
-        assert ( *ip == it->get());
-        }
-    assert ( it == c.end ());
+    for (int *ip = a2; ip < a2 + 5; ++ip, ++it) {
+      assert(it != c.end());
+      assert(*ip == it->get());
     }
+    assert(it == c.end());
+  }
+  {
+    typedef no_default_allocator<int> Alloc;
+    typedef std::list<int, Alloc> List;
+    int a1[] = {1, 2, 3, 4};
+    int a2[] = {1, 2, 4};
+    List c(a1, a1 + 4, Alloc::create());
+    c.remove(3);
+    assert(c == List(a2, a2 + 3, Alloc::create()));
+  }
 #if TEST_STD_VER >= 11
-    {
+  {
     int a1[] = {1, 2, 3, 4};
     int a2[] = {1, 2, 4};
-    std::list<int, min_allocator<int>> c(a1, a1+4);
+    std::list<int, min_allocator<int>> c(a1, a1 + 4);
     c.remove(3);
-    assert((c == std::list<int, min_allocator<int>>(a2, a2+3)));
-    }
+    assert((c == std::list<int, min_allocator<int>>(a2, a2 + 3)));
+  }
 #endif
 }

Modified: libcxx/trunk/test/support/controlled_allocators.hpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/controlled_allocators.hpp?rev=289735&r1=289734&r2=289735&view=diff
==============================================================================
--- libcxx/trunk/test/support/controlled_allocators.hpp (original)
+++ libcxx/trunk/test/support/controlled_allocators.hpp Wed Dec 14 16:48:38 2016
@@ -20,6 +20,10 @@
 #include "test_macros.h"
 #include "type_id.h"
 
+#if TEST_STD_VER < 11
+#error This header requires C++11 or greater
+#endif
+
 struct AllocController;
     // 'AllocController' is a concrete type that instruments and controls the
     // behavior of of test allocators.

Modified: libcxx/trunk/test/support/min_allocator.h
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/support/min_allocator.h?rev=289735&r1=289734&r2=289735&view=diff
==============================================================================
--- libcxx/trunk/test/support/min_allocator.h (original)
+++ libcxx/trunk/test/support/min_allocator.h Wed Dec 14 16:48:38 2016
@@ -42,6 +42,44 @@ public:
     friend bool operator!=(bare_allocator x, bare_allocator y) {return !(x == y);}
 };
 
+
+template <class T>
+class no_default_allocator
+{
+#if TEST_STD_VER >= 11
+    no_default_allocator() = delete;
+#else
+    no_default_allocator();
+#endif
+    struct construct_tag {};
+    explicit no_default_allocator(construct_tag) {}
+
+public:
+    static no_default_allocator create() {
+      construct_tag tag;
+      return no_default_allocator(tag);
+    }
+
+public:
+    typedef T value_type;
+
+    template <class U>
+    no_default_allocator(no_default_allocator<U>) TEST_NOEXCEPT {}
+
+    T* allocate(std::size_t n)
+    {
+        return static_cast<T*>(::operator new(n*sizeof(T)));
+    }
+
+    void deallocate(T* p, std::size_t)
+    {
+        return ::operator delete(static_cast<void*>(p));
+    }
+
+    friend bool operator==(no_default_allocator, no_default_allocator) {return true;}
+    friend bool operator!=(no_default_allocator x, no_default_allocator y) {return !(x == y);}
+};
+
 struct malloc_allocator_base {
     static size_t alloc_count;
     static size_t dealloc_count;




More information about the cfe-commits mailing list