Skip to content

Commit 923a4ef

Browse files
committed
[clang-format] Add PackParameters option to replace BinPackParameters.
The PackParameters option now provides the new BreakAlways setting
1 parent 4377656 commit 923a4ef

File tree

10 files changed

+289
-76
lines changed

10 files changed

+289
-76
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ``PackParameters``.
20842072

20852073
.. _BitFieldColonSpacing:
20862074

@@ -4984,6 +4972,44 @@ the configuration (without a prefix: ``Auto``).
49844972

49854973

49864974

4975+
.. _PackParameters:
4976+
4977+
**PackParameters** (``PackParametersStyle``) :versionbadge:`clang-format 20` :ref:`<PackParameters>`
4978+
The pack parameters style to use.
4979+
4980+
Possible values:
4981+
4982+
* ``PPS_CurrentLine`` (in configuration: ``CurrentLine``)
4983+
Put all parameters on the current line if they fit.
4984+
Otherwise, put each one on its own line.
4985+
4986+
.. code-block:: c++
4987+
4988+
void f(int a, int b, int c);
4989+
4990+
void f(int a,
4991+
int b,
4992+
int ccccccccccccccccccccccccccccccccccccc);
4993+
4994+
* ``PPS_BinPack`` (in configuration: ``BinPack``)
4995+
Bin-pack parameters.
4996+
4997+
.. code-block:: c++
4998+
4999+
void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
5000+
int ccccccccccccccccccccccccccccccccccccccccccc);
5001+
5002+
* ``PPS_BreakAlways`` (in configuration: ``BreakAlways``)
5003+
Always put each parameter on its own line.
5004+
5005+
.. code-block:: c++
5006+
5007+
void f(int a,
5008+
int b,
5009+
int c);
5010+
5011+
5012+
49875013
.. _PenaltyBreakAssignment:
49885014

49895015
**PenaltyBreakAssignment** (``Unsigned``) :versionbadge:`clang-format 5` :ref:`<PenaltyBreakAssignment>`

