llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Christophe Calmejane (christophe-calmejane)

<details>
<summary>Changes</summary>

Fix for #<!-- -->182274

---
Full diff: https://github.com/llvm/llvm-project/pull/182319.diff


2 Files Affected:

- (modified) clang/lib/Format/ContinuationIndenter.cpp (+7) 
- (modified) clang/unittests/Format/FormatTest.cpp (+58) 


``````````diff
diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index f1edd71df73fa..f097704fa8720 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -767,6 +767,13 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState 
&State, bool DryRun,
       return false;
     }
 
+    // When BeforeLambdaBody is set, lambda bodies are intentionally expanded
+    // onto their own lines. Do not prevent line breaks in the parent scope,
+    // as that would force all arguments (including those before the lambda)
+    // to be wrapped to new lines.
+    if (Style.BraceWrapping.BeforeLambdaBody)
+      return false;
+
     // For example, `/*Newline=*/false`.
     if (Previous.is(TT_BlockComment) && Current.SpacesRequiredBefore == 0)
       return false;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index d6c49163abbc9..5e56fce1f8d8c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24325,6 +24325,64 @@ TEST_F(FormatTest, FormatsLambdas) {
                "  })));\n"
                "}",
                Style);
+
+  // Verify that BeforeLambdaBody does not cause arguments before the lambda to
+  // be wrapped to a new line when the lambda is not the last argument.
+  FormatStyle BeforeLambdaBodyStyle = getLLVMStyle();
+  BeforeLambdaBodyStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
+  BeforeLambdaBodyStyle.BraceWrapping.BeforeLambdaBody = true;
+  BeforeLambdaBodyStyle.AllowShortLambdasOnASingleLine =
+      FormatStyle::ShortLambdaStyle::SLS_None;
+
+  // Lambda followed by another argument: args before lambda stay on first 
line.
+  verifyFormat("void test() {\n"
+               "  func(a, b,\n"
+               "       [](int x)\n"
+               "       {\n"
+               "         return x;\n"
+               "       },\n"
+               "       c);\n"
+               "}",
+               BeforeLambdaBodyStyle);
+  // Two lambdas as arguments: args before lambdas stay on first line.
+  verifyFormat("void test() {\n"
+               "  func(a, b,\n"
+               "       [](int x)\n"
+               "       {\n"
+               "         return x;\n"
+               "       },\n"
+               "       [](int y)\n"
+               "       {\n"
+               "         return y;\n"
+               "       });\n"
+               "}",
+               BeforeLambdaBodyStyle);
+
+  BeforeLambdaBodyStyle.AllowShortLambdasOnASingleLine =
+      FormatStyle::ShortLambdaStyle::SLS_Empty;
+  // With SLS_Empty, lambda followed by another argument.
+  verifyFormat("void test() {\n"
+               "  func(a, b,\n"
+               "       [](int x)\n"
+               "       {\n"
+               "         return x;\n"
+               "       },\n"
+               "       c);\n"
+               "}",
+               BeforeLambdaBodyStyle);
+  // With SLS_Empty, two lambdas as arguments.
+  verifyFormat("void test() {\n"
+               "  func(a, b,\n"
+               "       [](int x)\n"
+               "       {\n"
+               "         return x;\n"
+               "       },\n"
+               "       [](int y)\n"
+               "       {\n"
+               "         return y;\n"
+               "       });\n"
+               "}",
+               BeforeLambdaBodyStyle);
 }
 
 TEST_F(FormatTest, LambdaWithLineComments) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/182319
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to