[clang] [clang-format] Add BraceWrapping.AfterGotoLabel (PR #238785)

via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 5 19:43:01 PST 2026


================
@@ -3473,6 +3473,33 @@ TEST_F(FormatTest, FormatsLabels) {
                "  }\n"
                "}",
                Style);
+
+  Style = getGoogleStyle();
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.AfterGotoLabel = false;
+  Style.BraceWrapping.AfterCaseLabel = true;
+  Style.IndentCaseBlocks = true;
+  Style.IndentGotoLabels = FormatStyle::IGLS_NoIndent;
+  Style.IndentWidth = 4;
+  verifyFormat("void f() {\n"
+               "    while (a) {\n"
+               "label: {\n"
----------------
akasakariko wrote:

No, having `foo()` and `bar()` at 4 spaces and `}` at column 0 inside `while (a)` while `break;` is at 8 spaces is definitely not what a user wants; that was a side effect of calling `parseBlock()` after lowering `Line->Level` for `IGLS_NoIndent`, which only happened to look reasonable at function scope where `OldLineLevel` is 1 (`IGLS_OuterIndent` has a similar issue in nested blocks where the body ends up at the same indent as `break;` and `}` lines up with the `while` closing brace)

One option is what @owenca suggested in #232019, keeping the whole `label: {` block at `OldLineLevel` (and skipping the `IGLS_HalfIndent` offset) so it is indented like `if (cond) {` at every nesting depth, with `label: {` and `}` aligned at `OldLineLevel` and the body at `OldLineLevel + 1`

The alternative is applying `IndentGotoLabels` only to the `label: {` line while keeping the block body at `OldLineLevel + 1` and `}` at `OldLineLevel` (matching the `AfterGotoLabel: true` layout with `{` joined onto `label:`, which is also what `IGLS_HalfIndent` already produces), so braced and unbraced goto labels in the same function still share the `IndentGotoLabels` column, though `label: {` and `}` no longer align vertically under `NoIndent` or `OuterIndent`

Indenting the whole `label: {` block at `OldLineLevel` feels cleaner to me since the braces stay aligned at every nesting level; does that match what you have in mind, or would you prefer keeping `IndentGotoLabels` on `label: {`?


https://github.com/llvm/llvm-project/pull/238785


More information about the cfe-commits mailing list