lifted created this revision.
lifted added a reviewer: djasper.
lifted added subscribers: cfe-commits, klimek.

Add Qt brace style as described on https://wiki.qt.io/Qt_Coding_Style :

* Use attached braces: the opening brace goes on the same line as the start of
  the statement.  If the closing brace is followed by another keyword, it goes
  into the same line as well.

* Exception: Function implementations and class declarations always have the
  left brace on the start of a line.

http://reviews.llvm.org/D11837

Files:
  docs/ClangFormatStyleOptions.rst
  include/clang/Format/Format.h
  lib/Format/Format.cpp
  lib/Format/UnwrappedLineParser.cpp
  unittests/Format/FormatTest.cpp

Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -2329,13 +2329,16 @@
 
 TEST_F(FormatTest, FormatTryCatchBraceStyles) {
   FormatStyle Style = getLLVMStyle();
-  Style.BreakBeforeBraces = FormatStyle::BS_Attach;
-  verifyFormat("try {\n"
-               "  // something\n"
-               "} catch (...) {\n"
-               "  // something\n"
-               "}",
-               Style);
+  for (auto BraceStyle :
+       {FormatStyle::BS_Attach, FormatStyle::BS_Mozilla, FormatStyle::BS_Qt}) {
+    Style.BreakBeforeBraces = BraceStyle;
+    verifyFormat("try {\n"
+                 "  // something\n"
+                 "} catch (...) {\n"
+                 "  // something\n"
+                 "}",
+                 Style);
+  }
   Style.BreakBeforeBraces = FormatStyle::BS_Stroustrup;
   verifyFormat("try {\n"
                "  // something\n"
@@ -9060,6 +9063,57 @@
                "#endif",
                GNUBraceStyle);
 }
+
+TEST_F(FormatTest, QtBraceBreaking) {
+  FormatStyle QtBraceStyle = getLLVMStyle();
+  QtBraceStyle.BreakBeforeBraces = FormatStyle::BS_Qt;
+  verifyFormat("namespace a {\n"
+               "class A\n"
+               "{\n"
+               "  void f()\n"
+               "  {\n"
+               "    if (true) {\n"
+               "      a();\n"
+               "      b();\n"
+               "    }\n"
+               "  }\n"
+               "  void g() { return; }\n"
+               "};\n"
+               "enum E {\n"
+               "  A,\n"
+               "  // foo\n"
+               "  B,\n"
+               "  C\n"
+               "};\n"
+               "struct B {\n"
+               "  int x;\n"
+               "};\n"
+               "}\n",
+               QtBraceStyle);
+  verifyFormat("struct S {\n"
+               "  int Type;\n"
+               "  union {\n"
+               "    int x;\n"
+               "    double y;\n"
+               "  } Value;\n"
+               "  class C\n"
+               "  {\n"
+               "    MyFavoriteType Value;\n"
+               "  } Class;\n"
+               "};\n",
+               QtBraceStyle);
+
+  QtBraceStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
+  verifyFormat("static void foo(int g)\n"
+               "{\n"
+               "  qDebug(\"foo: %i\", g);\n"
+               "}\n"
+               "class Moo\n"
+               "{\n"
+               "};",
+               QtBraceStyle);
+}
+
 TEST_F(FormatTest, CatchExceptionReferenceBinding) {
   verifyFormat("void f() {\n"
                "  try {\n"
@@ -9336,6 +9390,7 @@
   CHECK_PARSE("BreakBeforeBraces: Allman", BreakBeforeBraces,
               FormatStyle::BS_Allman);
   CHECK_PARSE("BreakBeforeBraces: GNU", BreakBeforeBraces, FormatStyle::BS_GNU);
+  CHECK_PARSE("BreakBeforeBraces: Qt", BreakBeforeBraces, FormatStyle::BS_Qt);
 
   Style.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_All;
   CHECK_PARSE("AlwaysBreakAfterDefinitionReturnType: None",
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -459,6 +459,8 @@
     return InitialToken.isOneOf(tok::kw_namespace, tok::kw_class);
   case FormatStyle::BS_Mozilla:
     return InitialToken.isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union);
+  case FormatStyle::BS_Qt:
+    return InitialToken.is(tok::kw_class);
   case FormatStyle::BS_Allman:
   case FormatStyle::BS_GNU:
     return true;
Index: lib/Format/Format.cpp
===================================================================
--- lib/Format/Format.cpp
+++ lib/Format/Format.cpp
@@ -98,6 +98,7 @@
     IO.enumCase(Value, "Stroustrup", FormatStyle::BS_Stroustrup);
     IO.enumCase(Value, "Allman", FormatStyle::BS_Allman);
     IO.enumCase(Value, "GNU", FormatStyle::BS_GNU);
+    IO.enumCase(Value, "Qt", FormatStyle::BS_Qt);
   }
 };
 
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -176,7 +176,9 @@
     /// 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.
-    BS_GNU
+    BS_GNU,
+    /// Like ``Attach``, but break before braces on functions, and classes.
+    BS_Qt
   };
 
   /// \brief The brace breaking style to use.
Index: docs/ClangFormatStyleOptions.rst
===================================================================
--- docs/ClangFormatStyleOptions.rst
+++ docs/ClangFormatStyleOptions.rst
@@ -286,6 +286,8 @@
     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.
+  * ``BS_Qt`` (in configuration: ``Qt``)
+    Like ``Attach``, but break before braces on functions, and classes.
 
 
 **BreakBeforeTernaryOperators** (``bool``)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to