-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[clang-format] Fix a bug in annotating binary operator && #138633
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
Conversation
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFix #138485 Full diff: https://github.com/llvm/llvm-project/pull/138633.diff 2 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index caf386cffd25b..65c651809e7d0 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3093,10 +3093,12 @@ class AnnotatingParser {
if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
return TT_BinaryOperator;
- // "&&" followed by "*" or "&" is quite unlikely to be two successive unary
- // "&".
- if (Tok.is(tok::ampamp) && NextToken->isOneOf(tok::star, tok::amp))
+ // "&&" 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 0fb64ceec5c97..03a366469558a 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -754,6 +754,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsNonTemplateAngleBrackets) {
ASSERT_EQ(Tokens.size(), 27u) << Tokens;
EXPECT_TOKEN(Tokens[7], tok::less, TT_BinaryOperator);
EXPECT_TOKEN(Tokens[20], tok::greater, TT_BinaryOperator);
+
+ Tokens = annotate("bool foo = a < b && (c * d) > e;");
+ ASSERT_EQ(Tokens.size(), 16u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::less, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[6], tok::ampamp, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[9], tok::star, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[12], tok::greater, TT_BinaryOperator);
}
TEST_F(TokenAnnotatorTest, UnderstandsTemplateTemplateParameters) {
|
@owenca, I actually think that this introduced a regression that was previously fixed in the nightly snapshot of
With
But now, we are back to the wrong formatting of |
It was an incidental "fix" by 91328db, which caused a regression that's fixed by this patch.
Please open an issue for this. |
Done, cf. #139376. |
Fix #138485