[clang] [clang-format] Add ShortReturnTypeLength option. (PR #78011)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 12 17:31:23 PST 2024
https://github.com/rmarker created https://github.com/llvm/llvm-project/pull/78011
Resolves #78010
>From c4d28f82e108f9f12ccd0375e2a3502025b8c1e8 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 ShortReturnTypeLength option.
---
clang/docs/ClangFormatStyleOptions.rst | 8 ++++
clang/docs/ReleaseNotes.rst | 1 +
clang/include/clang/Format/Format.h | 8 ++++
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, 66 insertions(+), 1 deletion(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index ac9a0b70ed5daa4..3255ceb0aba75b4 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 3cbce1be1594376..04bf5cd4e768f34 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 5ffd63ee73fc361..f94d68f2cf2a853 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 102504182c4505b..bc0748ec52e6769 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 ff5ed6c306f383b..20ffbeef7e9a6e9 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 18ecba270e3455a..dcd8f768d1ab5ad 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 340ae39cb22b036..47b1018172590ac 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) {
More information about the cfe-commits
mailing list