[clang] [clang][bytecode] Add while loop condition decl to whole loop scope (PR #186816)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 16 08:20:07 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

Instead of the iteration scope.

---
Full diff: https://github.com/llvm/llvm-project/pull/186816.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+5-4) 
- (modified) clang/test/AST/ByteCode/loops.cpp (+13-3) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 8bbdf284b313d..de0465c8b33a6 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -6112,15 +6112,16 @@ bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
   LocalScope<Emitter> WholeLoopScope(this);
   LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
 
+  if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
+    if (!visitDeclStmt(CondDecl))
+      return false;
+  }
+
   this->fallthrough(CondLabel);
   this->emitLabel(CondLabel);
 
   {
     LocalScope<Emitter> CondScope(this);
-    if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
-      if (!visitDeclStmt(CondDecl))
-        return false;
-
     if (!this->visitBool(Cond))
       return false;
 
diff --git a/clang/test/AST/ByteCode/loops.cpp b/clang/test/AST/ByteCode/loops.cpp
index 38ab5613e1cbd..ff80ef5c6e2ed 100644
--- a/clang/test/AST/ByteCode/loops.cpp
+++ b/clang/test/AST/ByteCode/loops.cpp
@@ -1,7 +1,7 @@
-// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++14 -verify %s
-// RUN: %clang_cc1 -std=c++14 -verify=ref %s
+// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++14 -verify                %s
+// RUN: %clang_cc1                                         -std=c++14 -verify=ref            %s
 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -std=c++20 -verify=expected-cpp20 %s
-// RUN: %clang_cc1 -std=c++20 -verify=ref %s
+// RUN: %clang_cc1                                         -std=c++20 -verify=ref            %s
 
 namespace WhileLoop {
   constexpr int f() {
@@ -351,4 +351,14 @@ namespace Scopes {
     return n;
   }
   static_assert(foo() == 14, "");
+
+  constexpr bool WhileConditionDecl() {
+    bool b = true;
+    for (int i = 0; i < 3; ++i) {
+      while (int x = 0) {
+      }
+    }
+    return true;
+  }
+  static_assert(WhileConditionDecl(), "");
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/186816


More information about the cfe-commits mailing list