Skip to content

Commit 7c3237d

Browse files
[clang-format] Change BinPackParameters to enum and add AlwaysOnePerLine (#101882)
Related issues that have requested this feature: #51833 #23796 #53190 Partially solves - this issue requests is for both arguments and parameters
1 parent fdaaa87 commit 7c3237d

File tree

11 files changed

+275
-84
lines changed

11 files changed

+275
-84
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 36 additions & 15 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 ``BinPackParameters`` is ``OnePerLine``.
16211621

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

@@ -2067,20 +2067,41 @@ the configuration (without a prefix: ``Auto``).
20672067

20682068
.. _BinPackParameters:
20692069

2070-
**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.
2070+
**BinPackParameters** (``BinPackParametersStyle``) :versionbadge:`clang-format 3.7` :ref:`<BinPackParameters>`
2071+
The bin pack parameters style to use.
20732072

2074-
.. code-block:: c++
2073+
Possible values:
2074+
2075+
* ``BPPS_BinPack`` (in configuration: ``BinPack``)
2076+
Bin-pack parameters.
2077+
2078+
.. code-block:: c++
2079+
2080+
void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
2081+
int ccccccccccccccccccccccccccccccccccccccccccc);
2082+
2083+
* ``BPPS_OnePerLine`` (in configuration: ``OnePerLine``)
2084+
Put all parameters on the current line if they fit.
2085+
Otherwise, put each one on its own line.
2086+
2087+
.. code-block:: c++
2088+
2089+
void f(int a, int b, int c);
2090+
2091+
void f(int a,
2092+
int b,
2093+
int ccccccccccccccccccccccccccccccccccccc);
2094+
2095+
* ``BPPS_AlwaysOnePerLine`` (in configuration: ``AlwaysOnePerLine``)
2096+
Always put each parameter on its own line.
2097+
2098+
.. code-block:: c++
2099+
2100+
void f(int a,
2101+
int b,
2102+
int c);
20752103

2076-
true:
2077-
void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa,
2078-
int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
20792104

