[clang] [clang-format] Fix a bug in annotating arrows after init braces (PR #119958)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Dec 14 00:36:38 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->59066.
---
Full diff: https://github.com/llvm/llvm-project/pull/119958.diff
3 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+2-1)
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+3-2)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+7)
``````````diff
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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/119958
More information about the cfe-commits
mailing list