[clang] [CIR] Protect DeclMapRevertingRAII for move and copy (PR #247152)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 30 04:51:09 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Steffen Larsen (steffenlarsen)

<details>
<summary>Changes</summary>

DeclMapRevertingRAII currently uses the default copy and move constructors. Improper handling of objects of this class may cause unexpected behavior.

To amend this, we delete the copy constructor and define a move constructor that invalidates the original object.

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


1 Files Affected:

- (modified) clang/lib/CIR/CodeGen/CIRGenFunction.h (+14) 


``````````diff
diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h
index 9b209579b0bfbe..0609c324d69c47 100644
--- a/clang/lib/CIR/CodeGen/CIRGenFunction.h
+++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h
@@ -962,6 +962,7 @@ class CIRGenFunction : public CIRGenTypeCache {
   // localDeclMap. This is used in cases where things are being inserted into
   // the variable list but don't follow normal lookup/search rules, like in
   // OpenACC recipe generation.
+  // A nullptr VarDecl signals that the object is dead and should not revert.
   class DeclMapRevertingRAII {
     CIRGenFunction &cgf;
     const VarDecl *vd;
@@ -979,7 +980,20 @@ class CIRGenFunction : public CIRGenTypeCache {
         shouldDelete = true;
     }
 
+    // Define move constructor that invalidates the original object.
+    DeclMapRevertingRAII(DeclMapRevertingRAII &&other)
+        : cgf(other.cgf), vd(other.vd), shouldDelete(other.shouldDelete),
+          oldAddr(other.oldAddr) {
+      other.vd = nullptr; // Invalidate vd to signal moved object.
+    }
+
+    // Copy constructor is deleted to avoid accidentally creating multiple
+    // instances of this RAII object for the same variable.
+    DeclMapRevertingRAII(const DeclMapRevertingRAII &) = delete;
+
     ~DeclMapRevertingRAII() {
+      if (!vd)
+        return;
       if (shouldDelete)
         cgf.localDeclMap.erase(vd);
       else

``````````

</details>


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


More information about the cfe-commits mailing list