https://github.com/camc created https://github.com/llvm/llvm-project/pull/166033

Fixes #166013

Marks labels that appear in a c2y named break or continue statement as 
referenced to fix false-positive unused diagnostics.

>From d758fa880bc3f41c18ff71762e7a60d5eac1aafc Mon Sep 17 00:00:00 2001
From: camc <[email protected]>
Date: Sun, 2 Nov 2025 01:21:16 +0000
Subject: [PATCH] [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;
+  }
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to