https://github.com/steakhal created 
https://github.com/llvm/llvm-project/pull/246899

<sub>Stack created with <a href="https://github.com/github/gh-stack";>GitHub 
Stacks CLI</a> • <a href="https://gh.io/stacks-feedback";>Give Feedback 
💬</a></sub>

From 1e1801a572a5ec881d9cf964f6641370596d3bda Mon Sep 17 00:00:00 2001
From: Balazs Benics <[email protected]>
Date: Sat, 28 Nov 2026 19:21:56 +0000
Subject: [PATCH 1/4] [analyzer][NFC] Let GDMContextsTy destroy the GDM
 contexts

ProgramStateManager destroyed its GDM contexts by hand in its
destructor. Make GDMContextsTy a small non-copyable class that owns the
map and destroys the contexts in its own destructor, so the ownership
is expressed by the member rather than by the enclosing destructor.

Assisted-by: Claude
---
 .../Core/PathSensitive/ProgramState.h         | 28 ++++++++++++++++---
 .../lib/StaticAnalyzer/Core/ProgramState.cpp  |  7 -----
 2 files changed, 24 insertions(+), 11 deletions(-)

diff --git 
a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h 
b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
index e789a49f4cd50e..e12880c7a74077 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
@@ -22,6 +22,7 @@
 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
 #include "llvm/ADT/FoldingSet.h"
 #include "llvm/ADT/ImmutableMap.h"
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Allocator.h"
 #include <optional>
 #include <utility>
@@ -510,8 +511,29 @@ class ProgramStateManager {
 
   ProgramState::GenericDataMap::Factory     GDMFactory;
 
-  typedef llvm::DenseMap<const void *, std::pair<void *, void (*)(void *)>>
-      GDMContextsTy;
+  /// Owns the GDM contexts:
+  /// Maps a GDM key to its context and to the function that destroys it.
+  class GDMContextsTy {
+  public:
+    using GDMKey = void *;
+    using ConstGDMKey = const void *;
+    using DeleterTy = void (*)(GDMKey);
+    using ContextAndDeleter = std::pair<GDMKey, DeleterTy>;
+
+    GDMContextsTy() = default;
+    GDMContextsTy(const GDMContextsTy &) = delete;
+    GDMContextsTy &operator=(const GDMContextsTy &) = delete;
+
+    ~GDMContextsTy() {
+      for (auto [Ctx, Deleter] : llvm::make_second_range(Contexts))
+        Deleter(Ctx);
+    }
+
+    ContextAndDeleter &operator[](GDMKey Key) { return Contexts[Key]; }
+
+  private:
+    llvm::DenseMap<GDMKey, ContextAndDeleter> Contexts;
+  };
   GDMContextsTy GDMContexts;
 
   /// StateSet - FoldingSet containing all the states created for analyzing
@@ -540,8 +562,6 @@ class ProgramStateManager {
                  llvm::BumpPtrAllocator& alloc,
                  ExprEngine *expreng);
 
-  ~ProgramStateManager();
-
   ProgramStateRef getInitialState(const StackFrame *InitSF);
 
   ASTContext &getContext() { return svalBuilder->getContext(); }
diff --git a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp 
b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
index eaa623a3a9e121..00282fa37aaede 100644
--- a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
@@ -73,13 +73,6 @@ ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
   ConstraintMgr = (*CreateCMgr)(*this, ExprEng);
 }
 
-
-ProgramStateManager::~ProgramStateManager() {
-  for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
-       I!=E; ++I)
-    I->second.second(I->second.first);
-}
-
 ProgramStateRef ProgramStateManager::removeDeadBindingsFromEnvironmentAndStore(
     ProgramStateRef state, const StackFrame *SF, SymbolReaper &SymReaper) {
 

From 24c9c0e7a0e86b8839a05b1ecfe024e546808988 Mon Sep 17 00:00:00 2001
From: Balazs Benics <[email protected]>
Date: Sat, 28 Nov 2026 19:50:26 +0000
Subject: [PATCH 2/4] Fix const correctness

---
 .../clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h    | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git 
a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h 
b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
index e12880c7a74077..1ca74adce223e4 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
@@ -529,10 +529,10 @@ class ProgramStateManager {
         Deleter(Ctx);
     }
 
-    ContextAndDeleter &operator[](GDMKey Key) { return Contexts[Key]; }
+    ContextAndDeleter &operator[](ConstGDMKey Key) { return Contexts[Key]; }
 
   private:
-    llvm::DenseMap<GDMKey, ContextAndDeleter> Contexts;
+    llvm::DenseMap<ConstGDMKey, ContextAndDeleter> Contexts;
   };
   GDMContextsTy GDMContexts;
 

From 79c56ad4b517a78138bb40ee76f89f3d039e08fc Mon Sep 17 00:00:00 2001
From: Balazs Benics <[email protected]>
Date: Sat, 28 Nov 2026 19:50:39 +0000
Subject: [PATCH 3/4] NFC Simplify FindGDMContext

---
 clang/lib/StaticAnalyzer/Core/ProgramState.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp 
b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
index 00282fa37aaede..3f92b4b85ca9e1 100644
--- a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
@@ -516,13 +516,13 @@ void *ProgramStateManager::FindGDMContext(
     const void *K, void *(*CreateContext)(llvm::BumpPtrAllocator &),
     void (*DeleteContext)(void *)) {
 
-  std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
-  if (!p.first) {
-    p.first = CreateContext(Alloc);
-    p.second = DeleteContext;
+  auto &[GDMKey, Deleter] = GDMContexts[K];
+  if (!GDMKey) {
+    GDMKey = CreateContext(Alloc);
+    Deleter = DeleteContext;
   }
 
-  return p.first;
+  return GDMKey;
 }
 
 ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, const void 
*Key,

From 3eedecfc80cad91cee23bfdb2a844b0b4be514e8 Mon Sep 17 00:00:00 2001
From: Balazs Benics <[email protected]>
Date: Sat, 28 Nov 2026 20:43:08 +0000
Subject: [PATCH 4/4] Add back the dtor declaration for unique_ptr

---
 .../clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h     | 2 ++
 clang/lib/StaticAnalyzer/Core/ProgramState.cpp                 | 3 +++
 2 files changed, 5 insertions(+)

diff --git 
a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h 
b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
index 1ca74adce223e4..8562c608e36c00 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
@@ -562,6 +562,8 @@ class ProgramStateManager {
                  llvm::BumpPtrAllocator& alloc,
                  ExprEngine *expreng);
 
+  ~ProgramStateManager();
+
   ProgramStateRef getInitialState(const StackFrame *InitSF);
 
   ASTContext &getContext() { return svalBuilder->getContext(); }
diff --git a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp 
b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
index 3f92b4b85ca9e1..276aa8dbc914a3 100644
--- a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
@@ -73,6 +73,9 @@ ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
   ConstraintMgr = (*CreateCMgr)(*this, ExprEng);
 }
 
+// Out-of-line so unique_ptr members can use forward-declared types.
+ProgramStateManager::~ProgramStateManager() = default;
+
 ProgramStateRef ProgramStateManager::removeDeadBindingsFromEnvironmentAndStore(
     ProgramStateRef state, const StackFrame *SF, SymbolReaper &SymReaper) {
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to