PR c++/87519 - bogus warning with -Wsign-conversion.
authorMarek Polacek <polacek@redhat.com>
Thu, 8 Aug 2019 15:37:46 +0000 (15:37 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 8 Aug 2019 15:37:46 +0000 (15:37 +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: r274211

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

index 3af901c78ea26ff2dee66d0dcd426b1893ba132c..4d78888f38ba2750d9e85c30ef1f84df694993fa 100644 (file)
@@ -1,5 +1,9 @@
 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.
+
        * constexpr.c (inline_asm_in_constexpr_error): New.
        (cxx_eval_constant_expression) <case ASM_EXPR>: Call it.
        (potential_constant_expression_1) <case ASM_EXPR>: Likewise.
index 7e4ea3bee75bed9666bfe0fc329ae400be069710..4cc0ee0128d8bf0df8fa809da44880894eac749c 100644 (file)
@@ -5509,9 +5509,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)
index be085243d370ea343cf195ad1fa170a12c6fd3aa..6ed8aabaec87cbdf80396e18544ba1d988fa0587 100644 (file)
@@ -1,5 +1,8 @@
 2019-08-08  Marek Polacek  <polacek@redhat.com>
 
+       PR c++/87519 - bogus warning with -Wsign-conversion.
+       * g++.dg/warn/Wsign-conversion-5.C: New test.
+
        * g++.dg/cpp2a/inline-asm3.C: New test.
 
 2019-08-07  Steven G. Kargl  <kargl@gcc.gnu.org>
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);
+}