Skip to content

Commit

Permalink
Clang-format: add finer-grained options for putting all arguments on …
Browse files Browse the repository at this point in the history
…one line

Summary:
Add two new options,
AllowAllArgumentsOnNextLine and
AllowAllConstructorInitializersOnNextLine.  These mirror the existing
AllowAllParametersOfDeclarationOnNextLine and allow me to support an
internal style guide where I work.  I think this would be generally
useful, some have asked for it on stackoverflow:

https://stackoverflow.com/questions/30057534/clang-format-binpackarguments-not-working-as-expected

https://stackoverflow.com/questions/38635106/clang-format-how-to-prevent-all-function-arguments-on-next-line

Reviewers: djasper, krasimir, MyDeveloperDay

Reviewed By: MyDeveloperDay

Subscribers: jkorous, MyDeveloperDay, aol-nnov, lebedev.ri, uohcsemaj, cfe-commits, klimek

Differential Revision: https://reviews.llvm.org/D40988

Patch By: russellmcc  (Russell McClellan)

llvm-svn: 356834
  • Loading branch information
mydeveloperday committed Mar 23, 2019
1 parent a87ba1c commit c6deae4
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 9 deletions.
35 changes: 35 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Expand Up @@ -277,6 +277,41 @@ the configuration (without a prefix: ``Auto``).
int a; // My comment a vs. int a; // My comment a
int b = 2; // comment b int b = 2; // comment about b

**AllowAllArgumentsOnNextLine** (``bool``)
If a function call or braced initializer list doesn't fit on a
line, allow putting all arguments onto the next line, even if
``BinPackArguments`` is ``false``.

.. code-block:: c++

true:
callFunction(
a, b, c, d);

false:
callFunction(a,
b,
c,
d);

**AllowAllConstructorInitializersOnNextLine** (``bool``)
If a constructor definition with a member initializer list doesn't
fit on a single line, allow putting all member initializers onto the next
line, if ```ConstructorInitializerAllOnOneLineOrOnePerLine``` is true.
Note that this parameter has no effect if
```ConstructorInitializerAllOnOneLineOrOnePerLine``` is false.

.. code-block:: c++

true:
MyClass::MyClass() :
member0(0), member1(2) {}

false:
MyClass::MyClass() :
member0(0),
member1(2) {}

