[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-11-09 Thread Gedare Bloom via Phabricator via cfe-commits
gedare abandoned this revision.
gedare added a comment.

This is not needed due to https://github.com/llvm/llvm-project/pull/70768


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-08-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D154550#4562061 , @owenpan wrote:

> In D154550#4529346 , @gedare wrote:
>
>> In D154550#4526386 , @owenpan 
>> wrote:
>>
>>> Like `while (a);`, `while (a) {}` is also an empty loop, so `NonEmpty` is 
>>> misleading if it excludes the former but not the latter. IMO we should just 
>>> fix the bug without extending the option because any loop with a 
>>> single-statement body is a short loop.
>>
>> Agreed, except that many style guides (notably, K, LLVM, and Google) treat 
>> these two cases differently.
>
> LLVM doesn't merge short loops. Google uses `{}` instead of `;` whereas AFAIK 
> K does the opposite. I don't know of any major style that requires breaking 
> before the null statement and merging the empty block.

Got it. I can prepare a change to merge null as a short loop. I will get back 
to this in a couple weeks.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-08-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare added a comment.

In D154550#4562061 , @owenpan wrote:

> In D154550#4529346 , @gedare wrote:
>
>> In D154550#4526386 , @owenpan 
>> wrote:
>>
>>> Like `while (a);`, `while (a) {}` is also an empty loop, so `NonEmpty` is 
>>> misleading if it excludes the former but not the latter. IMO we should just 
>>> fix the bug without extending the option because any loop with a 
>>> single-statement body is a short loop.
>>
>> Agreed, except that many style guides (notably, K, LLVM, and Google) treat 
>> these two cases differently.
>
> LLVM doesn't merge short loops. Google uses `{}` instead of `;` whereas AFAIK 
> K does the opposite. I don't know of any major style that requires breaking 
> before the null statement and merging the empty block.

https://google.github.io/styleguide/cppguide.html#Formatting_Looping_Branching

Empty loop bodies should use either an empty pair of braces or continue with no 
braces, rather than a single semicolon.

  while (condition) {}  // Good - `{}` indicates no logic.
  while (condition) {
// Comments are okay, too
  }
  while (condition) continue;  // Good - `continue` indicates no logic.
  while (condition);  // Bad - looks like part of `do-while` loop.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-08-04 Thread Owen Pan via Phabricator via cfe-commits
owenpan added a comment.

In D154550#4529346 , @gedare wrote:

> In D154550#4526386 , @owenpan wrote:
>
>> Like `while (a);`, `while (a) {}` is also an empty loop, so `NonEmpty` is 
>> misleading if it excludes the former but not the latter. IMO we should just 
>> fix the bug without extending the option because any loop with a 
>> single-statement body is a short loop.
>
> Agreed, except that many style guides (notably, K, LLVM, and Google) treat 
> these two cases differently.

LLVM doesn't merge short loops. Google uses `{}` instead of `;` whereas AFAIK 
K does the opposite. I don't know of any major style that requires breaking 
before the null statement and merging the empty block.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-24 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 543721.
gedare added a comment.
This revision is now accepted and ready to land.

Change option name from NonEmpty to NonNullStatement. Fix space before semi.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -1380,7 +1380,8 @@
 
 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
   FormatStyle AllowsMergedLoops = getLLVMStyle();
-  AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine =
+  FormatStyle::SWFLS_NonNullStatement;
   verifyFormat("while (true) continue;", AllowsMergedLoops);
   verifyFormat("for (;;) continue;", AllowsMergedLoops);
   verifyFormat("for (int  : vec) v *= 2;", AllowsMergedLoops);
@@ -1436,6 +1437,66 @@
"  while (true);\n"
"}",
AllowsMergedLoops);
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_All;
+  verifyFormat("while (true) continue;", AllowsMergedLoops);
+  verifyFormat("for (;;) continue;", AllowsMergedLoops);
+  verifyFormat("for (int  : vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int , vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("while (true);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  while (true);",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;);",
+   AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int , vec)\n"
+   "  for (;;);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  BOOST_FOREACH (int , vec);",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) // Can't merge this\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) /* still don't merge */\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("do a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do /* Don't merge */\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do\n"
+   "  // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  // Without braces labels are interpreted differently.
+  verifyFormat("{\n"
+   "  do\n"
+   "  label:\n"
+   "a++;\n"
+   "  while (true);\n"
+   "}",
+   AllowsMergedLoops);
 }
 
 TEST_F(FormatTest, FormatShortBracedStatements) {
@@ -1443,7 +1504,8 @@
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
 FormatStyle::SIS_Never);
