Skip to content

Commit 86f7720

Browse files
committed
[clang-format] Add BreakAfterReturnType option to deprecate AlwaysBreakAfterReturnType.
1 parent 0473e32 commit 86f7720

File tree

10 files changed

+167
-20
lines changed

10 files changed

+167
-20
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,8 @@ the configuration (without a prefix: ``Auto``).
15321532
.. _AlwaysBreakAfterReturnType:
15331533

15341534
**AlwaysBreakAfterReturnType** (``ReturnTypeBreakingStyle``) :versionbadge:`clang-format 3.8` :ref:`<AlwaysBreakAfterReturnType>`
1535-
The function declaration return type breaking style to use.
1535+
The function declaration return type breaking style to use. This
1536+
option is **deprecated** and is retained for backwards compatibility.
15361537

15371538
Possible values:
15381539

@@ -2261,6 +2262,117 @@ the configuration (without a prefix: ``Auto``).
22612262
@Mock
22622263
DataLoad loader;
22632264
2265+
.. _BreakAfterReturnType:
2266+
2267+
**BreakAfterReturnType** (``ReturnTypeBreakingStyle``) :versionbadge:`clang-format 19` :ref:`<BreakAfterReturnType>`
2268+
The function declaration return type breaking style to use.
2269+
2270+
Possible values:
2271+
2272+
* ``RTBS_None`` (in configuration: ``None``)
2273+
This is **deprecated**. See ``Automatic`` below.
2274+
2275+
* ``RTBS_Automatic`` (in configuration: ``Automatic``)
2276+
Break after return type based on ``PenaltyReturnTypeOnItsOwnLine``.
2277+
2278+
.. code-block:: c++
2279+
2280+
class A {
2281+
int f() { return 0; };
2282+
};
2283+
int f();
2284+
int f() { return 1; }
2285+
int
2286+
LongName::AnotherLongName();
2287+
2288+
* ``RTBS_ExceptShortType`` (in configuration: ``ExceptShortType``)
2289+
Same as ``Automatic`` above, except that there is no break after short
2290+
return types.
2291+
2292+
.. code-block:: c++
2293+
2294+
class A {
2295+
int f() { return 0; };
2296+
};
2297+
int f();
2298+
int f() { return 1; }
2299+
int LongName::
2300+
AnotherLongName();
2301+
2302+
* ``RTBS_All`` (in configuration: ``All``)
2303+
Always break after the return type.
2304+
2305+
.. code-block:: c++
2306+
2307+
class A {
2308+
int
2309+
f() {
2310+
return 0;
2311+
};
2312+
};
2313+
int
2314+
f();
2315+
int
2316+
f() {
2317+
return 1;
2318+
}
2319+
int
2320+
LongName::AnotherLongName();
2321+
2322+
* ``RTBS_TopLevel`` (in configuration: ``TopLevel``)
2323+
Always break after the return types of top-level functions.
2324+
2325+
.. code-block:: c++
2326+
2327+
class A {
2328+
int f() { return 0; };
2329+
};
2330+
int
2331+
f();
2332+
int
2333+
f() {
2334+
return 1;
2335+
}
2336+
int
2337+
LongName::AnotherLongName();
2338+
2339+
* ``RTBS_AllDefinitions`` (in configuration: ``AllDefinitions``)
2340+
Always break after the return type of function definitions.
2341+
2342+
.. code-block:: c++
2343+
2344+
class A {
2345+
int
2346+
f() {
2347+
return 0;
2348+
};
2349+
};
2350+
int f();
2351+
int
2352+
f() {
2353+
return 1;
2354+
}
2355+
int
2356+
LongName::AnotherLongName();
2357+
2358+
* ``RTBS_TopLevelDefinitions`` (in configuration: ``TopLevelDefinitions``)
2359+
Always break after the return type of top-level definitions.
2360+
2361+
.. code-block:: c++
2362+
2363+
class A {
2364+
int f() { return 0; };
2365+
};
2366+
int f();
2367+
int
2368+
f() {
2369+
return 1;
2370+
}
2371+
int
2372+
LongName::AnotherLongName();
2373+
2374+
2375+
22642376
.. _BreakArrays:
22652377

22662378
**BreakArrays** (``Boolean``) :versionbadge:`clang-format 16` :ref:`<BreakArrays>`

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ AST Matchers
270270
clang-format
271271
------------
272272

273+
- Add BreakAfterReturnType option to deprecate AlwaysBreakAfterReturnType.
274+
273275
libclang
274276
--------
275277

