Skip to content

Commit 791637e

Browse files
authored
[clang-format] Don't allow casts in front of ampamp (#77704)
clang-format performs a heuristic to see if a right parenthesis is the parenthesis of a cast expression. This check never looked ahead to see if the next token is an ampamp && token. This resulted in the paren being set as an CastRParen, and the following ampamp got annotated as an UnaryOperator! Since && can never be a unary operator is standard C++, this patch forbids the right paren from ever becoming a cast. Fixes #77680
1 parent 17c062c commit 791637e

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2375,7 +2375,7 @@ class AnnotatingParser {
23752375
}
23762376
}
23772377

2378-
if (Tok.Next->is(tok::question))
2378+
if (Tok.Next->isOneOf(tok::question, tok::ampamp))
23792379
return false;
23802380

23812381
// `foreach((A a, B b) in someList)` should not be seen as a cast.

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
10661066
EXPECT_EQ(Tokens.size(), 17u) << Tokens;
10671067
EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
10681068
EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
1069+
1070+
Tokens = annotate("template <typename T>\n"
1071+
"concept C = (!Foo<T>) && Bar;");
1072+
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1073+
EXPECT_TOKEN(Tokens[15], tok::ampamp, TT_BinaryOperator);
10691074
}
10701075

10711076
TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {

0 commit comments

Comments
 (0)