>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ć <ar...@aarsen.me>
---
 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 %<main%>");
@@ -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 <j...@polyomino.org.uk> */
 /* { 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

Reply via email to