[llvm] r210429 - Fix some more moving-from-moved-from objects issues in SmallVector
David Blaikie
dblaikie at gmail.com
Sun Jun 8 09:55:14 PDT 2014
Author: dblaikie
Date: Sun Jun 8 11:55:13 2014
New Revision: 210429
URL: http://llvm.org/viewvc/llvm-project?rev=210429&view=rev
Log:
Fix some more moving-from-moved-from objects issues in SmallVector
(& because it makes it easier to test, this also improves
correctness/performance slightly by moving the last element in an insert
operation, rather than copying it)
Modified:
llvm/trunk/include/llvm/ADT/SmallVector.h
llvm/trunk/unittests/ADT/SmallVectorTest.cpp
Modified: llvm/trunk/include/llvm/ADT/SmallVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallVector.h?rev=210429&r1=210428&r2=210429&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallVector.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallVector.h Sun Jun 8 11:55:13 2014
@@ -516,10 +516,10 @@ public:
this->grow();
I = this->begin()+EltNo;
}
- ::new ((void*) this->end()) T(this->back());
- this->setEnd(this->end()+1);
+ ::new ((void*) this->end()) T(std::move(this->back()));
// Push everything else over.
this->move_backward(I, this->end()-1, this->end());
+ this->setEnd(this->end()+1);
// If we just moved the element we're inserting, be sure to update
// the reference.
Modified: llvm/trunk/unittests/ADT/SmallVectorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SmallVectorTest.cpp?rev=210429&r1=210428&r2=210429&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/SmallVectorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/SmallVectorTest.cpp Sun Jun 8 11:55:13 2014
@@ -42,12 +42,15 @@ public:
}
Constructable(const Constructable & src) : constructed(true) {
+ EXPECT_TRUE(src.constructed);
value = src.value;
++numConstructorCalls;
}
Constructable(Constructable && src) : constructed(true) {
+ EXPECT_TRUE(src.constructed);
value = src.value;
+ src.value = -1;
++numConstructorCalls;
}
@@ -59,6 +62,7 @@ public:
Constructable & operator=(const Constructable & src) {
EXPECT_TRUE(constructed);
+ EXPECT_TRUE(src.constructed);
value = src.value;
++numAssignmentCalls;
return *this;
@@ -66,7 +70,9 @@ public:
Constructable & operator=(Constructable && src) {
EXPECT_TRUE(constructed);
+ EXPECT_TRUE(src.constructed);
value = src.value;
+ src.value = -1;
++numAssignmentCalls;
return *this;
}
@@ -412,6 +418,18 @@ TYPED_TEST(SmallVectorTest, InsertTest)
EXPECT_EQ(this->theVector.begin() + 1, I);
this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
}
+
+// Insert a copy of a single element.
+TYPED_TEST(SmallVectorTest, InsertCopy) {
+ SCOPED_TRACE("InsertTest");
+
+ this->makeSequence(this->theVector, 1, 3);
+ Constructable C(77);
+ typename TypeParam::iterator I =
+ this->theVector.insert(this->theVector.begin() + 1, C);
+ EXPECT_EQ(this->theVector.begin() + 1, I);
+ this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
+}
// Insert repeated elements.
TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
More information about the llvm-commits
mailing list