Add TinyPtrVector support for general pointer-like things.
In particular, make TinyPtrVector<PtrIntPair<T *, 1>> work. Remove all
unnecessary assumptions that the element type has a formal "null"
representation. The important property to maintain is that
default-constructed element type has the same internal representation
as the default-constructed PointerUnion (all zero bits).
Remove the incorrect recursive behavior from
PointerUnion::isNull. This was never generally correct because it only
recursed over the first type parameter. With variadic templates it's
completely unnecessary.
llvm-svn: 369473
diff --git a/llvm/unittests/ADT/PointerUnionTest.cpp b/llvm/unittests/ADT/PointerUnionTest.cpp
index cd6e980..0a88114 100644
--- a/llvm/unittests/ADT/PointerUnionTest.cpp
+++ b/llvm/unittests/ADT/PointerUnionTest.cpp
@@ -13,14 +13,25 @@
namespace {
typedef PointerUnion<int *, float *> PU;
+typedef PointerUnion3<int *, float *, long long *> PU3;
+typedef PointerUnion4<int *, float *, long long *, double *> PU4;
struct PointerUnionTest : public testing::Test {
float f;
int i;
+ double d;
+ long long l;
PU a, b, c, n;
+ PU3 i3, f3, l3;
+ PU4 i4, f4, l4, d4;
+ PU4 i4null, f4null, l4null, d4null;
- PointerUnionTest() : f(3.14f), i(42), a(&f), b(&i), c(&i), n() {}
+ PointerUnionTest()
+ : f(3.14f), i(42), d(3.14), l(42), a(&f), b(&i), c(&i), n(), i3(&i),
+ f3(&f), l3(&l), i4(&i), f4(&f), l4(&l), d4(&d), i4null((int *)nullptr),
+ f4null((float *)nullptr), l4null((long long *)nullptr),
+ d4null((double *)nullptr) {}
};
TEST_F(PointerUnionTest, Comparison) {
@@ -32,6 +43,19 @@
EXPECT_FALSE(b != c);
EXPECT_TRUE(b != n);
EXPECT_FALSE(b == n);
+ EXPECT_TRUE(i3 == i3);
+ EXPECT_FALSE(i3 != i3);
+ EXPECT_TRUE(i3 != f3);
+ EXPECT_TRUE(f3 != l3);
+ EXPECT_TRUE(i4 == i4);
+ EXPECT_FALSE(i4 != i4);
+ EXPECT_TRUE(i4 != f4);
+ EXPECT_TRUE(i4 != l4);
+ EXPECT_TRUE(f4 != l4);
+ EXPECT_TRUE(l4 != d4);
+ EXPECT_TRUE(i4null != f4null);
+ EXPECT_TRUE(i4null != l4null);
+ EXPECT_TRUE(i4null != d4null);
}
TEST_F(PointerUnionTest, Null) {
@@ -51,6 +75,17 @@
b = nullptr;
EXPECT_EQ(n, b);
EXPECT_NE(b, c);
+ EXPECT_FALSE(i3.isNull());
+ EXPECT_FALSE(f3.isNull());
+ EXPECT_FALSE(l3.isNull());
+ EXPECT_FALSE(i4.isNull());
+ EXPECT_FALSE(f4.isNull());
+ EXPECT_FALSE(l4.isNull());
+ EXPECT_FALSE(d4.isNull());
+ EXPECT_TRUE(i4null.isNull());
+ EXPECT_TRUE(f4null.isNull());
+ EXPECT_TRUE(l4null.isNull());
+ EXPECT_TRUE(d4null.isNull());
}
TEST_F(PointerUnionTest, Is) {
@@ -60,6 +95,17 @@
EXPECT_FALSE(b.is<float *>());
EXPECT_TRUE(n.is<int *>());
EXPECT_FALSE(n.is<float *>());
+ EXPECT_TRUE(i3.is<int *>());
+ EXPECT_TRUE(f3.is<float *>());
+ EXPECT_TRUE(l3.is<long long *>());
+ EXPECT_TRUE(i4.is<int *>());
+ EXPECT_TRUE(f4.is<float *>());
+ EXPECT_TRUE(l4.is<long long *>());
+ EXPECT_TRUE(d4.is<double *>());
+ EXPECT_TRUE(i4null.is<int *>());
+ EXPECT_TRUE(f4null.is<float *>());
+ EXPECT_TRUE(l4null.is<long long *>());
+ EXPECT_TRUE(d4null.is<double *>());
}
TEST_F(PointerUnionTest, Get) {
@@ -105,4 +151,9 @@
EXPECT_TRUE(a != PU8(&a0));
}
+TEST_F(PointerUnionTest, GetAddrOfPtr1) {
+ EXPECT_TRUE((void *)b.getAddrOfPtr1() == (void *)&b);
+ EXPECT_TRUE((void *)n.getAddrOfPtr1() == (void *)&n);
+}
+
} // end anonymous namespace
diff --git a/llvm/unittests/ADT/TinyPtrVectorTest.cpp b/llvm/unittests/ADT/TinyPtrVectorTest.cpp
index c8c00d9..f885347 100644
--- a/llvm/unittests/ADT/TinyPtrVectorTest.cpp
+++ b/llvm/unittests/ADT/TinyPtrVectorTest.cpp
@@ -22,12 +22,21 @@
using namespace llvm;
namespace {
+template <typename T> struct RemovePointer : std::remove_pointer<T> {};
+
+template <typename PointerTy, unsigned IntBits, typename IntType,
+ typename PtrTraits, typename Info>
+struct RemovePointer<
+ PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>> {
+ typedef typename RemovePointer<PointerTy>::type type;
+};
template <typename VectorT>
class TinyPtrVectorTest : public testing::Test {
protected:
typedef typename VectorT::value_type PtrT;
- typedef typename std::remove_pointer<PtrT>::type ValueT;
+ typedef typename RemovePointer<PtrT>::type ValueT;
+ using PtrTraits = PointerLikeTypeTraits<PtrT>;
VectorT V;
VectorT V2;
@@ -37,11 +46,13 @@
TinyPtrVectorTest() {
for (size_t i = 0, e = array_lengthof(TestValues); i != e; ++i)
- TestPtrs.push_back(&TestValues[i]);
+ TestPtrs.push_back(PtrT(&TestValues[i]));
std::shuffle(TestPtrs.begin(), TestPtrs.end(), std::mt19937{});
}
+ PtrT makePtr(ValueT *V) { return PtrT(V); }
+
ArrayRef<PtrT> testArray(size_t N) {
return makeArrayRef(&TestPtrs[0], N);
}
@@ -69,9 +80,9 @@
}
};
-typedef ::testing::Types<TinyPtrVector<int*>,
- TinyPtrVector<double*>
- > TinyPtrVectorTestTypes;
+typedef ::testing::Types<TinyPtrVector<int *>, TinyPtrVector<double *>,
+ TinyPtrVector<PointerIntPair<int *, 1>>>
+ TinyPtrVectorTestTypes;
TYPED_TEST_CASE(TinyPtrVectorTest, TinyPtrVectorTestTypes);
TYPED_TEST(TinyPtrVectorTest, EmptyTest) {
@@ -95,8 +106,8 @@
this->expectValues(this->V, this->testArray(4));
this->V.pop_back();
this->expectValues(this->V, this->testArray(3));
- this->TestPtrs[3] = &this->TestValues[42];
- this->TestPtrs[4] = &this->TestValues[43];
+ this->TestPtrs[3] = this->makePtr(&this->TestValues[42]);
+ this->TestPtrs[4] = this->makePtr(&this->TestValues[43]);
this->V.push_back(this->TestPtrs[3]);
this->expectValues(this->V, this->testArray(4));
this->V.push_back(this->TestPtrs[4]);