[clang-format] Fix a bug in annotating && followed by * or 𐆍
Merged
Conversation
Member
|
@llvm/pr-subscribers-clang ChangesFixes #65877.Full diff: https://github.com/llvm/llvm-project/pull/65933.diff 2 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index e925bee44cd0c6..142168e074bbc2 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2598,9 +2598,12 @@ class AnnotatingParser {
if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
return TT_BinaryOperator;
- // "&&(" is quite unlikely to be two successive unary "&".
- if (Tok.is(tok::ampamp) && NextToken->is(tok::l_paren))
+ // "&&" followed by "(", "*", or "&" is quite unlikely to be two successive
+ // unary "&".
+ if (Tok.is(tok::ampamp) &&
+ NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
return TT_BinaryOperator;
+ }
// This catches some cases where evaluation order is used as control flow:
// aaa && aaa->f();
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 43485298371294..be025dab86fafa 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -280,6 +280,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_BinaryOperator);
EXPECT_TOKEN(Tokens[27], tok::ampamp, TT_BinaryOperator);
+ Tokens = annotate("foo = *i < *j && *j > *k;");
+ EXPECT_EQ(Tokens.size(), 15u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::less, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[7], tok::ampamp, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[10], tok::greater, TT_BinaryOperator);
+
FormatStyle Style = getLLVMStyle();
Style.TypeNames.push_back("MYI");
Tokens = annotate("if (MYI *p{nullptr})", Style);
|
Member
|
@llvm/pr-subscribers-clang-format ChangesFixes #65877.Full diff: https://github.com/llvm/llvm-project/pull/65933.diff 2 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index e925bee44cd0c6a..142168e074bbc27 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2598,9 +2598,12 @@ class AnnotatingParser {
if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
return TT_BinaryOperator;
- // "&&(" is quite unlikely to be two successive unary "&".
- if (Tok.is(tok::ampamp) && NextToken->is(tok::l_paren))
+ // "&&" followed by "(", "*", or "&" is quite unlikely to be two successive
+ // unary "&".
+ if (Tok.is(tok::ampamp) &&
+ NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
return TT_BinaryOperator;
+ }
// This catches some cases where evaluation order is used as control flow:
// aaa && aaa->f();
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 434852983712940..be025dab86fafa5 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -280,6 +280,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_BinaryOperator);
EXPECT_TOKEN(Tokens[27], tok::ampamp, TT_BinaryOperator);
+ Tokens = annotate("foo = *i < *j && *j > *k;");
+ EXPECT_EQ(Tokens.size(), 15u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::less, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[7], tok::ampamp, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[10], tok::greater, TT_BinaryOperator);
+
FormatStyle Style = getLLVMStyle();
Style.TypeNames.push_back("MYI");
Tokens = annotate("if (MYI *p{nullptr})", Style);
|
&& enclosed in < and >
rymiel
approved these changes
Sep 11, 2023
Member
rymiel
left a comment
There was a problem hiding this comment.
I'm also suspicious of if a dereference like *j should ever be legal in a template argument, but that would be a different case
&& enclosed in < and >&& followed by * or &
86b8369 to
70aafbc
Compare
mydeveloperday
approved these changes
Sep 14, 2023
kstoimenov
pushed a commit
to kstoimenov/llvm-project
that referenced
this pull request
Sep 14, 2023
This was referenced Sep 14, 2023
ZijunZhaoCCK
pushed a commit
to ZijunZhaoCCK/llvm-project
that referenced
this pull request
Sep 19, 2023
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
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 #65877.