[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:51:03 PST 2026
https://github.com/steakhal updated https://github.com/llvm/llvm-project/pull/246883
>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 1/3] [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 <benicsbalazs at gmail.com>
Date: Sat, 28 Nov 2026 19:50:26 +0000
Subject: [PATCH 2/3] 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 <benicsbalazs at gmail.com>
Date: Sat, 28 Nov 2026 19:50:39 +0000
Subject: [PATCH 3/3] 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,
More information about the cfe-commits
mailing list