[gcc(refs/vendors/ibm/heads/gcc-9-branch)] c++: Fix bogus -Wparentheses warning [PR95344]

Peter Bergner bergner@gcc.gnu.org
Fri Sep 4 19:17:17 GMT 2020


https://gcc.gnu.org/g:74eb19c2499c1b9011629799e16d74f299f35b33

commit 74eb19c2499c1b9011629799e16d74f299f35b33
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Jun 11 18:38:00 2020 -0400

    c++: Fix bogus -Wparentheses warning [PR95344]
    
    Since r267272, which added location wrappers, cp_fold loses
    TREE_NO_WARNING on a MODIFY_EXPR that finish_parenthesized_expr set, and
    that results in a bogus -Wparentheses warning.
    
    I.e., previously we had "b = 1" but now we have "VIEW_CONVERT_EXPR<bool>(b) = 1"
    and cp_fold_maybe_rvalue folds away the location wrapper and so we do
    2718             x = fold_build2_loc (loc, code, TREE_TYPE (x), op0, op1);
    in cp_fold and the flag is lost.
    
            PR c++/95344
            * cp-gimplify.c (cp_fold) <case MODIFY_EXPR>: Don't set
            TREE_THIS_VOLATILE here.
            (cp_fold): Set it here along with TREE_NO_WARNING.
    
            * c-c++-common/Wparentheses-2.c: New test.

Diff:
---
 gcc/cp/cp-gimplify.c                        |  6 ++++++
 gcc/testsuite/c-c++-common/Wparentheses-2.c | 18 ++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 0e15b1c8147..7525ee8d10f 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -2811,6 +2811,12 @@ cp_fold (tree x)
       return org_x;
     }
 
+  if (EXPR_P (x) && TREE_CODE (x) == code)
+    {
+      TREE_THIS_VOLATILE (x) = TREE_THIS_VOLATILE (org_x);
+      TREE_NO_WARNING (x) = TREE_NO_WARNING (org_x);
+    }
+
   fold_cache->put (org_x, x);
   /* Prevent that we try to fold an already folded result again.  */
   if (x != org_x)
diff --git a/gcc/testsuite/c-c++-common/Wparentheses-2.c b/gcc/testsuite/c-c++-common/Wparentheses-2.c
new file mode 100644
index 00000000000..1aa5d314ae7
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/Wparentheses-2.c
@@ -0,0 +1,18 @@
+// PR c++/95344 - bogus -Wparentheses warning.
+// { dg-do compile }
+// { dg-options "-Wparentheses" }
+
+#ifndef __cplusplus
+# define bool _Bool
+# define true 1
+# define false 0
+#endif
+
+void
+f (int i)
+{
+  bool b = false;
+  if (i == 99 ? (b = true) : false) // { dg-bogus "suggest parentheses" }
+    {
+    }
+}


More information about the Gcc-cvs mailing list