[cfe-dev] Clang-format: Braces Indent Style Whitesmiths

JVApen JVApen at gmail.com
Thu Jan 1 07:14:25 PST 2015


Hi Daniel,

Most of appears to be working with the Whitesmiths; however I still have
one group of tests failing.
These tests contain enumerations, the scenario is the following:

enum X
  {
  Y = 0
  }

which gets formatted to:

enum X
{
  Y = 0
}

If I call addUnwrappedLine() at the end of parseEnum() the result becomes
the following:

enum X
  {
    Y = 0
  }

The debug information tells me the following:
Line(0): enum[47] identifier[47]
Line(1): l_brace[47] identifier[47] equal[47] numeric_constant[47]
r_brace[47]
Line(0): eof[47]

Hower for this result I expect it to be more like:
Line(0): enum[47] identifier[47]
Line(1): l_brace[47]
Line(2): identifier[47] equal[47] numeric_constant[47]
Line(1): r_brace[47]
Line(0): eof[47]

However I'm guessing that I am missing something obvious;
I do however suspect parseBracedList() from doing something which I don't
completely understand
OR something in the UnwrappedLineFormatter.cpp file to mess this up.

Any idea on where to look for the responsible?

​JVApen​


On 26 December 2014 at 20:14, Daniel Jasper <djasper at google.com> wrote:

