From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by sourceware.org (Postfix) with ESMTPS id F29A0385382A; Fri, 30 Sep 2022 16:46:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org F29A0385382A 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 smtp2.mailbox.org (smtp2.mailbox.org [IPv6:2001:67c:2050:b231:465::2]) (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 4MfGMl021Hz9sSy; Fri, 30 Sep 2022 18:46:27 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aarsen.me; s=MBO0001; t=1664556387; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KiJ36AfK+Pa86dDHhTnUYB8ZeWNCYSk7XVja8xO/5QQ=; b=cb75pVwpVhj7j9QVMUdu0hx2xZkmjUch8n9Sb7uvSRfeTfLhZYeee8iMKvTxg6Bvr8OZ3E RBkqxQ3QRI8HYvyJa2hgQoX92GZFhnfH1GPEAo88Jx7uRwmGDT26XSriiTz4gex9CeK80+ +wgIultOC2cVeatAJ6WDXZjBcCihEzHGaXBHe7BDA9dVB+IsjM0sZZ3B1u2tu24r7Mz5eW kyYwryy5RcSPgQNFtXOsMnXeGSheD4/1H2/tD8QJ/IfAliAugEj8mDpIqWms4XFWd/UtiD Skb8yl8fqS5UxfTu9UrMRnSjiopozFAwpwDdESrSnhIzFC81sdgBMGPLXjnQpA== From: =?UTF-8?q?Arsen=20Arsenovi=C4=87?= To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, =?UTF-8?q?Arsen=20Arsenovi=C4=87?= Subject: [PATCH 05/10] c-family: Implement new `int main' semantics in freestanding Date: Fri, 30 Sep 2022 18:45:51 +0200 Message-Id: <20220930164556.1198044-6-arsen@aarsen.me> In-Reply-To: <20220930164556.1198044-1-arsen@aarsen.me> References: <20220930164556.1198044-1-arsen@aarsen.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4MfGMl021Hz9sSy X-Spam-Status: No, score=-10.6 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: >From now, by default, (specifically) `int main' in freestanding will implicitly return 0, as it does for hosted modes. The old behaviour is still accessible via -fno-builtin-main. gcc/c-family/ChangeLog: * c-common.cc (disable_builtin_function): Support special value `main' that, in freestanding, allows disabling special casing placed around `main'. * c-common.h: Add flag_builtin_main. (want_builtin_main_p): New function, true iff hosted OR builtin_main are set. gcc/c/ChangeLog: * c-decl.cc (grokdeclarator): Consider flag_builtin_main when deciding whether to emit warnings. (finish_function): Consider flag_builtin_main when deciding whether to emit an implicit zero return. * c-objc-common.cc (c_missing_noreturn_ok_p): Consider missing noreturn okay only when hosted or when builtin_main is enabled. gcc/cp/ChangeLog: * cp-tree.h (DECL_MAIN_P): Consider flag_builtin_main when deciding whether this function is to be the special function main. gcc/ChangeLog: * doc/invoke.texi: Document -fno-builtin-main. gcc/testsuite/ChangeLog: * gcc.dg/c11-noreturn-4.c: Add -fno-builtin-main to options. * gcc.dg/inline-10.c: Likewise. * gcc.dg/noreturn-4.c: Likewise. * g++.dg/freestanding-main-implicitly-returns.C: New test. * g++.dg/no-builtin-main.C: New test. * gcc.dg/freestanding-main-implicitly-returns.c: New test. * gcc.dg/no-builtin-main.c: New test. Signed-off-by: Arsen Arsenović --- gcc/c-family/c-common.cc | 6 ++++++ gcc/c-family/c-common.h | 10 ++++++++++ gcc/c/c-decl.cc | 4 ++-- gcc/c/c-objc-common.cc | 9 ++++++--- gcc/cp/cp-tree.h | 12 ++++++----- gcc/doc/invoke.texi | 20 ++++++++++++++----- .../freestanding-main-implicitly-returns.C | 5 +++++ gcc/testsuite/g++.dg/no-builtin-main.C | 5 +++++ gcc/testsuite/gcc.dg/c11-noreturn-4.c | 2 +- .../freestanding-main-implicitly-returns.c | 5 +++++ gcc/testsuite/gcc.dg/inline-10.c | 2 +- gcc/testsuite/gcc.dg/no-builtin-main.c | 5 +++++ gcc/testsuite/gcc.dg/noreturn-4.c | 2 +- 13 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 gcc/testsuite/g++.dg/freestanding-main-implicitly-returns.C create mode 100644 gcc/testsuite/g++.dg/no-builtin-main.C create mode 100644 gcc/testsuite/gcc.dg/freestanding-main-implicitly-returns.c create mode 100644 gcc/testsuite/gcc.dg/no-builtin-main.c diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index 3c60a89bfe2..117db8a1928 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -232,6 +232,10 @@ int flag_isoc2x; int flag_hosted = 1; +/* Nonzero means that we want to give main its special meaning */ + +int flag_builtin_main = 1; + /* ObjC language option variables. */ @@ -4878,6 +4882,8 @@ disable_builtin_function (const char *name) { if (startswith (name, "__builtin_")) error ("cannot disable built-in function %qs", name); + else if (strcmp("main", name) == 0) + flag_builtin_main = 0; else { disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin); diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index 5f470d94f4a..cbed5f0f9e8 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -687,6 +687,16 @@ extern int flag_isoc2x; extern int flag_hosted; +/* Nonzero means that we want to give main its special meaning */ + +extern int flag_builtin_main; + +/* Returns false if both flag_hosted and flag_builtin_main are zero, true + otherwise. */ +inline bool builtin_main_p() { + return flag_hosted || flag_builtin_main; +} + /* ObjC language option variables. */ diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index bac8e6cc3f6..5060b3f2a41 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -7695,7 +7695,7 @@ grokdeclarator (const struct c_declarator *declarator, /* Record presence of `inline' and `_Noreturn', if it is reasonable. */ - if (flag_hosted && MAIN_NAME_P (declarator->u.id.id)) + if (builtin_main_p() && MAIN_NAME_P (declarator->u.id.id)) { if (declspecs->inline_p) pedwarn (loc, 0, "cannot inline function %"); @@ -10314,7 +10314,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)) && builtin_main_p() && 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..e229580b182 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)); + /* Missing a return is only okay when flag_builtin_main is enabled, and we + are `int main'. */ + if (!MAIN_NAME_P (DECL_ASSEMBLER_NAME (decl))) + return false; + return builtin_main_p() + && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (decl))) == integer_type_node; } /* Called from check_global_declaration. */ diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 67aea9653e3..50751ab74b6 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -771,11 +771,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) \ - (DECL_EXTERN_C_FUNCTION_P (NODE) \ - && DECL_NAME (NODE) != NULL_TREE \ - && MAIN_NAME_P (DECL_NAME (NODE)) \ - && flag_hosted) +#define DECL_MAIN_P(NODE) \ + (DECL_EXTERN_C_FUNCTION_P (NODE) \ + && DECL_NAME (NODE) != NULL_TREE \ + && MAIN_NAME_P (DECL_NAME (NODE)) \ + && (flag_hosted || (flag_builtin_main \ + && TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (NODE))) \ + == integer_type_node))) /* Lookup walker marking. */ #define LOOKUP_SEEN_P(NODE) TREE_VISITED (NODE) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index a5dc6377835..79f144ec819 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -2537,8 +2537,10 @@ this switch only affects the @code{asm} and @code{typeof} keywords, since @code{inline} is a standard keyword in ISO C99. @item -fno-builtin +@itemx -fno-builtin-main @itemx -fno-builtin-@var{function} @opindex fno-builtin +@opindex fno-builtin-main @opindex fbuiltin @cindex built-in functions Don't recognize built-in functions that do not begin with @@ -2577,6 +2579,11 @@ built-in functions selectively when using @option{-fno-builtin} or #define strcpy(d, s) __builtin_strcpy ((d), (s)) @end smallexample +The special form @option{-fno-builtin-main} permits disabling special +handling of the @code{main} function, such as the implicit zero +return. This option has no effect in hosted mode (as hosted takes +precedence over it) and is not implied by @option{-fno-builtin}. + @item -fcond-mismatch @opindex fcond-mismatch Allow conditional expressions with mismatched types in the second and @@ -2585,13 +2592,16 @@ is not supported for C++. @item -ffreestanding @opindex ffreestanding +@opindex fno-builtin-main @cindex hosted environment Assert that compilation targets a freestanding environment. This -implies @option{-fno-builtin}. A freestanding environment -is one in which the standard library may not exist, and program startup may -not necessarily be at @code{main}. The most obvious example is an OS kernel. -This is equivalent to @option{-fno-hosted}. +implies @option{-fno-builtin}. A freestanding environment is one in +which the standard library may not exist, and program startup may not +necessarily be at @code{main}. The most obvious example is an OS +kernel. Note that this option does not imply +@option{-fno-builtin-main}. @option{-ffreestanding} is equivalent to +@option{-fno-hosted}. @xref{Standards,,Language Standards Supported by GCC}, for details of freestanding and hosted environments. @@ -5828,7 +5838,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}. -Wimplicit-function-declaration @r{(C and Objective-C only)} @gol -Winit-self @r{(only for C++)} @gol -Wlogical-not-parentheses @gol --Wmain @r{(only for C/ObjC and unless} @option{-ffreestanding}@r{)} @gol +-Wmain @r{(only for C/ObjC and unless} @option{-fno-builtin-main}@r{)} @gol -Wmaybe-uninitialized @gol -Wmemset-elt-size @gol -Wmemset-transposed-args @gol diff --git a/gcc/testsuite/g++.dg/freestanding-main-implicitly-returns.C b/gcc/testsuite/g++.dg/freestanding-main-implicitly-returns.C new file mode 100644 index 00000000000..068ddd4d5ad --- /dev/null +++ b/gcc/testsuite/g++.dg/freestanding-main-implicitly-returns.C @@ -0,0 +1,5 @@ +/* Make sure main is implicitly returned from, even in freestanding. */ +/* { dg-do compile } */ +/* { dg-options "-Wreturn-type -ffreestanding" } */ + +int main() {} diff --git a/gcc/testsuite/g++.dg/no-builtin-main.C b/gcc/testsuite/g++.dg/no-builtin-main.C new file mode 100644 index 00000000000..7431784d018 --- /dev/null +++ b/gcc/testsuite/g++.dg/no-builtin-main.C @@ -0,0 +1,5 @@ +/* Make sure we get warned about missing return with -fno-builtin-main. */ +/* { dg-do compile } */ +/* { dg-options "-Wreturn-type -ffreestanding -fno-builtin-main" } */ + +int main() {} /* { dg-warning "-Wreturn-type" } */ diff --git a/gcc/testsuite/gcc.dg/c11-noreturn-4.c b/gcc/testsuite/gcc.dg/c11-noreturn-4.c index a92a1140f67..16fd8f1b0be 100644 --- a/gcc/testsuite/gcc.dg/c11-noreturn-4.c +++ b/gcc/testsuite/gcc.dg/c11-noreturn-4.c @@ -1,6 +1,6 @@ /* Test C11 _Noreturn. Test _Noreturn on main, freestanding. */ /* { dg-do compile } */ -/* { dg-options "-std=c11 -pedantic-errors -ffreestanding" } */ +/* { dg-options "-std=c11 -pedantic-errors -ffreestanding -fno-builtin-main" } */ _Noreturn void exit (int); diff --git a/gcc/testsuite/gcc.dg/freestanding-main-implicitly-returns.c b/gcc/testsuite/gcc.dg/freestanding-main-implicitly-returns.c new file mode 100644 index 00000000000..068ddd4d5ad --- /dev/null +++ b/gcc/testsuite/gcc.dg/freestanding-main-implicitly-returns.c @@ -0,0 +1,5 @@ +/* Make sure main is implicitly returned from, even in freestanding. */ +/* { dg-do compile } */ +/* { dg-options "-Wreturn-type -ffreestanding" } */ + +int main() {} diff --git a/gcc/testsuite/gcc.dg/inline-10.c b/gcc/testsuite/gcc.dg/inline-10.c index f7a7592a6a9..bb59f5d81a7 100644 --- a/gcc/testsuite/gcc.dg/inline-10.c +++ b/gcc/testsuite/gcc.dg/inline-10.c @@ -1,6 +1,6 @@ /* Test inline main, gnu99 mode, freestanding, -pedantic-errors. */ /* Origin: Joseph Myers */ /* { dg-do compile } */ -/* { dg-options "-std=gnu99 -ffreestanding -pedantic-errors" } */ +/* { dg-options "-std=gnu99 -fno-builtin-main -ffreestanding -pedantic-errors" } */ inline int main (void) { return 1; } diff --git a/gcc/testsuite/gcc.dg/no-builtin-main.c b/gcc/testsuite/gcc.dg/no-builtin-main.c new file mode 100644 index 00000000000..7431784d018 --- /dev/null +++ b/gcc/testsuite/gcc.dg/no-builtin-main.c @@ -0,0 +1,5 @@ +/* Make sure we get warned about missing return with -fno-builtin-main. */ +/* { dg-do compile } */ +/* { dg-options "-Wreturn-type -ffreestanding -fno-builtin-main" } */ + +int main() {} /* { dg-warning "-Wreturn-type" } */ diff --git a/gcc/testsuite/gcc.dg/noreturn-4.c b/gcc/testsuite/gcc.dg/noreturn-4.c index 6fe144754d0..c1bdc76b16f 100644 --- a/gcc/testsuite/gcc.dg/noreturn-4.c +++ b/gcc/testsuite/gcc.dg/noreturn-4.c @@ -1,6 +1,6 @@ /* Check for "noreturn" warning in main. */ /* { dg-do compile } */ -/* { dg-options "-O2 -Wmissing-noreturn -ffreestanding" } */ +/* { dg-options "-O2 -Wmissing-noreturn -ffreestanding -fno-builtin-main" } */ extern void exit (int) __attribute__ ((__noreturn__)); int -- 2.37.3