Skip to content

Commit aee6b8e

Browse files
author
Krzysztof Parzyszek
committed
[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
1 parent 3087afb commit aee6b8e

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

llvm/include/llvm/ADT/IntervalMap.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,17 @@ class IntervalMap {
10421042
new(&rootLeaf()) RootLeaf();
10431043
}
10441044

1045+
// The default copy/move constructors and assignment operators would perform
1046+
// a shallow copy, leading to an incorrect internal state. To prevent
1047+
// accidental use, explicitly delete these operators.
1048+
// If necessary, implement them to perform a deep copy.
1049+
IntervalMap(const IntervalMap &Other) = delete;
1050+
IntervalMap(IntervalMap &&Other) = delete;
1051+
// Note: these are already implicitly deleted, because RootLeaf (union
1052+
// member) has a non-trivial assignment operator (because of std::pair).
1053+
IntervalMap &operator=(const IntervalMap &Other) = delete;
1054+
IntervalMap &operator=(IntervalMap &&Other) = delete;
1055+
10451056
~IntervalMap() {
10461057
clear();
10471058
rootLeaf().~RootLeaf();

llvm/unittests/ADT/IntervalMapTest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/ADT/IntervalMap.h"
1010
#include "gtest/gtest.h"
11+
#include <type_traits>
1112

1213
using namespace llvm;
1314

@@ -17,6 +18,15 @@ typedef IntervalMap<unsigned, unsigned, 4> UUMap;
1718
typedef IntervalMap<unsigned, unsigned, 4,
1819
IntervalMapHalfOpenInfo<unsigned>> UUHalfOpenMap;
1920

21+
static_assert(!std::is_copy_constructible<UUMap>::value,
22+
"IntervalMap copy constructor should be deleted");
23+
static_assert(!std::is_move_constructible<UUMap>::value,
24+
"IntervalMap move constructor should be deleted");
25+
static_assert(!std::is_copy_assignable<UUMap>::value,
26+
"IntervalMap copy assignment should be deleted");
27+
static_assert(!std::is_move_assignable<UUMap>::value,
28+
"IntervalMap move assignment should be deleted");
29+
2030
// Empty map tests
2131
TEST(IntervalMapTest, EmptyMap) {
2232
UUMap::Allocator allocator;

0 commit comments

Comments
 (0)