[clang] [clang] Mark labels referenced when used in named break or continue (PR #166033)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 3 17:53:14 PST 2025
https://github.com/camc updated https://github.com/llvm/llvm-project/pull/166033
>From d758fa880bc3f41c18ff71762e7a60d5eac1aafc Mon Sep 17 00:00:00 2001
From: camc <pushy-crop-cartel at duck.com>
Date: Sun, 2 Nov 2025 01:21:16 +0000
Subject: [PATCH 1/2] [clang] Mark labels referenced when used in named break
or continue
---
clang/docs/ReleaseNotes.rst | 3 +++
clang/lib/Sema/SemaStmt.cpp | 4 ++++
clang/test/Sema/labeled-break-continue.c | 18 +++++++++++++++---
3 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 92fc9381a5868..a366a8ec9630c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -390,6 +390,9 @@ Improvements to Clang's diagnostics
that were previously incorrectly accepted in case of other irrelevant
conditions are now consistently diagnosed, identical to C++ mode.
+- Fix false-positive unused label diagnostic when label used in a named break
+ or continue (#GH166013)
+
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index f39896336053e..f6f38d5943fc5 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3315,6 +3315,8 @@ StmtResult Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope,
LabelDecl *Target, SourceLocation LabelLoc) {
Scope *S;
if (Target) {
+ MarkAnyDeclReferenced(Target->getLocation(), Target,
+ /*MightBeOdrUse=*/false);
S = FindLabeledBreakContinueScope(*this, CurScope, ContinueLoc, Target,
LabelLoc,
/*IsContinue=*/true);
@@ -3352,6 +3354,8 @@ StmtResult Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope,
LabelDecl *Target, SourceLocation LabelLoc) {
Scope *S;
if (Target) {
+ MarkAnyDeclReferenced(Target->getLocation(), Target,
+ /*MightBeOdrUse=*/false);
S = FindLabeledBreakContinueScope(*this, CurScope, BreakLoc, Target,
LabelLoc,
/*IsContinue=*/false);
diff --git a/clang/test/Sema/labeled-break-continue.c b/clang/test/Sema/labeled-break-continue.c
index 78f81c484c3d5..6b4adc23dca8d 100644
--- a/clang/test/Sema/labeled-break-continue.c
+++ b/clang/test/Sema/labeled-break-continue.c
@@ -1,6 +1,6 @@
-// RUN: %clang_cc1 -std=c2y -verify -fsyntax-only -fblocks %s
-// RUN: %clang_cc1 -std=c23 -verify -fsyntax-only -fblocks -fnamed-loops %s
-// RUN: %clang_cc1 -x c++ -verify -fsyntax-only -fblocks -fnamed-loops %s
+// RUN: %clang_cc1 -std=c2y -verify -Wunused -fsyntax-only -fblocks %s
+// RUN: %clang_cc1 -std=c23 -verify -Wunused -fsyntax-only -fblocks -fnamed-loops %s
+// RUN: %clang_cc1 -x c++ -verify -Wunused -fsyntax-only -fblocks -fnamed-loops %s
void f1() {
l1: while (true) {
@@ -159,3 +159,15 @@ void f7() {
continue d; // expected-error {{'continue' label does not name an enclosing loop}}
}
}
+
+void f8() {
+ l1: // no-warning
+ while (true) {
+ break l1;
+ }
+
+ l2: // no-warning
+ while (true) {
+ continue l2;
+ }
+}
>From f713a038ccbb222110d742d6194ffdabe68a346c Mon Sep 17 00:00:00 2001
From: camc <pushy-crop-cartel at duck.com>
Date: Tue, 4 Nov 2025 01:52:57 +0000
Subject: [PATCH 2/2] address comments
---
clang/docs/ReleaseNotes.rst | 2 +-
clang/lib/Sema/SemaStmt.cpp | 8 ++++----
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a366a8ec9630c..5aaf314d0ff63 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -390,7 +390,7 @@ Improvements to Clang's diagnostics
that were previously incorrectly accepted in case of other irrelevant
conditions are now consistently diagnosed, identical to C++ mode.
-- Fix false-positive unused label diagnostic when label used in a named break
+- Fix false-positive unused label diagnostic when a label is used in a named break
or continue (#GH166013)
Improvements to Clang's time-trace
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index f6f38d5943fc5..83a41363124bb 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3281,6 +3281,10 @@ static Scope *FindLabeledBreakContinueScope(Sema &S, Scope *CurScope,
SourceLocation LabelLoc,
bool IsContinue) {
assert(Target && "not a named break/continue?");
+
+ S.MarkAnyDeclReferenced(Target->getLocation(), Target,
+ /*MightBeOdrUse=*/false);
+
Scope *Found = nullptr;
for (Scope *Scope = CurScope; Scope; Scope = Scope->getParent()) {
if (Scope->isFunctionScope())
@@ -3315,8 +3319,6 @@ StmtResult Sema::ActOnContinueStmt(SourceLocation ContinueLoc, Scope *CurScope,
LabelDecl *Target, SourceLocation LabelLoc) {
Scope *S;
if (Target) {
- MarkAnyDeclReferenced(Target->getLocation(), Target,
- /*MightBeOdrUse=*/false);
S = FindLabeledBreakContinueScope(*this, CurScope, ContinueLoc, Target,
LabelLoc,
/*IsContinue=*/true);
@@ -3354,8 +3356,6 @@ StmtResult Sema::ActOnBreakStmt(SourceLocation BreakLoc, Scope *CurScope,
LabelDecl *Target, SourceLocation LabelLoc) {
Scope *S;
if (Target) {
- MarkAnyDeclReferenced(Target->getLocation(), Target,
- /*MightBeOdrUse=*/false);
S = FindLabeledBreakContinueScope(*this, CurScope, BreakLoc, Target,
LabelLoc,
/*IsContinue=*/false);
More information about the cfe-commits
mailing list