On Tue, Jun 24, 2025 at 11:14:51AM -0400, Jason Merrill wrote:
> On 6/24/25 10:16 AM, Nathaniel Shead wrote:
> > 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.
>
> I suppose we could say "other than %<extern \"C++\"%>" to make that a little
> clearer. OK with that tweak.
>
> I wouldn't object to a fixup but it sounds more complicated than it's worth
> to have different fixups for the extern "C" { int main(); } and extern "C"
> int main(); cases.
>
> Jason
>
I think I wasn't totally clear sorry; here's a patch with what I meant.
Tested on x86_64-pc-linux-gnu, OK for trunk?
-- >8 --
Subject: [PATCH] c++: Add fix note for how to declare main in a module
This patch adds a note to help users unfamiliar with modules terminology
understand how to declare main in a named module since P3618.
There doesn't appear to be an easy robust location available for "the
start of this declaration" that I could find to attach a fixit to, but
the explanation should suffice.
gcc/cp/ChangeLog:
* decl.cc (grokfndecl): Add explanation of how to attach to
global module.
Signed-off-by: Nathaniel Shead <[email protected]>
---
gcc/cp/decl.cc | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 95bccfbb585..4fe97ffbf8f 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -11330,7 +11330,12 @@ grokfndecl (tree ctype,
pedwarn (location, OPT_Wpedantic, "cannot declare %<::main%> with a"
" linkage specification other than %<extern \"C++\"%>");
if (module_attach_p ())
- error_at (location, "cannot attach %<::main%> to a named module");
+ {
+ auto_diagnostic_group adg;
+ error_at (location, "cannot attach %<::main%> to a named module");
+ inform (location, "use %<extern \"C++\"%> to attach it to the "
+ "global module instead");
+ }
inlinep = 0;
publicp = 1;
}
--
2.47.0