]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Strengthen checks on 'main'
authorNathaniel Shead <nathanieloshead@gmail.com>
Sat, 11 May 2024 12:25:44 +0000 (22:25 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Wed, 22 May 2024 00:30:41 +0000 (10:30 +1000)
This patch adds some missing requirements for legal main declarations,
as according to [basic.start.main] p2.

gcc/cp/ChangeLog:

* decl.cc (grokfndecl): Check for main functions with language
linkage or module attachment.
(grokvardecl): Check for extern 'C' entities named main.

gcc/testsuite/ChangeLog:

* g++.dg/abi/main.C: Check pedwarn for main with linkage-spec.
* g++.dg/modules/contracts-1_b.C: Don't declare main in named
module.
* g++.dg/modules/contracts-3_b.C: Likewise.
* g++.dg/modules/contracts-4_d.C: Likewise.
* g++.dg/modules/horcrux-1_a.C: Export declarations, so that...
* g++.dg/modules/horcrux-1_b.C: Don't declare main in named
module.
* g++.dg/modules/main-1.C: New test.
* g++.dg/parse/linkage5.C: New test.
* g++.dg/parse/linkage6.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/decl.cc
gcc/testsuite/g++.dg/abi/main.C
gcc/testsuite/g++.dg/modules/contracts-1_b.C
gcc/testsuite/g++.dg/modules/contracts-3_b.C
gcc/testsuite/g++.dg/modules/contracts-4_d.C
gcc/testsuite/g++.dg/modules/horcrux-1_a.C
gcc/testsuite/g++.dg/modules/horcrux-1_b.C
gcc/testsuite/g++.dg/modules/main-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/parse/linkage5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/parse/linkage6.C [new file with mode: 0644]

index 6fcab615d55e6995842dad9bbc978d063971c437..a992d54dc8f0a14ce2b8c8b3b07b93e3a6076f19 100644 (file)
@@ -10788,6 +10788,11 @@ grokfndecl (tree ctype,
                  "cannot declare %<::main%> to be %qs", "consteval");
       if (!publicp)
        error_at (location, "cannot declare %<::main%> to be static");
+      if (current_lang_depth () != 0)
+       pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
+                " linkage specification");
+      if (module_attach_p ())
+       error_at (location, "cannot attach %<::main%> to a named module");
       inlinep = 0;
       publicp = 1;
     }
@@ -11287,10 +11292,16 @@ grokvardecl (tree type,
     DECL_INTERFACE_KNOWN (decl) = 1;
 
   if (DECL_NAME (decl)
-      && MAIN_NAME_P (DECL_NAME (decl))
-      && scope == global_namespace)
-    error_at (DECL_SOURCE_LOCATION (decl),
-             "cannot declare %<::main%> to be a global variable");
+      && MAIN_NAME_P (DECL_NAME (decl)))
+    {
+      if (scope == global_namespace)
+       error_at (DECL_SOURCE_LOCATION (decl),
+                 "cannot declare %<::main%> to be a global variable");
+      else if (DECL_EXTERN_C_P (decl))
+       error_at (DECL_SOURCE_LOCATION (decl),
+                 "an entity named %<main%> cannot be declared with "
+                 "C language linkage");
+    }
 
   /* Check that the variable can be safely declared as a concept.
      Note that this also forbids explicit specializations.  */
index 4c5f1ea213c2de58e9ea21c8f32099cf2589ccf0..2797a16df5b55b1b835517fddcc67389e1dbf5b4 100644 (file)
@@ -1,10 +1,11 @@
 /* { dg-do compile } */
+/* { dg-additional-options "-Wno-error=pedantic" }
 
 /* Check if entry points get implicit C linkage. If they don't, compiler will
  * error on incompatible declarations */
 
 int main();
-extern "C" int main();
+extern "C" int main();  // { dg-warning "linkage specification" }
 
 #ifdef __MINGW32__
 
index 30c15f6928b54cd6055e97cb456e63898d816582..aa36c8d6b1b68b1275088663fd5ad1856a07b4e3 100644 (file)
@@ -1,15 +1,11 @@
 // { dg-module-do run }
 // { dg-additional-options "-fmodules-ts -fcontracts -fcontract-continuation-mode=on" }
