[clang] [clang-format] Add BraceWrapping.AfterGotoLabel (PR #238785)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 4 05:48:43 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Asuka (akasakariko)
<details>
<summary>Changes</summary>
Add `BraceWrapping.AfterGotoLabel` so a custom style can format a goto label as `label: {` again
Default the option to `true` to retain the wrapping behavior introduced by #<!-- -->197538, and keep it independent of `AfterCaseLabel` and `IndentCaseBlocks`
```yaml
BreakBeforeBraces: Custom
BraceWrapping:
AfterGotoLabel: false
```
AI assistance was used to prepare this change
Validated on macOS arm64 with assertions enabled
- The regression fails before the parser change and passes afterward
- All 1312 `FormatTests` tests pass
- All 33 tests in `clang/test/Format` pass
- `clang-format-check-format` passes for all 90 source files
- The original reproducer matches the clang-format 22 output with the new option, including idempotence and YAML configuration round-trip checks
Fixes #<!-- -->232019
---
Full diff: https://github.com/llvm/llvm-project/pull/238785.diff
7 Files Affected:
- (modified) clang/docs/ClangFormatStyleOptions.md (+13)
- (modified) clang/docs/ReleaseNotes.md (+3)
- (modified) clang/include/clang/Format/Format.h (+12)
- (modified) clang/lib/Format/Format.cpp (+4)
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+5-1)
- (modified) clang/unittests/Format/ConfigParseTest.cpp (+1)
- (modified) clang/unittests/Format/FormatTest.cpp (+27)
``````````diff
diff --git a/clang/docs/ClangFormatStyleOptions.md b/clang/docs/ClangFormatStyleOptions.md
index 8e9ecbbc09c7df..2ccfcc7dc9b77c 100644
--- a/clang/docs/ClangFormatStyleOptions.md
+++ b/clang/docs/ClangFormatStyleOptions.md
@@ -2528,6 +2528,19 @@ the configuration (without a prefix: `Auto`).
}
```
+ - `bool AfterGotoLabel` Wrap the opening brace after a goto label. Defaults to `true`.
+
+ ```c++
+ true: false:
+ void f() { vs. void f() {
+ label: label: {
+ { foo();
+ foo(); bar();
+ bar(); }
+ } }
+ }
+ ```
+
- `bool AfterNamespace` Wrap namespace definitions.
```c++
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 409d2459b0580b..b88c3c7b29c098 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1422,6 +1422,9 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
### clang-format
+- Add `AfterGotoLabel` to `BraceWrapping` to allow attaching an opening brace to
+ a goto label when `BreakBeforeBraces` is `Custom`. It defaults to `true` to
+ preserve the existing wrapped layout.
- Add `SpacesInBlockComments` option to control spacing after `/*` and
before `*/` in ordinary block comments.
- Add `AfterRequiresExpression` sub-option of `BraceWrapping` to wrap the
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 1f29dd630b8020..2a01782402204a 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -1480,6 +1480,18 @@ struct FormatStyle {
/// }
/// \endcode
bool AfterFunction;
+ /// Wrap the opening brace after a goto label. Defaults to `true`.
+ /// \code
+ /// true: false:
+ /// void f() { vs. void f() {
+ /// label: label: {
+ /// { foo();
+ /// foo(); bar();
+ /// bar(); }
+ /// } }
+ /// }
+ /// \endcode
+ bool AfterGotoLabel;
/// Wrap namespace definitions.
/// \code
/// true:
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index f922310c7fcd39..5f82dc473e5090 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -224,6 +224,7 @@ template <> struct MappingTraits<FormatStyle::BraceWrappingFlags> {
IO.mapOptional("AfterExportBlock", Wrapping.AfterExportBlock);
IO.mapOptional("AfterExternBlock", Wrapping.AfterExternBlock);
IO.mapOptional("AfterFunction", Wrapping.AfterFunction);
+ IO.mapOptional("AfterGotoLabel", Wrapping.AfterGotoLabel);
IO.mapOptional("AfterNamespace", Wrapping.AfterNamespace);
IO.mapOptional("AfterObjCDeclaration", Wrapping.AfterObjCDeclaration);
IO.mapOptional("AfterRequiresExpression", Wrapping.AfterRequiresExpression);
@@ -1745,6 +1746,7 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
/*AfterControlStatement=*/FormatStyle::BWACS_Never,
/*AfterEnum=*/false,
/*AfterFunction=*/false,
+ /*AfterGotoLabel=*/true,
/*AfterNamespace=*/false,
/*AfterObjCDeclaration=*/false,
/*AfterRequiresExpression=*/false,
@@ -1821,6 +1823,7 @@ static void expandPresetsBraceWrapping(FormatStyle &Expanded) {
/*AfterControlStatement=*/FormatStyle::BWACS_Always,
/*AfterEnum=*/true,
/*AfterFunction=*/true,
+ /*AfterGotoLabel=*/true,
/*AfterNamespace=*/true,
/*AfterObjCDeclaration=*/true,
/*AfterRequiresExpression=*/true,
@@ -1925,6 +1928,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
/*AfterControlStatement=*/FormatStyle::BWACS_Never,
/*AfterEnum=*/false,
/*AfterFunction=*/false,
+ /*AfterGotoLabel=*/true,
/*AfterNamespace=*/false,
/*AfterObjCDeclaration=*/false,
/*AfterRequiresExpression=*/false,
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 27974cf25cc2ea..eda45d351c7480 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3557,8 +3557,12 @@ void UnwrappedLineParser::parseLabel(bool IsGotoLabel) {
--Level;
}
- if (!IsGotoLabel && !Style.IndentCaseBlocks &&
+ if (IsGotoLabel && !Style.BraceWrapping.AfterGotoLabel &&
CommentsBeforeNextToken.empty() && FormatTok->is(tok::l_brace)) {
+ parseBlock();
+ addUnwrappedLine();
+ } else if (!IsGotoLabel && !Style.IndentCaseBlocks &&
+ CommentsBeforeNextToken.empty() && FormatTok->is(tok::l_brace)) {
CompoundStatementIndenter Indenter(this, Level,
Style.BraceWrapping.AfterCaseLabel,
Style.BraceWrapping.IndentBraces);
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index 0ca02058a24948..a621444b989726 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -240,6 +240,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterEnum);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterExportBlock);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterFunction);
+ CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterGotoLabel);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterNamespace);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterObjCDeclaration);
CHECK_PARSE_NESTED_BOOL(BraceWrapping, AfterRequiresExpression);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index da1587641311bc..1da34dc20f844e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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"
+ " foo();\n"
+ " bar();\n"
+ "}\n"
+ " break;\n"
+ " }\n"
+ "}",
+ Style);
+ verifyFormat("void f() {\n"
+ "label:\n"
+ " // Comment\n"
+ " {\n"
+ " foo();\n"
+ " bar();\n"
+ " }\n"
+ "}",
+ Style);
}
TEST_F(FormatTest, MultiLineControlStatements) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/238785
More information about the cfe-commits
mailing list