public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ PATCH] Fix cp_parser_enum_specifier (PR c++/38021)
@ 2008-11-05 14:48 Jakub Jelinek
  2008-11-05 14:58 ` Manuel López-Ibáñez
  2008-11-05 15:47 ` Mark Mitchell
  0 siblings, 2 replies; 5+ messages in thread
From: Jakub Jelinek @ 2008-11-05 14:48 UTC (permalink / raw)
  To: Jason Merrill, Mark Mitchell; +Cc: gcc-patches

Hi!

If type_specifiers.type == error_mark_node, G++ hangs.  The problem
is returning from cp_parser_enum_specifier without undoing
cp_parser_parse_tentatively this function has done earlier.
So, either we abort the tentative parse (or commit to it) in this case
and replace the following
if (type_specifiers.type != error_mark_node)
  {
    some code
  }
else
  cp_parser_error (parser, "expected underlying type of enumeration");
with just some code, or we leave out the early exit and let the following
code handle it.  The following patch does the latter.

Ok for trunk?

2008-11-05  Jakub Jelinek  <jakub@redhat.com>

	PR c++/38021
	* parser.c (cp_parser_enum_specifier): Don't return if
	type specifier is errorneous.

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

--- gcc/cp/parser.c.jj	2008-10-29 18:49:05.000000000 +0100
+++ gcc/cp/parser.c	2008-11-05 15:26:34.000000000 +0100
@@ -11827,9 +11827,7 @@ cp_parser_enum_specifier (cp_parser* par
       /* Parse the type-specifier-seq.  */
       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
                                     &type_specifiers);
