c++: Fix up cp_parser_skip_to_pragma_eol [PR104623]
authorJakub Jelinek <jakub@redhat.com>
Tue, 15 Mar 2022 08:15:27 +0000 (09:15 +0100)
committerJakub Jelinek <jakub@redhat.com>
Tue, 15 Mar 2022 08:15:27 +0000 (09:15 +0100)
We ICE on the following testcase, because we tentatively parse it multiple
times and the erroneous attribute syntax results in
cp_parser_skip_to_end_of_statement, which when seeing CPP_PRAGMA (can be
any deferred one, OpenMP/OpenACC/ivdep etc.) it calls
cp_parser_skip_to_pragma_eol, which calls cp_lexer_purge_tokens_after.
That call purges all the tokens from CPP_PRAGMA until CPP_PRAGMA_EOL,
excluding the initial CPP_PRAGMA though (but including the final
CPP_PRAGMA_EOL).  This means the second time we parse this, we see
CPP_PRAGMA with no tokens after it from the pragma, most importantly
not the CPP_PRAGMA_EOL, so either if it is the last pragma in the TU,
we ICE, or if there are other pragmas we treat everything in between
as a pragma.

I've tried various things, including making the CPP_PRAGMA token
itself also purged, or changing the cp_parser_skip_to_end_of_statement
(and cp_parser_skip_to_end_of_block_or_statement) to call it with
NULL instead of token, so that this purging isn't done there,
but each patch resulted in lots of regressions.
But removing the purging altogether surprisingly doesn't regress anything,
and I think it is the right thing, if we e.g. parse tentatively, why can't
we parse the pragma multiple times or at least skip over it?

2022-03-15  Jakub Jelinek  <jakub@redhat.com>

PR c++/104623
* parser.cc (cp_parser_skip_to_pragma_eol): Don't purge any tokens.

* g++.dg/gomp/pr104623.C: New test.

gcc/cp/parser.cc
gcc/testsuite/g++.dg/gomp/pr104623.C [new file with mode: 0644]

index 87b9781..15ad640 100644 (file)
@@ -4111,8 +4111,6 @@ cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
 
   if (pragma_tok)
     {
-      /* Ensure that the pragma is not parsed again.  */
-      cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
       parser->lexer->in_pragma = false;
       if (parser->lexer->in_omp_attribute_pragma
          && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
diff --git a/gcc/testsuite/g++.dg/gomp/pr104623.C b/gcc/testsuite/g++.dg/gomp/pr104623.C
new file mode 100644 (file)
index 0000000..725e19e
--- /dev/null
@@ -0,0 +1,9 @@
+// PR c++/104623
+// { dg-do compile }
+
+void
+foo ()
+{
+  struct __attribute__() a     // { dg-error "expected primary-expression before" }
+  #pragma omp task
+}