atirit updated this revision to Diff 330495.
atirit added a comment.

Fixed `AfterEnum`'s compatibility with `AllowShortEnumsOnASingleLine`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D93938

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
@@ -13316,6 +13316,7 @@
 TEST_F(FormatTest, AllmanBraceBreaking) {
   FormatStyle AllmanBraceStyle = getLLVMStyle();
   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
+  AllmanBraceStyle.AllowShortEnumsOnASingleLine = false;
 
   EXPECT_EQ("namespace a\n"
             "{\n"
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3662,12 +3662,31 @@
     return true;
   }
 
-  if (isAllmanBrace(Left) || isAllmanBrace(Right))
-    return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
-           (Line.startsWith(tok::kw_typedef, tok::kw_enum) &&
-            Style.BraceWrapping.AfterEnum) ||
+  if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
+    bool lineContainsBreakingTokens = false;
+    FormatToken *breakingSearchToken = Right.Previous;
+    while ((breakingSearchToken = breakingSearchToken->Next)) {
+      bool hasBreakingComma = breakingSearchToken->is(tok::comma) &&
+                              breakingSearchToken->Next->is(tok::r_brace);
+      if (breakingSearchToken->isTrailingComment() || hasBreakingComma) {
+        lineContainsBreakingTokens = true;
+        break;
+      }
+    }
+    bool isAllowedByAfterEnum =
+        (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
+        (Line.startsWith(tok::kw_typedef, tok::kw_enum) &&
+         Style.BraceWrapping.AfterEnum);
+    bool isLineTooBig = (strlen(Right.TokenText.data()) +
+                         Right.OriginalColumn) > Style.ColumnLimit;
+    bool isAllowedByShortEnums = !Style.AllowShortEnumsOnASingleLine ||
+                                 isLineTooBig || lineContainsBreakingTokens;
+    return (isAllowedByAfterEnum &&
+            (isAllowedByShortEnums || lineContainsBreakingTokens)) ||
            (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) 
||
            (Line.startsWith(tok::kw_struct) && 
Style.BraceWrapping.AfterStruct);
+  }
+
   if (Left.is(TT_ObjCBlockLBrace) &&
       Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
     return true;


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -13316,6 +13316,7 @@
 TEST_F(FormatTest, AllmanBraceBreaking) {
   FormatStyle AllmanBraceStyle = getLLVMStyle();
   AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman;
+  AllmanBraceStyle.AllowShortEnumsOnASingleLine = false;
 
   EXPECT_EQ("namespace a\n"
             "{\n"
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -3662,12 +3662,31 @@
     return true;
   }
 
-  if (isAllmanBrace(Left) || isAllmanBrace(Right))
-    return (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
-           (Line.startsWith(tok::kw_typedef, tok::kw_enum) &&
-            Style.BraceWrapping.AfterEnum) ||
+  if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
+    bool lineContainsBreakingTokens = false;
+    FormatToken *breakingSearchToken = Right.Previous;
+    while ((breakingSearchToken = breakingSearchToken->Next)) {
+      bool hasBreakingComma = breakingSearchToken->is(tok::comma) &&
+                              breakingSearchToken->Next->is(tok::r_brace);
+      if (breakingSearchToken->isTrailingComment() || hasBreakingComma) {
+        lineContainsBreakingTokens = true;
+        break;
+      }
+    }
+    bool isAllowedByAfterEnum =
+        (Line.startsWith(tok::kw_enum) && Style.BraceWrapping.AfterEnum) ||
+        (Line.startsWith(tok::kw_typedef, tok::kw_enum) &&
+         Style.BraceWrapping.AfterEnum);
+    bool isLineTooBig = (strlen(Right.TokenText.data()) +
+                         Right.OriginalColumn) > Style.ColumnLimit;
+    bool isAllowedByShortEnums = !Style.AllowShortEnumsOnASingleLine ||
+                                 isLineTooBig || lineContainsBreakingTokens;
+    return (isAllowedByAfterEnum &&
+            (isAllowedByShortEnums || lineContainsBreakingTokens)) ||
            (Line.startsWith(tok::kw_class) && Style.BraceWrapping.AfterClass) ||
            (Line.startsWith(tok::kw_struct) && Style.BraceWrapping.AfterStruct);
+  }
+
   if (Left.is(TT_ObjCBlockLBrace) &&
       Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
     return true;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to