]> granicus.if.org Git - clang/commitdiff
clang-format: Fix formatting of function type parameters.
authorDaniel Jasper <djasper@google.com>
Mon, 16 Dec 2013 08:36:18 +0000 (08:36 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 16 Dec 2013 08:36:18 +0000 (08:36 +0000)
Before:
  void f() { typedef void (*f)(int * a); }
After:
  void f() { typedef void (*f)(int *a); }

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

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

index 9132ba43770425998e1ed9935e880cefcb559cd1..19759afc85423bff4c22a4397dbccc81ccc2aedd 100644 (file)
@@ -605,12 +605,17 @@ private:
           Previous->Type = TT_PointerOrReference;
         }
       }
-    } else if (Current.isOneOf(tok::kw_return, tok::kw_throw) ||
-               (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
-                !Line.InPPDirective &&
-                (!Current.Previous ||
-                 !Current.Previous->isOneOf(tok::kw_for, tok::kw_catch)))) {
+    } else if (Current.isOneOf(tok::kw_return, tok::kw_throw)) {
       Contexts.back().IsExpression = true;
+    } else if (Current.is(tok::l_paren) && !Line.MustBeDeclaration &&
+               !Line.InPPDirective) {
+      bool ParametersOfFunctionType =
+          Current.Previous && Current.Previous->is(tok::r_paren) &&
+          Current.Previous->MatchingParen &&
+          Current.Previous->MatchingParen->Type == TT_FunctionTypeLParen;
+      bool IsForOrCatch = Current.Previous &&
+                          Current.Previous->isOneOf(tok::kw_for, tok::kw_catch);
+      Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;
     } else if (Current.isOneOf(tok::r_paren, tok::greater, tok::comma)) {
       for (FormatToken *Previous = Current.Previous;
            Previous && Previous->isOneOf(tok::star, tok::amp);
index e34ebeb98cd002d8a855a0ef255dd08f7495da74..bd9d108357651e94e762df3d8dc6351ecb10a120 100644 (file)
@@ -4241,6 +4241,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
   verifyFormat("auto a = [](int **&, int ***) {};");
   verifyFormat("auto PointerBinding = [](const char *S) {};");
   verifyFormat("typedef typeof(int(int, int)) *MyFunc;");
+  verifyIndependentOfContext("typedef void (*f)(int *a);");
 
   verifyIndependentOfContext("InvalidRegions[*R] = 0;");