Author: Emilia Dreamer Date: 2022-10-18T08:17:23+03:00 New Revision: 94215d2b2103c5ad74596dd51704836f92afec89
URL: https://github.com/llvm/llvm-project/commit/94215d2b2103c5ad74596dd51704836f92afec89 DIFF: https://github.com/llvm/llvm-project/commit/94215d2b2103c5ad74596dd51704836f92afec89.diff LOG: [clang-format] Correctly annotate star/amp in function pointer params 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 Differential Revision: https://reviews.llvm.org/D135707 Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 597d7b64625f3..8eef2be404d18 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -302,7 +302,8 @@ class AnnotatingParser { 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 index a777382e762a0..e9f5d66d8c566 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -10459,6 +10459,7 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { 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 index c8de8a2a2abe4..14918c7e6ff96 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -131,6 +131,20 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { 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) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits