public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: Extend -Wpessimizing-move to other contexts
Date: Wed, 17 Aug 2022 12:11:16 -0400	[thread overview]
Message-ID: <98ce1382-bfba-af11-7602-6967d944b154@redhat.com> (raw)
In-Reply-To: <YvwHeg0xQ+1J4yEI@redhat.com>

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
> 


      reply	other threads:[~2022-08-17 16:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-02 23:04 Marek Polacek
2022-08-16 12:27 ` Marek Polacek
2022-08-22 11:48   ` Stephan Bergmann
2022-08-22 21:02     ` Marek Polacek
2022-08-16 19:23 ` Jason Merrill
2022-08-16 21:09   ` Marek Polacek
2022-08-17 16:11     ` Jason Merrill [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=98ce1382-bfba-af11-7602-6967d944b154@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).