**AllowAllParametersOfDeclarationOnNextLine** (``bool``)
If the function declaration doesn't fit on a line,
allow putting all parameters of a function declaration onto
Expand Down
35 changes: 35 additions & 0 deletions clang/include/clang/Format/Format.h
Expand Up @@ -154,6 +154,38 @@ struct FormatStyle {
/// \endcode
bool AlignTrailingComments;

/// \brief If a function call or braced initializer list doesn't fit on a
/// line, allow putting all arguments onto the next line, even if
/// ``BinPackArguments`` is ``false``.
/// \code
/// true:
/// callFunction(
/// a, b, c, d);
///
/// false:
/// callFunction(a,
/// b,
/// c,
/// d);
/// \endcode
bool AllowAllArgumentsOnNextLine;

/// \brief If a constructor definition with a member initializer list doesn't
/// fit on a single line, allow putting all member initializers onto the next
/// line, if ```ConstructorInitializerAllOnOneLineOrOnePerLine``` is true.
/// Note that this parameter has no effect if
/// ```ConstructorInitializerAllOnOneLineOrOnePerLine``` is false.
/// \code
/// true:
/// MyClass::MyClass() :
/// member0(0), member1(2) {}
///
/// false:
/// MyClass::MyClass() :
/// member0(0),
/// member1(2) {}
bool AllowAllConstructorInitializersOnNextLine;

/// 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``.
Expand Down Expand Up @@ -1761,6 +1793,9 @@ struct FormatStyle {
AlignEscapedNewlines == R.AlignEscapedNewlines &&
AlignOperands == R.AlignOperands &&
AlignTrailingComments == R.AlignTrailingComments &&
AllowAllArgumentsOnNextLine == R.AllowAllArgumentsOnNextLine &&
AllowAllConstructorInitializersOnNextLine ==
R.AllowAllConstructorInitializersOnNextLine &&
AllowAllParametersOfDeclarationOnNextLine ==
R.AllowAllParametersOfDeclarationOnNextLine &&
AllowShortBlocksOnASingleLine == R.AllowShortBlocksOnASingleLine &&
Expand Down
32 changes: 26 additions & 6 deletions clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -881,14 +881,30 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
State.Stack.back().BreakBeforeClosingBrace = true;

if (State.Stack.back().AvoidBinPacking) {
// If we are breaking after '(', '{', '<', this is not bin packing
// unless AllowAllParametersOfDeclarationOnNextLine is false or this is a
// dict/object literal.
if (!Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
// If we are breaking after '(', '{', '<', or this is the break after a ':'
// to start a member initializater list in a constructor, this should not
// be considered bin packing unless the relevant AllowAll option is false or
// this is a dict/object literal.
bool PreviousIsBreakingCtorInitializerColon =
Previous.is(TT_CtorInitializerColon) &&
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon;
if (!(Previous.isOneOf(tok::l_paren, tok::l_brace, TT_BinaryOperator) ||
PreviousIsBreakingCtorInitializerColon) ||
(!Style.AllowAllParametersOfDeclarationOnNextLine &&
State.Line->MustBeDeclaration) ||
(!Style.AllowAllArgumentsOnNextLine &&
!State.Line->MustBeDeclaration) ||
(!Style.AllowAllConstructorInitializersOnNextLine &&
PreviousIsBreakingCtorInitializerColon) ||
Previous.is(TT_DictLiteral))
State.Stack.back().BreakBeforeParameter = true;

// If we are breaking after a ':' to start a member initializer list,
// and we allow all arguments on the next line, we should not break
// before the next parameter.
if (PreviousIsBreakingCtorInitializerColon &&
Style.AllowAllConstructorInitializersOnNextLine)
State.Stack.back().BreakBeforeParameter = false;
}

return Penalty;
Expand Down Expand Up @@ -1102,9 +1118,13 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
? 0
: 2);
State.Stack.back().NestedBlockIndent = State.Stack.back().Indent;
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) {
State.Stack.back().AvoidBinPacking = true;
State.Stack.back().BreakBeforeParameter = false;
State.Stack.back().BreakBeforeParameter =
!Style.AllowAllConstructorInitializersOnNextLine;
} else {
State.Stack.back().BreakBeforeParameter = false;
}
}
if (Current.is(TT_CtorInitializerColon) &&
Style.BreakConstructorInitializers == FormatStyle::BCIS_AfterColon) {
Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Format/Format.cpp
Expand Up @@ -335,6 +335,10 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
IO.mapOptional("AlignOperands", Style.AlignOperands);
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
IO.mapOptional("AllowAllArgumentsOnNextLine",
Style.AllowAllArgumentsOnNextLine);
IO.mapOptional("AllowAllConstructorInitializersOnNextLine",
Style.AllowAllConstructorInitializersOnNextLine);
IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
Style.AllowAllParametersOfDeclarationOnNextLine);
IO.mapOptional("AllowShortBlocksOnASingleLine",
Expand All @@ -351,6 +355,7 @@ template <> struct MappingTraits<FormatStyle> {
Style.AlwaysBreakAfterDefinitionReturnType);
IO.mapOptional("AlwaysBreakAfterReturnType",
Style.AlwaysBreakAfterReturnType);

// If AlwaysBreakAfterDefinitionReturnType was specified but
// AlwaysBreakAfterReturnType was not, initialize the latter from the
// former for backwards compatibility.
Expand Down Expand Up @@ -641,6 +646,8 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignTrailingComments = true;
LLVMStyle.AlignConsecutiveAssignments = false;
LLVMStyle.AlignConsecutiveDeclarations = false;
LLVMStyle.AllowAllArgumentsOnNextLine = true;
LLVMStyle.AllowAllConstructorInitializersOnNextLine = true;
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
LLVMStyle.AllowShortBlocksOnASingleLine = false;
Expand Down

0 comments on commit c6deae4

Please sign in to comment.