>
>
> On Fri, Dec 26, 2014 at 7:53 PM, JVApen <JVApen at gmail.com> wrote:
>
>> Hi all,
>>
>> I was looking into the clang tools to experiment with; hoping to convince
>> some colleagues that tooling can save us time.
>> Anyhow; since I was trying to take baby steps; clang format looked like a
>> very good start.
>> Unfortunately, we would not be programmers if we wouldn't do things a bit
>> different than the rest of the world.
>>
>> The current code; with too many files; uses the Whitesmiths style
>> <http://en.wikipedia.org/wiki/Indent_style#Whitesmiths_style> at most
>> places.
>> Though this is not yet supported by clang format.
>> Going through the code, adding support for it did not look like a big
>> thing; since Allman and GNU are very similar.
>> The thing I did not realize is that both of them indent all code by one
>> level compared to the {,
>> which is not done by Whitesmiths style.
>>
>
> That actually shouldn't matter much. The whether or not to break before
> the "{" is controlled by calling addUnwrappedLine() one more time whereas
> the indentation is controlled by changing Line->Level. Basically all you
> should need to do is to change the order of "addUnwrappedLine();
> ++Line->Level;" for the addUnwrappedLine() call for the "{". Unfortunately,
> there is no nice abstraction for this and you'll need to look at how to do
> this for the different kinds of syntactic elements individually. I can look
> into it and give more help if you get stuck.
>
> I already wrote a unit test, based on the Allman unit test; useful for
>> test driven development
>> and experimented already with the code in
>> lib/Format/UnwrappedLineParser.cpp
>> Though can't seem to find out how to prevent the extra indent;
>> does anyone with more codebase experience have an idea where to start?
>>
>> JVApen
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150101/a8a27d84/attachment.html>
-------------- next part --------------
Index: docs/ClangFormatStyleOptions.rst
===================================================================
--- docs/ClangFormatStyleOptions.rst	(revision 225059)
+++ docs/ClangFormatStyleOptions.rst	(working copy)
@@ -256,6 +256,8 @@
     Like ``Attach``, but break before function definitions, and 'else'.
   * ``BS_Allman`` (in configuration: ``Allman``)
     Always break before braces.
+  * ``BS_Whitesmiths`` (in configuration: ``Whitesmiths``)
+    Like ``Allman``, with the braces intended too.
   * ``BS_GNU`` (in configuration: ``GNU``)
     Always break before braces and add an extra level of indentation to
     braces of control statements, not to those of class, function
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h	(revision 225059)
+++ include/clang/Format/Format.h	(working copy)
@@ -319,6 +319,8 @@
     BS_Stroustrup,
     /// Always break before braces.
     BS_Allman,
+	/// Allman style with a level of indentation before the braces
+	BS_Whitesmiths,
     /// Always break before braces and add an extra level of indentation to
     /// braces of control statements, not to those of class, function
     /// or other definitions.
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp	(revision 225059)
+++ lib/Format/Format.cpp	(working copy)
@@ -95,6 +95,7 @@
     IO.enumCase(Value, "Linux", FormatStyle::BS_Linux);
     IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
     IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
+    IO.enumCase(Value, "Whitesmiths", FormatStyle::BS_Whitesmiths);
     IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
   }
 };
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp	(revision 225059)
+++ lib/Format/TokenAnnotator.cpp	(working copy)
@@ -1876,6 +1876,7 @@
             Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline);
   } else if (isAllmanBrace(Left) || isAllmanBrace(Right)) {
     return Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+           Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths ||
            Style.BreakBeforeBraces == FormatStyle::BS_GNU;
   } else if (Style.Language == FormatStyle::LK_Proto &&
              Left.isNot(tok::l_brace) && Right.is(TT_SelectorName)) {
Index: lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- lib/Format/UnwrappedLineFormatter.cpp	(revision 225059)
+++ lib/Format/UnwrappedLineFormatter.cpp	(working copy)
@@ -135,6 +135,7 @@
     if (Limit == 0)
       return 0;
     if ((Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+         Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths ||
          Style.BreakBeforeBraces == FormatStyle::BS_GNU) &&
         (I[1]->First->is(tok::l_brace) && !Style.AllowShortBlocksOnASingleLine))
       return 0;
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp	(revision 225059)
+++ lib/Format/UnwrappedLineParser.cpp	(working copy)
@@ -32,6 +32,21 @@
 
 namespace {
 
+class LevelGuard {
+public:
+  LevelGuard(unsigned &Level)
+	  : Level(Level)
+	  , OldLevel(Level) {
+	}
+  ~LevelGuard() {
+    Level = OldLevel;
+  }
+
+private:
+  unsigned &Level;
+  unsigned OldLevel;
+};
+
 class ScopedDeclarationState {
 public:
   ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
@@ -155,19 +170,28 @@
 public:
   CompoundStatementIndenter(UnwrappedLineParser *Parser,
                             const FormatStyle &Style, unsigned &LineLevel)
-      : LineLevel(LineLevel), OldLineLevel(LineLevel) {
+      : LineLevel(LineLevel), OldLineLevel(LineLevel)
+	  , IndentAfterwardsNeeded(true) {
     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman) {
       Parser->addUnwrappedLine();
+    } else if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
+      Parser->addUnwrappedLine();
+      ++LineLevel;
+	  IndentAfterwardsNeeded = false;
     } else if (Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
       Parser->addUnwrappedLine();
       ++LineLevel;
     }
   }
+
+  bool mustIndentAfterwards() { return IndentAfterwardsNeeded; }
+
   ~CompoundStatementIndenter() { LineLevel = OldLineLevel; }
 
 private:
   unsigned &LineLevel;
   unsigned OldLineLevel;
+  bool     IndentAfterwardsNeeded;
 };
 
 namespace {
@@ -441,6 +465,7 @@
   case FormatStyle::BS_Linux:
     return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
   case FormatStyle::BS_Allman:
+  case FormatStyle::BS_Whitesmiths:
   case FormatStyle::BS_GNU:
     return true;
   default:
@@ -794,7 +819,14 @@
         if (Style.BreakBeforeBraces != FormatStyle::BS_Attach)
           addUnwrappedLine();
         FormatTok->Type = TT_FunctionLBrace;
-        parseBlock(/*MustBeDeclaration=*/false);
+
+		bool isWhitesmiths =
+			Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
+
+		LevelGuard Guard(Line->Level);
+		if (isWhitesmiths)
+		  ++Line->Level;
+        parseBlock(/*MustBeDeclaration=*/false, /*AddLevel=*/!isWhitesmiths);
         addUnwrappedLine();
         return;
       }
@@ -980,8 +1012,8 @@
 
 bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons) {
   bool HasError = false;
+  // Eat the l_brace
   nextToken();
-
   // FIXME: Once we have an expression parser in the UnwrappedLineParser,
   // replace this by using parseAssigmentExpression() inside.
   do {
@@ -1112,8 +1144,9 @@
   bool NeedsUnwrappedLine = false;
   if (FormatTok->Tok.is(tok::l_brace)) {
     CompoundStatementIndenter Indenter(this, Style, Line->Level);
-    parseBlock(/*MustBeDeclaration=*/false);
+    parseBlock(/*MustBeDeclaration=*/false, Indenter.mustIndentAfterwards());
     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+		Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths ||
         Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
       addUnwrappedLine();
     } else {
@@ -1131,7 +1164,7 @@
     nextToken();
     if (FormatTok->Tok.is(tok::l_brace)) {
       CompoundStatementIndenter Indenter(this, Style, Line->Level);
-      parseBlock(/*MustBeDeclaration=*/false);
+      parseBlock(/*MustBeDeclaration=*/false, Indenter.mustIndentAfterwards());
       addUnwrappedLine();
     } else if (FormatTok->Tok.is(tok::kw_if)) {
       parseIfThenElse();
@@ -1165,8 +1198,9 @@
   }
   if (FormatTok->is(tok::l_brace)) {
     CompoundStatementIndenter Indenter(this, Style, Line->Level);
-    parseBlock(/*MustBeDeclaration=*/false);
+    parseBlock(/*MustBeDeclaration=*/false, Indenter.mustIndentAfterwards());
     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+        Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths ||
         Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
         Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
       addUnwrappedLine();
@@ -1199,8 +1233,9 @@
     }
     NeedsUnwrappedLine = false;
     CompoundStatementIndenter Indenter(this, Style, Line->Level);
-    parseBlock(/*MustBeDeclaration=*/false);
+    parseBlock(/*MustBeDeclaration=*/false, Indenter.mustIndentAfterwards());
     if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+        Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths ||
         Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
         Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup) {
       addUnwrappedLine();
@@ -1216,13 +1251,17 @@
 void UnwrappedLineParser::parseNamespace() {
   assert(FormatTok->Tok.is(tok::kw_namespace) && "'namespace' expected");
 
+  LevelGuard Guard(Line->Level);
   const FormatToken &InitialToken = *FormatTok;
   nextToken();
   if (FormatTok->Tok.is(tok::identifier))
     nextToken();
   if (FormatTok->Tok.is(tok::l_brace)) {
-    if (ShouldBreakBeforeBrace(Style, InitialToken))
+    if (ShouldBreakBeforeBrace(Style, InitialToken)) {
       addUnwrappedLine();
+	  if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
+         ++Line->Level;
+	}
 
     bool AddLevel = Style.NamespaceIndentation == FormatStyle::NI_All ||
                     (Style.NamespaceIndentation == FormatStyle::NI_Inner &&
@@ -1246,7 +1285,7 @@
     parseParens();
   if (FormatTok->Tok.is(tok::l_brace)) {
     CompoundStatementIndenter Indenter(this, Style, Line->Level);
-    parseBlock(/*MustBeDeclaration=*/false);
+    parseBlock(/*MustBeDeclaration=*/false, Indenter.mustIndentAfterwards());
     addUnwrappedLine();
   } else {
     addUnwrappedLine();
@@ -1261,8 +1300,10 @@
   nextToken();
   if (FormatTok->Tok.is(tok::l_brace)) {
     CompoundStatementIndenter Indenter(this, Style, Line->Level);
-    parseBlock(/*MustBeDeclaration=*/false);
-    if (Style.BreakBeforeBraces == FormatStyle::BS_GNU)
+    parseBlock(/*MustBeDeclaration=*/false, Indenter.mustIndentAfterwards());
+
+    if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths ||
+        Style.BreakBeforeBraces == FormatStyle::BS_GNU)
       addUnwrappedLine();
   } else {
     addUnwrappedLine();
@@ -1288,10 +1329,11 @@
     --Line->Level;
   if (CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) {
     CompoundStatementIndenter Indenter(this, Style, Line->Level);
-    parseBlock(/*MustBeDeclaration=*/false);
+    parseBlock(/*MustBeDeclaration=*/false, Indenter.mustIndentAfterwards());
     if (FormatTok->Tok.is(tok::kw_break)) {
-      // "break;" after "}" on its own line only for BS_Allman and BS_GNU
+      // "break;" after "}" on its own line only for BS_Allman, BS_Whitesmiths and BS_GNU
       if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+          Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths ||
           Style.BreakBeforeBraces == FormatStyle::BS_GNU) {
         addUnwrappedLine();
       }
@@ -1372,6 +1414,11 @@
     return;
   }
 
+  LevelGuard Guard(Line->Level);
+  if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
+	  addUnwrappedLine();
+	  ++Line->Level;
+  }
   // Parse enum body.
   bool HasError = !parseBracedList(/*ContinueOnSemicolons=*/true);
   if (HasError) {
@@ -1483,13 +1530,19 @@
       }
     }
   }
+  LevelGuard Guard(Line->Level);
   if (FormatTok->Tok.is(tok::l_brace)) {
-    if (ShouldBreakBeforeBrace(Style, InitialToken))
+	bool isWhitesmiths = Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths;
+    if (ShouldBreakBeforeBrace(Style, InitialToken)) {
       addUnwrappedLine();
+	  if (isWhitesmiths)
+		  ++Line->Level;
+	}
 
-    parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/true,
+    parseBlock(/*MustBeDeclaration=*/true, /*AddLevel=*/!isWhitesmiths,
                /*MunchSemi=*/false);
   }
+
   // We fall through to parsing a structural element afterwards, so
   // class A {} n, m;
   // will end up in one unwrapped line.
@@ -1531,27 +1584,34 @@
   nextToken();
   nextToken(); // interface name
 
-  // @interface can be followed by either a base class, or a category.
-  if (FormatTok->Tok.is(tok::colon)) {
-    nextToken();
-    nextToken(); // base class name
-  } else if (FormatTok->Tok.is(tok::l_paren))
-    // Skip category, if present.
-    parseParens();
+  {
+    LevelGuard Guard(Line->Level);
 
-  if (FormatTok->Tok.is(tok::less))
-    parseObjCProtocolList();
+    // @interface can be followed by either a base class, or a category.
+    if (FormatTok->Tok.is(tok::colon)) {
+      nextToken();
+      nextToken(); // base class name
+    } else if (FormatTok->Tok.is(tok::l_paren))
+      // Skip category, if present.
+      parseParens();
 
-  if (FormatTok->Tok.is(tok::l_brace)) {
-    if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
-        Style.BreakBeforeBraces == FormatStyle::BS_GNU)
-      addUnwrappedLine();
-    parseBlock(/*MustBeDeclaration=*/true);
-  }
+    if (FormatTok->Tok.is(tok::less))
+      parseObjCProtocolList();
 
+    if (FormatTok->Tok.is(tok::l_brace)) {
+      if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+          Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths ||
+          Style.BreakBeforeBraces == FormatStyle::BS_GNU)
+        addUnwrappedLine();
+      if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)
+        ++Line->Level;
+      parseBlock(/*MustBeDeclaration=*/true);
+    }
+
   // With instance variables, this puts '}' on its own line.  Without instance
   // variables, this ends the @interface line.
   addUnwrappedLine();
+  }
 
   parseObjCUntilAtEnd();
 }
