Skip to content

Commit 1dda629

Browse files
committed
[clang-format] Change BinPackParameters to an enum to add a BreakAlways
option
1 parent 4377656 commit 1dda629

File tree

10 files changed

+270
-81
lines changed

10 files changed

+270
-81
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 ``CurrentLine``.
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_CurrentLine`` (in configuration: ``CurrentLine``)
2076+
Put all parameters on the current line if they fit.
2077+
Otherwise, put each one on its own line.
2078+
2079+
.. code-block:: c++
2080+
2081+
void f(int a, int b, int c);
2082+
2083+
void f(int a,
2084+
int b,
2085+
int ccccccccccccccccccccccccccccccccccccc);
2086+
2087+
* ``BPPS_BinPack`` (in configuration: ``BinPack``)
2088+
Bin-pack parameters.
2089+
2090+
.. code-block:: c++
2091+
2092+
void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
2093+
int ccccccccccccccccccccccccccccccccccccccccccc);
2094+
2095+
* ``BPPS_BreakAlways`` (in configuration: ``BreakAlways``)
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

@@ -4776,7 +4797,7 @@ the configuration (without a prefix: ``Auto``).
47764797
items into as few lines as possible when they go over ``ColumnLimit``.
47774798

47784799
If ``Auto`` (the default), delegates to the value in
4779-
``BinPackParameters``. If that is ``true``, bin-packs Objective-C
4800+
``BinPackParameters``. If that is ``BinPack``, bin-packs Objective-C
47804801
protocol conformance list items into as few lines as possible
47814802
whenever they go over ``ColumnLimit``.
47824803

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

47914812
.. code-block:: objc
47924813
4793-
Always (or Auto, if BinPackParameters=true):
4814+
Always (or Auto, if BinPackParameters==BinPack):
47944815
@interface ccccccccccccc () <
47954816
ccccccccccccc, ccccccccccccc,
47964817
ccccccccccccc, ccccccccccccc> {
47974818
}
47984819
4799-
Never (or Auto, if BinPackParameters=false):
4820+
Never (or Auto, if BinPackParameters!=BinPack):
48004821
@interface ddddddddddddd () <
48014822
ddddddddddddd,
48024823
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 ``CurrentLine``.
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+
/// Put all parameters on the current line if they fit.
1198+
/// Otherwise, put each one on its own line.
1199+
/// \code
1200+
/// void f(int a, int b, int c);
1201+
///
1202+
/// void f(int a,
1203+
/// int b,
1204+
/// int ccccccccccccccccccccccccccccccccccccc);
1205+
/// \endcode
1206+
BPPS_CurrentLine,
1207+
/// Bin-pack parameters.
1208+
/// \code
1209+
/// void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
1210+
/// int ccccccccccccccccccccccccccccccccccccccccccc);
1211+
/// \endcode
1212+
BPPS_BinPack,
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_BreakAlways,
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 {
@@ -3378,7 +3394,7 @@ struct FormatStyle {
33783394
/// items into as few lines as possible when they go over ``ColumnLimit``.
33793395
///
33803396
/// If ``Auto`` (the default), delegates to the value in
3381-
/// ``BinPackParameters``. If that is ``true``, bin-packs Objective-C
3397+
/// ``BinPackParameters``. If that is ``BinPack``, bin-packs Objective-C
33823398
/// protocol conformance list items into as few lines as possible
33833399
/// whenever they go over ``ColumnLimit``.
33843400
///
@@ -3390,13 +3406,13 @@ struct FormatStyle {
33903406
/// onto individual lines whenever they go over ``ColumnLimit``.
33913407
///
33923408
/// \code{.objc}
3393-
/// Always (or Auto, if BinPackParameters=true):
3409+
/// Always (or Auto, if BinPackParameters==BinPack):
33943410
/// @interface ccccccccccccc () <
33953411
/// ccccccccccccc, ccccccccccccc,
33963412
/// ccccccccccccc, ccccccccccccc> {
33973413
/// }
33983414
///
3399-
/// Never (or Auto, if BinPackParameters=false):
3415+
/// Never (or Auto, if BinPackParameters!=BinPack):
34003416
/// @interface ddddddddddddd () <
34013417
/// ddddddddddddd,
34023418
/// ddddddddddddd,

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 5 additions & 21 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.BinPackParameters == FormatStyle::BPPS_BinPack)) ||
415398
(Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
416399
Previous.isNot(tok::question)) ||
417400
(!Style.BreakBeforeTernaryOperators &&
@@ -1918,11 +1901,12 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
19181901
// for backwards compatibility.
19191902
bool ObjCBinPackProtocolList =
19201903
(Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto &&
1921-
Style.BinPackParameters) ||
1904+
Style.BinPackParameters == FormatStyle::BPPS_BinPack) ||
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.BinPackParameters == FormatStyle::BPPS_BinPack) ||
19261910
(State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList);
19271911

