Hi!

My changes for "Module Declarations Shouldn’t be Macros" paper broke
the following testcase.  The backup handling intentionally tries to
drop CPP_PRAGMA_EOL token if things go wrong, which is desirable for the
case where we haven't committed to the module preprocessing directive
(i.e. changed the first token to the magic one).  In that case there is
no preprocessing directive start and so CPP_PRAGMA_EOL would be wrong.
If there is a premature new-line after we've changed the first token though,
we shouldn't drop CPP_PRAGMA_EOL, because otherwise we ICE in the FE.

While clang++ and MSVC accept the testcase, in my reading it is incorrect
at least in the C++23 and newer wordings and I think the changes have been
a DR, https://eel.is/c++draft/cpp.module has no exception for new-lines
and https://eel.is/c++draft/cpp.pre#1.sentence-2 says that new-line (unless
deleted during phase 2 when after backslash)  ends the preprocessing
directive.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk and
15.2?

2025-06-27  Jakub Jelinek  <ja...@redhat.com>

        PR c++/120845
libcpp/
        * lex.cc (cpp_maybe_module_directive): If premature CPP_PRAGMA_EOL
        is seen after CPP_NAME, back it up instead of dropping it.
gcc/testsuite/
        * g++.dg/modules/cpp-21.C: New test.

--- libcpp/lex.cc.jj    2025-04-24 15:29:41.714893932 +0200
+++ libcpp/lex.cc       2025-06-27 12:19:23.593682734 +0200
@@ -3677,6 +3677,13 @@ cpp_maybe_module_directive (cpp_reader *
                break;
            }
          while (true);
+         if (peek->type == CPP_PRAGMA_EOL)
+           {
+             /* If we saw premature EOL, we want to keep it at this
+                point rather than drop it.  See PR120845.  */
+             _cpp_backup_tokens_direct (pfile, backup);
+             backup = 0;
+           }
        }
     }
   else
--- gcc/testsuite/g++.dg/modules/cpp-21.C.jj    2025-06-27 12:34:17.226227463 
+0200
+++ gcc/testsuite/g++.dg/modules/cpp-21.C       2025-06-27 12:34:28.052088809 
+0200
@@ -0,0 +1,8 @@
+// PR c++/120845
+// { dg-do compile }
+// { dg-additional-options "-fmodules" }
+
+export module pr120485
+    [[foobarbaz]];
+// { dg-error "expected ';' before end of line" "" { target *-*-* } .-2 }
+// { dg-warning "attribute ignored" "" { target *-*-* } .-2 }

        Jakub

Reply via email to