-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang-format] Correctly annotate user-defined conversion function call #137914
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
Merged
Conversation
This file contains hidden or 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) ChangesFix #137770 Full diff: https://github.com/llvm/llvm-project/pull/137914.diff 2 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index e56cc92987af7..ea0086fd49a33 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1656,11 +1656,15 @@ class AnnotatingParser {
// Skip to l_paren.
for (LParen = CurrentToken->Next;
LParen && LParen->isNot(tok::l_paren); LParen = LParen->Next) {
+ if (LParen->isPointerOrReference())
+ LParen->setFinalizedType(TT_PointerOrReference);
}
}
if (LParen && LParen->is(tok::l_paren)) {
- Tok->setFinalizedType(TT_FunctionDeclarationName);
- LParen->setFinalizedType(TT_FunctionDeclarationLParen);
+ if (!Contexts.back().IsExpression) {
+ Tok->setFinalizedType(TT_FunctionDeclarationName);
+ LParen->setFinalizedType(TT_FunctionDeclarationLParen);
+ }
break;
}
}
@@ -1683,7 +1687,8 @@ class AnnotatingParser {
if (CurrentToken->is(tok::comma) && Previous->isNot(tok::kw_operator))
break;
if (Previous->isOneOf(TT_BinaryOperator, TT_UnaryOperator, tok::comma,
- tok::star, tok::arrow, tok::amp, tok::ampamp) ||
+ tok::arrow) ||
+ Previous->isPointerOrReference() ||
// User defined literal.
Previous->TokenText.starts_with("\"\"")) {
Previous->setType(TT_OverloadedOperator);
@@ -3026,7 +3031,7 @@ class AnnotatingParser {
if (!NextToken ||
NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
- TT_RequiresClause, TT_FunctionDeclarationLParen) ||
+ TT_RequiresClause) ||
(NextToken->is(tok::kw_noexcept) && !IsExpression) ||
NextToken->canBePointerOrReferenceQualifier() ||
(NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 844f51f9e7c2e..124fb43dcf5ac 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -392,9 +392,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
Tokens = annotate("return s.operator int *();");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
- EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_FunctionDeclarationName);
+ // Not TT_FunctionDeclarationName.
+ EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_Unknown);
EXPECT_TOKEN(Tokens[5], tok::star, TT_PointerOrReference);
- EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen);
+ // Not TT_FunctionDeclarationLParen.
+ EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_Unknown);
+
+ Tokens = annotate("B &b = x.operator B &();");
+ ASSERT_EQ(Tokens.size(), 13u) << Tokens;
+ EXPECT_TOKEN(Tokens[8], tok::amp, TT_PointerOrReference);
Tokens = annotate("int8_t *a = MacroCall(int8_t, width * height * length);");
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
|
HazardyKnusperkeks
approved these changes
Apr 30, 2025
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
IanWood1
pushed a commit
to IanWood1/llvm-project
that referenced
this pull request
May 6, 2025
GeorgeARM
pushed a commit
to GeorgeARM/llvm-project
that referenced
this pull request
May 7, 2025
Ankur-0429
pushed a commit
to Ankur-0429/llvm-project
that referenced
this pull request
May 9, 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.
Fix #137770