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