Max_S created this revision.
Max_S requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Checks for newlines in option Style.EmptyLineBeforeAccessModifier are
now based on the formatted new lines and not on the new lines in the
file.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99503

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

Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -8889,6 +8889,8 @@
 
 TEST_F(FormatTest, FormatsAccessModifiers) {
   FormatStyle Style = getLLVMStyle();
+  FormatStyle NoEmptyLines = getLLVMStyle();
+  NoEmptyLines.MaxEmptyLinesToKeep = 0;
   EXPECT_EQ(Style.EmptyLineBeforeAccessModifier,
             FormatStyle::ELBAMS_LogicalBlock);
   verifyFormat("struct foo {\n"
@@ -8940,7 +8942,20 @@
                "  int j;\n"
                "};\n",
                Style);
+  verifyFormat("struct foo {\n"
+               "private:\n"
+               "  void f() {}\n"
+               "\n"
+               "private:\n"
+               "  int i;\n"
+               "\n"
+               "public:\n"
+               "protected:\n"
+               "  int j;\n"
+               "};\n",
+               NoEmptyLines);
   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
+  NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
   verifyFormat("struct foo {\n"
                "private:\n"
                "  void f() {}\n"
@@ -8950,6 +8965,16 @@
                "  int j;\n"
                "};\n",
                Style);
+  verifyFormat("struct foo {\n"
+               "private:\n"
+               "  void f() {}\n"
+               "private:\n"
+               "  int i;\n"
+               "public:\n"
+               "protected:\n"
+               "  int j;\n"
+               "};\n",
+               NoEmptyLines);
   verifyFormat("struct foo {\n"
                "private:\n"
                "  void f() {}\n"
@@ -9011,6 +9036,7 @@
                "};\n",
                Style);
   Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
+  NoEmptyLines.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always;
   verifyFormat("struct foo {\n"
                "private:\n"
                "  void f() {}\n"
@@ -9022,6 +9048,18 @@
                "  int j;\n"
                "};\n",
                Style);
+  verifyFormat("struct foo {\n"
+               "private:\n"
+               "  void f() {}\n"
+               "\n"
+               "private:\n"
+               "  int i;\n"
+               "\n"
+               "public:\n"
+               "protected:\n"
+               "  int j;\n"
+               "};\n",
+               NoEmptyLines);
   verifyFormat("struct foo {\n"
                "private:\n"
                "  void f() {}\n"
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1254,7 +1254,7 @@
   if (PreviousLine && RootToken.isAccessSpecifier()) {
     switch (Style.EmptyLineBeforeAccessModifier) {
     case FormatStyle::ELBAMS_Never:
-      if (RootToken.NewlinesBefore > 1)
+      if (Newlines > 1)
         Newlines = 1;
       break;
     case FormatStyle::ELBAMS_Leave:
@@ -1262,7 +1262,7 @@
       break;
     case FormatStyle::ELBAMS_LogicalBlock:
       if (PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
-          RootToken.NewlinesBefore <= 1)
+          Newlines <= 1)
         Newlines = 2;
       break;
     case FormatStyle::ELBAMS_Always: {
@@ -1272,7 +1272,7 @@
       else
         previousToken = PreviousLine->Last;
       if ((!previousToken || !previousToken->is(tok::l_brace)) &&
-          RootToken.NewlinesBefore <= 1)
+          Newlines <= 1)
         Newlines = 2;
     } break;
     }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D99503: Fixes for bug... Max Sagebaum via Phabricator via cfe-commits

Reply via email to