diff mbox

C++ parser: two fix-it hints for broken member declarations

Message ID tkrat.27e841888acabc28@netcologne.de
State New
Headers show

Commit Message

Volker Reichelt April 29, 2017, 5:42 p.m. UTC
Hi,

the following patch adds fix-it hints to the C++ parser for a stray
comma and a missing semicolon at the end of a member declaration.

Bootstrapped and regtested on x86_64-pc-linux-gnu.

OK for trunk?

Regards,
Volker


2017-04-29  Volker Reichelt  <v.reichelt@netcologne.de>

	* parser.c (cp_parser_member_declaration): Add fix-it hints for
	stray comma and missing semicolon at end of member declaration.

===================================================================

Comments

David Malcolm April 29, 2017, 10:10 p.m. UTC | #1
On Sat, 2017-04-29 at 19:42 +0200, Volker Reichelt wrote:
> Hi,
> 
> the following patch adds fix-it hints to the C++ parser for a stray
> comma and a missing semicolon at the end of a member declaration.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu.
> 
> OK for trunk?

OK for trunk

Thanks
Dave

> Regards,
> Volker
> 
> 
> 2017-04-29  Volker Reichelt  <v.reichelt@netcologne.de>
> 
>         * parser.c (cp_parser_member_declaration): Add fix-it hints
> for
>         stray comma and missing semicolon at end of member
> declaration.
> 
> Index: gcc/cp/parser.c
> ===================================================================
> --- gcc/cp/parser.c     2017-04-28
> +++ gcc/cp/parser.c     2017-04-28
> @@ -23461,8 +23461,10 @@
>               if (cp_lexer_next_token_is (parser->lexer,
> CPP_SEMICOLON))
>                 {
>                   cp_token *token = cp_lexer_previous_token (parser
> ->lexer);
> -                 error_at (token->location,
> -                           "stray %<,%> at end of member
> declaration");
> +                 gcc_rich_location richloc (token->location);
> +                 richloc.add_fixit_remove ();
> +                 error_at_rich_loc (&richloc, "stray %<,%> at end of
> "
> +                                    "member declaration");
>                 }
>             }
>           /* If the next token isn't a `;', then we have a parse
> error.  */
> @@ -23473,8 +23475,10 @@
>                  actual semicolon is missing.  Find the previous
> token
>                  and use that for our error position.  */
>               cp_token *token = cp_lexer_previous_token (parser
> ->lexer);
> -             error_at (token->location,
> -                       "expected %<;%> at end of member
> declaration");
> +             gcc_rich_location richloc (token->location);
> +             richloc.add_fixit_insert_after (";");
> +             error_at_rich_loc (&richloc, "expected %<;%> at end of
> "
> +                                "member declaration");
>  
>               /* Assume that the user meant to provide a semicolon. 
> If
>                  we were to cp_parser_skip_to_end_of_statement, we
> might
> ===================================================================
> 
> 2017-04-29  Volker Reichelt  <v.reichelt@netcologne.de>
> 
>         * g++.dg/diagnostic/member-decl-1.C: New test.
> 
> Index: gcc/testsuite/g++.dg/diagnostic/member-decl-1.C
> ===================================================================
> --- gcc/testsuite/g++.dg/diagnostic/member-decl-1.C     2017-04-29
> +++ gcc/testsuite/g++.dg/diagnostic/member-decl-1.C     2017-04-29
> @@ -0,0 +1,18 @@
> +// { dg-options "-fdiagnostics-show-caret" }
> +
> +struct A
> +{
> +  int i,;  /* { dg-error "stray .,. at end of member declaration" }
> +  { dg-begin-multiline-output "" }
> +   int i,;
> +        ^
> +        -
> +  { dg-end-multiline-output "" } */
> +
> +  int j  /* { dg-error "expected .;. at end of member declaration" }
> +  { dg-begin-multiline-output "" }
> +   int j
> +       ^
> +        ;
> +  { dg-end-multiline-output "" } */
> +};
> ===================================================================
>
diff mbox

Patch

Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c	2017-04-28
+++ gcc/cp/parser.c	2017-04-28
@@ -23461,8 +23461,10 @@ 
 	      if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
 		{
 		  cp_token *token = cp_lexer_previous_token (parser->lexer);
-		  error_at (token->location,
-			    "stray %<,%> at end of member declaration");
+		  gcc_rich_location richloc (token->location);
+		  richloc.add_fixit_remove ();
+		  error_at_rich_loc (&richloc, "stray %<,%> at end of "
+				     "member declaration");
 		}
 	    }
 	  /* If the next token isn't a `;', then we have a parse error.  */
@@ -23473,8 +23475,10 @@ 
 		 actual semicolon is missing.  Find the previous token
 		 and use that for our error position.  */
 	      cp_token *token = cp_lexer_previous_token (parser->lexer);
-	      error_at (token->location,
-			"expected %<;%> at end of member declaration");
+	      gcc_rich_location richloc (token->location);
+	      richloc.add_fixit_insert_after (";");
+	      error_at_rich_loc (&richloc, "expected %<;%> at end of "
+				 "member declaration");
 
 	      /* Assume that the user meant to provide a semicolon.  If
 		 we were to cp_parser_skip_to_end_of_statement, we might
===================================================================

2017-04-29  Volker Reichelt  <v.reichelt@netcologne.de>

	* g++.dg/diagnostic/member-decl-1.C: New test.

Index: gcc/testsuite/g++.dg/diagnostic/member-decl-1.C
===================================================================
--- gcc/testsuite/g++.dg/diagnostic/member-decl-1.C	2017-04-29
+++ gcc/testsuite/g++.dg/diagnostic/member-decl-1.C	2017-04-29
@@ -0,0 +1,18 @@ 
+// { dg-options "-fdiagnostics-show-caret" }
+
+struct A
+{
+  int i,;  /* { dg-error "stray .,. at end of member declaration" }
+  { dg-begin-multiline-output "" }
+   int i,;
+        ^
+        -
+  { dg-end-multiline-output "" } */
+
+  int j  /* { dg-error "expected .;. at end of member declaration" }
+  { dg-begin-multiline-output "" }
+   int j
+       ^
+        ;
+  { dg-end-multiline-output "" } */
+};