Skip to content

Commit 6ae14c0

Browse files
authored
[clang-format] Annotate the l_paren of function pointer types (#109229)
Fixes #109146.
1 parent a5876be commit 6ae14c0

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,18 @@ class AnnotatingParser {
468468
OpeningParen.Previous && OpeningParen.Previous->is(tok::kw_for);
469469
FormatToken *PossibleObjCForInToken = nullptr;
470470
while (CurrentToken) {
471-
if (CurrentToken->Previous->is(TT_PointerOrReference) &&
472-
CurrentToken->Previous->Previous->isOneOf(tok::l_paren,
473-
tok::coloncolon)) {
471+
const auto &Prev = *CurrentToken->Previous;
472+
if (Prev.is(TT_PointerOrReference) &&
473+
Prev.Previous->isOneOf(tok::l_paren, tok::coloncolon)) {
474474
ProbablyFunctionType = true;
475475
}
476476
if (CurrentToken->is(tok::comma))
477477
MightBeFunctionType = false;
478-
if (CurrentToken->Previous->is(TT_BinaryOperator))
478+
if (Prev.is(TT_BinaryOperator))
479479
Contexts.back().IsExpression = true;
480480
if (CurrentToken->is(tok::r_paren)) {
481+
if (Prev.is(TT_PointerOrReference) && Prev.Previous == &OpeningParen)
482+
MightBeFunctionType = true;
481483
if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
482484
ProbablyFunctionType && CurrentToken->Next &&
483485
(CurrentToken->Next->is(tok::l_paren) ||
@@ -550,8 +552,8 @@ class AnnotatingParser {
550552
bool ProbablyFunctionTypeLParen =
551553
(CurrentToken->is(tok::l_paren) && CurrentToken->Next &&
552554
CurrentToken->Next->isOneOf(tok::star, tok::amp, tok::caret));
553-
if ((CurrentToken->Previous->isOneOf(tok::kw_const, tok::kw_auto) ||
554-
CurrentToken->Previous->isTypeName(LangOpts)) &&
555+
if ((Prev.isOneOf(tok::kw_const, tok::kw_auto) ||
556+
Prev.isTypeName(LangOpts)) &&
555557
!(CurrentToken->is(tok::l_brace) ||
556558
(CurrentToken->is(tok::l_paren) && !ProbablyFunctionTypeLParen))) {
557559
Contexts.back().IsExpression = false;

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
809809
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
810810
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen);
811811

812+
Tokens = annotate("return (Foo (*)(void *, Bar, ...))&foo;");
813+
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
814+
EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionTypeLParen);
815+
EXPECT_TOKEN(Tokens[14], tok::r_paren, TT_CastRParen);
816+
EXPECT_TOKEN(Tokens[15], tok::amp, TT_UnaryOperator);
817+
812818
auto Style = getLLVMStyle();
813819
Style.TypeNames.push_back("Foo");
814820
Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);

0 commit comments

Comments
 (0)