PR c++/39751: ICE-on-invalid parsing regression.
Commit Message
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 <roger@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
--
Comments
On 2/26/22 19:55, Roger Sayle wrote:
>
> 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 <roger@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.
This is OK.
> (cp_parser_handle_statement_omp_attributes): Silence compilation
> warnings using memset to initialize structure.
> (cp_parser_late_parsing_omp_declare_simd): Likewise.
These are not; I don't want to uglify the code to satisfy a broken
warning. The warning is documented to allow this pattern:
Likewise, in C++ this option does not warn about the empty { }
initializer, for example:
struct s { int f, g, h; };
s x = { };
So all the instances you changed indicate a bug in the warning that
should be fixed rather than worked around.
Even if this were OK, it's completely unrelated to PR39751, so should
have been a separate patch.
> +++ b/gcc/testsuite/g++.dg/pr39751.C
Should go in g++.dg/parse/
> +/* { dg-options "-O2" } */
Unneeded.
Jason
@@ -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;
new file mode 100644
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+
+template < typename >
+struct A
+{
+A < struct
+{
+f () :
+
+// { dg-excess-errors "" }