]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Implement C++26 P3618R0 - Allow attaching main to the global module [PR120773]
authorJakub Jelinek <jakub@redhat.com>
Tue, 24 Jun 2025 17:00:11 +0000 (19:00 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 24 Jun 2025 17:00:11 +0000 (19:00 +0200)
The following patch implements the P3618R0 paper by tweaking pedwarn
condition, adjusting pedwarn wording, adjusting one testcase and adding 4
new ones.  The paper was voted in as DR, so it isn't guarded on C++ version.

2025-06-24  Jakub Jelinek  <jakub@redhat.com>

PR c++/120773
* decl.cc (grokfndecl): Implement C++26 P3618R0 - Allow attaching
main to the global module.  Only pedwarn for current_lang_name
other than lang_name_cplusplus and adjust pedwarn wording.

* g++.dg/parse/linkage5.C: Don't expect error on
extern "C++" int main ();.
* g++.dg/parse/linkage7.C: New test.
* g++.dg/parse/linkage8.C: New test.
* g++.dg/modules/main-2.C: New test.
* g++.dg/modules/main-3.C: New test.

gcc/cp/decl.cc
gcc/testsuite/g++.dg/modules/main-2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/main-3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/parse/linkage5.C
gcc/testsuite/g++.dg/parse/linkage7.C [new file with mode: 0644]
gcc/testsuite/g++.dg/parse/linkage8.C [new file with mode: 0644]

index febdc89f89ddda0a8a2fde940531405126d735ed..95bccfbb585ba8d286902f02e0d4c6ef5fe2e53b 100644 (file)
@@ -11326,9 +11326,9 @@ 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)
+      if (current_lang_name != lang_name_cplusplus)
        pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
-                " linkage specification");
+                " linkage specification other than %<extern \"C++\"%>");
       if (module_attach_p ())
        error_at (location, "cannot attach %<::main%> to a named module");
       inlinep = 0;
diff --git a/gcc/testsuite/g++.dg/modules/main-2.C b/gcc/testsuite/g++.dg/modules/main-2.C
new file mode 100644 (file)
index 0000000..8d17381
--- /dev/null
@@ -0,0 +1,4 @@
+// { dg-additional-options "-fmodules" }
+
+export module M;
+extern "C++" int main() {}
diff --git a/gcc/testsuite/g++.dg/modules/main-3.C b/gcc/testsuite/g++.dg/modules/main-3.C
new file mode 100644 (file)
index 0000000..10a2936
--- /dev/null
@@ -0,0 +1,7 @@
+// { dg-additional-options "-fmodules" }
+
+export module M;
+extern "C++" {
+  int main() {}
+}
+
index 451406de69b29337da7276f32f2aef1ba9debf31..1bd4736906c3bcaba14dcfd5676fa81604af4ea4 100644 (file)
@@ -1,5 +1,6 @@
 // { dg-do compile }
-// The main function shall not be declared with a linkage-specification.
+// The main function shall not be declared with a linkage-specification
+// other than "C++".
 
 extern "C" {
   int main();  // { dg-error "linkage" }
@@ -9,6 +10,6 @@ namespace foo {
   extern "C" int main();  // { dg-error "linkage" }
 }
 
-extern "C++" int main(); // { dg-error "linkage" }
+extern "C++" int main();
 
 extern "C" struct S { int main(); };  // OK
diff --git a/gcc/testsuite/g++.dg/parse/linkage7.C b/gcc/testsuite/g++.dg/parse/linkage7.C
new file mode 100644 (file)
index 0000000..91caf26
--- /dev/null
@@ -0,0 +1,7 @@
+// { dg-do compile }
+// The main function shall not be declared with a linkage-specification
+// other than "C++".
+
+extern "C++" {
+  int main();
+}
diff --git a/gcc/testsuite/g++.dg/parse/linkage8.C b/gcc/testsuite/g++.dg/parse/linkage8.C
new file mode 100644 (file)
index 0000000..b757ed5
--- /dev/null
@@ -0,0 +1,5 @@
+// { dg-do compile }
+// The main function shall not be declared with a linkage-specification
+// other than "C++".
+
+extern "C" int main(); // { dg-error "linkage" }