-      if (type_specifiers.type == error_mark_node)
-        return error_mark_node;
-     
+
       /* If that didn't work, stop.  */
       if (type_specifiers.type != error_mark_node)
         {
--- gcc/testsuite/g++.dg/cpp0x/enum1.C.jj	2008-11-05 15:30:23.000000000 +0100
+++ gcc/testsuite/g++.dg/cpp0x/enum1.C	2008-11-05 15:29:48.000000000 +0100
@@ -0,0 +1,5 @@
+// PR c++/38021
+// { dg-do compile }
+// { dg-options "-std=c++0x" }
+
+auto enum : { do	// { dg-error "expected" }

	Jakub

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

* Re: [C++ PATCH] Fix cp_parser_enum_specifier (PR c++/38021)
  2008-11-05 14:48 [C++ PATCH] Fix cp_parser_enum_specifier (PR c++/38021) Jakub Jelinek
@ 2008-11-05 14:58 ` Manuel López-Ibáñez
  2008-11-05 15:47 ` Mark Mitchell
  1 sibling, 0 replies; 5+ messages in thread
From: Manuel López-Ibáñez @ 2008-11-05 14:58 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Jason Merrill, Mark Mitchell, gcc-patches

2008/11/5 Jakub Jelinek <jakub@redhat.com>:
> @@ -0,0 +1,5 @@
> +// PR c++/38021
> +// { dg-do compile }
> +// { dg-options "-std=c++0x" }
> +
> +auto enum : { do       // { dg-error "expected" }
>

What does this message actually say?

Cheers,

Manuel.

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

* Re: [C++ PATCH] Fix cp_parser_enum_specifier (PR c++/38021)
  2008-11-05 14:48 [C++ PATCH] Fix cp_parser_enum_specifier (PR c++/38021) Jakub Jelinek
  2008-11-05 14:58 ` Manuel López-Ibáñez
@ 2008-11-05 15:47 ` Mark Mitchell
  2008-11-06 10:35   ` Jakub Jelinek
  1 sibling, 1 reply; 5+ messages in thread
From: Mark Mitchell @ 2008-11-05 15:47 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Jason Merrill, gcc-patches

Jakub Jelinek wrote:

> 2008-11-05  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/38021
> 	* parser.c (cp_parser_enum_specifier): Don't return if
> 	type specifier is errorneous.

"erroneous"

> 	* g++.dg/cpp0x/enum1.C: New test.

I think that your patch means that we'll (eventually) abort the
tentative parse and return after seeing something like "enum x : 3".
But, once we see the ":" we can commit to this being an enum-specifier;
there's no other reasonable interpretation of what the user meant.

The reason that this function delays committing is that we don't know if
we're looking at "enum X" or "enum X { ... " when this function is
called.  The former is not an enum-specifier, but rather an
elaborated-type-specifier.

This comment:

  /* Caller guarantees that the current token is 'enum', an identifier

     possibly follows, and the token after that is an opening brace.

     If we don't have an identifier, fabricate an anonymous name for

     the enumeration being defined.  */

is, AFAICT, false.  The caller is cp_parser_type_specifier, and I don't
see those kinds of checks.

But, when we see the colon, or the C++0x class/struct keyword, we can
commit.  "enum struct X" is only part of a C++0x enum-specifier, and so
is "enum X : ".

So, I think the function should be reworked in that way.

Thanks,

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

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

* Re: [C++ PATCH] Fix cp_parser_enum_specifier (PR c++/38021)
  2008-11-05 15:47 ` Mark Mitchell
@ 2008-11-06 10:35   ` Jakub Jelinek
  2008-11-09 23:22     ` Mark Mitchell
  0 siblings, 1 reply; 5+ messages in thread
From: Jakub Jelinek @ 2008-11-06 10:35 UTC (permalink / raw)
  To: Mark Mitchell; +Cc: Jason Merrill, gcc-patches

On Wed, Nov 05, 2008 at 07:46:08AM -0800, Mark Mitchell wrote:
> But, when we see the colon, or the C++0x class/struct keyword, we can
> commit.  "enum struct X" is only part of a C++0x enum-specifier, and so
> is "enum X : ".
> 
> So, I think the function should be reworked in that way.

Like this?

Note that we can't commit if we see enum class or enum struct, both
are valid for elaborated type specifier.

2008-11-06  Jakub Jelinek  <jakub@redhat.com>

	PR c++/38021
	* parser.c (cp_parser_enum_specifier): After parsing :,
	parse definitely.  Don't return early if type specifier
	is erroneous.

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

--- gcc/cp/parser.c.jj	2008-10-29 18:49:05.000000000 +0100
+++ gcc/cp/parser.c	2008-11-06 11:08:14.000000000 +0100
@@ -11780,6 +11780,7 @@ cp_parser_enum_specifier (cp_parser* par
   tree type;
   tree attributes;
   bool scoped_enum_p = false;
+  bool has_underlying_type = false;
   tree underlying_type = NULL_TREE;
 
   /* Parse tentatively so that we can back up if we don't find a
@@ -11805,7 +11806,7 @@ cp_parser_enum_specifier (cp_parser* par
 
       scoped_enum_p = true;
     }
-      
+
   attributes = cp_parser_attributes_opt (parser);
 
   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
@@ -11818,18 +11819,22 @@ cp_parser_enum_specifier (cp_parser* par
     {
       cp_decl_specifier_seq type_specifiers;
 
+      /* At this point this is surely not elaborated type specifier.  */
+      if (!cp_parser_parse_definitely (parser))
+	return NULL_TREE;
+
       if (cxx_dialect == cxx98)
         maybe_warn_cpp0x ("scoped enums");
 
       /* Consume the `:'.  */
       cp_lexer_consume_token (parser->lexer);
 
+      has_underlying_type = true;
+
       /* Parse the type-specifier-seq.  */
       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
                                     &type_specifiers);
-      if (type_specifiers.type == error_mark_node)
-        return error_mark_node;
-     
+
       /* If that didn't work, stop.  */
       if (type_specifiers.type != error_mark_node)
         {
@@ -11838,15 +11843,17 @@ cp_parser_enum_specifier (cp_parser* par
           if (underlying_type == error_mark_node)
             underlying_type = NULL_TREE;
         }
-      else
-        cp_parser_error (parser, "expected underlying type of enumeration");
     }
 
   /* Look for the `{' but don't consume it yet.  */
   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
-    cp_parser_simulate_error (parser);
+    {
+      cp_parser_error (parser, "expected %<{%>");
+      if (has_underlying_type)
+	return NULL_TREE;
+    }
 
-  if (!cp_parser_parse_definitely (parser))
+  if (!has_underlying_type && !cp_parser_parse_definitely (parser))
     return NULL_TREE;
 
   /* Issue an error message if type-definitions are forbidden here.  */
--- gcc/testsuite/g++.dg/cpp0x/enum1.C.jj	2008-11-05 15:30:23.000000000 +0100
+++ gcc/testsuite/g++.dg/cpp0x/enum1.C	2008-11-06 11:05:51.000000000 +0100
@@ -0,0 +1,6 @@
+// PR c++/38021
+// { dg-do compile }
+// { dg-options "-std=gnu++0x" }
+
+enum : { };	// { dg-error "expected type-specifier" }
+enum : 3 { };	// { dg-error "expected" }

	Jakub

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

* Re: [C++ PATCH] Fix cp_parser_enum_specifier (PR c++/38021)
  2008-11-06 10:35   ` Jakub Jelinek
@ 2008-11-09 23:22     ` Mark Mitchell
  0 siblings, 0 replies; 5+ messages in thread
From: Mark Mitchell @ 2008-11-09 23:22 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Jason Merrill, gcc-patches

Jakub Jelinek wrote:

> 2008-11-06  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR c++/38021
> 	* parser.c (cp_parser_enum_specifier): After parsing :,
> 	parse definitely.  Don't return early if type specifier
> 	is erroneous.
> 
> 	* g++.dg/cpp0x/enum1.C: New test.

OK.

-- 
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

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

end of thread, other threads:[~2008-11-09 22:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-11-05 14:48 [C++ PATCH] Fix cp_parser_enum_specifier (PR c++/38021) Jakub Jelinek
2008-11-05 14:58 ` Manuel López-Ibáñez
2008-11-05 15:47 ` Mark Mitchell
2008-11-06 10:35   ` Jakub Jelinek
2008-11-09 23:22     ` Mark Mitchell

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