public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Patrick Palka <ppalka@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: more ahead-of-time -Wparentheses warnings
Date: Thu, 26 Oct 2023 21:24:07 -0400	[thread overview]
Message-ID: <62809b18-f182-4599-b34f-135d9b04f1bd@redhat.com> (raw)
In-Reply-To: <0eeff018-2c0b-900f-783e-f71cceb1598b@idea>

On 10/26/23 17:37, Patrick Palka wrote:
> On Thu, 26 Oct 2023, Patrick Palka wrote:
> 
>> On Thu, 26 Oct 2023, Jason Merrill wrote:
>>
>>> On 10/25/23 14:55, Patrick Palka wrote:
>>>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look
>>>> OK for trunk?
>>>>
>>>> -- >8 --
>>>>
>>>> Now that we don't have to worry about looking thruogh NON_DEPENDENT_EXPR,
>>>> we can easily extend the -Wparentheses warning in convert_for_assignment
>>>> to consider (non-dependent) templated assignment operator expressions as
>>>> well, like r14-4111-g6e92a6a2a72d3b did in maybe_convert_cond.
>>>>
>>>> gcc/cp/ChangeLog:
>>>>
>>>> 	* cp-tree.h (is_assignment_op_expr_p): Declare.
>>>> 	* semantics.cc (is_assignment_op_expr_p): Generalize to return
>>>> 	true for assignment operator expression, not just one that
>>>> 	have been resolved to an operator overload.
>>>> 	(maybe_convert_cond): Remove now-redundant checks around
>>>> 	is_assignment_op_expr_p.
>>>> 	* typeck.cc (convert_for_assignment): Look through implicit
>>>> 	INDIRECT_REF in -Wparentheses warning logic, and generalize
>>>> 	to use is_assignment_op_expr_p.
>>>
>>> Do we want to factor out the whole warning logic rather than adjust it in both
>>> places?
>>
>> Sounds good, like so?  Bootstrap / regtest in progress.
>>
>> -- >8 --
>>
>> Subject: [PATCH] c++: more ahead-of-time -Wparentheses warnings
>>
>> Now that we don't have to worry about looking through NON_DEPENDENT_EXPR,
>> we can easily extend the -Wparentheses warning in convert_for_assignment
>> to consider (non-dependent) templated assignment operator expressions as
>> well, like r14-4111-g6e92a6a2a72d3b did in maybe_convert_cond.
>>
>> gcc/cp/ChangeLog:
>>
>> 	* cp-tree.h (maybe_warn_unparenthesized_assignment): Declare.
>> 	* semantics.cc (is_assignment_op_expr_p): Generalize to return
>> 	true for assignment operator expression, not just one that
>> 	have been resolved to an operator overload.
>> 	(maybe_warn_unparenthesized_assignment): Factored out from ...
>> 	(maybe_convert_cond): ... here.
>> 	(finish_parenthesized_expr): Also mention
>> 	maybe_warn_unparenthesized_assignment.
>> 	* typeck.cc (convert_for_assignment): Replace -Wparentheses
>> 	warning logic with maybe_warn_unparenthesized_assignment.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 	* g++.dg/warn/Wparentheses-13.C: Strengthen by expecting that
>> 	the -Wparentheses warning are issued ahead of time.
>> 	* g++.dg/warn/Wparentheses-23.C: Likewise.
>> 	* g++.dg/warn/Wparentheses-32.C: Remove xfails.
>> ---
>>   gcc/cp/cp-tree.h                            |  1 +
>>   gcc/cp/semantics.cc                         | 55 ++++++++++++++-------
>>   gcc/cp/typeck.cc                            | 13 ++---
>>   gcc/testsuite/g++.dg/warn/Wparentheses-13.C |  2 -
>>   gcc/testsuite/g++.dg/warn/Wparentheses-23.C |  3 --
>>   gcc/testsuite/g++.dg/warn/Wparentheses-32.C |  8 +--
>>   6 files changed, 44 insertions(+), 38 deletions(-)
>>
>> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
>> index 30fe716b109..98b29e9cf81 100644
>> --- a/gcc/cp/cp-tree.h
>> +++ b/gcc/cp/cp-tree.h
>> @@ -7875,6 +7875,7 @@ extern tree lambda_regenerating_args		(tree);
>>   extern tree most_general_lambda			(tree);
>>   extern tree finish_omp_target			(location_t, tree, tree, bool);
>>   extern void finish_omp_target_clauses		(location_t, tree, tree *);
>> +extern void maybe_warn_unparenthesized_assignment (tree, tsubst_flags_t);
>>   
>>   /* in tree.cc */
>>   extern int cp_tree_operand_length		(const_tree);
>> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
>> index 72ec72de690..5664da9f4f2 100644
>> --- a/gcc/cp/semantics.cc
>> +++ b/gcc/cp/semantics.cc
>> @@ -840,15 +840,20 @@ finish_goto_stmt (tree destination)
>>     return add_stmt (build_stmt (input_location, GOTO_EXPR, destination));
>>   }
>>   
>> -/* Returns true if CALL is a (possibly wrapped) CALL_EXPR or AGGR_INIT_EXPR
>> -   to operator= () that is written as an operator expression. */
>> +/* Returns true if T corresponds to an assignment operator expression.  */
>> +
>>   static bool
>> -is_assignment_op_expr_p (tree call)
>> +is_assignment_op_expr_p (tree t)
>>   {
>> -  if (call == NULL_TREE)
>> +  if (t == NULL_TREE)
>>       return false;
>>   
>> -  call = extract_call_expr (call);
>> +  if (TREE_CODE (t) == MODIFY_EXPR
>> +      || (TREE_CODE (t) == MODOP_EXPR
>> +	  && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR))
>> +    return true;
>> +
>> +  tree call = extract_call_expr (t);
>>     if (call == NULL_TREE
>>         || call == error_mark_node
>>         || !CALL_EXPR_OPERATOR_SYNTAX (call))
>> @@ -860,6 +865,28 @@ is_assignment_op_expr_p (tree call)
>>       && DECL_OVERLOADED_OPERATOR_IS (fndecl, NOP_EXPR);
>>   }
>>   
>> +/* Maybe warn about an unparenthesized 'a = b' (appearing in a boolean
>> +   context).  */
>> +
>> +void
>> +maybe_warn_unparenthesized_assignment (tree t, tsubst_flags_t complain)
>> +{
>> +  t = REFERENCE_REF_P (t) ? TREE_OPERAND (t, 0) : t;
> 
> Consider this to instead be written more idiomatically as

OK.

Jason


      reply	other threads:[~2023-10-27  1:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-25 18:55 Patrick Palka
2023-10-26 20:54 ` Jason Merrill
2023-10-26 21:32   ` Patrick Palka
2023-10-26 21:37     ` Patrick Palka
2023-10-27  1:24       ` 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=62809b18-f182-4599-b34f-135d9b04f1bd@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=ppalka@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).