[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)
@@ -3373,6 +3409,51 @@ the configuration (without a prefix: ``Auto``). +.. _BreakBeforeCloseBracketIf: + +**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ ` + 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 || gedare wrote: Yes, `BreakAfterCloseBracket` will only work if there is a break after the opening bracket. This is a holdover from how `AlwaysBreak` and `BlockIndent` alignment options work. 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)
@@ -3373,6 +3409,51 @@ the configuration (without a prefix: ``Auto``). +.. _BreakBeforeCloseBracketIf: + +**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ ` + 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 || + a || b b) +) + +.. _BreakBeforeCloseBracketLoop: + +**BreakBeforeCloseBracketLoop** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ ` + 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 && gedare wrote: as above. If the formatter decides to break after the `(` for any reason (e.g., because of `BreakAfterOpenBracketLoop` or because the first token exceeds the `ColumnLimit`), the `BreakBeforeCloseBracketLoop` will be in effect for the closing `)`. 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 5972376f719665225b04bf121cda6c769e3392d9 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 01/11] Format: add AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 69 +++-- clang/lib/Format/Format.cpp| 13 + clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 298 + 6 files changed, 391 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 3ac4318824ac0..2b2bcb6764e9b 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5305,6 +5321,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignAfterOpenBracket == R.AlignAfterOpenBracket && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 4e4e48f90a89f..f91da11cd2f44 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,36 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || + (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +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; P
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -3373,6 +3409,51 @@ the configuration (without a prefix: ``Auto``). +.. _BreakBeforeCloseBracketIf: + +**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ ` + 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 || gedare wrote: I probably need to fix the `false` case though, as disabling the option will not affect the break after the opener. 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 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/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From 5972376f719665225b04bf121cda6c769e3392d9 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/9] Format: add AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 69 +++-- clang/lib/Format/Format.cpp| 13 + clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 298 + 6 files changed, 391 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 3ac4318824ac0..2b2bcb6764e9b 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5305,6 +5321,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignAfterOpenBracket == R.AlignAfterOpenBracket && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 4e4e48f90a89f..f91da11cd2f44 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,36 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || + (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +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; Pre
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
https://github.com/mydeveloperday commented: your BreakBeforeCloseBracket true case seem to imply BreakAfterOpenBracket is that what you expect? 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)
@@ -3373,6 +3409,51 @@ the configuration (without a prefix: ``Auto``). +.. _BreakBeforeCloseBracketIf: + +**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ ` + 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 || + a || b b) +) + +.. _BreakBeforeCloseBracketLoop: + +**BreakBeforeCloseBracketLoop** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ ` + 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 && mydeveloperday wrote: again is this correct why not ```c++ while( a && b ) ``` 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)
@@ -3373,6 +3409,51 @@ the configuration (without a prefix: ``Auto``). +.. _BreakBeforeCloseBracketIf: + +**BreakBeforeCloseBracketIf** (``Boolean``) :versionbadge:`clang-format 21` :ref:`¶ ` + 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 || mydeveloperday wrote: is the true case correct 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)
https://github.com/mydeveloperday 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/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From 5972376f719665225b04bf121cda6c769e3392d9 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/8] Format: add AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 69 +++-- clang/lib/Format/Format.cpp| 13 + clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 298 + 6 files changed, 391 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 3ac4318824ac0..2b2bcb6764e9b 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5305,6 +5321,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignAfterOpenBracket == R.AlignAfterOpenBracket && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 4e4e48f90a89f..f91da11cd2f44 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,36 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || + (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +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; Pre
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -9694,6 +9694,304 @@ TEST_F(FormatTest, ParenthesesAndOperandAlignment) { Style); } +TEST_F(FormatTest, AlignAfterConditionalStatements) { gedare wrote: Yes, it might be a good idea to consolidate all of the alignment tests in a new file. I'll plan to do that in my next iteration on this work, depending on if the direction is good. 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: Take 3. This is now a simpler solution that separates rules for breaking within `if`, loop, and `switch` control statements. These explicit style options override the `AlwaysBreak` and `BlockIndent` options, which should be now only effective for function-like parentheses and braced initializer lists. IF this is the right direction to go, I can proceed to make function-like breaking and braced initializer list breaking also be explicit separate style options, and we can deprecate the `AlwaysBreak` and `BlockIndent` options as I think they would be entirely superseded by these finer-grained options. 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 5972376f719665225b04bf121cda6c769e3392d9 Mon Sep 17 00:00:00 2001 From: Gedare Bloom Date: Thu, 20 Jun 2024 17:35:39 -0600 Subject: [PATCH 1/6] Format: add AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 69 +++-- clang/lib/Format/Format.cpp| 13 + clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 298 + 6 files changed, 391 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 3ac4318824ac0..2b2bcb6764e9b 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5305,6 +5321,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignAfterOpenBracket == R.AlignAfterOpenBracket && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 4e4e48f90a89f..f91da11cd2f44 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,36 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || + (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +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; Pre
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
gedare 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. > > > > > > if I understand you correctly, you would like a new style option added for > > setting break option for all control statements in the same way? > > Yes. I guess this approach is in fact not desirable. Fixing these bugs is wedged by the behavior of `AlwaysBreak` and `BlockIndent` because they break after the opening parens of `if` statements, but not on other control statements (`for/while`, `switch`). I suspect this limitation was not clearly stated ahead of time, but makes it quite hard to provide backward compatibility without complexity. My first solution with sub-options for `AlwaysBreak` and `BlockIndent` handles the backward compatibility in a simpler way than the current solution. I do not know how to proceed. There are competing statements about what could be done. I can't see a simple fix that can provide backward compatibility and also satisfy the three referenced Issues. Proceeding in the current direction the only good solution that I see is to create multiple `BreakAfter` variants or sub-options to cover `BreakAfterIf` `BreakAfterLoop` `BreakAfterSwitch`. Whether each option should handle both breaking behavior and alignment behavior is also an unknown to me. I felt it was preferable to let `AlignAfteropenBracket` handle the alignment. This direction allows us to eventually deprecate `AlwaysBreak` and `BlockIndent` (by introducing a `BreakBeforeClosingBracket` option, for example). This is more or less in line with https://github.com/llvm/llvm-project/issues/80049 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: > Why would `AlignAfterControlStatement` have anything to do with > `AlignAfterOpenBracket`, which is for arguments of function/macro calls? > Unfortunately, `AlignAfterOpenBracket` is not just for arguments of function/macro calls. It affects parentheses of control statements already, at least in some regard: https://github.com/llvm/llvm-project/pull/77699 We could certainly make the distinction between breaking and alignment more explicit, as mentioned at https://github.com/llvm/llvm-project/issues/80049 That's really what this PR is doing, it is adding an option to break the start of the control expression. The alignment gets inherited from the `AlignAfterOpenBracket`. I guess another possiblity is to call this `AlignAfterControlStatement` and make the `AlignAfterOpenBracket` really only for functions/macros. 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/owenca commented: You need to run `ninja clang-format-style` to update the documentation. 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)
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. > > > > > > > > > if I understand you correctly, you would like a new style option added > > > for setting break option for all control statements in the same way? > > > > > > Yes. > > This is basically re-written as requested. The Alignment is determined by the > value of AlignAfterOpenBracket, except that because there is a forced break > with `MultiLine` the alignment will happen at the Continuation Indent. So > using the new option with `MultiLine` and `AlignAfterOpenBracket.Align` is > equivalent to using `AlignAfterOpenBracket.AlwaysBreak` with `MultiLine`. Why would `AlignAfterControlStatement` have anything to do with `AlignAfterOpenBracket`, which is for arguments of function/macro calls? I'm not sure if we should add this option if it's too complicated to understand. WDYT @mydeveloperday? 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)
@@ -9694,6 +9694,304 @@ TEST_F(FormatTest, ParenthesesAndOperandAlignment) { Style); } +TEST_F(FormatTest, AlignAfterConditionalStatements) { owenca wrote: ```suggestion TEST_F(FormatTest, AlignAfterControlStatements) { ``` Can you move this to a new file if all these new test cases are necessary and can't be shortened? 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)
@@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, owenca wrote: Code examples? 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/owenca 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/gedare updated https://github.com/llvm/llvm-project/pull/108332 >From b2cbc7730aca7c75679c070292cfd07cf55854af 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 AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 69 +++-- clang/lib/Format/Format.cpp| 13 + clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 298 + 6 files changed, 391 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 7fe41d800ccb3..45dc2ac40490e 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5303,6 +5319,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignAfterOpenBracket == R.AlignAfterOpenBracket && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 55e1d1ceb55b7..f4e48b7e37d54 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,36 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || + (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +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; Pre
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
owenca wrote: > When I updated, my build target for `FormatTests` stopped working. > > ``` > ninja -C build clang-format FormatTests > ninja: Entering directory `build' > ninja: error: unknown target 'FormatTests' > ``` > > I'm trying to figure out what changed or how to modify my workflow. I think I > ran this PR with stale tests. See https://github.com/llvm/llvm-project/pull/134196#issuecomment-2843956595. 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: When I updated, my build target for `FormatTests` stopped working. ``` ninja -C build clang-format FormatTests ninja: Entering directory `build' ninja: error: unknown target 'FormatTests' ``` I'm trying to figure out what changed or how to modify my workflow. I think I ran this PR with stale tests. 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)
@@ -5283,6 +5299,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignAfterOpenBracket == R.AlignAfterOpenBracket && + AlignAfterControlStatement == R.AlignAfterControlStatement && gedare wrote: done, good catch, missed this when I changed the option name. 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 8846ff045f969b258554c3cfb72161e9f61dda19 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 AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 69 +++-- clang/lib/Format/Format.cpp| 13 + clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 298 + 6 files changed, 391 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index f6ceef08b46da..274a9e4ee272a 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5283,6 +5299,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignAfterOpenBracket == R.AlignAfterOpenBracket && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && AlignConsecutiveBitFields == R.AlignConsecutiveBitFields && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 55e1d1ceb55b7..f4e48b7e37d54 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,36 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || + (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +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.isJavaScrip
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
@@ -5283,6 +5299,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignAfterOpenBracket == R.AlignAfterOpenBracket && + AlignAfterControlStatement == R.AlignAfterControlStatement && HazardyKnusperkeks wrote: Could you move this one line up? :) 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 8846ff045f969b258554c3cfb72161e9f61dda19 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 AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 69 +++-- clang/lib/Format/Format.cpp| 13 + clang/lib/Format/TokenAnnotator.cpp| 8 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 298 + 6 files changed, 391 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index f6ceef08b46da..274a9e4ee272a 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5283,6 +5299,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignAfterOpenBracket == R.AlignAfterOpenBracket && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && AlignConsecutiveBitFields == R.AlignConsecutiveBitFields && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 55e1d1ceb55b7..f4e48b7e37d54 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,36 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || + (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +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.isJavaScrip
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff HEAD~1 HEAD --extensions cpp,h -- clang/include/clang/Format/Format.h clang/lib/Format/ContinuationIndenter.cpp clang/lib/Format/Format.cpp clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/ConfigParseTest.cpp clang/unittests/Format/FormatTest.cpp `` View the diff from clang-format here. ``diff diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 08558d655..f4e48b7e3 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -834,16 +834,15 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, /* For backward compatibility, use AlignAfterOpenBracket * in case AlignAfterControlStatement is not initialized */ return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || -(Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && - Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); + (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); } -if (IsOtherConditional(*Tok.Previous)) { +if (IsOtherConditional(*Tok.Previous)) return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; -} if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak || - Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) { +Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) { return !Tok.Previous->is(TT_CastRParen) && - !(Style.isJavaScript() && Tok.is(Keywords.kw_await)); + !(Style.isJavaScript() && Tok.is(Keywords.kw_await)); } return false; }; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e6a82a093..cd7e4cb06 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -202,8 +202,10 @@ template <> struct MappingTraits { } }; -template <> struct ScalarEnumerationTraits { - static void enumeration(IO &IO, FormatStyle::BreakAfterControlStatementStyle &Value) { +template <> +struct ScalarEnumerationTraits { + static void enumeration(IO &IO, + FormatStyle::BreakAfterControlStatementStyle &Value) { IO.enumCase(Value, "Default", FormatStyle::BACSS_Default); IO.enumCase(Value, "MultiLine", FormatStyle::BACSS_MultiLine); IO.enumCase(Value, "No", FormatStyle::BACSS_No); diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 39ddb63d4..881d40abf 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -6221,7 +6221,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, const FormatToken *Previous = Right.MatchingParen->Previous; if (!Previous) return true; -if (Previous->isIf() || Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch)) { +if (Previous->isIf() || +Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch)) { return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; } return true; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 7841caefd..a314a0bff 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -9797,7 +9797,6 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) { "}", Style); - Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine; `` 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 64000ad7d2310ac916b37ed808997bfcb6b9f324 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 AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 70 +++-- clang/lib/Format/Format.cpp| 11 + clang/lib/Format/TokenAnnotator.cpp| 7 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 299 + 6 files changed, 390 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index f6ceef08b46da..274a9e4ee272a 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5283,6 +5299,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignAfterOpenBracket == R.AlignAfterOpenBracket && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && AlignConsecutiveBitFields == R.AlignConsecutiveBitFields && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 55e1d1ceb55b7..08558d6550832 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,37 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || +(Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) { + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +} +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())
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff HEAD~1 HEAD --extensions h,cpp -- clang/include/clang/Format/Format.h clang/lib/Format/ContinuationIndenter.cpp clang/lib/Format/Format.cpp clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/ConfigParseTest.cpp clang/unittests/Format/FormatTest.cpp `` View the diff from clang-format here. ``diff diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 08558d655..f4e48b7e3 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -834,16 +834,15 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, /* For backward compatibility, use AlignAfterOpenBracket * in case AlignAfterControlStatement is not initialized */ return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || -(Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && - Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); + (Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); } -if (IsOtherConditional(*Tok.Previous)) { +if (IsOtherConditional(*Tok.Previous)) return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; -} if (Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak || - Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) { +Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) { return !Tok.Previous->is(TT_CastRParen) && - !(Style.isJavaScript() && Tok.is(Keywords.kw_await)); + !(Style.isJavaScript() && Tok.is(Keywords.kw_await)); } return false; }; diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index e6a82a093..cd7e4cb06 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -202,8 +202,10 @@ template <> struct MappingTraits { } }; -template <> struct ScalarEnumerationTraits { - static void enumeration(IO &IO, FormatStyle::BreakAfterControlStatementStyle &Value) { +template <> +struct ScalarEnumerationTraits { + static void enumeration(IO &IO, + FormatStyle::BreakAfterControlStatementStyle &Value) { IO.enumCase(Value, "Default", FormatStyle::BACSS_Default); IO.enumCase(Value, "MultiLine", FormatStyle::BACSS_MultiLine); IO.enumCase(Value, "No", FormatStyle::BACSS_No); diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 39ddb63d4..881d40abf 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -6221,7 +6221,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, const FormatToken *Previous = Right.MatchingParen->Previous; if (!Previous) return true; -if (Previous->isIf() || Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch)) { +if (Previous->isIf() || +Previous->isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch)) { return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; } return true; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 7841caefd..a314a0bff 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -9797,7 +9797,6 @@ TEST_F(FormatTest, AlignAfterConditionalStatements) { "}", Style); - Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; Style.AlignAfterControlStatement = FormatStyle::BACSS_MultiLine; `` 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 64000ad7d2310ac916b37ed808997bfcb6b9f324 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 AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 70 +++-- clang/lib/Format/Format.cpp| 11 + clang/lib/Format/TokenAnnotator.cpp| 7 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 299 + 6 files changed, 390 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index f6ceef08b46da..274a9e4ee272a 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5283,6 +5299,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignAfterOpenBracket == R.AlignAfterOpenBracket && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && AlignConsecutiveBitFields == R.AlignConsecutiveBitFields && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 55e1d1ceb55b7..08558d6550832 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,37 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || +(Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) { + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +} +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())
[clang] [clang-format] Add AlignAfterOpenBracketOptions (PR #108332)
gedare 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. > > > > > > if I understand you correctly, you would like a new style option added for > > setting break option for all control statements in the same way? > > Yes. This is basically re-written as requested. The Alignment is determined by the value of AlignAfterOpenBracket, except that because there is a forced break with `MultiLine` the alignment will happen at the Continuation Indent. So using the new option with `MultiLine` and `AlignAfterOpenBracket.Align` is equivalent to using `AlignAfterOpenBracket.AlwaysBreak` with `MultiLine`. 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 64000ad7d2310ac916b37ed808997bfcb6b9f324 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 AlignAfterControlStatement Introduce new style option to allow overriding the breaking after the opening parenthesis for control statements (if/for/while/switch). Fixes #67738. Fixes #79176. Fixes #80123. --- clang/include/clang/Format/Format.h| 17 ++ clang/lib/Format/ContinuationIndenter.cpp | 70 +++-- clang/lib/Format/Format.cpp| 11 + clang/lib/Format/TokenAnnotator.cpp| 7 +- clang/unittests/Format/ConfigParseTest.cpp | 8 + clang/unittests/Format/FormatTest.cpp | 299 + 6 files changed, 390 insertions(+), 22 deletions(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index f6ceef08b46da..274a9e4ee272a 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -62,6 +62,22 @@ struct FormatStyle { /// \version 3.3 int AccessModifierOffset; + /// Different styles for breaking the parenthesis after a control statement + /// (``if/switch/while/for ...``). + /// \version 21 + enum BreakAfterControlStatementStyle : int8_t { +/// Use the default behavior. +BACSS_Default, +/// Force break after the left parenthesis of a control statement only +/// when the expression exceeds the column limit, and align on the +/// ``ContinuationIndentWidth``. +BACSS_MultiLine, +/// Do not force a break after the control statment. +BACSS_No, + }; + + BreakAfterControlStatementStyle AlignAfterControlStatement; + /// Different styles for aligning after open brackets. enum BracketAlignmentStyle : int8_t { /// Align parameters on the open bracket, e.g.: @@ -5283,6 +5299,7 @@ struct FormatStyle { bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignAfterOpenBracket == R.AlignAfterOpenBracket && + AlignAfterControlStatement == R.AlignAfterControlStatement && AlignArrayOfStructures == R.AlignArrayOfStructures && AlignConsecutiveAssignments == R.AlignConsecutiveAssignments && AlignConsecutiveBitFields == R.AlignConsecutiveBitFields && diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 55e1d1ceb55b7..08558d6550832 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -814,6 +814,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 IsOtherConditional = [&](const FormatToken &Tok) { +return Tok.isOneOf(tok::kw_for, tok::kw_while, tok::kw_switch) || + (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) && @@ -825,26 +830,37 @@ 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)); +if (Tok.Previous->isIf()) { + /* For backward compatibility, use AlignAfterOpenBracket + * in case AlignAfterControlStatement is not initialized */ + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine || +(Style.AlignAfterControlStatement == FormatStyle::BACSS_Default && + Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak); +} +if (IsOtherConditional(*Tok.Previous)) { + return Style.AlignAfterControlStatement == FormatStyle::BACSS_MultiLine; +} +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())
[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. > > if I understand you correctly, you would like a new style option added for > setting break option for all control statements in the same way? Yes. 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 prefer that we limit this to breaking after the left parenthesis of a > control statement, with true/false or Always, Never, Multiline, BlockIndent, > etc. if I understand you correctly, you would like a new style option added for setting break option for all control statements in the same 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)
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