[clang] [clang-format] Add ShortReturnTypeColumn option. (PR #78011)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 26 02:29:08 PST 2024
https://github.com/rmarker updated https://github.com/llvm/llvm-project/pull/78011
>From a1312a0a463bb946f336977b5b01ef7afbede678 Mon Sep 17 00:00:00 2001
From: rmarker <rmarker at outlook.com>
Date: Thu, 11 Jan 2024 15:01:18 +1030
Subject: [PATCH 1/3] [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 4dc0de3a90f2650..7836cc8f1c9bb5b 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 5330cd9caad8011..669b420fe21ec15 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -184,6 +184,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 bc9eecd42f9ebfd..7fd574c98a3944f 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 a3eb9138b218335..3f9c0cc815745c3 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 ff326dc784783b2..35478fac7b49629 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 2a8d79359a49b40..baf892b43320d16 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 e5e763edf5b5bf6..5325af89b90ee6b 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) {
>From 66f4c6af253575a258f26bb1f7afeab0e44ffb28 Mon Sep 17 00:00:00 2001
From: rmarker <rmarker at outlook.com>
Date: Fri, 26 Jan 2024 17:34:36 +1030
Subject: [PATCH 2/3] Take indentation into account for short return types.
---
clang/lib/Format/ContinuationIndenter.cpp | 2 +-
clang/unittests/Format/FormatTest.cpp | 9 ++++-----
clang/unittests/Format/FormatTestSelective.cpp | 3 +--
3 files changed, 6 insertions(+), 8 deletions(-)
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 3f9c0cc815745c3..3201bc0e7dc2c12 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -329,7 +329,7 @@ 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 <= Style.ShortReturnTypeColumn) {
+ State.Column - State.FirstIndent <= Style.ShortReturnTypeColumn) {
if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None)
return false;
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5325af89b90ee6b..778d98a4493b61c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12414,8 +12414,7 @@ TEST_F(FormatTest, BreaksLongDeclarations) {
" FunctionDeclaration();",
ShortReturnType);
verifyFormat("struct S {\n"
- " Type\n"
- " "
+ " Type "
"Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
"oooooooooooong::\n"
" FunctionDeclaration();\n"
@@ -14392,9 +14391,9 @@ TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
" bbbbbbbbbbbbbbbbbbb()\n"
" {\n"
" }\n"
- " void\n"
- " m(int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
- " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
+ " void m(\n"
+ " int aaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " int bbbbbbbbbbbbbbbbbbbbbbbb)\n"
" {\n"
" }\n"
"};",
diff --git a/clang/unittests/Format/FormatTestSelective.cpp b/clang/unittests/Format/FormatTestSelective.cpp
index c21c9bfe6079056..43de93d219b145c 100644
--- a/clang/unittests/Format/FormatTestSelective.cpp
+++ b/clang/unittests/Format/FormatTestSelective.cpp
@@ -522,8 +522,7 @@ TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
Style.ColumnLimit = 11;
EXPECT_EQ(" int a;\n"
- " void\n"
- " ffffff() {\n"
+ " void ffffff() {\n"
" }",
format(" int a;\n"
"void ffffff() {}",
>From 4024406a85d4509b4a0bdd41eb8cfe0441f2a3e7 Mon Sep 17 00:00:00 2001
From: rmarker <rmarker at outlook.com>
Date: Fri, 26 Jan 2024 20:52:15 +1030
Subject: [PATCH 3/3] Add AllowShortType option for AlwaysBreakAfterReturnType.
Replaced the new ShortReturnTypeColumn option with this alternative.
---
clang/docs/ClangFormatStyleOptions.rst | 38 +++++---
clang/docs/ReleaseNotes.rst | 2 +-
clang/include/clang/Format/Format.h | 33 ++++---
clang/lib/Format/ContinuationIndenter.cpp | 15 +--
clang/lib/Format/Format.cpp | 3 +-
clang/lib/Format/TokenAnnotator.cpp | 1 +
clang/unittests/Format/ConfigParseTest.cpp | 3 +-
clang/unittests/Format/FormatTest.cpp | 102 +++++++++++----------
8 files changed, 113 insertions(+), 84 deletions(-)
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index 7836cc8f1c9bb5b..9201a152b11625b 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -146,7 +146,7 @@ the configuration (without a prefix: ``Auto``).
.. _BasedOnStyle:
-**BasedOnStyle** (``String``) :ref:`¶ <BasedOnStyle>`
+**BasedOnStyle** (``String``) :ref:`¶ <BasedOnStyle>`
The style used for all options not specifically set in the configuration.
This option is supported only in the :program:`clang-format` configuration
@@ -1547,6 +1547,23 @@ the configuration (without a prefix: ``Auto``).
};
int f();
int f() { return 1; }
+ int foooooooooooooooooooooooooooooooooooooooooooooooo::
+ baaaaaaaaaaaaaaaaaaaaar();
+
+ * ``RTBS_AllowShortType`` (in configuration: ``AllowShortType``)
+ Break after return type automatically, while allowing a break after
+ short return types.
+ ``PenaltyReturnTypeOnItsOwnLine`` is taken into account.
+
+ .. code-block:: c++
+
+ class A {
+ int f() { return 0; };
+ };
+ int f();
+ int f() { return 1; }
+ int
+ foooooooooooooooooooooooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();
* ``RTBS_All`` (in configuration: ``All``)
Always break after the return type.
@@ -1580,6 +1597,8 @@ the configuration (without a prefix: ``Auto``).
f() {
return 1;
}
+ int
+ foooooooooooooooooooooooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();
* ``RTBS_AllDefinitions`` (in configuration: ``AllDefinitions``)
Always break after the return type of function definitions.
@@ -1597,6 +1616,8 @@ the configuration (without a prefix: ``Auto``).
f() {
return 1;
}
+ int
+ foooooooooooooooooooooooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();
* ``RTBS_TopLevelDefinitions`` (in configuration: ``TopLevelDefinitions``)
Always break after the return type of top-level definitions.
@@ -1611,6 +1632,8 @@ the configuration (without a prefix: ``Auto``).
f() {
return 1;
}
+ int
+ foooooooooooooooooooooooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();
@@ -4999,19 +5022,6 @@ 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 669b420fe21ec15..56befd7eefb2fa8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -184,7 +184,7 @@ AST Matchers
clang-format
------------
-- Add ``ShortReturnTypeColumn`` option.
+- Add ``AllowShortType`` option for ``AlwaysBreakAfterReturnType``.
libclang
--------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 7fd574c98a3944f..02f1c6c8b30e97c 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -922,8 +922,23 @@ struct FormatStyle {
/// };
/// int f();
/// int f() { return 1; }
+ /// int foooooooooooooooooooooooooooooooooooooooooooooooo::
+ /// baaaaaaaaaaaaaaaaaaaaar();
/// \endcode
RTBS_None,
+ /// Break after return type automatically, while allowing a break after
+ /// short return types.
+ /// ``PenaltyReturnTypeOnItsOwnLine`` is taken into account.
+ /// \code
+ /// class A {
+ /// int f() { return 0; };
+ /// };
+ /// int f();
+ /// int f() { return 1; }
+ /// int
+ /// foooooooooooooooooooooooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();
+ /// \endcode
+ RTBS_AllowShortType,
/// Always break after the return type.
/// \code
/// class A {
@@ -951,6 +966,8 @@ struct FormatStyle {
/// f() {
/// return 1;
/// }
+ /// int
+ /// foooooooooooooooooooooooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();
/// \endcode
RTBS_TopLevel,
/// Always break after the return type of function definitions.
@@ -966,6 +983,8 @@ struct FormatStyle {
/// f() {
/// return 1;
/// }
+ /// int
+ /// foooooooooooooooooooooooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();
/// \endcode
RTBS_AllDefinitions,
/// Always break after the return type of top-level definitions.
@@ -978,6 +997,8 @@ struct FormatStyle {
/// f() {
/// return 1;
/// }
+ /// int
+ /// foooooooooooooooooooooooooooooooooooooooooooooooo::baaaaaaaaaaaaaaaaaaaaar();
/// \endcode
RTBS_TopLevelDefinitions,
};
@@ -3932,17 +3953,6 @@ 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;
@@ -4910,7 +4920,6 @@ 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 3201bc0e7dc2c12..ec0fc7fb9243bd6 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -326,12 +326,13 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
return false;
}
- // Don't break after very short return types (e.g. "void") as that is often
- // unexpected.
- if (Current.is(TT_FunctionDeclarationName) &&
- State.Column - State.FirstIndent <= Style.ShortReturnTypeColumn) {
- if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None)
+ if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None) {
+ // Don't break after very short return types (e.g. "void") as that is often
+ // unexpected.
+ if (Current.is(TT_FunctionDeclarationName) &&
+ State.Column - State.FirstIndent < 6) {
return false;
+ }
}
// If binary operators are moved to the next line (including commas for some
@@ -588,7 +589,9 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
!State.Line->ReturnTypeWrapped &&
// Don't break before a C# function when no break after return type.
(!Style.isCSharp() ||
- Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None) &&
+ (Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None &&
+ Style.AlwaysBreakAfterReturnType !=
+ FormatStyle::RTBS_AllowShortType)) &&
// Don't always break between a JavaScript `function` and the function
// name.
!Style.isJavaScript() && Previous.isNot(tok::kw_template) &&
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 35478fac7b49629..a1034ce7c3266b9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -574,6 +574,7 @@ template <>
struct ScalarEnumerationTraits<FormatStyle::ReturnTypeBreakingStyle> {
static void enumeration(IO &IO, FormatStyle::ReturnTypeBreakingStyle &Value) {
IO.enumCase(Value, "None", FormatStyle::RTBS_None);
+ IO.enumCase(Value, "AllowShortType", FormatStyle::RTBS_AllowShortType);
IO.enumCase(Value, "All", FormatStyle::RTBS_All);
IO.enumCase(Value, "TopLevel", FormatStyle::RTBS_TopLevel);
IO.enumCase(Value, "TopLevelDefinitions",
@@ -1085,7 +1086,6 @@ 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);
@@ -1558,7 +1558,6 @@ 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/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 25fcceb87864379..81d866e8a2f4705 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3432,6 +3432,7 @@ bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
switch (Style.AlwaysBreakAfterReturnType) {
case FormatStyle::RTBS_None:
+ case FormatStyle::RTBS_AllowShortType:
return false;
case FormatStyle::RTBS_All:
case FormatStyle::RTBS_TopLevel:
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index baf892b43320d16..8b1b289508a5860 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -261,7 +261,6 @@ 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);
@@ -698,6 +697,8 @@ TEST(ConfigParseTest, ParsesConfiguration) {
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
FormatStyle::RTBS_None);
+ CHECK_PARSE("AlwaysBreakAfterReturnType: AllowShortType",
+ AlwaysBreakAfterReturnType, FormatStyle::RTBS_AllowShortType);
CHECK_PARSE("AlwaysBreakAfterReturnType: All", AlwaysBreakAfterReturnType,
FormatStyle::RTBS_All);
CHECK_PARSE("AlwaysBreakAfterReturnType: TopLevel",
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 778d98a4493b61c..c2f45767519346e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9872,9 +9872,30 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
verifyFormat("class A {\n"
" int f() { return 1; }\n"
" int g();\n"
+ " long fooooooooooooooooooooooooooooooooooooooooooooooo::\n"
+ " baaaaaaaaaaaaaaaaaaaar();\n"
"};\n"
"int f() { return 1; }\n"
- "int g();",
+ "int g();\n"
+ "int foooooooooooooooooooooooooooooooooooooooooooooooo::\n"
+ " baaaaaaaaaaaaaaaaaaaaar();",
+ Style);
+
+ // It is now allowed to break after a short return type if necessary.
+ Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllowShortType;
+ verifyFormat("class A {\n"
+ " int f() { return 1; }\n"
+ " int g();\n"
+ " long\n"
+ " "
+ "fooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaar();\n"
+ "};\n"
+ "int f() { return 1; }\n"
+ "int g();\n"
+ "int\n"
+ "foooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaaar();",
Style);
// All declarations and definitions should have the return type moved to its
@@ -9891,13 +9912,20 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
" }\n"
" int\n"
" g();\n"
+ " long\n"
+ " "
+ "fooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaar();\n"
"};\n"
"int\n"
"f() {\n"
" return 1;\n"
"}\n"
"int\n"
- "g();",
+ "g();\n"
+ "int\n"
+ "foooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaaar();",
Style);
// Top-level definitions, and no kinds of declarations should have the
@@ -9906,12 +9934,19 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
verifyFormat("class B {\n"
" int f() { return 1; }\n"
" int g();\n"
+ " long\n"
+ " "
+ "fooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaar();\n"
"};\n"
"int\n"
"f() {\n"
" return 1;\n"
"}\n"
- "int g();",
+ "int g();\n"
+ "int\n"
+ "foooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaaar();",
Style);
// Top-level definitions and declarations should have the return type moved
@@ -9920,13 +9955,20 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
verifyFormat("class C {\n"
" int f() { return 1; }\n"
" int g();\n"
+ " long\n"
+ " "
+ "fooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaar();\n"
"};\n"
"int\n"
"f() {\n"
" return 1;\n"
"}\n"
"int\n"
- "g();",
+ "g();\n"
+ "int\n"
+ "foooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaaar();",
Style);
// All definitions should have the return type moved to its own line, but no
@@ -9938,12 +9980,19 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
" return 1;\n"
" }\n"
" int g();\n"
+ " long\n"
+ " "
+ "fooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaar();\n"
"};\n"
"int\n"
"f() {\n"
" return 1;\n"
"}\n"
- "int g();",
+ "int g();\n"
+ "int\n"
+ "foooooooooooooooooooooooooooooooooooooooooooooooo::"
+ "baaaaaaaaaaaaaaaaaaaaar();",
Style);
verifyFormat("const char *\n"
"f(void) {\n" // Break here.
@@ -12406,49 +12455,6 @@ 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 "
- "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