public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH, C++] Fix-it info for invalid class/struct after enum
@ 2017-04-25  8:04 Volker Reichelt
  2017-04-25 12:07 ` Nathan Sidwell
  0 siblings, 1 reply; 2+ messages in thread
From: Volker Reichelt @ 2017-04-25  8:04 UTC (permalink / raw)
  To: gcc-patches; +Cc: David Malcolm

Hi,

the following patch adds fix-it information for a pedwarn in the C++
parser about the invalid use of class/struct after enum.
I factored out the call to cp_lexer_peek_token, because it was called
already 3 times (twice from within cp_lexer_next_token_is_keyword)
and I didn't want to add a 4th call to get the token's location.
I also fixed the broken indentation of the pedwarn.

Thanks to David for suggesting to use gcc_rich_location::add_range
to highlight multiple tokens.

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

OK for trunk?

Regards,
Volker


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

	* parser.c (cp_parser_elaborated_type_specifier): Add fix-it to
	diagnostic of invalid class/struct keyword after enum.

Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c	(revision 247110)
+++ gcc/cp/parser.c	(working copy)
@@ -17270,12 +17270,16 @@
       tag_type = enum_type;
       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
 	 enums) is used here.  */
-      if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
-	  || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
+      cp_token *token = cp_lexer_peek_token (parser->lexer);
+      if (cp_parser_is_keyword (token, RID_CLASS)
+	  || cp_parser_is_keyword (token, RID_STRUCT))
 	{
-	    pedwarn (input_location, 0, "elaborated-type-specifier "
-		      "for a scoped enum must not use the %qD keyword",
-		      cp_lexer_peek_token (parser->lexer)->u.value);
+	  gcc_rich_location richloc (token->location);
+	  richloc.add_range (input_location, false);
+	  richloc.add_fixit_remove ();
+	  pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
+			       "a scoped enum must not use the %qD keyword",
+			       token->u.value);
 	  /* Consume the `struct' or `class' and parse it anyway.  */
 	  cp_lexer_consume_token (parser->lexer);
 	}
===================================================================

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

	* g++.dg/cpp0x/enum34.C: New test.

Index: gcc/testsuite/g++.dg/cpp0x/enum34.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/enum34.C	2017-04-25
+++ gcc/testsuite/g++.dg/cpp0x/enum34.C	2017-04-25
@@ -0,0 +1,11 @@
+// { dg-options "-fdiagnostics-show-caret" }
+// { dg-do compile { target c++11 } }
+
+enum class E;
+
+enum class E e;  /* { dg-warning "scoped enum must not use" }
+  { dg-begin-multiline-output "" }
+ enum class E e;
+ ~~~~ ^~~~~
+      -----
+  { dg-end-multiline-output "" } */
===================================================================

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH, C++] Fix-it info for invalid class/struct after enum
  2017-04-25  8:04 [PATCH, C++] Fix-it info for invalid class/struct after enum Volker Reichelt
@ 2017-04-25 12:07 ` Nathan Sidwell
  0 siblings, 0 replies; 2+ messages in thread
From: Nathan Sidwell @ 2017-04-25 12:07 UTC (permalink / raw)
  To: Volker Reichelt, gcc-patches; +Cc: David Malcolm

On 04/25/2017 03:39 AM, Volker Reichelt wrote:
> Hi,
> 
> the following patch adds fix-it information for a pedwarn in the C++
> parser about the invalid use of class/struct after enum.

> 
> 2017-04-25  Volker Reichelt  <v.reichelt@netcologne.de>
> 
> 	* parser.c (cp_parser_elaborated_type_specifier): Add fix-it to
> 	diagnostic of invalid class/struct keyword after enum.

ok


-- 
Nathan Sidwell

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-04-25 11:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-25  8:04 [PATCH, C++] Fix-it info for invalid class/struct after enum Volker Reichelt
2017-04-25 12:07 ` Nathan Sidwell

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).