diff options
| author | Chandler Carruth <chandlerc@gmail.com> | 2013-11-26 00:54:44 +0000 |
|---|---|---|
| committer | Chandler Carruth <chandlerc@gmail.com> | 2013-11-26 00:54:44 +0000 |
| commit | 480f5d265a4b22eac82139df8d8edcfe7ba059d0 (patch) | |
| tree | 9d78f2dc5f672707e3f53e8c99668622c7a5d699 /llvm | |
| parent | 2664317b66283e149bd436c14659c283f17bba3a (diff) | |
| download | bcm5719-llvm-480f5d265a4b22eac82139df8d8edcfe7ba059d0.tar.gz bcm5719-llvm-480f5d265a4b22eac82139df8d8edcfe7ba059d0.zip | |
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
llvm-svn: 195705
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/include/llvm/ADT/SmallPtrSet.h | 6 | ||||
| -rw-r--r-- | llvm/lib/Support/SmallPtrSet.cpp | 5 |
2 files changed, 7 insertions, 4 deletions
diff --git a/llvm/include/llvm/ADT/SmallPtrSet.h b/llvm/include/llvm/ADT/SmallPtrSet.h index c7f5e5bfc0f..b52aebf7d55 100644 --- a/llvm/include/llvm/ADT/SmallPtrSet.h +++ b/llvm/include/llvm/ADT/SmallPtrSet.h @@ -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 diff --git a/llvm/lib/Support/SmallPtrSet.cpp b/llvm/lib/Support/SmallPtrSet.cpp index fa8d91545e7..f873d91d6e0 100644 --- a/llvm/lib/Support/SmallPtrSet.cpp +++ b/llvm/lib/Support/SmallPtrSet.cpp @@ -218,8 +218,7 @@ SmallPtrSetImpl::SmallPtrSetImpl(const void **SmallStorage, unsigned SmallSize, /// 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 SmallPtrSetImpl &RHS) { #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); |

