From: Marek Polacek Date: Thu, 15 Aug 2019 18:32:33 +0000 (+0000) Subject: PR c++/87519 - bogus warning with -Wsign-conversion. X-Git-Tag: releases/gcc-9.3.0~723 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9be0b095c9e1864a476d41faf84ad1d314f0ac47;p=thirdparty%2Fgcc.git PR c++/87519 - bogus warning with -Wsign-conversion. * typeck.c (cp_build_binary_op): Use same_type_p instead of comparing the types directly. * g++.dg/warn/Wsign-conversion-5.C: New test. From-SVN: r274545 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index a14a0d6ae0c6..42952c68f236 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -7,6 +7,12 @@ * parser.c (cp_parser_constructor_declarator_p): Handle the scenario when a parameter declaration begins with [[attribute]]. + 2019-08-08 Marek Polacek + + PR c++/87519 - bogus warning with -Wsign-conversion. + * typeck.c (cp_build_binary_op): Use same_type_p instead of comparing + the types directly. + 2019-08-14 Martin Sebor Backported from mainline diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index 3255af58a25e..bc4a76065821 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -5516,9 +5516,9 @@ cp_build_binary_op (const op_location_t &location, if (! converted) { warning_sentinel w (warn_sign_conversion, short_compare); - if (TREE_TYPE (op0) != result_type) + if (!same_type_p (TREE_TYPE (op0), result_type)) op0 = cp_convert_and_check (result_type, op0, complain); - if (TREE_TYPE (op1) != result_type) + if (!same_type_p (TREE_TYPE (op1), result_type)) op1 = cp_convert_and_check (result_type, op1, complain); if (op0 == error_mark_node || op1 == error_mark_node) diff --git a/gcc/testsuite/g++.dg/warn/Wsign-conversion-5.C b/gcc/testsuite/g++.dg/warn/Wsign-conversion-5.C new file mode 100644 index 000000000000..ff384164901f --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wsign-conversion-5.C @@ -0,0 +1,18 @@ +// PR c++/87519 - bogus warning with -Wsign-conversion. +// { dg-options "-Wsign-conversion" } + +typedef unsigned long int uint64_t; + +void f(unsigned long int a, int q) +{ + a += a + q; // { dg-warning "may change the sign" } + + // Explicit cast should disable the warning. + a = a + static_cast(q); + a = a + (uint64_t) q; + a = a + uint64_t(q); + a = a + static_cast(q); + a = a + (const uint64_t) q; + a = a + static_cast(q); + a = a + static_cast(q); +}