[clang-format] Fix a bug in annotating arrows after init braces#119958
Merged
Conversation
Member
|
@llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) ChangesFixes #59066. Full diff: https://github.com/llvm/llvm-project/pull/119958.diff 3 Files Affected:
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 05c86db55641f6..667874853152ed 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2403,7 +2403,8 @@ class AnnotatingParser {
// not auto operator->() -> xxx;
Current.setType(TT_TrailingReturnArrow);
} else if (Current.is(tok::arrow) && Current.Previous &&
- Current.Previous->is(tok::r_brace)) {
+ Current.Previous->is(tok::r_brace) &&
+ Current.Previous->is(BK_Block)) {
// Concept implicit conversion constraint needs to be treated like
// a trailing return type ... } -> <type>.
Current.setType(TT_TrailingReturnArrow);
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index de7e261b21d303..654148a161bd7f 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -570,8 +570,9 @@ void UnwrappedLineParser::calculateBraceTypes(bool ExpectClassBody) {
NextTok->isOneOf(Keywords.kw_of, Keywords.kw_in,
Keywords.kw_as));
ProbablyBracedList =
- ProbablyBracedList || (IsCpp && (PrevTok->Tok.isLiteral() ||
- NextTok->is(tok::l_paren)));
+ ProbablyBracedList ||
+ (IsCpp && (PrevTok->Tok.isLiteral() ||
+ NextTok->isOneOf(tok::l_paren, tok::arrow)));
// If there is a comma, semicolon or right paren after the closing
// brace, we assume this is a braced initializer list.
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 38658fcb0e9990..b2fb5227993c3f 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2277,6 +2277,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsTrailingReturnArrow) {
ASSERT_EQ(Tokens.size(), 21u) << Tokens;
EXPECT_TOKEN(Tokens[13], tok::arrow, TT_Unknown);
+ Tokens = annotate("Class<Type>{foo}->func(arg);");
+ ASSERT_EQ(Tokens.size(), 14u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_Unknown); // Not FunctionLBrace
+ EXPECT_BRACE_KIND(Tokens[4], BK_BracedInit);
+ EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
+ EXPECT_TOKEN(Tokens[7], tok::arrow, TT_Unknown);
+
auto Style = getLLVMStyle();
Style.StatementAttributeLikeMacros.push_back("emit");
Tokens = annotate("emit foo()->bar;", Style);
|
mydeveloperday
approved these changes
Dec 18, 2024
This was referenced Jun 2, 2025
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 #59066.