]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c++/87519 - bogus warning with -Wsign-conversion.
authorMarek Polacek <polacek@redhat.com>
Thu, 15 Aug 2019 18:32:33 +0000 (18:32 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 15 Aug 2019 18:32:33 +0000 (18:32 +0000)
* 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

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/g++.dg/warn/Wsign-conversion-5.C [new file with mode: 0644]

index a14a0d6ae0c6cedcc26b4e4984258f3e78b1d856..42952c68f2362e4b59601aa51fc56aee6bad7593 100644 (file)
@@ -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  <polacek@redhat.com>
+
+       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  <msebor@redhat.com>
 
        Backported from mainline
index 3255af58a25eacfedb260102e68682e05e31ace7..bc4a76065821dcc9f690c29d17cc6648327131d3 100644 (file)
@@ -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 (file)
index 0000000..ff38416
--- /dev/null
@@ -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<uint64_t>(q);
+  a = a + (uint64_t) q;
+  a = a + uint64_t(q);
+  a = a + static_cast<const uint64_t>(q);
+  a = a + (const uint64_t) q;
+  a = a + static_cast<unsigned long int>(q);
+  a = a + static_cast<const unsigned long int>(q);
+}