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

Ahmed Mahdy via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 18 17:46:13 PDT 2021


aybassiouny updated this revision to Diff 338403.
aybassiouny edited the summary of this revision.
aybassiouny added a comment.

After rechecking, turns out `AllowShortLambdasOnASingleLine` and `BeforeLambdaBody` both need to be turned on in order for the regression to be expressed, this affects the UT.

Also added `verifyFormat` check as suggested, it does not fail btw without this patch.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D99031/new/

https://reviews.llvm.org/D99031

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2589,6 +2589,25 @@
                    Style));
 }
 
+TEST_F(FormatTest, FormatsCompactNamespacesLambdaRegression) {
+  // Make sure compact namespaces are not confused with lambdas
+  FormatStyle CompactNamespacesStyle{getLLVMStyle()};
+  CompactNamespacesStyle.CompactNamespaces = true;
+  CompactNamespacesStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None;
+  CompactNamespacesStyle.BreakBeforeBraces = FormatStyle::BS_Custom;
+  CompactNamespacesStyle.BraceWrapping.BeforeLambdaBody = true;
+  verifyFormat("namespace out { namespace in {\n"
+               "}} // namespace out::in",
+               CompactNamespacesStyle);
+  EXPECT_EQ("namespace out { namespace in {\n"
+            "}} // namespace out::in",
+            format("namespace out {\n"
+                   "namespace in {\n"
+                   "} // namespace in\n"
+                   "} // namespace out",
+                   CompactNamespacesStyle));
+}
+
 TEST_F(FormatTest, FormatsExternC) {
   verifyFormat("extern \"C\" {\nint a;");
   verifyFormat("extern \"C\" {}");
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3494,7 +3494,8 @@
 }
 static bool isAllmanLambdaBrace(const FormatToken &Tok) {
   return (Tok.is(tok::l_brace) && Tok.is(BK_Block) &&
-          !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral));
+          !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral) &&
+          !Tok.Previous->Previous->is(tok::kw_namespace));
 }
 
 static bool isAllmanBraceIncludedBreakableLambda(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99031.338403.patch
Type: text/x-patch
Size: 1832 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210419/fa6954ed/attachment-0001.bin>


More information about the cfe-commits mailing list