https://github.com/owenca created https://github.com/llvm/llvm-project/pull/186732
Fixes #175241 >From 3c13318801bf640dc2140f97fb7266f41dc6d220 Mon Sep 17 00:00:00 2001 From: Owen Pan <[email protected]> Date: Sun, 15 Mar 2026 21:09:30 -0700 Subject: [PATCH] [clang-format] Correctly annotate binary stars in braced init lists Fixes #175241 --- clang/lib/Format/TokenAnnotator.cpp | 9 ++++++--- clang/unittests/Format/TokenAnnotatorTest.cpp | 6 ++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index a60e7fa3eb7f4..c32822ce90d1f 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1246,13 +1246,18 @@ class AnnotatingParser { OpeningBrace.overwriteFixedType(TT_DictLiteral); } } + bool IsBracedListComma = false; if (CurrentToken->is(tok::comma)) { if (Style.isJavaScript()) OpeningBrace.overwriteFixedType(TT_DictLiteral); + else + IsBracedListComma = OpeningBrace.is(BK_BracedInit); ++CommaCount; } if (!consumeToken()) return false; + if (IsBracedListComma) + Contexts.back().IsExpression = true; } return true; } @@ -3046,7 +3051,7 @@ class AnnotatingParser { return TT_BinaryOperator; if (NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren, - TT_RequiresClause) || + tok::semi, TT_RequiresClause) || (NextToken->is(tok::kw_noexcept) && !IsExpression) || NextToken->canBePointerOrReferenceQualifier() || (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) { @@ -3066,8 +3071,6 @@ class AnnotatingParser { return TT_PointerOrReference; if (NextToken->is(tok::kw_operator) && !IsExpression) return TT_PointerOrReference; - if (NextToken->isOneOf(tok::comma, tok::semi)) - return TT_PointerOrReference; // After right braces, star tokens are likely to be pointers to struct, // union, or class. diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index defd78aedfd70..4081b9c9b4994 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -416,6 +416,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) { EXPECT_TOKEN(Tokens[16], tok::star, TT_BinaryOperator); EXPECT_TOKEN(Tokens[22], tok::star, TT_BinaryOperator); + Tokens = annotate("Foo foo{bar, bar * bar};"); + ASSERT_EQ(Tokens.size(), 11u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::star, TT_BinaryOperator); + // Not TT_StartOfName. + EXPECT_TOKEN(Tokens[7], tok::identifier, TT_Unknown); + Tokens = annotate("NSError *__autoreleasing *foo;", getLLVMStyle(FormatStyle::LK_ObjC)); ASSERT_EQ(Tokens.size(), 7u) << Tokens; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
