From f44575cb88727193386428e9ced3439e4f98f493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arsen=20Arsenovi=C4=87?= Date: Fri, 14 Oct 2022 12:04:51 +0200 Subject: 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 | 2 +- gcc/c/c-objc-common.cc | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'gcc/c') diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 80f6e91..4746e31 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -10542,7 +10542,7 @@ finish_function (location_t end_loc) if (DECL_RESULT (fndecl) && DECL_RESULT (fndecl) != error_mark_node) DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; - if (MAIN_NAME_P (DECL_NAME (fndecl)) && flag_hosted + if (MAIN_NAME_P (DECL_NAME (fndecl)) && !TREE_THIS_VOLATILE (fndecl) && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (fndecl))) == integer_type_node && flag_isoc99) { diff --git a/gcc/c/c-objc-common.cc b/gcc/c/c-objc-common.cc index 70e10a9..b468091 100644 --- a/gcc/c/c-objc-common.cc +++ b/gcc/c/c-objc-common.cc @@ -37,9 +37,12 @@ static bool c_tree_printer (pretty_printer *, text_info *, const char *, bool c_missing_noreturn_ok_p (tree decl) { - /* A missing noreturn is not ok for freestanding implementations and - ok for the `main' function in hosted implementations. */ - return flag_hosted && MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl)); + /* A missing noreturn is ok for the `main' function. */ + if (!MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl))) + return false; + + return flag_hosted + || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl))) == integer_type_node; } /* Called from check_global_declaration. */ -- cgit v1.1