llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-format Author: Ameer J (ameerj) <details> <summary>Changes</summary> This adds an option to break function definition parameters, putting them on the next line after the function's opening paren. This was a missing step towards allowing styles which require all function definition parameters be on their own lines. Closes #<!-- -->62963 --- Full diff: https://github.com/llvm/llvm-project/pull/84988.diff 6 Files Affected: - (modified) clang/docs/ClangFormatStyleOptions.rst (+6) - (modified) clang/include/clang/Format/Format.h (+7) - (modified) clang/lib/Format/Format.cpp (+3) - (modified) clang/lib/Format/FormatToken.h (+3) - (modified) clang/lib/Format/TokenAnnotator.cpp (+8) - (modified) clang/unittests/Format/FormatTest.cpp (+18) ``````````diff diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 5b00a8f4c00fb8..a5e710e21c8338 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -3150,6 +3150,12 @@ the configuration (without a prefix: ``Auto``). +.. _BreakFunctionDefinitionParameters: + +**BreakFunctionDefinitionParameters** (``Boolean``) :versionbadge:`clang-format 19` :ref:`¶ <BreakFunctionDefinitionParameters>` + If ``true``, clang-format will always break before function definition + parameters + .. _BreakInheritanceList: **BreakInheritanceList** (``BreakInheritanceListStyle``) :versionbadge:`clang-format 7` :ref:`¶ <BreakInheritanceList>` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 590297fd89a398..eb115dfd2b275d 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -2218,6 +2218,11 @@ struct FormatStyle { /// \version 3.8 bool BreakAfterJavaFieldAnnotations; + /// If ``true``, clang-format will always break before function definition + /// parameters + /// \version 19 + bool BreakFunctionDefinitionParameters; + /// Allow breaking string literals when formatting. /// /// In C, C++, and Objective-C: @@ -4867,6 +4872,8 @@ struct FormatStyle { BreakBeforeInlineASMColon == R.BreakBeforeInlineASMColon && BreakBeforeTernaryOperators == R.BreakBeforeTernaryOperators && BreakConstructorInitializers == R.BreakConstructorInitializers && + BreakFunctionDefinitionParameters == + R.BreakFunctionDefinitionParameters && BreakInheritanceList == R.BreakInheritanceList && BreakStringLiterals == R.BreakStringLiterals && BreakTemplateDeclarations == R.BreakTemplateDeclarations && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e64ba7eebc1ce8..ebdb4eae9520d3 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -961,6 +961,8 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("BreakAfterJavaFieldAnnotations", Style.BreakAfterJavaFieldAnnotations); IO.mapOptional("BreakAfterReturnType", Style.BreakAfterReturnType); + IO.mapOptional("BreakFunctionDefinitionParameters", + Style.BreakFunctionDefinitionParameters); IO.mapOptional("BreakArrays", Style.BreakArrays); IO.mapOptional("BreakBeforeBinaryOperators", Style.BreakBeforeBinaryOperators); @@ -1471,6 +1473,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Leave; LLVMStyle.BreakAfterJavaFieldAnnotations = false; LLVMStyle.BreakAfterReturnType = FormatStyle::RTBS_None; + LLVMStyle.BreakFunctionDefinitionParameters = false; LLVMStyle.BreakArrays = true; LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; diff --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h index 31245495041960..b20682de91c0b7 100644 --- a/clang/lib/Format/FormatToken.h +++ b/clang/lib/Format/FormatToken.h @@ -571,6 +571,9 @@ struct FormatToken { /// Is optional and can be removed. bool Optional = false; + /// Might be function declaration open/closing paren. + bool MightBeFunctionDeclParen = false; + /// Number of optional braces to be inserted after this token: /// -1: a single left brace /// 0: no braces diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 04f0374b674e53..3d1c87d0b7253c 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1501,6 +1501,8 @@ class AnnotatingParser { (!Previous->isAttribute() && !Previous->isOneOf(TT_RequiresClause, TT_LeadingJavaAnnotation))) { Line.MightBeFunctionDecl = true; + Tok->MightBeFunctionDeclParen = true; + Tok->MatchingParen->MightBeFunctionDeclParen = true; } } break; @@ -5313,6 +5315,12 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line, if (Right.NewlinesBefore > 1 && Style.MaxEmptyLinesToKeep > 0) return true; + if (Style.BreakFunctionDefinitionParameters && Line.MightBeFunctionDecl && + Line.mightBeFunctionDefinition() && Left.is(tok::l_paren) && + Left.MightBeFunctionDeclParen && Left.ParameterCount > 0) { + return true; + } + if (Style.isCSharp()) { if (Left.is(TT_FatArrow) && Right.is(tok::l_brace) && Style.BraceWrapping.AfterFunction) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index fc367a7a5a8981..d70a3c916a2d75 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -7951,6 +7951,24 @@ TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { Input, Style); } +TEST_F(FormatTest, BreakFunctionDefinitionParameters) { + FormatStyle Style = getLLVMStyleWithColumns(80); + StringRef Input = "void functionDecl(paramA, paramB, paramC);\n" + "void emptyFunctionDefinition() {}\n" + "void functionDefinition(int A, int B, int C) {}"; + Style.BreakFunctionDefinitionParameters = false; + verifyFormat(StringRef("void functionDecl(paramA, paramB, paramC);\n" + "void emptyFunctionDefinition() {}\n" + "void functionDefinition(int A, int B, int C) {}"), + Input, Style); + Style.BreakFunctionDefinitionParameters = true; + verifyFormat(StringRef("void functionDecl(paramA, paramB, paramC);\n" + "void emptyFunctionDefinition() {}\n" + "void functionDefinition(\n" + " int A, int B, int C) {}"), + Input, Style); +} + TEST_F(FormatTest, BreakBeforeInlineASMColon) { FormatStyle Style = getLLVMStyle(); Style.BreakBeforeInlineASMColon = FormatStyle::BBIAS_Never; `````````` </details> https://github.com/llvm/llvm-project/pull/84988 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits