Page MenuHomePhabricator

clang-format: Support generalized lambda captures.
AbandonedPublic

Authored by strager on Jul 31 2015, 1:51 PM.

Details

Reviewers
djasper
Summary

Teach clang-format about C++14 generalized lambda captures:

auto f = [b = std::move(d)]() {};

(clang-format would not think there's a lambda here because
of the =.)

Diff Detail

Event Timeline

strager updated this revision to Diff 31160.Jul 31 2015, 1:51 PM
strager retitled this revision from to clang-format: Support generalized lambda captures..
strager updated this object.
strager added a reviewer: djasper.
strager added subscribers: sas, abdulras, cfe-commits.
djasper edited edge metadata.Aug 8 2015, 3:50 AM

This is the corresponding bug: llvm.org/PR19986

lib/Format/UnwrappedLineParser.cpp
1060–1061

I very much doubt that we'll have an Expression parser here anytime soon. So, I don't think that this FIXME makes much sense. Instead, please provide a comment on what this is actually doing and in which cases it might fail.

1064

I think this list should be extended to figure out certain cases where we know something is fishy. In particular:

  • If you find an l_square or less, call into parseSquare and parseAngle respectively.
  • If you find an r_brace or semi, something is wrong, break.
strager planned changes to this revision.Aug 11 2015, 6:12 PM
strager added inline comments.
lib/Format/UnwrappedLineParser.cpp
1060–1061

I copied the comment from elsewhere in the file.

1064

Will do.

strager marked 4 inline comments as done.Sep 16 2015, 5:16 PM
strager added inline comments.
lib/Format/UnwrappedLineParser.cpp
1060–1061

I expanded the comment, including a reference to the other reference to parseAssigmentExpression.

1064

Handling r_brace and semi is a bit weird, since we end up aborting mid-stream and what's left becomes unparsable/incomplete by clang-format.

parseAngle doesn't exist, and even if it did, the less-than operator wouldn't be handled properly.

I added l_square and l_brace support.

strager updated this revision to Diff 34946.Sep 16 2015, 5:16 PM
strager marked 2 inline comments as done.
strager edited edge metadata.

Address @djasper's comments.

djasper added inline comments.Sep 17 2015, 9:46 AM
lib/Format/UnwrappedLineParser.cpp
1060–1061

Again, please remove the FIXME. We aren't going to have an expression parser here (anytime soon) and shouldn't add (more) comments that make people think otherwise.

1064

Ah, parseAngle doesn't exist here. I was thinking about the TokenAnnotator.

I don't understand your comment about mid-stream. This is precisely about the case where the input is corrupt so that clang-format can recover and doesn't just parse the reset of the file input the lambda introducer.

strager marked 2 inline comments as done.Sep 17 2015, 1:00 PM
strager added inline comments.
lib/Format/UnwrappedLineParser.cpp
1060–1061

We aren't going to have an expression parser here (anytime soon) and shouldn't add (more) comments that make people think otherwise.

If there is enough need for the function, perhaps it will be written.

I don't think the comment implies some code will be written soon.

1064

This is precisely about the case where the input is corrupt so that clang-format can recover and doesn't just parse the reset of the file input the lambda introducer.

If I write this test:

verifyFormat("return [}] {};\n");

I get this output:

/Users/strager/Projects/llvm/tools/clang/unittests/Format/FormatTest.cpp:42: Failure
Value of: IncompleteFormat
  Actual: true
Expected: ExpectedIncompleteFormat
Which is: false
return [}] {};



/Users/strager/Projects/llvm/tools/clang/unittests/Format/FormatTest.cpp:65: Failure
Value of: format(test::messUp(Code), Style)
  Actual: "return [\n}] {};\n"
Expected: Code.str()
Which is: "return [}] {};\n"

How can I fix this?

djasper added inline comments.Sep 22 2015, 2:40 AM
lib/Format/UnwrappedLineParser.cpp
1064

So, there are two errors in the test.

  1. clang-format detects that there is a syntax error and reports this. Use verifyIncompleteFormat instead of verifyFormat.
  1. clang-format adds a '\n' after the opening square bracket. Add that in your test string.
strager abandoned this revision.Oct 10 2019, 6:41 PM
strager marked 2 inline comments as done.