llvm.org GIT mirror llvm / 2fbc59f
Fix PointerIntPair so that it can use an enum class as its integer template argument. Summary: The problem here is that an enum class can not be implicitly converted to an integer. That assumption snuck back into PointerIntPair. This commit fixes the issue and more importantly adds some unittests to make sure that we do not break this again. rdar://23594806 Reviewers: gribozavr Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D16131 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@257574 91177308-0d34-0410-b5e6-96231b3b80d8 Michael Gottesman 3 years ago
2 changed file(s) with 35 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
5454
5555 PointerTy getPointer() const { return Info::getPointer(Value); }
5656
57 IntType getInt() const { return (IntType)Info::getInt(Value); }
57 IntType getInt() const {
58 return (IntType)Info::getInt(Value);
59 }
5860
5961 void setPointer(PointerTy PtrVal) {
6062 Value = Info::updatePointer(Value, PtrVal);
6163 }
6264
63 void setInt(IntType IntVal) { Value = Info::updateInt(Value, IntVal); }
65 void setInt(IntType IntVal) {
66 Value = Info::updateInt(Value, static_cast(IntVal));
67 }
6468
6569 void initWithPointer(PointerTy PtrVal) {
6670 Value = Info::updatePointer(0, PtrVal);
6771 }
6872
6973 void setPointerAndInt(PointerTy PtrVal, IntType IntVal) {
70 Value = Info::updateInt(Info::updatePointer(0, PtrVal), IntVal);
74 Value = Info::updateInt(Info::updatePointer(0, PtrVal),
75 static_cast(IntVal));
7176 }
7277
7378 PointerTy const *getAddrOfPointer() const {
3434 Pair.setPointerAndInt(&s, 3U);
3535 EXPECT_EQ(&s, Pair.getPointer());
3636 EXPECT_EQ(3U, Pair.getInt());
37
38 // Make sure that we can perform all of our operations on enum classes.
39 //
40 // The concern is that enum classes are only explicitly convertible to
41 // integers. This means that if we assume in PointerIntPair this, a
42 // compilation error will result. This group of tests exercises the enum class
43 // code to make sure that we do not run into such issues in the future.
44 enum class E : unsigned {
45 Case1,
46 Case2,
47 Case3,
48 };
49 PointerIntPair Pair2(&s, E::Case1);
50 EXPECT_EQ(&s, Pair2.getPointer());
51 EXPECT_EQ(E::Case1, Pair2.getInt());
52
53 Pair2.setInt(E::Case2);
54 EXPECT_EQ(&s, Pair2.getPointer());
55 EXPECT_EQ(E::Case2, Pair2.getInt());
56
57 Pair2.setPointer(nullptr);
58 EXPECT_EQ(nullptr, Pair2.getPointer());
59 EXPECT_EQ(E::Case2, Pair2.getInt());
60
61 Pair2.setPointerAndInt(&s, E::Case3);
62 EXPECT_EQ(&s, Pair2.getPointer());
63 EXPECT_EQ(E::Case3, Pair2.getInt());
3764 }
3865
3966 TEST(PointerIntPairTest, DefaultInitialize) {