Re: [PATCH] c++: Extend -Wpessimizing-move to other contexts

2022-08-22 Thread Marek Polacek via Gcc-patches
On Mon, Aug 22, 2022 at 01:48:34PM +0200, Stephan Bergmann wrote:
> On 16/08/2022 14:27, Marek Polacek via Gcc-patches wrote:
> > Ping.  (The other std::move patches depend on this one.)
> 
> 
> "c++: Extend -Wpessimizing-move to other contexts" started to cause false
> positive

Thanks for reporting the problem.  I'm testing a simple patch (appended
below) and will post it once testing finishes.
 
> > $ cat test.cc
> > #include 
> > struct S1 {
> > S1();
> > S1(S1 const &) = delete;
> > S1(S1 &&);
> > S1 operator =(S1 const &) = delete;
> > S1 operator =(S1 &&);
> > };
> > struct S2 { S2(S1); };
> > S2 f() {
> > S1 s;
> > return { std::move(s) };
> > }
> > 
> > $ g++ -fsyntax-only -Wredundant-move test.cc
> > test.cc: In function ‘S2 f()’:
> > test.cc:12:27: warning: redundant move in return statement 
> > [-Wredundant-move]
> >12 | return { std::move(s) };
> >   |   ^
> > test.cc:12:27: note: remove ‘std::move’ call

--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -10447,7 +10447,7 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool 
return_p)
 return;

   /* A a = std::move (A());  */
-  if (TREE_CODE (expr) == TREE_LIST)
+  if (TREE_CODE (expr) == TREE_LIST && !return_p)
 {
   if (list_length (expr) == 1)
expr = TREE_VALUE (expr);
@@ -10456,7 +10456,7 @@ maybe_warn_pessimizing_move (tree expr, tree type, bool 
return_p)
 }
   /* A a = {std::move (A())};
  A a{std::move (A())};  */
