[PATCH] D135918: [clang-format] Fix lambda formatting in conditional

Björn Schäpers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 13 14:19:06 PDT 2022


HazardyKnusperkeks created this revision.
HazardyKnusperkeks added reviewers: MyDeveloperDay, owenpan, curdeius, rymiel.
HazardyKnusperkeks added a project: clang-format.
Herald added a project: All.
HazardyKnusperkeks requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Without the patch UnwrappedLineFormatter::analyzeSolutionSpace just ran out of possible formattings and would put everything just on one line. The problem was that the the line break was forbidden, but putting the conditional colon on the same line is also forbidden.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135918

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -26379,6 +26379,33 @@
                Style);
 }
 
+TEST_F(FormatTest, MultilineLambdaInConditional) {
+  auto Style = getLLVMStyleWithColumns(70);
+  verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak ? []() {\n"
+               "  ;\n"
+               "  return 5;\n"
+               "}()\n"
+               "                                                     : 2;",
+               Style);
+
+  Style = getLLVMStyleWithColumns(60);
+  verifyFormat("auto aLengthyIdentifier = oneExpressionSoThatWeBreak\n"
+               "                              ? []() {\n"
+               "                                  ;\n"
+               "                                  return 5;\n"
+               "                                }()\n"
+               "                              : 2;",
+               Style);
+
+  Style = getLLVMStyleWithColumns(40);
+  verifyFormat("auto aLengthyIdentifier =\n"
+               "    oneExpressionSoThatWeBreak ? []() {\n"
+               "      ;\n"
+               "      return 5;\n"
+               "    }()\n"
+               "                               : 2;", Style);
+}
+
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndent) {
   auto Style = getLLVMStyle();
 
Index: clang/lib/Format/ContinuationIndenter.cpp
===================================================================
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -1419,9 +1419,14 @@
       Previous && Previous->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
       !Previous->is(TT_DictLiteral) && State.Stack.size() > 1 &&
       !CurrentState.HasMultipleNestedBlocks) {
-    if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline)
+    if (State.Stack[State.Stack.size() - 2].NestedBlockInlined && Newline &&
+        // Do not forbid line breaks for directly invoced lambdas.
+        (!Current.MatchingParen ||
+         Current.MatchingParen->isNot(TT_LambdaLBrace) || !Current.Next ||
+         Current.Next->isNot(tok::l_paren))) {
       for (ParenState &PState : llvm::drop_end(State.Stack))
         PState.NoLineBreak = true;
+    }
     State.Stack[State.Stack.size() - 2].NestedBlockInlined = false;
   }
   if (Previous && (Previous->isOneOf(TT_BinaryOperator, TT_ConditionalExpr) ||


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135918.467604.patch
Type: text/x-patch
Size: 2506 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221013/6015511a/attachment-0001.bin>


More information about the cfe-commits mailing list