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] Fix main return type checking (PR c++/29735)


Hi!

cplus_decl_attributes can change the return type, so the main's
return type check should IMHO be done only after that call, not before,
otherwise we ICE on the testcase below because main with vector type
return value type hits an assertion later on.

Ok for 4.1/4.2/4.3?

2006-11-21  Jakub Jelinek  <jakub@redhat.com>

	PR c++/29735
	* decl.c (grokfndecl): Check main's return type after applying
	attributes, not before.

	* g++.dg/warn/main-3.C: New test.

--- gcc/cp/decl.c.jj	2006-11-20 23:12:57.000000000 +0100
+++ gcc/cp/decl.c	2006-11-21 17:20:08.000000000 +0100
@@ -6026,16 +6026,6 @@ grokfndecl (tree ctype,
 	error ("cannot declare %<::main%> to be inline");
       if (!publicp)
 	error ("cannot declare %<::main%> to be static");
-      if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
-			integer_type_node))
-	{
-	  tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
-	  tree newtype;
-	  error ("%<::main%> must return %<int%>");
-	  newtype =  build_function_type (integer_type_node,
-					  oldtypeargs);
-	  TREE_TYPE (decl) = newtype;
-	}
       check_main_parameter_types (decl);
       inlinep = 0;
       publicp = 1;
@@ -6143,6 +6133,19 @@ grokfndecl (tree ctype,
       *attrlist = NULL_TREE;
     }
 
+  /* Check main's return type after attributes have been applied.  */
+  if (ctype == NULL_TREE && DECL_MAIN_P (decl)
+      && !same_type_p (TREE_TYPE (TREE_TYPE (decl)),
+		       integer_type_node))
+    {
+      tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl));
+      tree newtype;
+      error ("%<::main%> must return %<int%>");
+      newtype = build_function_type (integer_type_node,
+				     oldtypeargs);
+      TREE_TYPE (decl) = newtype;
+    }
+
   if (ctype != NULL_TREE
       && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
       && check)
--- gcc/testsuite/g++.dg/warn/main-3.C.jj	2006-11-21 18:19:56.000000000 +0100
+++ gcc/testsuite/g++.dg/warn/main-3.C	2006-11-21 18:19:35.000000000 +0100
@@ -0,0 +1,7 @@
+// PR c++/29735
+// { dg-do compile }
+
+int __attribute__ ((vector_size (8))) main () // { dg-error "must return" }
+{
+  return 0;
+}

	Jakub


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