[clang-format] Allman brace wrapping for lambda

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[clang-format] Allman brace wrapping for lambda

Joerg Sonnenberger via cfe-dev
Hello,

When using allman brace wrapping some lambdas are not properly format, for example:
[]()
{
  return 42;
};
Will be correctly formatted but
foo([]()
{
  return 42;
});
will be formatted to
foo([]() { return 42; });
which is clearly incorrect (AllowShortBlocksOnASingleLine is disabled).

After looking at the code a little bit it seems that when a lambda is outside of a parenthesis it will not be parsed as a lambda (with UnwrappedLineParser::tryToParseLambda)  but like a function (in UnwrappedLineParser::parseStructuralElement line 1235-1245) which will call addUnwrappedLine() and thus correctly respect the allman brace wrapping. Instead when inside a parenthesis the lambda is "correctly" parsed as a lambda (in UnwrappedLineParser::tryToParseLambda) but no addUnwrappedLine will be called resulting in only one UnwrappedLine.
My first idea was to call addUnwrappedLine inside UnwrappedLineParser::tryToParseLambda but this will fail when the TokenAnnotator will try to parse the line as the opening parenthesis  won't be matched and finally the line will be marked as LT_Invalid and will never be formatted :/
My second idea was to modify TokenAnnotator::mustBreakBefore in order to detect lambda brace and set MustBreakBefore property to true for both braces of the lambda, it works kind of well but it won't support merging simple block into one line (as this is only done if we have multiple UnwrappedLine) :/

So can anyone give some advices on how to properly fix/implement this ?

Also I talk about lambdas but the same issue arise for every blocks that are inside parenthesis, for example:
FOO(if (a) { return true; })
won't be correctly formatted too.

Regards,
Fx

PS: bugs related to this issue
https://bugs.llvm.org/show_bug.cgi?id=27640
https://bugs.llvm.org//show_bug.cgi?id=32151
https://bugs.llvm.org//show_bug.cgi?id=23637

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev