[cfe-commits] [PATCH] Parsing C++0x lambda expressions

David Blaikie dblaikie at gmail.com
Wed Aug 3 10:23:45 PDT 2011


>> Okay, we'd like some additional tests in here to make sure that the disambiguation is working properly, the diagnostics work properly, etc.
>
> I took the liberty of helping out by having a go at some test cases
> for John. This covers some positive/negative cases (lambdas in
> objc(negative), objc++(negative), objc++0x, c++(negative), c++0x). I
> found a couple of bugs in the process:

I found some previous feedback by Douglas that was given before I was
subscribed to cfe-commits (
http://www.mail-archive.com/cfe-commits@cs.uiuc.edu/msg36457.html )

> +  bool first = true;
> +  while (Tok.isNot(tok::r_square)) {
> +    if (first) {
> +      first = false;
> +
> +      // Parse capture-default.
> +      if (Tok.is(tok::amp) && NextToken().isNot(tok::identifier)) {
> +        List.Default = DEFAULT_CAPTURE_BY_REF;
> +      } else if (Tok.is(tok::equal)) {
> +        List.Default = DEFAULT_CAPTURE_BY_COPY;
> +      }
> +
> +      if (List.Default != DEFAULT_CAPTURE_NONE) {
> +        // Consume '&' or '='.
> +        ConsumeToken();
> +        continue;
> +      }
> +    } else {
> +      if (ExpectAndConsume(tok::comma,
> +                           diag::err_expected_comma,
> +                           "",
> +                           tok::r_square)) {
> +        // FIXME: We could also expect end of capture list. Should the
> +        // diagnostic indicate this?
> +        return ExprError();
> +      }
> +    }
>
> I find this logic to be a bit twisty. Why not do an initial check for &
> (followed by non-identifier) or = to set the default capture kind, then go into
> the loop that handles individual captures?

I'd had the same thought when I was adding test cases & fixing a
couple of bugs, but when I went to rewrite it I wasn't confident
enough that I could come up with a better solution. After reading this
feedback I went back to have another go & came up with the version
I've attached (this includes everything I sent out in my last email,
plus a couple more test cases around the effected paths).

I still needed the "first" flag, unfortunately - if the comma after a
default capture mode was consumed in the check for the capture mode,
then "[&,]" might have parsed successfully (since the main loop
wouldn't've differentiated between the case where a variable was
required and one where it was optional). But this does seem to be at
least a little bit tidier.

- David
-------------- next part --------------
A non-text attachment was scrubbed...
Name: lambda-parse.patch
Type: application/octet-stream
Size: 17184 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20110803/e9c79b67/attachment.obj>


More information about the cfe-commits mailing list