-  else if (TREE_CODE (expr) == CONSTRUCTOR)
+  else if (TREE_CODE (expr) == CONSTRUCTOR && !return_p)
 {
   if (CONSTRUCTOR_NELTS (expr) == 1)
expr = CONSTRUCTOR_ELT (expr, 0)->value;

Marek



Re: [PATCH] c++: Extend -Wpessimizing-move to other contexts

2022-08-22 Thread Stephan Bergmann via Gcc-patches

On 16/08/2022 14:27, Marek Polacek via Gcc-patches wrote:

Ping.  (The other std::move patches depend on this one.)


 
"c++: Extend -Wpessimizing-move to other contexts" started to cause 
false positive



$ cat test.cc
#include 
struct S1 {
S1();
S1(S1 const &) = delete;
S1(S1 &&);
S1 operator =(S1 const &) = delete;
S1 operator =(S1 &&);
};
struct S2 { S2(S1); };
S2 f() {
S1 s;
return { std::move(s) };
}

$ g++ -fsyntax-only -Wredundant-move test.cc
test.cc: In function ‘S2 f()’:
test.cc:12:27: warning: redundant move in return statement [-Wredundant-move]
   12 | return { std::move(s) };
  |   ^
test.cc:12:27: note: remove ‘std::move’ call




Re: [PATCH] c++: Extend -Wpessimizing-move to other contexts

2022-08-17 Thread Jason Merrill via Gcc-patches

On 8/16/22 14:09, Marek Polacek wrote:

On Tue, Aug 16, 2022 at 03:23:18PM -0400, Jason Merrill wrote:

On 8/2/22 16:04, Marek Polacek wrote:

In my recent patch which enhanced -Wpessimizing-move so that it warns
about class prvalues too I said that I'd like to extend it so that it
warns in more contexts where a std::move can prevent copy elision, such
as:

T t = std::move(T());
T t(std::move(T()));
T t{std::move(T())};
T t = {std::move(T())};
void foo (T);
foo (std::move(T()));

This patch does that by adding two maybe_warn_pessimizing_move calls.
These must happen before we've converted the initializers otherwise the
std::move will be buried in a TARGET_EXPR.

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

PR c++/106276

gcc/cp/ChangeLog:

* call.cc (build_over_call): Call maybe_warn_pessimizing_move.
* cp-tree.h (maybe_warn_pessimizing_move): Declare.
* decl.cc (build_aggr_init_full_exprs): Call
maybe_warn_pessimizing_move.
* typeck.cc (maybe_warn_pessimizing_move): Handle TREE_LIST and
CONSTRUCTOR.  Add a bool parameter and use it.  Adjust a diagnostic
message.
(check_return_expr): Adjust the call to maybe_warn_pessimizing_move.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/Wpessimizing-move7.C: Add dg-warning.
* g++.dg/cpp0x/Wpessimizing-move8.C: New test.
---
   gcc/cp/call.cc|  5 +-
   gcc/cp/cp-tree.h  |  1 +
   gcc/cp/decl.cc|  3 +-
   gcc/cp/typeck.cc  | 58 -
   .../g++.dg/cpp0x/Wpessimizing-move7.C | 16 ++---
   .../g++.dg/cpp0x/Wpessimizing-move8.C | 65 +++
   6 files changed, 120 insertions(+), 28 deletions(-)
   create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move8.C

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 01a7be10077..370137ebd6d 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -9627,10 +9627,13 @@ build_over_call (struct z_candidate *cand, int flags, 
tsubst_flags_t complain)
 if (!conversion_warning)
arg_complain &= ~tf_warning;
+  if (arg_complain & tf_warning)
+   maybe_warn_pessimizing_move (arg, type, /*return_p*/false);
+
 val = convert_like_with_context (conv, arg, fn, i - is_method,
   arg_complain);
 val = convert_for_arg_passing (type, val, arg_complain);
-   
+
 if (val == error_mark_node)
   return error_mark_node;
 else
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 3278b4114bd..5a8af22b509 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -8101,6 +8101,7 @@ extern tree finish_right_unary_fold_expr (tree, int);
   extern tree finish_binary_fold_expr  (tree, tree, int);
   extern tree treat_lvalue_as_rvalue_p  (tree, bool);
   extern bool decl_in_std_namespace_p   (tree);
+extern void maybe_warn_pessimizing_move (tree, tree, bool);
   /* in typeck2.cc */
   extern void require_complete_eh_spec_types   (tree, tree);
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 70ad681467e..dc6853a7de1 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -7220,9 +7220,10 @@ check_array_initializer (tree decl, tree type, tree init)
   static tree
   build_aggr_init_full_exprs (tree decl, tree init, int flags)
-
   {
 gcc_assert (stmts_are_full_exprs_p ());
+  if (init)
+maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);


This is a surprising place to add this.  Why here rather than in
build_aggr_init or check_initializer?


IIRC it just felt appropriate since we only want to invoke maybe_warn_ on the
full expression, not any subexpressions -- we're looking to see if the
outermost expr is a std::move.  Also, we want to warn for all types, not just
classes.


Makes sense.  The patch is OK.


But I can move the call into some place in check_initializer if you prefer.

Marek





Re: [PATCH] c++: Extend -Wpessimizing-move to other contexts

2022-08-16 Thread Marek Polacek via Gcc-patches
On Tue, Aug 16, 2022 at 03:23:18PM -0400, Jason Merrill wrote:
> On 8/2/22 16:04, Marek Polacek wrote:
> > In my recent patch which enhanced -Wpessimizing-move so that it warns
> > about class prvalues too I said that I'd like to extend it so that it
> > warns in more contexts where a std::move can prevent copy elision, such
> > as:
> > 
> >T t = std::move(T());
> >T t(std::move(T()));
> >T t{std::move(T())};
> >T t = {std::move(T())};
> >void foo (T);
> >foo (std::move(T()));
> > 
> > This patch does that by adding two maybe_warn_pessimizing_move calls.
> > These must happen before we've converted the initializers otherwise the
> > std::move will be buried in a TARGET_EXPR.
> > 
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > PR c++/106276
> > 
> > gcc/cp/ChangeLog:
> > 
> > * call.cc (build_over_call): Call maybe_warn_pessimizing_move.
> > * cp-tree.h (maybe_warn_pessimizing_move): Declare.
> > * decl.cc (build_aggr_init_full_exprs): Call
> > maybe_warn_pessimizing_move.
> > * typeck.cc (maybe_warn_pessimizing_move): Handle TREE_LIST and
> > CONSTRUCTOR.  Add a bool parameter and use it.  Adjust a diagnostic
> > message.
> > (check_return_expr): Adjust the call to maybe_warn_pessimizing_move.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> > * g++.dg/cpp0x/Wpessimizing-move7.C: Add dg-warning.
> > * g++.dg/cpp0x/Wpessimizing-move8.C: New test.
> > ---
> >   gcc/cp/call.cc|  5 +-
> >   gcc/cp/cp-tree.h  |  1 +
> >   gcc/cp/decl.cc|  3 +-
> >   gcc/cp/typeck.cc  | 58 -
> >   .../g++.dg/cpp0x/Wpessimizing-move7.C | 16 ++---
> >   .../g++.dg/cpp0x/Wpessimizing-move8.C | 65 +++
> >   6 files changed, 120 insertions(+), 28 deletions(-)
> >   create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move8.C
> > 
> > diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> > index 01a7be10077..370137ebd6d 100644
> > --- a/gcc/cp/call.cc
> > +++ b/gcc/cp/call.cc
> > @@ -9627,10 +9627,13 @@ build_over_call (struct z_candidate *cand, int 
> > flags, tsubst_flags_t complain)
> > if (!conversion_warning)
> > arg_complain &= ~tf_warning;
> > +  if (arg_complain & tf_warning)
> > +   maybe_warn_pessimizing_move (arg, type, /*return_p*/false);
> > +
> > val = convert_like_with_context (conv, arg, fn, i - is_method,
> >arg_complain);
> > val = convert_for_arg_passing (type, val, arg_complain);
> > -   
> > +
> > if (val == error_mark_node)
> >   return error_mark_node;
> > else
> > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> > index 3278b4114bd..5a8af22b509 100644
> > --- a/gcc/cp/cp-tree.h
> > +++ b/gcc/cp/cp-tree.h
> > @@ -8101,6 +8101,7 @@ extern tree finish_right_unary_fold_expr (tree, 
> > int);
> >   extern tree finish_binary_fold_expr  (tree, tree, int);
> >   extern tree treat_lvalue_as_rvalue_p   (tree, bool);
> >   extern bool decl_in_std_namespace_p(tree);
> > +extern void maybe_warn_pessimizing_move (tree, tree, bool);
> >   /* in typeck2.cc */
> >   extern void require_complete_eh_spec_types(tree, tree);
> > diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> > index 70ad681467e..dc6853a7de1 100644
> > --- a/gcc/cp/decl.cc
> > +++ b/gcc/cp/decl.cc
> > @@ -7220,9 +7220,10 @@ check_array_initializer (tree decl, tree type, tree 
> > init)
> >   static tree
> >   build_aggr_init_full_exprs (tree decl, tree init, int flags)
> > -
> >   {
> > gcc_assert (stmts_are_full_exprs_p ());
> > +  if (init)
> > +maybe_warn_pessimizing_move (init, TREE_TYPE (decl), 
> > /*return_p*/false);
> 
> This is a surprising place to add this.  Why here rather than in
> build_aggr_init or check_initializer?

IIRC it just felt appropriate since we only want to invoke maybe_warn_ on the
full expression, not any subexpressions -- we're looking to see if the
outermost expr is a std::move.  Also, we want to warn for all types, not just
classes.

But I can move the call into some place in check_initializer if you prefer.

Marek



Re: [PATCH] c++: Extend -Wpessimizing-move to other contexts

2022-08-16 Thread Jason Merrill via Gcc-patches

On 8/2/22 16:04, Marek Polacek wrote:

In my recent patch which enhanced -Wpessimizing-move so that it warns
about class prvalues too I said that I'd like to extend it so that it
warns in more contexts where a std::move can prevent copy elision, such
as:

   T t = std::move(T());
   T t(std::move(T()));
   T t{std::move(T())};
   T t = {std::move(T())};
   void foo (T);
   foo (std::move(T()));

This patch does that by adding two maybe_warn_pessimizing_move calls.
These must happen before we've converted the initializers otherwise the
std::move will be buried in a TARGET_EXPR.

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

PR c++/106276

gcc/cp/ChangeLog:

* call.cc (build_over_call): Call maybe_warn_pessimizing_move.
* cp-tree.h (maybe_warn_pessimizing_move): Declare.
* decl.cc (build_aggr_init_full_exprs): Call
maybe_warn_pessimizing_move.
* typeck.cc (maybe_warn_pessimizing_move): Handle TREE_LIST and
CONSTRUCTOR.  Add a bool parameter and use it.  Adjust a diagnostic
message.
(check_return_expr): Adjust the call to maybe_warn_pessimizing_move.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/Wpessimizing-move7.C: Add dg-warning.
* g++.dg/cpp0x/Wpessimizing-move8.C: New test.
---
  gcc/cp/call.cc|  5 +-
  gcc/cp/cp-tree.h  |  1 +
  gcc/cp/decl.cc|  3 +-
  gcc/cp/typeck.cc  | 58 -
  .../g++.dg/cpp0x/Wpessimizing-move7.C | 16 ++---
  .../g++.dg/cpp0x/Wpessimizing-move8.C | 65 +++
  6 files changed, 120 insertions(+), 28 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move8.C

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 01a7be10077..370137ebd6d 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -9627,10 +9627,13 @@ build_over_call (struct z_candidate *cand, int flags, 
tsubst_flags_t complain)
if (!conversion_warning)
arg_complain &= ~tf_warning;
  
+  if (arg_complain & tf_warning)

+   maybe_warn_pessimizing_move (arg, type, /*return_p*/false);
+
val = convert_like_with_context (conv, arg, fn, i - is_method,
   arg_complain);
val = convert_for_arg_passing (type, val, arg_complain);
-   
+
if (val == error_mark_node)
  return error_mark_node;
else
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 3278b4114bd..5a8af22b509 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -8101,6 +8101,7 @@ extern tree finish_right_unary_fold_expr (tree, int);
  extern tree finish_binary_fold_expr  (tree, tree, int);
  extern tree treat_lvalue_as_rvalue_p   (tree, bool);
  extern bool decl_in_std_namespace_p(tree);
+extern void maybe_warn_pessimizing_move (tree, tree, bool);
  
  /* in typeck2.cc */

  extern void require_complete_eh_spec_types(tree, tree);
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 70ad681467e..dc6853a7de1 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -7220,9 +7220,10 @@ check_array_initializer (tree decl, tree type, tree init)
  
  static tree

  build_aggr_init_full_exprs (tree decl, tree init, int flags)
-
  {
gcc_assert (stmts_are_full_exprs_p ());
+  if (init)
+maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);


This is a surprising place to add this.  Why here rather than in 
build_aggr_init or check_initializer?



return build_aggr_init (decl, init, flags, tf_warning_or_error);
  }
  
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc

index 9500c4e2fe8..2650beb780e 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -10368,17 +10368,17 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p)
  }
  }
  
