Handling of main() function for freestanding
Jason Merrill
jason@redhat.com
Fri Oct 7 13:51:31 GMT 2022
On 10/7/22 07:30, Jonathan Wakely wrote:
> On Tue, 4 Oct 2022 at 23:25, Jason Merrill <jason@redhat.com> wrote:
>>
>> On 9/28/22 16:15, Jonathan Wakely wrote:
>>> As part of implementing a C++23 proposal [1] to massively increase the
>>> scope of the freestanding C++ standard library some questions came up
>>> about the special handling of main() that happens for hosted
>>> environments.
>>>
>>> As required by both C++ (all versions) and C (since C99), falling off
>>> the end of the main() function is not undefined, the compiler is
>>> required to insert an implicit 'return 0' [2][3]. However, this
>>> special handling only applies to hosted environments. For freestanding
>>> the return type or even the existence of main is
>>> implementation-defined. As a result, GCC gives a -Wreturn-type warning
>>> for this code with -ffreestanding, but not with -fhosted:
>>>
>>> int main() { }
>>>
>>> Arsen (CC'd) has been working on the libstdc++ changes for the
>>> freestanding proposal, and several thousand libstdc++ tests were
>>> failing when using -ffreestanding, because of the -Wreturn-type
>>> warnings. He wrote a patch to the compiler [4] to add a new
>>> -fspecial-main flag which defaults to on for -fhosted, but can be used
>>> with -ffreestanding to do the implicit 'return 0' (and so disable the
>>> -Wreturn-type warnings) for freestanding as well. This fixes the
>>> libstdc++ test FAILs.
>>>
>>> However, after discussing this briefly with Jason it occurred to us
>>> that if the user declares an 'int main()' function, it's a pretty big
>>> hint that they do want main() to return an int. And so having
>>> undefined behaviour do to a missing return isn't really doing anybody
>>> any favours. If you're compiling for freestanding and you *don't* want
>>> to return a value from main(), then just declare it as void main()
>>> instead. So now we're wondering if we need -fspecial-main at all, or
>>> if int main() and int main(int, char**) should always be "special",
>>> even for freestanding. So Arsen wrote a patch to do that too [5].
>>>
>>> The argument against making 'int main()' imply 'special main' is that
>>> in a freestanding environment, a function called 'int main()' might be
>>> just a normal function, not the program's entry point. And in that
>>> case, maybe you really do want -Wreturn-type warnings. I don't know
>>> how realistic that is.
>>>
>>> So the question is, should Arsen continue with his -fspecial-main
>>> patch, and propose it along with the libstdc++ changes, or should gcc
>>> change to always make 'int main()' "special" even for freestanding?
>>> void main() and long main() and other signatures would still be
>>> allowed for freestanding, and would not have the implicit 'return 0'.
>>
>> I would rather not add a flag. No well-defined freestanding program is
>> affected by implicit return 0 from main, it should always be enabled.
>
> There are some tests that fail if we do that. For whatever reason,
> they're checking the current semantics.
> * gcc.dg/c11-noreturn-4.c: Add -fno-builtin-main to options.
> * gcc.dg/inline-10.c: Likewise.
IMO we still shouldn't emit these pedwarns when freestanding, we
shouldn't require people to add another flag to avoid them.
Adding the implicit return 0 unconditionally doesn't mean we also need
to adopt all the other special treatment of main.
And I guess we shouldn't implicitly return 0 if the function is declared
noreturn.
> * gcc.dg/noreturn-4.c: Likewise.
I'd be inclined to drop this test.
> Arsen implemented Jakub's suggestion which is to add the implicit
> return by default, but add -fno-builtin-main to restore the previous
> behaviour. Is that acceptable? If not, can you and Jakub reach
> consensus so that Arsen knows what to do instead?
> His -fno-builtin-main patch is at
> https://gcc.gnu.org/pipermail/gcc-patches/2022-September/602644.html
> (This is the only one of his patch series not committed, and results
> in 100s of FAILs for libstdc++ when testing with -fffreestanding).
More information about the Gcc
mailing list