This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [C++0x] avoid extra tentative parse in range-based for loops


On Wed, Dec 22, 2010 at 6:37 PM, Jason Merrill <jason@redhat.com> wrote:
> As you mentioned, this now needs a tweak for the colon autocorrect patch.
Ok. I've added it to cp_parser_for_init_statement, as it is here where
the colon is parsed.

>> Â Â Â /* If the tentative parse failed, then we shall need to look for an
>> Â Â Â Â expression-statement. Â*/
>
> This comment is in the wrong place now; it should stay with the call to
> cp_parser_parse_definitely.
Right. Moved.

>> + Â Â Âif (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
>> + Â Â Â {
>> + Â Â Â Â /* It is a range-for, consume the ':' */
>> + Â Â Â Â cp_lexer_consume_token (parser->lexer);
>> + Â Â Â Â if (cxx_dialect < cxx0x)
>> + Â Â Â Â Â {
>> + Â Â Â Â Â Â error_at (cp_lexer_peek_token (parser->lexer)->location,
>> + Â Â Â Â Â Â Â Â Â Â Â "range-based-for loops are not allowed "
>> + Â Â Â Â Â Â Â Â Â Â Â "in C++98 mode");
>> + Â Â Â Â Â }
>> + Â Â Â Â else if (*decl && *decl != error_mark_node )
>> + Â Â Â Â Â is_range_for = true;
>> + Â Â Â Â /* Else an error has been found in the declaration. It should
>> have
>> + Â Â Â Â Â Âbeen reported by now, so no message is required. Â*/
>> + Â Â Â }
>
> I think error-recovery will be better if we always set is_range_for when we
> see a colon.
Even if we are in cxx98 mode? Certainly we would avoid a cascade of errors.

What do you think of the proposed patch? I added a few tests for
error_mark_node to avoid cascaded errors once the first has been
reported. Also, the testcase range-for9.C now emits just one error
(the right one).

Regards.
Rodrigo

--

Changelog:

gcc/cp/

2010-12-27  Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>

	* cp-tree.h (begin_for_scope): New prototype.
	(begin_for_stmt): Update prototype.
	(begin_range_for_stmt): Update prototype.
	* init.c (build_vec_init): Update call to begin_for_stmt.
	* parser.c (cp_parser_for): New.
	(cp_parser_c_for): Add three new parameters.
	(cp_parser_range_for): Likewise. Most parsing code removed.
	(cp_parser_iteration_statement): Call cp_parser_for instead of
	cp_parser_c_for and cp_parser_range_for.
	(cp_parser_for_init_statement): Add new parameter and return type.
	(cp_parser_block_declaration): Update call to
	cp_parser_simple_declaration.
	(cp_parser_simple_declaration): Add new parameter.
	Update call to cp_parser_init_declarator.
	(cp_parser_init_declarator): Add new parameter.
	* pt.c (tsubst_expr): Update call to begin_for_stmt.
	* semantics.c (begin_for_scope): New.
	(begin_for_stmt): Add two new parameters.
	(begin_range_for_stmt): Likewise.

gcc/testsuite/

2010-12-27  Rodrigo Rivas Costa <rodrigorivascosta@gmail.com>

	* g++.dg/cpp0x/range-for4.C: Delete useless include and duplicated
	comment.
	* g++.dg/cpp0x/range-for8.C: New.
	* g++.dg/cpp0x/range-for9.C: New.

Attachment: range_for5.txt
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]