[clang] [clang-format] Add ShortReturnTypeColumn option. (PR #78011)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 25 21:25:04 PST 2024


https://github.com/rmarker updated https://github.com/llvm/llvm-project/pull/78011

>From bada422a19015c6f84558a302fa9ef4026e01689 Mon Sep 17 00:00:00 2001
From: rmarker <rmarker at outlook.com>
Date: Thu, 11 Jan 2024 15:01:18 +1030
Subject: [PATCH] [clang-format] Add ShortReturnTypeColumn option.

---
 clang/docs/ClangFormatStyleOptions.rst     | 13 +++++++
 clang/docs/ReleaseNotes.rst                |  1 +
 clang/include/clang/Format/Format.h        | 12 ++++++
 clang/lib/Format/ContinuationIndenter.cpp  |  3 +-
 clang/lib/Format/Format.cpp                |  2 +
 clang/unittests/Format/ConfigParseTest.cpp |  1 +
 clang/unittests/Format/FormatTest.cpp      | 44 ++++++++++++++++++++++
 7 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 4dc0de3a90f265..7836cc8f1c9bb5 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -4999,6 +4999,19 @@ the configuration (without a prefix: ``Auto``).
        int bar;                           int bar;
      } // namespace b                   } // namespace b
 
+.. _ShortReturnTypeColumn:
+
+**ShortReturnTypeColumn** (``Unsigned``) :versionbadge:`clang-format 19` :ref:`¶ <ShortReturnTypeColumn>`
+  When ``AlwaysBreakAfterReturnType`` is ``None``, line breaks are prevented
+  after short return types. This configures the column limit for a type
+  to be regarded as short.
+
+
+  .. note::
+
+   This isn't the length of the type itself, but the column where it
+   finishes. I.e. it includes indentation, etc.
+
 .. _SkipMacroDefinitionBody:
 
 **SkipMacroDefinitionBody** (``Boolean``) :versionbadge:`clang-format 18` :ref:`¶ <SkipMacroDefinitionBody>`
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c4c257d47a0a4b..5cda4ef226913d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -177,6 +177,7 @@ AST Matchers
 
 clang-format
 ------------
+- Add ``ShortReturnTypeColumn`` option.
 
 libclang
 --------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index bc9eecd42f9ebf..7fd574c98a3944 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -3932,6 +3932,17 @@ struct FormatStyle {
   /// \version 13
   unsigned ShortNamespaceLines;
 
+  /// When ``AlwaysBreakAfterReturnType`` is ``None``, line breaks are prevented
+  /// after short return types. This configures the column limit for a type
+  /// to be regarded as short.
+  ///
+  /// \note
+  ///  This isn't the length of the type itself, but the column where it
+  ///  finishes. I.e. it includes indentation, etc.
+  /// \endnote
+  /// \version 19
+  unsigned ShortReturnTypeColumn;
+
   /// Do not format macro definition body.
   /// \version 18
   bool SkipMacroDefinitionBody;
@@ -4899,6 +4910,7 @@ struct FormatStyle {
            RequiresExpressionIndentation == R.RequiresExpressionIndentation &&
            SeparateDefinitionBlocks == R.SeparateDefinitionBlocks &&
            ShortNamespaceLines == R.ShortNamespaceLines &&
+           ShortReturnTypeColumn == R.ShortReturnTypeColumn &&
            SkipMacroDefinitionBody == R.SkipMacroDefinitionBody &&
            SortIncludes == R.SortIncludes &&
            SortJavaStaticImport == R.SortJavaStaticImport &&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index a3eb9138b21833..3f9c0cc815745c 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -328,7 +328,8 @@ 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) && State.Column < 6) {
+  if (Current.is(TT_FunctionDeclarationName) &&
+      State.Column <= Style.ShortReturnTypeColumn) {
     if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None)
       return false;
   }
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ff326dc784783b..35478fac7b4962 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1085,6 +1085,7 @@ template <> struct MappingTraits<FormatStyle> {
                    Style.RequiresExpressionIndentation);
     IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
     IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
+    IO.mapOptional("ShortReturnTypeColumn", Style.ShortReturnTypeColumn);
     IO.mapOptional("SkipMacroDefinitionBody", Style.SkipMacroDefinitionBody);
     IO.mapOptional("SortIncludes", Style.SortIncludes);
     IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
@@ -1557,6 +1558,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
   LLVMStyle.RequiresExpressionIndentation = FormatStyle::REI_OuterScope;
   LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
   LLVMStyle.ShortNamespaceLines = 1;
+  LLVMStyle.ShortReturnTypeColumn = 5;
   LLVMStyle.SkipMacroDefinitionBody = false;
   LLVMStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
   LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 2a8d79359a49b4..baf892b43320d1 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -261,6 +261,7 @@ TEST(ConfigParseTest, ParsesConfiguration) {
   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
               PenaltyReturnTypeOnItsOwnLine, 1234u);
+  CHECK_PARSE("ShortReturnTypeColumn: 1234", ShortReturnTypeColumn, 1234u);
   CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
               SpacesBeforeTrailingComments, 1234u);
   CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index e5e763edf5b5bf..5325af89b90ee6 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12406,6 +12406,50 @@ TEST_F(FormatTest, BreaksLongDeclarations) {
   verifyFormat("template <typename T> // Templates on own line.\n"
                "static int            // Some comment.\n"
                "MyFunction(int a);");
+
+  FormatStyle ShortReturnType = getLLVMStyle();
+  verifyFormat("Type "
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "ooooooooong::\n"
+               "    FunctionDeclaration();",
+               ShortReturnType);
+  verifyFormat("struct S {\n"
+               "  Type\n"
+               "  "
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "oooooooooooong::\n"
+               "      FunctionDeclaration();\n"
+               "}",
+               ShortReturnType);
+
+  ShortReturnType.ShortReturnTypeColumn = 0;
+  verifyFormat("Type\n"
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "ooooooooong::\n"
+               "    FunctionDeclaration();",
+               ShortReturnType);
+  verifyFormat("struct S {\n"
+               "  Type\n"
+               "  "
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "oooooooooooong::\n"
+               "      FunctionDeclaration();\n"
+               "}",
+               ShortReturnType);
+
+  ShortReturnType.ShortReturnTypeColumn = 7;
+  verifyFormat("Type "
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "ooooooooong::\n"
+               "    FunctionDeclaration();",
+               ShortReturnType);
+  verifyFormat("struct S {\n"
+               "  Type "
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "oooooooooooong::\n"
+               "      FunctionDeclaration();\n"
+               "}",
+               ShortReturnType);
 }
 
 TEST_F(FormatTest, FormatsAccessModifiers) {



More information about the cfe-commits mailing list