public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: David Malcolm <dmalcolm@redhat.com>
Cc: Jeff Law <law@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH 2/2] v2: C++: improvements to binary operator diagnostics (PR c++/87504)
Date: Wed, 12 Dec 2018 20:43:00 -0000	[thread overview]
Message-ID: <1aec1d54-419c-c4d8-2f3e-c34f22ce9559@redhat.com> (raw)
In-Reply-To: <1543962908-32306-2-git-send-email-dmalcolm@redhat.com>

On 12/4/18 5:35 PM, David Malcolm wrote:
> The v1 patch:
>    https://gcc.gnu.org/ml/gcc-patches/2018-11/msg00303.html
> has bitrotten somewhat, so here's v2 of the patch, updated relative
> to r266740.
> 
> Blurb from v1 patch follows:
> 
> The C frontend is able (where expression locations are available) to print
> problems with binary operators in 3-location form, labelling the types of
> the expressions:
> 
>    arg_0 op arg_1
>    ~~~~~ ^~ ~~~~~
>      |        |
>      |        arg1 type
>      arg0 type
> 
> The C++ frontend currently just shows the combined location:
> 
>    arg_0 op arg_1
>    ~~~~~~^~~~~~~~
> 
> and fails to highlight where the subexpressions are, or their types.
> 
> This patch introduces a op_location_t struct for handling the above
> operator-location vs combined-location split, and a new
> class binary_op_rich_location for displaying the above, so that the
> C++ frontend is able to use the more detailed 3-location form for
> type mismatches in binary operators, and for -Wtautological-compare
> (where types are not displayed).  Both forms can be seen in this
> example:
> 
> bad-binary-ops.C:69:20: error: no match for 'operator&&' (operand types are
>    's' and 't')
>     69 |   return ns_4::foo && ns_4::inner::bar;
>        |          ~~~~~~~~~ ^~ ~~~~~~~~~~~~~~~~
>        |                |                   |
>        |                s                   t
> bad-binary-ops.C:69:20: note: candidate: 'operator&&(bool, bool)' <built-in>
>     69 |   return ns_4::foo && ns_4::inner::bar;
>        |          ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~
> 
> The patch also allows from some uses of macros in
> -Wtautological-compare, where both sides of the comparison have
> been spelled the same way, e.g.:
> 
> Wtautological-compare-ranges.c:23:11: warning: self-comparison always
>     evaluates to true [-Wtautological-compare]
>     23 |   if (FOO == FOO);
>        |           ^~
> 
> Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu, in
> conjunction with the previous patch.
> 
> OK for trunk?
> Dave
> 
> gcc/c-family/ChangeLog:
> 	PR c++/87504
> 	* c-common.h (warn_tautological_cmp): Convert 1st param from
> 	location_t to const op_location_t &.
> 	* c-warn.c (find_array_ref_with_const_idx_r): Strip location
> 	wrapper when testing for INTEGER_CST.
> 	(warn_tautological_bitwise_comparison): Convert 1st param from
> 	location_t to const op_location_t &; use it to build a
> 	binary_op_rich_location, and use this.
> 	(spelled_the_same_p): New function.
> 	(warn_tautological_cmp): Convert 1st param from location_t to
> 	const op_location_t &.  Warn for macro expansions if
> 	spelled_the_same_p.  Use binary_op_rich_location.
> 
> gcc/c/ChangeLog:
> 	PR c++/87504
> 	* c-typeck.c (class maybe_range_label_for_tree_type_mismatch):
> 	Move from here to gcc-rich-location.h and gcc-rich-location.c.
> 	(build_binary_op): Use struct op_location_t and
> 	class binary_op_rich_location.
> 
> gcc/cp/ChangeLog:
> 	PR c++/87504
> 	* call.c (op_error): Convert 1st param from location_t to
> 	const op_location_t &.  Use binary_op_rich_location for binary
> 	ops.
> 	(build_conditional_expr_1): Convert 1st param from location_t to
> 	const op_location_t &.
> 	(build_conditional_expr): Likewise.
> 	(build_new_op_1): Likewise.
> 	(build_new_op): Likewise.
> 	* cp-tree.h (build_conditional_expr): Likewise.
> 	(build_new_op): Likewise.
> 	(build_x_binary_op): Likewise.
> 	(cp_build_binary_op): Likewise.
> 	* parser.c (cp_parser_primary_expression): Build a location
> 	for id-expression nodes.
> 	(cp_parser_binary_expression): Use an op_location_t when
> 	calling build_x_binary_op.
> 	(cp_parser_operator): Build a location for user-defined literals.
> 	* typeck.c (build_x_binary_op): Convert 1st param from location_t
> 	to const op_location_t &.
> 	(cp_build_binary_op): Likewise.  Use binary_op_rich_location.
> 
> gcc/ChangeLog:
> 	PR c++/87504
> 	* gcc-rich-location.c
> 	(maybe_range_label_for_tree_type_mismatch::get_text): Move here from
> 	c/c-typeck.c.
> 	(binary_op_rich_location::binary_op_rich_location): New ctor.
> 	(binary_op_rich_location::use_operator_loc_p): New function.
> 	* gcc-rich-location.h
> 	(class maybe_range_label_for_tree_type_mismatch)): Move here from
> 	c/c-typeck.c.
> 	(struct op_location_t): New forward decl.
> 	(class binary_op_rich_location): New class.
> 	* tree.h (struct op_location_t): New struct.
> 
> gcc/testsuite/ChangeLog:
> 	* c-c++-common/Wtautological-compare-ranges.c: New test.
> 	* g++.dg/cpp0x/pr51420.C: Add -fdiagnostics-show-caret and update
> 	expected output.
> 	* g++.dg/diagnostic/bad-binary-ops.C: Update expected output from
> 	1-location form to 3-location form, with labelling of ranges with
> 	types.  Add examples of id-expression nodes with namespaces.
> 	* g++.dg/diagnostic/param-type-mismatch-2.C: Likewise.
> 
> This is the 2nd commit message:
> 
> FIXME: column and multiline fixes to * g++.dg/cpp0x/pr51420.C
> ---
>   gcc/c-family/c-common.h                            |  3 +-
>   gcc/c-family/c-warn.c                              | 57 +++++++++++---
>   gcc/c/c-typeck.c                                   | 41 +---------
>   gcc/cp/call.c                                      | 28 ++++---
>   gcc/cp/cp-tree.h                                   | 10 ++-
>   gcc/cp/parser.c                                    | 32 ++++++--
>   gcc/cp/typeck.c                                    | 14 ++--
>   gcc/gcc-rich-location.c                            | 89 ++++++++++++++++++++++
>   gcc/gcc-rich-location.h                            | 57 ++++++++++++++
>   .../c-c++-common/Wtautological-compare-ranges.c    | 42 ++++++++++
>   gcc/testsuite/g++.dg/cpp0x/pr51420.C               | 10 +++
>   gcc/testsuite/g++.dg/diagnostic/bad-binary-ops.C   | 57 +++++++++++++-
>   .../g++.dg/diagnostic/param-type-mismatch-2.C      |  4 +-
>   gcc/tree.h                                         | 49 ++++++++++++
>   14 files changed, 417 insertions(+), 76 deletions(-)
>   create mode 100644 gcc/testsuite/c-c++-common/Wtautological-compare-ranges.c
> 
> diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
> index 4187343..0b9ddf6 100644
> --- a/gcc/c-family/c-common.h
> +++ b/gcc/c-family/c-common.h
> @@ -1268,7 +1268,8 @@ extern void constant_expression_error (tree);
>   extern void overflow_warning (location_t, tree, tree = NULL_TREE);
>   extern void warn_logical_operator (location_t, enum tree_code, tree,
>   				   enum tree_code, tree, enum tree_code, tree);
> -extern void warn_tautological_cmp (location_t, enum tree_code, tree, tree);
> +extern void warn_tautological_cmp (const op_location_t &, enum tree_code,
> +				   tree, tree);
>   extern void warn_logical_not_parentheses (location_t, enum tree_code, tree,
>   					  tree);
>   extern bool warn_if_unused_value (const_tree, location_t);
> diff --git a/gcc/c-family/c-warn.c b/gcc/c-family/c-warn.c
> index fc7f87c..fce9d84 100644
> --- a/gcc/c-family/c-warn.c
> +++ b/gcc/c-family/c-warn.c
> @@ -322,7 +322,8 @@ find_array_ref_with_const_idx_r (tree *expr_p, int *, void *)
>   
>     if ((TREE_CODE (expr) == ARRAY_REF
>          || TREE_CODE (expr) == ARRAY_RANGE_REF)
> -      && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
> +      && (TREE_CODE (tree_strip_any_location_wrapper (TREE_OPERAND (expr, 1)))
> +	  == INTEGER_CST))
>       return integer_type_node;

