[clang-format] Correctly annotate binary stars in braced init lists#186732
Merged
[clang-format] Correctly annotate binary stars in braced init lists#186732
Conversation
Member
|
@llvm/pr-subscribers-clang-format Author: owenca (owenca) ChangesFixes #175241 Full diff: https://github.com/llvm/llvm-project/pull/186732.diff 2 Files Affected:
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;
|
HazardyKnusperkeks
approved these changes
Mar 16, 2026
neonetizen
pushed a commit
to neonetizen/llvm-project
that referenced
this pull request
Mar 17, 2026
Contributor
|
/cherry-pick 9d1d7a5 |
Member
|
/pull-request #188330 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #175241