-  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
+  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine,
+FormatStyle::SWFLS_Never);
   EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
   verifyFormat("for (;;) {\n"
"  f();\n"
@@ -1488,7 +1550,8 @@
   AllowSimpleBracedStatements.ColumnLimit = 40;
   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
   FormatStyle::SBS_Always;
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine =
+  FormatStyle::SWFLS_NonNullStatement;
   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
@@ -1605,7 +1668,8 @@
"}",
AllowSimpleBracedStatements);
 
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine =
+  

[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-24 Thread Gedare Bloom via Phabricator via cfe-commits
gedare planned changes to this revision.
gedare added a comment.

In D154550#4526386 , @owenpan wrote:

> Like `while (a);`, `while (a) {}` is also an empty loop, so `NonEmpty` is 
> misleading if it excludes the former but not the latter. IMO we should just 
> fix the bug without extending the option because any loop with a 
> single-statement body is a short loop.

Agreed, except that many style guides (notably, K, LLVM, and Google) treat 
these two cases differently. The loop with only a semi-colon is a special case, 
and is what I'm looking to support. K defines the semi-colon terminated 
for/while loop as a body composed of a //null statement//. I believe that `{ }` 
is instead an empty body, as it does not parse as a statement, despite 
generating identical code. Maybe`NonNullStatement` is precise and better than 
`NonEmpty`?  I will prepare that change, and fix the spacing before the `;`.

Whether someone thinks collapsing bracketed bodies that are short should be 
supported is a separate problem, and even if so, it would require a similar 
style option change as I suggest to allow for backward compatibility. I'm not 
personally interested in that case, as the code base I work with uses the 
isolated semicolon.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-23 Thread Owen Pan via Phabricator via cfe-commits
owenpan added a comment.

Like `while (a);`, `while (a) {}` is also an empty loop, so `NonEmpty` is 
misleading if it excludes the former but not the latter. IMO we should just fix 
the bug without extending the option because any loop with a single-statement 
body is a short loop.




Comment at: clang/unittests/Format/FormatTest.cpp:1444-1467
+  verifyFormat("while (true) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);

There should be no spaces between `)` and `;`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-07 Thread Björn Schäpers via Phabricator via cfe-commits
HazardyKnusperkeks accepted this revision.
HazardyKnusperkeks added a comment.
This revision is now accepted and ready to land.

Please wait for other opinions.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-06 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537859.
gedare added a comment.

Regenerate complete diff


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -1380,7 +1380,7 @@
 
 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
   FormatStyle AllowsMergedLoops = getLLVMStyle();
-  AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_NonEmpty;
   verifyFormat("while (true) continue;", AllowsMergedLoops);
   verifyFormat("for (;;) continue;", AllowsMergedLoops);
   verifyFormat("for (int  : vec) v *= 2;", AllowsMergedLoops);
@@ -1436,6 +1436,66 @@
"  while (true);\n"
"}",
AllowsMergedLoops);
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_All;
+  verifyFormat("while (true) continue;", AllowsMergedLoops);
+  verifyFormat("for (;;) continue;", AllowsMergedLoops);
+  verifyFormat("for (int  : vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int , vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("while (true) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  while (true) ;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int , vec)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  BOOST_FOREACH (int , vec) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) // Can't merge this\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) /* still don't merge */\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("do a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do /* Don't merge */\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do\n"
+   "  // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  // Without braces labels are interpreted differently.
+  verifyFormat("{\n"
+   "  do\n"
+   "  label:\n"
+   "a++;\n"
+   "  while (true);\n"
+   "}",
+   AllowsMergedLoops);
 }
 
 TEST_F(FormatTest, FormatShortBracedStatements) {
@@ -1443,7 +1503,8 @@
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
 FormatStyle::SIS_Never);
