[clang-format] Add BreakFunctionDeclarationParameters option.#196567
Conversation
Adds an option the break function declaration parameters, always putting them on the next line after the function opening parentheses. This is an equivalent of `BreakFunctionDefinitionParameters`, but for function declarations.
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang-format Author: stativ ChangesAdds an option the break function declaration parameters, always putting them on the next line after the function opening parentheses. This is an equivalent of Full diff: https://github.com/llvm/llvm-project/pull/196567.diff 6 Files Affected:
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index d492f2364cf74..04d382df3f90f 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -3857,6 +3857,20 @@ the configuration (without a prefix: ``Auto``).
initializer2()
+.. _BreakFunctionDeclarationParameters:
+
+**BreakFunctionDeclarationParameters** (``Boolean``) :versionbadge:`clang-format 23` :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:
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 98400a1609b6a..0e883837ac0e9 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -2644,6 +2644,20 @@ struct FormatStyle {
/// \version 5
BreakConstructorInitializersStyle BreakConstructorInitializers;
+ /// 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 23
+ bool BreakFunctionDeclarationParameters;
+
/// If ``true``, clang-format will always break before function definition
/// parameters.
/// \code
@@ -6076,6 +6090,8 @@ struct FormatStyle {
BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators &&
BreakBinaryOperations == R.BreakBinaryOperations &&
BreakConstructorInitializers == R.BreakConstructorInitializers &&
+ BreakFunctionDeclarationParameters ==
+ R.BreakFunctionDeclarationParameters &&
BreakFunctionDefinitionParameters ==
R.BreakFunctionDefinitionParameters &&
BreakInheritanceList == R.BreakInheritanceList &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 2147a812e27c1..74b31810843fc 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1318,6 +1318,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("BreakBinaryOperations", Style.BreakBinaryOperations);
IO.mapOptional("BreakConstructorInitializers",
Style.BreakConstructorInitializers);
+ IO.mapOptional("BreakFunctionDeclarationParameters",
+ Style.BreakFunctionDeclarationParameters);
IO.mapOptional("BreakFunctionDefinitionParameters",
Style.BreakFunctionDefinitionParameters);
IO.mapOptional("BreakInheritanceList", Style.BreakInheritanceList);
@@ -1885,6 +1887,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.BreakBeforeTernaryOperators = true;
LLVMStyle.BreakBinaryOperations = {FormatStyle::BBO_Never, {}};
LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
+ LLVMStyle.BreakFunctionDeclarationParameters = false;
LLVMStyle.BreakFunctionDefinitionParameters = false;
LLVMStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
LLVMStyle.BreakStringLiterals = true;
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 898759cb8ea1b..640f03a4ac130 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -5758,6 +5758,12 @@ bool TokenAnnotator::mustBreakBefore(AnnotatedLine &Line,
const FormatToken &Left = *Right.Previous;
+ if (Style.BreakFunctionDeclarationParameters && Line.MightBeFunctionDecl &&
+ !Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
+ Left.ParameterCount > 0) {
+ return true;
+ }
+
if (Style.BreakFunctionDefinitionParameters && Line.MightBeFunctionDecl &&
Line.mightBeFunctionDefinition() && Left.MightBeFunctionDeclParen &&
Left.ParameterCount > 0) {
diff --git a/clang/unittests/Format/AlignBracketsTest.cpp b/clang/unittests/Format/AlignBracketsTest.cpp
index cd314305751e7..fcfcae20e3e11 100644
--- a/clang/unittests/Format/AlignBracketsTest.cpp
+++ b/clang/unittests/Format/AlignBracketsTest.cpp
@@ -731,6 +731,15 @@ TEST_F(AlignBracketsTest, FormatsDefinitionBreakAlways) {
"}",
BreakAlways);
+ // Ensure BreakFunctionDeclarationParameters interacts correctly when
+ // PackParameters.BinPack is set to BPPS_AlwaysOnePerLine.
+ BreakAlways.BreakFunctionDeclarationParameters = true;
+ verifyFormat("void f(\n"
+ " int a,\n"
+ " int b);",
+ BreakAlways);
+ BreakAlways.BreakFunctionDeclarationParameters = false;
+
// Ensure BreakFunctionDefinitionParameters interacts correctly when
// PackParameters.BinPack is set to BPPS_AlwaysOnePerLine.
BreakAlways.BreakFunctionDefinitionParameters = true;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f5e496652e15e..4245bd1c58153 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8119,6 +8119,33 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLine) {
Style);
}
+TEST_F(FormatTest, BreakFunctionDeclarationParameters) {
+ StringRef Input = "void functionDecl(int A, int B, int C);\n"
+ "void emptyFunctionDecl();\n"
+ "void functionDefinition(int A, int B, int C) {}";
+ verifyFormat(Input);
+
+ FormatStyle Style = getLLVMStyle();
+ EXPECT_FALSE(Style.BreakFunctionDeclarationParameters);
+ Style.BreakFunctionDeclarationParameters = true;
+ verifyFormat("void functionDecl(\n"
+ " int A, int B, int C);\n"
+ "void emptyFunctionDecl();\n"
+ "void functionDefinition(int A, int B, int C) {}",
+ Input, Style);
+
+ // Test the style where all parameters are on their own lines.
+ Style.AllowAllParametersOfDeclarationOnNextLine = false;
+ Style.PackParameters.BinPack = FormatStyle::BPPS_OnePerLine;
+ verifyFormat("void functionDecl(\n"
+ " int A,\n"
+ " int B,\n"
+ " int C);\n"
+ "void emptyFunctionDecl();\n"
+ "void functionDefinition(int A, int B, int C) {}",
+ Input, Style);
+}
+
TEST_F(FormatTest, BreakFunctionDefinitionParameters) {
StringRef Input = "void functionDecl(paramA, paramB, paramC);\n"
"void emptyFunctionDefinition() {}\n"
|
HazardyKnusperkeks
left a comment
There was a problem hiding this comment.
As far as I see only two things are missing:
- A config parse test
- A note in the release notes
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
|
Everything should be fixed. I was not able to run the |
|
@stativ Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/88/builds/23227 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/141/builds/18394 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/72/builds/17648 Here is the relevant piece of the build log for the reference |
Adds an option the break function declaration parameters, always putting them on the next line after the function opening parentheses.
This is an equivalent of
BreakFunctionDefinitionParameters, but for function declarations.