Skip to content
Merged
42 changes: 42 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6843,6 +6843,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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ clang-format
- Adds ``AllowShortNamespacesOnASingleLine`` option.
- Adds ``VariableTemplates`` option.
- Adds support for bash globstar in ``.clang-format-ignore``.
- Adds ``WrapNamespaceBodyWithEmptyLines`` option.

libclang
--------
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 @@ -5143,6 +5143,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 @@ -5326,7 +5363,8 @@ struct FormatStyle {
UseTab == R.UseTab && VariableTemplates == R.VariableTemplates &&
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 @@ -839,6 +839,18 @@ template <> struct ScalarEnumerationTraits<FormatStyle::UseTabStyle> {
}
};

template <>
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 @@ -1171,6 +1183,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 @@ -1639,6 +1653,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
17 changes: 17 additions & 0 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,23 @@ 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 @@ -865,6 +865,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
130 changes: 130 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28427,6 +28427,136 @@ TEST_F(FormatTest, ShortNamespacesOption) {
Style);
}

TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) {
auto Style = getLLVMStyle();
Style.FixNamespaceComments = false;
Style.MaxEmptyLinesToKeep = 2;
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"
"}",
"namespace N {\n"
"\n"
"\n"
"int f1(int a) { return 2 * a; }\n"
"\n"
"\n"
"}",
Style);

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

Style.CompactNamespaces = true;

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

TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
auto Style = getLLVMStyle();
Style.FixNamespaceComments = false;
Style.MaxEmptyLinesToKeep = 2;
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"
"}",
"namespace N {\n"
"int f1(int a) { return 2 * a; }\n"
"}",
Style);

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

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

Style.CompactNamespaces = true;

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

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