]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libcpp: Fix up cpp_maybe_module_directive [PR120845]
authorJakub Jelinek <jakub@redhat.com>
Sun, 3 Aug 2025 16:27:42 +0000 (18:27 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Sun, 3 Aug 2025 16:27:42 +0000 (18:27 +0200)
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.

The patch arranges for eol being set only in the not_module case.

2025-08-03  Jakub Jelinek  <jakub@redhat.com>

PR c++/120845
libcpp/
* lex.cc (cpp_maybe_module_directive): Move eol variable declaration
to the start of the function, initialize to false and only set it to
peek->type == CPP_PRAGMA_EOL in the not_module case.  Formatting fix.
gcc/testsuite/
* g++.dg/modules/cpp-21.C: New test.

gcc/testsuite/g++.dg/modules/cpp-21.C [new file with mode: 0644]
libcpp/lex.cc

diff --git a/gcc/testsuite/g++.dg/modules/cpp-21.C b/gcc/testsuite/g++.dg/modules/cpp-21.C
new file mode 100644 (file)
index 0000000..fdd0492
--- /dev/null
@@ -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 }
index e7705a64f395d6f892d3a237c3da66b9a76aebce..2ba9d588708fcd25376dec2965639795eff56efd 100644 (file)
@@ -3505,6 +3505,7 @@ cpp_maybe_module_directive (cpp_reader *pfile, cpp_token *result)
   cpp_token *keyword = peek;
   cpp_hashnode *(&n_modules)[spec_nodes::M_HWM][2] = pfile->spec_nodes.n_modules;
   int header_count = 0;
+  bool eol = false;
 
   /* Make sure the incoming state is as we expect it.  This way we
      can restore it using constants.  */
@@ -3564,10 +3565,10 @@ cpp_maybe_module_directive (cpp_reader *pfile, cpp_token *result)
      tokens.  C++ keywords are not yet relevant.  */
   if (peek->type == CPP_NAME
       || peek->type == CPP_COLON
-      ||  (header_count
-          ? (peek->type == CPP_LESS
-             || (peek->type == CPP_STRING && peek->val.str.text[0] != 'R')
-             || peek->type == CPP_HEADER_NAME)
+      || (header_count
+         ? (peek->type == CPP_LESS
+            || (peek->type == CPP_STRING && peek->val.str.text[0] != 'R')
+            || peek->type == CPP_HEADER_NAME)
           : peek->type == CPP_SEMICOLON))
     {
       pfile->state.pragma_allow_expansion = !CPP_OPTION (pfile, preprocessed);
@@ -3689,22 +3690,19 @@ cpp_maybe_module_directive (cpp_reader *pfile, cpp_token *result)
       pfile->state.in_deferred_pragma = false;
       /* Do not let this remain on.  */
       pfile->state.angled_headers = false;
+      /* If we saw EOL, we should drop it, because this isn't a module
+        control-line after all.  */
+      eol = peek->type == CPP_PRAGMA_EOL;
     }
 
   /* In either case we want to backup the peeked tokens.  */
-  if (backup)
+  if (backup && (!eol || backup > 1))
     {
-      /* If we saw EOL, we should drop it, because this isn't a module
-        control-line after all.  */
-      bool eol = peek->type == CPP_PRAGMA_EOL;
-      if (!eol || backup > 1)
-       {
-         /* Put put the peeked tokens back  */
-         _cpp_backup_tokens_direct (pfile, backup);
-         /* But if the last one was an EOL, forget it.  */
-         if (eol)
-           pfile->lookaheads--;
-       }
+      /* Put the peeked tokens back.  */
+      _cpp_backup_tokens_direct (pfile, backup);
+      /* But if the last one was an EOL in the not_module case, forget it.  */
+      if (eol)
+       pfile->lookaheads--;
     }
 }