[clang] [clang-format] Annotate tok::star in a*b*c as BinaryOperator (PR #137433)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 25 19:27:30 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fix #<!-- -->137400
---
Full diff: https://github.com/llvm/llvm-project/pull/137433.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+5-1)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+5)
``````````diff
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 8d4aeb7dec89a..d5c98e253ca57 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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/137433
More information about the cfe-commits
mailing list