[clang] d403c70 - [clang-format] Annotate tok::star in a*b*c as BinaryOperator (#137433)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Apr 26 12:55:03 PDT 2025
Author: Owen Pan
Date: 2025-04-26T12:55:00-07:00
New Revision: d403c70ff31830bf5311be11b51363ae98d06068
URL: https://github.com/llvm/llvm-project/commit/d403c70ff31830bf5311be11b51363ae98d06068
DIFF: https://github.com/llvm/llvm-project/commit/d403c70ff31830bf5311be11b51363ae98d06068.diff
LOG: [clang-format] Annotate tok::star in a*b*c as BinaryOperator (#137433)
Fix #137400
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 3e17c688dbcce..e56cc92987af7 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3095,10 +3095,14 @@ class AnnotatingParser {
// This catches some cases where evaluation order is used as control flow:
// aaa && aaa->f();
+ // Or expressions like:
+ // width * height * length
if (NextToken->Tok.isAnyIdentifier()) {
const FormatToken *NextNextToken = NextToken->getNextNonComment();
- if (NextNextToken && NextNextToken->is(tok::arrow))
+ if (NextNextToken && (NextNextToken->is(tok::arrow) ||
+ NextNextToken->isPointerOrReference())) {
return TT_BinaryOperator;
+ }
}
// It is very unlikely that we are going to find a pointer or reference type
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 08216dd971ac4..844f51f9e7c2e 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -395,6 +395,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_FunctionDeclarationName);
EXPECT_TOKEN(Tokens[5], tok::star, TT_PointerOrReference);
EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen);
+
+ Tokens = annotate("int8_t *a = MacroCall(int8_t, width * height * length);");
+ ASSERT_EQ(Tokens.size(), 16u) << Tokens;
+ EXPECT_TOKEN(Tokens[9], tok::star, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[11], tok::star, TT_BinaryOperator);
}
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
More information about the cfe-commits
mailing list