[PATCH] D115738: [clang-format] Code following C# Lambda Expressions has wrong formatting

Peter Stys via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 14 08:52:54 PST 2021


peterstys created this revision.
peterstys added a reviewer: MyDeveloperDay.
peterstys requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

The alignment fix introduced by https://reviews.llvm.org/D104388 caused a regression whereby formatting of code that follows the lambda block is incorrect i.e. separate expressions are put on the same line.

For example:

  public void Test() {
    while (true) {
      preBindEnumerators.RemoveAll(enumerator => !enumerator.MoveNext());
      CodeThatFollowsLambda();
      IsWellAligned(); 
  }

will be formatted as:

  public void Test() {
    while (true) {
      preBindEnumerators.RemoveAll(enumerator => !enumerator.MoveNext());
      CodeThatFollowsLambda(); IsWellAligned(); 
  }

The problem is that the "Fat arrow" token parsing inside the parseParens() function uses parseStructuralElement() which does not like to be called inside the parenthesis. It seems that code that follows is considered part of the parenthesis block.

As a fix, parseStructuralElement parser inside the parseParens() was replaced with parseChildBlock() if the following token was a "left brace" and nextToken() otherwise. Added few new unit tests to demonstrate the regressions.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115738

Files:
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/FormatTestCSharp.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115738.394274.patch
Type: text/x-patch
Size: 4624 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211214/f0d74ef6/attachment-0001.bin>


More information about the cfe-commits mailing list