[llvm] r195705 - Lift self-copy protection up to the header file and add self-move

Chandler Carruth chandlerc at gmail.com
Mon Nov 25 16:54:44 PST 2013


Author: chandlerc
Date: Mon Nov 25 18:54:44 2013
New Revision: 195705

URL: http://llvm.org/viewvc/llvm-project?rev=195705&view=rev
Log:
Lift self-copy protection up to the header file and add self-move
protection to the same layer.

This is in line with Howard's advice on how best to handle self-move
assignment as he explained on SO[1]. It also ensures that implementing
swap with move assignment continues to work in the case of self-swap.

[1]: http://stackoverflow.com/questions/9322174/move-assignment-operator-and-if-this-rhs

Modified:
    llvm/trunk/include/llvm/ADT/SmallPtrSet.h
    llvm/trunk/lib/Support/SmallPtrSet.cpp

Modified: llvm/trunk/include/llvm/ADT/SmallPtrSet.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/SmallPtrSet.h?rev=195705&r1=195704&r2=195705&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/SmallPtrSet.h (original)
+++ llvm/trunk/include/llvm/ADT/SmallPtrSet.h Mon Nov 25 18:54:44 2013
@@ -293,14 +293,16 @@ public:
 
   SmallPtrSet<PtrType, SmallSize> &
   operator=(const SmallPtrSet<PtrType, SmallSize> &RHS) {
-    CopyFrom(RHS);
+    if (&RHS != this)
+      CopyFrom(RHS);
     return *this;
   }
 
 #if LLVM_HAS_RVALUE_REFERENCES
   SmallPtrSet<PtrType, SmallSize>&
   operator=(SmallPtrSet<PtrType, SmallSize> &&RHS) {
-    MoveFrom(SmallSizePowTwo, std::move(RHS));
+    if (&RHS != this)
+      MoveFrom(SmallSizePowTwo, std::move(RHS));
     return *this;
   }
 #endif

Modified: llvm/trunk/lib/Support/SmallPtrSet.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/SmallPtrSet.cpp?rev=195705&r1=195704&r2=195705&view=diff
==============================================================================
--- llvm/trunk/lib/Support/SmallPtrSet.cpp (original)
+++ llvm/trunk/lib/Support/SmallPtrSet.cpp Mon Nov 25 18:54:44 2013
@@ -218,8 +218,7 @@ SmallPtrSetImpl::SmallPtrSetImpl(const v
 /// CopyFrom - implement operator= from a smallptrset that has the same pointer
 /// type, but may have a different small size.
 void SmallPtrSetImpl::CopyFrom(const SmallPtrSetImpl &RHS) {
-  if (&RHS == this)
-    return;
+  assert(&RHS != this && "Self-copy should be handled by the caller.");
 
   if (isSmall() && RHS.isSmall())
     assert(CurArraySize == RHS.CurArraySize &&
@@ -256,6 +255,8 @@ void SmallPtrSetImpl::CopyFrom(const Sma
 
 #if LLVM_HAS_RVALUE_REFERENCES
 void SmallPtrSetImpl::MoveFrom(unsigned SmallSize, SmallPtrSetImpl &&RHS) {
+  assert(&RHS != this && "Self-move should be handled by the caller.");
+
   if (!isSmall())
     free(CurArray);
 





More information about the llvm-commits mailing list