clang/include/clang/Format/Format.h

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ``PackParameters``.
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 {
@@ -3537,6 +3526,37 @@ struct FormatStyle {
35373526
/// \version 14
35383527
PackConstructorInitializersStyle PackConstructorInitializers;
35393528

3529+
/// Different way to try to fit all parameters on a line.
3530+
enum PackParametersStyle : int8_t {
3531+
/// Put all parameters on the current line if they fit.
3532+
/// Otherwise, put each one on its own line.
3533+
/// \code
3534+
/// void f(int a, int b, int c);
3535+
///
3536+
/// void f(int a,
3537+
/// int b,
3538+
/// int ccccccccccccccccccccccccccccccccccccc);
3539+
/// \endcode
3540+
PPS_CurrentLine,
3541+
/// Bin-pack parameters.
3542+
/// \code
3543+
/// void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
3544+
/// int ccccccccccccccccccccccccccccccccccccccccccc);
3545+
/// \endcode
3546+
PPS_BinPack,
3547+
/// Always put each parameter on its own line.
3548+
/// \code
3549+
/// void f(int a,
3550+
/// int b,
3551+
/// int c);
3552+
/// \endcode
3553+
PPS_BreakAlways,
3554+
};
3555+
3556+
/// The pack parameters style to use.
3557+
/// \version 20
3558+
PackParametersStyle PackParameters;
3559+
35403560
/// The penalty for breaking around an assignment operator.
35413561
/// \version 5
35423562
unsigned PenaltyBreakAssignment;
@@ -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 &&
@@ -5094,6 +5113,7 @@ struct FormatStyle {
50945113
ObjCSpaceAfterProperty == R.ObjCSpaceAfterProperty &&
50955114
ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList &&
50965115
PackConstructorInitializers == R.PackConstructorInitializers &&
5116+
PackParameters == R.PackParameters &&
50975117
PenaltyBreakAssignment == R.PenaltyBreakAssignment &&
50985118
PenaltyBreakBeforeFirstCallParameter ==
50995119
R.PenaltyBreakBeforeFirstCallParameter &&

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.PackParameters == FormatStyle::PPS_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.PackParameters == FormatStyle::PPS_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.PackParameters == FormatStyle::PPS_BinPack) ||
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
@@ -464,6 +464,14 @@ struct ScalarEnumerationTraits<FormatStyle::PackConstructorInitializersStyle> {
464464
}
465465
};
466466

467+
template <> struct ScalarEnumerationTraits<FormatStyle::PackParametersStyle> {
468+
static void enumeration(IO &IO, FormatStyle::PackParametersStyle &Value) {
469+
IO.enumCase(Value, "CurrentLine", FormatStyle::PPS_CurrentLine);
470+
IO.enumCase(Value, "BinPack", FormatStyle::PPS_BinPack);
471+
IO.enumCase(Value, "BreakAlways", FormatStyle::PPS_BreakAlways);
472+
}
473+
};
474+
467475
template <> struct ScalarEnumerationTraits<FormatStyle::PointerAlignmentStyle> {
468476
static void enumeration(IO &IO, FormatStyle::PointerAlignmentStyle &Value) {
469477
IO.enumCase(Value, "Middle", FormatStyle::PAS_Middle);
@@ -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 was true then the equilvalent
866+
// value for PackParameters is PPS_BinPack and PPS_CurrentLine 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);
@@ -1037,6 +1054,7 @@ template <> struct MappingTraits<FormatStyle> {
10371054
Style.ObjCSpaceBeforeProtocolList);
10381055
IO.mapOptional("PackConstructorInitializers",
10391056
Style.PackConstructorInitializers);
1057+
IO.mapOptional("PackParameters", Style.PackParameters);
10401058
IO.mapOptional("PenaltyBreakAssignment", Style.PenaltyBreakAssignment);
10411059
IO.mapOptional("PenaltyBreakBeforeFirstCallParameter",
10421060
Style.PenaltyBreakBeforeFirstCallParameter);
@@ -1174,6 +1192,18 @@ template <> struct MappingTraits<FormatStyle> {
11741192
Style.PackConstructorInitializers = FormatStyle::PCIS_CurrentLine;
11751193
}
11761194

1195+
// If BinPackParameters was specified but PackParameters was not, initialize
1196+
// the latter from the former for backwards compatibility.
1197+
if (IsChromiumOrMozilla) {
1198+
if (BinPackParameters &&
1199+
Style.PackParameters == FormatStyle::PPS_CurrentLine) {
1200+
Style.PackParameters = FormatStyle::PPS_BinPack;
1201+
}
1202+
} else if (!BinPackParameters &&
1203+
Style.PackParameters == FormatStyle::PPS_BinPack) {
1204+
Style.PackParameters = FormatStyle::PPS_CurrentLine;
1205+
}
1206+
11771207
if (Style.LineEnding == FormatStyle::LE_DeriveLF) {
11781208
if (!DeriveLineEnding)
11791209
Style.LineEnding = UseCRLF ? FormatStyle::LE_CRLF : FormatStyle::LE_LF;
@@ -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,
@@ -1543,6 +1572,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
15431572
LLVMStyle.ObjCSpaceAfterProperty = false;
15441573
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
15451574
LLVMStyle.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
1575+
LLVMStyle.PackParameters = FormatStyle::PPS_BinPack;
15461576
LLVMStyle.PointerAlignment = FormatStyle::PAS_Right;
15471577
LLVMStyle.PPIndentWidth = -1;
15481578
LLVMStyle.QualifierAlignment = FormatStyle::QAS_Leave;
@@ -1823,8 +1853,8 @@ 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;
18271856
ChromiumStyle.DerivePointerAlignment = false;
1857+
ChromiumStyle.PackParameters = FormatStyle::PPS_CurrentLine;
18281858
if (Language == FormatStyle::LK_ObjC)
18291859
ChromiumStyle.ColumnLimit = 80;
18301860
}
@@ -1838,7 +1868,6 @@ 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;
@@ -1851,6 +1880,7 @@ FormatStyle getMozillaStyle() {
18511880
MozillaStyle.IndentCaseLabels = true;
18521881
MozillaStyle.ObjCSpaceAfterProperty = true;
18531882
MozillaStyle.ObjCSpaceBeforeProtocolList = false;
1883+
MozillaStyle.PackParameters = FormatStyle::PPS_CurrentLine;
18541884
MozillaStyle.PenaltyReturnTypeOnItsOwnLine = 200;
18551885
MozillaStyle.PointerAlignment = FormatStyle::PAS_Left;
18561886
MozillaStyle.SpaceAfterTemplateKeyword = false;

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::PPS_BreakAlways == Style.PackParameters &&
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: 15 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);
@@ -456,6 +455,21 @@ TEST(ConfigParseTest, ParsesConfiguration) {
456455
"AllowAllConstructorInitializersOnNextLine: false",
457456
PackConstructorInitializers, FormatStyle::PCIS_CurrentLine);
458457

458+
Style.PackParameters = FormatStyle::PPS_BinPack;
459+
CHECK_PARSE("PackParameters: CurrentLine", PackParameters,
460+
FormatStyle::PPS_CurrentLine);
461+
CHECK_PARSE("PackParameters: BinPack", PackParameters,
462+
FormatStyle::PPS_BinPack);
463+
CHECK_PARSE("PackParameters: BreakAlways", PackParameters,
464+
FormatStyle::PPS_BreakAlways);
465+
// For backward compatibility.
466+
CHECK_PARSE("BasedOnStyle: Mozilla\n"
467+
"BinPackParameters: true",
468+
PackParameters, FormatStyle::PPS_BinPack);
469+
CHECK_PARSE("BasedOnStyle: Llvm\n"
470+
"BinPackParameters: false",
471+
PackParameters, FormatStyle::PPS_CurrentLine);
472+
459473
Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_LogicalBlock;
460474
CHECK_PARSE("EmptyLineBeforeAccessModifier: Never",
461475
EmptyLineBeforeAccessModifier, FormatStyle::ELBAMS_Never);

0 commit comments

Comments
 (0)