-/* Warn about wrong usage of std::move in a return statement.  RETVAL

-   is the expression we are returning; FUNCTYPE is the type the function
-   is declared to return.  */
+/* Warn about dubious usage of std::move (in a return statement, if RETURN_P
+   is true).  EXPR is the std::move expression; TYPE is the type of the object
+   being initialized.  */
  
-static void

-maybe_warn_pessimizing_move (tree retval, tree functype)
+void
+maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
  {
if (!(warn_pessimizing_move || warn_redundant_move))
  return;
  
-  location_t loc = cp_expr_loc_or_input_loc (retval);

+  const location_t loc = cp_expr_loc_or_input_loc (expr);
  
/* C++98 doesn't know move.  */

if (cxx_dialect < cxx11)
@@ -10390,14 +10390,32 @@ maybe_warn_pessimizing_move (tree retval, tree 
functype)
  return;
  
/* This is only interesting for class types.  */

-  if (!CLASS_TYPE_P (functype))
+  if (!CLASS_TYPE_P (type))
  return;
  
+  /* A a = std::move (A());  */

+  if (TREE_CODE (expr) == TREE_LIST)
+{
+  if (list_length (expr) == 

Re: [PATCH] c++: Extend -Wpessimizing-move to other contexts

2022-08-16 Thread Marek Polacek via Gcc-patches
Ping.  (The other std::move patches depend on this one.)

(can_do_rvo_p is renamed to can_elide_copy_prvalue_p in the PR90428 patch.)

On Tue, Aug 02, 2022 at 07:04:47PM -0400, Marek Polacek via Gcc-patches wrote:
> In my recent patch which enhanced -Wpessimizing-move so that it warns
> about class prvalues too I said that I'd like to extend it so that it
> warns in more contexts where a std::move can prevent copy elision, such
> as:
> 
>   T t = std::move(T());
>   T t(std::move(T()));
>   T t{std::move(T())};
>   T t = {std::move(T())};
>   void foo (T);
>   foo (std::move(T()));
> 
> This patch does that by adding two maybe_warn_pessimizing_move calls.
> These must happen before we've converted the initializers otherwise the
> std::move will be buried in a TARGET_EXPR.
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
>   PR c++/106276
> 
> gcc/cp/ChangeLog:
> 
>   * call.cc (build_over_call): Call maybe_warn_pessimizing_move.
>   * cp-tree.h (maybe_warn_pessimizing_move): Declare.
>   * decl.cc (build_aggr_init_full_exprs): Call
>   maybe_warn_pessimizing_move.
>   * typeck.cc (maybe_warn_pessimizing_move): Handle TREE_LIST and
>   CONSTRUCTOR.  Add a bool parameter and use it.  Adjust a diagnostic
>   message.
>   (check_return_expr): Adjust the call to maybe_warn_pessimizing_move.
> 
> gcc/testsuite/ChangeLog:
> 
>   * g++.dg/cpp0x/Wpessimizing-move7.C: Add dg-warning.
>   * g++.dg/cpp0x/Wpessimizing-move8.C: New test.
> ---
>  gcc/cp/call.cc|  5 +-
>  gcc/cp/cp-tree.h  |  1 +
>  gcc/cp/decl.cc|  3 +-
>  gcc/cp/typeck.cc  | 58 -
>  .../g++.dg/cpp0x/Wpessimizing-move7.C | 16 ++---
>  .../g++.dg/cpp0x/Wpessimizing-move8.C | 65 +++
>  6 files changed, 120 insertions(+), 28 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move8.C
> 
> diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
> index 01a7be10077..370137ebd6d 100644
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -9627,10 +9627,13 @@ build_over_call (struct z_candidate *cand, int flags, 
> tsubst_flags_t complain)
>if (!conversion_warning)
>   arg_complain &= ~tf_warning;
>  
> +  if (arg_complain & tf_warning)
> + maybe_warn_pessimizing_move (arg, type, /*return_p*/false);
> +
>val = convert_like_with_context (conv, arg, fn, i - is_method,
>  arg_complain);
>val = convert_for_arg_passing (type, val, arg_complain);
> - 
> +
>if (val == error_mark_node)
>  return error_mark_node;
>else
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index 3278b4114bd..5a8af22b509 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -8101,6 +8101,7 @@ extern tree finish_right_unary_fold_expr (tree, 
> int);
>  extern tree finish_binary_fold_expr  (tree, tree, int);
>  extern tree treat_lvalue_as_rvalue_p  (tree, bool);
>  extern bool decl_in_std_namespace_p   (tree);
> +extern void maybe_warn_pessimizing_move   (tree, tree, bool);
>  
>  /* in typeck2.cc */
>  extern void require_complete_eh_spec_types   (tree, tree);
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index 70ad681467e..dc6853a7de1 100644
> --- a/gcc/cp/decl.cc
> +++ b/gcc/cp/decl.cc
> @@ -7220,9 +7220,10 @@ check_array_initializer (tree decl, tree type, tree 
> init)
>  
>  static tree
>  build_aggr_init_full_exprs (tree decl, tree init, int flags)
> - 
>  {
>gcc_assert (stmts_are_full_exprs_p ());
> +  if (init)
> +maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
>return build_aggr_init (decl, init, flags, tf_warning_or_error);
>  }
>  
> diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
> index 9500c4e2fe8..2650beb780e 100644
> --- a/gcc/cp/typeck.cc
> +++ b/gcc/cp/typeck.cc
> @@ -10368,17 +10368,17 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p)
>  }
>  }
>  
> -/* Warn about wrong usage of std::move in a return statement.  RETVAL
> -   is the expression we are returning; FUNCTYPE is the type the function
> -   is declared to return.  */
> +/* Warn about dubious usage of std::move (in a return statement, if RETURN_P
> +   is true).  EXPR is the std::move expression; TYPE is the type of the 
> object
> +   being initialized.  */
>  
> -static void
> -maybe_warn_pessimizing_move (tree retval, tree functype)
> +void
> +maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
>  {
>if (!(warn_pessimizing_move || warn_redundant_move))
>  return;
>  
> -  location_t loc = cp_expr_loc_or_input_loc (retval);
> +  const location_t loc = cp_expr_loc_or_input_loc (expr);
>  
>/* C++98 doesn't know move.  */
>if (cxx_dialect < cxx11)
> @@ -10390,14 +10390,32 @@ maybe_warn_pessimizing_move (tree retval, tree 
> functype)
>  return;
>  
>

[PATCH] c++: Extend -Wpessimizing-move to other contexts

2022-08-02 Thread Marek Polacek via Gcc-patches
In my recent patch which enhanced -Wpessimizing-move so that it warns
about class prvalues too I said that I'd like to extend it so that it
warns in more contexts where a std::move can prevent copy elision, such
as:

  T t = std::move(T());
  T t(std::move(T()));
  T t{std::move(T())};
  T t = {std::move(T())};
  void foo (T);
  foo (std::move(T()));

This patch does that by adding two maybe_warn_pessimizing_move calls.
These must happen before we've converted the initializers otherwise the
std::move will be buried in a TARGET_EXPR.

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

PR c++/106276

gcc/cp/ChangeLog:

* call.cc (build_over_call): Call maybe_warn_pessimizing_move.
* cp-tree.h (maybe_warn_pessimizing_move): Declare.
* decl.cc (build_aggr_init_full_exprs): Call
maybe_warn_pessimizing_move.
* typeck.cc (maybe_warn_pessimizing_move): Handle TREE_LIST and
CONSTRUCTOR.  Add a bool parameter and use it.  Adjust a diagnostic
message.
(check_return_expr): Adjust the call to maybe_warn_pessimizing_move.

gcc/testsuite/ChangeLog:

* g++.dg/cpp0x/Wpessimizing-move7.C: Add dg-warning.
* g++.dg/cpp0x/Wpessimizing-move8.C: New test.
---
 gcc/cp/call.cc|  5 +-
 gcc/cp/cp-tree.h  |  1 +
 gcc/cp/decl.cc|  3 +-
 gcc/cp/typeck.cc  | 58 -
 .../g++.dg/cpp0x/Wpessimizing-move7.C | 16 ++---
 .../g++.dg/cpp0x/Wpessimizing-move8.C | 65 +++
 6 files changed, 120 insertions(+), 28 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/Wpessimizing-move8.C

diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc
index 01a7be10077..370137ebd6d 100644
--- a/gcc/cp/call.cc
+++ b/gcc/cp/call.cc
@@ -9627,10 +9627,13 @@ build_over_call (struct z_candidate *cand, int flags, 
tsubst_flags_t complain)
   if (!conversion_warning)
arg_complain &= ~tf_warning;
 
+  if (arg_complain & tf_warning)
+   maybe_warn_pessimizing_move (arg, type, /*return_p*/false);
+
   val = convert_like_with_context (conv, arg, fn, i - is_method,
   arg_complain);
   val = convert_for_arg_passing (type, val, arg_complain);
-   
+
   if (val == error_mark_node)
 return error_mark_node;
   else
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 3278b4114bd..5a8af22b509 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -8101,6 +8101,7 @@ extern tree finish_right_unary_fold_expr (tree, int);
 extern tree finish_binary_fold_expr  (tree, tree, int);
 extern tree treat_lvalue_as_rvalue_p(tree, bool);
 extern bool decl_in_std_namespace_p (tree);
+extern void maybe_warn_pessimizing_move (tree, tree, bool);
 
 /* in typeck2.cc */
 extern void require_complete_eh_spec_types (tree, tree);
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 70ad681467e..dc6853a7de1 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -7220,9 +7220,10 @@ check_array_initializer (tree decl, tree type, tree init)
 
 static tree
 build_aggr_init_full_exprs (tree decl, tree init, int flags)
- 
 {
   gcc_assert (stmts_are_full_exprs_p ());
+  if (init)
+maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false);
   return build_aggr_init (decl, init, flags, tf_warning_or_error);
 }
 
diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 9500c4e2fe8..2650beb780e 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -10368,17 +10368,17 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p)
 }
 }
 
-/* Warn about wrong usage of std::move in a return statement.  RETVAL
-   is the expression we are returning; FUNCTYPE is the type the function
-   is declared to return.  */
+/* Warn about dubious usage of std::move (in a return statement, if RETURN_P
+   is true).  EXPR is the std::move expression; TYPE is the type of the object
+   being initialized.  */
 
-static void
-maybe_warn_pessimizing_move (tree retval, tree functype)
+void
+maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
 {
   if (!(warn_pessimizing_move || warn_redundant_move))
 return;
 
-  location_t loc = cp_expr_loc_or_input_loc (retval);
+  const location_t loc = cp_expr_loc_or_input_loc (expr);
 
   /* C++98 doesn't know move.  */
   if (cxx_dialect < cxx11)
@@ -10390,14 +10390,32 @@ maybe_warn_pessimizing_move (tree retval, tree 
functype)
 return;
 
   /* This is only interesting for class types.  */
-  if (!CLASS_TYPE_P (functype))
+  if (!CLASS_TYPE_P (type))
 return;
 
+  /* A a = std::move (A());  */
+  if (TREE_CODE (expr) == TREE_LIST)
+{
+  if (list_length (expr) == 1)
+   expr = TREE_VALUE (expr);
+  else
+   return;
+}
+  /* A a = {std::move (A())};
+ A a{std::move (A())};  */
+  else if (TREE_CODE (expr) == CONSTRUCTOR)
+{
+  if