public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Arsen Arsenović" <arsen@aarsen.me>
To: Jonathan Wakely <jwakely.gcc@gmail.com>, gcc-patches@gcc.gnu.org
Cc: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>,
	Jakub Jelinek <jakub@redhat.com>,
	"Joseph S. Myers" <joseph@codesourcery.com>,
	Jason Merrill <jason@redhat.com>
Subject: Re: Handling of main() function for freestanding
Date: Fri, 14 Oct 2022 12:04:51 +0200	[thread overview]
Message-ID: <1860348.8SKhNGa0Zi@bstg> (raw)
In-Reply-To: <5b636835-20af-2372-75d2-92e31a3393ed@redhat.com>


[-- Attachment #1.1: Type: text/plain, Size: 324 bytes --]

On Thursday, 13 October 2022 23:16:02 CEST Jason Merrill wrote:
> I liked in the previous version that you checked the return type of
> main when !flag_hosted, here and in c_missing_noreturn_ok_p.  Let's
> bring that back.
Ah, right; I forgot about that.  What do you think of this?  

Thanks,
-- 
Arsen Arsenović

[-- Attachment #1.2: v4-0001-c-family-Implicitly-return-zero-from-main-even-on.patch --]
[-- Type: text/x-patch, Size: 7528 bytes --]

From dfc1677d1dcfa1b3b963fca8ea5eb8878c4e63d0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Arsen=20Arsenovi=C4=87?= <arsen@aarsen.me>
Date: Thu, 13 Oct 2022 21:46:30 +0200
Subject: [PATCH v4] 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 ++++++---
 gcc/cp/cp-tree.h                                | 15 ++++++++++++---
 gcc/cp/decl.cc                                  |  3 ++-
 gcc/testsuite/g++.dg/freestanding-main.C        |  5 +++++
 gcc/testsuite/g++.dg/freestanding-nonint-main.C |  5 +++++
 gcc/testsuite/gcc.dg/freestanding-main.c        |  5 +++++
 gcc/testsuite/gcc.dg/freestanding-nonint-main.c |  5 +++++
 gcc/testsuite/gcc.dg/noreturn-4.c               | 10 ----------
 9 files changed, 41 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/freestanding-main.C
 create mode 100644 gcc/testsuite/g++.dg/freestanding-nonint-main.C
 create mode 100644 gcc/testsuite/gcc.dg/freestanding-main.c
 create mode 100644 gcc/testsuite/gcc.dg/freestanding-nonint-main.c
 delete mode 100644 gcc/testsuite/gcc.dg/noreturn-4.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..b4680912547 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.  */
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 3b67be651b9..33529fa8093 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -772,11 +772,20 @@ 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_ANY_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 `int main', or we are hosted. */
+#define DECL_MAIN_FREESTANDING_P(NODE)				\
+  (DECL_MAIN_ANY_P(NODE)					\
+   && (flag_hosted						\
+       || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (NODE)))	\
+          == integer_type_node))
+
+/* Nonzero iff NODE is a declaration for `main', and we are hosted. */
+#define DECL_MAIN_P(NODE) (DECL_MAIN_ANY_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/g++.dg/freestanding-nonint-main.C b/gcc/testsuite/g++.dg/freestanding-nonint-main.C
new file mode 100644
index 00000000000..a8571cc6f0b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/freestanding-nonint-main.C
@@ -0,0 +1,5 @@
+/* Check that we get the right warning for nonint main in freestanding. */
+/* { dg-do compile } */
+/* { dg-options "-ffreestanding -Wreturn-type" } */
+
+const char *main() {} /* { dg-warning "-Wreturn-type" } */
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() {}
diff --git a/gcc/testsuite/gcc.dg/freestanding-nonint-main.c b/gcc/testsuite/gcc.dg/freestanding-nonint-main.c
new file mode 100644
index 00000000000..d8393346b09
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/freestanding-nonint-main.c
@@ -0,0 +1,5 @@
+/* Check that we get the right warning for nonint main in freestanding. */
+/* { dg-do compile } */
+/* { dg-options "-ffreestanding -Wreturn-type" } */
+
+const char *main(void) {} /* { dg-warning "-Wreturn-type" } */
diff --git a/gcc/testsuite/gcc.dg/noreturn-4.c b/gcc/testsuite/gcc.dg/noreturn-4.c
deleted file mode 100644
index 6fe144754d0..00000000000
--- a/gcc/testsuite/gcc.dg/noreturn-4.c
+++ /dev/null
@@ -1,10 +0,0 @@
-/* Check for "noreturn" warning in main. */
-/* { dg-do compile } */
-/* { dg-options "-O2 -Wmissing-noreturn -ffreestanding" } */
-extern void exit (int) __attribute__ ((__noreturn__));
-
-int
-main (void) /* { dg-warning "function might be candidate for attribute 'noreturn'" "warn for main" } */
-{
-  exit (0);
-}
-- 
2.38.0


[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 358 bytes --]

  reply	other threads:[~2022-10-14 10:04 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-28 20:15 Jonathan Wakely
2022-09-29  6:00 ` Richard Biener
2022-09-29  7:12   ` Jakub Jelinek
2022-09-29  9:21     ` Jonathan Wakely
2022-10-04 22:25 ` Jason Merrill
2022-10-04 23:28   ` Joel Sherrill
2022-10-07 11:30   ` Jonathan Wakely
2022-10-07 13:51     ` Jason Merrill
2022-10-07 13:53       ` Jakub Jelinek
2022-10-13 17:03       ` Arsen Arsenović
2022-10-13 17:10         ` Jakub Jelinek
2022-10-13 17:26           ` Arsen Arsenović
2022-10-13 17:24         ` Jason Merrill
2022-10-13 20:14           ` Arsen Arsenović
2022-10-13 21:16             ` Jason Merrill
2022-10-14 10:04               ` Arsen Arsenović [this message]
2022-10-14 15:17                 ` Jason Merrill
2022-10-21 10:33                 ` Ping (c,c++): " Arsen Arsenović
2022-10-21 21:02                   ` Joseph Myers
2022-10-23 11:54                     ` Arsen Arsenović
2022-10-24 13:46                       ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1860348.8SKhNGa0Zi@bstg \
    --to=arsen@aarsen.me \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=jason@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=jwakely.gcc@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).