Skip to content

Commit d403c70

Browse files
authored
[clang-format] Annotate tok::star in a*b*c as BinaryOperator (#137433)
Fix #137400
1 parent ae07f48 commit d403c70

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

clang/lib/Format/TokenAnnotator.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -3095,10 +3095,14 @@ class AnnotatingParser {
30953095

30963096
// This catches some cases where evaluation order is used as control flow:
30973097
// aaa && aaa->f();
3098+
// Or expressions like:
3099+
// width * height * length
30983100
if (NextToken->Tok.isAnyIdentifier()) {
30993101
const FormatToken *NextNextToken = NextToken->getNextNonComment();
3100-
if (NextNextToken && NextNextToken->is(tok::arrow))
3102+
if (NextNextToken && (NextNextToken->is(tok::arrow) ||
3103+
NextNextToken->isPointerOrReference())) {
31013104
return TT_BinaryOperator;
3105+
}
31023106
}
31033107

31043108
// It is very unlikely that we are going to find a pointer or reference type

clang/unittests/Format/TokenAnnotatorTest.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
395395
EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_FunctionDeclarationName);
396396
EXPECT_TOKEN(Tokens[5], tok::star, TT_PointerOrReference);
397397
EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen);
398+
399+
Tokens = annotate("int8_t *a = MacroCall(int8_t, width * height * length);");
400+
ASSERT_EQ(Tokens.size(), 16u) << Tokens;
401+
EXPECT_TOKEN(Tokens[9], tok::star, TT_BinaryOperator);
402+
EXPECT_TOKEN(Tokens[11], tok::star, TT_BinaryOperator);
398403
}
399404

400405
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {

0 commit comments

Comments
 (0)