[PATCH] D93938: [clang-format] Fixed AfterEnum handling

Ally Tiritoglu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Mar 13 23:51:03 PST 2021


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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93938.330495.patch
Type: text/x-patch
Size: 2382 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210314/18b51d8c/attachment-0001.bin>


More information about the cfe-commits mailing list