Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format] Add new option: WrapNamespaceBodyWithNewlines #106145

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6652,6 +6652,48 @@ the configuration (without a prefix: ``Auto``).

For example: BOOST_PP_STRINGIZE

.. _WrapNamespaceBodyWithEmptyLines:

**WrapNamespaceBodyWithEmptyLines** (``WrapNamespaceBodyWithEmptyLinesStyle``) :versionbadge:`clang-format 20` :ref:`¶ <WrapNamespaceBodyWithEmptyLines>`
Controls number of empty lines at the begging and at the end of
namespace definition.

Possible values:

* ``WNBWELS_Never`` (in configuration: ``Never``)
Removes all empty lines at the beginning and at the end of
namespace definition.

.. code-block:: c++

namespace N1 {
namespace N2
function();
}
}

* ``WNBWELS_Always`` (in configuration: ``Always``)
Always adds an empty line at the beginning and at the end of
namespace definition. MaxEmptyLinesToKeep is also applied, but
empty lines between consecutive namespace declarations are
always removed.

.. code-block:: c++

namespace N1 {
namespace N2 {

function();

}
}

* ``WNBWELS_Leave`` (in configuration: ``Leave``)
Keeps existing newlines at the beginning and at the end of
namespace definition using MaxEmptyLinesToKeep for formatting.



.. END_FORMAT_STYLE_OPTIONS

Adding additional style options
Expand Down
40 changes: 39 additions & 1 deletion clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -5057,6 +5057,43 @@ struct FormatStyle {
/// \version 11
std::vector<std::string> WhitespaceSensitiveMacros;

/// Different styles for modify number of empty lines in
/// the beginning and at the of end of namespaces.
enum WrapNamespaceBodyWithEmptyLinesStyle : int8_t {
/// Removes all empty lines at the beginning and at the end of
/// namespace definition.
/// \code
/// namespace N1 {
/// namespace N2
/// function();
/// }
/// }
/// \endcode
WNBWELS_Never,
/// Always adds an empty line at the beginning and at the end of
/// namespace definition. MaxEmptyLinesToKeep is also applied, but
/// empty lines between consecutive namespace declarations are
/// always removed.
/// \code
/// namespace N1 {
/// namespace N2 {
///
/// function();
///
/// }
/// }
/// \endcode
WNBWELS_Always,
/// Keeps existing newlines at the beginning and at the end of
/// namespace definition using MaxEmptyLinesToKeep for formatting.
WNBWELS_Leave
};

/// Controls number of empty lines at the begging and at the end of
/// namespace definition.
/// \version 20
WrapNamespaceBodyWithEmptyLinesStyle WrapNamespaceBodyWithEmptyLines;

bool operator==(const FormatStyle &R) const {
return AccessModifierOffset == R.AccessModifierOffset &&
AlignAfterOpenBracket == R.AlignAfterOpenBracket &&
Expand Down Expand Up @@ -5234,7 +5271,8 @@ struct FormatStyle {
TypenameMacros == R.TypenameMacros && UseTab == R.UseTab &&
VerilogBreakBetweenInstancePorts ==
R.VerilogBreakBetweenInstancePorts &&
WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros;
WhitespaceSensitiveMacros == R.WhitespaceSensitiveMacros &&
WrapNamespaceBodyWithEmptyLines == R.WrapNamespaceBodyWithEmptyLines;
}

std::optional<FormatStyle> GetLanguageStyle(LanguageKind Language) const;
Expand Down
15 changes: 15 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,18 @@ template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
}
};

template <>
dmasloff marked this conversation as resolved.
Show resolved Hide resolved
struct ScalarEnumerationTraits<
FormatStyle::WrapNamespaceBodyWithEmptyLinesStyle> {
static void
enumeration(IO &IO,
FormatStyle::WrapNamespaceBodyWithEmptyLinesStyle &Value) {
IO.enumCase(Value, "Never", FormatStyle::WNBWELS_Never);
IO.enumCase(Value, "Always", FormatStyle::WNBWELS_Always);
IO.enumCase(Value, "Leave", FormatStyle::WNBWELS_Leave);
}
};

