From 3c81d6240b95decab635819959b36fa5b6d0079e Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Thu, 18 Feb 2016 21:00:08 +0000 Subject: [PATCH] [ADT] Fix PointerEmbeddedInt when the underlying type is uintptr_t. ...and when you try to store negative values in it. llvm-svn: 261259 --- llvm/include/llvm/ADT/PointerEmbeddedInt.h | 25 ++++++++++++++------ llvm/unittests/ADT/PointerEmbeddedIntTest.cpp | 34 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/llvm/include/llvm/ADT/PointerEmbeddedInt.h b/llvm/include/llvm/ADT/PointerEmbeddedInt.h index 9562819..546597f 100644 --- a/llvm/include/llvm/ADT/PointerEmbeddedInt.h +++ b/llvm/include/llvm/ADT/PointerEmbeddedInt.h @@ -11,6 +11,7 @@ #define LLVM_ADT_POINTEREMBEDDEDINT_H #include "llvm/ADT/DenseMapInfo.h" +#include "llvm/Support/MathExtras.h" #include "llvm/Support/PointerLikeTypeTraits.h" #include @@ -30,6 +31,8 @@ template class PointerEmbeddedInt { uintptr_t Value; + // Note: This '<' is correct; using '<=' would result in some shifts + // overflowing their storage types. static_assert(Bits < sizeof(uintptr_t) * CHAR_BIT, "Cannot embed more bits than we have in a pointer!"); @@ -42,26 +45,34 @@ class PointerEmbeddedInt { Mask = static_cast(-1) << Bits }; + static constexpr const struct RawValueTag {} RawValue = RawValueTag(); + friend class PointerLikeTypeTraits; - explicit PointerEmbeddedInt(uintptr_t Value) : Value(Value) {} + explicit PointerEmbeddedInt(uintptr_t Value, RawValueTag) : Value(Value) {} public: PointerEmbeddedInt() : Value(0) {} - PointerEmbeddedInt(IntT I) : Value(static_cast(I) << Shift) { - assert((I & Mask) == 0 && "Integer has bits outside those preserved!"); + PointerEmbeddedInt(IntT I) { + *this = I; } PointerEmbeddedInt &operator=(IntT I) { - assert((I & Mask) == 0 && "Integer has bits outside those preserved!"); + assert((std::is_signed::value ? llvm::isInt(I) + : llvm::isUInt(I)) && + "Integer has bits outside those preserved!"); Value = static_cast(I) << Shift; return *this; } // Note that this implicit conversion additionally allows all of the basic // comparison operators to work transparently, etc. - operator IntT() const { return static_cast(Value >> Shift); } + operator IntT() const { + if (std::is_signed::value) + return static_cast(static_cast(Value) >> Shift); + return static_cast(Value >> Shift); + } }; // Provide pointer like traits to support use with pointer unions and sum @@ -75,10 +86,10 @@ public: return reinterpret_cast(P.Value); } static inline T getFromVoidPointer(void *P) { - return T(reinterpret_cast(P)); + return T(reinterpret_cast(P), T::RawValue); } static inline T getFromVoidPointer(const void *P) { - return T(reinterpret_cast(P)); + return T(reinterpret_cast(P), T::RawValue); } enum { NumLowBitsAvailable = T::Shift }; diff --git a/llvm/unittests/ADT/PointerEmbeddedIntTest.cpp b/llvm/unittests/ADT/PointerEmbeddedIntTest.cpp index b10365a..9c27f8e 100644 --- a/llvm/unittests/ADT/PointerEmbeddedIntTest.cpp +++ b/llvm/unittests/ADT/PointerEmbeddedIntTest.cpp @@ -43,4 +43,38 @@ TEST(PointerEmbeddedIntTest, Basic) { EXPECT_FALSE(42 >= J); } +TEST(PointerEmbeddedIntTest, intptr_t) { + PointerEmbeddedInt IPos = 42, INeg = -42; + EXPECT_EQ(42, IPos); + EXPECT_EQ(-42, INeg); + + PointerEmbeddedInt U = 42, USaturated = 255; + EXPECT_EQ(42U, U); + EXPECT_EQ(255U, USaturated); + + PointerEmbeddedInt::digits> + IMax = std::numeric_limits::max() >> 1, + IMin = std::numeric_limits::min() >> 1; + EXPECT_EQ(std::numeric_limits::max() >> 1, IMax); + EXPECT_EQ(std::numeric_limits::min() >> 1, IMin); + + PointerEmbeddedInt::digits - 1> + UMax = std::numeric_limits::max() >> 1, + UMin = std::numeric_limits::min() >> 1; + EXPECT_EQ(std::numeric_limits::max() >> 1, UMax); + EXPECT_EQ(std::numeric_limits::min() >> 1, UMin); +} + +TEST(PointerEmbeddedIntTest, PointerLikeTypeTraits) { + PointerEmbeddedInt I = 42; + using ITraits = PointerLikeTypeTraits; + EXPECT_EQ(42, ITraits::getFromVoidPointer(ITraits::getAsVoidPointer(I))); + + PointerEmbeddedInt::digits - 1> + Max = std::numeric_limits::max() >> 1; + using MaxTraits = PointerLikeTypeTraits; + EXPECT_EQ(std::numeric_limits::max() >> 1, + MaxTraits::getFromVoidPointer(MaxTraits::getAsVoidPointer(Max))); +} + } // end anonymous namespace -- 2.7.4