Max_S updated this revision to Diff 329574.
Max_S added a comment.

Updating D98237 <https://reviews.llvm.org/D98237>: [clang-format] Option for 
empty lines after an access modifier.

- Added additional verification tests
- Added expected formating tests
- Changed comment to reflect the new logic


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D98237

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  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
@@ -9179,6 +9179,48 @@
                Style);
 }
 
+TEST_F(FormatTest, FormatsAfterAccessModifiers) {
+  char const* const test0NL =
+               "class Foo {\n"
+               "private: int i;\n"
+               "};";
+  char const* const test1NL =
+               "class Foo {\n"
+               "private:\n"
+               "  int i;\n"
+               "};";
+  char const* const test2NL =
+               "class Foo {\n"
+               "private:\n"
+               "\n"
+               "  int i;\n"
+               "};";
+  char const* const test3NL =
+               "class Foo {\n"
+               "private:\n"
+               "\n"
+               "\n"
+               "  int i;\n"
+               "};";
+  EXPECT_EQ(test1NL, format(test0NL));
+  verifyFormat(test1NL);
+  EXPECT_EQ(test1NL, format(test2NL));
+  EXPECT_EQ(test1NL, format(test3NL));
+
+  FormatStyle StyleWithLine = getLLVMStyle();
+  StyleWithLine.EmptyLinesAfterAccessModifier = 1u;
+  EXPECT_EQ(test2NL, format(test0NL, StyleWithLine));
+  EXPECT_EQ(test2NL, format(test1NL, StyleWithLine));
+  verifyFormat(test2NL, StyleWithLine);
+  EXPECT_EQ(test2NL, format(test3NL, StyleWithLine));
+
+  StyleWithLine.EmptyLinesAfterAccessModifier = 2u;
+  EXPECT_EQ(test3NL, format(test0NL, StyleWithLine));
+  EXPECT_EQ(test3NL, format(test1NL, StyleWithLine));
+  EXPECT_EQ(test3NL, format(test2NL, StyleWithLine));
+  verifyFormat(test3NL, StyleWithLine);
+}
+
 TEST_F(FormatTest, FormatsArrays) {
   verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaa[aaaaaaaaaaaaaaaaaaaaaaaaa]\n"
                "                         [bbbbbbbbbbbbbbbbbbbbbbbbb] = c;");
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1278,10 +1278,10 @@
     }
   }
 
-  // Remove empty lines after access specifiers.
+  // Force the configured amount of new lines after an access specifier
   if (PreviousLine && PreviousLine->First->isAccessSpecifier() &&
       (!PreviousLine->InPPDirective || !RootToken.HasUnescapedNewline))
-    Newlines = std::min(1u, Newlines);
+    Newlines = Style.EmptyLinesAfterAccessModifier + 1u;
 
   if (Newlines)
     Indent = NewlineIndent;
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -586,6 +586,8 @@
     IO.mapOptional("DisableFormat", Style.DisableFormat);
     IO.mapOptional("EmptyLineBeforeAccessModifier",
                    Style.EmptyLineBeforeAccessModifier);
+    IO.mapOptional("EmptyLinesAfterAccessModifier",
+                   Style.EmptyLinesAfterAccessModifier);
     IO.mapOptional("ExperimentalAutoDetectBinPacking",
                    Style.ExperimentalAutoDetectBinPacking);
     IO.mapOptional("FixNamespaceComments", Style.FixNamespaceComments);
@@ -975,6 +977,7 @@
   LLVMStyle.DeriveLineEnding = true;
   LLVMStyle.DerivePointerAlignment = false;
   LLVMStyle.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
+  LLVMStyle.EmptyLinesAfterAccessModifier = 0u;
   LLVMStyle.ExperimentalAutoDetectBinPacking = false;
   LLVMStyle.FixNamespaceComments = true;
   LLVMStyle.ForEachMacros.push_back("foreach");
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ clang/include/clang/Format/Format.h
@@ -1953,6 +1953,9 @@
   /// Defines in which cases to put empty line before access modifiers.
   EmptyLineBeforeAccessModifierStyle EmptyLineBeforeAccessModifier;
 
+  /// Defines how many lines are put after access modifiers.
+  unsigned EmptyLinesAfterAccessModifier;
+
   /// If ``true``, clang-format detects whether function calls and
   /// definitions are formatted with one parameter per line.
   ///
@@ -3201,6 +3204,7 @@
            DerivePointerAlignment == R.DerivePointerAlignment &&
            DisableFormat == R.DisableFormat &&
            EmptyLineBeforeAccessModifier == R.EmptyLineBeforeAccessModifier &&
+           EmptyLinesAfterAccessModifier == R.EmptyLinesAfterAccessModifier &&
            ExperimentalAutoDetectBinPacking ==
                R.ExperimentalAutoDetectBinPacking &&
            FixNamespaceComments == R.FixNamespaceComments &&
Index: clang/docs/ClangFormatStyleOptions.rst
===================================================================
--- clang/docs/ClangFormatStyleOptions.rst
+++ clang/docs/ClangFormatStyleOptions.rst
@@ -2195,7 +2195,17 @@
       protected:
       };
 
+**EmptyLinesAfterAccessModifier** (``unsigned``)
+  Defines how many lines are put after access modifiers.
 
+  .. code-block:: c++
+
+     0:                                     1:
+     struct Foo {                   vs.     struct Foo {
+     private:                               private:
+       int x;
+     };                                       int x;
+                                            };
 
 **ExperimentalAutoDetectBinPacking** (``bool``)
   If ``true``, clang-format detects whether function calls and
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to