Subject: Re: bin/2273: Revised warning for return types other than int from main.
To: None <gnats-bugs@NetBSD.ORG>
From: der Mouse <mouse@Collatz.McRCIM.McGill.EDU>
List: netbsd-bugs
Date: 05/06/1996 07:23:49
seebs is basically right, unsurprisingly, but I find that this patch
alone is not enough.  If nothing else, when building the kernel
(kernels being a standalone environment, where main is not a special
name), the warning is provoked 'cause main returns void.

I added -Wmain-type and -Wno-main-type to control the warning, and added
-Wno-main-type to sys/arch/sparc/conf/Makefile.sparc (the sparc port
being the one I noticed the kernel problem on).  If this is accepted,
other kernel template Makefiles should probably get it too.

--- OLD/gnu/usr.bin/gcc/common/c-decl.c	Thu Jan  1 00:00:00 1970
+++ NEW/gnu/usr.bin/gcc/common/c-decl.c	Thu Jan  1 00:00:00 1970
@@ -508,6 +508,10 @@
 
 int warn_missing_declarations;
 
+/* Nonzero means warn if main's type isn't one of the allowed ones. */
+
+int warn_main_type;
+
 /* Nonzero means warn about multiple (redundant) decls for the same single
    variable or function.  */
 
@@ -659,6 +663,10 @@
     warn_missing_declarations = 1;
   else if (!strcmp (p, "-Wno-missing-declarations"))
     warn_missing_declarations = 0;
+  else if (!strcmp (p, "-Wmain-type"))
+    warn_main_type = 1;
+  else if (!strcmp (p, "-Wno-main-type"))
+    warn_main_type = 0;
   else if (!strcmp (p, "-Wredundant-decls"))
     warn_redundant_decls = 1;
   else if (!strcmp (p, "-Wno-redundant-decls"))
@@ -726,6 +734,7 @@
       warn_char_subscripts = 1;
       warn_parentheses = 1;
       warn_missing_braces = 1;
+      warn_main_type = 1;
     }
   else
     return 0;
@@ -6101,6 +6110,12 @@
 	   && old_decl == IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)))
     warning_with_decl (decl1,
 		    "`%s' was used with no declaration before its definition");
+
+  /* XXX should check args too: (void) or (int, char **) */
+  if (warn_main_type
+      && !strcmp("main",IDENTIFIER_POINTER(DECL_NAME(decl1)))
+      && TYPE_MAIN_VARIANT(TREE_TYPE(TREE_TYPE(decl1))) != integer_type_node)
+    warning_with_decl (decl1, "return type of `main' is not `int'");
 
   /* This is a definition, not a reference.
      So normally clear DECL_EXTERNAL.
--- OLD/gnu/usr.bin/gcc/common/toplev.c	Thu Jan  1 00:00:00 1970
+++ NEW/gnu/usr.bin/gcc/common/toplev.c	Thu Jan  1 00:00:00 1970
@@ -625,6 +625,8 @@
   "-Wno-import",
   "-Wimplicit",
   "-Wno-implicit",
+  "-Wmain-type",
+  "-Wno-main-type",
   "-Wmissing-braces",
   "-Wno-missing-braces",
   "-Wmissing-declarations",
--- OLD/sys/arch/sparc/conf/Makefile.sparc	Thu Jan  1 00:00:00 1970
+++ NEW/sys/arch/sparc/conf/Makefile.sparc	Thu Jan  1 00:00:00 1970
@@ -34,7 +34,7 @@
 
 INCLUDES=	-I. -I$S/arch -I$S
 CPPFLAGS=	${INCLUDES} ${IDENT} -D_KERNEL
-GCCWFLAGS=-Werror -Wall -Wstrict-prototypes -Wmissing-prototypes
+GCCWFLAGS=-Werror -Wall -Wstrict-prototypes -Wmissing-prototypes -Wno-main-type
 CFLAGS=		${DEBUG} -O2 ${GCCWFLAGS}
 AFLAGS=		-x assembler-with-cpp -traditional-cpp -D_LOCORE
 LINKFLAGS=	-N -p -Ttext F8004000 -e start

					der Mouse

			    mouse@collatz.mcrcim.mcgill.edu