[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
Mon May 24 14:42:05 PDT 2021


aybassiouny updated this revision to Diff 347504.
aybassiouny added a comment.

Add an assert


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
@@ -3493,8 +3493,12 @@
   return true;
 }
 static bool isAllmanLambdaBrace(const FormatToken &Tok) {
+  assert(!Tok.is(tok::l_brace) || !Tok.is(BK_Block) ||
+         !Tok.isOneOf(TT_ObjCBlockLBrace, TT_DictLiteral) ||
+         (Tok.Previous && Tok.Previous->Previous));
   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.347504.patch
Type: text/x-patch
Size: 2020 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210524/ec354673/attachment.bin>


More information about the cfe-commits mailing list