c++: Handle COMPOUND_EXPRs in get_narrower and warnings_for_convert_and_check [PR91993]
authorJakub Jelinek <jakub@redhat.com>
Mon, 23 Mar 2020 18:47:24 +0000 (19:47 +0100)
committerJakub Jelinek <jakub@redhat.com>
Mon, 23 Mar 2020 18:47:24 +0000 (19:47 +0100)
As the testcases shows, the -Wconversion warning behaves quite differently
when -fsanitize=undefined vs. when not sanitizing, but in the end it is
not something specific to sanitizing, if a user uses
  return static_cast<uc>(static_cast<uc>((d++, a) << 1U) | b) | c;
instead of
  return static_cast<uc>(static_cast<uc>(a << 1U) | b) | c;
and thus there is some COMPOUND_EXPR involved, cp_build_binary_op behaves
significantly different, e.g. shorten_binary_op will have different result
(uc for the case without COMPOUND_EXPR, int with it), but it isn't limited
to that.

> How about improving get_narrower to handle COMPOUND_EXPR?  I'd think that
> would do the trick without affecting evaluation order.

Not completely, had to change also warnings_for_convert_and_check, but with
that it works.  The float-cast-overflow* changes are needed because now with
-O1+ we emit lots of -Woverflow warnings on the testcase, but we do emit
those warnings on the testcase even when compiling just with -O1 and without
-fsanitize=float-cast-overflow, so it seems to me a change in the right
direction, having -fsanitize= or explicit comma expressions smaller effect
on the warnings that are emitted.

2020-03-23  Jakub Jelinek  <jakub@redhat.com>

PR c++/91993
* tree.c (get_narrower): Handle COMPOUND_EXPR by recursing on
ultimate rhs and if returned something different, reconstructing
the COMPOUND_EXPRs.

* c-warn.c (warnings_for_convert_and_check): For expr and/or
result being COMPOUND_EXPRs, skip to ultimate rhs.

* g++.dg/warn/Wconversion-pr91993.C: New test.
* g++.dg/ubsan/pr91993.C: New test.
* c-c++-common/ubsan/float-cast-overflow-1.c: Add -Wno-overflow
to dg-options.
* c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
* c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.

gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c-warn.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/ubsan/float-cast-overflow-1.c
gcc/testsuite/c-c++-common/ubsan/float-cast-overflow-2.c
gcc/testsuite/c-c++-common/ubsan/float-cast-overflow-4.c
gcc/testsuite/g++.dg/ubsan/pr91993.C [new file with mode: 0644]
gcc/testsuite/g++.dg/warn/Wconversion-pr91993.C [new file with mode: 0644]
gcc/tree.c

index 4509447caa3e7627e58acf8e4264af2089997b90..9ba58273c21683f6d9e3f34e1cd25c68477a6d6f 100644 (file)
@@ -1,3 +1,10 @@
+2020-03-23  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/91993
+       * tree.c (get_narrower): Handle COMPOUND_EXPR by recursing on
+       ultimate rhs and if returned something different, reconstructing
+       the COMPOUND_EXPRs.
+
 2020-03-23  Lewis Hyatt  <lhyatt@gmail.com>
 
        * opts.c (print_filtered_help): Improve the help text for alias options.
index 3f195ebcc5d65c73798ecbe26ccd33e9defc5398..0d805c64bdee33e754e130fbc985297f9259a116 100644 (file)
@@ -1,3 +1,9 @@
+2020-03-23  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/91993
+       * c-warn.c (warnings_for_convert_and_check): For expr and/or
+       result being COMPOUND_EXPRs, skip to ultimate rhs.
+
 2020-03-20  Richard Sandiford  <richard.sandiford@arm.com>
 
        PR middle-end/94072
