Skip to content

Commit 0b2c88e

Browse files
authored
[clang-format] Fix a bug in annotating && followed by * or & (#65933)
Fixes #65877.
1 parent 660876a commit 0b2c88e

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,9 +2598,12 @@ class AnnotatingParser {
25982598
if (InTemplateArgument && NextToken->Tok.isAnyIdentifier())
25992599
return TT_BinaryOperator;
26002600

2601-
// "&&(" is quite unlikely to be two successive unary "&".
2602-
if (Tok.is(tok::ampamp) && NextToken->is(tok::l_paren))
2601+
// "&&" followed by "(", "*", or "&" is quite unlikely to be two successive
2602+
// unary "&".
2603+
if (Tok.is(tok::ampamp) &&
2604+
NextToken->isOneOf(tok::l_paren, tok::star, tok::amp)) {
26032605
return TT_BinaryOperator;
2606+
}
26042607

26052608
// This catches some cases where evaluation order is used as control flow:
26062609
// aaa && aaa->f();

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
280280
EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_BinaryOperator);
281281
EXPECT_TOKEN(Tokens[27], tok::ampamp, TT_BinaryOperator);
282282

283+
Tokens = annotate("foo = *i < *j && *j > *k;");
284+
EXPECT_EQ(Tokens.size(), 15u) << Tokens;
285+
EXPECT_TOKEN(Tokens[4], tok::less, TT_BinaryOperator);
286+
EXPECT_TOKEN(Tokens[7], tok::ampamp, TT_BinaryOperator);
287+
EXPECT_TOKEN(Tokens[10], tok::greater, TT_BinaryOperator);
288+
283289
FormatStyle Style = getLLVMStyle();
284290
Style.TypeNames.push_back("MYI");
285291
Tokens = annotate("if (MYI *p{nullptr})", Style);

0 commit comments

Comments
 (0)