Skip to content

Commit 8067b5c

Browse files
authored
[clang-format] Add BreakAfterOpenBracket* and BreakBeforeCloseBracket* (#108332)
Replace the `AlwaysBreak` and `BlockIndent` suboptions of `AlignAfterOpenBracket` with new style options `BreakAfterOpenBracket*` and `BreakBeforeCloseBracket*` for `*` in `BracedList` for braced list initializers, `if` for if conditional statements, `Loop` for loop control statements (for/while), `Switch` for switch statements, and `Function` for function calls/declarations/definitions. Deprecates `AlwaysBreak` and `BlockIndent`. Fixes #67738 Fixes #79176 Fixes #80123 Fixes #151844
1 parent ba0be89 commit 8067b5c

File tree

12 files changed

+566
-192
lines changed

12 files changed

+566
-192
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 149 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -197,57 +197,29 @@ the configuration (without a prefix: ``Auto``).
197197

198198
.. _AlignAfterOpenBracket:
199199

200-
**AlignAfterOpenBracket** (``BracketAlignmentStyle``) :versionbadge:`clang-format 3.8` :ref:`<AlignAfterOpenBracket>`
200+
**AlignAfterOpenBracket** (``Boolean``) :versionbadge:`clang-format 3.8` :ref:`<AlignAfterOpenBracket>`
201201
If ``true``, horizontally aligns arguments after an open bracket.
202202

203-
This applies to round brackets (parentheses), angle brackets and square
204-
brackets.
205-
206-
Possible values:
207-
208-
* ``BAS_Align`` (in configuration: ``Align``)
209-
Align parameters on the open bracket, e.g.:
210-
211-
.. code-block:: c++
212-
213-
someLongFunction(argument1,
214-
argument2);
215-
216-
* ``BAS_DontAlign`` (in configuration: ``DontAlign``)
217-
Don't align, instead use ``ContinuationIndentWidth``, e.g.:
218-
219-
.. code-block:: c++
220-
221-
someLongFunction(argument1,
222-
argument2);
223-
224-
* ``BAS_AlwaysBreak`` (in configuration: ``AlwaysBreak``)
225-
Always break after an open bracket, if the parameters don't fit
226-
on a single line, e.g.:
227-
228-
.. code-block:: c++
229203

230-
someLongFunction(
231-
argument1, argument2);
232-
233-
* ``BAS_BlockIndent`` (in configuration: ``BlockIndent``)
234-
Always break after an open bracket, if the parameters don't fit
235-
on a single line. Closing brackets will be placed on a new line.
236-
E.g.:
237-
238-
.. code-block:: c++
204+
.. code-block:: c++
239205

240-
someLongFunction(
241-
argument1, argument2
242-
)
206+
true: vs. false
207+
someLongFunction(argument1, someLongFunction(argument1,
208+
argument2); argument2);
243209

244210

245-
.. note::
246-
247-
This currently only applies to braced initializer lists (when
248-
``Cpp11BracedListStyle`` is not ``Block``) and parentheses.
211+
.. note::
249212

213+
As of clang-format 22 this option is a bool with the previous
214+
option of ``Align`` replaced with ``true``, ``DontAlign`` replaced
215+
with ``false``, and the options of ``AlwaysBreak`` and ``BlockIndent``
216+
replaced with ``true`` and with setting of new style options using
217+
``BreakAfterOpenBracketBracedList``, ``BreakAfterOpenBracketFunction``,
218+
``BreakAfterOpenBracketIf``, ``BreakBeforeCloseBracketBracedList``,
219+
``BreakBeforeCloseBracketFunction``, and ``BreakBeforeCloseBracketIf``.
250220

221+
This applies to round brackets (parentheses), angle brackets and square
222+
brackets.
251223

252224
.. _AlignArrayOfStructures:
253225

@@ -2746,6 +2718,67 @@ the configuration (without a prefix: ``Auto``).
27462718
@Mock
27472719
DataLoad loader;
27482720
2721+
.. _BreakAfterOpenBracketBracedList:
2722+
2723+
**BreakAfterOpenBracketBracedList** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakAfterOpenBracketBracedList>`
2724+
Force break after the left bracket of a braced initializer list (when
2725+
``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
2726+
limit.
2727+
2728+
.. code-block:: c++
2729+
2730+
true: false:
2731+
vector<int> x { vs. vector<int> x {1,
2732+
1, 2, 3} 2, 3}
2733+
2734+
.. _BreakAfterOpenBracketFunction:
2735+
2736+
**BreakAfterOpenBracketFunction** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakAfterOpenBracketFunction>`
2737+
Force break after the left parenthesis of a function (declaration,
2738+
definition, call) when the parameters exceed the column limit.
2739+
2740+
.. code-block:: c++
2741+
2742+
true: false:
2743+
foo ( vs. foo (a,
2744+
a , b) b)
2745+
2746+
.. _BreakAfterOpenBracketIf:
2747+
2748+
**BreakAfterOpenBracketIf** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakAfterOpenBracketIf>`
2749+
Force break after the left parenthesis of an if control statement
2750+
when the expression exceeds the column limit.
2751+
2752+
.. code-block:: c++
2753+
2754+
true: false:
2755+
if constexpr ( vs. if constexpr (a ||
2756+
a || b) b)
2757+
2758+
.. _BreakAfterOpenBracketLoop:
2759+
2760+
**BreakAfterOpenBracketLoop** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakAfterOpenBracketLoop>`
2761+
Force break after the left parenthesis of a loop control statement
2762+
when the expression exceeds the column limit.
2763+
2764+
.. code-block:: c++
2765+
2766+
true: false:
2767+
while ( vs. while (a &&
2768+
a && b) { b) {
2769+
2770+
.. _BreakAfterOpenBracketSwitch:
2771+
2772+
**BreakAfterOpenBracketSwitch** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakAfterOpenBracketSwitch>`
2773+
Force break after the left parenthesis of a switch control statement
2774+
when the expression exceeds the column limit.
2775+
2776+
.. code-block:: c++
2777+
2778+
true: false:
2779+
switch ( vs. switch (a +
2780+
a + b) { b) {
2781+
27492782
.. _BreakAfterReturnType:
27502783

27512784
**BreakAfterReturnType** (``ReturnTypeBreakingStyle``) :versionbadge:`clang-format 19` :ref:`<BreakAfterReturnType>`
@@ -3383,6 +3416,79 @@ the configuration (without a prefix: ``Auto``).
33833416

33843417

33853418

3419+
.. _BreakBeforeCloseBracketBracedList:
3420+
3421+
**BreakBeforeCloseBracketBracedList** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakBeforeCloseBracketBracedList>`
3422+
Force break before the right bracket of a braced initializer list (when
3423+
``Cpp11BracedListStyle`` is ``true``) when the list exceeds the column
3424+
limit. The break before the right bracket is only made if there is a
3425+
break after the opening bracket.
3426+
3427+
.. code-block:: c++
3428+
3429+
true: false:
3430+
vector<int> x { vs. vector<int> x {
3431+
1, 2, 3 1, 2, 3}
3432+
}
3433+
3434+
.. _BreakBeforeCloseBracketFunction:
3435+
3436+
**BreakBeforeCloseBracketFunction** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakBeforeCloseBracketFunction>`
3437+
Force break before the right parenthesis of a function (declaration,
3438+
definition, call) when the parameters exceed the column limit.
3439+
3440+
.. code-block:: c++
3441+
3442+
true: false:
3443+
foo ( vs. foo (
3444+
a , b a , b)
3445+
)
3446+
3447+
.. _BreakBeforeCloseBracketIf:
3448+
3449+
**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakBeforeCloseBracketIf>`
3450+
Force break before the right parenthesis of an if control statement
3451+
when the expression exceeds the column limit. The break before the
3452+
closing parenthesis is only made if there is a break after the opening
3453+
parenthesis.
3454+
3455+
.. code-block:: c++
3456+
3457+
true: false:
3458+
if constexpr ( vs. if constexpr (
3459+
a || b a || b )
3460+
)
3461+
3462+
.. _BreakBeforeCloseBracketLoop:
3463+
3464+
**BreakBeforeCloseBracketLoop** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakBeforeCloseBracketLoop>`
3465+
Force break before the right parenthesis of a loop control statement
3466+
when the expression exceeds the column limit. The break before the
3467+
closing parenthesis is only made if there is a break after the opening
3468+
parenthesis.
3469+
3470+
.. code-block:: c++
3471+
3472+
true: false:
3473+
while ( vs. while (
3474+
a && b a && b) {
3475+
) {
3476+
3477+
.. _BreakBeforeCloseBracketSwitch:
3478+
3479+
**BreakBeforeCloseBracketSwitch** (``Boolean``) :versionbadge:`clang-format 22` :ref:`<BreakBeforeCloseBracketSwitch>`
3480+
Force break before the right parenthesis of a switch control statement
3481+
when the expression exceeds the column limit. The break before the
3482+
closing parenthesis is only made if there is a break after the opening
3483+
parenthesis.
3484+
3485+
.. code-block:: c++
3486+
3487+
true: false:
3488+
switch ( vs. switch (
3489+
a + b a + b) {
3490+
) {
3491+
33863492
.. _BreakBeforeConceptDeclarations:
33873493

33883494
**BreakBeforeConceptDeclarations** (``BreakBeforeConceptDeclarationsStyle``) :versionbadge:`clang-format 12` :ref:`<BreakBeforeConceptDeclarations>`

clang/docs/ReleaseNotes.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,14 @@ clang-format
613613
literals.
614614
- Add ``Leave`` suboption to ``IndentPPDirectives``.
615615
- Add ``AllowBreakBeforeQtProperty`` option.
616+
- Add ``BreakAfterOpenBracketBracedList'', ``BreakAfterOpenBracketFunction'',
617+
``BreakAfterOpenBracketIf``, ``BreakAfterOpenBracketLoop``,
618+
``BreakAfterOpenBracketSwitch``, ``BreakBeforeCloseBracketBracedList'',
619+
``BreakBeforeCloseBracketFunction``, ``BreakBeforeCloseBracketIf``,
620+
``BreakBeforeCloseBracketLoop``, ``BreakBeforeCloseBracketSwitch`` options.
621+
- Deprecate ``AlwaysBreak`` and ``BlockIndent`` suboptions from the
622+
``AlignAfterOpenBracket`` option, and make ``AlignAfterOpenBracket`` a
623+
``bool`` type.
616624

617625
libclang
618626
--------

0 commit comments

Comments
 (0)