llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

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

Supersedes #<!-- -->246883 (reopened from the llvm remote so it can be stacked).

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


2 Files Affected:

- (modified) 
clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h (+24-2) 
- (modified) clang/lib/StaticAnalyzer/Core/ProgramState.cpp (+7-11) 


``````````diff
diff --git 
a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h 
b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h
index e789a49f4cd50..8562c608e36c0 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[](ConstGDMKey Key) { return Contexts[Key]; }
+
+  private:
+    llvm::DenseMap<ConstGDMKey, ContextAndDeleter> Contexts;
+  };
   GDMContextsTy GDMContexts;
 
   /// StateSet - FoldingSet containing all the states created for analyzing
diff --git a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp 
b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
index eaa623a3a9e12..276aa8dbc914a 100644
--- a/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
@@ -73,12 +73,8 @@ 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);
-}
+// 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) {
@@ -523,13 +519,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,

``````````

</details>


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

Reply via email to