]> granicus.if.org Git - clang/commitdiff
clang-format: Fix incorrect function type detection.
authorDaniel Jasper <djasper@google.com>
Mon, 13 Jun 2016 07:49:35 +0000 (07:49 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 13 Jun 2016 07:49:35 +0000 (07:49 +0000)
Before:
  returnsFunction (&param1, &param2)(param);

After:
  returnsFunction(&param1, &param2)(param);

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

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

index 8a36aca97e5437ce25e5150e4aa924b96edf76c9..41bf10dd80089060dca2d8d074b01ccecb0853db 100644 (file)
@@ -178,8 +178,8 @@ private:
       Left->Type = TT_ObjCMethodExpr;
     }
 
-    bool MightBeFunctionType = CurrentToken->isOneOf(tok::star, tok::amp) &&
-                               !Contexts[Contexts.size() - 2].IsExpression;
+    bool MightBeFunctionType = !Contexts[Contexts.size() - 2].IsExpression;
+    bool ProbablyFunctionType = CurrentToken->isOneOf(tok::star, tok::amp);
     bool HasMultipleLines = false;
     bool HasMultipleParametersOnALine = false;
     bool MightBeObjCForRangeLoop =
@@ -206,14 +206,15 @@ private:
       if (CurrentToken->Previous->is(TT_PointerOrReference) &&
           CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
                                                     tok::coloncolon))
-        MightBeFunctionType = true;
+        ProbablyFunctionType = true;
+      if (CurrentToken->is(tok::comma))
+        MightBeFunctionType = false;
       if (CurrentToken->Previous->is(TT_BinaryOperator))
         Contexts.back().IsExpression = true;
       if (CurrentToken->is(tok::r_paren)) {
-        if (MightBeFunctionType && CurrentToken->Next &&
+        if (MightBeFunctionType && ProbablyFunctionType && CurrentToken->Next &&
             (CurrentToken->Next->is(tok::l_paren) ||
-             (CurrentToken->Next->is(tok::l_square) &&
-              Line.MustBeDeclaration)))
+             (CurrentToken->Next->is(tok::l_square) && Line.MustBeDeclaration)))
           Left->Type = TT_FunctionTypeLParen;
         Left->MatchingParen = CurrentToken;
         CurrentToken->MatchingParen = Left;
index d82770ba683ea8faed8129377475810effbbea95..3b580b5ae244f172895f042ce1c61cc6cacbb4a7 100644 (file)
@@ -6097,6 +6097,7 @@ TEST_F(FormatTest, FormatsFunctionTypes) {
   verifyFormat("some_var = function(*some_pointer_var)[0];");
   verifyFormat("void f() { function(*some_pointer_var)[0] = 10; }");
   verifyFormat("int x = f(&h)();");
+  verifyFormat("returnsFunction(&param1, &param2)(param);");
 }
 
 TEST_F(FormatTest, FormatsPointersToArrayTypes) {