Skip to content

Commit e6f04cd

Browse files
committed
[clang-format] Deprecate BinPackParameters and add BreakParameters.
BreakParameters includes a new break always value.
1 parent 4377656 commit e6f04cd

File tree

10 files changed

+303
-86
lines changed

10 files changed

+303
-86
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ the configuration (without a prefix: ``Auto``).
16171617
**AllowAllParametersOfDeclarationOnNextLine** (``Boolean``) :versionbadge:`clang-format 3.3` :ref:`<AllowAllParametersOfDeclarationOnNextLine>`
16181618
If the function declaration doesn't fit on a line,
16191619
allow putting all parameters of a function declaration onto
1620-
the next line even if ``BinPackParameters`` is ``false``.
1620+
the next line even if ``BreakParameters`` is ``OnePerLine``.
16211621

16221622
.. code-block:: c++
16231623

@@ -2068,19 +2068,7 @@ the configuration (without a prefix: ``Auto``).
20682068
.. _BinPackParameters:
20692069

20702070
**BinPackParameters** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`<BinPackParameters>`
2071-
If ``false``, a function declaration's or function definition's
2072-
parameters will either all be on the same line or will have one line each.
2073-
2074-
.. code-block:: c++
2075-
2076-
true:
2077-
void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa,
2078-
int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
2079-
2080-
false:
2081-
void f(int aaaaaaaaaaaaaaaaaaaa,
2082-
int aaaaaaaaaaaaaaaaaaaa,
2083-
int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
2071+
This option is **deprecated**. See ``BreakParameters``.
20842072

20852073
.. _BitFieldColonSpacing:
20862074

@@ -3401,6 +3389,44 @@ the configuration (without a prefix: ``Auto``).
34013389

34023390

34033391

3392+
.. _BreakParameters:
3393+
3394+
**BreakParameters** (``BreakParametersStyle``) :versionbadge:`clang-format 20` :ref:`<BreakParameters>`
3395+
The break parameters style to use.
3396+
3397+
Possible values:
3398+
3399+
* ``BRPS_OnePerLine`` (in configuration: ``OnePerLine``)
3400+
Put all parameters on the current line if they fit.
3401+
Otherwise, put each one on its own line.
3402+
3403+
.. code-block:: c++
3404+
3405+
void f(int a, int b, int c);
3406+
3407+
void f(int a,
3408+
int b,
3409+
int ccccccccccccccccccccccccccccccccccccc);
3410+
3411+
* ``BRPS_Never`` (in configuration: ``Never``)
3412+
Bin-pack parameters.
3413+
3414+
.. code-block:: c++
3415+
3416+
void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
3417+
int ccccccccccccccccccccccccccccccccccccccccccc);
3418+
3419+
* ``BRPS_Always`` (in configuration: ``Always``)
3420+
Always put each parameter on its own line.
3421+
3422+
.. code-block:: c++
3423+
3424+
void f(int a,
3425+
int b,
3426+
int c);
3427+
3428+
3429+
34043430
.. _BreakStringLiterals:
34053431

34063432
**BreakStringLiterals** (``Boolean``) :versionbadge:`clang-format 3.9` :ref:`<BreakStringLiterals>`
@@ -4776,7 +4802,7 @@ the configuration (without a prefix: ``Auto``).
47764802
items into as few lines as possible when they go over ``ColumnLimit``.
47774803

47784804
If ``Auto`` (the default), delegates to the value in
4779-
``BinPackParameters``. If that is ``true``, bin-packs Objective-C
4805+
``BreakParameters``. If that is ``Never``, bin-packs Objective-C
47804806
protocol conformance list items into as few lines as possible
47814807
whenever they go over ``ColumnLimit``.
47824808

@@ -4790,13 +4816,13 @@ the configuration (without a prefix: ``Auto``).
47904816

47914817
.. code-block:: objc
47924818
4793-
Always (or Auto, if BinPackParameters=true):
4819+
Always (or Auto, if BreakParameters==Never):
47944820
@interface ccccccccccccc () <
47954821
ccccccccccccc, ccccccccccccc,
47964822
ccccccccccccc, ccccccccccccc> {
47974823
}
47984824
4799-
Never (or Auto, if BinPackParameters=false):
4825+
Never (or Auto, if BreakParameters!=Never):
48004826
@interface ddddddddddddd () <
48014827
ddddddddddddd,
48024828
ddddddddddddd,

