[clang] [clang][bytecode] Clean up CondScope after while loop (PR #186816)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 16 08:43:40 PDT 2026


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

>From 7fb56a397fc9af9e19eae3c209514749ea263a48 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 16 Mar 2026 15:50:55 +0100
Subject: [PATCH] asdf

---
 clang/lib/AST/ByteCode/Compiler.cpp | 26 +++++++++++++++-----------
 clang/test/AST/ByteCode/loops.cpp   | 16 +++++++++++++---
 2 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 8bbdf284b313d..fc2da6362d0bb 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -6115,11 +6115,13 @@ bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
   this->fallthrough(CondLabel);
   this->emitLabel(CondLabel);
 
-  {
-    LocalScope<Emitter> CondScope(this);
-    if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
-      if (!visitDeclStmt(CondDecl))
-        return false;
+  // Start of the loop body {
+  LocalScope<Emitter> CondScope(this);
+
+  if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
+    if (!visitDeclStmt(CondDecl))
+      return false;
+  }
 
     if (!this->visitBool(Cond))
       return false;
@@ -6135,12 +6137,14 @@ bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
 
     if (!CondScope.destroyLocals())
       return false;
-  }
-  if (!this->jump(CondLabel))
-    return false;
-  this->fallthrough(EndLabel);
-  this->emitLabel(EndLabel);
-  return WholeLoopScope.destroyLocals();
+    // } End of loop body.
+
+    if (!this->jump(CondLabel))
+      return false;
+    this->fallthrough(EndLabel);
+    this->emitLabel(EndLabel);
+
+    return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
 }
 
 template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
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(), "");
 }



More information about the cfe-commits mailing list