diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -4048,6 +4048,11 @@ **PenaltyBreakOpenParenthesis** (``Unsigned``) :versionbadge:`clang-format 14` :ref:`¶ ` The penalty for breaking after ``(``. +.. _PenaltyBreakReturn: + +**PenaltyBreakReturn** (``Unsigned``) :versionbadge:`clang-format 18` :ref:`¶ ` + The penalty for breaking after ``return``. + .. _PenaltyBreakString: **PenaltyBreakString** (``Unsigned``) :versionbadge:`clang-format 3.7` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -3082,6 +3082,10 @@ /// \version 14 unsigned PenaltyBreakOpenParenthesis; + /// The penalty for breaking after ``return``. + /// \version 18 + unsigned PenaltyBreakReturn; + /// The penalty for each line break introduced inside a string literal. /// \version 3.7 unsigned PenaltyBreakString; @@ -4391,6 +4395,7 @@ PenaltyBreakComment == R.PenaltyBreakComment && PenaltyBreakFirstLessLess == R.PenaltyBreakFirstLessLess && PenaltyBreakOpenParenthesis == R.PenaltyBreakOpenParenthesis && + PenaltyBreakReturn == R.PenaltyBreakReturn && PenaltyBreakString == R.PenaltyBreakString && PenaltyBreakTemplateDeclaration == R.PenaltyBreakTemplateDeclaration && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -967,6 +967,7 @@ Style.PenaltyBreakFirstLessLess); IO.mapOptional("PenaltyBreakOpenParenthesis", Style.PenaltyBreakOpenParenthesis); + IO.mapOptional("PenaltyBreakReturn", Style.PenaltyBreakReturn); IO.mapOptional("PenaltyBreakString", Style.PenaltyBreakString); IO.mapOptional("PenaltyBreakTemplateDeclaration", Style.PenaltyBreakTemplateDeclaration); @@ -1480,6 +1481,7 @@ LLVMStyle.PenaltyBreakAssignment = prec::Assignment; LLVMStyle.PenaltyBreakComment = 300; LLVMStyle.PenaltyBreakFirstLessLess = 120; + LLVMStyle.PenaltyBreakReturn = 100; LLVMStyle.PenaltyBreakString = 1000; LLVMStyle.PenaltyExcessCharacter = 1000000; LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60; diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3525,6 +3525,8 @@ } } + if (Left.is(tok::kw_return)) + return Style.PenaltyBreakReturn; if (Left.is(tok::coloncolon)) return 500; if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) || @@ -5526,7 +5528,8 @@ } return Left.isOneOf(tok::comma, tok::coloncolon, tok::semi, tok::l_brace, - tok::kw_class, tok::kw_struct, tok::comment) || + tok::kw_class, tok::kw_struct, tok::comment, + tok::kw_return) || Right.isMemberAccess() || Right.isOneOf(TT_TrailingReturnArrow, TT_LambdaArrow, tok::lessless, tok::colon, tok::l_square, tok::at) || diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -8916,6 +8916,9 @@ verifyFormat("return aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) &&\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); + verifyFormat("return\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaa();"); verifyFormat("return\n" " // true if code is one of a or b.\n" " code == a || code == b;"); @@ -22179,9 +22182,9 @@ verifyFormat("FctWithLongLineInLambda_SLS_All(\n" " []()\n" " {\n" - " return " - "HereAVeryLongLineThatWillBeFormattedOnMultipleLineAndShouldNotB" - "eConsiderAsInline;\n" + " return\n" + " HereAVeryLongLineThatWillBeFormattedOnMultipleLineAnd" + "ShouldNotBeConsiderAsInline;\n" " });", LLVMWithBeforeLambdaBody); verifyFormat(