clang/include/clang/Format/Format.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,8 @@ struct FormatStyle {
10101010
/// \version 3.7
10111011
DefinitionReturnTypeBreakingStyle AlwaysBreakAfterDefinitionReturnType;
10121012

1013-
/// The function declaration return type breaking style to use.
1013+
/// The function declaration return type breaking style to use. This
1014+
/// option is **deprecated** and is retained for backwards compatibility.
10141015
/// \version 3.8
10151016
ReturnTypeBreakingStyle AlwaysBreakAfterReturnType;
10161017

@@ -1565,6 +1566,10 @@ struct FormatStyle {
15651566
/// \version 16
15661567
AttributeBreakingStyle BreakAfterAttributes;
15671568

1569+
/// The function declaration return type breaking style to use.
1570+
/// \version 19
1571+
ReturnTypeBreakingStyle BreakAfterReturnType;
1572+
15681573
/// If ``true``, clang-format will always break after a Json array ``[``
15691574
/// otherwise it will scan until the closing ``]`` to determine if it should
15701575
/// add newlines between elements (prettier compatible).
@@ -4804,7 +4809,6 @@ struct FormatStyle {
48044809
R.AllowShortIfStatementsOnASingleLine &&
48054810
AllowShortLambdasOnASingleLine == R.AllowShortLambdasOnASingleLine &&
48064811
AllowShortLoopsOnASingleLine == R.AllowShortLoopsOnASingleLine &&
4807-
AlwaysBreakAfterReturnType == R.AlwaysBreakAfterReturnType &&
48084812
AlwaysBreakBeforeMultilineStrings ==
48094813
R.AlwaysBreakBeforeMultilineStrings &&
48104814
AlwaysBreakTemplateDeclarations ==
@@ -4817,6 +4821,7 @@ struct FormatStyle {
48174821
BreakAdjacentStringLiterals == R.BreakAdjacentStringLiterals &&
48184822
BreakAfterAttributes == R.BreakAfterAttributes &&
48194823
BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations &&
4824+
BreakAfterReturnType == R.BreakAfterReturnType &&
48204825
BreakArrays == R.BreakArrays &&
48214826
BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators &&
48224827
BreakBeforeBraces == R.BreakBeforeBraces &&

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,12 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
329329
// Don't break after very short return types (e.g. "void") as that is often
330330
// unexpected.
331331
if (Current.is(TT_FunctionDeclarationName)) {
332-
if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_None &&
332+
if (Style.BreakAfterReturnType == FormatStyle::RTBS_None &&
333333
State.Column < 6) {
334334
return false;
335335
}
336336

337-
if (Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_ExceptShortType) {
337+
if (Style.BreakAfterReturnType == FormatStyle::RTBS_ExceptShortType) {
338338
assert(State.Column >= State.FirstIndent);
339339
if (State.Column - State.FirstIndent < 6)
340340
return false;
@@ -595,7 +595,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
595595
!State.Line->ReturnTypeWrapped &&
596596
// Don't break before a C# function when no break after return type.
597597
(!Style.isCSharp() ||
598-
Style.AlwaysBreakAfterReturnType > FormatStyle::RTBS_ExceptShortType) &&
598+
Style.BreakAfterReturnType > FormatStyle::RTBS_ExceptShortType) &&
599599
// Don't always break between a JavaScript `function` and the function
600600
// name.
601601
!Style.isJavaScript() && Previous.isNot(tok::kw_template) &&

clang/lib/Format/Format.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ template <> struct MappingTraits<FormatStyle> {
956956
IO.mapOptional("BreakAfterAttributes", Style.BreakAfterAttributes);
957957
IO.mapOptional("BreakAfterJavaFieldAnnotations",
958958
Style.BreakAfterJavaFieldAnnotations);
959+
IO.mapOptional("BreakAfterReturnType", Style.BreakAfterReturnType);
959960
IO.mapOptional("BreakArrays", Style.BreakArrays);
960961
IO.mapOptional("BreakBeforeBinaryOperators",
961962
Style.BreakBeforeBinaryOperators);
@@ -1137,6 +1138,14 @@ template <> struct MappingTraits<FormatStyle> {
11371138
}
11381139
}
11391140

1141+
// If AlwaysBreakAfterReturnType was specified but
1142+
// BreakAfterReturnType was not, initialize the latter from the
1143+
// former for backwards compatibility.
1144+
if (Style.AlwaysBreakAfterReturnType != FormatStyle::RTBS_None &&
1145+
Style.BreakAfterReturnType == FormatStyle::RTBS_None) {
1146+
Style.BreakAfterReturnType = Style.AlwaysBreakAfterReturnType;
1147+
}
1148+
11401149
// If BreakBeforeInheritanceComma was specified but BreakInheritance was
11411150
// not, initialize the latter from the former for backwards compatibility.
11421151
if (BreakBeforeInheritanceComma &&
@@ -1465,6 +1474,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14651474
LLVMStyle.BreakAdjacentStringLiterals = true;
14661475
LLVMStyle.BreakAfterAttributes = FormatStyle::ABS_Leave;
14671476
LLVMStyle.BreakAfterJavaFieldAnnotations = false;
1477+
LLVMStyle.BreakAfterReturnType = FormatStyle::RTBS_None;
14681478
LLVMStyle.BreakArrays = true;
14691479
LLVMStyle.BreakBeforeBinaryOperators = FormatStyle::BOS_None;
14701480
LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach;
@@ -1824,6 +1834,7 @@ FormatStyle getMozillaStyle() {
18241834
MozillaStyle.AlwaysBreakTemplateDeclarations = FormatStyle::BTDS_Yes;
18251835
MozillaStyle.BinPackParameters = false;
18261836
MozillaStyle.BinPackArguments = false;
1837+
MozillaStyle.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
18271838
MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
18281839
MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
18291840
MozillaStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
@@ -1868,6 +1879,7 @@ FormatStyle getGNUStyle() {
18681879
FormatStyle Style = getLLVMStyle();
18691880
Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
18701881
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
1882+
Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
18711883
Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All;
18721884
Style.BreakBeforeBraces = FormatStyle::BS_GNU;
18731885
Style.BreakBeforeTernaryOperators = true;
@@ -1905,6 +1917,7 @@ FormatStyle getMicrosoftStyle(FormatStyle::LanguageKind Language) {
19051917
Style.AllowShortLoopsOnASingleLine = false;
19061918
Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None;
19071919
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
1920+
Style.BreakAfterReturnType = FormatStyle::RTBS_None;
19081921
return Style;
19091922
}
19101923

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,14 +3425,13 @@ static bool isFunctionDeclarationName(bool IsCpp, const FormatToken &Current,
34253425
bool TokenAnnotator::mustBreakForReturnType(const AnnotatedLine &Line) const {
34263426
assert(Line.MightBeFunctionDecl);
34273427

3428-
if ((Style.AlwaysBreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3429-
Style.AlwaysBreakAfterReturnType ==
3430-
FormatStyle::RTBS_TopLevelDefinitions) &&
3428+
if ((Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevel ||
3429+
Style.BreakAfterReturnType == FormatStyle::RTBS_TopLevelDefinitions) &&
34313430
Line.Level > 0) {
34323431
return false;
34333432
}
34343433

3435-
switch (Style.AlwaysBreakAfterReturnType) {
3434+
switch (Style.BreakAfterReturnType) {
34363435
case FormatStyle::RTBS_None:
34373436
case FormatStyle::RTBS_Automatic:
34383437
case FormatStyle::RTBS_ExceptShortType:

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,22 @@ TEST(ConfigParseTest, ParsesConfiguration) {
677677
" AfterControlStatement: false",
678678
BraceWrapping.AfterControlStatement, FormatStyle::BWACS_Never);
679679

680+
Style.BreakAfterReturnType = FormatStyle::RTBS_All;
681+
CHECK_PARSE("BreakAfterReturnType: None", BreakAfterReturnType,
682+
FormatStyle::RTBS_None);
683+
CHECK_PARSE("BreakAfterReturnType: Automatic", BreakAfterReturnType,
684+
FormatStyle::RTBS_Automatic);
685+
CHECK_PARSE("BreakAfterReturnType: ExceptShortType", BreakAfterReturnType,
686+
FormatStyle::RTBS_ExceptShortType);
687+
CHECK_PARSE("BreakAfterReturnType: All", BreakAfterReturnType,
688+
FormatStyle::RTBS_All);
689+
CHECK_PARSE("BreakAfterReturnType: TopLevel", BreakAfterReturnType,
690+
FormatStyle::RTBS_TopLevel);
691+
CHECK_PARSE("BreakAfterReturnType: AllDefinitions", BreakAfterReturnType,
692+
FormatStyle::RTBS_AllDefinitions);
693+
CHECK_PARSE("BreakAfterReturnType: TopLevelDefinitions", BreakAfterReturnType,
694+
FormatStyle::RTBS_TopLevelDefinitions);
695+
680696
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
681697
CHECK_PARSE("AlwaysBreakAfterReturnType: None", AlwaysBreakAfterReturnType,
682698
FormatStyle::RTBS_None);

clang/unittests/Format/DefinitionBlockSeparatorTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ TEST_F(DefinitionBlockSeparatorTest, Basic) {
144144
Style);
145145

146146
FormatStyle BreakAfterReturnTypeStyle = Style;
147-
BreakAfterReturnTypeStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
147+
BreakAfterReturnTypeStyle.BreakAfterReturnType = FormatStyle::RTBS_All;
148148
// Test uppercased long typename
149149
verifyFormat("class Foo {\n"
150150
" void\n"

clang/unittests/Format/FormatTest.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9870,7 +9870,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
98709870
Style.ColumnLimit = 60;
98719871

98729872
// No declarations or definitions should be moved to own line.
9873-
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
9873+
Style.BreakAfterReturnType = FormatStyle::RTBS_None;
98749874
verifyFormat("class A {\n"
98759875
" int f() { return 1; }\n"
98769876
" int g();\n"
@@ -9884,7 +9884,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
98849884
Style);
98859885

98869886
// It is now allowed to break after a short return type if necessary.
9887-
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_Automatic;
9887+
Style.BreakAfterReturnType = FormatStyle::RTBS_Automatic;
98889888
verifyFormat("class A {\n"
98899889
" int f() { return 1; }\n"
98909890
" int g();\n"
@@ -9898,7 +9898,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
98989898
Style);
98999899

99009900
// It now must never break after a short return type.
9901-
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_ExceptShortType;
9901+
Style.BreakAfterReturnType = FormatStyle::RTBS_ExceptShortType;
99029902
verifyFormat("class A {\n"
99039903
" int f() { return 1; }\n"
99049904
" int g();\n"
@@ -9913,7 +9913,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
99139913

99149914
// All declarations and definitions should have the return type moved to its
99159915
// own line.
9916-
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
9916+
Style.BreakAfterReturnType = FormatStyle::RTBS_All;
99179917
Style.TypenameMacros = {"LIST"};
99189918
verifyFormat("SomeType\n"
99199919
"funcdecl(LIST(uint64_t));",
@@ -9940,7 +9940,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
99409940

99419941
// Top-level definitions, and no kinds of declarations should have the
99429942
// return type moved to its own line.
9943-
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
9943+
Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevelDefinitions;
99449944
verifyFormat("class B {\n"
99459945
" int f() { return 1; }\n"
99469946
" int g();\n"
@@ -9954,7 +9954,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
99549954

99559955
// Top-level definitions and declarations should have the return type moved
99569956
// to its own line.
9957-
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_TopLevel;
9957+
Style.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
99589958
verifyFormat("class C {\n"
99599959
" int f() { return 1; }\n"
99609960
" int g();\n"
@@ -9971,7 +9971,7 @@ TEST_F(FormatTest, ReturnTypeBreakingStyle) {
99719971

99729972
// All definitions should have the return type moved to its own line, but no
99739973
// kinds of declarations.
9974-
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
9974+
Style.BreakAfterReturnType = FormatStyle::RTBS_AllDefinitions;
99759975
verifyFormat("class D {\n"
99769976
" int\n"
99779977
" f() {\n"
@@ -11872,7 +11872,7 @@ TEST_F(FormatTest, UnderstandsAttributes) {
1187211872
"aaaaaaaaaaaaaaaaaaaaaaa(int i);");
1187311873
verifyFormat("__attribute__((nodebug)) ::qualified_type f();");
1187411874
FormatStyle AfterType = getLLVMStyle();
11875-
AfterType.AlwaysBreakAfterReturnType = FormatStyle::RTBS_All;
11875+
AfterType.BreakAfterReturnType = FormatStyle::RTBS_All;
1187611876
verifyFormat("__attribute__((nodebug)) void\n"
1187711877
"foo() {}",
1187811878
AfterType);

clang/unittests/Format/FormatTestCSharp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ TEST_F(FormatTestCSharp, CSharpNullForgiving) {
505505

506506
TEST_F(FormatTestCSharp, AttributesIndentation) {
507507
FormatStyle Style = getMicrosoftStyle(FormatStyle::LK_CSharp);
508-
Style.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None;
508+
Style.BreakAfterReturnType = FormatStyle::RTBS_None;
509509

510510
verifyFormat("[STAThread]\n"
511511
"static void Main(string[] args)\n"

0 commit comments

Comments
 (0)