Max_S created this revision.
Max_S requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
The current logic for access modifiers in classes ignores the option
'MaxEmptyLinesToKeep=1'. It is therefore impossible to have a coding style that
requests one empty line after an access modifier. The patch allows the user to
configure how many empty lines clang-format should add after an access
modifier. This will remove lines if there are to many and will add them if
there are missing.
Repository:
rG LLVM Github Monorepo
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,21 @@
Style);
}
+TEST_F(FormatTest, FormatsAfterAccessModifiers) {
+ verifyFormat("class Foo {\n"
+ "private:\n"
+ " int i;\n"
+ "};");
+
+ FormatStyle StyleWithLine = getLLVMStyle();
+ StyleWithLine.EmptyLinesAfterAccessModifier = 1u;
+ verifyFormat("class Foo {\n"
+ "private:\n"
+ "\n"
+ " int i;\n"
+ "};", 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
@@ -1281,7 +1281,7 @@
// Remove empty lines after access specifiers.
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
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits