Skip to content

[clang-format] Add AlignAfterOpenBracketOptions #108332

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
81 changes: 81 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2736,6 +2736,42 @@ the configuration (without a prefix: ``Auto``).
@Mock
DataLoad loader;

.. _BreakAfterOpenBracketIf:

**BreakAfterOpenBracketIf** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakAfterOpenBracketIf>`
Force break after the left parenthesis of an if control statement
when the expression exceeds the column limit.

.. code-block:: c++

true: false:
if constexpr ( vs. if constexpr (a ||
a || b) b)

.. _BreakAfterOpenBracketLoop:

**BreakAfterOpenBracketLoop** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakAfterOpenBracketLoop>`
Force break after the left parenthesis of a loop control statement
when the expression exceeds the column limit.

.. code-block:: c++

true: false:
while ( vs. while (a &&
a && b) { b) {

.. _BreakAfterOpenBracketSwitch:

**BreakAfterOpenBracketSwitch** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakAfterOpenBracketSwitch>`
Force break after the left parenthesis of a switch control statement
when the expression exceeds the column limit.

.. code-block:: c++

true: false:
switch ( vs. switch (a &&
a && b) { b) {

.. _BreakAfterReturnType:

**BreakAfterReturnType** (``ReturnTypeBreakingStyle``) :versionbadge:`clang-format 19` :ref:`¶ <BreakAfterReturnType>`
Expand Down Expand Up @@ -3373,6 +3409,51 @@ the configuration (without a prefix: ``Auto``).



.. _BreakBeforeCloseBracketIf:

**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakBeforeCloseBracketIf>`
Force break before the right parenthesis of an if control statement
when the expression exceeds the column limit. The break before the
closing parenthesis is only made if there is a break after the opening
parenthesis.

.. code-block:: c++

true: false:
if constexpr ( vs. if constexpr (
a || b a || b )
)

.. _BreakBeforeCloseBracketLoop:

**BreakBeforeCloseBracketLoop** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakBeforeCloseBracketLoop>`
Force break before the right parenthesis of a loop control statement
when the expression exceeds the column limit. The break before the
closing parenthesis is only made if there is a break after the opening
parenthesis.

.. code-block:: c++

true: false:
while ( vs. while (
a && b a && b) {
) {

.. _BreakBeforeCloseBracketSwitch:

**BreakBeforeCloseBracketSwitch** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ <BreakBeforeCloseBracketSwitch>`
Force break before the right parenthesis of a switch control statement
when the expression exceeds the column limit. The break before the
closing parenthesis is only made if there is a break after the opening
parenthesis.

.. code-block:: c++

true: false:
switch ( vs. switch (
a && b a && b) {
) {

.. _BreakBeforeConceptDeclarations:

**BreakBeforeConceptDeclarations** (``BreakBeforeConceptDeclarationsStyle``) :versionbadge:`clang-format 12` :ref:`¶ <BreakBeforeConceptDeclarations>`
Expand Down
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,9 @@ clang-format
``enum`` enumerator lists.
- Add ``OneLineFormatOffRegex`` option for turning formatting off for one line.
- Add ``SpaceAfterOperatorKeyword`` option.
- Add ``BreakAfterOpenBracketIf``, ``BreakAfterOpenBracketLoop``,
``BreakAfterOpenBracketSwitch``, ``BreakBeforeCloseBracketIf``,
``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch`` options.

clang-refactor
--------------
Expand Down
75 changes: 75 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,36 @@ struct FormatStyle {
/// \version 3.3
int AccessModifierOffset;

/// Force break after the left parenthesis of an if control statement
/// when the expression exceeds the column limit.
/// \code
/// true: false:
/// if constexpr ( vs. if constexpr (a ||
/// a || b) b)
/// \endcode
/// \version 21
bool BreakAfterOpenBracketIf;

/// Force break after the left parenthesis of a loop control statement
/// when the expression exceeds the column limit.
/// \code
/// true: false:
/// while ( vs. while (a &&
/// a && b) { b) {
/// \endcode
/// \version 21
bool BreakAfterOpenBracketLoop;

/// Force break after the left parenthesis of a switch control statement
/// when the expression exceeds the column limit.
/// \code
/// true: false:
/// switch ( vs. switch (a &&
/// a && b) { b) {
/// \endcode
/// \version 21
bool BreakAfterOpenBracketSwitch;

/// Different styles for aligning after open brackets.
enum BracketAlignmentStyle : int8_t {
/// Align parameters on the open bracket, e.g.:
Expand Down Expand Up @@ -2215,6 +2245,45 @@ struct FormatStyle {
/// \version 3.7
BraceBreakingStyle BreakBeforeBraces;

/// Force break before the right parenthesis of an if control statement
/// when the expression exceeds the column limit. The break before the
/// closing parenthesis is only made if there is a break after the opening
/// parenthesis.
/// \code
/// true: false:
/// if constexpr ( vs. if constexpr (
/// a || b a || b )
/// )
/// \endcode
/// \version 21
bool BreakBeforeCloseBracketIf;

/// Force break before the right parenthesis of a loop control statement
/// when the expression exceeds the column limit. The break before the
/// closing parenthesis is only made if there is a break after the opening
/// parenthesis.
/// \code
/// true: false:
/// while ( vs. while (
/// a && b a && b) {
/// ) {
/// \endcode
/// \version 21
bool BreakBeforeCloseBracketLoop;

/// Force break before the right parenthesis of a switch control statement
/// when the expression exceeds the column limit. The break before the
/// closing parenthesis is only made if there is a break after the opening
/// parenthesis.
/// \code
/// true: false:
/// switch ( vs. switch (
/// a && b a && b) {
/// ) {
/// \endcode
/// \version 21
bool BreakBeforeCloseBracketSwitch;

/// Different ways to break before concept declarations.
enum BreakBeforeConceptDeclarationsStyle : int8_t {
/// Keep the template declaration line together with ``concept``.
Expand Down Expand Up @@ -5354,10 +5423,16 @@ struct FormatStyle {
BreakAdjacentStringLiterals == R.BreakAdjacentStringLiterals &&
BreakAfterAttributes == R.BreakAfterAttributes &&
BreakAfterJavaFieldAnnotations == R.BreakAfterJavaFieldAnnotations &&
BreakAfterOpenBracketIf == R.BreakAfterOpenBracketIf &&
BreakAfterOpenBracketLoop == R.BreakAfterOpenBracketLoop &&
BreakAfterOpenBracketSwitch == R.BreakAfterOpenBracketSwitch &&
BreakAfterReturnType == R.BreakAfterReturnType &&
BreakArrays == R.BreakArrays &&
BreakBeforeBinaryOperators == R.BreakBeforeBinaryOperators &&
BreakBeforeBraces == R.BreakBeforeBraces &&
BreakBeforeCloseBracketIf == R.BreakBeforeCloseBracketIf &&
BreakBeforeCloseBracketLoop == R.BreakBeforeCloseBracketLoop &&
BreakBeforeCloseBracketSwitch == R.BreakBeforeCloseBracketSwitch &&
BreakBeforeConceptDeclarations == R.BreakBeforeConceptDeclarations &&
BreakBeforeInlineASMColon == R.BreakBeforeInlineASMColon &&
BreakBeforeTemplateCloser == R.BreakBeforeTemplateCloser &&
Expand Down
90 changes: 67 additions & 23 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,13 @@ bool ContinuationIndenter::canBreak(const LineState &State) {

// Allow breaking before the right parens with block indentation if there was
// a break after the left parens, which is tracked by BreakBeforeClosingParen.
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
Current.is(tok::r_paren)) {
bool might_break_before =
Style.BreakBeforeCloseBracketIf || Style.BreakBeforeCloseBracketLoop ||
Style.BreakBeforeCloseBracketSwitch ||
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;

if (might_break_before && Current.is(tok::r_paren))
return CurrentState.BreakBeforeClosingParen;
}

if (Style.BreakBeforeTemplateCloser && Current.is(TT_TemplateCloser))
return CurrentState.BreakBeforeClosingAngle;
Expand Down Expand Up @@ -814,6 +817,11 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
// parenthesis by disallowing any further line breaks if there is no line
// break after the opening parenthesis. Don't break if it doesn't conserve
// columns.
auto IsLoopConditional = [&](const FormatToken &Tok) {
return Tok.isOneOf(tok::kw_for, tok::kw_while) ||
(Style.isJavaScript() && Tok.is(Keywords.kw_await) && Tok.Previous &&
Tok.Previous->is(tok::kw_for));
};
auto IsOpeningBracket = [&](const FormatToken &Tok) {
auto IsStartOfBracedList = [&]() {
return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
Expand All @@ -826,25 +834,32 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
if (!Tok.Previous)
return true;
if (Tok.Previous->isIf())
return Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak;
return !Tok.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
tok::kw_switch) &&
!(Style.isJavaScript() && Tok.Previous->is(Keywords.kw_await));
return Style.BreakAfterOpenBracketIf;
if (IsLoopConditional(*Tok.Previous))
return Style.BreakAfterOpenBracketLoop;
if (Tok.Previous->is(tok::kw_switch))
return Style.BreakAfterOpenBracketSwitch;
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
return !Tok.Previous->is(TT_CastRParen) &&
!(Style.isJavaScript() && Tok.is(Keywords.kw_await));
}
return false;
};
auto IsFunctionCallParen = [](const FormatToken &Tok) {
return Tok.is(tok::l_paren) && Tok.ParameterCount > 0 && Tok.Previous &&
Tok.Previous->is(tok::identifier);
};
auto IsInTemplateString = [this](const FormatToken &Tok) {
auto IsInTemplateString = [this](const FormatToken &Tok, bool NestBlocks) {
if (!Style.isJavaScript())
return false;
for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous) {
if (Prev->is(TT_TemplateString) && Prev->opensScope())
return true;
if (Prev->opensScope() ||
(Prev->is(TT_TemplateString) && Prev->closesScope())) {
break;
}
if (Prev->opensScope() && !NestBlocks)
return false;
if (Prev->is(TT_TemplateString) && Prev->closesScope())
return false;
}
return false;
};
Expand All @@ -866,21 +881,25 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
Tok.isOneOf(tok::ellipsis, Keywords.kw_await))) {
return true;
}
const auto *Previous = Tok.Previous;
if (!Previous || (!Previous->isOneOf(TT_FunctionDeclarationLParen,
TT_LambdaDefinitionLParen) &&
!IsFunctionCallParen(*Previous))) {
const auto *Previous = TokAfterLParen.Previous;
assert(Previous); // IsOpeningBracket(Previous)
if (Previous->Previous &&
(Previous->Previous->isIf() || IsLoopConditional(*Previous->Previous) ||
Previous->Previous->is(tok::kw_switch))) {
return false;
}
if (!Previous->isOneOf(TT_FunctionDeclarationLParen,
TT_LambdaDefinitionLParen) &&
!IsFunctionCallParen(*Previous)) {
return true;
}
if (IsOpeningBracket(Tok) || IsInTemplateString(Tok))
if (IsOpeningBracket(Tok) || IsInTemplateString(Tok, true))
return true;
const auto *Next = Tok.Next;
return !Next || Next->isMemberAccess() ||
Next->is(TT_FunctionDeclarationLParen) || IsFunctionCallParen(*Next);
};
if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) &&
IsOpeningBracket(Previous) && State.Column > getNewLineColumn(State) &&
if (IsOpeningBracket(Previous) && State.Column > getNewLineColumn(State) &&
// Don't do this for simple (no expressions) one-argument function calls
// as that feels like needlessly wasting whitespace, e.g.:
//
Expand Down Expand Up @@ -910,7 +929,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
!(Current.MacroParent && Previous.MacroParent) &&
(Current.isNot(TT_LineComment) ||
Previous.isOneOf(BK_BracedInit, TT_VerilogMultiLineListLParen)) &&
!IsInTemplateString(Current)) {
!IsInTemplateString(Current, false)) {
CurrentState.Indent = State.Column + Spaces;
CurrentState.IsAligned = true;
}
Expand Down Expand Up @@ -1247,8 +1266,28 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
}

if (PreviousNonComment && PreviousNonComment->is(tok::l_paren)) {
CurrentState.BreakBeforeClosingParen =
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
auto Previous = PreviousNonComment->Previous;
if (Previous) {

auto IsLoopConditional = [&](const FormatToken &Tok) {
return Tok.isOneOf(tok::kw_for, tok::kw_while) ||
(Style.isJavaScript() && Tok.is(Keywords.kw_await) &&
Tok.Previous && Tok.Previous->is(tok::kw_for));
};

if (Previous->isIf()) {
CurrentState.BreakBeforeClosingParen = Style.BreakBeforeCloseBracketIf;
} else if (IsLoopConditional(*Previous)) {
CurrentState.BreakBeforeClosingParen =
Style.BreakBeforeCloseBracketLoop;
} else if (Previous->is(tok::kw_switch)) {
CurrentState.BreakBeforeClosingParen =
Style.BreakBeforeCloseBracketSwitch;
} else {
CurrentState.BreakBeforeClosingParen =
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
}
}
}

if (PreviousNonComment && PreviousNonComment->is(TT_TemplateOpener))
Expand Down Expand Up @@ -1398,6 +1437,11 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
if ((Style.BreakBeforeCloseBracketIf || Style.BreakBeforeCloseBracketLoop ||
Style.BreakBeforeCloseBracketSwitch) &&
Current.is(tok::r_paren) && State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
}
if (Style.BreakBeforeTemplateCloser && Current.is(TT_TemplateCloser) &&
State.Stack.size() > 1) {
return State.Stack[State.Stack.size() - 2].LastSpace;
Expand Down
Loading