[clang] [clang-format] Annotate tok::star in a*b*c as BinaryOperator (PR #137433)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 25 19:26:57 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/137433
Fix #137400
>From 62b436a3b71dd7109977b95afeda3db15e37d74c Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 25 Apr 2025 19:24:44 -0700
Subject: [PATCH] [clang-format] Annotate tok::star in a*b*c as BinaryOperator
Fix #137400
---
clang/lib/Format/TokenAnnotator.cpp | 6 +++++-
clang/unittests/Format/TokenAnnotatorTest.cpp | 5 +++++
2 files changed, 10 insertions(+), 1 deletion(-)
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) {
More information about the cfe-commits
mailing list