[clang] [clang-format] Handle sizeof in C compound literals (PR #187633)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Mar 19 22:48:16 PDT 2026
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/187633
Fixes #168237
>From ba0c47f2f679423b641b14859b4b68e34ebbfc68 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 19 Mar 2026 22:46:50 -0700
Subject: [PATCH] [clang-format] Handle sizeof in C compound literals
Fixes #168237
---
clang/lib/Format/TokenAnnotator.cpp | 11 +++++++++++
clang/unittests/Format/FormatTest.cpp | 2 ++
2 files changed, 13 insertions(+)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 55a6d1a9427f9..ee14469cfc968 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -4991,6 +4991,17 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
spaceRequiredBeforeParens(Right);
}
}
+ auto CompoundLiteral = [](const FormatToken &Tok) {
+ if (Tok.isNot(tok::l_paren))
+ return false;
+ const auto *RParen = Tok.MatchingParen;
+ if (!RParen)
+ return false;
+ const auto *Next = RParen->Next;
+ return Next && Next->is(tok::l_brace) && Next->is(BK_BracedInit);
+ };
+ if (Left.is(tok::kw_sizeof) && CompoundLiteral(Right))
+ return true;
// Handle builtins like identifiers.
if (Line.Type != LT_PreprocessorDirective &&
(Left.Tok.getIdentifierInfo() || Left.is(tok::r_paren))) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 7628195e74a77..f88f930bfd23f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -17532,6 +17532,8 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeParens) {
"#elif ((a || b) && c) || d\n"
"#endif",
NoSpace);
+ // Space between sizeof and C compound literal.
+ verifyFormat("a = sizeof (int){};", NoSpace);
FormatStyle Space = getLLVMStyle();
Space.SpaceBeforeParens = FormatStyle::SBPO_Always;
More information about the cfe-commits
mailing list