]> granicus.if.org Git - clang/commitdiff
clang-format: fix formatting of ObjC @synchronized blocks
authorFrancois Ferrand <thetypz@gmail.com>
Tue, 27 Feb 2018 13:48:21 +0000 (13:48 +0000)
committerFrancois Ferrand <thetypz@gmail.com>
Tue, 27 Feb 2018 13:48:21 +0000 (13:48 +0000)
Summary:
The blocks used to be formatted using the "default" behavior, and would
thus be mistaken for function calls followed by blocks: this could lead
to unexpected inlining of the block and extra line-break before the
opening brace.

They are now formatted similarly to `@autoreleasepool` blocks, as
expected:

  @synchronized(self) {
      f();
  }

Reviewers: krasimir, djasper, klimek

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D43114

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@326191 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestObjC.cpp

index 7cde7ca88e7bdbc759f493910375d79c075b3f3d..8ab61f900c94a68ac9ec00f4202cd91746029e57 100644 (file)
@@ -1135,6 +1135,18 @@ void UnwrappedLineParser::parseStructuralElement() {
         }
         addUnwrappedLine();
         return;
+      case tok::objc_synchronized:
+        nextToken();
+        if (FormatTok->Tok.is(tok::l_paren))
+           // Skip synchronization object
+           parseParens();
+        if (FormatTok->Tok.is(tok::l_brace)) {
+          if (Style.BraceWrapping.AfterObjCDeclaration)
+            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 40dc375d5768fe90780bd35f03849733c3e1a33e..d592b39a482e9138b372ce3c18ae10296888609f 100644 (file)
@@ -209,6 +209,24 @@ TEST_F(FormatTestObjC, FormatObjCGenerics) {
                "        aaaaaaaaaaaaaaaaa);\n");
 }
 
+TEST_F(FormatTestObjC, FormatObjCSynchronized) {
+  verifyFormat("@synchronized(self) {\n"
+               "  f();\n"
+               "}\n"
+               "@synchronized(self) {\n"
+               "  f();\n"
+               "}\n");
+  Style.BreakBeforeBraces = FormatStyle::BS_Allman;
+  verifyFormat("@synchronized(self)\n"
+               "{\n"
+               "  f();\n"
+               "}\n"
+               "@synchronized(self)\n"
+               "{\n"
+               "  f();\n"
+               "}\n");
+}
+
 TEST_F(FormatTestObjC, FormatObjCInterface) {
   verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
                "@public\n"