[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-19 Thread Björn Schäpers via cfe-commits

https://github.com/HazardyKnusperkeks approved this pull request.


https://github.com/llvm/llvm-project/pull/77699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-20 Thread Owen Pan via cfe-commits


@@ -768,15 +768,25 @@ 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.
+  const auto IsOpeningBracket = [&](const FormatToken &Tok) {
+const auto IsStartOfBracedList = [&]() {

owenca wrote:

```suggestion
  auto IsOpeningBracket = [&](const FormatToken &Tok) {
auto IsStartOfBracedList = [&]() {
```

https://github.com/llvm/llvm-project/pull/77699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-20 Thread Owen Pan via cfe-commits


@@ -768,15 +768,25 @@ 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.
+  const auto IsOpeningBracket = [&](const FormatToken &Tok) {
+const auto IsStartOfBracedList = [&]() {
+  return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
+ Style.Cpp11BracedListStyle;
+};
+if (Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) ||
+IsStartOfBracedList()) {
+  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);
+}
+return false;

owenca wrote:

```suggestion
if (!Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) &&
!IsStartOfBracedList()) {
  return false;
}
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);
```

https://github.com/llvm/llvm-project/pull/77699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-21 Thread Gedare Bloom via cfe-commits

https://github.com/gedare updated 
https://github.com/llvm/llvm-project/pull/77699

>From ff055d9c064d1fb359d59eeb47cb5f8c6c422bec Mon Sep 17 00:00:00 2001
From: Gedare Bloom 
Date: Fri, 7 Jul 2023 17:28:47 -0600
Subject: [PATCH 1/5] Fix formatting of if statements with BlockIndent

A bug with BlockIndent prevents line breaks within if (and else if) clauses.
While fixing this bug, it appears that AlignAfterOpenBracket is not designed
to work with loop and if statements, but AlwaysBreak works on if clauses.
The documentation and tests are not clear on whether or not this is intended.
This patch preserves the AlwaysBreak behavior and supports BlockIndent on if
clauses while fixing the bug.

It may be reasonable to go the other way and create an explicit option for
alignment of if (and loop) clauses intentionally.

Fixes #54663.

Differential Revision: https://reviews.llvm.org/D154755
---
 clang/lib/Format/ContinuationIndenter.cpp |  6 ++--
 clang/unittests/Format/FormatTest.cpp | 40 ---
 2 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index 102504182c4505b..8b288c62473db9f 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -775,8 +775,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState 
&State, bool DryRun,
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
   (!Previous.Previous ||
-   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
-   tok::kw_switch)) &&
+   !(Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+tok::kw_switch) ||
+ (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+  Previous.Previous->isIf( &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 25ef5c680af862b..8d55e62e2558d3d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25889,8 +25889,8 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
@@ -25903,12 +25903,44 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (camelCaseName < alsoLongName ||\n"
+   "  anotherEvenLongerName <=\n"
+   "  thisReallyReallyReallyReallyReallyReallyLongerName 
||"
+   "\n"
+   "  otherName < thisLastName) {\n"
+   "return;\n"
+   "  } else if (quiteLongName < alsoLongName ||\n"
+   " anotherEvenLongerName <=\n"
+   " 
thisReallyReallyReallyReallyReallyReallyLonger"
+   "Name ||\n"
+   " otherName < thisLastName) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "if (false) {\n"
+   "  return;\n"
+   "} else if (thisIsRatherALongIfClause && "
+   "thatIExpectToBeBroken ||\n"
+   "   ontoMultipleLines && whenFormattedCorrectly) 
{\n"
+   "  return;\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {

>From 55cfce78dbfc87af455e6bf822b8f0a6a5f9f3d6 Mon Sep 17 00:00:00 2001
From: Gedare Bloom 
Date: Wed, 10 Jan 2024 14:44:41 -0700
Subject: [PATCH 2/5] Refactor complex conditional logic as a lambda

---
 clang/lib/Format/ContinuationIndenter.cpp | 25 +++
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --gi

[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-21 Thread Owen Pan via cfe-commits

https://github.com/owenca approved this pull request.


https://github.com/llvm/llvm-project/pull/77699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-22 Thread Owen Pan via cfe-commits

https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/77699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-10 Thread Gedare Bloom via cfe-commits

https://github.com/gedare created 
https://github.com/llvm/llvm-project/pull/77699

A bug with BlockIndent prevents line breaks within if (and else if) clauses.

While fixing this bug, it appears that AlignAfterOpenBracket is not designed to 
work with loop and if statements, but AlwaysBreak works on if clauses. The 
documentation and tests are not clear on whether or not this behavior is 
intended.

This PR preserves the `AlwaysBreak` behavior on `if` clauses without supporting 
`BlockIndent` on `if` clauses  to avoid regressions while fixing the bug.

It may be reasonable to create an explicit option for alignment of if (and 
loop) clauses intentionally for both `AlwaysBreak` and `BlockIndent`

Fixes #54663.

Migrated from Differential Revision: https://reviews.llvm.org/D154755
See more discussion there. Addressed last open comment from the rev about 
refactoring the complex conditional logic involved with the 
`AlignAfterOpenBracket` line break behavior.

>From ff055d9c064d1fb359d59eeb47cb5f8c6c422bec Mon Sep 17 00:00:00 2001
From: Gedare Bloom 
Date: Fri, 7 Jul 2023 17:28:47 -0600
Subject: [PATCH 1/2] Fix formatting of if statements with BlockIndent

A bug with BlockIndent prevents line breaks within if (and else if) clauses.
While fixing this bug, it appears that AlignAfterOpenBracket is not designed
to work with loop and if statements, but AlwaysBreak works on if clauses.
The documentation and tests are not clear on whether or not this is intended.
This patch preserves the AlwaysBreak behavior and supports BlockIndent on if
clauses while fixing the bug.

It may be reasonable to go the other way and create an explicit option for
alignment of if (and loop) clauses intentionally.

Fixes #54663.

Differential Revision: https://reviews.llvm.org/D154755
---
 clang/lib/Format/ContinuationIndenter.cpp |  6 ++--
 clang/unittests/Format/FormatTest.cpp | 40 ---
 2 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index 102504182c4505..8b288c62473db9 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -775,8 +775,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState 
&State, bool DryRun,
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
   (!Previous.Previous ||
-   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
-   tok::kw_switch)) &&
+   !(Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+tok::kw_switch) ||
+ (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+  Previous.Previous->isIf( &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 25ef5c680af862..8d55e62e2558d3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25889,8 +25889,8 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
@@ -25903,12 +25903,44 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (camelCaseName < alsoLongName ||\n"
+   "  anotherEvenLongerName <=\n"
+   "  thisReallyReallyReallyReallyReallyReallyLongerName 
||"
+   "\n"
+   "  otherName < thisLastName) {\n"
+   "return;\n"
+   "  } else if (quiteLongName < alsoLongName ||\n"
+   " anotherEvenLongerName <=\n"
+   " 
thisReallyReallyReallyReallyReallyReallyLonger"
+   "Name ||\n"
+   " otherName < thisLastName) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (ThisIsRatherALongIfClause && thatIExpectToBeB

[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-10 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-format

Author: Gedare Bloom (gedare)


Changes

A bug with BlockIndent prevents line breaks within if (and else if) clauses.

While fixing this bug, it appears that AlignAfterOpenBracket is not designed to 
work with loop and if statements, but AlwaysBreak works on if clauses. The 
documentation and tests are not clear on whether or not this behavior is 
intended.

This PR preserves the `AlwaysBreak` behavior on `if` clauses without supporting 
`BlockIndent` on `if` clauses  to avoid regressions while fixing the bug.

It may be reasonable to create an explicit option for alignment of if (and 
loop) clauses intentionally for both `AlwaysBreak` and `BlockIndent`

Fixes #54663.

Migrated from Differential Revision: https://reviews.llvm.org/D154755
See more discussion there. Addressed last open comment from the rev about 
refactoring the complex conditional logic involved with the 
`AlignAfterOpenBracket` line break behavior.

---
Full diff: https://github.com/llvm/llvm-project/pull/77699.diff


2 Files Affected:

- (modified) clang/lib/Format/ContinuationIndenter.cpp (+16-7) 
- (modified) clang/unittests/Format/FormatTest.cpp (+36-4) 


``diff
diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index 102504182c4505..29426c225fec9e 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -768,15 +768,24 @@ 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 isOpeningBracket = [&](const FormatToken &Tok) {
+auto isStartOfBracedList = [&](const FormatToken &Tok) {
+  return Tok.is(tok::l_brace) && Tok.isNot(BK_Block) &&
+ Style.Cpp11BracedListStyle;
+};
+if (Tok.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) ||
+isStartOfBracedList(Tok)) {
+  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);
+}
+  };
   if ((Style.AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) &&
-  (Previous.isOneOf(tok::l_paren, TT_TemplateOpener, tok::l_square) ||
-   (Previous.is(tok::l_brace) && Previous.isNot(BK_Block) &&
-Style.Cpp11BracedListStyle)) &&
-  State.Column > getNewLineColumn(State) &&
-  (!Previous.Previous ||
-   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
-   tok::kw_switch)) &&
+  isOpeningBracket(Previous) && State.Column > getNewLineColumn(State) &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 25ef5c680af862..8d55e62e2558d3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25889,8 +25889,8 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
@@ -25903,12 +25903,44 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (camelCaseName < alsoLongName ||\n"
+   "  anotherEvenLongerName <=\n"
+   "  thisReallyReallyReallyReallyReallyReallyLongerName 
||"
+   "\n"
+   "  otherName < thisLastName) {\n"
+   "return;\n"
+   "  } else if (quiteLongName < alsoLongName ||\n"
+   " anotherEvenLongerName <=\n"
+   " 
thisReallyReallyReallyReallyReallyReallyLonger"
+   "Name ||\n"
+   " otherName < thisLastName) {\n"
+   "return;\n"
+   "  }\n"
+  

[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-11 Thread Gedare Bloom via cfe-commits

https://github.com/gedare updated 
https://github.com/llvm/llvm-project/pull/77699

>From ff055d9c064d1fb359d59eeb47cb5f8c6c422bec Mon Sep 17 00:00:00 2001
From: Gedare Bloom 
Date: Fri, 7 Jul 2023 17:28:47 -0600
Subject: [PATCH 1/3] Fix formatting of if statements with BlockIndent

A bug with BlockIndent prevents line breaks within if (and else if) clauses.
While fixing this bug, it appears that AlignAfterOpenBracket is not designed
to work with loop and if statements, but AlwaysBreak works on if clauses.
The documentation and tests are not clear on whether or not this is intended.
This patch preserves the AlwaysBreak behavior and supports BlockIndent on if
clauses while fixing the bug.

It may be reasonable to go the other way and create an explicit option for
alignment of if (and loop) clauses intentionally.

Fixes #54663.

Differential Revision: https://reviews.llvm.org/D154755
---
 clang/lib/Format/ContinuationIndenter.cpp |  6 ++--
 clang/unittests/Format/FormatTest.cpp | 40 ---
 2 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index 102504182c4505..8b288c62473db9 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -775,8 +775,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState 
&State, bool DryRun,
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
   (!Previous.Previous ||
-   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
-   tok::kw_switch)) &&
+   !(Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+tok::kw_switch) ||
+ (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+  Previous.Previous->isIf( &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 25ef5c680af862..8d55e62e2558d3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25889,8 +25889,8 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
@@ -25903,12 +25903,44 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (camelCaseName < alsoLongName ||\n"
+   "  anotherEvenLongerName <=\n"
+   "  thisReallyReallyReallyReallyReallyReallyLongerName 
||"
+   "\n"
+   "  otherName < thisLastName) {\n"
+   "return;\n"
+   "  } else if (quiteLongName < alsoLongName ||\n"
+   " anotherEvenLongerName <=\n"
+   " 
thisReallyReallyReallyReallyReallyReallyLonger"
+   "Name ||\n"
+   " otherName < thisLastName) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "if (false) {\n"
+   "  return;\n"
+   "} else if (thisIsRatherALongIfClause && "
+   "thatIExpectToBeBroken ||\n"
+   "   ontoMultipleLines && whenFormattedCorrectly) 
{\n"
+   "  return;\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {

>From 55cfce78dbfc87af455e6bf822b8f0a6a5f9f3d6 Mon Sep 17 00:00:00 2001
From: Gedare Bloom 
Date: Wed, 10 Jan 2024 14:44:41 -0700
Subject: [PATCH 2/3] Refactor complex conditional logic as a lambda

---
 clang/lib/Format/ContinuationIndenter.cpp | 25 +++
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/

[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-11 Thread Björn Schäpers via cfe-commits


@@ -768,15 +768,25 @@ 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 isOpeningBracket = [&](const FormatToken &Tok) {
+auto isStartOfBracedList = [&](const FormatToken &Tok) {

HazardyKnusperkeks wrote:

```suggestion
  auto IsOpeningBracket = [&](const FormatToken &Tok) {
auto IsStartOfBracedList = [&](const FormatToken &Tok) {
```

https://github.com/llvm/llvm-project/pull/77699
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format]: Fix formatting of if statements with BlockIndent (PR #77699)

2024-01-11 Thread Gedare Bloom via cfe-commits

https://github.com/gedare updated 
https://github.com/llvm/llvm-project/pull/77699

>From ff055d9c064d1fb359d59eeb47cb5f8c6c422bec Mon Sep 17 00:00:00 2001
From: Gedare Bloom 
Date: Fri, 7 Jul 2023 17:28:47 -0600
Subject: [PATCH 1/4] Fix formatting of if statements with BlockIndent

A bug with BlockIndent prevents line breaks within if (and else if) clauses.
While fixing this bug, it appears that AlignAfterOpenBracket is not designed
to work with loop and if statements, but AlwaysBreak works on if clauses.
The documentation and tests are not clear on whether or not this is intended.
This patch preserves the AlwaysBreak behavior and supports BlockIndent on if
clauses while fixing the bug.

It may be reasonable to go the other way and create an explicit option for
alignment of if (and loop) clauses intentionally.

Fixes #54663.

Differential Revision: https://reviews.llvm.org/D154755
---
 clang/lib/Format/ContinuationIndenter.cpp |  6 ++--
 clang/unittests/Format/FormatTest.cpp | 40 ---
 2 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index 102504182c4505..8b288c62473db9 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -775,8 +775,10 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState 
&State, bool DryRun,
 Style.Cpp11BracedListStyle)) &&
   State.Column > getNewLineColumn(State) &&
   (!Previous.Previous ||
-   !Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
-   tok::kw_switch)) &&
+   !(Previous.Previous->isOneOf(TT_CastRParen, tok::kw_for, tok::kw_while,
+tok::kw_switch) ||
+ (Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent &&
+  Previous.Previous->isIf( &&
   // Don't do this for simple (no expressions) one-argument function calls
   // as that feels like needlessly wasting whitespace, e.g.:
   //
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 25ef5c680af862..8d55e62e2558d3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25889,8 +25889,8 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
@@ -25903,12 +25903,44 @@ TEST_F(FormatTest, 
AlignAfterOpenBracketBlockIndentIfStatement) {
"}",
Style);
 
-  verifyFormat("if (quitelongarg !=\n"
-   "(alsolongarg - 1)) { // ABC is a very long "
+  verifyFormat("if (quiteLongArg !=\n"
+   "(alsoLongArg - 1)) { // ABC is a very long "
"comment\n"
"  return;\n"
"}",
Style);
+
+  verifyFormat("void foo() {\n"
+   "  if (camelCaseName < alsoLongName ||\n"
+   "  anotherEvenLongerName <=\n"
+   "  thisReallyReallyReallyReallyReallyReallyLongerName 
||"
+   "\n"
+   "  otherName < thisLastName) {\n"
+   "return;\n"
+   "  } else if (quiteLongName < alsoLongName ||\n"
+   " anotherEvenLongerName <=\n"
+   " 
thisReallyReallyReallyReallyReallyReallyLonger"
+   "Name ||\n"
+   " otherName < thisLastName) {\n"
+   "return;\n"
+   "  }\n"
+   "}",
+   Style);
+
+  Style.ContinuationIndentWidth = 2;
+  verifyFormat("void foo() {\n"
+   "  if (ThisIsRatherALongIfClause && thatIExpectToBeBroken ||\n"
+   "  ontoMultipleLines && whenFormattedCorrectly) {\n"
+   "if (false) {\n"
+   "  return;\n"
+   "} else if (thisIsRatherALongIfClause && "
+   "thatIExpectToBeBroken ||\n"
+   "   ontoMultipleLines && whenFormattedCorrectly) 
{\n"
+   "  return;\n"
+   "}\n"
+   "  }\n"
+   "}",
+   Style);
 }
 
 TEST_F(FormatTest, AlignAfterOpenBracketBlockIndentForStatement) {

>From 55cfce78dbfc87af455e6bf822b8f0a6a5f9f3d6 Mon Sep 17 00:00:00 2001
From: Gedare Bloom 
Date: Wed, 10 Jan 2024 14:44:41 -0700
Subject: [PATCH 2/4] Refactor complex conditional logic as a lambda

---
 clang/lib/Format/ContinuationIndenter.cpp | 25 +++
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/