[clang] [clang] Mark labels referenced when used in named break or continue (PR #166033)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 1 18:27:19 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (camc)
<details>
<summary>Changes</summary>
Fixes #<!-- -->166013
Marks labels that appear in a c2y named break or continue statement as referenced to fix false-positive unused diagnostics.
---
Full diff: https://github.com/llvm/llvm-project/pull/166033.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+3)
- (modified) clang/lib/Sema/SemaStmt.cpp (+4)
- (modified) clang/test/Sema/labeled-break-continue.c (+15-3)
``````````diff
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;
+ }
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/166033
More information about the cfe-commits
mailing list