This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Patch for C99 'main' semantics
- To: gcc-patches at gcc dot gnu dot org
- Subject: Patch for C99 'main' semantics
- From: "Joseph S. Myers" <jsm28 at cam dot ac dot uk>
- Date: Mon, 24 Jul 2000 23:22:54 +0100 (BST)
C99 requires that, in a hosted environment, reaching the end of 'main'
shall return 0. This patch implements this.
I followed the conservative approach of leaving the behaviour
unchanged outside of C99 mode, and if DEFAULT_MAIN_RETURN is defined.
For the former: while it is OK by the old standard to return 0, it
seems to me that users compiling code for the old standard will want
and expect the appropriate diagnostics (if -Wreturn-type), likely
errors if a program with no return value from 'main' is run from a
Makefile, etc.. For the latter: an alternative approach would be to
apply the DEFAULT_MAIN_RETURN only outside of C99 mode, or to
eliminate DEFAULT_MAIN_RETURN altogether. DEFAULT_MAIN_RETURN is only
defined in i370/i370.h and vax/vms.h; the latter conflicts with C99
since 'main' should return 0 rather than 1, and it's the
responsibility of the implementation code calling 'main' to convert 0
into a success status for the environment. I'm not familiar with VMS.
Relevant clauses: 5.1.2.2.3 Program termination, 7.20.4.3 The exit
function.
Special treatment of 'main' in finish_function is disabled by this
patch if freestanding; apart from the returns from 'main', the only
change this makes is to disable the on-by-default warning for non-int
return type (if warn_main is 0: the other warnings about funny types
for main are off, but this one is on by default, but shouldn't be with
-ffreestanding; -ffreestanding already disables the -Wall warnings
about 'main' but not the -Wmain ones; and if -Wmain is given the
warning here will have been given elsewhere and is disabled here
anyway).
I tend to feel that -ffreestanding should disable all special
treatment of library function names - it already enabled -fno-builtin,
but there are also default attributes added to various functions and
special_function_p in calls.c. This might need more attributes for
freestanding users to add manually.
(GCC should define __STDC_HOSTED__ to 1 if hosted, 0 if not. This is
probably the work of a moment for someone who groks specs. I think
that 1 is the appropriate value in hosted mode, even though GCC can't
guarantee the conformance of the whole library.)
This patch has been bootstrapped with no regressions on
i686-pc-linux-gnu.
gcc/ChangeLog:
2000-07-24 Joseph S. Myers <jsm28@cam.ac.uk>
* c-decl.c (finish_function): Don't treat 'main' specially unless
flag_hosted. In C99 mode, return 0 from 'main' unless
DEFAULT_MAIN_RETURN is otherwise defined.
gcc/testsuite/ChangeLog:
2000-07-24 Joseph S. Myers <jsm28@cam.ac.uk>
* gcc.dg/c99-main-1.c: New test.
--- c-decl.c.orig Tue Jul 18 18:34:47 2000
+++ c-decl.c Mon Jul 24 17:34:32 2000
@@ -6571,7 +6571,8 @@
setjmp_protect_args ();
}
- if (! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main"))
+ if (! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main")
+ && flag_hosted)
{
if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
!= integer_type_node)
@@ -6586,6 +6587,9 @@
#ifdef DEFAULT_MAIN_RETURN
/* Make it so that `main' always returns success by default. */
DEFAULT_MAIN_RETURN;
+#else
+ if (flag_isoc99)
+ c_expand_return (integer_zero_node);
#endif
}
}
--- testsuite/gcc.dg/c99-main-1.c.orig Thu Jan 1 00:00:00 1970
+++ testsuite/gcc.dg/c99-main-1.c Mon Jul 24 21:56:18 2000
@@ -0,0 +1,9 @@
+/* Test for implicit return 0 from main in C99. */
+/* Origin: Joseph Myers <jsm28@cam.ac.uk> */
+/* { dg-do run } */
+/* { dg-options "-std=iso9899:1999 -pedantic-errors -Wreturn-type -O -fhosted" } */
+
+int
+main (void)
+{
+} /* { dg-bogus "control reaches end" "missing implicit return" } */
--
Joseph S. Myers
jsm28@cam.ac.uk