Skip to content
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
15 changes: 15 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3565,6 +3565,21 @@ the configuration (without a prefix: ``Auto``).



.. _BreakFunctionDeclarationParameters:

**BreakFunctionDeclarationParameters** (``Boolean``) :versionbadge:`clang-format 22` :ref:`¶ <BreakFunctionDeclarationParameters>`
If ``true``, clang-format will always break before function declaration
parameters.

.. code-block:: c++

true:
void functionDeclaration(
int A, int B);

false:
void functionDeclaration(int A, int B);

.. _BreakFunctionDefinitionParameters:

**BreakFunctionDefinitionParameters** (``Boolean``) :versionbadge:`clang-format 19` :ref:`¶ <BreakFunctionDefinitionParameters>`
Expand Down
16 changes: 16 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,20 @@ struct FormatStyle {
/// \version 19
bool BreakFunctionDefinitionParameters;

/// If ``true``, clang-format will always break before function declaration
/// parameters.
/// \code
/// true:
/// void functionDeclaration(
/// int A, int B);
///
/// false:
/// void functionDeclaration(int A, int B);
///
/// \endcode
/// \version 22
bool BreakFunctionDeclarationParameters;
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't quite tell but it feel like true is "AlwaysBreakFunctionDeclarationParameters" because false doesn't feel like NeverBreak... just depends on what is there currently?

Copy link
Contributor

Choose a reason for hiding this comment

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

Please sort the option (here and elsewhere) alphabetically.


/// Break after each annotation on a field in Java files.
/// \code{.java}
/// true: false:
Expand Down Expand Up @@ -5497,6 +5511,8 @@ struct FormatStyle {
BreakConstructorInitializers == R.BreakConstructorInitializers &&
BreakFunctionDefinitionParameters ==
R.BreakFunctionDefinitionParameters &&
BreakFunctionDeclarationParameters ==
R.BreakFunctionDeclarationParameters &&
BreakInheritanceList == R.BreakInheritanceList &&
BreakStringLiterals == R.BreakStringLiterals &&
BreakTemplateDeclarations == R.BreakTemplateDeclarations &&
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 @@ -1083,6 +1083,8 @@ template <> struct MappingTraits<FormatStyle> {
Style.BreakConstructorInitializers);
IO.mapOptional("BreakFunctionDefinitionParameters",
Style.BreakFunctionDefinitionParameters);
IO.mapOptional("BreakFunctionDeclarationParameters",
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to add a parsing test for this.

Style.BreakFunctionDeclarationParameters);
IO.mapOptional("BreakInheritanceList", Style.BreakInheritanceList);
IO.mapOptional("BreakStringLiterals", Style.BreakStringLiterals);
IO.mapOptional("BreakTemplateDeclarations",
Expand Down Expand Up @@ -1617,6 +1619,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.BreakBinaryOperations = FormatStyle::BBO_Never;
LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
LLVMStyle.BreakFunctionDefinitionParameters = false;
LLVMStyle.BreakFunctionDeclarationParameters = false;
LLVMStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
LLVMStyle.BreakStringLiterals = true;
LLVMStyle.BreakTemplateDeclarations = FormatStyle::BTDS_MultiLine;
Expand Down
6 changes: 6 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5626,6 +5626,12 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
return true;
}

if (Style.BreakFunctionDeclarationParameters && Line.MightBeFunctionDecl &&
!Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
Left.ParameterCount > 0) {
return true;
}

// Ignores the first parameter as this will be handled separately by
// BreakFunctionDefinitionParameters or AlignAfterOpenBracket.
if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine &&
Expand Down
33 changes: 33 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8143,6 +8143,39 @@ TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
Input, Style);
}

TEST_F(FormatTest, BreakFunctionDeclarationParameters) {
StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
"void emptyFunctionDefinition() {}\n"
"void functionDefinition(int A, int B, int C) {}\n"
"Class::Class(int A, int B) : m_A(A), m_B(B) {}";
verifyFormat(Input);

FormatStyle Style = getLLVMStyle();
EXPECT_FALSE(Style.BreakFunctionDeclarationParameters);
Style.BreakFunctionDeclarationParameters = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

your documentation suggest that having this as false, doesn't break, but actually almost looks like it "joins" the lines, but looking at the code I can't see that is the case,

does false really mean "Leave" or "join"...could you add a test if you mean Join but also move it to an enum so we can have a "Leave" case..

How does this interact with other options? doing similar things..

verifyFormat("void functionDecl(\n"
" paramA, paramB, paramC);\n"
"void emptyFunctionDefinition() {}\n"
"void functionDefinition(int A, int B, int C) {}\n"
"class Class {\n"
" Class(\n"
" int A, int B);\n"
"};\n",
Input, Style);

// Test the style where all parameters are on their own lines.
Style.AllowAllParametersOfDeclarationOnNextLine = false;
Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
verifyFormat("void functionDecl(\n"
" paramA,\n"
" paramB,\n"
" paramC);\n"
"void emptyFunctionDefinition() {}\n"
"void functionDefinition(int A, int B, int C) {}\n"
"Class::Class(int A, int B) : m_A(A), m_B(B) {}",
Input, Style);
}

TEST_F(FormatTest, BreakBeforeInlineASMColon) {
FormatStyle Style = getLLVMStyle();
Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never;
Expand Down
Loading