[ADT] Explicitly delete copy/move constructors and operator= in IntervalMap

The default implementations will perform a shallow copy instead of a deep
copy, causing some internal data structures to be shared between different
objects. Disable these operations so they don't get accidentally used.

Differential Revision: https://reviews.llvm.org/D126401
This commit is contained in:
Krzysztof Parzyszek 2022-05-25 11:20:02 -07:00
parent 3087afb421
commit aee6b8efd0
2 changed files with 21 additions and 0 deletions

View File

@ -1042,6 +1042,17 @@ public:
new(&rootLeaf()) RootLeaf();
}
// The default copy/move constructors and assignment operators would perform
// a shallow copy, leading to an incorrect internal state. To prevent
// accidental use, explicitly delete these operators.
// If necessary, implement them to perform a deep copy.
IntervalMap(const IntervalMap &Other) = delete;
IntervalMap(IntervalMap &&Other) = delete;
// Note: these are already implicitly deleted, because RootLeaf (union
// member) has a non-trivial assignment operator (because of std::pair).
IntervalMap &operator=(const IntervalMap &Other) = delete;
IntervalMap &operator=(IntervalMap &&Other) = delete;
~IntervalMap() {
clear();
rootLeaf().~RootLeaf();

View File

@ -8,6 +8,7 @@
#include "llvm/ADT/IntervalMap.h"
#include "gtest/gtest.h"
#include <type_traits>
using namespace llvm;
@ -17,6 +18,15 @@ typedef IntervalMap<unsigned, unsigned, 4> UUMap;
typedef IntervalMap<unsigned, unsigned, 4,
IntervalMapHalfOpenInfo<unsigned>> UUHalfOpenMap;
static_assert(!std::is_copy_constructible<UUMap>::value,
"IntervalMap copy constructor should be deleted");
static_assert(!std::is_move_constructible<UUMap>::value,
"IntervalMap move constructor should be deleted");
static_assert(!std::is_copy_assignable<UUMap>::value,
"IntervalMap copy assignment should be deleted");
static_assert(!std::is_move_assignable<UUMap>::value,
"IntervalMap move assignment should be deleted");
// Empty map tests
TEST(IntervalMapTest, EmptyMap) {
UUMap::Allocator allocator;