Skip to content

[clang-format] Change BinPackParameters to enum and add AlwaysOnePerLine #101882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ the configuration (without a prefix: ``Auto``).
**AllowAllParametersOfDeclarationOnNextLine** (``Boolean``) :versionbadge:`clang-format 3.3` :ref:`¶ <AllowAllParametersOfDeclarationOnNextLine>`
If the function declaration doesn't fit on a line,
allow putting all parameters of a function declaration onto
the next line even if ``BinPackParameters`` is ``false``.
the next line even if ``BinPackParameters`` is ``OnePerLine``.

.. code-block:: c++

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

.. _BinPackParameters:

**BinPackParameters** (``Boolean``) :versionbadge:`clang-format 3.7` :ref:`¶ <BinPackParameters>`
If ``false``, a function declaration's or function definition's
parameters will either all be on the same line or will have one line each.
**BinPackParameters** (``BinPackParametersStyle``) :versionbadge:`clang-format 3.7` :ref:`¶ <BinPackParameters>`
The bin pack parameters style to use.

.. code-block:: c++
Possible values:

* ``BPPS_BinPack`` (in configuration: ``BinPack``)
Bin-pack parameters.

.. code-block:: c++

void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
int ccccccccccccccccccccccccccccccccccccccccccc);

* ``BPPS_OnePerLine`` (in configuration: ``OnePerLine``)
Put all parameters on the current line if they fit.
Otherwise, put each one on its own line.

.. code-block:: c++

void f(int a, int b, int c);

void f(int a,
int b,
int ccccccccccccccccccccccccccccccccccccc);

* ``BPPS_AlwaysOnePerLine`` (in configuration: ``AlwaysOnePerLine``)
Always put each parameter on its own line.

.. code-block:: c++

void f(int a,
int b,
int c);

true:
void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa,
int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}

false:
void f(int aaaaaaaaaaaaaaaaaaaa,
int aaaaaaaaaaaaaaaaaaaa,
int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}

.. _BitFieldColonSpacing:

Expand Down Expand Up @@ -4816,7 +4837,7 @@ the configuration (without a prefix: ``Auto``).
items into as few lines as possible when they go over ``ColumnLimit``.

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

Expand All @@ -4830,13 +4851,13 @@ the configuration (without a prefix: ``Auto``).

.. code-block:: objc

Always (or Auto, if BinPackParameters=true):
Always (or Auto, if BinPackParameters==BinPack):
@interface ccccccccccccc () <
ccccccccccccc, ccccccccccccc,
ccccccccccccc, ccccccccccccc> {
}

