[clang] 6ea3d9e - [clang-format] Fix CompactNamespaces corner case when AllowShortLambdasOnASingleLine/BraceWrapping.BeforeLambdaBody are set

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 14 12:47:18 PST 2022


Author: Marek Kurdej
Date: 2022-01-14T21:47:16+01:00
New Revision: 6ea3d9efc53641d930e9176916ff09ca6f73aef2

URL: https://github.com/llvm/llvm-project/commit/6ea3d9efc53641d930e9176916ff09ca6f73aef2
DIFF: https://github.com/llvm/llvm-project/commit/6ea3d9efc53641d930e9176916ff09ca6f73aef2.diff

LOG: [clang-format] Fix CompactNamespaces corner case when AllowShortLambdasOnASingleLine/BraceWrapping.BeforeLambdaBody are set

In clang-format 12, `CompactNamespaces` misformatted the code when `AllowShortLambdasOnASingleLine` is set to false and `BraceWrapping.BeforeLambdaBody` is true.

Input:
```
namespace out {
namespace in {
}
} // namespace out::in
```

Expected output:
```
namespace out { namespace in {
}} // namespace out::in
```

Output from v12:
```
namespace out {
namespace in {
}
} // namespace out::in
```

Config triggering the issue:
```
---
AllowShortLambdasOnASingleLine: None
BraceWrapping:
  BeforeLambdaBody :    true
BreakBeforeBraces: Custom
CompactNamespaces: true
...
```

Seems there's a corner case when `AllowShortLambdasOnASingleLine` is false, and `BraceWrapping.BeforeLambdaBody` is true, that causes CompactNamespaces to stop working.
The cause was a misannotation of `{` opening brace after `namespace` as a lambda opening brace.
The regression was probably introduced with [this commit](https://github.com/llvm/llvm-project/commit/fa0118e6e588fe303b08e7e06ba28ac1f8d50c68).

Originally contributed by Ahmed Mahdy (@aybassiouny). Thank you!

Reviewed By: Wawha, HazardyKnusperkeks

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

Added: 
    

Modified: 
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index d6e1f73410302..eb6975bbe77e8 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3830,6 +3830,21 @@ TEST_F(FormatTest, FormatsCompactNamespaces) {
                    "} // namespace mid\n"
                    "} // namespace out",
                    Style));
+
+  Style.CompactNamespaces = true;
+  Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.BeforeLambdaBody = true;
+  verifyFormat("namespace out { namespace in {\n"
+               "}} // namespace out::in",
+               Style);
+  EXPECT_EQ("namespace out { namespace in {\n"
+            "}} // namespace out::in",
+            format("namespace out {\n"
+                   "namespace in {\n"
+                   "} // namespace in\n"
+                   "} // namespace out",
+                   Style));
 }
 
 TEST_F(FormatTest, FormatsExternC) {


        


More information about the cfe-commits mailing list