rymiel created this revision. rymiel added reviewers: HazardyKnusperkeks, owenpan, MyDeveloperDay, curdeius. Herald added a project: All. rymiel requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Inside the arguments part of a function pointer declaration, `determineStarAmpUsage` results in a binary operator rather than pointers, because said parens are assumed to be an expression. This patch correctly marks the argument parens of a function pointer type as not an expression. Note that this fix already existed for Objective-C blocks as part of f1f267b447f60528440d2c066b29ab014ae7f90f. As Objective-C blocks and C/C++ function pointers share a lot of the same logic, that fix also makes sense here. Fixes https://github.com/llvm/llvm-project/issues/31659 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D135707 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ 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) { Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ 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;"); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ 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 =
Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ 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) { Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ 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;"); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ 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 =
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits