On Tue, Jun 24, 2025 at 01:03:53PM +0200, Jakub Jelinek wrote:
> Hi!
>
> 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.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
>
> 2025-06-24 Jakub Jelinek <[email protected]>
>
> 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.jj 2025-06-19 08:55:04.408676724 +0200
> +++ gcc/cp/decl.cc 2025-06-23 17:47:13.942011687 +0200
> @@ -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 %<\"C++\"%>");
> if (module_attach_p ())
> error_at (location, "cannot attach %<::main%> to a named module");
Maybe it would be nice to add a note/fixit that users can now work
around this error by marking main as 'extern "C++"'? But overall LGTM.
> inlinep = 0;
> --- gcc/testsuite/g++.dg/parse/linkage5.C.jj 2024-05-22 09:11:46.979234663
> +0200
> +++ gcc/testsuite/g++.dg/parse/linkage5.C 2025-06-23 18:00:38.067742494
> +0200
> @@ -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
> --- gcc/testsuite/g++.dg/parse/linkage7.C.jj 2025-06-23 18:01:17.622237056
> +0200
> +++ gcc/testsuite/g++.dg/parse/linkage7.C 2025-06-23 18:01:32.385048426
> +0200
> @@ -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();
> +}
> --- gcc/testsuite/g++.dg/parse/linkage8.C.jj 2025-06-23 18:01:39.830953283
> +0200
> +++ gcc/testsuite/g++.dg/parse/linkage8.C 2025-06-23 18:01:57.657725492
> +0200
> @@ -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" }
> --- gcc/testsuite/g++.dg/modules/main-2.C.jj 2025-06-23 18:25:17.058941644
> +0200
> +++ gcc/testsuite/g++.dg/modules/main-2.C 2025-06-23 18:26:11.416253264
> +0200
> @@ -0,0 +1,4 @@
> +// { dg-additional-options "-fmodules" }
> +
> +export module M;
> +extern "C++" int main() {}
> --- gcc/testsuite/g++.dg/modules/main-3.C.jj 2025-06-23 18:26:20.393139580
> +0200
> +++ gcc/testsuite/g++.dg/modules/main-3.C 2025-06-23 18:26:33.190977509
> +0200
> @@ -0,0 +1,7 @@
> +// { dg-additional-options "-fmodules" }
> +
> +export module M;
> +extern "C++" {
> + int main() {}
> +}
> +
>
> Jakub
>