From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [IPv6:2001:67c:2050:0:465::101]) by sourceware.org (Postfix) with ESMTPS id 2FB533858C56; Thu, 13 Oct 2022 20:14:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2FB533858C56 Authentication-Results: sourceware.org; dmarc=pass (p=reject dis=none) header.from=aarsen.me Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=aarsen.me Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4MpLN86rPFz9s7x; Thu, 13 Oct 2022 22:14:48 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aarsen.me; s=MBO0001; t=1665692088; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=psz+x6qvOXqfhZJddeDUd/rZ+FYzbH+vNMXqkDC+8OA=; b=gKbcJ+WRbs/KIhOy4QyLOHmsk5J+xLXShFPcLYNXo5gW1Y6imZkDiMsU25RErnbFu1lhxg rQi77ZRixChRVewUv8F8nNQ8JjhZaoQUzUEe2KO0Xg1rBZg2O1tN8Qhp7MDim1qrmNbbWX iQpPyLqgd0uffYh2NS6zHqgqdcogfEZw8csGO8aOmjJjceol4TY1UmSdJCfJ3xdGgWozYy qLjb4Nf9KSsqkOHWUW4cnh/zywhDT+xePsduXsMZA0oLZfxXPAupRX40mFxtgaRf2OSYtl gdcpJCkw4kPqAOdVjmyMgYdjbp5OYjpjrcg9GPusuqXV4StL4Nv9uPPo5YHkrQ== From: Arsen =?utf-8?B?QXJzZW5vdmnEhw==?= To: Jonathan Wakely , gcc-patches@gcc.gnu.org Cc: "gcc@gcc.gnu.org" , Jakub Jelinek , "Joseph S. Myers" , Jason Merrill Subject: Re: Handling of main() function for freestanding Date: Thu, 13 Oct 2022 22:14:46 +0200 Message-ID: <4964626.imyG0pze7u@bstg> In-Reply-To: <2a959ba5-e9a3-768b-2d66-cba2bc389eca@redhat.com> References: <16718001.dR7co8bJll@bstg> <2a959ba5-e9a3-768b-2d66-cba2bc389eca@redhat.com> MIME-Version: 1.0 Content-Type: multipart/signed; boundary="nextPart6098651.B9SeqL4A8m"; micalg="pgp-sha512"; protocol="application/pgp-signature" X-Rspamd-Queue-Id: 4MpLN86rPFz9s7x X-Spam-Status: No, score=-11.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_INFOUSMEBIZ,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --nextPart6098651.B9SeqL4A8m Content-Type: multipart/mixed; boundary="nextPart3671470.KnvtM3xH8j"; protected-headers="v1" Content-Transfer-Encoding: 7Bit From: Arsen =?utf-8?B?QXJzZW5vdmnEhw==?= To: Jonathan Wakely , gcc-patches@gcc.gnu.org Subject: Re: Handling of main() function for freestanding Date: Thu, 13 Oct 2022 22:14:17 +0200 Message-ID: <4964626.imyG0pze7u@bstg> In-Reply-To: <2a959ba5-e9a3-768b-2d66-cba2bc389eca@redhat.com> MIME-Version: 1.0 This is a multi-part message in MIME format. --nextPart3671470.KnvtM3xH8j Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" On Thursday, 13 October 2022 19:24:41 CEST Jason Merrill wrote: > I was arguing that we don't need the new flag; there shouldn't be any > need to turn it off. At the time, I opted to go with a more conservative route; I haven't=20 been around enough to have very strong opinions ;) I certainly can't=20 think of a way always adding a return can go wrong, but figured someone,=20 somehow, might rely on this behavior. Removed the flag and tested on=20 x86_64-pc-linux-gnu, v3 attached. =46WIW, there's precedent for treating main specially regardless of=20 flag_hosted (e.g. it's always marked extern "C" in the C++ frontend,=20 AFAICT). =2D-=20 Arsen Arsenovi=C4=87 --nextPart3671470.KnvtM3xH8j Content-Disposition: attachment; filename="v3-0001-c-family-Implicitly-return-zero-from-main-even-on.patch" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="utf-8"; name="v3-0001-c-family-Implicitly-return-zero-from-main-even-on.patch" >From e60be6bb45fdba8085bde5d1883deeae640e786b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arsen=20Arsenovi=C4=87?= Date: Thu, 13 Oct 2022 21:46:30 +0200 Subject: [PATCH v3] 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. gcc/cp/ChangeLog: * cp-tree.h (DECL_MAIN_FREESTANDING_P): Move most DECL_MAIN_P logic here, so that we can use it when not hosted. (DECL_MAIN_P): Implement in terms of DECL_MAIN_FREESTANDING_P. * decl.cc (finish_function): Use DECL_MAIN_FREESTANDING_P instead of DECL_MAIN_P, to lose the hosted requirement, but check noreturn. gcc/testsuite/ChangeLog: * g++.dg/freestanding-main.C: New test. * gcc.dg/freestanding-main.c: New test. --- gcc/c/c-decl.cc | 2 +- gcc/c/c-objc-common.cc | 5 ++--- gcc/cp/cp-tree.h | 8 +++++--- gcc/cp/decl.cc | 3 ++- gcc/testsuite/g++.dg/freestanding-main.C | 5 +++++ gcc/testsuite/gcc.dg/freestanding-main.c | 5 +++++ 6 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.dg/freestanding-main.C create mode 100644 gcc/testsuite/gcc.dg/freestanding-main.c diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index 193e268f04e..8c655590558 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -10442,7 +10442,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 70e10a98e33..2933414fd45 100644 --- a/gcc/c/c-objc-common.cc +++ b/gcc/c/c-objc-common.cc @@ -37,9 +37,8 @@ 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. */ + return MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl)); } /* Called from check_global_declaration. */ diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 3b67be651b9..4c7adfbffd8 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -772,11 +772,13 @@ typedef struct ptrmem_cst * ptrmem_cst_t; /* Returns nonzero iff NODE is a declaration for the global function `main'. */ -#define DECL_MAIN_P(NODE) \ +#define DECL_MAIN_FREESTANDING_P(NODE) \ (DECL_EXTERN_C_FUNCTION_P (NODE) \ && DECL_NAME (NODE) != NULL_TREE \ - && MAIN_NAME_P (DECL_NAME (NODE)) \ - && flag_hosted) + && MAIN_NAME_P (DECL_NAME (NODE))) + +/* Nonzero iff NODE is a declaration for `main', and we are hosted. */ +#define DECL_MAIN_P(NODE) (DECL_MAIN_FREESTANDING_P(NODE) && flag_hosted) /* Lookup walker marking. */ #define LOOKUP_SEEN_P(NODE) TREE_VISITED (NODE) diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 82eb0c2f22a..cfc8cd5afd7 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -17854,7 +17854,8 @@ finish_function (bool inline_p) if (!DECL_CLONED_FUNCTION_P (fndecl)) { /* Make it so that `main' always returns 0 by default. */ - if (DECL_MAIN_P (current_function_decl)) + if (DECL_MAIN_FREESTANDING_P (current_function_decl) + && !TREE_THIS_VOLATILE (current_function_decl)) finish_return_stmt (integer_zero_node); if (use_eh_spec_block (current_function_decl)) diff --git a/gcc/testsuite/g++.dg/freestanding-main.C b/gcc/testsuite/g++.dg/freestanding-main.C new file mode 100644 index 00000000000..3718cc4508e --- /dev/null +++ b/gcc/testsuite/g++.dg/freestanding-main.C @@ -0,0 +1,5 @@ +/* Make sure we don't get a missing return warning on freestanding. */ +/* { dg-do compile } */ +/* { dg-options "-ffreestanding -Wreturn-type" } */ + +int main() {} diff --git a/gcc/testsuite/gcc.dg/freestanding-main.c b/gcc/testsuite/gcc.dg/freestanding-main.c new file mode 100644 index 00000000000..3718cc4508e --- /dev/null +++ b/gcc/testsuite/gcc.dg/freestanding-main.c @@ -0,0 +1,5 @@ +/* Make sure we don't get a missing return warning on freestanding. */ +/* { dg-do compile } */ +/* { dg-options "-ffreestanding -Wreturn-type" } */ + +int main() {} -- 2.38.0 --nextPart3671470.KnvtM3xH8j-- --nextPart6098651.B9SeqL4A8m Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part. Content-Transfer-Encoding: 7Bit -----BEGIN PGP SIGNATURE----- iNUEABYKAH0WIQT+4rPRE/wAoxYtYGFSwpQwHqLEkwUCY0hxmV8UgAAAAAAuAChp c3N1ZXItZnByQG5vdGF0aW9ucy5vcGVucGdwLmZpZnRoaG9yc2VtYW4ubmV0RkVF MkIzRDExM0ZDMDBBMzE2MkQ2MDYxNTJDMjk0MzAxRUEyQzQ5MwAKCRBSwpQwHqLE k1+7AQCNKxWjxl3ArHk6v1UD14zU9KIwQPgZ6FOa8fk8knzyIQD/dfYPzTN/8OA1 TuLkzVdKPh/9mgzxn8IYo47lCm4crAg= =WMwr -----END PGP SIGNATURE----- --nextPart6098651.B9SeqL4A8m--