Re: [PATCH] c++: Quash bogus -Wunused-value with new [PR107797]

2023-01-23 Thread Jason Merrill via Gcc-patches

On 1/23/23 16:28, Marek Polacek wrote:

On Fri, Jan 20, 2023 at 02:37:19PM -0500, Jason Merrill wrote:

On 1/19/23 21:03, Marek Polacek wrote:

We shouldn't emit "right operand of comma operator has no effect"
when that comma operator was created by the compiler for "new int{}".
convert_to_void/COMPOUND_EXPR already checks warning_suppressed_p so
we can just suppress -Wunused-value.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

PR c++/107797

gcc/cp/ChangeLog:

* cvt.cc (ocp_convert): copy_warning when creating a new
COMPOUND_EXPR.
* init.cc (build_new_1): Suppress -Wunused-value on
compiler-generated COMPOUND_EXPRs.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wunused-value-1.C: New test.
---
   gcc/cp/cvt.cc   |  6 --
   gcc/cp/init.cc  |  2 ++
   gcc/testsuite/g++.dg/warn/Wunused-value-1.C | 12 
   3 files changed, 18 insertions(+), 2 deletions(-)
   create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-value-1.C

diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index f816c474cef..52e96fbe590 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -3800,6 +3800,8 @@ build_new_1 (vec **placement, tree type, 
tree nelts,
 if (cookie_expr)
   rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
+  suppress_warning (rval, OPT_Wunused_value);


This makes sense, but IIUC since rval is built with no location, this just
sets nowarning_flag?


Correct (I didn't realize this when writing the patch).  There's another
suppress_warning on a location-less expr just a few lines above.
  

 if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
   /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
  and return the call (which doesn't need to be adjusted).  */
diff --git a/gcc/cp/cvt.cc b/gcc/cp/cvt.cc
index 0cbfd8060cb..17827d06a4a 100644
--- a/gcc/cp/cvt.cc
+++ b/gcc/cp/cvt.cc
@@ -711,8 +711,10 @@ ocp_convert (tree type, tree expr, int convtype, int flags,
return error_mark_node;
if (e == TREE_OPERAND (expr, 1))
return expr;
-  return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
-TREE_OPERAND (expr, 0), e);
+  e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
+ TREE_OPERAND (expr, 0), e);
+  copy_warning (e, expr);


And so I don't know what effect this would have; copy_warning doesn't seem
to propagate nowarning_flag, which seems like a bug.


It actually does propagate nowarning_flag: in copy_warning we have

219   /* The no-warning bit might be set even if the map has not been 
consulted, or
220  otherwise if there's no entry in the map.  */
221   set_no_warning_bit (to, supp);

so 'e' ends up with TREE_NO_WARNING set.


Ah, yes.  Pesky overloaded functions.


I would not think we want to build 'rval' in build_new_1 with a location
or use TREE_NO_WARNING directly instead of suppress_warning so I'm leaving
the patch as-is, but please let me know if you disagree.


Agreed, the patch is OK.

Jason



Re: [PATCH] c++: Quash bogus -Wunused-value with new [PR107797]

2023-01-23 Thread Marek Polacek via Gcc-patches
On Fri, Jan 20, 2023 at 02:37:19PM -0500, Jason Merrill wrote:
> On 1/19/23 21:03, Marek Polacek wrote:
> > We shouldn't emit "right operand of comma operator has no effect"
> > when that comma operator was created by the compiler for "new int{}".
> > convert_to_void/COMPOUND_EXPR already checks warning_suppressed_p so
> > we can just suppress -Wunused-value.
> > 
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > PR c++/107797
> > 
> > gcc/cp/ChangeLog:
> > 
> > * cvt.cc (ocp_convert): copy_warning when creating a new
> > COMPOUND_EXPR.
> > * init.cc (build_new_1): Suppress -Wunused-value on
> > compiler-generated COMPOUND_EXPRs.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > * g++.dg/warn/Wunused-value-1.C: New test.
> > ---
> >   gcc/cp/cvt.cc   |  6 --
> >   gcc/cp/init.cc  |  2 ++
> >   gcc/testsuite/g++.dg/warn/Wunused-value-1.C | 12 
> >   3 files changed, 18 insertions(+), 2 deletions(-)
> >   create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-value-1.C
> > 
> > diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
> > index f816c474cef..52e96fbe590 100644
> > --- a/gcc/cp/init.cc
> > +++ b/gcc/cp/init.cc
> > @@ -3800,6 +3800,8 @@ build_new_1 (vec **placement, tree type, 
> > tree nelts,
> > if (cookie_expr)
> >   rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
> > +  suppress_warning (rval, OPT_Wunused_value);
> 
> This makes sense, but IIUC since rval is built with no location, this just
> sets nowarning_flag?

Correct (I didn't realize this when writing the patch).  There's another
suppress_warning on a location-less expr just a few lines above.
 
> > if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
> >   /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
> >  and return the call (which doesn't need to be adjusted).  */
> > diff --git a/gcc/cp/cvt.cc b/gcc/cp/cvt.cc
> > index 0cbfd8060cb..17827d06a4a 100644
> > --- a/gcc/cp/cvt.cc
> > +++ b/gcc/cp/cvt.cc
> > @@ -711,8 +711,10 @@ ocp_convert (tree type, tree expr, int convtype, int 
> > flags,
> > return error_mark_node;
> >if (e == TREE_OPERAND (expr, 1))
> > return expr;
> > -  return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE 
> > (e),
> > -TREE_OPERAND (expr, 0), e);
> > +  e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
> > + TREE_OPERAND (expr, 0), e);
> > +  copy_warning (e, expr);
> 
> And so I don't know what effect this would have; copy_warning doesn't seem
> to propagate nowarning_flag, which seems like a bug.

It actually does propagate nowarning_flag: in copy_warning we have

219   /* The no-warning bit might be set even if the map has not been 
consulted, or
220  otherwise if there's no entry in the map.  */
221   set_no_warning_bit (to, supp);

so 'e' ends up with TREE_NO_WARNING set.

I would not think we want to build 'rval' in build_new_1 with a location
or use TREE_NO_WARNING directly instead of suppress_warning so I'm leaving
the patch as-is, but please let me know if you disagree.

Marek



Re: [PATCH] c++: Quash bogus -Wunused-value with new [PR107797]

2023-01-20 Thread Jason Merrill via Gcc-patches

On 1/19/23 21:03, Marek Polacek wrote:

We shouldn't emit "right operand of comma operator has no effect"
when that comma operator was created by the compiler for "new int{}".
convert_to_void/COMPOUND_EXPR already checks warning_suppressed_p so
we can just suppress -Wunused-value.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

PR c++/107797

gcc/cp/ChangeLog:

* cvt.cc (ocp_convert): copy_warning when creating a new
COMPOUND_EXPR.
* init.cc (build_new_1): Suppress -Wunused-value on
compiler-generated COMPOUND_EXPRs.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wunused-value-1.C: New test.
---
  gcc/cp/cvt.cc   |  6 --
  gcc/cp/init.cc  |  2 ++
  gcc/testsuite/g++.dg/warn/Wunused-value-1.C | 12 
  3 files changed, 18 insertions(+), 2 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-value-1.C

diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index f816c474cef..52e96fbe590 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -3800,6 +3800,8 @@ build_new_1 (vec **placement, tree type, 
tree nelts,
if (cookie_expr)
  rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
  
+  suppress_warning (rval, OPT_Wunused_value);


This makes sense, but IIUC since rval is built with no location, this 
just sets nowarning_flag?



if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
  /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
 and return the call (which doesn't need to be adjusted).  */
diff --git a/gcc/cp/cvt.cc b/gcc/cp/cvt.cc
index 0cbfd8060cb..17827d06a4a 100644
--- a/gcc/cp/cvt.cc
+++ b/gcc/cp/cvt.cc
@@ -711,8 +711,10 @@ ocp_convert (tree type, tree expr, int convtype, int flags,
return error_mark_node;
   if (e == TREE_OPERAND (expr, 1))
return expr;
-  return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
-TREE_OPERAND (expr, 0), e);
+  e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
+ TREE_OPERAND (expr, 0), e);
+  copy_warning (e, expr);


And so I don't know what effect this would have; copy_warning doesn't 
seem to propagate nowarning_flag, which seems like a bug.



+  return e;
 }
 
   complete_type (type);



diff --git a/gcc/testsuite/g++.dg/warn/Wunused-value-1.C 
b/gcc/testsuite/g++.dg/warn/Wunused-value-1.C
new file mode 100644
index 000..2ba5587fce0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wunused-value-1.C
@@ -0,0 +1,12 @@
+// PR c++/107797
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wunused" }
+
+void
+g ()
+{
+  (long) new int{};
+  long(new int{});
+  (long) new int();
+  long(new int());
+}

base-commit: 86caab6c5d1e26e1c54c3dceacc873d6e27bfc09




[PATCH] c++: Quash bogus -Wunused-value with new [PR107797]

2023-01-19 Thread Marek Polacek via Gcc-patches
We shouldn't emit "right operand of comma operator has no effect"
when that comma operator was created by the compiler for "new int{}".
convert_to_void/COMPOUND_EXPR already checks warning_suppressed_p so
we can just suppress -Wunused-value.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

PR c++/107797

gcc/cp/ChangeLog:

* cvt.cc (ocp_convert): copy_warning when creating a new
COMPOUND_EXPR.
* init.cc (build_new_1): Suppress -Wunused-value on
compiler-generated COMPOUND_EXPRs.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wunused-value-1.C: New test.
---
 gcc/cp/cvt.cc   |  6 --
 gcc/cp/init.cc  |  2 ++
 gcc/testsuite/g++.dg/warn/Wunused-value-1.C | 12 
 3 files changed, 18 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-value-1.C

diff --git a/gcc/cp/cvt.cc b/gcc/cp/cvt.cc
index 0cbfd8060cb..17827d06a4a 100644
--- a/gcc/cp/cvt.cc
+++ b/gcc/cp/cvt.cc
@@ -711,8 +711,10 @@ ocp_convert (tree type, tree expr, int convtype, int flags,
return error_mark_node;
   if (e == TREE_OPERAND (expr, 1))
return expr;
-  return build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
-TREE_OPERAND (expr, 0), e);
+  e = build2_loc (EXPR_LOCATION (expr), COMPOUND_EXPR, TREE_TYPE (e),
+ TREE_OPERAND (expr, 0), e);
+  copy_warning (e, expr);
+  return e;
 }
 
   complete_type (type);
diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index f816c474cef..52e96fbe590 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -3800,6 +3800,8 @@ build_new_1 (vec **placement, tree type, 
tree nelts,
   if (cookie_expr)
 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
 
+  suppress_warning (rval, OPT_Wunused_value);
+
   if (rval == data_addr && TREE_CODE (alloc_expr) == TARGET_EXPR)
 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
and return the call (which doesn't need to be adjusted).  */
diff --git a/gcc/testsuite/g++.dg/warn/Wunused-value-1.C 
b/gcc/testsuite/g++.dg/warn/Wunused-value-1.C
new file mode 100644
index 000..2ba5587fce0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wunused-value-1.C
@@ -0,0 +1,12 @@
+// PR c++/107797
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wunused" }
+
+void
+g ()
+{
+  (long) new int{};
+  long(new int{});
+  (long) new int();
+  long(new int());
+}

base-commit: 86caab6c5d1e26e1c54c3dceacc873d6e27bfc09
-- 
2.39.0