Index: tools/CMakeLists.txt
===================================================================
--- tools/CMakeLists.txt	(revision 225059)
+++ tools/CMakeLists.txt	(working copy)
@@ -21,3 +21,5 @@
 # to keep the primary Clang repository small and focused.
 # It also may be included by LLVM_EXTERNAL_CLANG_TOOLS_EXTRA_SOURCE_DIR.
 add_llvm_external_project(clang-tools-extra extra)
+add_subdirectory(include-what-you-use)
+add_subdirectory(qconnectlint)
Index: tools/Makefile
===================================================================
--- tools/Makefile	(revision 225059)
+++ tools/Makefile	(working copy)
@@ -11,7 +11,7 @@
 
 include $(CLANG_LEVEL)/../../Makefile.config
 
-DIRS := 
+DIRS := include-what-you-use qconnectlint
 PARALLEL_DIRS := clang-format driver diagtool
 
 ifeq ($(ENABLE_CLANG_STATIC_ANALYZER), 1)
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp	(revision 225059)
+++ unittests/Format/FormatTest.cpp	(working copy)
@@ -2262,6 +2262,16 @@
                "  // something\n"
                "}",
                Style);
+  Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
+  verifyFormat("try\n"
+               "  {\n"
+               "  // something\n"
+               "  }\n"
+               "catch (...)\n"
+               "  {\n"
+               "  // something\n"
+               "  }",
+               Style);
   Style.BreakBeforeBraces = FormatStyle::BS_GNU;
   verifyFormat("try\n"
                "  {\n"
@@ -3715,6 +3725,13 @@
                "}",
                Style);
 
