]> granicus.if.org Git - clang/commitdiff
clang-format: use AfterControlStatement to format ObjC control blocks
authorFrancois Ferrand <thetypz@gmail.com>
Tue, 27 Feb 2018 13:48:27 +0000 (13:48 +0000)
committerFrancois Ferrand <thetypz@gmail.com>
Tue, 27 Feb 2018 13:48:27 +0000 (13:48 +0000)
ObjC defines `@autoreleasepool` and `@synchronized` control blocks. These
used to be formatted according to the `AfterObjCDeclaration` brace-
wrapping flag, which is not very consistent.

This patch changes the behavior to use the `AfterControlStatement` flag
instead. This should not affect the behavior unless a custom brace
wrapping mode is used.

Reviewers: krasimir, djasper, klimek, benhamilton

Subscribers: cfe-commits

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

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

include/clang/Format/Format.h
lib/Format/UnwrappedLineFormatter.cpp
lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTestObjC.cpp

index 94df954b605c00b7f21d0eacd79439607d2986e3..3060d9b944e7dc0cfef96521487bbecdeaf9400d 100644 (file)
@@ -662,7 +662,9 @@ struct FormatStyle {
     ///   }
     /// \endcode
     bool AfterNamespace;
-    /// \brief Wrap ObjC definitions (``@autoreleasepool``, interfaces, ..).
+    /// \brief Wrap ObjC definitions (interfaces, implementations...).
+    /// \note @autoreleasepool and @synchronized blocks are wrapped
+    /// according to `AfterControlStatement` flag.
     bool AfterObjCDeclaration;
     /// \brief Wrap struct definitions.
     /// \code
index 253f89da9d14ba4cfc97bf1b825e1ad903983498..2ce39fb04c6cfe3ae9061c8adf56586e5bacdd56 100644 (file)
@@ -314,6 +314,14 @@ private:
       }
       return MergedLines;
     }
+    // Don't merge block with left brace wrapped after ObjC special blocks
+    if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
+        I[-1]->First->is(tok::at) && I[-1]->First->Next) {
+      tok::ObjCKeywordKind kwId = I[-1]->First->Next->Tok.getObjCKeywordID();
+      if (kwId == clang::tok::objc_autoreleasepool ||
+          kwId == clang::tok::objc_synchronized)
+        return 0;
+    }
     // Try to merge a block with left brace wrapped that wasn't yet covered
     if (TheLine->Last->is(tok::l_brace)) {
       return !Style.BraceWrapping.AfterFunction ||
index 8ab61f900c94a68ac9ec00f4202cd91746029e57..5ab5cc46cdcd9223be0e4e8917f0a638ffe9737c 100644 (file)
@@ -1129,7 +1129,7 @@ void UnwrappedLineParser::parseStructuralElement() {
       case tok::objc_autoreleasepool:
         nextToken();
         if (FormatTok->Tok.is(tok::l_brace)) {
-          if (Style.BraceWrapping.AfterObjCDeclaration)
+          if (Style.BraceWrapping.AfterControlStatement)
             addUnwrappedLine();
           parseBlock(/*MustBeDeclaration=*/false);
         }
@@ -1141,7 +1141,7 @@ void UnwrappedLineParser::parseStructuralElement() {
            // Skip synchronization object
            parseParens();
         if (FormatTok->Tok.is(tok::l_brace)) {
-          if (Style.BraceWrapping.AfterObjCDeclaration)
+          if (Style.BraceWrapping.AfterControlStatement)
             addUnwrappedLine();
           parseBlock(/*MustBeDeclaration=*/false);
         }
index d592b39a482e9138b372ce3c18ae10296888609f..15c3d7622cfd589b5cceb32dcd58f0f0cc646bce 100644 (file)
@@ -187,7 +187,8 @@ TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
                "@autoreleasepool {\n"
                "  f();\n"
                "}\n");
-  Style.BreakBeforeBraces = FormatStyle::BS_Allman;
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.AfterControlStatement = true;
   verifyFormat("@autoreleasepool\n"
                "{\n"
                "  f();\n"
@@ -216,7 +217,8 @@ TEST_F(FormatTestObjC, FormatObjCSynchronized) {
                "@synchronized(self) {\n"
                "  f();\n"
                "}\n");
-  Style.BreakBeforeBraces = FormatStyle::BS_Allman;
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.AfterControlStatement = true;
   verifyFormat("@synchronized(self)\n"
                "{\n"
                "  f();\n"