[llvm-branch-commits] [clang] release/20.x: [clang-format] Handle C-style cast of member function pointer type (#126340) (PR #126479)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Feb 9 23:53:07 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: None (llvmbot)
<details>
<summary>Changes</summary>
Backport 8d373ceaec1f1b27c9e682cfaf71aae19ea48d98
Requested by: @<!-- -->owenca
---
Full diff: https://github.com/llvm/llvm-project/pull/126479.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+5-2)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+6)
``````````diff
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index a172df5291ae62a..4246ade6e19be53 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -477,8 +477,9 @@ class AnnotatingParser {
FormatToken *PossibleObjCForInToken = nullptr;
while (CurrentToken) {
const auto &Prev = *CurrentToken->Previous;
+ const auto *PrevPrev = Prev.Previous;
if (Prev.is(TT_PointerOrReference) &&
- Prev.Previous->isOneOf(tok::l_paren, tok::coloncolon)) {
+ PrevPrev->isOneOf(tok::l_paren, tok::coloncolon)) {
ProbablyFunctionType = true;
}
if (CurrentToken->is(tok::comma))
@@ -486,8 +487,10 @@ class AnnotatingParser {
if (Prev.is(TT_BinaryOperator))
Contexts.back().IsExpression = true;
if (CurrentToken->is(tok::r_paren)) {
- if (Prev.is(TT_PointerOrReference) && Prev.Previous == &OpeningParen)
+ if (Prev.is(TT_PointerOrReference) &&
+ (PrevPrev == &OpeningParen || PrevPrev->is(tok::coloncolon))) {
MightBeFunctionType = true;
+ }
if (OpeningParen.isNot(TT_CppCastLParen) && MightBeFunctionType &&
ProbablyFunctionType && CurrentToken->Next &&
(CurrentToken->Next->is(tok::l_paren) ||
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index fc77e277947c563..2147a1b950dd19b 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -845,6 +845,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
EXPECT_TOKEN(Tokens[14], tok::r_paren, TT_CastRParen);
EXPECT_TOKEN(Tokens[15], tok::amp, TT_UnaryOperator);
+ Tokens = annotate("return (Foo (Bar::*)())&Bar::foo;");
+ ASSERT_EQ(Tokens.size(), 17u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionTypeLParen);
+ EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_CastRParen);
+ EXPECT_TOKEN(Tokens[11], tok::amp, TT_UnaryOperator);
+
auto Style = getLLVMStyle();
Style.TypeNames.push_back("Foo");
Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);
``````````
</details>
https://github.com/llvm/llvm-project/pull/126479
More information about the llvm-branch-commits
mailing list