Subject: Re: main return...
To: John F. Woods <jfw@FunHouse.com>
From: Alan Barrett <apb@iafrica.com>
List: current-users
Date: 03/22/1996 15:48:36
> > Would anyone *object* to enabling the gcc hack for detecting invalid return
> > types from main, and returning 0 automatically if no return is specified?
> 
> I would object.  Now, if it automatically made demons fly from one's nose
> if it detects an invalid return type, that I could go for.

I found the following code in gcc/common/c-decl.c in a function called
`finish_function', which is apparently executed when the compiler processes
the end of a function:

    #ifdef DEFAULT_MAIN_RETURN
      if (! strcmp (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "main"))
	{
	  if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl)))
	      != integer_type_node)
	    warning_with_decl (fndecl, "return type of `%s' is not `int'");
	  else
	    {
	      /* Make it so that `main' always returns success by default.  */
	      DEFAULT_MAIN_RETURN;
	    }
	}
    #endif

Assuming DEFAULT_MAIN_RETURN is defined as

    #define DEFAULT_MAIN_RETURN  c_expand_return (integer_zero_node)

then this seems to do two things:

  * if main() was declared as returning a type other than int, then warn.
  * if main() was declared as returning int, then pretend that there's a
    return(0) just before we fall off the end of main().

It seems to me that those two things should be controlled separately;
for example, I might not want the default return 0, but nevertheless I
do want a warning if main() is declared incorrectly (actually, I want
demons to fly from the programmer's nose, but I would settle for
having the program print a rude message and die at run time).

Hmm, if the warning were in start_function() rather than in
finish_function(), then it could also insert some code for
MAKE_DEMONS_FLY_FROM_NOSE, to be executed at the start of a main()
function that was declared as returning non-int.

--apb (Alan Barrett)