I think we want fold_for_warn here.  OK with that change (assuming it 
passes).

Jason

  parent reply	other threads:[~2018-12-12 20:43 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-05 19:44 [PATCH 1/2] C++: more location wrapper nodes (PR c++/43064, PR c++/43486) David Malcolm
2018-11-05 19:44 ` [PATCH 2/2] C++: improvements to binary operator diagnostics (PR c++/87504) David Malcolm
2018-11-19 16:51 ` [PING] Re: [PATCH 1/2] C++: more location wrapper nodes (PR c++/43064, PR c++/43486) David Malcolm
2018-12-03 22:10   ` Jeff Law
2018-12-04 15:20     ` David Malcolm
2018-12-04 21:48     ` [PATCH 1/2] v2: " David Malcolm
2018-12-04 21:48       ` [PATCH 2/2] v2: C++: improvements to binary operator diagnostics (PR c++/87504) David Malcolm
2018-12-11 19:52         ` PING " David Malcolm
2018-12-12 20:43         ` Jason Merrill [this message]
2018-12-19 23:28           ` Aaron Sawdey
2018-12-20  2:13             ` [PATCH] -Wtautological-compare: fix comparison of macro expansions David Malcolm
2018-12-20 14:29               ` David Malcolm
2018-12-20 23:35                 ` Aaron Sawdey
2018-12-04 23:31     ` [PING] Re: [PATCH 1/2] C++: more location wrapper nodes (PR c++/43064, PR c++/43486) Jason Merrill
2018-12-07 19:25       ` [PATCH 1/2] v3: " David Malcolm
2018-12-12 20:37         ` Jason Merrill
2018-12-13 19:24           ` [PATCH] v4: " David Malcolm
2018-12-13 20:38             ` Jason Merrill
2018-12-14 23:29               ` [PATCH] v5: " David Malcolm
2018-12-17 19:33                 ` Jason Merrill
2018-12-17 23:30                   ` David Malcolm
2018-12-18 20:23                     ` Jason Merrill
2018-12-18 20:34                     ` [PATCH] v6: " David Malcolm
2018-12-18 20:40                       ` Jason Merrill
2018-12-19 15:35                         ` David Malcolm
2018-12-19 19:01 ` [PATCH 1/2] " Thomas Schwinge
2018-12-20  2:29   ` David Malcolm
2020-03-26  5:02   ` [PATCH, OpenACC] Bug fix for processing OpenACC data clauses in C++ Sandra Loosemore
     [not found]     ` <4a68ec90-456a-cf49-036e-471ba275706c@codesourcery.com>
2020-03-26 14:27       ` C++ 'NON_LVALUE_EXPR' in OMP array section handling (was: [PATCH, OpenACC] Bug fix for processing OpenACC data clauses in C++) Thomas Schwinge
2020-03-26 15:09         ` C++ 'NON_LVALUE_EXPR' in OMP array section handling Sandra Loosemore
2020-03-26 20:53           ` Thomas Schwinge
2020-05-25 10:56             ` [WIP] Fold 'NON_LVALUE_EXPR' some more (was: C++ 'NON_LVALUE_EXPR' in OMP array section handling) Thomas Schwinge
2020-11-26  9:36               ` Don't create location wrapper nodes within OpenACC clauses (was: [WIP] Fold 'NON_LVALUE_EXPR' some more (was: C++ 'NON_LVALUE_EXPR' in OMP array section handling)) Thomas Schwinge
2020-11-26 10:02                 ` Jakub Jelinek

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=1aec1d54-419c-c4d8-2f3e-c34f22ce9559@redhat.com \
    --to=jason@redhat.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=law@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).