[clang] [clang-format] Fix a bug in annotating braces (PR #134039)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 1 23:55:54 PDT 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/134039
Fix #133873
>From 50f03ef8b041cacba171ce0cfb156e4f677a9353 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 1 Apr 2025 23:54:33 -0700
Subject: [PATCH] [clang-format] Fix a bug in annotating braces
Fix #133873
---
clang/lib/Format/UnwrappedLineParser.cpp | 6 +++++-
clang/unittests/Format/TokenAnnotatorTest.cpp | 5 +++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index f7712bea01c2c..213b706807b2a 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1887,8 +1887,11 @@ void UnwrappedLineParser::parseStructuralElement(
if (FormatTok->isBinaryOperator())
nextToken();
break;
- case tok::caret:
+ case tok::caret: {
+ const auto *Prev = FormatTok->getPreviousNonComment();
nextToken();
+ if (Prev && Prev->is(tok::identifier))
+ break;
// Block return type.
if (FormatTok->Tok.isAnyIdentifier() || FormatTok->isTypeName(LangOpts)) {
nextToken();
@@ -1903,6 +1906,7 @@ void UnwrappedLineParser::parseStructuralElement(
if (FormatTok->is(tok::l_brace))
parseChildBlock();
break;
+ }
case tok::l_brace:
if (InRequiresExpression)
FormatTok->setFinalizedType(TT_BracedListLBrace);
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index af9fd574b068c..7e0af1c7b4c36 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3622,6 +3622,11 @@ TEST_F(TokenAnnotatorTest, BraceKind) {
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_BRACE_KIND(Tokens[7], BK_BracedInit);
EXPECT_BRACE_KIND(Tokens[9], BK_BracedInit);
+
+ Tokens = annotate("return lhs ^ Byte{rhs};");
+ ASSERT_EQ(Tokens.size(), 9u) << Tokens;
+ EXPECT_BRACE_KIND(Tokens[4], BK_BracedInit);
+ EXPECT_BRACE_KIND(Tokens[6], BK_BracedInit);
}
TEST_F(TokenAnnotatorTest, UnderstandsElaboratedTypeSpecifier) {
More information about the cfe-commits
mailing list