[clang] [analyzer][NFC] Let GDMContextsTy destroy the GDM contexts (PR #246883)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 28 11:24:20 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-static-analyzer-1
Author: Balázs Benics (steakhal)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/246883.diff
2 Files Affected:
- (modified) clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h (+24-4)
- (modified) clang/lib/StaticAnalyzer/Core/ProgramState.cpp (-7)
``````````diff
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
index e789a49f4cd50..e12880c7a7407 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 eaa623a3a9e12..00282fa37aaed 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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/246883
More information about the cfe-commits
mailing list