template <> struct MappingTraits<FormatStyle> {
static void mapping(IO &IO, FormatStyle &Style) {
// When reading, read the language first, we need it for getPredefinedStyle.
Expand Down Expand Up @@ -1154,6 +1166,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.VerilogBreakBetweenInstancePorts);
IO.mapOptional("WhitespaceSensitiveMacros",
Style.WhitespaceSensitiveMacros);
IO.mapOptional("WrapNamespaceBodyWithEmptyLines",
Style.WrapNamespaceBodyWithEmptyLines);

// If AlwaysBreakAfterDefinitionReturnType was specified but
// BreakAfterReturnType was not, initialize the latter from the former for
Expand Down Expand Up @@ -1623,6 +1637,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.WhitespaceSensitiveMacros.push_back("NS_SWIFT_NAME");
LLVMStyle.WhitespaceSensitiveMacros.push_back("PP_STRINGIZE");
LLVMStyle.WhitespaceSensitiveMacros.push_back("STRINGIZE");
LLVMStyle.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave;

LLVMStyle.PenaltyBreakAssignment = prec::Assignment;
LLVMStyle.PenaltyBreakBeforeFirstCallParameter = 19;
Expand Down
16 changes: 16 additions & 0 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,22 @@ static auto computeNewlines(const AnnotatedLine &Line,
Newlines = 1;
}

if (Style.WrapNamespaceBodyWithEmptyLines != FormatStyle::WNBWELS_Leave) {
// Modify empty lines after TT_NamespaceLBrace.
if (PreviousLine && PreviousLine->endsWith(TT_NamespaceLBrace)) {
if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never)
Newlines = 1;
else if (!Line.startsWithNamespace())
Newlines = std::max(Newlines, 2u);
}
// Modify empty lines before TT_NamespaceRBrace.
if (Line.startsWith(TT_NamespaceRBrace)) {
if (Style.WrapNamespaceBodyWithEmptyLines == FormatStyle::WNBWELS_Never)
Newlines = 1;
else if (!PreviousLine->startsWith(TT_NamespaceRBrace))
Newlines = std::max(Newlines, 2u);
}

// Insert or remove empty line before access specifiers.
if (PreviousLine && RootToken.isAccessSpecifier()) {
switch (Style.EmptyLineBeforeAccessModifier) {
Expand Down
8 changes: 8 additions & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,14 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("SortUsingDeclarations: true", SortUsingDeclarations,
FormatStyle::SUD_LexicographicNumeric);

Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Leave;
CHECK_PARSE("WrapNamespaceBodyWithEmptyLines: Never",
WrapNamespaceBodyWithEmptyLines, FormatStyle::WNBWELS_Never);
CHECK_PARSE("WrapNamespaceBodyWithEmptyLines: Always",
WrapNamespaceBodyWithEmptyLines, FormatStyle::WNBWELS_Always);
CHECK_PARSE("WrapNamespaceBodyWithEmptyLines: Leave",
WrapNamespaceBodyWithEmptyLines, FormatStyle::WNBWELS_Leave);

// FIXME: This is required because parsing a configuration simply overwrites
// the first N elements of the list instead of resetting it.
Style.ForEachMacros.clear();
Expand Down
132 changes: 132 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28104,6 +28104,138 @@ TEST_F(FormatTest, BreakBinaryOperations) {
Style);
}

TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) {
auto Style = getLLVMStyle();
Style.FixNamespaceComments = false;
Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Never;

// Empty namespace.
verifyFormat("namespace N {}", Style);

// Single namespace.
verifyFormat("namespace N {\n"
"int f1(int a) { return 2 * a; }\n"
"}",
Style);

// Nested namespace.
verifyFormat("namespace N1 {\n"
"namespace N2 {\n"
"int a = 1;\n"
"}\n"
"}",
Style);

Style.CompactNamespaces = true;

verifyFormat("namespace N1 { namespace N2 {\n"
"int a = 1;\n"
"}}",
Style);

// Removing empty lines.
verifyFormat("namespace N {\n"
"\n"
"int a = 1;\n"
"\n"
Comment on lines +28138 to +28140
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO Never should remove all empty lines regardless MaxEmptyLinesToKeep.

"}",
"namespace N {\n"
"\n"
"\n"
"int a = 1;\n"
"\n"
"\n"
"}",
Style);

Style.MaxEmptyLinesToKeep = 0;

verifyFormat("namespace N {\n"
"int a = 1;\n"
"}",
"namespace N {\n"
"\n"
"\n"
"int a = 1;\n"
"\n"
"\n"
"}",
Style);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please modify the above tests to test removing empty lines, e.g.:

  verifyFormat("namespace N {\n"
               "int f1(int a) { return 2 * a; }\n"
               "}",
               "namespace N {\n"
               "\n"
               "int f1(int a) { return 2 * a; }\n"
               "\n"
               "}",
               Style);


TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make similar changes where applicable.

auto Style = getLLVMStyle();
Style.FixNamespaceComments = false;
Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always;

// Empty namespace.
verifyFormat("namespace N {}", Style);

// Single namespace.
verifyFormat("namespace N {\n"
"\n"
"int f1(int a) { return 2 * a; }\n"
"\n"
"}",
Style);

// Nested namespace.
verifyFormat("namespace N1 {\n"
"namespace N2 {\n"
"\n"
"int a = 1;\n"
"\n"
"}\n"
"}",
Style);

Style.CompactNamespaces = true;

// Nested namespace.
verifyFormat("namespace N1 { namespace N2 {\n"
"\n"
"int a = 1;\n"
"\n"
"}}",
Style);

Style.MaxEmptyLinesToKeep = 2;

// Nested namespace.
verifyNoChange("namespace N1 { namespace N2 {\n"
"\n"
"\n"
"int a = 1;\n"
"\n"
"\n"
"}}",
Style);

Style.CompactNamespaces = false;

// Single namespace.
verifyNoChange("namespace N {\n"
"\n"
"\n"
"int a = 1;\n"
"\n"
"\n"
"}",
Style);

// Nested namespace.
verifyNoChange("namespace N1 {\n"
"namespace N2 {\n"
"\n"
"\n"
"int a = 1;\n"
"\n"
"\n"
"}\n"
"}",
Style);
}

} // namespace
} // namespace test
} // namespace format
Expand Down
Loading