c++: bogus -Wparentheses warning [PR112765]

Message ID 20231129194210.2626367-1-ppalka@redhat.com
State New
Headers
Series c++: bogus -Wparentheses warning [PR112765] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 fail Patch failed to apply

Commit Message

Patrick Palka Nov. 29, 2023, 7:42 p.m. UTC
  Bootstrapped and regtested on x86_64-pc-linu-xgnu, does this look OK for
trunk?

-- >8 --

We need to consistently look through implicit INDIRECT_REF when
setting/checking for -Wparentheses warning suppression.  In passing
use STRIP_REFERENCE_REF consistently as well.

	PR c++/112765

gcc/cp/ChangeLog:

	* pt.cc (tsubst_expr) <case MODOP_EXPR>: Look through
	implicit INDIRECT_REF when propagating -Wparentheses
	warning suppression.
	* semantics.cc (maybe_warn_unparenthesized_assignment):
	Replace REFERENCE_REF_P stripping with STRIP_REFERENCE_REF.
	(finish_parenthesized_expr): Likewise.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wparentheses-33.C: New test.
---
 gcc/cp/pt.cc                                |  2 +-
 gcc/cp/semantics.cc                         |  6 ++----
 gcc/testsuite/g++.dg/warn/Wparentheses-33.C | 24 +++++++++++++++++++++
 3 files changed, 27 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wparentheses-33.C
  

Comments

Jason Merrill Nov. 29, 2023, 8:41 p.m. UTC | #1
On 11/29/23 14:42, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linu-xgnu, does this look OK for
> trunk?

OK.

> -- >8 --
> 
> We need to consistently look through implicit INDIRECT_REF when
> setting/checking for -Wparentheses warning suppression.  In passing
> use STRIP_REFERENCE_REF consistently as well.
> 
> 	PR c++/112765
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (tsubst_expr) <case MODOP_EXPR>: Look through
> 	implicit INDIRECT_REF when propagating -Wparentheses
> 	warning suppression.
> 	* semantics.cc (maybe_warn_unparenthesized_assignment):
> 	Replace REFERENCE_REF_P stripping with STRIP_REFERENCE_REF.
> 	(finish_parenthesized_expr): Likewise.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/warn/Wparentheses-33.C: New test.
> ---
>   gcc/cp/pt.cc                                |  2 +-
>   gcc/cp/semantics.cc                         |  6 ++----
>   gcc/testsuite/g++.dg/warn/Wparentheses-33.C | 24 +++++++++++++++++++++
>   3 files changed, 27 insertions(+), 5 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wparentheses-33.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 00b095265b6..fc4464dec02 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -20282,7 +20282,7 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
>   	   build_x_modify_expr sets it and it must not be reset
>   	   here.  */
>   	if (warning_suppressed_p (t, OPT_Wparentheses))
> -	  suppress_warning (r, OPT_Wparentheses);
> +	  suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses);
>   
>   	RETURN (r);
>         }
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index 3bf586453dc..fc00c20cba4 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -871,8 +871,7 @@ is_assignment_op_expr_p (tree t)
>   void
>   maybe_warn_unparenthesized_assignment (tree t, tsubst_flags_t complain)
>   {
> -  if (REFERENCE_REF_P (t))
> -    t = TREE_OPERAND (t, 0);
> +  t = STRIP_REFERENCE_REF (t);
>   
>     if ((complain & tf_warning)
>         && warn_parentheses
> @@ -2176,8 +2175,7 @@ finish_parenthesized_expr (cp_expr expr)
>       {
>         /* This inhibits warnings in maybe_warn_unparenthesized_assignment
>   	 and c_common_truthvalue_conversion.  */
> -      tree inner = REFERENCE_REF_P (expr) ? TREE_OPERAND (expr, 0) : *expr;
> -      suppress_warning (inner, OPT_Wparentheses);
> +      suppress_warning (STRIP_REFERENCE_REF (*expr), OPT_Wparentheses);
>       }
>   
>     if (TREE_CODE (expr) == OFFSET_REF
> diff --git a/gcc/testsuite/g++.dg/warn/Wparentheses-33.C b/gcc/testsuite/g++.dg/warn/Wparentheses-33.C
> new file mode 100644
> index 00000000000..6c65037d1b4
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wparentheses-33.C
> @@ -0,0 +1,24 @@
> +// PR c++/112765
> +
> +struct A {
> +  A& operator=(const A&);
> +  operator bool() const;
> +};
> +
> +template<class T>
> +void f(A a1, A a2) {
> +  if ((a2 = a1)) // { dg-bogus "suggest parentheses" }
> +    return;
> +  bool b = (a2 = a1); // { dg-bogus "suggest parentheses" }
> +}
> +
> +template void f<int>(A, A);
> +
> +template<class T>
> +void g(T a1, T a2) {
> +  if ((a2 = a1)) // { dg-bogus "suggest parentheses" }
> +    return;
> +  bool b = (a2 = a1); // { dg-bogus "suggest parentheses" }
> +}
> +
> +template void g<A>(A, A);
  

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 00b095265b6..fc4464dec02 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -20282,7 +20282,7 @@  tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	   build_x_modify_expr sets it and it must not be reset
 	   here.  */
 	if (warning_suppressed_p (t, OPT_Wparentheses))
-	  suppress_warning (r, OPT_Wparentheses);
+	  suppress_warning (STRIP_REFERENCE_REF (r), OPT_Wparentheses);
 
 	RETURN (r);
       }
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 3bf586453dc..fc00c20cba4 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -871,8 +871,7 @@  is_assignment_op_expr_p (tree t)
 void
 maybe_warn_unparenthesized_assignment (tree t, tsubst_flags_t complain)
 {
-  if (REFERENCE_REF_P (t))
-    t = TREE_OPERAND (t, 0);
+  t = STRIP_REFERENCE_REF (t);
 
   if ((complain & tf_warning)
       && warn_parentheses
@@ -2176,8 +2175,7 @@  finish_parenthesized_expr (cp_expr expr)
     {
       /* This inhibits warnings in maybe_warn_unparenthesized_assignment
 	 and c_common_truthvalue_conversion.  */
-      tree inner = REFERENCE_REF_P (expr) ? TREE_OPERAND (expr, 0) : *expr;
-      suppress_warning (inner, OPT_Wparentheses);
+      suppress_warning (STRIP_REFERENCE_REF (*expr), OPT_Wparentheses);
     }
 
   if (TREE_CODE (expr) == OFFSET_REF
diff --git a/gcc/testsuite/g++.dg/warn/Wparentheses-33.C b/gcc/testsuite/g++.dg/warn/Wparentheses-33.C
new file mode 100644
index 00000000000..6c65037d1b4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wparentheses-33.C
@@ -0,0 +1,24 @@ 
+// PR c++/112765
+
+struct A {
+  A& operator=(const A&);
+  operator bool() const;
+};
+
+template<class T>
+void f(A a1, A a2) {
+  if ((a2 = a1)) // { dg-bogus "suggest parentheses" }
+    return;
+  bool b = (a2 = a1); // { dg-bogus "suggest parentheses" }
+}
+
+template void f<int>(A, A);
+
+template<class T>
+void g(T a1, T a2) {
+  if ((a2 = a1)) // { dg-bogus "suggest parentheses" }
+    return;
+  bool b = (a2 = a1); // { dg-bogus "suggest parentheses" }
+}
+
+template void g<A>(A, A);