[PATCH] c++: Improve the MVP -Wparentheses diagnostic.
Jason Merrill
jason@redhat.com
Wed Oct 28 21:49:20 GMT 2020
On 10/28/20 5:14 PM, Marek Polacek wrote:
> I noticed that declarator->parenthesized is, for this warning, only set
> to the opening paren. But we can easily make it a range and generate
> a nicer diagnostic. Moreover, we can then offer a fix-it hint.
>
> TL;DR: This patch changes
>
> mvp3.C:8:7: warning: unnecessary parentheses in declaration of ‘i’ [-Wparentheses]
> 8 | int (i);
> | ^
>
> to
>
> mvp3.C:8:7: warning: unnecessary parentheses in declaration of ‘i’ [-Wparentheses]
> 8 | int (i);
> | ^~~
> mvp3.C:8:7: note: remove parentheses
> 8 | int (i);
> | ^~~
> | - -
>
> Tested by using -fdiagnostics-generate-patch and verifying that the
> generated patch DTRT.
OK.
> gcc/cp/ChangeLog:
>
> * decl.c (grokdeclarator): Offer a fix-it hint for the "unnecessary
> parentheses in declaration" warning.
> * parser.c (cp_parser_direct_declarator): When setting
> declarator->parenthesized, use a location range.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/warn/mvp3.C: New test.
> ---
> gcc/cp/decl.c | 13 +++++++++++--
> gcc/cp/parser.c | 4 +++-
> gcc/testsuite/g++.dg/warn/mvp3.C | 30 ++++++++++++++++++++++++++++++
> 3 files changed, 44 insertions(+), 3 deletions(-)
> create mode 100644 gcc/testsuite/g++.dg/warn/mvp3.C
>
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index 2de4e1657fb..ee3c353935d 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -12037,8 +12037,17 @@ grokdeclarator (const cp_declarator *declarator,
> && inner_declarator->u.id.qualifying_scope
> && (MAYBE_CLASS_TYPE_P (type)
> || TREE_CODE (type) == ENUMERAL_TYPE)))
> - warning_at (declarator->parenthesized, OPT_Wparentheses,
> - "unnecessary parentheses in declaration of %qs", name);
> + {
> + if (warning_at (declarator->parenthesized, OPT_Wparentheses,
> + "unnecessary parentheses in declaration of %qs",
> + name))
> + {
> + gcc_rich_location iloc (declarator->parenthesized);
> + iloc.add_fixit_remove (get_start (declarator->parenthesized));
> + iloc.add_fixit_remove (get_finish (declarator->parenthesized));
> + inform (&iloc, "remove parentheses");
> + }
> + }
> if (declarator->kind == cdk_id || declarator->kind == cdk_decomp)
> break;
>
> diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
> index 03780fab0a7..57759fc1882 100644
> --- a/gcc/cp/parser.c
> +++ b/gcc/cp/parser.c
> @@ -21864,7 +21864,9 @@ cp_parser_direct_declarator (cp_parser* parser,
> open_paren = NULL;
> }
> if (open_paren)
> - declarator->parenthesized = open_paren->location;
> + declarator->parenthesized = make_location (open_paren->location,
> + open_paren->location,
> + close_paren->location);
> }
>
> /* If we entered a scope, we must exit it now. */
> diff --git a/gcc/testsuite/g++.dg/warn/mvp3.C b/gcc/testsuite/g++.dg/warn/mvp3.C
> new file mode 100644
> index 00000000000..4d371c5b093
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/mvp3.C
> @@ -0,0 +1,30 @@
> +// { dg-do compile }
> +// { dg-options "-Wparentheses -fdiagnostics-show-caret" }
> +// Test fix-it hints for the MVP warning.
> +
> +void
> +g ()
> +{
> + int (i); // { dg-warning "7:unnecessary parentheses" }
> +/* { dg-begin-multiline-output "" }
> + int (i);
> + ^~~
> + { dg-end-multiline-output "" } */
> +// { dg-message "7:remove parentheses" "" { target *-*-* } 8 }
> +/* { dg-begin-multiline-output "" }
> + int (i);
> + ^~~
> + - -
> + { dg-end-multiline-output "" } */
> + int (fn(void)); // { dg-warning "7:unnecessary parentheses" }
> +/* { dg-begin-multiline-output "" }
> + int (fn(void));
> + ^~~~~~~~~~
> + { dg-end-multiline-output "" } */
> +// { dg-message "7:remove parentheses" "" { target *-*-* } 19 }
> +/* { dg-begin-multiline-output "" }
> + int (fn(void));
> + ^~~~~~~~~~
> + - -
> + { dg-end-multiline-output "" } */
> +}
>
> base-commit: a3c13696fd2e18e6e2de52b25ddfe72284335732
>
More information about the Gcc-patches
mailing list