Skip to content

Commit

Permalink
[clang-format] Fix a bug in annotating && followed by * or &
Browse files Browse the repository at this point in the history
Fixes llvm#65877.
  • Loading branch information
owenca committed Sep 11, 2023
1 parent 00add6e commit 70aafbc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 70aafbc

Please sign in to comment.