llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: None (rmarker) <details> <summary>Changes</summary> Adds a new BreakAfterReturnType option, with a more relevant name, that deprecates and replaces AlwaysBreakAfterReturnType. Following up on #<!-- -->78010. --- Full diff: https://github.com/llvm/llvm-project/pull/80827.diff 10 Files Affected: - (modified) clang/docs/ClangFormatStyleOptions.rst (+113-1) - (modified) clang/docs/ReleaseNotes.rst (+2) - (modified) clang/include/clang/Format/Format.h (+7-2) - (modified) clang/lib/Format/ContinuationIndenter.cpp (+3-3) - (modified) clang/lib/Format/Format.cpp (+13) - (modified) clang/lib/Format/TokenAnnotator.cpp (+3-4) - (modified) clang/unittests/Format/ConfigParseTest.cpp (+16) - (modified) clang/unittests/Format/DefinitionBlockSeparatorTest.cpp (+1-1) - (modified) clang/unittests/Format/FormatTest.cpp (+8-8) - (modified) clang/unittests/Format/FormatTestCSharp.cpp (+1-1) ``````````diff diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index f86be2c1246fb..9507813accb99 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -1532,7 +1532,8 @@ the configuration (without a prefix: ``Auto``). .. _AlwaysBreakAfterReturnType: **AlwaysBreakAfterReturnType** (``ReturnTypeBreakingStyle``) :versionbadge:`clang-format 3.8` :ref:`¶ <AlwaysBreakAfterReturnType>` - The function declaration return type breaking style to use. + The function declaration return type breaking style to use. This + option is **deprecated** and is retained for backwards compatibility. Possible values: @@ -2261,6 +2262,117 @@ the configuration (without a prefix: ``Auto``). @Mock DataLoad loader; +.. _BreakAfterReturnType: + +**BreakAfterReturnType** (``ReturnTypeBreakingStyle``) :versionbadge:`clang-format 19` :ref:`¶ <BreakAfterReturnType>` + The function declaration return type breaking style to use. + + Possible values: + + * ``RTBS_None`` (in configuration: ``None``) + This is **deprecated**. See ``Automatic`` below. + + * ``RTBS_Automatic`` (in configuration: ``Automatic``) + Break after return type based on ``PenaltyReturnTypeOnItsOwnLine``. + + .. code-block:: c++ + + class A { + int f() { return 0; }; + }; + int f(); + int f() { return 1; } + int + LongName::AnotherLongName(); + + * ``RTBS_ExceptShortType`` (in configuration: ``ExceptShortType``) + Same as ``Automatic`` above, except that there is no break after short + return types. + + .. code-block:: c++ + + class A { + int f() { return 0; }; + }; + int f(); + int f() { return 1; } + int LongName:: + AnotherLongName(); + + * ``RTBS_All`` (in configuration: ``All``) + Always break after the return type. + + .. code-block:: c++ + + class A { + int + f() { + return 0; + }; + }; + int + f(); + int + f() { + return 1; + } + int + LongName::AnotherLongName(); + + * ``RTBS_TopLevel`` (in configuration: ``TopLevel``) + Always break after the return types of top-level functions. + + .. code-block:: c++ + + class A { + int f() { return 0; }; + }; + int + f(); + int + f() { + return 1; + } + int + LongName::AnotherLongName(); + + * ``RTBS_AllDefinitions`` (in configuration: ``AllDefinitions``) + Always break after the return type of function definitions. + + .. code-block:: c++ + + class A { + int + f() { + return 0; + }; + }; + int f(); + int + f() { + return 1; + } + int + LongName::AnotherLongName(); + + * ``RTBS_TopLevelDefinitions`` (in configuration: ``TopLevelDefinitions``) + Always break after the return type of top-level definitions. + + .. code-block:: c++ + + class A { + int f() { return 0; }; + }; + int f(); + int + f() { + return 1; + } + int + LongName::AnotherLongName(); + + + .. _BreakArrays: **BreakArrays** (``Boolean``) :versionbadge:`clang-format 16` :ref:`¶ <BreakArrays>` diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4d57ea4fd55b8..d09221ab7f74c 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -270,6 +270,8 @@ AST Matchers clang-format ------------ +- Add BreakAfterReturnType option to deprecate AlwaysBreakAfterReturnType. + libclang -------- diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 415321310c24f..d838c2ab32b26 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -1010,7 +1010,8 @@ struct FormatStyle { /// \version 3.7 DefinitionReturnTypeBreakingStyle AlwaysBreakAfterDefinitionReturnType; - /// The function declaration return type breaking style to use. + /// The function declaration return type breaking style to use. This + /// option is **deprecated** and is retained for backwards compatibility. /// \version 3.8 ReturnTypeBreakingStyle AlwaysBreakAfterReturnType; @@ -1565,6 +1566,10 @@ struct FormatStyle { /// \version 16 AttributeBreakingStyle BreakAfterAttributes; + /// The function declaration return type breaking style to use. + /// \version 19 + ReturnTypeBreakingStyle BreakAfterReturnType; + /// If ``true``, clang-format will always break after a Json array ``[`` /// otherwise it will scan until the closing ``]`` to determine if it should /// add newlines between elements (prettier compatible). @@ -4804,7 +4809,6 @@ struct FormatStyle { R.AllowShortIfStatementsOnASingleLine && AllowShortLambdasOnASingleLine == R.AllowShortLambdasOnASingleLine && AllowShortLoopsOnASingleLine == R.AllowShortLoopsOnASingleLine && - AlwaysBreakAfterReturnType == R.AlwaysBreakAfterReturnType && AlwaysBreakBeforeMultilineStrings == R.AlwaysBreakBeforeMultilineStrings && AlwaysBreakTemplateDeclarations == @@ -4817,6 +4821,7 @@ struct FormatStyle { BreakAdjacentStringLiterals == R.BreakAdjacentStringLiterals && BreakAfterAttributes == R.BreakAfterAttributes && BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations && + BreakAfterReturnType == R.BreakAfterReturnType && BreakArrays == R.BreakArrays && BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators && BreakBeforeBraces == R.BreakBeforeBraces && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 671ae540c75b1..027a291ad4965 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -329,12 +329,12 @@ bool ContinuationIndenter::canBreak(const LineState &State) { // Don't break after very short return types (e.g. "void") as that is often // unexpected. if (Current.is(TT_FunctionDeclarationName)) { - if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None && + if (Style.BreakAfterReturnType == FormatStyle::RTBS_None && State.Column < 6) { return false; } - if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_ExceptShortType) { + if (Style.BreakAfterReturnType == FormatStyle::RTBS_ExceptShortType) { assert(State.Column >= State.FirstIndent); if (State.Column - State.FirstIndent < 6) return false; @@ -595,7 +595,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { !State.Line->ReturnTypeWrapped && // Don't break before a C# function when no break after return type. (!Style.isCSharp() || - Style.AlwaysBreakAfterReturnType > FormatStyle::RTBS_ExceptShortType) && + Style.BreakAfterReturnType > FormatStyle::RTBS_ExceptShortType) && // Don't always break between a JavaScript `function` and the function // name. !Style.isJavaScript() && Previous.isNot(tok::kw_template) && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 9c780cd7a5f4e..658d8722003b7 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -956,6 +956,7 @@ template <> struct MappingTraits<FormatStyle> { IO.mapOptional("BreakAfterAttributes", Style.BreakAfterAttributes); IO.mapOptional("BreakAfterJavaFieldAnnotations", Style.BreakAfterJavaFieldAnnotations); + IO.mapOptional("BreakAfterReturnType", Style.BreakAfterReturnType); IO.mapOptional("BreakArrays", Style.BreakArrays); IO.mapOptional("BreakBeforeBinaryOperators", Style.BreakBeforeBinaryOperators); @@ -1137,6 +1138,14 @@ template <> struct MappingTraits<FormatStyle> { } } + // If AlwaysBreakAfterReturnType was specified but + // BreakAfterReturnType was not, initialize the latter from the + // former for backwards compatibility. + if (Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None && + Style.BreakAfterReturnType == FormatStyle::RTBS_None) { + Style.BreakAfterReturnType = Style.AlwaysBreakAfterReturnType; + } + // If BreakBeforeInheritanceComma was specified but BreakInheritance was // not, initialize the latter from the former for backwards compatibility. if (BreakBeforeInheritanceComma && @@ -1465,6 +1474,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.BreakAdjacentStringLiterals = true; LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Leave; LLVMStyle.BreakAfterJavaFieldAnnotations = false; + LLVMStyle.BreakAfterReturnType = FormatStyle::RTBS_None; LLVMStyle.BreakArrays = true; LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None; LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; @@ -1824,6 +1834,7 @@ FormatStyle getMozillaStyle() { MozillaStyle.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes; MozillaStyle.BinPackParameters = false; MozillaStyle.BinPackArguments = false; + MozillaStyle.BreakAfterReturnType = FormatStyle::RTBS_TopLevel; MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla; MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma; MozillaStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma; @@ -1868,6 +1879,7 @@ FormatStyle getGNUStyle() { FormatStyle Style = getLLVMStyle(); Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All; Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; + Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; Style.BreakBeforeBraces = FormatStyle::BS_GNU; Style.BreakBeforeTernaryOperators = true; @@ -1905,6 +1917,7 @@ FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language) { Style.AllowShortLoopsOnASingleLine = false; Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None; Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; + Style.BreakAfterReturnType = FormatStyle::RTBS_None; return Style; } diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 7a62f5fd3dfbb..7287823f5062a 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3425,14 +3425,13 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current, bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const { assert(Line.MightBeFunctionDecl); - if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel || - Style.AlwaysBreakAfterReturnType == - FormatStyle::RTBS_TopLevelDefinitions) && + if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel || + Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) && Line.Level > 0) { return false; } - switch (Style.AlwaysBreakAfterReturnType) { + switch (Style.BreakAfterReturnType) { case FormatStyle::RTBS_None: case FormatStyle::RTBS_Automatic: case FormatStyle::RTBS_ExceptShortType: diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index 3f1fc89351694..c6faa0e91464a 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -677,6 +677,22 @@ TEST(ConfigParseTest, ParsesConfiguration) { " AfterControlStatement: false", BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never); + Style.BreakAfterReturnType = FormatStyle::RTBS_All; + CHECK_PARSE("BreakAfterReturnType: None", BreakAfterReturnType, + FormatStyle::RTBS_None); + CHECK_PARSE("BreakAfterReturnType: Automatic", BreakAfterReturnType, + FormatStyle::RTBS_Automatic); + CHECK_PARSE("BreakAfterReturnType: ExceptShortType", BreakAfterReturnType, + FormatStyle::RTBS_ExceptShortType); + CHECK_PARSE("BreakAfterReturnType: All", BreakAfterReturnType, + FormatStyle::RTBS_All); + CHECK_PARSE("BreakAfterReturnType: TopLevel", BreakAfterReturnType, + FormatStyle::RTBS_TopLevel); + CHECK_PARSE("BreakAfterReturnType: AllDefinitions", BreakAfterReturnType, + FormatStyle::RTBS_AllDefinitions); + CHECK_PARSE("BreakAfterReturnType: TopLevelDefinitions", BreakAfterReturnType, + FormatStyle::RTBS_TopLevelDefinitions); + Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType, FormatStyle::RTBS_None); diff --git a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp index f5489498a93b9..7a120935cfa96 100644 --- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp +++ b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp @@ -144,7 +144,7 @@ TEST_F(DefinitionBlockSeparatorTest, Basic) { Style); FormatStyle BreakAfterReturnTypeStyle = Style; - BreakAfterReturnTypeStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; + BreakAfterReturnTypeStyle.BreakAfterReturnType = FormatStyle::RTBS_All; // Test uppercased long typename verifyFormat("class Foo {\n" " void\n" diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 87a02a4dfbf2b..38a222380185b 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -9870,7 +9870,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { Style.ColumnLimit = 60; // No declarations or definitions should be moved to own line. - Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; + Style.BreakAfterReturnType = FormatStyle::RTBS_None; verifyFormat("class A {\n" " int f() { return 1; }\n" " int g();\n" @@ -9884,7 +9884,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { Style); // It is now allowed to break after a short return type if necessary. - Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_Automatic; + Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic; verifyFormat("class A {\n" " int f() { return 1; }\n" " int g();\n" @@ -9898,7 +9898,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { Style); // It now must never break after a short return type. - Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_ExceptShortType; + Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType; verifyFormat("class A {\n" " int f() { return 1; }\n" " int g();\n" @@ -9913,7 +9913,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { // All declarations and definitions should have the return type moved to its // own line. - Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; + Style.BreakAfterReturnType = FormatStyle::RTBS_All; Style.TypenameMacros = {"LIST"}; verifyFormat("SomeType\n" "funcdecl(LIST(uint64_t));", @@ -9940,7 +9940,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { // Top-level definitions, and no kinds of declarations should have the // return type moved to its own line. - Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; + Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions; verifyFormat("class B {\n" " int f() { return 1; }\n" " int g();\n" @@ -9954,7 +9954,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { // Top-level definitions and declarations should have the return type moved // to its own line. - Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel; + Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel; verifyFormat("class C {\n" " int f() { return 1; }\n" " int g();\n" @@ -9971,7 +9971,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) { // All definitions should have the return type moved to its own line, but no // kinds of declarations. - Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; + Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions; verifyFormat("class D {\n" " int\n" " f() {\n" @@ -11872,7 +11872,7 @@ TEST_F(FormatTest, UnderstandsAttributes) { "aaaaaaaaaaaaaaaaaaaaaaa(int i);"); verifyFormat("__attribute__((nodebug)) ::qualified_type f();"); FormatStyle AfterType = getLLVMStyle(); - AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All; + AfterType.BreakAfterReturnType = FormatStyle::RTBS_All; verifyFormat("__attribute__((nodebug)) void\n" "foo() {}", AfterType); diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index 6f5e1e41ef7e0..de261c0948308 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -505,7 +505,7 @@ TEST_F(FormatTestCSharp, CSharpNullForgiving) { TEST_F(FormatTestCSharp, AttributesIndentation) { FormatStyle Style = getMicrosoftStyle(FormatStyle::LK_CSharp); - Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; + Style.BreakAfterReturnType = FormatStyle::RTBS_None; verifyFormat("[STAThread]\n" "static void Main(string[] args)\n" `````````` </details> https://github.com/llvm/llvm-project/pull/80827 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits