[clang] [clang-format] Fix parsing of goto labels (PR #197538)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Thu May 14 02:01:18 PDT 2026
https://github.com/HazardyKnusperkeks updated https://github.com/llvm/llvm-project/pull/197538
>From 7548f379bccfe3d736f25e3c6ca9af66544e107e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Wed, 13 May 2026 21:30:37 +0200
Subject: [PATCH 1/2] [clang-format] Fix parsing of goto labels
Fixes #196662.
---
clang/lib/Format/UnwrappedLineParser.cpp | 5 +-
clang/unittests/Format/FormatTest.cpp | 66 +++++++++++++++++-------
2 files changed, 51 insertions(+), 20 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 022fd62ed2bfc..910f3f1836936 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3376,6 +3376,7 @@ void UnwrappedLineParser::parseDoWhile() {
void UnwrappedLineParser::parseLabel(
FormatStyle::IndentGotoLabelStyle IndentGotoLabels) {
+ const bool IsGotoLabel = FormatTok->is(TT_GotoLabelColon);
nextToken();
unsigned OldLineLevel = Line->Level;
@@ -3392,8 +3393,8 @@ void UnwrappedLineParser::parseLabel(
break;
}
- if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() &&
- FormatTok->is(tok::l_brace)) {
+ if (!IsGotoLabel && !Style.IndentCaseBlocks &&
+ CommentsBeforeNextToken.empty() && FormatTok->is(tok::l_brace)) {
CompoundStatementIndenter Indenter(this, Line->Level,
Style.BraceWrapping.AfterCaseLabel,
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 4245bd1c58153..cb2024a05997f 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3139,14 +3139,18 @@ TEST_F(FormatTest, FormatsLabels) {
"}");
verifyFormat("{\n"
" some_code();\n"
- "test_label: { some_other_code(); }\n"
+ "test_label:\n"
+ " {\n"
+ " some_other_code();\n"
+ " }\n"
"}");
verifyFormat("{\n"
" some_code();\n"
- "test_label: {\n"
- " some_other_code();\n"
- " some_other_code();\n"
- "}\n"
+ "test_label:\n"
+ " {\n"
+ " some_other_code();\n"
+ " some_other_code();\n"
+ " }\n"
"}");
verifyFormat("{\n"
"L0:\n"
@@ -3155,10 +3159,11 @@ TEST_F(FormatTest, FormatsLabels) {
" g();\n"
"}");
verifyFormat("{\n"
- "[[foo]] L1: {\n"
- "[[bar]] [[baz]] L2:\n"
- " g();\n"
- "}\n"
+ "[[foo]] L1:\n"
+ " {\n"
+ " [[bar]] [[baz]] L2:\n"
+ " g();\n"
+ " }\n"
"}");
verifyFormat("{\n"
"[[foo]] L1:\n"
@@ -3168,6 +3173,25 @@ TEST_F(FormatTest, FormatsLabels) {
" g();\n"
" }\n"
"}");
+ verifyFormat("void func() {\n"
+ "label:\n"
+ " {\n"
+ " // Block\n"
+ " }\n"
+ "}");
+ verifyFormat("void func() {\n"
+ "label: // Comment\n"
+ " {\n"
+ " // Block\n"
+ " }\n"
+ "}");
+ verifyFormat("void func() {\n"
+ "label:\n"
+ " // Comment\n"
+ " {\n"
+ " // Block\n"
+ " }\n"
+ "}");
FormatStyle Style = getLLVMStyle();
Style.IndentGotoLabels = FormatStyle::IGLS_NoIndent;
@@ -3196,7 +3220,10 @@ TEST_F(FormatTest, FormatsLabels) {
Style);
verifyFormat("{\n"
" some_code();\n"
- "test_label: { some_other_code(); }\n"
+ "test_label:\n"
+ " {\n"
+ " some_other_code();\n"
+ " }\n"
"}",
Style);
verifyFormat("{\n"
@@ -3331,17 +3358,17 @@ TEST_F(FormatTest, FormatsLabels) {
verifyFormat("{\n"
" some_code();\n"
"test_label:\n"
- "{\n"
- " some_other_code();\n"
- "}\n"
+ " {\n"
+ " some_other_code();\n"
+ " }\n"
"}",
Style);
verifyFormat("{\n"
"[[foo]] L1:\n"
- "{\n"
- "[[bar]] [[baz]] L2:\n"
- " g();\n"
- "}\n"
+ " {\n"
+ " [[bar]] [[baz]] L2:\n"
+ " g();\n"
+ " }\n"
"}",
Style);
}
@@ -18572,7 +18599,10 @@ TEST_F(FormatTest, ConfigurableSpaceBeforeColon) {
"}",
CaseStyle);
verifyFormat("switch (x) {\n"
- "goto_label: { break; }\n"
+ "goto_label:\n"
+ " {\n"
+ " break;\n"
+ " }\n"
"default : {\n"
" break;\n"
"}\n"
>From 72d67489c1bf12e196f84289e271506c6ad6a57c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <github at hazardy.de>
Date: Thu, 14 May 2026 11:01:09 +0200
Subject: [PATCH 2/2] Apply suggestions from code review
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Co-authored-by: owenca <owenpiano at gmail.com>
Co-authored-by: Björn Schäpers <github at hazardy.de>
---
clang/lib/Format/UnwrappedLineParser.cpp | 1 -
clang/unittests/Format/FormatTest.cpp | 7 -------
2 files changed, 8 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 910f3f1836936..245345316910e 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3395,7 +3395,6 @@ void UnwrappedLineParser::parseLabel(
if (!IsGotoLabel && !Style.IndentCaseBlocks &&
CommentsBeforeNextToken.empty() && FormatTok->is(tok::l_brace)) {
-
CompoundStatementIndenter Indenter(this, Line->Level,
Style.BraceWrapping.AfterCaseLabel,
Style.BraceWrapping.IndentBraces);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index cb2024a05997f..676b4196a615e 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3185,13 +3185,6 @@ TEST_F(FormatTest, FormatsLabels) {
" // Block\n"
" }\n"
"}");
- verifyFormat("void func() {\n"
- "label:\n"
- " // Comment\n"
- " {\n"
- " // Block\n"
- " }\n"
- "}");
FormatStyle Style = getLLVMStyle();
Style.IndentGotoLabels = FormatStyle::IGLS_NoIndent;
More information about the cfe-commits
mailing list