https://github.com/Glitch-On updated https://github.com/llvm/llvm-project/pull/240605
>From 0ada78282c687c145c818ee524ee3256bb0d49c9 Mon Sep 17 00:00:00 2001 From: Vansh <[email protected]> Date: Tue, 10 Nov 2026 04:29:31 +0530 Subject: [PATCH 1/3] [clang-format] Fix unformatted nested designated initializers The value of a designated initializer, such as `.a = {` or `.a = T{`, was treated as a nested block inlined into its parent, like a JavaScript closure passed as a function argument. A break after its `{` then set NoLineBreak on every enclosing scope. The enclosing braced list still needed breaks before its remaining elements and its closing brace, so no state was left and the line was kept as written. LLVM style got around this by breaking after the `=`, which is why it puts the `{` on its own line below `.a =`. With BreakBeforeBinaryOperators: All (used by WebKit), a break after `=` is not allowed, so nothing was formatted. Don't treat the value of a designated initializer as an inlined block. Each designated initializer already goes on its own line. Fixes #201736 Fixes #237142 --- clang/lib/Format/ContinuationIndenter.cpp | 28 +++++++++ clang/unittests/Format/FormatTest.cpp | 71 +++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index 384bad64923150..b6e5c358458bf6 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -1803,9 +1803,37 @@ ContinuationIndenter::getNewLineColumn(const LineState &State) { return CurrentState.Indent; } +// Returns true if \p Tok is the "=" of a designated initializer, e.g. in +// ".a = ...", "[0] = ..." or ".a[0].b = ...". +static bool isDesignatedInitializerEqual(const FormatToken &Tok) { + if (Tok.isNot(tok::equal)) + return false; + // Only the first token of a designator is annotated, so walk back to it. + for (const auto *Prev = Tok.getPreviousNonComment(); Prev; + Prev = Prev->getPreviousNonComment()) { + if (Prev->is(tok::r_square)) { + Prev = Prev->MatchingParen; + if (!Prev) + return false; + } + if (Prev->isOneOf(TT_DesignatedInitializerPeriod, + TT_DesignatedInitializerLSquare)) { + return true; + } + if (Prev->isNoneOf(tok::identifier, tok::period, tok::l_square)) + return false; + } + return false; +} + static bool hasNestedBlockInlined(const FormatToken *Previous, const FormatToken &Current, const FormatStyle &Style) { + // The value of a designated initializer is not inlined into the enclosing + // braced list, which puts each designated initializer on its own line anyway. + // Treating it as inlined would forbid breaking the rest of that list. + if (isDesignatedInitializerEqual(*Previous)) + return false; if (Previous->isNot(tok::l_paren)) return true; if (Previous->ParameterCount > 1) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 4c27da647e65ad..0454ae08de48e8 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5395,6 +5395,77 @@ TEST_F(FormatTest, DesignatedInitializers) { " },\n" " }) {\n" "}"); + + constexpr StringRef Nested("S s = {\n" + " .a = {\n" + " .x = 1 + 2,\n" + " .y = (3 * 4),\n" + " },\n" + " [0] = {\n" + " .x = 1,\n" + " },\n" + " .b.c[1] = {\n" + " .x = 1,\n" + " },\n" + " .f = []() {\n" + " int x = 0;\n" + " return x;\n" + " },\n" + "};"); + verifyFormat(Nested); + + auto Style = getLLVMStyle(); + Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; + verifyFormat(Nested, Style); + verifyFormat("void foo() {\n" + " Outer x = {\n" + " .inner = {\n" + " .a = 1 + 2,\n" + " .b = (3 * 4),\n" + " },\n" + " };\n" + "}", + "void foo() {\n" + " Outer x = {\n" + " .inner = {\n" + ".a = 1 + 2,\n" + " .b = ( 3*4 ),\n" + " },\n" + " };\n" + "}", + Style); + + Style = getWebKitStyle(); + Style.ColumnLimit = 120; + verifyFormat("Source make()\n" + "{\n" + " return Source {\n" + " .layout = Layout {\n" + " .a = 1,\n" + " .b = 2,\n" + " },\n" + " .count = 3,\n" + " .fn = []() {\n" + " int x = 0;\n" + " x = x + 1;\n" + " },\n" + " };\n" + "}", + "Source make()\n" + "{\n" + " return Source{\n" + " .layout = Layout{\n" + " .a = 1,\n" + " .b = 2,\n" + " },\n" + " .count = 3,\n" + " .fn = []() {\n" + "int x = 0;\n" + " x = x + 1;\n" + " },\n" + " };\n" + "}", + Style); } TEST_F(FormatTest, BracedInitializerIndentWidth) { >From ac4f2e5d842dbc31159a4616c95d76dcd74e8aa4 Mon Sep 17 00:00:00 2001 From: Vansh <[email protected]> Date: Tue, 17 Nov 2026 01:54:56 +0530 Subject: [PATCH 2/3] [clang-format] Drop redundant test input in DesignatedInitializers The one-argument verifyFormat already messes up the input before formatting, so the separate test with hand-mangled input from #237142 adds nothing over verifyFormat(Nested, Style). --- clang/unittests/Format/FormatTest.cpp | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 0454ae08de48e8..e7fad1eca260ee 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5417,23 +5417,6 @@ TEST_F(FormatTest, DesignatedInitializers) { auto Style = getLLVMStyle(); Style.BreakBeforeBinaryOperators = FormatStyle::BOS_All; verifyFormat(Nested, Style); - verifyFormat("void foo() {\n" - " Outer x = {\n" - " .inner = {\n" - " .a = 1 + 2,\n" - " .b = (3 * 4),\n" - " },\n" - " };\n" - "}", - "void foo() {\n" - " Outer x = {\n" - " .inner = {\n" - ".a = 1 + 2,\n" - " .b = ( 3*4 ),\n" - " },\n" - " };\n" - "}", - Style); Style = getWebKitStyle(); Style.ColumnLimit = 120; >From 639a9a5a582a1de5d5492e992af3f64d08ab91ad Mon Sep 17 00:00:00 2001 From: Vansh <[email protected]> Date: Tue, 17 Nov 2026 02:20:26 +0530 Subject: [PATCH 3/3] [clang-format] Use one-string verifyFormat in WebKit test verifyFormat already runs a messed-up copy of the code through the formatter, so the hand-written unformatted input was redundant. The test still fails without the fix. --- clang/unittests/Format/FormatTest.cpp | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index e7fad1eca260ee..008272d1fe7fdb 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -5434,20 +5434,6 @@ TEST_F(FormatTest, DesignatedInitializers) { " },\n" " };\n" "}", - "Source make()\n" - "{\n" - " return Source{\n" - " .layout = Layout{\n" - " .a = 1,\n" - " .b = 2,\n" - " },\n" - " .count = 3,\n" - " .fn = []() {\n" - "int x = 0;\n" - " x = x + 1;\n" - " },\n" - " };\n" - "}", Style); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
