Fix some more moving-from-moved-from objects issues in SmallVector
authorDavid Blaikie <dblaikie@gmail.com>
Sun, 8 Jun 2014 16:55:13 +0000 (16:55 +0000)
committerDavid Blaikie <dblaikie@gmail.com>
Sun, 8 Jun 2014 16:55:13 +0000 (16:55 +0000)
(& 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)

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

include/llvm/ADT/SmallVector.h
unittests/ADT/SmallVectorTest.cpp

index 29ad250010e6ba279317f8b3814813d88a6a0a71..94bd9cc7b2ca4024bb2762b046762c7afe15968f 100644 (file)
@@ -516,10 +516,10 @@ public:
       this->grow();
       I = this->begin()+EltNo;
     }
       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());
     // 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.
 
     // If we just moved the element we're inserting, be sure to update
     // the reference.
index cccf93b6bc50f00de4fd213a4517ce09d9064f4c..935c761ca78ef63fd86c5e5dd266e1c1f11f2cbc 100644 (file)
@@ -42,12 +42,15 @@ public:
   }
 
   Constructable(const Constructable & src) : constructed(true) {
   }
 
   Constructable(const Constructable & src) : constructed(true) {
+    EXPECT_TRUE(src.constructed);
     value = src.value;
     ++numConstructorCalls;
   }
 
   Constructable(Constructable && src) : constructed(true) {
     value = src.value;
     ++numConstructorCalls;
   }
 
   Constructable(Constructable && src) : constructed(true) {
+    EXPECT_TRUE(src.constructed);
     value = src.value;
     value = src.value;
+    src.value = -1;
     ++numConstructorCalls;
   }
 
     ++numConstructorCalls;
   }
 
@@ -59,6 +62,7 @@ public:
 
   Constructable & operator=(const Constructable & src) {
     EXPECT_TRUE(constructed);
 
   Constructable & operator=(const Constructable & src) {
     EXPECT_TRUE(constructed);
+    EXPECT_TRUE(src.constructed);
     value = src.value;
     ++numAssignmentCalls;
     return *this;
     value = src.value;
     ++numAssignmentCalls;
     return *this;
@@ -66,7 +70,9 @@ public:
 
   Constructable & operator=(Constructable && src) {
     EXPECT_TRUE(constructed);
 
   Constructable & operator=(Constructable && src) {
     EXPECT_TRUE(constructed);
+    EXPECT_TRUE(src.constructed);
     value = src.value;
     value = src.value;
+    src.value = -1;
     ++numAssignmentCalls;
     return *this;
   }
     ++numAssignmentCalls;
     return *this;
   }
@@ -413,6 +419,18 @@ TYPED_TEST(SmallVectorTest, InsertTest) {
   this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
 }
 
   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) {
   SCOPED_TRACE("InsertRepeatedTest");
 // Insert repeated elements.
 TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
   SCOPED_TRACE("InsertRepeatedTest");