2080-
false:
2081-
void f(int aaaaaaaaaaaaaaaaaaaa,
2082-
int aaaaaaaaaaaaaaaaaaaa,
2083-
int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
20842105

20852106
.. _BitFieldColonSpacing:
20862107

@@ -4817,7 +4838,7 @@ the configuration (without a prefix: ``Auto``).
48174838
items into as few lines as possible when they go over ``ColumnLimit``.
48184839

48194840
If ``Auto`` (the default), delegates to the value in
4820-
``BinPackParameters``. If that is ``true``, bin-packs Objective-C
4841+
``BinPackParameters``. If that is ``BinPack``, bin-packs Objective-C
48214842
protocol conformance list items into as few lines as possible
48224843
whenever they go over ``ColumnLimit``.
48234844

@@ -4831,13 +4852,13 @@ the configuration (without a prefix: ``Auto``).
48314852

48324853
.. code-block:: objc
48334854
4834-
Always (or Auto, if BinPackParameters=true):
4855+
Always (or Auto, if BinPackParameters==BinPack):
48354856
@interface ccccccccccccc () <
48364857
ccccccccccccc, ccccccccccccc,
48374858
ccccccccccccc, ccccccccccccc> {
48384859
}
48394860
4840-
Never (or Auto, if BinPackParameters=false):
4861+
Never (or Auto, if BinPackParameters!=BinPack):
48414862
@interface ddddddddddddd () <
48424863
ddddddddddddd,
48434864
ddddddddddddd,

clang/include/clang/Format/Format.h

Lines changed: 33 additions & 17 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 ``BinPackParameters`` is ``OnePerLine``.
663663
/// \code
664664
/// true:
665665
/// void myFunction(
@@ -1192,20 +1192,36 @@ 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+
/// Different way to try to fit all parameters on a line.
1196+
enum BinPackParametersStyle : int8_t {
1197+
/// Bin-pack parameters.
1198+
/// \code
1199+
/// void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
1200+
/// int ccccccccccccccccccccccccccccccccccccccccccc);
1201+
/// \endcode
1202+
BPPS_BinPack,
1203+
/// Put all parameters on the current line if they fit.
1204+
/// Otherwise, put each one on its own line.
1205+
/// \code
1206+
/// void f(int a, int b, int c);
1207+
///
1208+
/// void f(int a,
1209+
/// int b,
1210+
/// int ccccccccccccccccccccccccccccccccccccc);
1211+
/// \endcode
1212+
BPPS_OnePerLine,
1213+
/// Always put each parameter on its own line.
1214+
/// \code
1215+
/// void f(int a,
1216+
/// int b,
1217+
/// int c);
1218+
/// \endcode
1219+
BPPS_AlwaysOnePerLine,
1220+
};
1221+
1222+
/// The bin pack parameters style to use.
12071223
/// \version 3.7
1208-
bool BinPackParameters;
1224+
BinPackParametersStyle BinPackParameters;
12091225

12101226
/// Styles for adding spacing around ``:`` in bitfield definitions.
12111227
enum BitFieldColonSpacingStyle : int8_t {
@@ -3414,7 +3430,7 @@ struct FormatStyle {
34143430
/// items into as few lines as possible when they go over ``ColumnLimit``.
34153431
///
34163432
/// If ``Auto`` (the default), delegates to the value in
3417-
/// ``BinPackParameters``. If that is ``true``, bin-packs Objective-C
3433+
/// ``BinPackParameters``. If that is ``BinPack``, bin-packs Objective-C
34183434
/// protocol conformance list items into as few lines as possible
34193435
/// whenever they go over ``ColumnLimit``.
34203436
///
@@ -3426,13 +3442,13 @@ struct FormatStyle {
34263442
/// onto individual lines whenever they go over ``ColumnLimit``.
34273443
///
34283444
/// \code{.objc}
3429-
/// Always (or Auto, if BinPackParameters=true):
3445+
/// Always (or Auto, if BinPackParameters==BinPack):
34303446
/// @interface ccccccccccccc () <
34313447
/// ccccccccccccc, ccccccccccccc,
34323448
/// ccccccccccccc, ccccccccccccc> {
34333449
/// }
34343450
///
3435-
/// Never (or Auto, if BinPackParameters=false):
3451+
/// Never (or Auto, if BinPackParameters!=BinPack):
34363452
/// @interface ddddddddddddd () <
34373453
/// ddddddddddddd,
34383454
/// ddddddddddddd,

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -128,25 +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-
assert(Current.Previous);
135-
const auto &Previous = *Current.Previous;
136-
if (Current.is(TT_CtorInitializerComma) &&
137-
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
138-
return true;
139-
}
140-
if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
141-
return true;
142-
return Previous.is(tok::comma) && !Current.isTrailingComment() &&
143-
((Previous.isNot(TT_CtorInitializerComma) ||
144-
Style.BreakConstructorInitializers !=
145-
FormatStyle::BCIS_BeforeComma) &&
146-
(Previous.isNot(TT_InheritanceComma) ||
147-
Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma));
148-
}
149-
150131
// Returns \c true if \c Token in an alignable binary operator
151132
static bool isAlignableBinaryOperator(const FormatToken &Token) {
152133
// No need to align binary operators that only have two operands.
@@ -437,7 +418,8 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
437418
// sets BreakBeforeParameter to avoid bin packing and this creates a
438419
// completely unnecessary line break after a template type that isn't
439420
// line-wrapped.
440-
(Previous.NestingLevel == 1 || Style.BinPackParameters)) ||
421+
(Previous.NestingLevel == 1 ||
422+
Style.BinPackParameters == FormatStyle::BPPS_BinPack)) ||
441423
(Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
442424
Previous.isNot(tok::question)) ||
443425
(!Style.BreakBeforeTernaryOperators &&
@@ -1951,11 +1933,12 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
19511933
// for backwards compatibility.
19521934
bool ObjCBinPackProtocolList =
19531935
(Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto &&
1954-
Style.BinPackParameters) ||
1936+
Style.BinPackParameters == FormatStyle::BPPS_BinPack) ||
19551937
Style.ObjCBinPackProtocolList == FormatStyle::BPS_Always;
19561938

19571939
bool BinPackDeclaration =
1958-
(State.Line->Type != LT_ObjCDecl && Style.BinPackParameters) ||
1940+
(State.Line->Type != LT_ObjCDecl &&
1941+
Style.BinPackParameters == FormatStyle::BPPS_BinPack) ||
19591942
(State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList);
19601943

19611944
bool GenericSelection =

clang/lib/Format/Format.cpp

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

137+
template <>
138+
struct ScalarEnumerationTraits<FormatStyle::BinPackParametersStyle> {
139+
static void enumeration(IO &IO, FormatStyle::BinPackParametersStyle &Value) {
140+
IO.enumCase(Value, "BinPack", FormatStyle::BPPS_BinPack);
141+
IO.enumCase(Value, "OnePerLine", FormatStyle::BPPS_OnePerLine);
142+
IO.enumCase(Value, "AlwaysOnePerLine", FormatStyle::BPPS_AlwaysOnePerLine);
143+
144+
// For backward compatibility.
145+
IO.enumCase(Value, "true", FormatStyle::BPPS_BinPack);
146+
IO.enumCase(Value, "false", FormatStyle::BPPS_OnePerLine);
147+
}
148+
};
149+
137150
template <> struct ScalarEnumerationTraits<FormatStyle::BinPackStyle> {
138151
static void enumeration(IO &IO, FormatStyle::BinPackStyle &Value) {
139152
IO.enumCase(Value, "Auto", FormatStyle::BPS_Auto);
@@ -1461,7 +1474,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14611474
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
14621475
LLVMStyle.AttributeMacros.push_back("__capability");
14631476
LLVMStyle.BinPackArguments = true;
1464-
LLVMStyle.BinPackParameters = true;
1477+
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
14651478
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
14661479
LLVMStyle.BracedInitializerIndentWidth = std::nullopt;
14671480
LLVMStyle.BraceWrapping = {/*AfterCaseLabel=*/false,
@@ -1836,7 +1849,7 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
18361849
ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18371850
ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
18381851
ChromiumStyle.AllowShortLoopsOnASingleLine = false;
1839-
ChromiumStyle.BinPackParameters = false;
1852+
ChromiumStyle.BinPackParameters = FormatStyle::BPPS_OnePerLine;
18401853
ChromiumStyle.DerivePointerAlignment = false;
18411854
if (Language == FormatStyle::LK_ObjC)
18421855
ChromiumStyle.ColumnLimit = 80;
@@ -1851,7 +1864,7 @@ FormatStyle getMozillaStyle() {
18511864
MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
18521865
FormatStyle::DRTBS_TopLevel;
18531866
MozillaStyle.BinPackArguments = false;
1854-
MozillaStyle.BinPackParameters = false;
1867+
MozillaStyle.BinPackParameters = FormatStyle::BPPS_OnePerLine;
18551868
MozillaStyle.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
18561869
MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
18571870
MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;

clang/lib/Format/FormatToken.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,5 +322,22 @@ CommaSeparatedList::getColumnFormat(unsigned RemainingCharacters) const {
322322
return BestFormat;
323323
}
324324

325+
bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style) {
326+
assert(Current.Previous);
327+
const auto &Previous = *Current.Previous;
328+
if (Current.is(TT_CtorInitializerComma) &&
329+
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
330+
return true;
331+
}
332+
if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
333+
return true;
334+
return Previous.is(tok::comma) && !Current.isTrailingComment() &&
335+
((Previous.isNot(TT_CtorInitializerComma) ||
336+
Style.BreakConstructorInitializers !=
337+
FormatStyle::BCIS_BeforeComma) &&
338+
(Previous.isNot(TT_InheritanceComma) ||
339+
Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma));
340+
}
341+
325342
} // namespace format
326343
} // namespace clang

clang/lib/Format/FormatToken.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,9 @@ inline bool continuesLineComment(const FormatToken &FormatTok,
19781978
FormatTok.OriginalColumn >= MinContinueColumn;
19791979
}
19801980

1981+
// Returns \c true if \c Current starts a new parameter.
1982+
bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style);
1983+
19811984
} // namespace format
19821985
} // namespace clang
19831986

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5479,6 +5479,14 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
54795479
return true;
54805480
}
54815481

