This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/24907] "int x, ;" accepted


Hi

I belive that the attached patch fixes PR c++/24907. The problem was
that comma was allowed after processing each declarator, but next
iteration didn't require a declarator to appear. The fix is simply to
require comma at the beginning of second and later iterations.
The patch also contains a testcase.

Bootstrap passed, check-g++ passed without FAILs, both on
i686-pc-linux-gnu.

Petr

ChangeLog entry:

2005-11-23  Petr Machata  <machata@post.cz>

	PR c++/24907
	* parser.c (cp_parser_simple_declaration): Require comma at the 
	beginning of processing second and later declarators, instead of
	allowing the comma at the end of each iteration.

Index: ../gcc/gcc/testsuite/g++.dg/parse/comma2.C
===================================================================
--- ../gcc/gcc/testsuite/g++.dg/parse/comma2.C	(revision 0)
+++ ../gcc/gcc/testsuite/g++.dg/parse/comma2.C	(revision 0)
@@ -0,0 +1,19 @@
+// { dg-do compile }
+
+// Copyright (C) 2005 Free Software Foundation, Inc.
+
+// PR c++/24907 [3.4/4.0/4.1/4.2 Regression] "int x, ;" accepted
+
+int x;
+int y,; /* { dg-error "expected unqualified-id" } */
+
+int main()
+{
+  int a;
+  int b,;    /* { dg-error "expected unqualified-id" } */
+  int c,d;
+  int e,f,;  /* { dg-error "expected unqualified-id" } */
+  int g,h,i;
+  int j,k,l,;/* { dg-error "expected unqualified-id" } */
+  int m,,,n; /* { dg-error "expected unqualified-id" } */
+}
Index: ../gcc/gcc/cp/parser.c
===================================================================
--- ../gcc/gcc/cp/parser.c	(revision 107291)
+++ ../gcc/gcc/cp/parser.c	(working copy)
@@ -7142,7 +7142,16 @@
       bool function_definition_p;
       tree decl;
 
-      saw_declarator = true;
+      if (saw_declarator)
+	{
+	  /* If we are processing next declarator, coma is expected */
+	  token = cp_lexer_peek_token (parser->lexer);
+	  gcc_assert (token->type == CPP_COMMA);
+	  cp_lexer_consume_token (parser->lexer);
+	}
+      else
+	saw_declarator = true;
+
       /* Parse the init-declarator.  */
       decl = cp_parser_init_declarator (parser, &decl_specifiers,
 					function_definition_allowed_p,
@@ -7177,7 +7186,7 @@
       token = cp_lexer_peek_token (parser->lexer);
       /* If it's a `,', there are more declarators to come.  */
       if (token->type == CPP_COMMA)
-	cp_lexer_consume_token (parser->lexer);
+	/* will be consumed next time around */;
       /* If it's a `;', we are done.  */
       else if (token->type == CPP_SEMICOLON)
 	break;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]