[PATCH] D152975: [clang-format] Allow break after return keyword

Gedare Bloom via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 14 15:31:21 PDT 2023


gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.
Herald added a comment.

NOTE: Clang-Format Team Automated Review Comment

Your review contains a change to clang/include/clang/Format/Format.h but does not contain an update to ClangFormatStyleOptions.rst

ClangFormatStyleOptions.rst is generated via clang/docs/tools/dump_format_style.py,  please run this to regenerate the .rst

You can validate that the rst is valid by running.

  ./docs/tools/dump_format_style.py
  mkdir -p html
  /usr/bin/sphinx-build -n ./docs ./html


Currently new line breaks are not added between the `return` keyword
and the return value. With long, singleton return values, it can be
preferred to break before the return value. An example of this would be
a lengthy string return value.

Adds a new style option PenaltyBreakReturn to control when breaks are
preferred. With the current setting of 100, most existing unit tests pass.
One unit test needed to be tweaked, as it assumes very long return values
do not get broken from the return keyword. Added a new unit test to exercise
the long return value.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152975

Files:
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ 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(
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ 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) ||
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ 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;
Index: clang/include/clang/Format/Format.h
===================================================================
--- clang/include/clang/Format/Format.h
+++ 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 &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152975.531553.patch
Type: text/x-patch
Size: 4031 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230614/38cdd01e/attachment.bin>


More information about the cfe-commits mailing list