Never (or Auto, if BinPackParameters=false):
Never (or Auto, if BinPackParameters!=BinPack):
@interface ddddddddddddd () <
ddddddddddddd,
ddddddddddddd,
Expand Down
50 changes: 33 additions & 17 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ struct FormatStyle {

/// If the function declaration doesn't fit on a line,
/// allow putting all parameters of a function declaration onto
/// the next line even if ``BinPackParameters`` is ``false``.
/// the next line even if ``BinPackParameters`` is ``OnePerLine``.
/// \code
/// true:
/// void myFunction(
Expand Down Expand Up @@ -1192,20 +1192,36 @@ struct FormatStyle {
/// \version 3.7
bool BinPackArguments;

/// If ``false``, a function declaration's or function definition's
/// parameters will either all be on the same line or will have one line each.
/// \code
/// true:
/// void f(int aaaaaaaaaaaaaaaaaaaa, int aaaaaaaaaaaaaaaaaaaa,
/// int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
///
/// false:
/// void f(int aaaaaaaaaaaaaaaaaaaa,
/// int aaaaaaaaaaaaaaaaaaaa,
/// int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {}
/// \endcode
/// Different way to try to fit all parameters on a line.
enum BinPackParametersStyle : int8_t {
/// Bin-pack parameters.
/// \code
/// void f(int a, int bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,
/// int ccccccccccccccccccccccccccccccccccccccccccc);
/// \endcode
BPPS_BinPack,
/// Put all parameters on the current line if they fit.
/// Otherwise, put each one on its own line.
/// \code
/// void f(int a, int b, int c);
///
/// void f(int a,
/// int b,
/// int ccccccccccccccccccccccccccccccccccccc);
/// \endcode
BPPS_OnePerLine,
/// Always put each parameter on its own line.
/// \code
/// void f(int a,
/// int b,
/// int c);
/// \endcode
BPPS_AlwaysOnePerLine,
};

/// The bin pack parameters style to use.
/// \version 3.7
bool BinPackParameters;
BinPackParametersStyle BinPackParameters;

/// Styles for adding spacing around ``:`` in bitfield definitions.
enum BitFieldColonSpacingStyle : int8_t {
Expand Down Expand Up @@ -3413,7 +3429,7 @@ struct FormatStyle {
/// items into as few lines as possible when they go over ``ColumnLimit``.
///
/// If ``Auto`` (the default), delegates to the value in
/// ``BinPackParameters``. If that is ``true``, bin-packs Objective-C
/// ``BinPackParameters``. If that is ``BinPack``, bin-packs Objective-C
/// protocol conformance list items into as few lines as possible
/// whenever they go over ``ColumnLimit``.
///
Expand All @@ -3425,13 +3441,13 @@ struct FormatStyle {
/// onto individual lines whenever they go over ``ColumnLimit``.
///
/// \code{.objc}
/// Always (or Auto, if BinPackParameters=true):
/// Always (or Auto, if BinPackParameters==BinPack):
/// @interface ccccccccccccc () <
/// ccccccccccccc, ccccccccccccc,
/// ccccccccccccc, ccccccccccccc> {
/// }
///
/// Never (or Auto, if BinPackParameters=false):
/// Never (or Auto, if BinPackParameters!=BinPack):
/// @interface ddddddddddddd () <
/// ddddddddddddd,
/// ddddddddddddd,
Expand Down
27 changes: 5 additions & 22 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,6 @@ static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
}

// Returns \c true if \c Current starts a new parameter.
static bool startsNextParameter(const FormatToken &Current,
const FormatStyle &Style) {
assert(Current.Previous);
const auto &Previous = *Current.Previous;
if (Current.is(TT_CtorInitializerComma) &&
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
return true;
}
if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
return true;
return Previous.is(tok::comma) && !Current.isTrailingComment() &&
((Previous.isNot(TT_CtorInitializerComma) ||
Style.BreakConstructorInitializers !=
FormatStyle::BCIS_BeforeComma) &&
(Previous.isNot(TT_InheritanceComma) ||
Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma));
}

// Returns \c true if \c Token in an alignable binary operator
static bool isAlignableBinaryOperator(const FormatToken &Token) {
// No need to align binary operators that only have two operands.
Expand Down Expand Up @@ -437,7 +418,8 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
// sets BreakBeforeParameter to avoid bin packing and this creates a
// completely unnecessary line break after a template type that isn't
// line-wrapped.
(Previous.NestingLevel == 1 || Style.BinPackParameters)) ||
(Previous.NestingLevel == 1 ||
Style.BinPackParameters == FormatStyle::BPPS_BinPack)) ||
(Style.BreakBeforeTernaryOperators && Current.is(TT_ConditionalExpr) &&
Previous.isNot(tok::question)) ||
(!Style.BreakBeforeTernaryOperators &&
Expand Down Expand Up @@ -1950,11 +1932,12 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
// for backwards compatibility.
bool ObjCBinPackProtocolList =
(Style.ObjCBinPackProtocolList == FormatStyle::BPS_Auto &&
Style.BinPackParameters) ||
Style.BinPackParameters == FormatStyle::BPPS_BinPack) ||
Style.ObjCBinPackProtocolList == FormatStyle::BPS_Always;

bool BinPackDeclaration =
(State.Line->Type != LT_ObjCDecl && Style.BinPackParameters) ||
(State.Line->Type != LT_ObjCDecl &&
Style.BinPackParameters == FormatStyle::BPPS_BinPack) ||
(State.Line->Type == LT_ObjCDecl && ObjCBinPackProtocolList);

bool GenericSelection =
Expand Down
19 changes: 16 additions & 3 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ template <> struct ScalarEnumerationTraits<FormatStyle::BinaryOperatorStyle> {
}
};

template <>
struct ScalarEnumerationTraits<FormatStyle::BinPackParametersStyle> {
static void enumeration(IO &IO, FormatStyle::BinPackParametersStyle &Value) {
IO.enumCase(Value, "BinPack", FormatStyle::BPPS_BinPack);
IO.enumCase(Value, "OnePerLine", FormatStyle::BPPS_OnePerLine);
IO.enumCase(Value, "AlwaysOnePerLine", FormatStyle::BPPS_AlwaysOnePerLine);

// For backward compatibility.
IO.enumCase(Value, "true", FormatStyle::BPPS_BinPack);
IO.enumCase(Value, "false", FormatStyle::BPPS_OnePerLine);
}
};

template <> struct ScalarEnumerationTraits<FormatStyle::BinPackStyle> {
static void enumeration(IO &IO, FormatStyle::BinPackStyle &Value) {
IO.enumCase(Value, "Auto", FormatStyle::BPS_Auto);
Expand Down Expand Up @@ -1460,7 +1473,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlwaysBreakBeforeMultilineStrings = false;
LLVMStyle.AttributeMacros.push_back("__capability");
LLVMStyle.BinPackArguments = true;
LLVMStyle.BinPackParameters = true;
LLVMStyle.BinPackParameters = FormatStyle::BPPS_BinPack;
LLVMStyle.BitFieldColonSpacing = FormatStyle::BFCS_Both;
LLVMStyle.BracedInitializerIndentWidth = std::nullopt;
LLVMStyle.BraceWrapping = {/*AfterCaseLabel=*/false,
Expand Down Expand Up @@ -1835,7 +1848,7 @@ FormatStyle getChromiumStyle(FormatStyle::LanguageKind Language) {
ChromiumStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_Inline;
ChromiumStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;
ChromiumStyle.AllowShortLoopsOnASingleLine = false;
ChromiumStyle.BinPackParameters = false;
ChromiumStyle.BinPackParameters = FormatStyle::BPPS_OnePerLine;
ChromiumStyle.DerivePointerAlignment = false;
if (Language == FormatStyle::LK_ObjC)
ChromiumStyle.ColumnLimit = 80;
Expand All @@ -1850,7 +1863,7 @@ FormatStyle getMozillaStyle() {
MozillaStyle.AlwaysBreakAfterDefinitionReturnType =
FormatStyle::DRTBS_TopLevel;
MozillaStyle.BinPackArguments = false;
MozillaStyle.BinPackParameters = false;
MozillaStyle.BinPackParameters = FormatStyle::BPPS_OnePerLine;
MozillaStyle.BreakAfterReturnType = FormatStyle::RTBS_TopLevel;
MozillaStyle.BreakBeforeBraces = FormatStyle::BS_Mozilla;
MozillaStyle.BreakConstructorInitializers = FormatStyle::BCIS_BeforeComma;
Expand Down
17 changes: 17 additions & 0 deletions clang/lib/Format/FormatToken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,22 @@ CommaSeparatedList::getColumnFormat(unsigned RemainingCharacters) const {
return BestFormat;
}

bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style) {
assert(Current.Previous);
const auto &Previous = *Current.Previous;
if (Current.is(TT_CtorInitializerComma) &&
Style.BreakConstructorInitializers == FormatStyle::BCIS_BeforeComma) {
return true;
}
if (Style.Language == FormatStyle::LK_Proto && Current.is(TT_SelectorName))
return true;
return Previous.is(tok::comma) && !Current.isTrailingComment() &&
((Previous.isNot(TT_CtorInitializerComma) ||
Style.BreakConstructorInitializers !=
FormatStyle::BCIS_BeforeComma) &&
(Previous.isNot(TT_InheritanceComma) ||
Style.BreakInheritanceList != FormatStyle::BILS_BeforeComma));
}

} // namespace format
} // namespace clang
3 changes: 3 additions & 0 deletions clang/lib/Format/FormatToken.h
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,9 @@ inline bool continuesLineComment(const FormatToken &FormatTok,
FormatTok.OriginalColumn >= MinContinueColumn;
}

// Returns \c true if \c Current starts a new parameter.
bool startsNextParameter(const FormatToken &Current, const FormatStyle &Style);

} // namespace format
} // namespace clang

Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5475,6 +5475,14 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
return true;
}

// Ignores the first parameter as this will be handled separately by
// BreakFunctionDefinitionParameters or AlignAfterOpenBracket.
if (Style.BinPackParameters == FormatStyle::BPPS_AlwaysOnePerLine &&
Line.MightBeFunctionDecl && !Left.opensScope() &&
startsNextParameter(Right, Style)) {
return true;
}

const auto *BeforeLeft = Left.Previous;
const auto *AfterRight = Right.Next;

Expand Down
14 changes: 13 additions & 1 deletion clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
CHECK_PARSE_BOOL(BinPackArguments);
CHECK_PARSE_BOOL(BinPackParameters);
CHECK_PARSE_BOOL(BreakAdjacentStringLiterals);
CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations);
CHECK_PARSE_BOOL(BreakBeforeTernaryOperators);
Expand Down Expand Up @@ -436,6 +435,19 @@ TEST(ConfigParseTest, ParsesConfiguration) {
CHECK_PARSE("BreakBeforeInheritanceComma: true", BreakInheritanceList,
FormatStyle::BILS_BeforeComma);

Style.BinPackParameters = FormatStyle::BPPS_OnePerLine;
CHECK_PARSE("BinPackParameters: BinPack", BinPackParameters,
FormatStyle::BPPS_BinPack);
CHECK_PARSE("BinPackParameters: OnePerLine", BinPackParameters,
FormatStyle::BPPS_OnePerLine);
CHECK_PARSE("BinPackParameters: AlwaysOnePerLine", BinPackParameters,
FormatStyle::BPPS_AlwaysOnePerLine);
// For backward compatibility.
CHECK_PARSE("BinPackParameters: true", BinPackParameters,
FormatStyle::BPPS_BinPack);
CHECK_PARSE("BinPackParameters: false", BinPackParameters,
FormatStyle::BPPS_OnePerLine);

Style.PackConstructorInitializers = FormatStyle::PCIS_BinPack;
CHECK_PARSE("PackConstructorInitializers: Never", PackConstructorInitializers,
FormatStyle::PCIS_Never);
Expand Down
Loading
Loading