]> granicus.if.org Git - clang/commitdiff
[clang-format] Don't allow newline after uppercase Obj-C block return types
authorDaniel Jasper <djasper@google.com>
Mon, 26 Sep 2016 22:19:08 +0000 (22:19 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 26 Sep 2016 22:19:08 +0000 (22:19 +0000)
Fixes the following:
  BOOL (^aaa)(void) = ^BOOL {
  };

The first BOOL's token was getting set to TT_FunctionAnnotationRParen
incorrectly, which was causing an unexpected newline after (^aaa). This
was introduced in r245846.

Patch by Kent Sutherland, thank you!

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

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 72d869782cea7cb0111dc7efd68ba8a53280399e..428a0688893be5d7f3c93bf07cd9a890980de8f6 100644 (file)
@@ -1037,12 +1037,17 @@ private:
           !Current.Next->isBinaryOperator() &&
           !Current.Next->isOneOf(tok::semi, tok::colon, tok::l_brace,
                                  tok::period, tok::arrow, tok::coloncolon))
-        if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
-          if (BeforeParen->is(tok::identifier) &&
-              BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
-              (!BeforeParen->Previous ||
-               BeforeParen->Previous->ClosesTemplateDeclaration))
-            Current.Type = TT_FunctionAnnotationRParen;
+        if (FormatToken *AfterParen = Current.MatchingParen->Next) {
+          // Make sure this isn't the return type of an Obj-C block declaration
+          if (AfterParen->Tok.isNot(tok::caret)) {
+            if (FormatToken *BeforeParen = Current.MatchingParen->Previous)
+              if (BeforeParen->is(tok::identifier) &&
+                  BeforeParen->TokenText == BeforeParen->TokenText.upper() &&
+                  (!BeforeParen->Previous ||
+                   BeforeParen->Previous->ClosesTemplateDeclaration))
+                Current.Type = TT_FunctionAnnotationRParen;
+          }
+        }
     } else if (Current.is(tok::at) && Current.Next) {
       if (Current.Next->isStringLiteral()) {
         Current.Type = TT_ObjCStringLiteral;
index bac2a5b7a91344aa519e2db642800cbc2e54c869..06a7e7ee917189f73f9f1feefcb83560a6a95213 100644 (file)
@@ -11156,6 +11156,8 @@ TEST_F(FormatTest, FormatsBlocks) {
                "  }\n"
                "});");
   verifyFormat("Block b = ^int *(A *a, B *b) {}");
+  verifyFormat("BOOL (^aaa)(void) = ^BOOL {\n"
+               "};");
 
   FormatStyle FourIndent = getLLVMStyle();
   FourIndent.ObjCBlockIndentWidth = 4;