[llvm] r243995 - [ArrayRef] Make copy use std::uninitialized_copy.

Benjamin Kramer benny.kra at googlemail.com
Tue Aug 4 08:52:56 PDT 2015


Author: d0k
Date: Tue Aug  4 10:52:56 2015
New Revision: 243995

URL: http://llvm.org/viewvc/llvm-project?rev=243995&view=rev
Log:
[ArrayRef] Make copy use std::uninitialized_copy.

std::copy does not work for non-trivially copyable classes when we're
copying into uninitialized memory.

Modified:
    llvm/trunk/include/llvm/ADT/ArrayRef.h
    llvm/trunk/unittests/ADT/ArrayRefTest.cpp

Modified: llvm/trunk/include/llvm/ADT/ArrayRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ArrayRef.h?rev=243995&r1=243994&r2=243995&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/ArrayRef.h (original)
+++ llvm/trunk/include/llvm/ADT/ArrayRef.h Tue Aug  4 10:52:56 2015
@@ -148,7 +148,7 @@ namespace llvm {
     // copy - Allocate copy in Allocator and return ArrayRef<T> to it.
     template <typename Allocator> ArrayRef<T> copy(Allocator &A) {
       T *Buff = A.template Allocate<T>(Length);
-      std::copy(begin(), end(), Buff);
+      std::uninitialized_copy(begin(), end(), Buff);
       return ArrayRef<T>(Buff, Length);
     }
 

Modified: llvm/trunk/unittests/ADT/ArrayRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ArrayRefTest.cpp?rev=243995&r1=243994&r2=243995&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/ArrayRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/ArrayRefTest.cpp Tue Aug  4 10:52:56 2015
@@ -31,7 +31,7 @@ static_assert(
     !std::is_convertible<ArrayRef<volatile int *>, ArrayRef<int *>>::value,
     "Removing volatile");
 
-namespace llvm {
+namespace {
 
 TEST(ArrayRefTest, AllocatorCopy) {
   BumpPtrAllocator Alloc;
@@ -45,6 +45,18 @@ TEST(ArrayRefTest, AllocatorCopy) {
   EXPECT_NE(Array1.data(), Array1c.data());
   EXPECT_TRUE(Array2.equals(Array2c));
   EXPECT_NE(Array2.data(), Array2c.data());
+
+  // Check that copy can cope with uninitialized memory.
+  struct NonAssignable {
+    const char *Ptr;
+
+    NonAssignable(const NonAssignable &RHS) = default;
+    void operator=(const NonAssignable &RHS) { assert(RHS.Ptr != nullptr); }
+    bool operator==(const NonAssignable &RHS) const { return Ptr == RHS.Ptr; }
+  } Array3Src[] = {{"hello"}, {"world"}};
+  ArrayRef<NonAssignable> Array3Copy = makeArrayRef(Array3Src).copy(Alloc);
+  EXPECT_EQ(makeArrayRef(Array3Src), Array3Copy);
+  EXPECT_NE(makeArrayRef(Array3Src).data(), Array3Copy.data());
 }
 
 TEST(ArrayRefTest, DropBack) {





More information about the llvm-commits mailing list