5482+
// Ignores the first parameter as this will be handled separately by
5483+
// BreakFunctionDefinitionParameters or AlignAfterOpenBracket.
5484+
if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine &&
5485+
Line.MightBeFunctionDecl && !Left.opensScope() &&
5486+
startsNextParameter(Right, Style)) {
5487+
return true;
5488+
}
5489+
54825490
const auto *BeforeLeft = Left.Previous;
54835491
const auto *AfterRight = Right.Next;
54845492

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
160160
CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
161161
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
162162
CHECK_PARSE_BOOL(BinPackArguments);
163-
CHECK_PARSE_BOOL(BinPackParameters);
164163
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
165164
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
166165
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
@@ -436,6 +435,19 @@ TEST(ConfigParseTest, ParsesConfiguration) {
436435
CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
437436
FormatStyle::BILS_BeforeComma);
438437

438+
Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
439+
CHECK_PARSE("BinPackParameters: BinPack", BinPackParameters,
440+
FormatStyle::BPPS_BinPack);
441+
CHECK_PARSE("BinPackParameters: OnePerLine", BinPackParameters,
442+
FormatStyle::BPPS_OnePerLine);
443+
CHECK_PARSE("BinPackParameters: AlwaysOnePerLine", BinPackParameters,
444+
FormatStyle::BPPS_AlwaysOnePerLine);
445+
// For backward compatibility.
446+
CHECK_PARSE("BinPackParameters: true", BinPackParameters,
447+
FormatStyle::BPPS_BinPack);
448+
CHECK_PARSE("BinPackParameters: false", BinPackParameters,
449+
FormatStyle::BPPS_OnePerLine);
450+
439451
Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
440452
CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
441453
FormatStyle::PCIS_Never);

0 commit comments

Comments
 (0)