https://github.com/owenca created https://github.com/llvm/llvm-project/pull/144095
Stop looking for function decls after hitting a BK_BracedInit l_brace. Fixes #144057. >From 8fa1829548708152118b8f24b9069580f92a750d Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Fri, 13 Jun 2025 08:30:21 -0700 Subject: [PATCH] [clang-format] Fix a bug in annotating braces Stop looking for function decls after hitting a BK_BracedInit l_brace. Fixes #144057. --- clang/lib/Format/TokenAnnotator.cpp | 2 +- clang/unittests/Format/TokenAnnotatorTest.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index aed1672afac66..d2f8b2703a9a3 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3978,7 +3978,7 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedLine &Line) const { for (auto *Tok = FirstNonComment && FirstNonComment->isNot(tok::kw_using) ? FirstNonComment->Next : nullptr; - Tok; Tok = Tok->Next) { + Tok && Tok->isNot(BK_BracedInit); Tok = Tok->Next) { if (Tok->is(TT_StartOfName)) SeenName = true; if (Tok->Previous->EndsCppAttributeGroup) diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 873c6c492d18c..a1285e4bc9bf8 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -3754,6 +3754,13 @@ TEST_F(TokenAnnotatorTest, BraceKind) { ASSERT_EQ(Tokens.size(), 9u) << Tokens; EXPECT_BRACE_KIND(Tokens[4], BK_BracedInit); EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit); + + Tokens = annotate("auto f1{&T::operator()};"); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_BRACE_KIND(Tokens[2], BK_BracedInit); + // Not TT_FunctionDeclarationName. + EXPECT_TOKEN(Tokens[6], tok::kw_operator, TT_Unknown); + EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit); } TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits