[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:49 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: owenca (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->168237
---
Full diff: https://github.com/llvm/llvm-project/pull/187633.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+11)
- (modified) clang/unittests/Format/FormatTest.cpp (+2)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/187633
More information about the cfe-commits
mailing list