Skip to content

Commit bc87f9b

Browse files
owencatstellar
authored andcommitted
[clang-format] Handle C-style cast of member function pointer type (#126340)
Fixes #125012. (cherry picked from commit 8d373ce)
1 parent 5777d5d commit bc87f9b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,20 @@ class AnnotatingParser {
477477
FormatToken *PossibleObjCForInToken = nullptr;
478478
while (CurrentToken) {
479479
const auto &Prev = *CurrentToken->Previous;
480+
const auto *PrevPrev = Prev.Previous;
480481
if (Prev.is(TT_PointerOrReference) &&
481-
Prev.Previous->isOneOf(tok::l_paren, tok::coloncolon)) {
482+
PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
482483
ProbablyFunctionType = true;
483484
}
484485
if (CurrentToken->is(tok::comma))
485486
MightBeFunctionType = false;
486487
if (Prev.is(TT_BinaryOperator))
487488
Contexts.back().IsExpression = true;
488489
if (CurrentToken->is(tok::r_paren)) {
489-
if (Prev.is(TT_PointerOrReference) && Prev.Previous == &OpeningParen)
490+
if (Prev.is(TT_PointerOrReference) &&
491+
(PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
490492
MightBeFunctionType = true;
493+
}
491494
if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
492495
ProbablyFunctionType && CurrentToken->Next &&
493496
(CurrentToken->Next->is(tok::l_paren) ||

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
845845
EXPECT_TOKEN(Tokens[14], tok::r_paren, TT_CastRParen);
846846
EXPECT_TOKEN(Tokens[15], tok::amp, TT_UnaryOperator);
847847

848+
Tokens = annotate("return (Foo (Bar::*)())&Bar::foo;");
849+
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
850+
EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionTypeLParen);
851+
EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_CastRParen);
852+
EXPECT_TOKEN(Tokens[11], tok::amp, TT_UnaryOperator);
853+
848854
auto Style = getLLVMStyle();
849855
Style.TypeNames.push_back("Foo");
850856
Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);

0 commit comments

Comments
 (0)