release/22.x: [clang-format] Correctly annotate binary stars in braced init lists (#186732)#188330
Open
llvmbot wants to merge 1 commit intollvm:release/22.xfrom
Open
release/22.x: [clang-format] Correctly annotate binary stars in braced init lists (#186732)#188330llvmbot wants to merge 1 commit intollvm:release/22.xfrom
llvmbot wants to merge 1 commit intollvm:release/22.xfrom
Conversation
…lvm#186732) Fixes llvm#175241 (cherry picked from commit 9d1d7a5)
Member
Author
|
@HazardyKnusperkeks What do you think about merging this PR to the release branch? |
Member
Author
|
@llvm/pr-subscribers-clang-format Author: llvmbot ChangesBackport 9d1d7a5 Requested by: @carlosgalvezp Full diff: https://github.com/llvm/llvm-project/pull/188330.diff 2 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index c0d31a1c9e2f4..fe1361bb25693 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1251,13 +1251,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;
}
@@ -3051,7 +3056,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())) {
@@ -3071,8 +3076,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 e449922710029..82b6c7451d622 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;
|
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.
Backport 9d1d7a5
Requested by: @carlosgalvezp