https://github.com/ojhunt created https://github.com/llvm/llvm-project/pull/152606
Make sure we don't push the break and continue scope for a while loop until after we have evaluated the condition. >From 467f72d8e2e7f3abbd6824433172fddcd24a771e Mon Sep 17 00:00:00 2001 From: Oliver Hunt <oli...@apple.com> Date: Thu, 7 Aug 2025 15:29:51 -0700 Subject: [PATCH] [clang][Sema] Fix the continue and break scope for while loops Make sure we don't push the break and continue scope for a while loop until after we have evaluated the condition. --- clang/docs/ReleaseNotes.rst | 2 ++ clang/lib/Parse/ParseStmt.cpp | 3 ++- clang/test/Sema/while-loop-condition-scope.c | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 clang/test/Sema/while-loop-condition-scope.c diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 0e9fcaa5fac6a..4062c4b7a6fdb 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -161,6 +161,8 @@ Bug Fixes in This Version targets that treat ``_Float16``/``__fp16`` as native scalar types. Previously the warning was silently lost because the operands differed only by an implicit cast chain. (#GH149967). +- Correct the continue and break scope for while statements to be after the + condition is evaluated. Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp index bf1978c22ee9f..b1b700951231f 100644 --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -1734,7 +1734,6 @@ StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) { Scope::DeclScope | Scope::ControlScope; else ScopeFlags = Scope::BreakScope | Scope::ContinueScope; - ParseScope WhileScope(this, ScopeFlags); // Parse the condition. Sema::ConditionResult Cond; @@ -1744,6 +1743,8 @@ StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) { Sema::ConditionKind::Boolean, LParen, RParen)) return StmtError(); + ParseScope WhileScope(this, ScopeFlags); + // OpenACC Restricts a while-loop inside of certain construct/clause // combinations, so diagnose that here in OpenACC mode. SemaOpenACC::LoopInConstructRAII LCR{getActions().OpenACC()}; diff --git a/clang/test/Sema/while-loop-condition-scope.c b/clang/test/Sema/while-loop-condition-scope.c new file mode 100644 index 0000000000000..d87362bdc668d --- /dev/null +++ b/clang/test/Sema/while-loop-condition-scope.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +void f() { + while (({ continue; 1; })) { + // expected-error@-1 {{'continue' statement not in loop statement}} + + } + while (({ break; 1; })) { + // expected-error@-1 {{'break' statement not in loop or switch statement}} + } +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits