Skip to content

[clang-format] Add RemoveEmptyLinesInUnwrappedLines option #112325

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

Merged
merged 3 commits into from
Oct 18, 2024
Merged
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
25 changes: 25 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5505,6 +5505,31 @@ the configuration (without a prefix: ``Auto``).
}
}

.. _RemoveEmptyLinesInUnwrappedLines:

**RemoveEmptyLinesInUnwrappedLines** (``Boolean``) :versionbadge:`clang-format 20` :ref:`¶ <RemoveEmptyLinesInUnwrappedLines>`
Remove empty lines within unwrapped lines.

.. code-block:: c++

false: true:

int c vs. int c = a + b;

= a + b;

enum : unsigned vs. enum : unsigned {
AA = 0,
{ BB
AA = 0, } myEnum;
BB
} myEnum;

while ( vs. while (true) {
}
true) {
}

.. _RemoveParentheses:

**RemoveParentheses** (``RemoveParenthesesStyle``) :versionbadge:`clang-format 17` :ref:`¶ <RemoveParentheses>`
Expand Down
6 changes: 4 additions & 2 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,10 @@ clang-format
- Adds ``BreakBinaryOperations`` option.
- Adds ``TemplateNames`` option.
- Adds ``AlignFunctionDeclarations`` option to ``AlignConsecutiveDeclarations``.
- Adds ``IndentOnly`` suboption to ``ReflowComments`` to fix the indentation of multi-line comments
without touching their contents, renames ``false`` to ``Never``, and ``true`` to ``Always``.
- Adds ``IndentOnly`` suboption to ``ReflowComments`` to fix the indentation of
multi-line comments without touching their contents, renames ``false`` to
``Never``, and ``true`` to ``Always``.
- Adds ``RemoveEmptyLinesInUnwrappedLines`` option.

libclang
--------
Expand Down
25 changes: 25 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3938,6 +3938,29 @@ struct FormatStyle {
/// \version 14
bool RemoveBracesLLVM;

/// Remove empty lines within unwrapped lines.
/// \code
/// false: true:
///
/// int c vs. int c = a + b;
///
/// = a + b;
///
/// enum : unsigned vs. enum : unsigned {
/// AA = 0,
/// { BB
/// AA = 0, } myEnum;
/// BB
/// } myEnum;
///
/// while ( vs. while (true) {
/// }
/// true) {
/// }
/// \endcode
/// \version 20
bool RemoveEmptyLinesInUnwrappedLines;

/// Types of redundant parentheses to remove.
enum RemoveParenthesesStyle : int8_t {
/// Do not remove parentheses.
Expand Down Expand Up @@ -5232,6 +5255,8 @@ struct FormatStyle {
RawStringFormats == R.RawStringFormats &&
ReferenceAlignment == R.ReferenceAlignment &&
RemoveBracesLLVM == R.RemoveBracesLLVM &&
RemoveEmptyLinesInUnwrappedLines ==
R.RemoveEmptyLinesInUnwrappedLines &&
RemoveParentheses == R.RemoveParentheses &&
RemoveSemicolon == R.RemoveSemicolon &&
RequiresClausePosition == R.RequiresClausePosition &&
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("ReferenceAlignment", Style.ReferenceAlignment);
IO.mapOptional("ReflowComments", Style.ReflowComments);
IO.mapOptional("RemoveBracesLLVM", Style.RemoveBracesLLVM);
IO.mapOptional("RemoveEmptyLinesInUnwrappedLines",
Style.RemoveEmptyLinesInUnwrappedLines);
IO.mapOptional("RemoveParentheses", Style.RemoveParentheses);
IO.mapOptional("RemoveSemicolon", Style.RemoveSemicolon);
IO.mapOptional("RequiresClausePosition", Style.RequiresClausePosition);
Expand Down Expand Up @@ -1582,6 +1584,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.ReferenceAlignment = FormatStyle::RAS_Pointer;
LLVMStyle.ReflowComments = FormatStyle::RCS_Always;
LLVMStyle.RemoveBracesLLVM = false;
LLVMStyle.RemoveEmptyLinesInUnwrappedLines = false;
LLVMStyle.RemoveParentheses = FormatStyle::RPS_Leave;
LLVMStyle.RemoveSemicolon = false;
LLVMStyle.RequiresClausePosition = FormatStyle::RCPS_OwnLine;
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5509,8 +5509,10 @@ static bool isAllmanLambdaBrace(const FormatToken &Tok) {
bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
const FormatToken &Right) const {
const FormatToken &Left = *Right.Previous;
if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0)
if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0 &&
(!Style.RemoveEmptyLinesInUnwrappedLines || &Right == Line.First)) {
return true;
}

if (Style.BreakFunctionDefinitionParameters && Line.MightBeFunctionDecl &&
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
Expand Down
1 change: 1 addition & 0 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList);
CHECK_PARSE_BOOL(Cpp11BracedListStyle);
CHECK_PARSE_BOOL(RemoveBracesLLVM);
CHECK_PARSE_BOOL(RemoveEmptyLinesInUnwrappedLines);
CHECK_PARSE_BOOL(RemoveSemicolon);
CHECK_PARSE_BOOL(SkipMacroDefinitionBody);
CHECK_PARSE_BOOL(SpacesInSquareBrackets);
Expand Down
77 changes: 77 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28135,6 +28135,83 @@ TEST_F(FormatTest, BreakBinaryOperations) {
Style);
}

TEST_F(FormatTest, RemovesEmptyLinesInUnwrappedLines) {
auto Style = getLLVMStyle();
Style.RemoveEmptyLinesInUnwrappedLines = true;

verifyFormat("int c = a + b;",
"int c\n"
"\n"
" = a + b;",
Style);

verifyFormat("enum : unsigned { AA = 0, BB } myEnum;",
"enum : unsigned\n"
"\n"
"{\n"
" AA = 0,\n"
" BB\n"
"} myEnum;",
Style);

verifyFormat("class B : public E {\n"
"private:\n"
"};",
"class B : public E\n"
"\n"
"{\n"
"private:\n"
"};",
Style);

verifyFormat(
"struct AAAAAAAAAAAAAAA test[3] = {{56, 23, \"hello\"}, {7, 5, \"!!\"}};",
"struct AAAAAAAAAAAAAAA test[3] = {{56,\n"
"\n"
" 23, \"hello\"},\n"
" {7, 5, \"!!\"}};",
Style);

verifyFormat("int myFunction(int aaaaaaaaaaaaa, int ccccccccccccc, int d);",
"int myFunction(\n"
"\n"
" int aaaaaaaaaaaaa,\n"
"\n"
" int ccccccccccccc, int d);",
Style);

verifyFormat("switch (e) {\n"
"case 1:\n"
" return e;\n"
"case 2:\n"
" return 2;\n"
"}",
"switch (\n"
"\n"
" e) {\n"
"case 1:\n"
" return e;\n"
"case 2:\n"
" return 2;\n"
"}",
Style);

verifyFormat("while (true) {\n"
"}",
"while (\n"
"\n"
" true) {\n"
"}",
Style);

verifyFormat("void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames(\n"
" std::map<int, std::string> *outputMap);",
"void loooonFunctionIsVeryLongButNotAsLongAsJavaTypeNames\n"
"\n"
" (std::map<int, std::string> *outputMap);",
Style);
}

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