If an error occurs in a template parameter list, the frontend sometimes calls cp_parser_skip_until_found to find the ">" that marks the end of the list. The algorithm used there has some flaws, though: 1) It doesn't keep track of the nesting levels of "< ... >". This results in bogus error messages when templates are used as default parameters or when template template parameters are involved, e.g.: template<TEMPLATE<int> class> struct A {}; yields: bug.cc:1: error: 'TEMPLATE' has not been declared bug.cc:1: error: expected `>' before '<' token bug.cc:1: error: expected identifier before '>' token bug.cc:1: error: expected unqualified-id before '>' token 2) It doesn't stop at ";", or "{", or "}". (These tokens probably indicate the end of a declaration or the start of a function/struct body which is not part of a template parameter list.) Because of this large parts of the source might be skipped, if one forgets the closing ">", again resulting in bogus errors, e.g.: template<int N struct A; bool i = 1 > 0; int j = i; yields: bug.cc:1: error: expected `>' before 'struct' bug.cc:2: error: expected unqualified-id before numeric constant bug.cc:3: error: 'i' was not declared in this scope Posting a patch soon.
Subject: Bug number PR c++/28858 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-08/msg01013.html
Subject: Bug 28858 Author: reichelt Date: Fri Sep 8 22:56:44 2006 New Revision: 116788 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=116788 Log: PR c++/28858 * parser.c (cp_parser_skip_until_found): Rename to cp_parser_skip_to_end_of_template_parameter_list. Remove last two parameters. Track levels of '< ... >'. Stop at '{', '}', or ';'. Reorganize. Adjust comment. (cp_parser_template_declaration_after_export): Adjust call. (cp_parser_enclosed_template_argument_list): Likewise. * g++.dg/parse/template20.C: New test. * g++.dg/template/operator8.C: Remove obsolete part. * g++.dg/parse/def-tmpl-arg1.C: Adjust error-markers. * g++.old-deja/g++.pt/crash65.C: Likewise. Added: trunk/gcc/testsuite/g++.dg/parse/template20.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/parser.c trunk/gcc/testsuite/ChangeLog trunk/gcc/testsuite/g++.dg/parse/def-tmpl-arg1.C trunk/gcc/testsuite/g++.dg/template/operator8.C trunk/gcc/testsuite/g++.old-deja/g++.pt/crash65.C
Fixed on mainline. Won't backport to 4.1 and 4.0 branch.