[clang] [clang][bytecode] Create local scopes for if then/else statements (PR #120852)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sat Dec 21 12:27:20 PST 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/120852

In case those aren't compound statements.

>From a2cf825d002c867804bb65e6f156a87c8da6b07d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 21 Dec 2024 20:04:44 +0100
Subject: [PATCH] [clang][bytecode] Create local scopes for if then/else
 statements

In case those aren't compound statements.
---
 clang/lib/AST/ByteCode/Compiler.cpp | 27 +++++++++++++++++++++------
 clang/test/AST/ByteCode/if.cpp      | 27 +++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 59c77f0ce78d2b..68c75b01e6f6df 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4974,20 +4974,35 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
     LabelTy LabelEnd = this->getLabel();
     if (!this->jumpFalse(LabelElse))
       return false;
-    if (!visitStmt(IS->getThen()))
-      return false;
+    {
+      LocalScope<Emitter> ThenScope(this);
+      if (!visitStmt(IS->getThen()))
+        return false;
+      if (!ThenScope.destroyLocals())
+        return false;
+    }
     if (!this->jump(LabelEnd))
       return false;
     this->emitLabel(LabelElse);
-    if (!visitStmt(Else))
-      return false;
+    {
+      LocalScope<Emitter> ElseScope(this);
+      if (!visitStmt(Else))
+        return false;
+      if (!ElseScope.destroyLocals())
+        return false;
+    }
     this->emitLabel(LabelEnd);
   } else {
     LabelTy LabelEnd = this->getLabel();
     if (!this->jumpFalse(LabelEnd))
       return false;
-    if (!visitStmt(IS->getThen()))
-      return false;
+    {
+      LocalScope<Emitter> ThenScope(this);
+      if (!visitStmt(IS->getThen()))
+        return false;
+      if (!ThenScope.destroyLocals())
+        return false;
+    }
     this->emitLabel(LabelEnd);
   }
 
diff --git a/clang/test/AST/ByteCode/if.cpp b/clang/test/AST/ByteCode/if.cpp
index 540cb76fbaac3c..c48b2b8d378c85 100644
--- a/clang/test/AST/ByteCode/if.cpp
+++ b/clang/test/AST/ByteCode/if.cpp
@@ -76,3 +76,30 @@ namespace IfScope {
   }
   static_assert(foo() == 13, "");
 }
+
+namespace IfScope2 {
+  struct __bit_iterator {
+    unsigned __ctz_;
+  };
+  constexpr void __fill_n_bool(__bit_iterator) {}
+
+  constexpr void fill_n(__bit_iterator __first) {
+    if (false)
+      __fill_n_bool(__first);
+    else
+      __fill_n_bool(__first);
+  }
+
+  struct bitset{
+    constexpr void reset() {
+      auto m = __bit_iterator(8);
+      fill_n(m);
+    }
+  };
+  consteval bool foo() {
+    bitset v;
+    v.reset();
+    return true;
+  }
+  static_assert(foo());
+}



More information about the cfe-commits mailing list