-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang-format] Handle C-style cast of member function pointer type #126340
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFixes #125012. Full diff: https://github.com/llvm/llvm-project/pull/126340.diff 2 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 94fd7ba9c0e79e3..b3540f39e6f69fc 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 1b09c4570345622..54d8ff0571ca686 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -874,6 +874,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);
|
This was referenced Feb 8, 2025
HazardyKnusperkeks
approved these changes
Feb 8, 2025
@owenca is it possible to get this fix on the 20 branch? |
You mean the 20.x release branch? |
Yes. |
/cherry-pick 8d373ce |
/pull-request #126479 |
Thanks. |
swift-ci
pushed a commit
to swiftlang/llvm-project
that referenced
this pull request
Feb 11, 2025
…lvm#126340) Fixes llvm#125012. (cherry picked from commit 8d373ce)
Icohedron
pushed a commit
to Icohedron/llvm-project
that referenced
this pull request
Feb 11, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #125012.