[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-26 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

In D114320#3154544 , @jessesna wrote:

> Thanks a lot for your time and help @MyDeveloperDay

No problem, come play some more! We need people to help and review.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-25 Thread Jesses Gott via Phabricator via cfe-commits
jessesna added a comment.

Thanks a lot for your time and help @MyDeveloperDay


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-25 Thread MyDeveloperDay via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG813d486cbc99: [clang-format] Extend 
AllowShortBlocksOnASingleLine for else blocks (authored by jessesna, committed 
by MyDeveloperDay).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -5407,6 +5407,27 @@
   EXPECT_EQ("void f() { }", format("void f() {}", Style));
   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.BeforeElse = false;
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+  verifyFormat("if (a)\n"
+   "{\n"
+   "} else if (b)\n"
+   "{\n"
+   "} else\n"
+   "{ }",
+   Style);
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
+  verifyFormat("if (a) {\n"
+   "} else if (b) {\n"
+   "} else {\n"
+   "}",
+   Style);
+  Style.BraceWrapping.BeforeElse = true;
+  verifyFormat("if (a) { }\n"
+   "else if (b) { }\n"
+   "else { }",
+   Style);
 }
 
 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -320,9 +320,9 @@
 }
 // Try to merge a control statement block with left brace wrapped
 if (I[1]->First->is(tok::l_brace) &&
-(TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
- tok::kw_switch, tok::kw_try, tok::kw_do,
- TT_ForEachMacro) ||
+(TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
+ tok::kw_for, tok::kw_switch, tok::kw_try,
+ tok::kw_do, TT_ForEachMacro) ||
  (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
   TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
 Style.BraceWrapping.AfterControlStatement ==
@@ -335,7 +335,7 @@
  ? 1
  : 0;
 } else if (I[1]->First->is(tok::l_brace) &&
-   TheLine->First->isOneOf(tok::kw_if, tok::kw_while,
+   TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
tok::kw_for)) {
   return (Style.BraceWrapping.AfterControlStatement ==
   FormatStyle::BWACS_Always)
@@ -569,7 +569,7 @@
 
 // Check that the current line allows merging. This depends on whether we
 // are in a control flow statements as well as several style flags.
-if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
+if (Line.First->is(tok::kw_case) ||
 (Line.First->Next && Line.First->Next->is(tok::kw_else)))
   return 0;
 // default: in switch statement
@@ -578,20 +578,21 @@
   if (Tok && Tok->is(tok::colon))
 return 0;
 }
-if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
-tok::kw___try, tok::kw_catch, tok::kw___finally,
-tok::kw_for, tok::r_brace, Keywords.kw___except)) {
+if (Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while, 
tok::kw_do,
+tok::kw_try, tok::kw___try, tok::kw_catch,
+tok::kw___finally, tok::kw_for, tok::r_brace,
+Keywords.kw___except)) {
   if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
 return 0;
   // Don't merge when we can't except the case when
   // the control statement block is empty
   if (!Style.AllowShortIfStatementsOnASingleLine &&
-  Line.startsWith(tok::kw_if) &&
+  Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
   !Style.BraceWrapping.AfterControlStatement &&
   !I[1]->First->is(tok::r_brace))
 return 0;
   if (!Style.AllowShortIfStatementsOnASingleLine &&
-  Line.startsWith(tok::kw_if) &&
+  Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
   Style.BraceWrapping.AfterControlStatement ==
   FormatStyle::BWACS_Always &&
   I + 2 != E && !I[2]->First->is(tok::r_brace))


Index: clang/unittests/Format/FormatTest.cpp
===
--- 

[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-25 Thread Jesses Gott via Phabricator via cfe-commits
jessesna added a comment.

Great. Thanks a lot. It's Jesses Gott and jesses.gott.na+l...@gmail.com


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-25 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

one of us can commit it for you but we need to do the following to attribute it 
to you

commit --amend --author="John Doe "

So I need your name and email address.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-25 Thread Jesses Gott via Phabricator via cfe-commits
jessesna added a comment.

In D114320#3153795 , @MyDeveloperDay 
wrote:

> Thank you for the patch, Do you need help committing this? if so we need your 
> name and email address to do so
>
> https://llvm.org/docs/DeveloperPolicy.html#commit-messages

Thanks for the reminder. I wasn't sure about where we are in the process. I 
just did a rebase, compiled and run the tests again and it's all still fine but 
this is my first contribution and i don't think i have commit access according 
to https://www.llvm.org/docs/DeveloperPolicy.html#obtaining-commit-access . I'd 
be glad if someone else could commit for me. What's the best method to send the 
email address?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-25 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

Thank you for the patch, Do you need help committing this? if so we need your 
name and email address to do so

https://llvm.org/docs/DeveloperPolicy.html#commit-messages


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-24 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay accepted this revision.
MyDeveloperDay added a comment.
This revision is now accepted and ready to land.

In D114320#3151741 , @jessesna wrote:

> In D114320#3148046 , 
> @MyDeveloperDay wrote:
>
>> Ok now I'm a little puzzled, shouldn't this be covered by 
>> `AllowShortIfStatementsOnASingleLine`?
>
> That's exactly what i thought when i first stumbled over the behavior :D But 
> I think the majority of users might not even consider adding empty else 
> blocks. Only a few either just do it or have to comply with some standards, 
> f.e. MISRA  .

I tend to agree with you.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-24 Thread Jesses Gott via Phabricator via cfe-commits
jessesna added a comment.

In D114320#3148046 , @MyDeveloperDay 
wrote:

> Ok now I'm a little puzzled, shouldn't this be covered by 
> `AllowShortIfStatementsOnASingleLine`?

That's exactly what i thought when i first stumbled over the behavior :D But I 
think the majority of users might not even consider adding empty else blocks. 
Only a few either just do it or have to comply with some standards, f.e. MISRA 
 .


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-22 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

Ok now I'm a little puzzled, shouldn't this be covered by 
`AllowShortIfStatementsOnASingleLine`?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-22 Thread Owen Pan via Phabricator via cfe-commits
owenpan accepted this revision.
owenpan added a comment.

Should we add an if-else example to the documentation?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-22 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay accepted this revision.
MyDeveloperDay added a comment.

I agree I think this LGTM


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-22 Thread Jesses Gott via Phabricator via cfe-commits
jessesna updated this revision to Diff 388961.
jessesna added a comment.

use the more dense "verifyFormat(after,Style)" calls


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -5394,6 +5394,27 @@
   EXPECT_EQ("void f() { }", format("void f() {}", Style));
   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.BeforeElse = false;
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+  verifyFormat("if (a)\n"
+   "{\n"
+   "} else if (b)\n"
+   "{\n"
+   "} else\n"
+   "{ }",
+   Style);
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
+  verifyFormat("if (a) {\n"
+   "} else if (b) {\n"
+   "} else {\n"
+   "}",
+   Style);
+  Style.BraceWrapping.BeforeElse = true;
+  verifyFormat("if (a) { }\n"
+   "else if (b) { }\n"
+   "else { }",
+   Style);
 }
 
 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -320,9 +320,9 @@
 }
 // Try to merge a control statement block with left brace wrapped
 if (I[1]->First->is(tok::l_brace) &&
-(TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
- tok::kw_switch, tok::kw_try, tok::kw_do,
- TT_ForEachMacro) ||
+(TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
+ tok::kw_for, tok::kw_switch, tok::kw_try,
+ tok::kw_do, TT_ForEachMacro) ||
  (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
   TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
 Style.BraceWrapping.AfterControlStatement ==
@@ -335,7 +335,7 @@
  ? 1
  : 0;
 } else if (I[1]->First->is(tok::l_brace) &&
-   TheLine->First->isOneOf(tok::kw_if, tok::kw_while,
+   TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
tok::kw_for)) {
   return (Style.BraceWrapping.AfterControlStatement ==
   FormatStyle::BWACS_Always)
@@ -569,7 +569,7 @@
 
 // Check that the current line allows merging. This depends on whether we
 // are in a control flow statements as well as several style flags.
-if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
+if (Line.First->is(tok::kw_case) ||
 (Line.First->Next && Line.First->Next->is(tok::kw_else)))
   return 0;
 // default: in switch statement
@@ -578,20 +578,21 @@
   if (Tok && Tok->is(tok::colon))
 return 0;
 }
-if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
-tok::kw___try, tok::kw_catch, tok::kw___finally,
-tok::kw_for, tok::r_brace, Keywords.kw___except)) {
+if (Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while, 
tok::kw_do,
+tok::kw_try, tok::kw___try, tok::kw_catch,
+tok::kw___finally, tok::kw_for, tok::r_brace,
+Keywords.kw___except)) {
   if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
 return 0;
   // Don't merge when we can't except the case when
   // the control statement block is empty
   if (!Style.AllowShortIfStatementsOnASingleLine &&
-  Line.startsWith(tok::kw_if) &&
+  Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
   !Style.BraceWrapping.AfterControlStatement &&
   !I[1]->First->is(tok::r_brace))
 return 0;
   if (!Style.AllowShortIfStatementsOnASingleLine &&
-  Line.startsWith(tok::kw_if) &&
+  Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
   Style.BraceWrapping.AfterControlStatement ==
   FormatStyle::BWACS_Always &&
   I + 2 != E && !I[2]->First->is(tok::r_brace))


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -5394,6 +5394,27 @@
   EXPECT_EQ("void f() { }", 

[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-22 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added inline comments.



Comment at: clang/unittests/Format/FormatTest.cpp:5406
+   "{ }",
+   format("if(a){}else if(b){}else{}", Style), Style);
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;

you can remove the "format(" here now..."

when its 2 arguments to `verifyFormat` its a kind of 
`verifyFormat(after,before,Style)`

alternatively you can use:

`verifyFormat(after,Style)`

it will deliberately `messUp` the the text and ensure it resets itself back to 
the desired after



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-22 Thread Jesses Gott via Phabricator via cfe-commits
jessesna updated this revision to Diff 388924.
jessesna added a comment.

use verifyFormat instead of EXPECT_EQ (as this does some extra stuff to ensure 
its stable)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -5394,6 +5394,27 @@
   EXPECT_EQ("void f() { }", format("void f() {}", Style));
   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.BeforeElse = false;
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+  verifyFormat("if (a)\n"
+   "{\n"
+   "} else if (b)\n"
+   "{\n"
+   "} else\n"
+   "{ }",
+   format("if(a){}else if(b){}else{}", Style), Style);
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
+  verifyFormat("if (a) {\n"
+   "} else if (b) {\n"
+   "} else {\n"
+   "}",
+   format("if(a){}else if(b){}else{}", Style), Style);
+  Style.BraceWrapping.BeforeElse = true;
+  verifyFormat("if (a) { }\n"
+   "else if (b) { }\n"
+   "else { }",
+   format("if(a){}else if(b){}else{}", Style), Style);
 }
 
 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -320,9 +320,9 @@
 }
 // Try to merge a control statement block with left brace wrapped
 if (I[1]->First->is(tok::l_brace) &&
-(TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
- tok::kw_switch, tok::kw_try, tok::kw_do,
- TT_ForEachMacro) ||
+(TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
+ tok::kw_for, tok::kw_switch, tok::kw_try,
+ tok::kw_do, TT_ForEachMacro) ||
  (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
   TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
 Style.BraceWrapping.AfterControlStatement ==
@@ -335,7 +335,7 @@
  ? 1
  : 0;
 } else if (I[1]->First->is(tok::l_brace) &&
-   TheLine->First->isOneOf(tok::kw_if, tok::kw_while,
+   TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
tok::kw_for)) {
   return (Style.BraceWrapping.AfterControlStatement ==
   FormatStyle::BWACS_Always)
@@ -569,7 +569,7 @@
 
 // Check that the current line allows merging. This depends on whether we
 // are in a control flow statements as well as several style flags.
-if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
+if (Line.First->is(tok::kw_case) ||
 (Line.First->Next && Line.First->Next->is(tok::kw_else)))
   return 0;
 // default: in switch statement
@@ -578,20 +578,21 @@
   if (Tok && Tok->is(tok::colon))
 return 0;
 }
-if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
-tok::kw___try, tok::kw_catch, tok::kw___finally,
-tok::kw_for, tok::r_brace, Keywords.kw___except)) {
+if (Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while, 
tok::kw_do,
+tok::kw_try, tok::kw___try, tok::kw_catch,
+tok::kw___finally, tok::kw_for, tok::r_brace,
+Keywords.kw___except)) {
   if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
 return 0;
   // Don't merge when we can't except the case when
   // the control statement block is empty
   if (!Style.AllowShortIfStatementsOnASingleLine &&
-  Line.startsWith(tok::kw_if) &&
+  Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
   !Style.BraceWrapping.AfterControlStatement &&
   !I[1]->First->is(tok::r_brace))
 return 0;
   if (!Style.AllowShortIfStatementsOnASingleLine &&
-  Line.startsWith(tok::kw_if) &&
+  Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
   Style.BraceWrapping.AfterControlStatement ==
   FormatStyle::BWACS_Always &&
   I + 2 != E && !I[2]->First->is(tok::r_brace))


Index: clang/unittests/Format/FormatTest.cpp

[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-22 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

Nothing majorly wrong here, its looks pretty good.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-22 Thread MyDeveloperDay via Phabricator via cfe-commits
MyDeveloperDay added a comment.

Could you add the case  where the else is not empty.




Comment at: clang/unittests/Format/FormatTest.cpp:5400
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+  EXPECT_EQ("if (a)\n"
+"{\n"

Can you please use verifyFormat  (as this does some extra stuff to ensure its 
stable)



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D114320/new/

https://reviews.llvm.org/D114320

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D114320: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks

2021-11-20 Thread Jesses Gott via Phabricator via cfe-commits
jessesna created this revision.
jessesna added a reviewer: klimek.
jessesna requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Extend AllowShortBlocksOnASingleLine for else blocks. See 
https://bugs.llvm.org/show_bug.cgi?id=49722


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114320

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -5394,6 +5394,27 @@
   EXPECT_EQ("void f() { }", format("void f() {}", Style));
   Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
   EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.BeforeElse = false;
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+  EXPECT_EQ("if (a)\n"
+"{\n"
+"} else if (b)\n"
+"{\n"
+"} else\n"
+"{ }",
+format("if(a){}else if(b){}else{}", Style));
+  Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
+  EXPECT_EQ("if (a) {\n"
+"} else if (b) {\n"
+"} else {\n"
+"}",
+format("if(a){}else if(b){}else{}", Style));
+  Style.BraceWrapping.BeforeElse = true;
+  EXPECT_EQ("if (a) { }\n"
+"else if (b) { }\n"
+"else { }",
+format("if(a){}else if(b){}else{}", Style));
 }
 
 TEST_F(FormatTest, FormatBeginBlockEndMacros) {
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -320,9 +320,9 @@
 }
 // Try to merge a control statement block with left brace wrapped
 if (I[1]->First->is(tok::l_brace) &&
-(TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
- tok::kw_switch, tok::kw_try, tok::kw_do,
- TT_ForEachMacro) ||
+(TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
+ tok::kw_for, tok::kw_switch, tok::kw_try,
+ tok::kw_do, TT_ForEachMacro) ||
  (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
   TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
 Style.BraceWrapping.AfterControlStatement ==
@@ -335,7 +335,7 @@
  ? 1
  : 0;
 } else if (I[1]->First->is(tok::l_brace) &&
-   TheLine->First->isOneOf(tok::kw_if, tok::kw_while,
+   TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
tok::kw_for)) {
   return (Style.BraceWrapping.AfterControlStatement ==
   FormatStyle::BWACS_Always)
@@ -569,7 +569,7 @@
 
 // Check that the current line allows merging. This depends on whether we
 // are in a control flow statements as well as several style flags.
-if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
+if (Line.First->is(tok::kw_case) ||
 (Line.First->Next && Line.First->Next->is(tok::kw_else)))
   return 0;
 // default: in switch statement
@@ -578,20 +578,21 @@
   if (Tok && Tok->is(tok::colon))
 return 0;
 }
-if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
-tok::kw___try, tok::kw_catch, tok::kw___finally,
-tok::kw_for, tok::r_brace, Keywords.kw___except)) {
+if (Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while, 
tok::kw_do,
+tok::kw_try, tok::kw___try, tok::kw_catch,
+tok::kw___finally, tok::kw_for, tok::r_brace,
+Keywords.kw___except)) {
   if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
 return 0;
   // Don't merge when we can't except the case when
   // the control statement block is empty
   if (!Style.AllowShortIfStatementsOnASingleLine &&
-  Line.startsWith(tok::kw_if) &&
+  Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
   !Style.BraceWrapping.AfterControlStatement &&
   !I[1]->First->is(tok::r_brace))
 return 0;
   if (!Style.AllowShortIfStatementsOnASingleLine &&
-  Line.startsWith(tok::kw_if) &&
+  Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
   Style.BraceWrapping.AfterControlStatement ==
   FormatStyle::BWACS_Always &&
   I + 2 != E && !I[2]->First->is(tok::r_brace))


Index: clang/unittests/Format/FormatTest.cpp