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

via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 12 17:32:04 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format

@llvm/pr-subscribers-clang

Author: None (rmarker)

<details>
<summary>Changes</summary>

Resolves #<!-- -->78010

---
Full diff: https://github.com/llvm/llvm-project/pull/78011.diff


7 Files Affected:

- (modified) clang/docs/ClangFormatStyleOptions.rst (+8) 
- (modified) clang/docs/ReleaseNotes.rst (+1) 
- (modified) clang/include/clang/Format/Format.h (+8) 
- (modified) clang/lib/Format/ContinuationIndenter.cpp (+2-1) 
- (modified) clang/lib/Format/Format.cpp (+2) 
- (modified) clang/unittests/Format/ConfigParseTest.cpp (+1) 
- (modified) clang/unittests/Format/FormatTest.cpp (+44) 


``````````diff
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index ac9a0b70ed5daa..3255ceb0aba75b 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -4994,6 +4994,14 @@ the configuration (without a prefix: ``Auto``).
        int bar;                           int bar;
      } // namespace b                   } // namespace b
 
+.. _ShortReturnTypeLength:
+
+**ShortReturnTypeLength** (``Unsigned``) :versionbadge:`clang-format 18` :ref:`¶ <ShortReturnTypeLength>`
+  When AlwaysBreakAfterReturnType is None, line breaks are prevented after
+  short return types. This configures the character limit for a type to be
+  regarded as short. Note that this isn't the length of the type itself,
+  but the column where it finishes. I.e. it includes indentation, etc.
+
 .. _SortIncludes:
 
 **SortIncludes** (``SortIncludesOptions``) :versionbadge:`clang-format 3.8` :ref:`¶ <SortIncludes>`
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3cbce1be159437..04bf5cd4e768f3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -1131,6 +1131,7 @@ clang-format
 - Add ``BreakAdjacentStringLiterals`` option.
 - Add ``ObjCPropertyAttributeOrder`` which can be used to sort ObjC property
   attributes (like ``nonatomic, strong, nullable``).
+- Add ``ShortReturnTypeLength`` option.
 - Add ``.clang-format-ignore`` files.
 - Add ``AlignFunctionPointers`` sub-option for ``AlignConsecutiveDeclarations``.
 
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 5ffd63ee73fc36..f94d68f2cf2a85 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -3928,6 +3928,13 @@ struct FormatStyle {
   /// \version 13
   unsigned ShortNamespaceLines;
 
+  /// When AlwaysBreakAfterReturnType is None, line breaks are prevented after
+  /// short return types. This configures the character limit for a type to be
+  /// regarded as short. Note that this isn't the length of the type itself,
+  /// but the column where it finishes. I.e. it includes indentation, etc.
+  /// \version 18
+  unsigned ShortReturnTypeLength;
+
   /// Include sorting options.
   enum SortIncludesOptions : int8_t {
     /// Includes are never sorted.
@@ -4890,6 +4897,7 @@ struct FormatStyle {
            RequiresExpressionIndentation == R.RequiresExpressionIndentation &&
            SeparateDefinitionBlocks == R.SeparateDefinitionBlocks &&
            ShortNamespaceLines == R.ShortNamespaceLines &&
+           ShortReturnTypeLength == R.ShortReturnTypeLength &&
            SortIncludes == R.SortIncludes &&
            SortJavaStaticImport == R.SortJavaStaticImport &&
            SpaceAfterCStyleCast == R.SpaceAfterCStyleCast &&
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 102504182c4505..bc0748ec52e676 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.ShortReturnTypeLength) {
     if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None)
       return false;
   }
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ff5ed6c306f383..20ffbeef7e9a6e 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1083,6 +1083,7 @@ template <> struct MappingTraits<FormatStyle> {
                    Style.RequiresExpressionIndentation);
     IO.mapOptional("SeparateDefinitionBlocks", Style.SeparateDefinitionBlocks);
     IO.mapOptional("ShortNamespaceLines", Style.ShortNamespaceLines);
+    IO.mapOptional("ShortReturnTypeLength", Style.ShortReturnTypeLength);
     IO.mapOptional("SortIncludes", Style.SortIncludes);
     IO.mapOptional("SortJavaStaticImport", Style.SortJavaStaticImport);
     IO.mapOptional("SortUsingDeclarations", Style.SortUsingDeclarations);
@@ -1554,6 +1555,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
   LLVMStyle.RequiresExpressionIndentation = FormatStyle::REI_OuterScope;
   LLVMStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Leave;
   LLVMStyle.ShortNamespaceLines = 1;
+  LLVMStyle.ShortReturnTypeLength = 5;
   LLVMStyle.SortIncludes = FormatStyle::SI_CaseSensitive;
   LLVMStyle.SortJavaStaticImport = FormatStyle::SJSIO_Before;
   LLVMStyle.SortUsingDeclarations = FormatStyle::SUD_LexicographicNumeric;
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 18ecba270e3455..dcd8f768d1ab5a 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -258,6 +258,7 @@ TEST(ConfigParseTest, ParsesConfiguration) {
   CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
   CHECK_PARSE("PenaltyReturnTypeOnItsOwnLine: 1234",
               PenaltyReturnTypeOnItsOwnLine, 1234u);
+  CHECK_PARSE("ShortReturnTypeLength: 1234", ShortReturnTypeLength, 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 340ae39cb22b03..47b1018172590a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12398,6 +12398,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"
+               "      FunctionDefinition();\n"
+               "}",
+               ShortReturnType);
+
+  ShortReturnType.ShortReturnTypeLength = 0;
+  verifyFormat("Type\n"
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "ooooooooong::\n"
+               "    FunctionDeclaration();",
+               ShortReturnType);
+  verifyFormat("struct S {\n"
+               "  Type\n"
+               "  "
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "oooooooooooong::\n"
+               "      FunctionDefinition();\n"
+               "}",
+               ShortReturnType);
+
+  ShortReturnType.ShortReturnTypeLength = 7;
+  verifyFormat("Type "
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "ooooooooong::\n"
+               "    FunctionDeclaration();",
+               ShortReturnType);
+  verifyFormat("struct S {\n"
+               "  Type "
+               "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
+               "oooooooooooong::\n"
+               "      FunctionDefinition();\n"
+               "}",
+               ShortReturnType);
 }
 
 TEST_F(FormatTest, FormatsAccessModifiers) {

``````````

</details>


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


More information about the cfe-commits mailing list