[clang] deec7fb - [clang-format] Correctly annotate tok::star in braced list (#138389)

via cfe-commits cfe-commits at lists.llvm.org
Sat May 3 14:54:52 PDT 2025


Author: Owen Pan
Date: 2025-05-03T14:54:48-07:00
New Revision: deec7fb42dd27d38b2d2925f5e635f27007a0bd7

URL: https://github.com/llvm/llvm-project/commit/deec7fb42dd27d38b2d2925f5e635f27007a0bd7
DIFF: https://github.com/llvm/llvm-project/commit/deec7fb42dd27d38b2d2925f5e635f27007a0bd7.diff

LOG: [clang-format] Correctly annotate tok::star in braced list (#138389)

Fix #138382

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 ea0086fd49a33..caf386cffd25b 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3103,10 +3103,14 @@ class AnnotatingParser {
     // Or expressions like:
     //   width * height * length
     if (NextToken->Tok.isAnyIdentifier()) {
-      const FormatToken *NextNextToken = NextToken->getNextNonComment();
-      if (NextNextToken && (NextNextToken->is(tok::arrow) ||
-                            NextNextToken->isPointerOrReference())) {
-        return TT_BinaryOperator;
+      auto *NextNextToken = NextToken->getNextNonComment();
+      if (NextNextToken) {
+        if (NextNextToken->is(tok::arrow))
+          return TT_BinaryOperator;
+        if (NextNextToken->isPointerOrReference()) {
+          NextNextToken->setFinalizedType(TT_BinaryOperator);
+          return TT_BinaryOperator;
+        }
       }
     }
 

diff  --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 124fb43dcf5ac..0fb64ceec5c97 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -406,6 +406,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
   ASSERT_EQ(Tokens.size(), 16u) << Tokens;
   EXPECT_TOKEN(Tokens[9], tok::star, TT_BinaryOperator);
   EXPECT_TOKEN(Tokens[11], tok::star, TT_BinaryOperator);
+
+  Tokens = annotate("float foo[3] = {M * H * H, H * M * H, H * H * M};");
+  ASSERT_EQ(Tokens.size(), 27u) << Tokens;
+  EXPECT_TOKEN(Tokens[16], tok::star, TT_BinaryOperator);
+  EXPECT_TOKEN(Tokens[22], tok::star, TT_BinaryOperator);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {


        


More information about the cfe-commits mailing list