c-family: Implicitly return zero from main even on freestanding
commitf44575cb88727193386428e9ced3439e4f98f493
authorArsen Arsenović <arsen@aarsen.me>
Fri, 14 Oct 2022 10:04:51 +0000 (14 12:04 +0200)
committerJason Merrill <jason@redhat.com>
Mon, 24 Oct 2022 13:37:33 +0000 (24 09:37 -0400)
tree676300379ab6560f5333503d820489ecab859ef0
parent5bcd92d0d4029f3d1d2eacc0e2bff1685545b74f
c-family: Implicitly return zero from main even on freestanding

... unless marked noreturn.

This should not get in anyone's way, but should permit the use of main()
in freestanding more easily, especially for writing test cases that
should work both in freestanding and hosted modes.

gcc/c/ChangeLog:

* c-decl.cc (finish_function): Ignore hosted when deciding
whether to implicitly return zero, but check noreturn.
* c-objc-common.cc (c_missing_noreturn_ok_p): Loosen the
requirements to just MAIN_NAME_P when hosted, or `int main'
otherwise.

gcc/cp/ChangeLog:

* cp-tree.h (DECL_MAIN_P): Move most logic, besides the hosted
check, from here...
(DECL_MAIN_ANY_P): ... to here, so that it can be reused ...
(DECL_MAIN_FREESTANDING_P): ... here, with an additional
constraint on (hosted OR return type == int)
* decl.cc (finish_function): Use DECL_MAIN_FREESTANDING_P
instead of DECL_MAIN_P, to loosen the hosted requirement, but
check noreturn, before adding implicit returns.

gcc/testsuite/ChangeLog:

* gcc.dg/noreturn-4.c: Removed.
* g++.dg/freestanding-main.C: New test.
* g++.dg/freestanding-nonint-main.C: New test.
* gcc.dg/freestanding-main.c: New test.
* gcc.dg/freestanding-nonint-main.c: New test.
gcc/c/c-decl.cc
gcc/c/c-objc-common.cc
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/testsuite/g++.dg/freestanding-main.C [new file with mode: 0644]
gcc/testsuite/g++.dg/freestanding-nonint-main.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/freestanding-main.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/freestanding-nonint-main.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/noreturn-4.c [deleted file]