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]

PR c++/26997 g++ reports misleading error message when the identifier with error occurs earlier on the same line


Bootstrapped and regression tested on x86_64-unknown-linux-gnu with
--enable-languages=all,obj-c++ --enable-decimal-float.

The patch should be self-explanatory (otherwise, it needs more
comments). There may be other ways to improve this. See
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26997#c5

OK for trunk?

2008-10-26  Manuel López-Ibáñez  <manu@gcc.gnu.org>

	PR c++/26997
cp/
	* parser.c (cp_parser_cast_expression): Parse definitely if we are
	sure this is a cast to non-function type.
testsuite/
	* g++.dg/warn/pr26997.C: New.
Index: gcc/testsuite/g++.dg/warn/pr26997.C
===================================================================
--- gcc/testsuite/g++.dg/warn/pr26997.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/pr26997.C	(revision 0)
@@ -0,0 +1,15 @@
+// PR c++/26997
+// { dg-do compile }
+void * malloc (unsigned long size);
+typedef struct { int a; } t;
+
+void foo()
+{
+  t *v3;
+  v3 = (t *)
+    malloc(
+	   sizeof(t) 
+	   * 
+           t->a // { dg-error "before '->' token" }
+	   );
+}
Index: gcc/cp/parser.c
===================================================================
--- gcc/cp/parser.c	(revision 141322)
+++ gcc/cp/parser.c	(working copy)
@@ -5986,21 +5986,34 @@ cp_parser_cast_expression (cp_parser *pa
 	}
 
       /* Restore the saved message.  */
       parser->type_definition_forbidden_message = saved_message;
 
-      /* If ok so far, parse the dependent expression. We cannot be
-	 sure it is a cast. Consider `(T ())'.  It is a parenthesized
-	 ctor of T, but looks like a cast to function returning T
-	 without a dependent expression.  */
+      /* If this is a FUNCTION_TYPE we cannot be sure it is a
+	 cast. Consider `(T ())'.  It is a parenthesized ctor of T,
+	 but looks like a cast to function returning T without a
+	 dependent expression.  */
       if (!cp_parser_error_occurred (parser))
-	expr = cp_parser_cast_expression (parser,
-					  /*address_p=*/false,
-					  /*cast_p=*/true);
-
+	{
+	  if (TREE_CODE (type) != FUNCTION_TYPE) 
+	    {
+	      /* But if it is not a cast to function, then we are sure
+		 this is a cast.  */
+	      cp_parser_parse_definitely (parser);
+	      expr = cp_parser_cast_expression (parser,
+						/*address_p=*/false,
+						/*cast_p=*/true);
+	      goto definitive;
+	    }
+	  expr = cp_parser_cast_expression (parser,
+					    /*address_p=*/false,
+					    /*cast_p=*/true);
+	}
+       
       if (cp_parser_parse_definitely (parser))
 	{
+	definitive:
 	  /* Warn about old-style casts, if so requested.  */
 	  if (warn_old_style_cast
 	      && !in_system_header
 	      && !VOID_TYPE_P (type)
 	      && current_lang_name != lang_name_c)

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