+  Style.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
+  verifyFormat("void someLongFunction(\n"
+               "    int someLongParameter) const\n"
+               "  {\n"
+               "  }",
+               Style);
+
   // Unless these are unknown annotations.
   verifyFormat("void SomeFunction(aaaaaaaaaa aaaaaaaaaaaaaaa,\n"
                "                  aaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
@@ -8404,6 +8421,193 @@
                BreakBeforeBraceShortIfs);
 }
 
+TEST_F(FormatTest, WhitesmithsBraceBreaking) {
+  FormatStyle WhitesmithsBraceStyle = getLLVMStyle();
+  WhitesmithsBraceStyle.BreakBeforeBraces = FormatStyle::BS_Whitesmiths;
+  verifyFormat("namespace a\n"
+               "  {\n"
+               "  class A\n"
+               "    {\n"
+               "    void f()\n"
+               "      {\n"
+               "      if (true)\n"
+               "        {\n"
+               "        a();\n"
+               "        b();\n"
+               "        }\n"
+               "      }\n"
+               "    void g() { return; }\n"
+               "    };\n"
+               "  struct B\n"
+               "    {\n"
+               "    int x;\n"
+               "    };\n"
+               "  }",
+               WhitesmithsBraceStyle);
+
+  verifyFormat("void f()\n"
+               "  {\n"
+               "  if (true)\n"
+               "    {\n"
+               "    a();\n"
+               "    }\n"
+               "  else if (false)\n"
+               "    {\n"
+               "    b();\n"
+               "    }\n"
+               "  else\n"
+               "    {\n"
+               "    c();\n"
+               "    }\n"
+               "  }\n",
+               WhitesmithsBraceStyle);
+
+  verifyFormat("void f()\n"
+               "  {\n"
+               "  for (int i = 0; i < 10; ++i)\n"
+               "    {\n"
+               "    a();\n"
+               "    }\n"
+               "  while (false)\n"
+               "    {\n"
+               "    b();\n"
+               "    }\n"
+               "  do\n"
+               "    {\n"
+               "    c();\n"
+               "    }\n"
+			   "  while (false)\n"
+               "  }\n",
+               WhitesmithsBraceStyle);
+
+  verifyFormat("void f(int a)\n"
+               "  {\n"
+               "  switch (a)\n"
+               "    {\n"
+               "    case 0:\n"
+               "      break;\n"
+               "    case 1:\n"
+               "      {\n"
+               "      break;\n"
+               "      }\n"
+               "    case 2:\n"
+               "      {\n"
+               "      }\n"
+               "      break;\n"
+               "    default:\n"
+               "      break;\n"
+               "    }\n"
+               "  }\n",
+               WhitesmithsBraceStyle);
+
+  verifyFormat("enum X\n"
+               "  {\n"
+               "  Y = 0,\n"
+               "  }\n",
+               WhitesmithsBraceStyle);
+  verifyFormat("enum X\n"
+               "  {\n"
+               "  Y = 0\n"
+               "  }\n",
+               WhitesmithsBraceStyle);
+
+  verifyFormat("@interface BSApplicationController ()\n"
+               "  {\n"
+               "  @private\n"
+               "    id _extraIvar;\n"
+               "  }\n"
+               "@end\n",
+               WhitesmithsBraceStyle);
+
+  verifyFormat("#ifdef _DEBUG\n"
+               "int foo(int i = 0)\n"
+               "#else\n"
+               "int foo(int i = 5)\n"
+               "#endif\n"
+               "  {\n"
+               "  return i;\n"
+               "  }",
+               WhitesmithsBraceStyle);
+
+  verifyFormat("void foo() {}\n"
+               "void bar()\n"
+               "#ifdef _DEBUG\n"
+               "  {\n"
+               "  foo();\n"
+               "  }\n"
+               "#else\n"
+               "  {\n"
+               "  }\n"
+               "#endif",
+               WhitesmithsBraceStyle);
+
+  verifyFormat("void foobar() { int i = 5; }\n"
+               "#ifdef _DEBUG\n"
+               "void bar() {}\n"
+               "#else\n"
+               "void bar() { foobar(); }\n"
+               "#endif",
+               WhitesmithsBraceStyle);
+
+  // This shouldn't affect ObjC blocks..
+  verifyFormat("[self doSomeThingWithACompletionHandler:^{\n"
+               "  // ...\n"
+               "  int i;\n"
+               "}];",
+               WhitesmithsBraceStyle);
+  verifyFormat("void (^block)(void) = ^{\n"
+               "  // ...\n"
+               "  int i;\n"
+               "};",
+               WhitesmithsBraceStyle);
+  // .. or dict literals.
+  verifyFormat("void f()\n"
+               "  {\n"
+               "  [object someMethod:@{ @\"a\" : @\"b\" }];\n"
+               "  }",
+               WhitesmithsBraceStyle);
+  verifyFormat("int f()\n"
+               "  { // comment\n"
+               "  return 42;\n"
+               "  }",
+               WhitesmithsBraceStyle);
+
+  WhitesmithsBraceStyle.ColumnLimit = 19;
+  verifyFormat("void f() { int i; }", WhitesmithsBraceStyle);
+  WhitesmithsBraceStyle.ColumnLimit = 18;
+  verifyFormat("void f()\n"
+               "  {\n"
+               "  int i;\n"
+               "  }",
+               WhitesmithsBraceStyle);
+  WhitesmithsBraceStyle.ColumnLimit = 80;
+
+  FormatStyle BreakBeforeBraceShortIfs = WhitesmithsBraceStyle;
+  BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
+  BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
+  verifyFormat("void f(bool b)\n"
+               "  {\n"
+               "  if (b)\n"
+               "    {\n"
+               "    return;\n"
+               "    }\n"
+               "  }\n",
+               BreakBeforeBraceShortIfs);
+  verifyFormat("void f(bool b)\n"
+               "  {\n"
+               "  if (b) return;\n"
+               "  }\n",
+               BreakBeforeBraceShortIfs);
+  verifyFormat("void f(bool b)\n"
+               "  {\n"
+               "  while (b)\n"
+               "    {\n"
+               "    return;\n"
+               "    }\n"
+               "  }\n",
+               BreakBeforeBraceShortIfs);
+}
+
 TEST_F(FormatTest, GNUBraceBreaking) {
   FormatStyle GNUBraceStyle = getLLVMStyle();
   GNUBraceStyle.BreakBeforeBraces = FormatStyle::BS_GNU;
@@ -8792,6 +8996,8 @@
               FormatStyle::BS_Stroustrup);
   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
               FormatStyle::BS_Allman);
+  CHECK_PARSE("BreakBeforeBraces: Whitesmiths", BreakBeforeBraces,
+              FormatStyle::BS_Whitesmiths);
   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
 
   Style.NamespaceIndentation = FormatStyle::NI_All;


More information about the cfe-dev mailing list