clang/include/clang/Format/Format.h

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ struct FormatStyle {
659659

660660
/// If the function declaration doesn't fit on a line,
661661
/// allow putting all parameters of a function declaration onto
662-
/// the next line even if ``BinPackParameters`` is ``false``.
662+
/// the next line even if ``BreakParameters`` is ``OnePerLine``.
663663
/// \code
664664
/// true:
665665
/// void myFunction(
@@ -1192,20 +1192,9 @@ struct FormatStyle {
11921192
/// \version 3.7
11931193
bool BinPackArguments;
11941194

1195-
/// If ``false``, a function declaration's or function definition's
1196-
/// parameters will either all be on the same line or will have one line each.
1197-
/// \code
1198-
/// true:
1199-
/// void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa,
1200-
/// int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
1201-
///
1202-
/// false:
1203-
/// void f(int aaaaaaaaaaaaaaaaaaaa,
1204-
/// int aaaaaaaaaaaaaaaaaaaa,
1205-
/// int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
1206-
/// \endcode
1195+
/// This option is **deprecated**. See ``BreakParameters``.
12071196
/// \version 3.7
1208-
bool BinPackParameters;
1197+
// bool BinPackParameters;
12091198

12101199
/// Styles for adding spacing around ``:`` in bitfield definitions.
12111200
enum BitFieldColonSpacingStyle : int8_t {
@@ -2386,6 +2375,37 @@ struct FormatStyle {
23862375
/// \version 7
23872376
BreakInheritanceListStyle BreakInheritanceList;
23882377

2378+
/// Different ways to break parameters.
2379+
enum BreakParametersStyle : int8_t {
2380+
/// Put all parameters on the current line if they fit.
2381+
/// Otherwise, put each one on its own line.
2382+
/// \code
2383+
/// void f(int a, int b, int c);
2384+
///
2385+
/// void f(int a,
2386+
/// int b,
2387+
/// int ccccccccccccccccccccccccccccccccccccc);
2388+
/// \endcode
2389+
BRPS_OnePerLine,
2390+
/// Bin-pack parameters.
2391+
/// \code
2392+
/// void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
2393+
/// int ccccccccccccccccccccccccccccccccccccccccccc);
2394+
/// \endcode
2395+
BRPS_Never,
2396+
/// Always put each parameter on its own line.
2397+
/// \code
2398+
/// void f(int a,
2399+
/// int b,
2400+
/// int c);
2401+
/// \endcode
2402+
BRPS_Always,
2403+
};
2404+
2405+
/// The break parameters style to use.
2406+
/// \version 20
2407+
BreakParametersStyle BreakParameters;
2408+
23892409
/// The template declaration breaking style to use.
23902410
/// \version 19
23912411
BreakTemplateDeclarationsStyle BreakTemplateDeclarations;
@@ -3378,7 +3398,7 @@ struct FormatStyle {
33783398
/// items into as few lines as possible when they go over ``ColumnLimit``.
33793399
///
33803400
/// If ``Auto`` (the default), delegates to the value in
3381-
/// ``BinPackParameters``. If that is ``true``, bin-packs Objective-C
3401+
/// ``BreakParameters``. If that is ``Never``, bin-packs Objective-C
33823402
/// protocol conformance list items into as few lines as possible
33833403
/// whenever they go over ``ColumnLimit``.
33843404
///
@@ -3390,13 +3410,13 @@ struct FormatStyle {
33903410
/// onto individual lines whenever they go over ``ColumnLimit``.
33913411
///
33923412
/// \code{.objc}
3393-
/// Always (or Auto, if BinPackParameters=true):
3413+
/// Always (or Auto, if BreakParameters==Never):
33943414
/// @interface ccccccccccccc () <
33953415
/// ccccccccccccc, ccccccccccccc,
33963416
/// ccccccccccccc, ccccccccccccc> {
33973417
/// }
33983418
///
3399-
/// Never (or Auto, if BinPackParameters=false):
3419+
/// Never (or Auto, if BreakParameters!=Never):
34003420
/// @interface ddddddddddddd () <
34013421
/// ddddddddddddd,
34023422
/// ddddddddddddd,
@@ -5024,7 +5044,6 @@ struct FormatStyle {
50245044
R.AlwaysBreakBeforeMultilineStrings &&
50255045
AttributeMacros == R.AttributeMacros &&
50265046
BinPackArguments == R.BinPackArguments &&
5027-
BinPackParameters == R.BinPackParameters &&
50285047
BitFieldColonSpacing == R.BitFieldColonSpacing &&
50295048
BracedInitializerIndentWidth == R.BracedInitializerIndentWidth &&
50305049
BreakAdjacentStringLiterals == R.BreakAdjacentStringLiterals &&
@@ -5041,6 +5060,7 @@ struct FormatStyle {
50415060
BreakFunctionDefinitionParameters ==
50425061
R.BreakFunctionDefinitionParameters &&
50435062
BreakInheritanceList == R.BreakInheritanceList &&
5063+
BreakParameters == R.BreakParameters &&
50445064
BreakStringLiterals == R.BreakStringLiterals &&
50455065
BreakTemplateDeclarations == R.BreakTemplateDeclarations &&
50465066
ColumnLimit == R.ColumnLimit && CommentPragmas == R.CommentPragmas &&

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,6 @@ static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
128128
return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
129129
}
130130

131-
// Returns \c true if \c Current starts a new parameter.
132-
static bool startsNextParameter(const FormatToken &Current,
133-
const FormatStyle &Style) {
134-
const FormatToken &Previous = *Current.Previous;
135-
if (Current.is(TT_CtorInitializerComma) &&
136-
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
137-
return true;
138-
}
139-
if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
140-
return true;
141-
return Previous.is(tok::comma) && !Current.isTrailingComment() &&
142-
((Previous.isNot(TT_CtorInitializerComma) ||
143-
Style.BreakConstructorInitializers !=
144-
FormatStyle::BCIS_BeforeComma) &&
145-
(Previous.isNot(TT_InheritanceComma) ||
146-
Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma));
147-
}
148-
149131
static bool opensProtoMessageField(const FormatToken &LessTok,
150132
const FormatStyle &Style) {
151133
if (LessTok.isNot(tok::less))
@@ -411,7 +393,8 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
411393
// sets BreakBeforeParameter to avoid bin packing and this creates a
412394
// completely unnecessary line break after a template type that isn't
413395
// line-wrapped.
414-
(Previous.NestingLevel == 1 || Style.BinPackParameters)) ||
396+
(Previous.NestingLevel == 1 ||
397+
Style.BreakParameters == FormatStyle::BRPS_Never)) ||
415398
(Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
416399
Previous.isNot(tok::question)) ||
417400
(!Style.BreakBeforeTernaryOperators &&
@@ -1914,15 +1897,16 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
19141897
Current.MatchingParen->getPreviousNonComment() &&
19151898
Current.MatchingParen->getPreviousNonComment()->is(tok::comma);
19161899

1917-
// If ObjCBinPackProtocolList is unspecified, fall back to BinPackParameters
1900+
// If ObjCBinPackProtocolList is unspecified, fall back to BreakParameters
19181901
// for backwards compatibility.
19191902
bool ObjCBinPackProtocolList =
19201903
(Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto &&
1921-
Style.BinPackParameters) ||
1904+
Style.BreakParameters == FormatStyle::BRPS_Never) ||
19221905
Style.ObjCBinPackProtocolList == FormatStyle::BPS_Always;
19231906

19241907
bool BinPackDeclaration =
1925-
(State.Line->Type != LT_ObjCDecl && Style.BinPackParameters) ||
1908+
(State.Line->Type != LT_ObjCDecl &&
1909+
Style.BreakParameters == FormatStyle::BRPS_Never) ||
19261910
(State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList);
19271911

19281912
bool GenericSelection =

clang/lib/Format/Format.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,14 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
134134
}
135135
};
136136

137+
template <> struct ScalarEnumerationTraits<FormatStyle::BreakParametersStyle> {
138+
static void enumeration(IO &IO, FormatStyle::BreakParametersStyle &Value) {
139+
IO.enumCase(Value, "OnePerLine", FormatStyle::BRPS_OnePerLine);
140+
IO.enumCase(Value, "Never", FormatStyle::BRPS_Never);
141+
IO.enumCase(Value, "Always", FormatStyle::BRPS_Always);
142+
}
143+
};
144+
137145
template <> struct ScalarEnumerationTraits<FormatStyle::BinPackStyle> {
138146
static void enumeration(IO &IO, FormatStyle::BinPackStyle &Value) {
139147
IO.enumCase(Value, "Auto", FormatStyle::BPS_Auto);
@@ -852,6 +860,15 @@ template <> struct MappingTraits<FormatStyle> {
852860
bool OnCurrentLine = IsGoogleOrChromium;
853861
bool OnNextLine = true;
854862

863+
// For backward compatibility:
864+
// The default value of BinPackParameters was true unless BasedOnStyle was
865+
// Mozilla or Chromium. If BinPackParameters is true then the equilvalent
866+
// value for BreakParameters is BRPS_Never and BRPS_OnePerLine otherwise.
867+
const bool IsChromiumOrMozilla =
868+
BasedOnStyle.equals_insensitive("chromium") ||
869+
BasedOnStyle.equals_insensitive("mozilla");
870+
bool BinPackParameters = !IsChromiumOrMozilla;
871+
855872
bool BreakBeforeInheritanceComma = false;
856873
bool BreakConstructorInitializersBeforeComma = false;
857874

@@ -870,6 +887,7 @@ template <> struct MappingTraits<FormatStyle> {
870887
IO.mapOptional("AlwaysBreakAfterReturnType", Style.BreakAfterReturnType);
871888
IO.mapOptional("AlwaysBreakTemplateDeclarations",
872889
Style.BreakTemplateDeclarations);
890+
IO.mapOptional("BinPackParameters", BinPackParameters);
873891
IO.mapOptional("BreakBeforeInheritanceComma",
874892
BreakBeforeInheritanceComma);
875893
IO.mapOptional("BreakConstructorInitializersBeforeComma",
@@ -947,7 +965,6 @@ template <> struct MappingTraits<FormatStyle> {
947965
Style.AlwaysBreakBeforeMultilineStrings);
948966
IO.mapOptional("AttributeMacros", Style.AttributeMacros);
949967
IO.mapOptional("BinPackArguments", Style.BinPackArguments);
950-
IO.mapOptional("BinPackParameters", Style.BinPackParameters);
951968
IO.mapOptional("BitFieldColonSpacing", Style.BitFieldColonSpacing);
952969
IO.mapOptional("BracedInitializerIndentWidth",
953970
Style.BracedInitializerIndentWidth);
@@ -973,6 +990,7 @@ template <> struct MappingTraits<FormatStyle> {
973990
IO.mapOptional("BreakFunctionDefinitionParameters",
974991
Style.BreakFunctionDefinitionParameters);
975992
IO.mapOptional("BreakInheritanceList", Style.BreakInheritanceList);
993+
IO.mapOptional("BreakParameters", Style.BreakParameters);
976994
IO.mapOptional("BreakStringLiterals", Style.BreakStringLiterals);
977995
IO.mapOptional("BreakTemplateDeclarations",
978996
Style.BreakTemplateDeclarations);
@@ -1159,6 +1177,18 @@ template <> struct MappingTraits<FormatStyle> {
11591177
Style.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
11601178
}
11611179

1180+
// If BinPackParameters was specified but BreakParameters was not,
1181+
// initialize the latter from the former for backwards compatibility.
1182+
if (IsChromiumOrMozilla) {
1183+
if (BinPackParameters &&
1184+
(Style.BreakParameters == FormatStyle::BRPS_OnePerLine)) {
1185+
Style.BreakParameters = FormatStyle::BRPS_Never;
1186+
}
1187+
} else if (!BinPackParameters &&
1188+
(Style.BreakParameters == FormatStyle::BRPS_Never)) {
1189+
Style.BreakParameters = FormatStyle::BRPS_OnePerLine;
1190+
}
1191+
11621192
if (!IsGoogleOrChromium) {
11631193
if (Style.PackConstructorInitializers == FormatStyle::PCIS_BinPack &&
11641194
OnCurrentLine) {
@@ -1449,7 +1479,6 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14491479
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
14501480
LLVMStyle.AttributeMacros.push_back("__capability");
14511481
LLVMStyle.BinPackArguments = true;
1452-
LLVMStyle.BinPackParameters = true;
14531482
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
14541483
LLVMStyle.BracedInitializerIndentWidth = std::nullopt;
14551484
LLVMStyle.BraceWrapping = {/*AfterCaseLabel=*/false,
@@ -1483,6 +1512,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14831512
LLVMStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeColon;
14841513
LLVMStyle.BreakFunctionDefinitionParameters = false;
14851514
LLVMStyle.BreakInheritanceList = FormatStyle::BILS_BeforeColon;
1515+
LLVMStyle.BreakParameters = FormatStyle::BRPS_Never;
14861516
LLVMStyle.BreakStringLiterals = true;
14871517
LLVMStyle.BreakTemplateDeclarations = FormatStyle::BTDS_MultiLine;
14881518
LLVMStyle.ColumnLimit = 80;
@@ -1823,7 +1853,7 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
18231853
ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18241854
ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
18251855
ChromiumStyle.AllowShortLoopsOnASingleLine = false;
1826-
ChromiumStyle.BinPackParameters = false;
1856+
ChromiumStyle.BreakParameters = FormatStyle::BRPS_OnePerLine;
18271857
ChromiumStyle.DerivePointerAlignment = false;
18281858
if (Language == FormatStyle::LK_ObjC)
18291859
ChromiumStyle.ColumnLimit = 80;
@@ -1838,11 +1868,11 @@ FormatStyle getMozillaStyle() {
18381868
MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
18391869
FormatStyle::DRTBS_TopLevel;
18401870
MozillaStyle.BinPackArguments = false;
1841-
MozillaStyle.BinPackParameters = false;
18421871
MozillaStyle.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
18431872
MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
18441873
MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
18451874
MozillaStyle.BreakInheritanceList = FormatStyle::BILS_BeforeComma;
1875+
MozillaStyle.BreakParameters = FormatStyle::BRPS_OnePerLine;
18461876
MozillaStyle.BreakTemplateDeclarations = FormatStyle::BTDS_Yes;
18471877
MozillaStyle.ConstructorInitializerIndentWidth = 2;
18481878
MozillaStyle.ContinuationIndentWidth = 2;

0 commit comments

Comments
 (0)