[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
owenca wrote: I prefer that we limit this to breaking after the left parenthesis of a control statement, with true/false or Always, Never, Multiline, BlockIndent, etc. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
HazardyKnusperkeks wrote: Was there a change which I should look at again? I find the GitHub UI for reviews terrible. If it was just a rebase my vote stands. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From 5cec30f5d93a22f10a985cb3e4418e7d29d31a00 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/7] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index bf6dd9e13915f8c..8e3383ec027dede 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 16956b4e0fbd4f3..4a24e39ab3aff7a 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside b
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From 8ca6e30c6fbe1e51b711c5c15cdeb6517097f3e8 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/7] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index bbb912eb10e94d..7ad59fa7a7be1f 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 6f432d1d503154..6488de9d200512 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside brack
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
gedare wrote: Rebased to `main` for `21.0.0git` https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From 8ca6e30c6fbe1e51b711c5c15cdeb6517097f3e8 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/5] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index bbb912eb10e94d..7ad59fa7a7be1f 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 6f432d1d503154..6488de9d200512 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside brack
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -1169,6 +1181,18 @@ template <> struct MappingTraits { IO.mapOptional("WhitespaceSensitiveMacros", Style.WhitespaceSensitiveMacros); +// If AlignAfterOpenBracket was specified but AlignAfterOpenBracketBreak +// was not, initialize the latter for backwards compatibility. +if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak || + Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) && +Style.AlignAfterOpenBracketBreak == +FormatStyle::AlignAfterOpenBracketCustom()) { HazardyKnusperkeks wrote: Ok, got it. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/HazardyKnusperkeks approved this pull request. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -1169,6 +1181,18 @@ template <> struct MappingTraits { IO.mapOptional("WhitespaceSensitiveMacros", Style.WhitespaceSensitiveMacros); +// If AlignAfterOpenBracket was specified but AlignAfterOpenBracketBreak +// was not, initialize the latter for backwards compatibility. +if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak || + Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) && +Style.AlignAfterOpenBracketBreak == +FormatStyle::AlignAfterOpenBracketCustom()) { gedare wrote: Oh. I misunderstood this question at first. I guess it is possible, but it is not likely, for someone to explicitly set `AlwaysBreak` or `BlockIndent` together with `AlignAfterOpenBracketBreak = {false, false, false}`. It's a conflicting set of options in some way. It could be documented as invalid? https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare deleted https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From c7b34d10bb8f937f9a11778c327f82cee8e60fe5 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/5] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index bbb912eb10e94d..7ad59fa7a7be1f 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 6f432d1d503154..6488de9d200512 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside brack
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -1452,6 +1476,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { FormatStyle LLVMStyle; LLVMStyle.AccessModifierOffset = -2; LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; + LLVMStyle.AlignAfterOpenBracketBreak = {}; gedare wrote: Ok yes, now it is explicitly initialized. This isn't a functional change since the `FormatStyle::AlignAfterOpenBracketCustom() == {false, false, false} == {}` but I suppose it makes more sense to be clear. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -1169,6 +1181,18 @@ template <> struct MappingTraits { IO.mapOptional("WhitespaceSensitiveMacros", Style.WhitespaceSensitiveMacros); +// If AlignAfterOpenBracket was specified but AlignAfterOpenBracketBreak +// was not, initialize the latter for backwards compatibility. +if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak || + Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) && +Style.AlignAfterOpenBracketBreak == +FormatStyle::AlignAfterOpenBracketCustom()) { gedare wrote: Got your point. yes, now it is done that way. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From c7b34d10bb8f937f9a11778c327f82cee8e60fe5 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/4] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index bbb912eb10e94d..7ad59fa7a7be1f 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 6f432d1d503154..6488de9d200512 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside brack
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From c7b34d10bb8f937f9a11778c327f82cee8e60fe5 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/3] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index bbb912eb10e94d..7ad59fa7a7be1f 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 6f432d1d503154..6488de9d200512 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside brack
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
gedare wrote: There's a problem between this implementation and the fix in #119989 that I need to figure out. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -1452,6 +1476,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { FormatStyle LLVMStyle; LLVMStyle.AccessModifierOffset = -2; LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; + LLVMStyle.AlignAfterOpenBracketBreak = {}; HazardyKnusperkeks wrote: But when it is not set to Align when reading the configuration, but that didn't set `AlignAfterOpenBracketBreak`. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -1452,6 +1476,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { FormatStyle LLVMStyle; LLVMStyle.AccessModifierOffset = -2; LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; + LLVMStyle.AlignAfterOpenBracketBreak = {}; gedare wrote: These options (currently) have no effect when using `BAS_Align`. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -811,10 +816,11 @@ 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.AlignAfterOpenBracketBreak.InIfConditionalStatements; gedare wrote: It's been refactored into the `IsOtherConditional()`. I may need to look a bit closer at the JavaScript (TypeScript) cases. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
gedare wrote: > I don't think this is the way to go, or at least not with this name. > > How about going the `Custom` way? Adding a `Custom` option to `AlignAfterOpenBracket` is an interesting proposal. There have been some ideas floated in other Issues and PRs related to this, for example: * #118046 * #80049 My concern with a `Custom` option is what to include. At this point it could be an enumeration of the different kinds of blocks (functions, if/else, for, other) and the different kinds of openers (parens, braces, square brackets, angles), and alignment of the parameters (`Align`, `DontAlign`), and breaking behavior (`DontBreak`, `AlwaysBreak` or better yet `InitialBreak` and `BlockIndent` or better `LastBreak`). At the moment, these alignment and breaking options are fairly complex and bug-prone. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -811,10 +816,11 @@ 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.AlignAfterOpenBracketBreak.InIfConditionalStatements; HazardyKnusperkeks wrote: Don't you change the behavior here? https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -1169,6 +1181,18 @@ template <> struct MappingTraits { IO.mapOptional("WhitespaceSensitiveMacros", Style.WhitespaceSensitiveMacros); +// If AlignAfterOpenBracket was specified but AlignAfterOpenBracketBreak +// was not, initialize the latter for backwards compatibility. +if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak || + Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) && +Style.AlignAfterOpenBracketBreak == +FormatStyle::AlignAfterOpenBracketCustom()) { HazardyKnusperkeks wrote: Couldn't it be set explicitly to that? https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -1169,6 +1181,18 @@ template <> struct MappingTraits { IO.mapOptional("WhitespaceSensitiveMacros", Style.WhitespaceSensitiveMacros); +// If AlignAfterOpenBracket was specified but AlignAfterOpenBracketBreak +// was not, initialize the latter for backwards compatibility. +if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak || + Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) && +Style.AlignAfterOpenBracketBreak == +FormatStyle::AlignAfterOpenBracketCustom()) { + if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak) +Style.AlignAfterOpenBracketBreak.InIfConditionalStatements = true; HazardyKnusperkeks wrote: ```suggestion Style.AlignAfterOpenBracketBreak.InIfConditionalStatements = Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak; ``` https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/HazardyKnusperkeks edited https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/HazardyKnusperkeks commented: I don't think this is the way to go, or at least not with this name. How about going the `Custom` way? https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -1452,6 +1476,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { FormatStyle LLVMStyle; LLVMStyle.AccessModifierOffset = -2; LLVMStyle.AlignAfterOpenBracket = FormatStyle::BAS_Align; + LLVMStyle.AlignAfterOpenBracketBreak = {}; HazardyKnusperkeks wrote: You set for google true, false, true. Why not here? https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
gedare wrote: Rebased to main. This PR addresses several long-standing issues. It would be great to get a review. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From 70e0cbd5c648a532952ddb00a719b03a8f31160e Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/2] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 4be448171699ca..ecaa3699fe810c 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 6383934afa2c40..23cb0e59c20281 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside brack
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From f21e1c62aa64ce497d5d4b500f412752eae9ceb0 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/2] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index f36a5472b7e17d..e1f4b48119b78a 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index debba1c7822839..cdef3cf35a7112 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside brack
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From d4ea6c119580f4e153a0844f0e29cb393c340279 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/2] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 8add0a53e5be13..b2c04f37f8f621 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index a0762b088b68ef..bee91c52f12166 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside brack
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
gedare wrote: ping https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
gedare wrote: Rebased to main to pick up recent regression fixes BlockIndent/AlwaysBreak. https://github.com/llvm/llvm-project/pull/108332 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From d099408d791fef55b3064f6597bdd2fb0b4537da Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/2] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 44 +-- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 355 insertions(+), 11 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index a427d7cd40fcdd..62462c3202bee0 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index d8b62c7652a0f6..6e173d6d2b459f 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding options. +/// (``for/while/switch...``). +/// \code +///true: false: +///while ( vs. while (a && +/// a && b ) { b) { +/// \endcode +bool InOtherConditionalStatements; +/// Break inside brack
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Gedare Bloom (gedare) Changes Introduce sub-options for `AlignAfterOpenBracketBreak` to allow for control of `AlignAfterOpenBracket` with `AlwaysBreak` and `BlockIndent` selectively for `if` conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738 Fixes #79176 Fixes #80123 --- Patch is 29.57 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/108332.diff 8 Files Affected: - (modified) clang/docs/ClangFormatStyleOptions.rst (+60) - (modified) clang/docs/ReleaseNotes.rst (+2) - (modified) clang/include/clang/Format/Format.h (+73) - (modified) clang/lib/Format/ContinuationIndenter.cpp (+32-9) - (modified) clang/lib/Format/Format.cpp (+26) - (modified) clang/lib/Format/TokenAnnotator.cpp (+7-1) - (modified) clang/unittests/Format/ConfigParseTest.cpp (+12) - (modified) clang/unittests/Format/FormatTest.cpp (+143) ``diff diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index a427d7cd40fcdd..62462c3202bee0 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 9860b25f2e7fa6..2141f6b2ad1d41 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -502,6 +502,8 @@ clang-format - Adds ``BreakBinaryOperations`` option. +- Adds ``AlignAfterOpenBracketBreak`` sub-options for better control of + ``AlignAfterOpenBracket`` with ``AlwaysBreak`` or ``BlockIndent`` modes. libclang diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index d8b62c7652a0f6..6e173d6d2b459f 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b)
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/gedare created https://github.com/llvm/llvm-project/pull/108332 Introduce sub-options for `AlignAfterOpenBracketBreak` to allow for control of `AlignAfterOpenBracket` with `AlwaysBreak` and `BlockIndent` selectively for `if` conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738 Fixes #79176 Fixes #80123 >From d7f91961dc2fba5d441d2e6b4f04042d4cbd01ed Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/2] Format: add AlignAfterOpenBracketOptions Introduce new options to allow for control of AlwaysBreak and BlockIndent selectively for If conditional statements (as currently supported), other conditional statements (for/while/switch), and other statements. Fixes #67738. Fixes #79176. Fixes #80123. --- clang/docs/ClangFormatStyleOptions.rst | 60 + clang/include/clang/Format/Format.h| 73 +++ clang/lib/Format/ContinuationIndenter.cpp | 41 -- clang/lib/Format/Format.cpp| 26 clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 12 ++ clang/unittests/Format/FormatTest.cpp | 143 + 7 files changed, 353 insertions(+), 10 deletions(-) diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index a427d7cd40fcdd..62462c3202bee0 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -246,6 +246,66 @@ the configuration (without a prefix: ``Auto``). +.. _AlignAfterOpenBracketBreak: + +**AlignAfterOpenBracketBreak** (``AlignAfterOpenBracketCustom``) :versionbadge:`clang-format 20` :ref:`¶ ` + Control of when ``AlignAfterOpenBracket`` breaks an opening bracket. + + If ``AlignAfterOpenBracket`` is set to ``AlwaysBreak`` or ``BlockIndent``, + use this to specify how different cases of breaking the opening brackets + should be handled. Otherwise, this is ignored. Setting any of these to + ``false`` will cause them to not break. At least one of these must be set + to ``true``, otherwise a default (backward compatible) breaking behavior + is used. This is ignored for ``Align`` and ``DontAlign``. + + .. code-block:: c++ + +# Example of usage: +AlignAfterOpenBracket: AlwaysBreak +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + Nested configuration flags: + + Precise control over breaking the opening bracket of + ``AlignAfterOpenBracket``. + + .. code-block:: c++ + +# Should be declared this way: +AlignAfterOpenBracketBreak: + InIfConditionalStatements: true + InOtherConditionalStatements: false + Other: true + + * ``bool InIfConditionalStatements`` Break inside if/else if statements. + +.. code-block:: c++ + + true: false: + if constexpr ( vs. if constexpr (a || + a || b) b) + + * ``bool InOtherConditionalStatements`` Break inside conditional statements not covered by preceding options. +(``for/while/switch...``). + +.. code-block:: c++ + + true: false: + while ( vs. while (a && + a && b ) { b) { + + * ``bool Other`` Break inside brackets not covered by preceding options. + +.. code-block:: c++ + + true: false: + someLongFunction(vs. someLongFunction(argument1, + argument1, argument2); argument2); + + .. _AlignArrayOfStructures: **AlignArrayOfStructures** (``ArrayInitializerAlignmentStyle``) :versionbadge:`clang-format 13` :ref:`¶ ` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index d8b62c7652a0f6..6e173d6d2b459f 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -106,6 +106,78 @@ struct FormatStyle { /// \version 3.8 BracketAlignmentStyle AlignAfterOpenBracket; + /// Precise control over breaking the opening bracket of + /// ``AlignAfterOpenBracket``. + /// \code + /// # Should be declared this way: + /// AlignAfterOpenBracketBreak: + /// InIfConditionalStatements: true + /// InOtherConditionalStatements: false + /// Other: true + /// \endcode + struct AlignAfterOpenBracketCustom { +/// Break inside if/else if statements. +/// \code +///true: false: +///if constexpr ( vs. if constexpr (a || +/// a || b) b) +/// \endcode +bool InIfConditionalStatements; +/// Break inside conditional statements not covered by preceding