[llvm-branch-commits] [clang] 275e5b4 - [clang-format] Don't annotate C compound literal r_paren (#180436)
Cullen Rhodes via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Feb 11 01:57:26 PST 2026
Author: owenca
Date: 2026-02-11T09:57:17Z
New Revision: 275e5b478d5d383147d8d7da294a04649b8be454
URL: https://github.com/llvm/llvm-project/commit/275e5b478d5d383147d8d7da294a04649b8be454
DIFF: https://github.com/llvm/llvm-project/commit/275e5b478d5d383147d8d7da294a04649b8be454.diff
LOG: [clang-format] Don't annotate C compound literal r_paren (#180436)
Fixes #180179
(cherry picked from commit 2302110838463736b0512b4d7fbd94c4694f66d0)
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 3208d8e28dd86..17f5babf90f30 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2921,7 +2921,9 @@ class AnnotatingParser {
if (!AfterRParen->Next)
return false;
- if (AfterRParen->is(tok::l_brace) &&
+ // 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;
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index a04c6bea4d050..8594f60453982 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;
More information about the llvm-branch-commits
mailing list