[PATCH] clang-format: Support @synchronized.

strager strager.nds at gmail.com
Thu Jun 25 23:35:13 PDT 2015


Strip out ObjCSpaceAfterSynchronized option.


http://reviews.llvm.org/D10371

Files:
  lib/Format/TokenAnnotator.cpp
  lib/Format/UnwrappedLineParser.cpp
  unittests/Format/FormatTest.cpp

Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -121,7 +121,8 @@
 
     if (Left->Previous &&
         (Left->Previous->isOneOf(tok::kw_static_assert, tok::kw_if,
-                                 tok::kw_while, tok::l_paren, tok::comma) ||
+                                 tok::kw_while, tok::l_paren, tok::comma,
+                                 tok::objc_synchronized) ||
          Left->Previous->is(TT_BinaryOperator))) {
       // static_assert, if and while usually contain expressions.
       Contexts.back().IsExpression = true;
@@ -1840,7 +1841,8 @@
     return Line.Type == LT_ObjCDecl || Left.is(tok::semi) ||
            (Style.SpaceBeforeParens != FormatStyle::SBPO_Never &&
             (Left.isOneOf(tok::kw_if, tok::kw_for, tok::kw_while,
-                          tok::kw_switch, tok::kw_case, TT_ForEachMacro) ||
+                          tok::kw_switch, tok::kw_case, tok::objc_synchronized,
+                          TT_ForEachMacro) ||
              (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch,
                            tok::kw_new, tok::kw_delete) &&
               (!Left.Previous || Left.Previous->isNot(tok::period))))) ||
Index: lib/Format/UnwrappedLineParser.cpp
===================================================================
--- lib/Format/UnwrappedLineParser.cpp
+++ lib/Format/UnwrappedLineParser.cpp
@@ -656,6 +656,18 @@
       nextToken();
       addUnwrappedLine();
       return;
+    case tok::objc_synchronized:
+      nextToken();
+      if (FormatTok->Tok.is(tok::l_paren))
+        parseParens();
+      if (FormatTok->Tok.is(tok::l_brace)) {
+        if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
+            Style.BreakBeforeBraces == FormatStyle::BS_GNU)
+          addUnwrappedLine();
+        parseBlock(/*MustBeDeclaration=*/false);
+      }
+      addUnwrappedLine();
+      return;
     case tok::objc_try:
       // This branch isn't strictly necessary (the kw_try case below would
       // do this too after the tok::at is parsed above).  But be explicit.
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -2391,6 +2391,30 @@
                "});\n");
 }
 
+TEST_F(FormatTest, FormatObjCSynchronized) {
+  FormatStyle Style = getLLVMStyleWithColumns(32);
+  verifyFormat("@synchronized (foo) {\n"
+               "  f();\n"
+               "}\n",
+               Style);
+  verifyFormat("@synchronized ([self\n"
+               "    veryLongMethodNameWithParameters:\n"
+               "        YES]) {\n"
+               "  f();\n"
+               "}\n",
+               Style);
+  Style.BreakBeforeBraces = FormatStyle::BS_Allman;
+  verifyFormat("@synchronized (foo)\n"
+               "{\n"
+               "  f();\n"
+               "}\n"
+               "@synchronized (foo)\n"
+               "{\n"
+               "  f();\n"
+               "}\n",
+               Style);
+}
+
 TEST_F(FormatTest, StaticInitializers) {
   verifyFormat("static SomeClass SC = {1, 'a'};");
 
@@ -7327,7 +7351,7 @@
   verifyFormat("char *buf1 = @encode(int **);");
   verifyFormat("Protocol *proto = @protocol(p1);");
   verifyFormat("SEL s = @selector(foo:);");
-  verifyFormat("@synchronized(self) {\n"
+  verifyFormat("@synchronized (self) {\n"
                "  f();\n"
                "}");

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D10371.28534.patch
Type: text/x-patch
Size: 3542 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150626/962030a7/attachment.bin>


More information about the cfe-commits mailing list