This is a fix for PR c++/39751 which is an ICE-on-invalid regression in
the C++ parser after encountering the end of file.  The one line change
is to check that the tokens cached in DECL_PENDING_INLINE_INFO haven't
been purged before processing them in cp_parser_late_parsing_for_member.

Alas in addition to the one line fix (and new test case), I've also
taken the opportunity to silence the -Wmissing-field-initializers
warnings compiling this source file, by replacing the " = { };" with
explicit calls to memset to initialize/reset structures.

This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check with no new (unexpected) failures.  Ok for mainline?


2022-02-26  Roger Sayle  <ro...@nextmovesoftware.com>

gcc/cp/ChangeLog
        PR c++/39751
        * parser.cc (cp_parser_late_parsing_for_member): Confirm the token
        stream hasn't been purged before processing DECL_PENDING_INLINE.

        (cp_parser_handle_statement_omp_attributes): Silence compilation
        warnings using memset to initialize structure.
        (cp_parser_late_parsing_omp_declare_simd): Likewise.

gcc/testsuite/ChangeLog
        PR c++/39751
        * g++.dg/pr39751.C: New test case.


Thanks in advance,
Roger
--

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 03d99ab..585502b 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -11935,7 +11935,8 @@ cp_parser_handle_statement_omp_attributes (cp_parser 
*parser, tree attrs)
            if (flag_openmp || dir->simd)
              tokens += (last - first) + 1;
          }
-       cp_omp_attribute_data v = {};
+       cp_omp_attribute_data v;
+       memset (&v, 0, sizeof (v));
        vec.safe_push (v);
        *pa = TREE_CHAIN (*pa);
       }
@@ -12021,7 +12022,8 @@ cp_parser_handle_statement_omp_attributes (cp_parser 
*parser, tree attrs)
        continue;
       cp_token *first = v->tokens->first;
       cp_token *last = v->tokens->last;
-      cp_token tok = {};
+      cp_token tok;
+      memset (&tok, 0, sizeof (tok));
       tok.type = CPP_PRAGMA;
       tok.keyword = RID_MAX;
       tok.u.value = build_int_cst (NULL, v->dir->id);
@@ -12029,13 +12031,14 @@ cp_parser_handle_statement_omp_attributes (cp_parser 
*parser, tree attrs)
       lexer->buffer->quick_push (tok);
       while (++first < last)
        lexer->buffer->quick_push (*first);
-      tok = {};
+      memset (&tok, 0, sizeof (tok));
       tok.type = CPP_PRAGMA_EOL;
       tok.keyword = RID_MAX;
       tok.location = last->location;
       lexer->buffer->quick_push (tok);
     }
-  cp_token tok = {};
+  cp_token tok;
+  memset (&tok, 0, sizeof (tok));
   tok.type = CPP_EOF;
   tok.keyword = RID_MAX;
   tok.location = lexer->buffer->last ().location;
@@ -32126,8 +32129,9 @@ cp_parser_late_parsing_for_member (cp_parser* parser, 
tree member_function)
   maybe_begin_member_template_processing (member_function);
 
   /* If the body of the function has not yet been parsed, parse it
-     now.  */
-  if (DECL_PENDING_INLINE_P (member_function))
+     now.  Except if the tokens have been purged (PR c++/39751).  */
+  if (DECL_PENDING_INLINE_P (member_function)
+      && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
     {
       tree function_scope;
       cp_token_cache *tokens;
@@ -45693,7 +45697,8 @@ cp_parser_late_parsing_omp_declare_simd (cp_parser 
*parser, tree attrs)
                    lexer->debugging_p = parser->lexer->debugging_p;
                  }
                vec_safe_reserve (lexer->buffer, (last - first) + 2);
-               cp_token tok = {};
+               cp_token tok;
+               memset (&tok, 0, sizeof (tok));
                tok.type = CPP_PRAGMA;
                tok.keyword = RID_MAX;
                tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
@@ -45701,12 +45706,12 @@ cp_parser_late_parsing_omp_declare_simd (cp_parser 
*parser, tree attrs)
                lexer->buffer->quick_push (tok);
                while (++first < last)
                  lexer->buffer->quick_push (*first);
-               tok = {};
+               memset (&tok, 0, sizeof (tok));
                tok.type = CPP_PRAGMA_EOL;
                tok.keyword = RID_MAX;
                tok.location = last->location;
                lexer->buffer->quick_push (tok);
-               tok = {};
+               memset (&tok, 0, sizeof (tok));
                tok.type = CPP_EOF;
                tok.keyword = RID_MAX;
                tok.location = last->location;
diff --git a/gcc/testsuite/g++.dg/pr39751.C b/gcc/testsuite/g++.dg/pr39751.C
new file mode 100644
index 0000000..91b09ac
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr39751.C
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+template < typename >
+struct A
+{
+A < struct
+{
+f () :
+
+// { dg-excess-errors "" }

Reply via email to