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]

C++ PATCH: PR 29886


This patch fixes PR c++/29886, a rejects-valid regression on this
code:

+  if (int(A::x[i])) {}

which is a valid cast.  We issued a bogus error suggesting that
A::x[i] was an invalid VLA declaration.  

The parser was using at_function_scope_p to check to see if we were in
a function body because we correctly reject VLAs outside of function
bodies.  However, after seeing "A::" above we enter the scope of "A".
That's correct, because we want to resolve "i" to "A::i" if "A::i"
exists.  But, at_function_scope_p then sees that the innermost scope
is "A" and returns false.

I fixed this by giving the parser it's own "in_function_body" variable
since the parser itself can easily know whether or not we are in the
body of a function.  (That should also be marginally more efficient
than calling at_function_scope_p, which sometimes involves calls to
same_type_p.)  It might be possible to make in_function_body a global
variable, and use that in lieu of at_function_scope_p everywhere, but
I didn't try to go that far.

Tested on x86_64-unknown-linux-gnu, applied on the mainline.  I will
apply this to the 4.2 and 4.1 branches after testing.

Thanks to Jakub for investigating this PR and for providing me with
useful analysis.
 
--
Mark Mitchell
CodeSourcery
mark@codesourcery.com
(650) 331-3385 x713

2006-11-26  Mark Mitchell  <mark@codesourcery.com>

	PR c++/29886
	* parser.c (cp_parser): Add in_function_body.
	(cp_parser_new): Initialize it.
	(cp_parser_primary_expression): Use parser->in_function_body
	instead of at_function_scope_p.
	(cp_parser_asm_definition): Likewise.
	(cp_parser_direct_declarator): Likewise.
	(cp_parser_class_specifier): Clear parser->in_function_body.
	(cp_parser_constructor_declarator_p): Use parser->in_function_body
	instead of at_function_scope_p.
	(cp_parser_function_body_after_declarator): Set
	parser->in_function_body.

2006-11-26  Mark Mitchell  <mark@codesourcery.com>

	PR c++/29886
	* g++.dg/expr/cast8.C: New test.

Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c	(revision 119231)
+++ gcc/cp/parser.c	(working copy)
@@ -1390,6 +1390,10 @@ typedef struct cp_parser GTY(())
      character set.  */
   bool translate_strings_p;
 
+  /* TRUE if we are presently parsing the body of a function, but not
+     a local class.  */
+  bool in_function_body;
+
   /* If non-NULL, then we are parsing a construct where new type
      definitions are not permitted.  The string stored here will be
      issued as an error message if a type is defined.  */
@@ -2634,6 +2638,9 @@ cp_parser_new (void)
   /* String literals should be translated to the execution character set.  */
   parser->translate_strings_p = true;
 
+  /* We are not parsing a function body.  */
+  parser->in_function_body = false;
+
   /* The unparsed function queue is empty.  */
   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
 
@@ -2990,7 +2997,7 @@ cp_parser_primary_expression (cp_parser 
 		 int i = ({ int j = 3; j + 1; });
 
 	       at class or namespace scope.  */
-	    if (!at_function_scope_p ())
+	    if (!parser->in_function_body)
 	      error ("statement-expressions are allowed only inside functions");
 	    /* Start the statement-expression.  */
 	    expr = begin_stmt_expr ();
@@ -10952,7 +10959,7 @@ cp_parser_asm_definition (cp_parser* par
      too.  Doing that means that we have to treat the `::' operator as
      two `:' tokens.  */
   if (cp_parser_allow_gnu_extensions_p (parser)
-      && at_function_scope_p ()
+      && parser->in_function_body
       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
 	  || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
     {
@@ -11018,7 +11025,7 @@ cp_parser_asm_definition (cp_parser* par
   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
 
   /* Create the ASM_EXPR.  */
-  if (at_function_scope_p ())
+  if (parser->in_function_body)
     {
       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
 				  inputs, clobbers);
@@ -11698,7 +11705,7 @@ cp_parser_direct_declarator (cp_parser* 
 	      /* Normally, the array bound must be an integral constant
 		 expression.  However, as an extension, we allow VLAs
 		 in function scopes.  */
-	      else if (!at_function_scope_p ())
+	      else if (!parser->in_function_body)
 		{
 		  error ("array bound is not an integer constant");
 		  bounds = error_mark_node;
@@ -13080,6 +13087,7 @@ cp_parser_class_specifier (cp_parser* pa
   int has_trailing_semicolon;
   bool nested_name_specifier_p;
   unsigned saved_num_template_parameter_lists;
+  bool saved_in_function_body;
   tree old_scope = NULL_TREE;
   tree scope = NULL_TREE;
 
@@ -13114,6 +13122,9 @@ cp_parser_class_specifier (cp_parser* pa
   saved_num_template_parameter_lists
     = parser->num_template_parameter_lists;
   parser->num_template_parameter_lists = 0;
+  /* We are not in a function body.  */
+  saved_in_function_body = parser->in_function_body;
+  parser->in_function_body = false;
 
   /* Start the class.  */
   if (nested_name_specifier_p)
@@ -13222,7 +13233,8 @@ cp_parser_class_specifier (cp_parser* pa
   /* Put back any saved access checks.  */
   pop_deferring_access_checks ();
 
-  /* Restore the count of active template-parameter-lists.  */
+  /* Restore saved state.  */
+  parser->in_function_body = saved_in_function_body;
   parser->num_template_parameter_lists
     = saved_num_template_parameter_lists;
 
@@ -15388,7 +15400,7 @@ cp_parser_constructor_declarator_p (cp_p
   /* The common case is that this is not a constructor declarator, so
      try to avoid doing lots of work if at all possible.  It's not
      valid declare a constructor at function scope.  */
-  if (at_function_scope_p ())
+  if (parser->in_function_body)
     return false;
   /* And only certain tokens can begin a constructor declarator.  */
   next_token = cp_lexer_peek_token (parser->lexer);
@@ -15578,8 +15590,11 @@ cp_parser_function_definition_after_decl
   tree fn;
   bool ctor_initializer_p = false;
   bool saved_in_unbraced_linkage_specification_p;
+  bool saved_in_function_body;
   unsigned saved_num_template_parameter_lists;
 
+  saved_in_function_body = parser->in_function_body;
+  parser->in_function_body = true;
   /* If the next token is `return', then the code may be trying to
      make use of the "named return value" extension that G++ used to
      support.  */
@@ -15633,6 +15648,7 @@ cp_parser_function_definition_after_decl
     = saved_in_unbraced_linkage_specification_p;
   parser->num_template_parameter_lists
     = saved_num_template_parameter_lists;
+  parser->in_function_body = saved_in_function_body;
 
   return fn;
 }
Index: gcc/testsuite/g++.dg/expr/cast8.C
===================================================================
--- gcc/testsuite/g++.dg/expr/cast8.C	(revision 0)
+++ gcc/testsuite/g++.dg/expr/cast8.C	(revision 0)
@@ -0,0 +1,11 @@
+// PR c++/29886
+
+struct A {
+  static int x[1];
+};
+
+void foo(int i)
+{
+  if (int(A::x[i])) {}
+}
+


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