[PATCH] [clang-format] add AfterMultilineControlStatement

2016-08-06 Thread Rinat Ibragimov via cfe-commits
Hi.

When indent is four spaces, "if" conditions spanning multiple lines
could be difficult to discern from the "if" body. Indentation is the
same, so they are visually blending. (See example below). One of the
solutions is to place opening brace on a separate line when "if"
condition spans multiple lines. Always putting it on a separate line
in undesired, as there are usually no so many "if"s with multiline
conditions.

To make that possible, proposed patch adds another setting,
BraceWrapping.AfterMultilineControlStatement which acts like
BraceWrapping.AfterControlStatement, but only if condition in control
statement spans over multiple lines.

I didn't see a way to detach brace and move it to the next line once
unwrapped lines are constructed. So when AfterMultilineControlStatement
is enabled, all opening braces go to a separate unwrapped line. Then,
if length of the line is not higher that column limit, code tries
to reattach brace back.

To make what I said above a bit clearer, here are examples:

if (0 + 1 + 2 + 3 + 4 + 5 +
6 + 7 + 8 + 9 + 10 + 11 +
12 + 13 + 14 + 15) {
some_code();
some_other_code();
}

This patch allows to change that to:

if (0 + 1 + 2 + 3 + 4 + 5 +
6 + 7 + 8 + 9 + 10 + 11 +
12 + 13 + 14 + 15)
{
some_code();
some_other_code();
}

while keeping opening brace for short conditions on the same line:

if (0) {
some_code();
}


---
Rinat---
 docs/ClangFormatStyleOptions.rst |   1 +
 include/clang/Format/Format.h|   2 +
 lib/Format/Format.cpp|   7 +-
 lib/Format/UnwrappedLineFormatter.cpp|  28 +-
 lib/Format/UnwrappedLineParser.cpp   |  14 +--
 test/Format/multiline-control-statements.cpp | 123 +++
 unittests/Format/FormatTest.cpp  |   1 +
 7 files changed, 167 insertions(+), 9 deletions(-)
 create mode 100644 test/Format/multiline-control-statements.cpp

diff --git a/docs/ClangFormatStyleOptions.rst b/docs/ClangFormatStyleOptions.rst
index a548e83..e83f144 100644
--- a/docs/ClangFormatStyleOptions.rst
+++ b/docs/ClangFormatStyleOptions.rst
@@ -340,6 +340,7 @@ the configuration (without a prefix: ``Auto``).
   * ``bool AfterControlStatement`` Wrap control statements (``if``/``for``/``while``/``switch``/..).
   * ``bool AfterEnum`` Wrap enum definitions.
   * ``bool AfterFunction`` Wrap function definitions.
+  * ``bool AfterMultilineControlStatement`` Wrap control statements that span over more than one line.
   * ``bool AfterNamespace`` Wrap namespace definitions.
   * ``bool AfterObjCDeclaration`` Wrap ObjC definitions (``@autoreleasepool``, interfaces, ..).
   * ``bool AfterStruct`` Wrap struct definitions.
diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h
index 1ff305d..80434dc 100644
--- a/include/clang/Format/Format.h
+++ b/include/clang/Format/Format.h
@@ -257,6 +257,8 @@ struct FormatStyle {
 bool AfterEnum;
 /// \brief Wrap function definitions.
 bool AfterFunction;
+/// \bried Wrap control statements spanning over more than one line.
+bool AfterMultilineControlStatement;
 /// \brief Wrap namespace definitions.
 bool AfterNamespace;
 /// \brief Wrap ObjC definitions (``@autoreleasepool``, interfaces, ..).
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index b0f64e2..3afacd2 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -363,6 +363,8 @@ template <> struct MappingTraits {
 IO.mapOptional("AfterControlStatement", Wrapping.AfterControlStatement);
 IO.mapOptional("AfterEnum", Wrapping.AfterEnum);
 IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
+IO.mapOptional("AfterMultilineControlStatement",
+   Wrapping.AfterMultilineControlStatement);
 IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
 IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
 IO.mapOptional("AfterStruct", Wrapping.AfterStruct);
@@ -440,7 +442,7 @@ static FormatStyle expandPresets(const FormatStyle &Style) {
 return Style;
   FormatStyle Expanded = Style;
   Expanded.BraceWrapping = {false, false, false, false, false, false,
-false, false, false, false, false};
+false, false, false, false, false, false};
   switch (Style.BreakBeforeBraces) {
   case FormatStyle::BS_Linux:
 Expanded.BraceWrapping.AfterClass = true;
@@ -464,6 +466,7 @@ static FormatStyle expandPresets(const FormatStyle &Style) {
 Expanded.BraceWrapping.AfterControlStatement = true;
 Expanded.BraceWrapping.AfterEnum = true;
 Expanded.BraceWrapping.AfterFunction = true;
+Expanded.BraceWrapping.AfterMultilineControlStatement = true;
 Expanded.BraceWrapping.AfterNamespace = true;
 Expanded.BraceWrapping.AfterObjCDeclaration = true;
 Expanded.BraceWrapping.AfterStruct = true;
@@ -472,7 +475,7 @@ static FormatStyle expandPresets(const FormatStyle &Style) {
 b

Re: [PATCH] [clang-format] add AfterMultilineControlStatement

2016-08-07 Thread Rinat Ibragimov via cfe-commits
I've seen couple of projects that have similar opening brace placement
for multiline conditions. Here are links to their style guides:

https://github.com/mpv-player/mpv/blob/master/DOCS/contribute.md

and

http://gem5.org/Coding_Style#Braces


>Воскресенье,  7 августа 2016, 3:04 +03:00 от Rinat Ibragimov via cfe-commits 
>:
>
>Hi.
>
>When indent is four spaces, "if" conditions spanning multiple lines
>could be difficult to discern from the "if" body. Indentation is the
>same, so they are visually blending. (See example below). One of the
>solutions is to place opening brace on a separate line when "if"
>condition spans multiple lines. Always putting it on a separate line
>in undesired, as there are usually no so many "if"s with multiline
>conditions.
>
>To make that possible, proposed patch adds another setting,
>BraceWrapping.AfterMultilineControlStatement which acts like
>BraceWrapping.AfterControlStatement, but only if condition in control
>statement spans over multiple lines.
>
>I didn't see a way to detach brace and move it to the next line once
>unwrapped lines are constructed. So when AfterMultilineControlStatement
>is enabled, all opening braces go to a separate unwrapped line. Then,
>if length of the line is not higher that column limit, code tries
>to reattach brace back.
>
>To make what I said above a bit clearer, here are examples:
>
>if (0 + 1 + 2 + 3 + 4 + 5 +
>6 + 7 + 8 + 9 + 10 + 11 +
>12 + 13 + 14 + 15) {
>some_code();
>some_other_code();
>}
>
>This patch allows to change that to:
>
>if (0 + 1 + 2 + 3 + 4 + 5 +
>6 + 7 + 8 + 9 + 10 + 11 +
>12 + 13 + 14 + 15)
>{
>some_code();
>some_other_code();
>}
>
>while keeping opening brace for short conditions on the same line:
>
>if (0) {
>some_code();
>}
>
>
>---
>Rinat
>___
>cfe-commits mailing list
>cfe-commits@lists.llvm.org
>http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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