diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -302,7 +302,8 @@ Contexts.back().ContextType = Context::ForEachMacro; Contexts.back().IsExpression = false; } else if (OpeningParen.Previous && OpeningParen.Previous->MatchingParen && - OpeningParen.Previous->MatchingParen->is(TT_ObjCBlockLParen)) { + OpeningParen.Previous->MatchingParen->isOneOf( + TT_ObjCBlockLParen, TT_FunctionTypeLParen)) { Contexts.back().IsExpression = false; } else if (!Line.MustBeDeclaration && !Line.InPPDirective) { bool IsForOrCatch = diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -10459,6 +10459,7 @@ verifyFormat("#define MACRO() [](A *a) { return 1; }"); verifyFormat("Constructor() : member([](A *a, B *b) {}) {}"); verifyIndependentOfContext("typedef void (*f)(int *a);"); + verifyIndependentOfContext("typedef void (*f)(Type *a);"); verifyIndependentOfContext("int i{a * b};"); verifyIndependentOfContext("aaa && aaa->f();"); verifyIndependentOfContext("int x = ~*p;"); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -131,6 +131,20 @@ Tokens = annotate("delete[] *(ptr);"); EXPECT_EQ(Tokens.size(), 9u) << Tokens; EXPECT_TOKEN(Tokens[3], tok::star, TT_UnaryOperator); + + Tokens = annotate("void f() { void (*fnptr)(char* foo); }"); + EXPECT_EQ(Tokens.size(), 18u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionTypeLParen); + // FIXME: The star of a function pointer probably makes more sense as + // TT_PointerOrReference. + EXPECT_TOKEN(Tokens[7], tok::star, TT_UnaryOperator); + EXPECT_TOKEN(Tokens[12], tok::star, TT_PointerOrReference); + + Tokens = annotate("void f() { void (*fnptr)(t* foo); }"); + EXPECT_EQ(Tokens.size(), 18u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionTypeLParen); + EXPECT_TOKEN(Tokens[7], tok::star, TT_UnaryOperator); + EXPECT_TOKEN(Tokens[12], tok::star, TT_PointerOrReference); } TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {