From 56d0d2cd0fc9b1e98ea50d444161286216e16f0a Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Mon, 6 May 2013 16:38:42 -0700 Subject: [PATCH] Bug 869238 - Silence various -Wtype-limit warnings in Casting.h when instantiated with types with various size relationships. r=froydnj --- mfbt/Casting.h | 76 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/mfbt/Casting.h b/mfbt/Casting.h index 753bd675289..581bdafae1b 100644 --- a/mfbt/Casting.h +++ b/mfbt/Casting.h @@ -31,17 +31,43 @@ struct BoundsCheckImpl; // that are obviously the same type (and will undergo no further conversions), // even when it's not strictly necessary, for explicitness. +enum UUComparison { FromIsBigger, FromIsNotBigger }; + +// Unsigned-to-unsigned range check + +template sizeof(To)) ? FromIsBigger : FromIsNotBigger> +struct UnsignedUnsignedCheck; + +template +struct UnsignedUnsignedCheck +{ + public: + static bool check(const From from) { + return from <= From(To(-1)); + } +}; + +template +struct UnsignedUnsignedCheck +{ + public: + static bool check(const From from) { + return true; + } +}; + template struct BoundsCheckImpl { public: static bool check(const From from) { - typedef typename Conditional= sizeof(To), From, To>::Type - LargerType; - return LargerType(from) <= LargerType(To(-1)); + return UnsignedUnsignedCheck::check(from); } }; +// Signed-to-unsigned range check + template struct BoundsCheckImpl { @@ -50,34 +76,60 @@ struct BoundsCheckImpl if (from < 0) return false; if (sizeof(To) >= sizeof(From)) - return To(from) <= To(-1); + return true; return from <= From(To(-1)); } }; +// Unsigned-to-signed range check + +enum USComparison { FromIsSmaller, FromIsNotSmaller }; + +template +struct UnsignedSignedCheck; + +template +struct UnsignedSignedCheck +{ + public: + static bool check(const From from) { + return true; + } +}; + +template +struct UnsignedSignedCheck +{ + public: + static bool check(const From from) { + const To MaxValue = To((1ULL << (CHAR_BIT * sizeof(To) - 1)) - 1); + return from <= From(MaxValue); + } +}; + template struct BoundsCheckImpl { public: static bool check(const From from) { - if (sizeof(From) < sizeof(To)) - return true; - const To MaxValue = To((1ULL << (CHAR_BIT * sizeof(To) - 1)) - 1); - return from <= From(MaxValue); + return UnsignedSignedCheck::check(from); } }; +// Signed-to-signed range check + template struct BoundsCheckImpl { public: static bool check(const From from) { - typedef typename Conditional= sizeof(From), To, From>::Type - LargerType; + if (sizeof(From) <= sizeof(To)) + return true; const To MaxValue = To((1ULL << (CHAR_BIT * sizeof(To) - 1)) - 1); const To MinValue = -MaxValue - To(1); - return LargerType(MinValue) <= LargerType(from) && - LargerType(from) <= LargerType(MaxValue); + return From(MinValue) <= from && + From(from) <= From(MaxValue); } };