19281912
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, "CurrentLine", FormatStyle::BPPS_CurrentLine);
141+
IO.enumCase(Value, "BinPack", FormatStyle::BPPS_BinPack);
142+
IO.enumCase(Value, "BreakAlways", FormatStyle::BPPS_BreakAlways);
143+
144+
// For backward compatibility.
145+
IO.enumCase(Value, "true", FormatStyle::BPPS_BinPack);
146+
IO.enumCase(Value, "false", FormatStyle::BPPS_CurrentLine);
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);
@@ -1449,7 +1462,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14491462
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
14501463
LLVMStyle.AttributeMacros.push_back("__capability");
14511464
LLVMStyle.BinPackArguments = true;
1452-
LLVMStyle.BinPackParameters = true;
1465+
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
14531466
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
14541467
LLVMStyle.BracedInitializerIndentWidth = std::nullopt;
14551468
LLVMStyle.BraceWrapping = {/*AfterCaseLabel=*/false,
@@ -1823,7 +1836,7 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
18231836
ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
18241837
ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
18251838
ChromiumStyle.AllowShortLoopsOnASingleLine = false;
1826-
ChromiumStyle.BinPackParameters = false;
1839+
ChromiumStyle.BinPackParameters = FormatStyle::BPPS_CurrentLine;
18271840
ChromiumStyle.DerivePointerAlignment = false;
18281841
if (Language == FormatStyle::LK_ObjC)
18291842
ChromiumStyle.ColumnLimit = 80;
@@ -1838,7 +1851,7 @@ FormatStyle getMozillaStyle() {
18381851
MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
18391852
FormatStyle::DRTBS_TopLevel;
18401853
MozillaStyle.BinPackArguments = false;
1841-
MozillaStyle.BinPackParameters = false;
1854+
MozillaStyle.BinPackParameters = FormatStyle::BPPS_CurrentLine;
18421855
MozillaStyle.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
18431856
MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
18441857
MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;

clang/lib/Format/FormatToken.h

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

1981+
// Returns \c true if \c Current starts a new parameter.
1982+
inline bool startsNextParameter(const FormatToken &Current,
1983+
const FormatStyle &Style) {
1984+
const FormatToken &Previous = *Current.Previous;
1985+
if (Current.is(TT_CtorInitializerComma) &&
1986+
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
1987+
return true;
1988+
}
1989+
if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
1990+
return true;
1991+
return Previous.is(tok::comma) && !Current.isTrailingComment() &&
1992+
((Previous.isNot(TT_CtorInitializerComma) ||
1993+
Style.BreakConstructorInitializers !=
1994+
FormatStyle::BCIS_BeforeComma) &&
1995+
(Previous.isNot(TT_InheritanceComma) ||
1996+
Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma));
1997+
}
1998+
19811999
} // namespace format
19822000
} // namespace clang
19832001

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5458,6 +5458,14 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
54585458
return true;
54595459
}
54605460

5461+
// Ignores the first parameter as this will be handled separately by
5462+
// BreakFunctionDefinitionParameters or AlignAfterOpenBracket.
5463+
if (FormatStyle::BPPS_BreakAlways == Style.BinPackParameters &&
5464+
Line.MightBeFunctionDecl && !Left.opensScope() &&
5465+
startsNextParameter(Right, Style)) {
5466+
return true;
5467+
}
5468+
54615469
const auto *BeforeLeft = Left.Previous;
54625470
const auto *AfterRight = Right.Next;
54635471

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);
@@ -428,6 +427,19 @@ TEST(ConfigParseTest, ParsesConfiguration) {
428427
CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
429428
FormatStyle::BILS_BeforeComma);
430429

430+
Style.BinPackParameters = FormatStyle::BPPS_BinPack;
431+
CHECK_PARSE("BinPackParameters: CurrentLine", BinPackParameters,
432+
FormatStyle::BPPS_CurrentLine);
433+
CHECK_PARSE("BinPackParameters: BinPack", BinPackParameters,
434+
FormatStyle::BPPS_BinPack);
435+
CHECK_PARSE("BinPackParameters: BreakAlways", BinPackParameters,
436+
FormatStyle::BPPS_BreakAlways);
437+
// For backward compatibility.
438+
CHECK_PARSE("BinPackParameters: true", BinPackParameters,
439+
FormatStyle::BPPS_BinPack);
440+
CHECK_PARSE("BinPackParameters: false", BinPackParameters,
441+
FormatStyle::BPPS_CurrentLine);
442+
431443
Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
432444
CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
433445
FormatStyle::PCIS_Never);

0 commit comments

Comments
 (0)