This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: C++ PATCH for C++17 selection statements with initializer
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Marek Polacek <polacek at redhat dot com>
- Cc: Jason Merrill <jason at redhat dot com>, GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Wed, 5 Oct 2016 15:31:51 +0200
- Subject: Re: C++ PATCH for C++17 selection statements with initializer
- Authentication-results: sourceware.org; auth=none
- References: <20161005131425.GQ3223@redhat.com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Wed, Oct 05, 2016 at 03:14:25PM +0200, Marek Polacek wrote:
> This is my attempt to implement P0305R1, Selection statements with initializer.
> It allows the users to write
>
> if (init; cond) // ...
>
> which is equivalent to
>
> {
> init
> if (cond) // ...
> }
Well, it isn't exactly equivalent, because unlike { init; if (cond) /* ... */; }
there aren't two scopes, but just one. So I'd say you should have tests
that verify that init and cond are indeed in the same scope, e.g. by trying
something like if (int c = 5; int c = 5) ... and verifying dg-error is
reported.
> + case CPP_CLOSE_PAREN:
> + /* If the next token is a non-nested '(', then we have reached
> + the end of the if condition. */
Looks like typo, shouldn't that be ')' ?
Also, do you really need two counters?
> + if (paren_depth-- == 0)
> + return false;
> + break;
> +
> + case CPP_OPEN_PAREN:
> + ++paren_depth;
> + break;
> +
> + case CPP_CLOSE_BRACE:
> + --brace_depth;
I mean, shouldn't you also stop before } when seeing if (int a = }; b)
rather than wrapping around?
> + /* Consume the token. */
> + cp_lexer_consume_token (parser->lexer);
> + }
Also, do you really need to consume all the tokens and then rollback, rather
than just use peek_nth_token with the index increasing in each iteration?
Jakub