[clang] [analyzer][NFC] Let GDMContextsTy destroy the GDM contexts (PR #246883)
Balázs Benics via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 28 11:23:47 PST 2026
https://github.com/steakhal created https://github.com/llvm/llvm-project/pull/246883
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
>From 1e1801a572a5ec881d9cf964f6641370596d3bda Mon Sep 17 00:00:00 2001
From: Balazs Benics <benicsbalazs at gmail.com>
Date: Sat, 28 Nov 2026 19:21:56 +0000
Subject: [PATCH] [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) {
More information about the cfe-commits
mailing list