-  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
+  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine,
+FormatStyle::SWFLS_Never);
   EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
   verifyFormat("for (;;) {\n"
"  f();\n"
@@ -1488,7 +1549,8 @@
   AllowSimpleBracedStatements.ColumnLimit = 40;
   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
   FormatStyle::SBS_Always;
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine =
+  FormatStyle::SWFLS_NonEmpty;
   AllowSimpleBracedStatements.BreakBeforeBraces = FormatStyle::BS_Custom;
   AllowSimpleBracedStatements.BraceWrapping.AfterFunction = true;
   AllowSimpleBracedStatements.BraceWrapping.SplitEmptyRecord = false;
@@ -1605,7 +1667,8 @@
"}",
AllowSimpleBracedStatements);
 
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine =
+  FormatStyle::SWFLS_Never;
   verifyFormat("while (true) {}", AllowSimpleBracedStatements);
   verifyFormat("while (true) {\n"
 

[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-06 Thread Gedare Bloom via Phabricator via cfe-commits
gedare updated this revision to Diff 537857.
gedare added a comment.

Reorder traits and add comment about backward compatibility.


Herald added a comment.

NOTE: Clang-Format Team Automated Review Comment

It looks like your clang-format review does not contain any unit tests, please 
try to ensure all code changes have a unit test (unless this is an `NFC` or 
refactoring, adding documentation etc..)

Add your unit tests in `clang/unittests/Format` and you can build with `ninja 
FormatTests`.  We recommend using the `verifyFormat(xxx)` format of unit tests 
rather than `EXPECT_EQ` as this will ensure you change is tolerant to random 
whitespace changes (see FormatTest.cpp as an example)

For situations where your change is altering the TokenAnnotator.cpp which can 
happen if you are trying to improve the annotation phase to ensure we are 
correctly identifying the type of a token, please add a token annotator test in 
`TokenAnnotatorTest.cpp`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

Files:
  clang/lib/Format/Format.cpp


Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -593,9 +593,11 @@
 template <> struct 
ScalarEnumerationTraits {
   static void enumeration(IO , FormatStyle::ShortWhileForLoopStyle ) {
 IO.enumCase(Value, "Never", FormatStyle::SWFLS_Never);
-IO.enumCase(Value, "false", FormatStyle::SWFLS_Never);
 IO.enumCase(Value, "NonEmpty", FormatStyle::SWFLS_NonEmpty);
 IO.enumCase(Value, "All", FormatStyle::SWFLS_All);
+
+// For backward compatibility.
+IO.enumCase(Value, "false", FormatStyle::SWFLS_Never);
 IO.enumCase(Value, "true", FormatStyle::SWFLS_NonEmpty);
   }
 };


Index: clang/lib/Format/Format.cpp
===
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -593,9 +593,11 @@
 template <> struct ScalarEnumerationTraits {
   static void enumeration(IO , FormatStyle::ShortWhileForLoopStyle ) {
 IO.enumCase(Value, "Never", FormatStyle::SWFLS_Never);
-IO.enumCase(Value, "false", FormatStyle::SWFLS_Never);
 IO.enumCase(Value, "NonEmpty", FormatStyle::SWFLS_NonEmpty);
 IO.enumCase(Value, "All", FormatStyle::SWFLS_All);
+
+// For backward compatibility.
+IO.enumCase(Value, "false", FormatStyle::SWFLS_Never);
 IO.enumCase(Value, "true", FormatStyle::SWFLS_NonEmpty);
   }
 };
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-06 Thread Björn Schäpers via Phabricator via cfe-commits
HazardyKnusperkeks added inline comments.



Comment at: clang/lib/Format/Format.cpp:595-599
+IO.enumCase(Value, "Never", FormatStyle::SWFLS_Never);
+IO.enumCase(Value, "false", FormatStyle::SWFLS_Never);
+IO.enumCase(Value, "NonEmpty", FormatStyle::SWFLS_NonEmpty);
+IO.enumCase(Value, "All", FormatStyle::SWFLS_All);
+IO.enumCase(Value, "true", FormatStyle::SWFLS_NonEmpty);




Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D154550

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


[PATCH] D154550: [clang-format] Allow empty loops on a single line.

2023-07-05 Thread Gedare Bloom via Phabricator via cfe-commits
gedare created this revision.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
gedare requested review of this revision.

Changes the AllowShortLoopsOnASingleLine from a boolean to a style option
consisting of three choices: Never, NonEmpty, and All. Never does not merge a
loop body back to the header's line. NonEmpty allows for merging a loop body
only if it has an expression. The new option is All, which will merge empty
loop bodies, i.e., a semi-colon.

The options of true and false are maintained for backward compatibility to mean
NonEmpty and Never, respectively.

See also Github Issue 61708.
https://github.com/llvm/llvm-project/issues/61708


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154550

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/UnwrappedLineFormatter.cpp
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

Index: clang/unittests/Format/FormatTest.cpp
===
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -1380,7 +1380,7 @@
 
 TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
   FormatStyle AllowsMergedLoops = getLLVMStyle();
-  AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_NonEmpty;
   verifyFormat("while (true) continue;", AllowsMergedLoops);
   verifyFormat("for (;;) continue;", AllowsMergedLoops);
   verifyFormat("for (int  : vec) v *= 2;", AllowsMergedLoops);
@@ -1436,6 +1436,66 @@
"  while (true);\n"
"}",
AllowsMergedLoops);
+  AllowsMergedLoops.AllowShortLoopsOnASingleLine = FormatStyle::SWFLS_All;
+  verifyFormat("while (true) continue;", AllowsMergedLoops);
+  verifyFormat("for (;;) continue;", AllowsMergedLoops);
+  verifyFormat("for (int  : vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int , vec) v *= 2;", AllowsMergedLoops);
+  verifyFormat("while (true) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  while (true) ;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) continue;",
+   AllowsMergedLoops);
+  verifyFormat("while (true)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("BOOST_FOREACH (int , vec)\n"
+   "  for (;;) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;)\n"
+   "  BOOST_FOREACH (int , vec) ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) // Can't merge this\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("for (;;) /* still don't merge */\n"
+   "  ;",
+   AllowsMergedLoops);
+  verifyFormat("do a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do /* Don't merge */\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  verifyFormat("do\n"
+   "  // Don't merge\n"
+   "  a++;\n"
+   "while (true);",
+   AllowsMergedLoops);
+  // Without braces labels are interpreted differently.
+  verifyFormat("{\n"
+   "  do\n"
+   "  label:\n"
+   "a++;\n"
+   "  while (true);\n"
+   "}",
+   AllowsMergedLoops);
 }
 
 TEST_F(FormatTest, FormatShortBracedStatements) {
@@ -1443,7 +1503,8 @@
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine, false);
   EXPECT_EQ(AllowSimpleBracedStatements.AllowShortIfStatementsOnASingleLine,
 FormatStyle::SIS_Never);
-  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine, false);
+  EXPECT_EQ(AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine,
+FormatStyle::SWFLS_Never);
   EXPECT_EQ(AllowSimpleBracedStatements.BraceWrapping.AfterFunction, false);
   verifyFormat("for (;;) {\n"
"  f();\n"
@@ -1488,7 +1549,8 @@
   AllowSimpleBracedStatements.ColumnLimit = 40;
   AllowSimpleBracedStatements.AllowShortBlocksOnASingleLine =
   FormatStyle::SBS_Always;
-  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = true;
+  AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine