Bug 28858 - [4.0/4.1/4.2 regression] Algorithm to find the end of a template parameter list is flawed
Summary: [4.0/4.1/4.2 regression] Algorithm to find the end of a template parameter li...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.2.0
: P4 normal
Target Milestone: 4.2.0
Assignee: Volker Reichelt
URL:
Keywords: diagnostic, error-recovery
Depends on:
Blocks:
 
Reported: 2006-08-26 20:55 UTC by Volker Reichelt
Modified: 2006-09-08 23:28 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Volker Reichelt 2006-08-26 20:55:25 UTC
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.
Comment 1 patchapp@dberlin.org 2006-08-26 22:46:35 UTC
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
Comment 2 Volker Reichelt 2006-09-08 22:56:53 UTC
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

Comment 3 Volker Reichelt 2006-09-08 23:28:40 UTC
Fixed on mainline.
Won't backport to 4.1 and 4.0 branch.