This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Better parsing of lambda captures with initializer expressions.
AbandonedPublic

Authored by curdeius on Sep 18 2017, 8:48 AM.

Details

Reviewers
djasper
klimek
Summary

This fixes bug #19986 (https://bugs.llvm.org/show_bug.cgi?id=19986).
The code was incorrectly formatted to (mind additional spaces inside brackets) when lambda capture contained an initializer expression.
This patch does not handle all possible initializers, but the most common once.
To handle these resting cases, we'd have to parse the whole initializer expressions or handle lambda detection differently.

Before:

int i = 100;
auto f1 = [ i, value = 23 ]() { return i + value; };
auto f2 = [ i, value{23} ]() { return i + value; };

After:

int i = 100;
auto f1 = [i, value = 23]() { return i + value; };
auto f2 = [i, value{23}]() { return i + value; };

Event Timeline

curdeius created this revision.Sep 18 2017, 8:48 AM
curdeius updated this revision to Diff 115814.Sep 19 2017, 2:58 AM

Minor: use FormatToken::isNot instead of !FormatToken::is.

klimek edited edge metadata.Sep 19 2017, 3:02 AM

Thanks for the patch and the discussion around this. I fixed this in r313622 in what I think is a more principled approach that also works for nested lambdas (and gets rid of a lot of now-obsolete code).
The big problem with this code was that it evolved a bit to the point where we were able to generalize it, but we never did. Let me know if you find any problems with the aforementioned change.

curdeius abandoned this revision.Sep 19 2017, 7:25 AM

Ok. Nice patch. You can close https://bugs.llvm.org/show_bug.cgi?id=19986 now.