index 9315cac683e71e8b7da8cc00d8246e334c8cf309..f6b3afc727c2e6c2ccc8cd9ebd453f7b74b04550 100644 (file)
@@ -1359,6 +1359,11 @@ warnings_for_convert_and_check (location_t loc, tree type, tree expr,
 {
   loc = expansion_point_location_if_in_system_header (loc);
 
+  while (TREE_CODE (expr) == COMPOUND_EXPR)
+    expr = TREE_OPERAND (expr, 1);
+  while (TREE_CODE (result) == COMPOUND_EXPR)
+    result = TREE_OPERAND (result, 1);
+
   bool cst = TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant;
 
   tree exprtype = TREE_TYPE (expr);
index c1ab2bc79bb800fe1df28c675625e2e9c5b80c36..1d053e0772168f3e3189064a863b30df469339fc 100644 (file)
@@ -1,3 +1,13 @@
+2020-03-23  Jakub Jelinek  <jakub@redhat.com>
+
+       PR c++/91993
+       * g++.dg/warn/Wconversion-pr91993.C: New test.
+       * g++.dg/ubsan/pr91993.C: New test.
+       * c-c++-common/ubsan/float-cast-overflow-1.c: Add -Wno-overflow
+       to dg-options.
+       * c-c++-common/ubsan/float-cast-overflow-2.c: Likewise.
+       * c-c++-common/ubsan/float-cast-overflow-4.c: Likewise.
+
 2020-03-23  Srinath Parvathaneni  <srinath.parvathaneni@arm.com>
             Andre Vieira  <andre.simoesdiasvieira@arm.com>
             Mihail Ionescu  <mihail.ionescu@arm.com>
index 8139cc1723fbc6050579fec772883ca747b1e501..6df065fd8f7758b5539182556181493476037b73 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do run { target { lp64 || ilp32 } } } */
-/* { dg-options "-fsanitize=float-cast-overflow" } */
+/* { dg-options "-fsanitize=float-cast-overflow -Wno-overflow" } */
 /* { dg-additional-options "-ffloat-store" { target { ia32 } } } */
 /* { dg-additional-options "-mieee" { target { { alpha*-*-* } || { sh*-*-* } } } } */
 
index 426c625fc6b5841cd27525294e076705e9514718..ea685ceaabdb6afe7104653c496eab74e032dd17 100644 (file)
@@ -1,6 +1,6 @@
 /* { dg-do run } */
 /* { dg-require-effective-target int128 } */
-/* { dg-options "-fsanitize=float-cast-overflow" } */
+/* { dg-options "-fsanitize=float-cast-overflow -Wno-overflow" } */
 
 #include "float-cast.h"
 
index 48ad257c641005fd3be0de223234e264ac2d0ecd..68f854906490ae5200a0da4c07a2dc1e64617109 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do run { target { lp64 } } } */
-/* { dg-options "-fsanitize=float-cast-overflow" } */
+/* { dg-options "-fsanitize=float-cast-overflow -Wno-overflow" } */
 
 #include <limits.h>
 #include "float-cast.h"
diff --git a/gcc/testsuite/g++.dg/ubsan/pr91993.C b/gcc/testsuite/g++.dg/ubsan/pr91993.C
new file mode 100644 (file)
index 0000000..f903398
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/91993
+// { dg-do compile }
+// { dg-options "-Wconversion -fsanitize=undefined" }
+
+typedef unsigned char uc;
+
+int
+foo (const uc &a, const uc &b, const uc &c)
+{
+  return static_cast<uc>(static_cast<uc>(a << 1U) | b) | c;            // { dg-bogus "conversion from 'int' to 'unsigned char' may change value" }
+}
+
+int
+bar (const uc &a, const uc &b, const uc &c, int &d)
+{
+  return static_cast<uc>(static_cast<uc>((d++, a) << 1U) | b) | c;     // { dg-bogus "conversion from 'int' to 'unsigned char' may change value" }
+}
diff --git a/gcc/testsuite/g++.dg/warn/Wconversion-pr91993.C b/gcc/testsuite/g++.dg/warn/Wconversion-pr91993.C
new file mode 100644 (file)
index 0000000..63a8462
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/91993
+// { dg-do compile }
+// { dg-options "-Wconversion" }
+
+typedef unsigned char uc;
+
+int
+foo (const uc &a, const uc &b, const uc &c)
+{
+  return static_cast<uc>(static_cast<uc>(a << 1U) | b) | c;            // { dg-bogus "conversion from 'int' to 'unsigned char' may change value" }
+}
+
+int
+bar (const uc &a, const uc &b, const uc &c, int &d)
+{
+  return static_cast<uc>(static_cast<uc>((d++, a) << 1U) | b) | c;     // { dg-bogus "conversion from 'int' to 'unsigned char' may change value" }
+}
index ee4519363a7d0523dff6a539bd798c10569201bc..f38dfffc16ebedf3e2dfd6a9fafddeb534aab864 100644 (file)
@@ -8862,6 +8862,21 @@ get_narrower (tree op, int *unsignedp_ptr)
   tree win = op;
   bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op));
 
+  if (TREE_CODE (op) == COMPOUND_EXPR)
+    {
+      while (TREE_CODE (op) == COMPOUND_EXPR)
+       op = TREE_OPERAND (op, 1);
+      tree ret = get_narrower (op, unsignedp_ptr);
+      if (ret == op)
+       return win;
+      op = win;
+      for (tree *p = &win; TREE_CODE (op) == COMPOUND_EXPR;
+          op = TREE_OPERAND (op, 1), p = &TREE_OPERAND (*p, 1))
+       *p = build2_loc (EXPR_LOCATION (op), COMPOUND_EXPR,
+                        TREE_TYPE (ret), TREE_OPERAND (op, 0),
+                        ret);
+      return win;
+    }
   while (TREE_CODE (op) == NOP_EXPR)
     {
       int bitschange