diff mbox series

c++: pedwarn for empty unnamed enum in decl [PR67048]

Message ID 20220429141218.41850-1-polacek@redhat.com
State New
Headers show
Series c++: pedwarn for empty unnamed enum in decl [PR67048] | expand

Commit Message

Marek Polacek April 29, 2022, 2:12 p.m. UTC
[dcl.dcl]/5 says that

  enum { };

is ill-formed, and since r197742 we issue a pedwarn.  However, the
pedwarn also fires for

   enum { } x;

which is well-formed.  So only warn when {} is followed by a ;.  This
should be correct since you can't have "enum {}, <whatever>" -- that
produces "expected unqualified-id before ',' token".

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?  I think
this could go to 12.2 and 11.4 too.

	PR c++/67048

gcc/cp/ChangeLog:

	* parser.cc (cp_parser_enum_specifier): Warn about empty unnamed enum
	only when it's followed by a semicolon.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/enum42.C: New test.
---
 gcc/cp/parser.cc                    | 4 +++-
 gcc/testsuite/g++.dg/cpp0x/enum42.C | 7 +++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/enum42.C


base-commit: 9ae8b993cd362e8aea4f65580aaf1453120207f2

Comments

Jason Merrill April 29, 2022, 6:31 p.m. UTC | #1
On 4/29/22 10:12, Marek Polacek wrote:
> [dcl.dcl]/5 says that
> 
>    enum { };
> 
> is ill-formed, and since r197742 we issue a pedwarn.  However, the
> pedwarn also fires for
> 
>     enum { } x;
> 
> which is well-formed.  So only warn when {} is followed by a ;.  This
> should be correct since you can't have "enum {}, <whatever>" -- that
> produces "expected unqualified-id before ',' token".
> 
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?  I think
> this could go to 12.2 and 11.4 too.

OK.

> 	PR c++/67048
> 
> gcc/cp/ChangeLog:
> 
> 	* parser.cc (cp_parser_enum_specifier): Warn about empty unnamed enum
> 	only when it's followed by a semicolon.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp0x/enum42.C: New test.
> ---
>   gcc/cp/parser.cc                    | 4 +++-
>   gcc/testsuite/g++.dg/cpp0x/enum42.C | 7 +++++++
>   2 files changed, 10 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp0x/enum42.C
> 
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index 2235da10c7c..0fa37fdfb66 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -21007,7 +21007,9 @@ cp_parser_enum_specifier (cp_parser* parser)
>         /* If the next token is not '}', then there are some enumerators.  */
>         else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
>   	{
> -	  if (is_unnamed && !scoped_enum_p)
> +	  if (is_unnamed && !scoped_enum_p
> +	      /* Don't warn for enum {} a; here.  */
> +	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
>   	    pedwarn (type_start_token->location, OPT_Wpedantic,
>   		     "ISO C++ forbids empty unnamed enum");
>   	}
> diff --git a/gcc/testsuite/g++.dg/cpp0x/enum42.C b/gcc/testsuite/g++.dg/cpp0x/enum42.C
> new file mode 100644
> index 00000000000..05b372a1947
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp0x/enum42.C
> @@ -0,0 +1,7 @@
> +// PR c++/67048
> +// { dg-do compile { target c++11 } }
> +// { dg-options -Wpedantic }
> +
> +typedef enum {} X;
> +enum {} x;
> +enum {} y, z;
> 
> base-commit: 9ae8b993cd362e8aea4f65580aaf1453120207f2
diff mbox series

Patch

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 2235da10c7c..0fa37fdfb66 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -21007,7 +21007,9 @@  cp_parser_enum_specifier (cp_parser* parser)
       /* If the next token is not '}', then there are some enumerators.  */
       else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
 	{
-	  if (is_unnamed && !scoped_enum_p)
+	  if (is_unnamed && !scoped_enum_p
+	      /* Don't warn for enum {} a; here.  */
+	      && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
 	    pedwarn (type_start_token->location, OPT_Wpedantic,
 		     "ISO C++ forbids empty unnamed enum");
 	}
diff --git a/gcc/testsuite/g++.dg/cpp0x/enum42.C b/gcc/testsuite/g++.dg/cpp0x/enum42.C
new file mode 100644
index 00000000000..05b372a1947
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/enum42.C
@@ -0,0 +1,7 @@ 
+// PR c++/67048
+// { dg-do compile { target c++11 } }
+// { dg-options -Wpedantic }
+
+typedef enum {} X;
+enum {} x;
+enum {} y, z;