From 3f9bc2d28c85fd11a7a5418b0749df6c3c7b91d1 Mon Sep 17 00:00:00 2001 From: Billy Robert O'Neal III Date: Mon, 8 May 2017 21:52:05 +0000 Subject: [PATCH] Resolve integer overflow warnings in GCD and LCM tests lcm.pass.cpp: 19: Update headers to that actually used in the test. 41: test0 was triggering narrowing warnings for all callers, because the inputs were always ints, but some of the explicit template arguments were smaller than that. Instead, have this function accept ints and static_cast explicitly to the types we want before calling std::lcm. 47: Replace unnecessary ternary. 55: Use foo_t instead of typename foo<>::type 111/116: intX_t were not std::qualified but only headers were included. 141: C1XX has a bug where it interprets 2147483648 as unsigned int. Then the negation trips "negation of unsigned value, result still unsigned" warnings. Perma-workaround this issue by saying INT_MIN, which better documents the intended behavior and avoids triggering warnings on C1XX. gcd.pass.cpp: Same changes as lcm.pass.cpp but for GCD. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@302472 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../numeric.ops/numeric.ops.gcd/gcd.pass.cpp | 45 +++++++++-------- .../numeric.ops/numeric.ops.lcm/lcm.pass.cpp | 49 ++++++++++--------- 2 files changed, 52 insertions(+), 42 deletions(-) diff --git a/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp b/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp index 961b515ef..517a62a00 100644 --- a/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp +++ b/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp @@ -16,8 +16,10 @@ #include #include +#include +#include #include // for rand() -#include +#include constexpr struct { int x; @@ -36,21 +38,24 @@ constexpr struct { template -constexpr bool test0(Input1 in1, Input2 in2, Output out) +constexpr bool test0(int in1, int in2, int out) { - static_assert((std::is_same::value), "" ); - static_assert((std::is_same::value), "" ); - return out == std::gcd(in1, in2) ? true : (std::abort(), false); + auto value1 = static_cast(in1); + auto value2 = static_cast(in2); + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v, ""); + assert(static_cast(out) == std::gcd(value1, value2)); + return true; } template constexpr bool do_test(int = 0) { - using S1 = typename std::make_signed::type; - using S2 = typename std::make_signed::type; - using U1 = typename std::make_unsigned::type; - using U2 = typename std::make_unsigned::type; + using S1 = std::make_signed_t; + using S2 = std::make_signed_t; + using U1 = std::make_unsigned_t; + using U2 = std::make_unsigned_t; bool accumulate = true; for (auto TC : Cases) { { // Test with two signed types @@ -103,15 +108,15 @@ int main() assert(do_test(non_cce)); assert(do_test(non_cce)); - static_assert(do_test< int8_t>(), ""); - static_assert(do_test(), ""); - static_assert(do_test(), ""); - static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); - assert(do_test< int8_t>(non_cce)); - assert(do_test(non_cce)); - assert(do_test(non_cce)); - assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); static_assert(do_test(), ""); static_assert(do_test(), ""); @@ -133,8 +138,8 @@ int main() // LWG#2837 { - auto res = std::gcd((int64_t)1234, (int32_t)-2147483648); - static_assert( std::is_same::type>::value, ""); - assert(res == 2); + auto res = std::gcd(static_cast(1234), INT32_MIN); + static_assert(std::is_same_v, ""); + assert(res == 2); } } diff --git a/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp b/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp index 90d48398f..6bd8a4f1e 100644 --- a/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp +++ b/test/std/numerics/numeric.ops/numeric.ops.lcm/lcm.pass.cpp @@ -11,12 +11,14 @@ // // template -// constexpr common_type_t<_M,_N> gcd(_M __m, _N __n) +// constexpr common_type_t<_M,_N> lcm(_M __m, _N __n) #include #include +#include +#include #include -#include +#include constexpr struct { int x; @@ -34,21 +36,24 @@ constexpr struct { }; template -constexpr bool test0(Input1 in1, Input2 in2, Output out) +constexpr bool test0(int in1, int in2, int out) { - static_assert((std::is_same::value), "" ); - static_assert((std::is_same::value), "" ); - return out == std::lcm(in1, in2) ? true : (std::abort(), false); + auto value1 = static_cast(in1); + auto value2 = static_cast(in2); + static_assert(std::is_same_v, ""); + static_assert(std::is_same_v, ""); + assert(static_cast(out) == std::lcm(value1, value2)); + return true; } template constexpr bool do_test(int = 0) { - using S1 = typename std::make_signed::type; - using S2 = typename std::make_signed::type; - using U1 = typename std::make_unsigned::type; - using U2 = typename std::make_unsigned::type; + using S1 = std::make_signed_t; + using S2 = std::make_signed_t; + using U1 = std::make_unsigned_t; + using U2 = std::make_unsigned_t; bool accumulate = true; for (auto TC : Cases) { { // Test with two signed types @@ -101,15 +106,15 @@ int main() assert(do_test(non_cce)); assert(do_test(non_cce)); - static_assert(do_test< int8_t>(), ""); - static_assert(do_test(), ""); - static_assert(do_test(), ""); - static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); + static_assert(do_test(), ""); - assert(do_test< int8_t>(non_cce)); - assert(do_test(non_cce)); - assert(do_test(non_cce)); - assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); + assert(do_test(non_cce)); static_assert(do_test(), ""); static_assert(do_test(), ""); @@ -131,9 +136,9 @@ int main() // LWG#2837 { - auto res1 = std::lcm((int64_t)1234, (int32_t)-2147483648); - (void) std::lcm(INT_MIN, 2); // this used to trigger UBSAN - static_assert( std::is_same::type>::value, ""); - assert(res1 == 1324997410816LL); + auto res1 = std::lcm(static_cast(1234), INT32_MIN); + (void)std::lcm(INT_MIN, 2UL); // this used to trigger UBSAN + static_assert(std::is_same_v, ""); + assert(res1 == 1324997410816LL); } }