[clang] [clang] Reject a label with no statement as a substatement (PR #244133)
Mohit Mishra via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 3 21:24:52 PST 2026
https://github.com/mohitmishra786 updated https://github.com/llvm/llvm-project/pull/244133
>From d803699f34fd7c0fee04ed7fb094238321fde08b Mon Sep 17 00:00:00 2001
From: Mohit Mishra <immadmohit at gmail.com>
Date: Thu, 19 Nov 2026 15:02:11 +0530
Subject: [PATCH] [clang] Reject a label with no statement as a substatement
A label may be followed directly by '}' only when it is in a compound
statement (C23 6.8.2, C++23 [stmt.block]). The parser checked for the
closing brace without checking where the label was, so it accepted
`if (a) L: }`, `switch (a) case 1: }` and `switch (a) default: }`.
Only take the label-at-end-of-compound path when the statement context
is a compound statement. Otherwise fall through to parsing the
substatement, which reports "expected statement" like GCC does.
Fixes #237246
---
clang/docs/ReleaseNotes.md | 1 +
clang/lib/Parse/ParseStmt.cpp | 12 +++++++++---
clang/test/Parser/c2x-label.c | 18 ++++++++++++++++++
clang/test/Parser/cxx2b-label.cpp | 18 ++++++++++++++++++
4 files changed, 46 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 9af260e7ff4769..67c191e33a833d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1037,6 +1037,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed `#embed` of a missing file not being diagnosed if `-MD` is used. (#GH247359)
- Fixed `__has_embed` return value with `limit` greater than the file size. (#GH247344)
- Fixed a C2y rejects-valid where only a single `label:` per loop can be used with `break label;`. (#GH246505)
+- Clang no longer accepts a label, `case` or `default` label with no following statement as the substatement of an `if`, `switch` or loop, such as `if (a) L: }`. A label may only be followed directly by `}` when it appears in a compound statement. (#GH237246)
#### Bug Fixes to Compiler Builtins
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index e6f14d180524b7..f3356e93c9b767 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -775,7 +775,9 @@ Parser::ParseLabeledStatement(ParsedAttributes &Attrs,
}
// The label may have no statement following it
- if (SubStmt.isUnset() && Tok.is(tok::r_brace)) {
+ if (SubStmt.isUnset() && Tok.is(tok::r_brace) &&
+ (StmtCtx & ParsedStmtContext::AllowDeclarationsInC) !=
+ ParsedStmtContext()) {
DiagnoseLabelAtEndOfCompoundStatement();
SubStmt = Actions.ActOnNullStmt(ColonLoc);
}
@@ -936,7 +938,9 @@ Parser::ParseCaseStatement(ParsedStmtContext StmtCtx, bool MissingCase,
// If we found a non-case statement, start by parsing it.
StmtResult SubStmt;
- if (Tok.is(tok::r_brace)) {
+ if (Tok.is(tok::r_brace) &&
+ (StmtCtx & ParsedStmtContext::AllowDeclarationsInC) !=
+ ParsedStmtContext()) {
// "switch (X) { case 4: }", is valid and is treated as if label was
// followed by a null statement.
DiagnoseLabelAtEndOfCompoundStatement();
@@ -988,7 +992,9 @@ Parser::ParseDefaultStatement(ParsedStmtContext StmtCtx,
StmtResult SubStmt;
- if (Tok.is(tok::r_brace)) {
+ if (Tok.is(tok::r_brace) &&
+ (StmtCtx & ParsedStmtContext::AllowDeclarationsInC) !=
+ ParsedStmtContext()) {
// "switch (X) {... default: }", is valid and is treated as if label was
// followed by a null statement.
DiagnoseLabelAtEndOfCompoundStatement();
diff --git a/clang/test/Parser/c2x-label.c b/clang/test/Parser/c2x-label.c
index f7fcd6e524637c..a9892e00807298 100644
--- a/clang/test/Parser/c2x-label.c
+++ b/clang/test/Parser/c2x-label.c
@@ -28,3 +28,21 @@ int test_label_in_switch(int v) {
return 0;
}
+
+// A label that is the substatement of a selection or iteration statement is
+// not at the end of a compound statement, so it still needs a statement.
+void test_label_as_substatement(int v) {
+ if (v) L1:
+} // c17-error {{expected statement}} c2x-error {{expected statement}}
+
+void test_case_as_substatement(int v) {
+ switch (v) case 1:
+} // c17-error {{expected statement}} c2x-error {{expected statement}}
+
+void test_default_as_substatement(int v) {
+ switch (v) default:
+} // c17-error {{expected statement}} c2x-error {{expected statement}}
+
+void test_label_as_loop_body(int v) {
+ while (v) L2:
+} // c17-error {{expected statement}} c2x-error {{expected statement}}
diff --git a/clang/test/Parser/cxx2b-label.cpp b/clang/test/Parser/cxx2b-label.cpp
index a490370409cc0c..38ddf00afd8e95 100644
--- a/clang/test/Parser/cxx2b-label.cpp
+++ b/clang/test/Parser/cxx2b-label.cpp
@@ -29,3 +29,21 @@ int test_label_in_switch(int v) {
return 0;
}
+
+// A label that is the substatement of a selection or iteration statement is
+// not at the end of a compound statement, so it still needs a statement.
+void test_label_as_substatement(int v) {
+ if (v) L1:
+} // expected-error {{expected statement}}
+
+void test_case_as_substatement(int v) {
+ switch (v) case 1:
+} // expected-error {{expected statement}}
+
+void test_default_as_substatement(int v) {
+ switch (v) default:
+} // expected-error {{expected statement}}
+
+void test_label_as_loop_body(int v) {
+ while (v) L2:
+} // expected-error {{expected statement}}
More information about the cfe-commits
mailing list