[clang] [clang] Fix rejects-valid on break label; with multiple labels on a loop (PR #246516)
Jan Schultke via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 26 14:08:19 PST 2026
https://github.com/eisenwave created https://github.com/llvm/llvm-project/pull/246516
Fixes #246505
>From e91251a5727231ef6baef93991558c4bed9d5905 Mon Sep 17 00:00:00 2001
From: Eisenwave <me at eisenwave.net>
Date: Thu, 26 Nov 2026 23:05:47 +0100
Subject: [PATCH] [clang] Fix rejects-valid on break label; with multiple
labels on a loop
Co-authored-by: Copilot <copilot at github.com>
---
clang/docs/ReleaseNotes.md | 1 +
clang/include/clang/Parse/Parser.h | 39 +++---
clang/include/clang/Sema/Scope.h | 17 ++-
clang/lib/Parse/ParseStmt.cpp | 121 ++++++++++--------
clang/lib/Sema/Scope.cpp | 12 +-
clang/lib/Sema/SemaStmt.cpp | 2 +-
clang/test/Sema/labeled-break-continue.c | 37 ++++--
clang/test/SemaCXX/labeled-break-continue.cpp | 22 ++++
8 files changed, 158 insertions(+), 93 deletions(-)
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 4bebce45232cef..89119de20dc3a0 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -997,6 +997,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
- Fixed `-print-dependency-directives-minimized-source` output for object-like macros starting with '(' so that it is not incorrectly turned into a function-like macro.
- Fixed an exponential time-increase bug when collecting argument dependent lookup candidates when the same type is used several times at each level.
- Fixed conversion to a C23 enumeration with an underlying type of `bool`, which truncated the value instead of converting it like `bool` (e.g. `(enum E : bool)2` was 0 instead of 1). This also affected compound assignment and increment. (#GH241289)
+- Fixed a C2y rejects-valid where only a single `label:` per loop can be used with `break label;`. (#GH246505)
#### Bug Fixes to Compiler Builtins
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 4f9087b29c7b48..912af7b0948ff7 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -21,6 +21,7 @@
#include "clang/Sema/SemaCodeCompletion.h"
#include "clang/Sema/SemaObjC.h"
#include "clang/Sema/SemaOpenMP.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Frontend/OpenMP/OMPContext.h"
@@ -7385,7 +7386,7 @@ class Parser : public CodeCompletionHandler {
StmtResult
ParseStatement(SourceLocation *TrailingElseLoc = nullptr,
ParsedStmtContext StmtCtx = ParsedStmtContext::SubStmt,
- LabelDecl *PrecedingLabel = nullptr);
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels = {});
/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
/// \verbatim
@@ -7441,12 +7442,13 @@ class Parser : public CodeCompletionHandler {
StmtResult
ParseStatementOrDeclaration(StmtVector &Stmts, ParsedStmtContext StmtCtx,
SourceLocation *TrailingElseLoc = nullptr,
- LabelDecl *PrecedingLabel = nullptr);
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels = {});
StmtResult ParseStatementOrDeclarationAfterAttributes(
StmtVector &Stmts, ParsedStmtContext StmtCtx,
SourceLocation *TrailingElseLoc, ParsedAttributes &DeclAttrs,
- ParsedAttributes &DeclSpecAttrs, LabelDecl *PrecedingLabel);
+ ParsedAttributes &DeclSpecAttrs,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels);
/// Parse an expression statement.
StmtResult ParseExprStatement(ParsedStmtContext StmtCtx);
@@ -7463,7 +7465,8 @@ class Parser : public CodeCompletionHandler {
/// \endverbatim
///
StmtResult ParseLabeledStatement(ParsedAttributes &Attrs,
- ParsedStmtContext StmtCtx);
+ ParsedStmtContext StmtCtx,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels);
/// ParseCaseStatement
/// \verbatim
@@ -7472,9 +7475,10 @@ class Parser : public CodeCompletionHandler {
/// [GNU] 'case' constant-expression '...' constant-expression ':' statement
/// \endverbatim
///
- StmtResult ParseCaseStatement(ParsedStmtContext StmtCtx,
- bool MissingCase = false,
- ExprResult Expr = ExprResult());
+ StmtResult
+ ParseCaseStatement(ParsedStmtContext StmtCtx, bool MissingCase = false,
+ ExprResult Expr = ExprResult(),
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels = {});
/// ParseDefaultStatement
/// \verbatim
@@ -7483,7 +7487,9 @@ class Parser : public CodeCompletionHandler {
/// \endverbatim
/// Note that this does not parse the 'statement' at the end.
///
- StmtResult ParseDefaultStatement(ParsedStmtContext StmtCtx);
+ StmtResult
+ ParseDefaultStatement(ParsedStmtContext StmtCtx,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels = {});
StmtResult ParseCompoundStatement(bool isStmtExpr = false);
@@ -7572,7 +7578,7 @@ class Parser : public CodeCompletionHandler {
/// [C++] 'switch' '(' condition ')' statement
/// \endverbatim
StmtResult ParseSwitchStatement(SourceLocation *TrailingElseLoc,
- LabelDecl *PrecedingLabel);
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels);
/// ParseWhileStatement
/// \verbatim
@@ -7581,7 +7587,7 @@ class Parser : public CodeCompletionHandler {
/// [C++] 'while' '(' condition ')' statement
/// \endverbatim
StmtResult ParseWhileStatement(SourceLocation *TrailingElseLoc,
- LabelDecl *PrecedingLabel);
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels);
/// ParseDoStatement
/// \verbatim
@@ -7589,7 +7595,7 @@ class Parser : public CodeCompletionHandler {
/// 'do' statement 'while' '(' expression ')' ';'
/// \endverbatim
/// Note: this lets the caller parse the end ';'.
- StmtResult ParseDoStatement(LabelDecl *PrecedingLabel);
+ StmtResult ParseDoStatement(llvm::ArrayRef<LabelDecl *> PrecedingLabels);
/// ParseForStatement
/// \verbatim
@@ -7617,7 +7623,7 @@ class Parser : public CodeCompletionHandler {
/// [C++0x] braced-init-list [TODO]
/// \endverbatim
StmtResult ParseForStatement(SourceLocation *TrailingElseLoc,
- LabelDecl *PrecedingLabel,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels,
CXXExpansionStmtDecl *ESD = nullptr);
void ParseForRangeInitializerAfterColon(ForRangeInit &FRI,
@@ -7691,14 +7697,15 @@ class Parser : public CodeCompletionHandler {
/// expression
/// expansion-init-list
/// \endverbatim
- StmtResult ParseExpansionStatement(SourceLocation *TrailingElseLoc,
- LabelDecl *PrecedingLabel,
- SourceLocation TemplateLoc);
+ StmtResult
+ ParseExpansionStatement(SourceLocation *TrailingElseLoc,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels,
+ SourceLocation TemplateLoc);
StmtResult ParsePragmaLoopHint(StmtVector &Stmts, ParsedStmtContext StmtCtx,
SourceLocation *TrailingElseLoc,
ParsedAttributes &Attrs,
- LabelDecl *PrecedingLabel);
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels);
void ParseMicrosoftIfExistsStatement(StmtVector &Stmts);
diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 58ca2c0738f3c7..2e41850e3eeb2a 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -15,6 +15,7 @@
#include "clang/AST/Decl.h"
#include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
@@ -253,9 +254,9 @@ class Scope {
/// available for this variable in the current scope.
llvm::SmallPtrSet<VarDecl *, 8> ReturnSlots;
- /// If this scope belongs to a loop or switch statement, the label that
- /// directly precedes it, if any.
- LabelDecl *PrecedingLabel;
+ /// If this scope belongs to a loop or switch statement, the labels that
+ /// directly precede it, if any.
+ llvm::SmallVector<LabelDecl *, 2> PrecedingLabels;
void setFlags(Scope *Parent, unsigned F);
@@ -270,8 +271,10 @@ class Scope {
void setFlags(unsigned F) { setFlags(getParent(), F); }
- /// Get the label that precedes this scope.
- LabelDecl *getPrecedingLabel() const { return PrecedingLabel; }
+ /// Get the labels that precede this scope.
+ llvm::ArrayRef<LabelDecl *> getPrecedingLabels() const {
+ return PrecedingLabels;
+ }
/// isBlockScope - Return true if this scope correspond to a closure.
bool isBlockScope() const { return Flags & BlockScope; }
@@ -623,10 +626,10 @@ class Scope {
bool Contains(const Scope& rhs) const { return Depth < rhs.Depth; }
/// Mark that we're entering the body of a loop (for, while, do).
- void EnterLoopBody(LabelDecl *PrecedingLabel);
+ void EnterLoopBody(llvm::ArrayRef<LabelDecl *> PrecedingLabels);
/// Mark that we're entering the body of a switch statement.
- void EnterSwitchBody(LabelDecl *PrecedingLabel);
+ void EnterSwitchBody(llvm::ArrayRef<LabelDecl *> PrecedingLabels);
/// Mark that we're leaving the body of a loop; this is only needed for do
/// loops where the condition follows the loop body.
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 4c3c4e5117813d..12e9831a7db63c 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -39,7 +39,7 @@ using namespace clang;
StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc,
ParsedStmtContext StmtCtx,
- LabelDecl *PrecedingLabel) {
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
StmtResult Res;
// We may get back a null statement if we found a #pragma. Keep going until
@@ -47,16 +47,16 @@ StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc,
StmtVector Stmts;
do {
Res = ParseStatementOrDeclaration(Stmts, StmtCtx, TrailingElseLoc,
- PrecedingLabel);
+ PrecedingLabels);
} while (!Res.isInvalid() && !Res.get());
return Res;
}
-StmtResult Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
- ParsedStmtContext StmtCtx,
- SourceLocation *TrailingElseLoc,
- LabelDecl *PrecedingLabel) {
+StmtResult Parser::ParseStatementOrDeclaration(
+ StmtVector &Stmts, ParsedStmtContext StmtCtx,
+ SourceLocation *TrailingElseLoc,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
ParenBraceBracketBalancer BalancerRAIIObj(*this);
@@ -77,7 +77,7 @@ StmtResult Parser::ParseStatementOrDeclaration(StmtVector &Stmts,
StmtResult Res = ParseStatementOrDeclarationAfterAttributes(
Stmts, StmtCtx, TrailingElseLoc, CXX11Attrs, GNUOrMSAttrs,
- PrecedingLabel);
+ PrecedingLabels);
MaybeDestroyTemplateIds();
takeAndConcatenateAttrs(CXX11Attrs, std::move(GNUOrMSAttrs));
@@ -134,7 +134,7 @@ class StatementFilterCCC final : public CorrectionCandidateCallback {
StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
StmtVector &Stmts, ParsedStmtContext StmtCtx,
SourceLocation *TrailingElseLoc, ParsedAttributes &CXX11Attrs,
- ParsedAttributes &GNUAttrs, LabelDecl *PrecedingLabel) {
+ ParsedAttributes &GNUAttrs, llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
const char *SemiError = nullptr;
StmtResult Res;
SourceLocation GNUAttributeLoc;
@@ -168,7 +168,7 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
takeAndConcatenateAttrs(CXX11Attrs, std::move(GNUAttrs));
// identifier ':' statement
- return ParseLabeledStatement(CXX11Attrs, StmtCtx);
+ return ParseLabeledStatement(CXX11Attrs, StmtCtx, PrecedingLabels);
}
// Look up the identifier, and typo-correct it to a keyword if it's not
@@ -269,11 +269,11 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
// Trying to parse this as a regular 'for' statement instead yields
// better error recovery.
ConsumeToken();
- return ParseForStatement(TrailingElseLoc, PrecedingLabel);
+ return ParseForStatement(TrailingElseLoc, PrecedingLabels);
}
SourceLocation TemplateLoc = ConsumeToken();
- return ParseExpansionStatement(TrailingElseLoc, PrecedingLabel,
+ return ParseExpansionStatement(TrailingElseLoc, PrecedingLabels,
TemplateLoc);
}
@@ -284,9 +284,10 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
}
case tok::kw_case: // C99 6.8.1: labeled-statement
- return ParseCaseStatement(StmtCtx);
+ return ParseCaseStatement(StmtCtx, /*MissingCase=*/false, ExprResult(),
+ PrecedingLabels);
case tok::kw_default: // C99 6.8.1: labeled-statement
- return ParseDefaultStatement(StmtCtx);
+ return ParseDefaultStatement(StmtCtx, PrecedingLabels);
case tok::l_brace: // C99 6.8.2: compound-statement
return ParseCompoundStatement();
@@ -298,12 +299,12 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
case tok::kw_if: // C99 6.8.4.1: if-statement
return ParseIfStatement(TrailingElseLoc);
case tok::kw_switch: // C99 6.8.4.2: switch-statement
- return ParseSwitchStatement(TrailingElseLoc, PrecedingLabel);
+ return ParseSwitchStatement(TrailingElseLoc, PrecedingLabels);
case tok::kw_while: // C99 6.8.5.1: while-statement
- return ParseWhileStatement(TrailingElseLoc, PrecedingLabel);
+ return ParseWhileStatement(TrailingElseLoc, PrecedingLabels);
case tok::kw_do: // C99 6.8.5.2: do-statement
- Res = ParseDoStatement(PrecedingLabel);
+ Res = ParseDoStatement(PrecedingLabels);
SemiError = "do/while";
break;
case tok::kw_for: // C99 6.8.5.3: for-statement
@@ -316,11 +317,11 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
Tok.setKind(tok::kw_template);
SourceLocation TemplateLoc = ConsumeToken();
Tok.setKind(tok::kw_for);
- return ParseExpansionStatement(TrailingElseLoc, PrecedingLabel,
+ return ParseExpansionStatement(TrailingElseLoc, PrecedingLabels,
TemplateLoc);
}
- return ParseForStatement(TrailingElseLoc, PrecedingLabel);
+ return ParseForStatement(TrailingElseLoc, PrecedingLabels);
case tok::kw_goto: // C99 6.8.6.1: goto-statement
Res = ParseGotoStatement();
@@ -519,7 +520,7 @@ StmtResult Parser::ParseStatementOrDeclarationAfterAttributes(
ProhibitAttributes(CXX11Attrs);
ProhibitAttributes(GNUAttrs);
return ParsePragmaLoopHint(Stmts, StmtCtx, TrailingElseLoc, CXX11Attrs,
- PrecedingLabel);
+ PrecedingLabels);
case tok::annot_pragma_dump:
ProhibitAttributes(CXX11Attrs);
@@ -719,8 +720,10 @@ static void DiagnoseLabelFollowedByDecl(Parser &P, const Stmt *SubStmt) {
}
}
-StmtResult Parser::ParseLabeledStatement(ParsedAttributes &Attrs,
- ParsedStmtContext StmtCtx) {
+StmtResult
+Parser::ParseLabeledStatement(ParsedAttributes &Attrs,
+ ParsedStmtContext StmtCtx,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
"Not an identifier!");
@@ -740,6 +743,10 @@ StmtResult Parser::ParseLabeledStatement(ParsedAttributes &Attrs,
LabelDecl *LD = Actions.LookupOrCreateLabel(
IdentTok.getIdentifierInfo(), IdentTok.getLocation(), /*GnuLabelLoc=*/{},
/*IsLabelStmt=*/true);
+ llvm::SmallVector<LabelDecl *, 2> Labels(PrecedingLabels.begin(),
+ PrecedingLabels.end());
+ if (LD)
+ Labels.push_back(LD);
// Read label attributes, if present.
StmtResult SubStmt;
@@ -761,7 +768,7 @@ StmtResult Parser::ParseLabeledStatement(ParsedAttributes &Attrs,
ParsedAttributes EmptyCXX11Attrs(AttrFactory);
SubStmt = ParseStatementOrDeclarationAfterAttributes(
Stmts, StmtCtx, /*TrailingElseLoc=*/nullptr, EmptyCXX11Attrs,
- TempAttrs, LD);
+ TempAttrs, Labels);
if (!TempAttrs.empty() && !SubStmt.isInvalid())
SubStmt = Actions.ActOnAttributedStmt(TempAttrs, SubStmt.get());
}
@@ -775,7 +782,7 @@ StmtResult Parser::ParseLabeledStatement(ParsedAttributes &Attrs,
// If we've not parsed a statement yet, parse one now.
if (SubStmt.isUnset())
- SubStmt = ParseStatement(nullptr, StmtCtx, LD);
+ SubStmt = ParseStatement(nullptr, StmtCtx, Labels);
// Broken substmt shouldn't prevent the label from being added to the AST.
if (SubStmt.isInvalid())
@@ -797,8 +804,10 @@ StmtResult Parser::ParseLabeledStatement(ParsedAttributes &Attrs,
SubStmt.get());
}
-StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx,
- bool MissingCase, ExprResult Expr) {
+StmtResult
+Parser::ParseCaseStatement(ParsedStmtContext StmtCtx, bool MissingCase,
+ ExprResult Expr,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
// [OpenMP 5.1] 2.1.3: A stand-alone directive may not be used in place of a
@@ -907,7 +916,8 @@ StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx,
// continue parsing the sub-stmt.
if (Case.isInvalid()) {
if (TopLevelCase.isInvalid()) // No parsed case stmts.
- return ParseStatement(/*TrailingElseLoc=*/nullptr, StmtCtx);
+ return ParseStatement(/*TrailingElseLoc=*/nullptr, StmtCtx,
+ PrecedingLabels);
// Otherwise, just don't add it as a nested case.
} else {
// If this is the first case statement we parsed, it becomes TopLevelCase.
@@ -932,7 +942,8 @@ StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx,
DiagnoseLabelAtEndOfCompoundStatement();
SubStmt = Actions.ActOnNullStmt(ColonLoc);
} else {
- SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr, StmtCtx);
+ SubStmt =
+ ParseStatement(/*TrailingElseLoc=*/nullptr, StmtCtx, PrecedingLabels);
}
// Install the body into the most deeply-nested case.
@@ -948,7 +959,9 @@ StmtResult Parser::ParseCaseStatement(ParsedStmtContext StmtCtx,
return TopLevelCase;
}
-StmtResult Parser::ParseDefaultStatement(ParsedStmtContext StmtCtx) {
+StmtResult
+Parser::ParseDefaultStatement(ParsedStmtContext StmtCtx,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
assert(Tok.is(tok::kw_default) && "Not a default stmt!");
// [OpenMP 5.1] 2.1.3: A stand-alone directive may not be used in place of a
@@ -981,7 +994,8 @@ StmtResult Parser::ParseDefaultStatement(ParsedStmtContext StmtCtx) {
DiagnoseLabelAtEndOfCompoundStatement();
SubStmt = Actions.ActOnNullStmt(ColonLoc);
} else {
- SubStmt = ParseStatement(/*TrailingElseLoc=*/nullptr, StmtCtx);
+ SubStmt =
+ ParseStatement(/*TrailingElseLoc=*/nullptr, StmtCtx, PrecedingLabels);
}
// Broken sub-stmt shouldn't prevent forming the case statement properly.
@@ -1681,8 +1695,9 @@ StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
ThenStmt.get(), ElseLoc, ElseStmt.get());
}
-StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc,
- LabelDecl *PrecedingLabel) {
+StmtResult
+Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
@@ -1747,7 +1762,7 @@ StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc,
// See comments in ParseIfStatement for why we create a scope for the
// condition and a new scope for substatement in C++.
//
- getCurScope()->EnterSwitchBody(PrecedingLabel);
+ getCurScope()->EnterSwitchBody(PrecedingLabels);
ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
// We have incremented the mangling number for the SwitchScope and the
@@ -1765,8 +1780,9 @@ StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc,
return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
}
-StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc,
- LabelDecl *PrecedingLabel) {
+StmtResult
+Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
assert(Tok.is(tok::kw_while) && "Not a while stmt!");
SourceLocation WhileLoc = Tok.getLocation();
ConsumeToken(); // eat the 'while'.
@@ -1807,7 +1823,7 @@ StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc,
// combinations, so diagnose that here in OpenACC mode.
SemaOpenACC::LoopInConstructRAII LCR{getActions().OpenACC()};
getActions().OpenACC().ActOnWhileStmt(WhileLoc);
- getCurScope()->EnterLoopBody(PrecedingLabel);
+ getCurScope()->EnterLoopBody(PrecedingLabels);
// C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
// there is no compound stmt. C90 does not have this clause. We only do this
@@ -1839,7 +1855,8 @@ StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc,
return Actions.ActOnWhileStmt(WhileLoc, LParen, Cond, RParen, Body.get());
}
-StmtResult Parser::ParseDoStatement(LabelDecl *PrecedingLabel) {
+StmtResult
+Parser::ParseDoStatement(llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
assert(Tok.is(tok::kw_do) && "Not a do stmt!");
SourceLocation DoLoc = ConsumeToken(); // eat the 'do'.
@@ -1852,7 +1869,7 @@ StmtResult Parser::ParseDoStatement(LabelDecl *PrecedingLabel) {
// combinations, so diagnose that here in OpenACC mode.
SemaOpenACC::LoopInConstructRAII LCR{getActions().OpenACC()};
getActions().OpenACC().ActOnDoStmt(DoLoc);
- getCurScope()->EnterLoopBody(PrecedingLabel);
+ getCurScope()->EnterLoopBody(PrecedingLabels);
// C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
// there is no compound stmt. C90 does not have this clause. We only do this
@@ -1981,9 +1998,10 @@ void Parser::ParseForRangeInitializerAfterColon(ForRangeInit &FRI,
std::move(Actions.ExprEvalContexts.back().ForRangeLifetimeExtendTemps);
}
-StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc,
- LabelDecl *PrecedingLabel,
- CXXExpansionStmtDecl *ESD) {
+StmtResult
+Parser::ParseForStatement(SourceLocation *TrailingElseLoc,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels,
+ CXXExpansionStmtDecl *ESD) {
assert(Tok.is(tok::kw_for) && "Not a for stmt!");
SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
@@ -2336,7 +2354,7 @@ StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc,
// Set this only right before parsing the body to disallow break/continue in
// the other parts.
- getCurScope()->EnterLoopBody(PrecedingLabel);
+ getCurScope()->EnterLoopBody(PrecedingLabels);
bool BodyStartsWithAttr = Tok.isOneOf(tok::l_square, tok::kw___attribute);
SourceLocation BodyBeginLoc = Tok.getLocation();
@@ -2526,11 +2544,11 @@ StmtResult Parser::ParseDeferStatement(SourceLocation *TrailingElseLoc) {
return Actions.ActOnEndOfDeferStmt(Res.get(), getCurScope());
}
-StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
- ParsedStmtContext StmtCtx,
- SourceLocation *TrailingElseLoc,
- ParsedAttributes &Attrs,
- LabelDecl *PrecedingLabel) {
+StmtResult
+Parser::ParsePragmaLoopHint(StmtVector &Stmts, ParsedStmtContext StmtCtx,
+ SourceLocation *TrailingElseLoc,
+ ParsedAttributes &Attrs,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels) {
// Create temporary attribute list.
ParsedAttributes TempAttrs(AttrFactory);
@@ -2555,7 +2573,7 @@ StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts,
ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
StmtResult S = ParseStatementOrDeclarationAfterAttributes(
Stmts, StmtCtx, TrailingElseLoc, Attrs, EmptyDeclSpecAttrs,
- PrecedingLabel);
+ PrecedingLabels);
Attrs.takeAllPrependingFrom(TempAttrs);
@@ -2776,9 +2794,10 @@ StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
}
-StmtResult Parser::ParseExpansionStatement(SourceLocation *TrailingElseLoc,
- LabelDecl *PrecedingLabel,
- SourceLocation TemplateLoc) {
+StmtResult
+Parser::ParseExpansionStatement(SourceLocation *TrailingElseLoc,
+ llvm::ArrayRef<LabelDecl *> PrecedingLabels,
+ SourceLocation TemplateLoc) {
assert(Tok.is(tok::kw_for));
CXXExpansionStmtDecl *ExpansionDecl =
@@ -2791,7 +2810,7 @@ StmtResult Parser::ParseExpansionStatement(SourceLocation *TrailingElseLoc,
++TParamDepthGuard;
StmtResult SR =
- ParseForStatement(TrailingElseLoc, PrecedingLabel, ExpansionDecl);
+ ParseForStatement(TrailingElseLoc, PrecedingLabels, ExpansionDecl);
if (SR.isInvalid())
return SR;
diff --git a/clang/lib/Sema/Scope.cpp b/clang/lib/Sema/Scope.cpp
index fc79b1a056ed9e..c8faeb6e609956 100644
--- a/clang/lib/Sema/Scope.cpp
+++ b/clang/lib/Sema/Scope.cpp
@@ -99,7 +99,7 @@ void Scope::Init(Scope *parent, unsigned flags) {
UsingDirectives.clear();
Entity = nullptr;
ErrorTrap.reset();
- PrecedingLabel = nullptr;
+ PrecedingLabels.clear();
NRVO = std::nullopt;
}
@@ -113,23 +113,23 @@ bool Scope::containedInPrototypeScope() const {
return false;
}
-void Scope::EnterLoopBody(LabelDecl *LD) {
+void Scope::EnterLoopBody(llvm::ArrayRef<LabelDecl *> Labels) {
Flags |= BreakScope | ContinueScope;
BreakParent = ContinueParent = this;
- PrecedingLabel = LD;
+ PrecedingLabels.assign(Labels.begin(), Labels.end());
}
-void Scope::EnterSwitchBody(LabelDecl *LD) {
+void Scope::EnterSwitchBody(llvm::ArrayRef<LabelDecl *> Labels) {
Flags |= BreakScope;
BreakParent = this;
- PrecedingLabel = LD;
+ PrecedingLabels.assign(Labels.begin(), Labels.end());
}
void Scope::LeaveLoopBody() {
Flags &= ~(BreakScope | ContinueScope);
BreakParent = getParent()->BreakParent;
ContinueParent = getParent()->ContinueParent;
- PrecedingLabel = nullptr;
+ PrecedingLabels.clear();
}
// The algorithm for updating NRVO candidate is as follows:
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 602ef09280f794..667b1426200e4b 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -3477,7 +3477,7 @@ static Scope *FindLabeledBreakContinueScope(Sema &S, Scope *CurScope,
}
if (Scope->isBreakOrContinueScope() &&
- Scope->getPrecedingLabel() == Target) {
+ llvm::is_contained(Scope->getPrecedingLabels(), Target)) {
Found = Scope;
break;
}
diff --git a/clang/test/Sema/labeled-break-continue.c b/clang/test/Sema/labeled-break-continue.c
index 6b4adc23dca8dc..a794601a9137c3 100644
--- a/clang/test/Sema/labeled-break-continue.c
+++ b/clang/test/Sema/labeled-break-continue.c
@@ -46,26 +46,26 @@ void f2() {
void f3() {
a: b: c: d: while (true) {
- break a; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
- break b; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
- break c; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
+ break a;
+ break b;
+ break c;
break d;
- continue a; // expected-error {{'continue' label does not name an enclosing loop}}
- continue b; // expected-error {{'continue' label does not name an enclosing loop}}
- continue c; // expected-error {{'continue' label does not name an enclosing loop}}
+ continue a;
+ continue b;
+ continue c;
continue d;
e: while (true) {
- break a; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
- break b; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
- break c; // expected-error {{'break' label does not name an enclosing loop or 'switch'}}
+ break a;
+ break b;
+ break c;
break d;
break e;
- continue a; // expected-error {{'continue' label does not name an enclosing loop}}
- continue b; // expected-error {{'continue' label does not name an enclosing loop}}
- continue c; // expected-error {{'continue' label does not name an enclosing loop}}
+ continue a;
+ continue b;
+ continue c;
continue d;
continue e;
}
@@ -171,3 +171,16 @@ void f8() {
continue l2;
}
}
+
+void f9() {
+ switch (0) {
+ a: default: while (true) {
+ break a;
+ continue a;
+ }
+ b: case 0: while (true) {
+ break b;
+ continue b;
+ }
+ }
+}
diff --git a/clang/test/SemaCXX/labeled-break-continue.cpp b/clang/test/SemaCXX/labeled-break-continue.cpp
index 0ebfb94f64f8de..381b557f430567 100644
--- a/clang/test/SemaCXX/labeled-break-continue.cpp
+++ b/clang/test/SemaCXX/labeled-break-continue.cpp
@@ -86,3 +86,25 @@ void f7() {
});
}
}
+
+void f8() {
+ a: b: while (true) {
+ break a;
+ continue a;
+ break b;
+ continue b;
+ }
+}
+
+void f9() {
+ switch (0) {
+ a: default: while (true) {
+ break a;
+ continue a;
+ }
+ b: case 0: while (true) {
+ break b;
+ continue b;
+ }
+ }
+}
More information about the cfe-commits
mailing list