-module;
 #include <cstdio>
-export module bar;
-// { dg-module-cmi bar }
 import foo;
 
 template<typename T>
 bool bar_fn_pre(T n) { printf("bar fn pre(%d)\n", n); return true; }
 
-export
 template<typename T>
 T bar_fn(T n)
   [[ pre: bar_fn_pre(n) && n > 0 ]]
index b1d6375391b6b94acbf60f0f6628ddb42ba1b6b7..1a29e2c3e5d64ff308352f5a6c3b2e432337a77c 100644 (file)
@@ -1,15 +1,11 @@
 // { dg-module-do run }
 // { dg-additional-options "-fmodules-ts -fcontracts -fcontract-role=default:ignore,ignore,ignore" }
-module;
 #include <cstdio>
-export module bar;
-// { dg-module-cmi bar }
 import foo;
 
 template<typename T>
 bool bar_fn_pre(T n) { printf("bar fn pre(%d)\n", n); return true; }
 
-export
 template<typename T>
 T bar_fn(T n)
   [[ pre: bar_fn_pre(n) && n > 0 ]]
index dc56251d1d889c7f9c944e5a3e6f5223a84c53e0..9e6b7c3d4de5dc83e2471658e88f07e79f55a525 100644 (file)
@@ -1,8 +1,6 @@
 // { dg-module-do run }
 // { dg-additional-options "-fmodules-ts -fcontracts" }
-module;
 #include <cstdio>
-export module baz;
 import foo;
 import bar;
 
index ff548d039eac4d2fefccd42cd47525020883b2af..c115bd2965b037e3d90093e86a6c4636aecfc923 100644 (file)
@@ -3,13 +3,16 @@
 export module foo;
 // { dg-module-cmi foo }
 
+export
 template<typename _Tp, _Tp __v>
 struct integral_constant
 {};
 
+export
 template<bool __v>
 using __bool_constant = integral_constant<bool, __v>;
 
+export
 template<typename _Tp, typename... _Args>
 struct __is_constructible_impl
   : public __bool_constant<__is_constructible(_Tp, _Args...)>
index 842bf4135851fc93a7d53e29b585a7b8588242db..54aa069436aff10feac3c1544925a6ec52fcdf23 100644 (file)
@@ -1,6 +1,6 @@
 // { dg-additional-options -fmodules-ts }
 
-module foo;
+import foo;
 
 int main ()
 {
diff --git a/gcc/testsuite/g++.dg/modules/main-1.C b/gcc/testsuite/g++.dg/modules/main-1.C
new file mode 100644 (file)
index 0000000..0d79edd
--- /dev/null
@@ -0,0 +1,5 @@
+// { dg-additional-options "-fmodules-ts" }
+// { dg-prune-output "not writing module" }
+
+export module M;
+int main() {}  // { dg-error "attach" }
diff --git a/gcc/testsuite/g++.dg/parse/linkage5.C b/gcc/testsuite/g++.dg/parse/linkage5.C
new file mode 100644 (file)
index 0000000..451406d
--- /dev/null
@@ -0,0 +1,14 @@
+// { dg-do compile }
+// The main function shall not be declared with a linkage-specification.
+
+extern "C" {
+  int main();  // { dg-error "linkage" }
+}
+
+namespace foo {
+  extern "C" int main();  // { dg-error "linkage" }
+}
+
+extern "C++" int main(); // { dg-error "linkage" }
+
+extern "C" struct S { int main(); };  // OK
diff --git a/gcc/testsuite/g++.dg/parse/linkage6.C b/gcc/testsuite/g++.dg/parse/linkage6.C
new file mode 100644 (file)
index 0000000..44081c1
--- /dev/null
@@ -0,0 +1,13 @@
+// { dg-do compile }
+// A program that declares an entity named main with C language linkage
+// (in any namespace) is ill-formed.
+
+namespace foo {
+  extern "C" int main;  // { dg-error "linkage" }
+  extern "C" struct A {
+    int main;  // OK
+  };
+  extern "C" struct B {
+    int main();  // OK
+  };
+}