[clang] 5b8ffb4 - [clang-format] [PR45791] BeforeLambdaBody is confused by comment inside lambda

via cfe-commits cfe-commits at lists.llvm.org
Thu May 7 11:54:25 PDT 2020


Author: mydeveloperday
Date: 2020-05-07T19:53:56+01:00
New Revision: 5b8ffb414200aa65ab26b16415a98a63d81c14ca

URL: https://github.com/llvm/llvm-project/commit/5b8ffb414200aa65ab26b16415a98a63d81c14ca
DIFF: https://github.com/llvm/llvm-project/commit/5b8ffb414200aa65ab26b16415a98a63d81c14ca.diff

LOG: [clang-format] [PR45791] BeforeLambdaBody is confused by comment inside lambda

Summary:
https://bugs.llvm.org/show_bug.cgi?id=45791

Lambda with line comment is incorrectly formatted

```
auto k = []() // comment
{ return; };
````

```
auto k = []() // comment { return; };
```

Reviewed By: Wawha

Subscribers: cfe-commits

Tags: #clang, #clang-format

Differential Revision: https://reviews.llvm.org/D79320

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 5fdb1dbc433c..cf885240a4f2 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -330,7 +330,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
   const FormatToken &Current = *State.NextToken;
   const FormatToken &Previous = *Current.Previous;
   if (Style.BraceWrapping.BeforeLambdaBody && Current.CanBreakBefore &&
-      Current.is(TT_LambdaLBrace)) {
+      Current.is(TT_LambdaLBrace) && Previous.isNot(TT_LineComment)) {
     auto LambdaBodyLength = getLengthToMatchingParen(Current, State.Stack);
     return (LambdaBodyLength > getColumnLimit(State));
   }

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index aba209b4252d..86eeb2705595 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -14587,6 +14587,30 @@ TEST_F(FormatTest, FormatsLambdas) {
                LLVMWithBeforeLambdaBody);
 }
 
+TEST_F(FormatTest, LambdaWithLineComments) {
+  FormatStyle LLVMWithBeforeLambdaBody = getLLVMStyle();
+  LLVMWithBeforeLambdaBody.BreakBeforeBraces = FormatStyle::BS_Custom;
+  LLVMWithBeforeLambdaBody.BraceWrapping.BeforeLambdaBody = true;
+  LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
+      FormatStyle::ShortLambdaStyle::SLS_All;
+
+  verifyFormat("auto k = []() { return; }", LLVMWithBeforeLambdaBody);
+  verifyFormat("auto k = []() // comment\n"
+               "{ return; }",
+               LLVMWithBeforeLambdaBody);
+  verifyFormat("auto k = []() /* comment */ { return; }",
+               LLVMWithBeforeLambdaBody);
+  verifyFormat("auto k = []() /* comment */ /* comment */ { return; }",
+               LLVMWithBeforeLambdaBody);
+  verifyFormat("auto k = []() // X\n"
+               "{ return; }",
+               LLVMWithBeforeLambdaBody);
+  verifyFormat(
+      "auto k = []() // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"
+      "{ return; }",
+      LLVMWithBeforeLambdaBody);
+}
+
 TEST_F(FormatTest, EmptyLinesInLambdas) {
   verifyFormat("auto lambda = []() {\n"
                "  x(); //\n"


        


More information about the cfe-commits mailing list