[clang] [clang-format] Don't annotate C compound literal r_paren (PR #180436)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 9 22:00:34 PST 2026
https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/180436
>From 80331fe1c9e9c9c73d17f9239d0d41e63518f38b Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sun, 8 Feb 2026 12:03:03 -0800
Subject: [PATCH 1/2] [clang-format] Don't annotate C compound literal r_paren
Fixes #180179
---
clang/lib/Format/TokenAnnotator.cpp | 2 +-
clang/unittests/Format/TokenAnnotatorTest.cpp | 8 ++++++++
2 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 3208d8e28dd86..1803bbbaeeff4 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2921,7 +2921,7 @@ class AnnotatingParser {
if (!AfterRParen->Next)
return false;
- if (AfterRParen->is(tok::l_brace) &&
+ if (Style.Language != FormatStyle::LK_C && AfterRParen->is(tok::l_brace) &&
AfterRParen->getBlockKind() == BK_BracedInit) {
return true;
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index b3e127c3eb6ba..defd78aedfd70 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -964,6 +964,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
EXPECT_TOKEN(Tokens[11], tok::amp, TT_UnaryOperator);
}
+TEST_F(TokenAnnotatorTest, CompoundLiteral) {
+ auto Tokens =
+ annotate("return (struct foo){};", getLLVMStyle(FormatStyle::LK_C));
+ ASSERT_EQ(Tokens.size(), 9u) << Tokens;
+ // Not TT_CastRParen.
+ EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_Unknown);
+}
+
TEST_F(TokenAnnotatorTest, UnderstandsDynamicExceptionSpecifier) {
auto Tokens = annotate("void f() throw(int);");
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
>From 3ffd5d4108e51a2c5046a28f87d880811360c78a Mon Sep 17 00:00:00 2001
From: owenca <owenpiano at gmail.com>
Date: Mon, 9 Feb 2026 22:00:24 -0800
Subject: [PATCH 2/2] Clarify compound literal handling in comments
Add comments to clarify the handling of parentheses and braces.
---
clang/lib/Format/TokenAnnotator.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 1803bbbaeeff4..17f5babf90f30 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2921,6 +2921,8 @@ class AnnotatingParser {
if (!AfterRParen->Next)
return false;
+ // A pair of parentheses before an l_brace in C starts a compound literal
+ // and is not a cast.
if (Style.Language != FormatStyle::LK_C && AfterRParen->is(tok::l_brace) &&
AfterRParen->getBlockKind() == BK_BracedInit) {
return true;
More information about the cfe-commits
mailing list