[ArrayRef] Make copy use std::uninitialized_copy.

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

llvm-svn: 243995
This commit is contained in:
Benjamin Kramer 2015-08-04 15:52:56 +00:00
parent 924879ad2c
commit 07f42cd526
2 changed files with 14 additions and 2 deletions

View File

@ -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);
}

View File

@ -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) {