SymbolManager::SymbolDependencies: Use unique_ptr to simplify memory management
authorDavid Blaikie <dblaikie@gmail.com>
Wed, 29 Apr 2020 04:03:57 +0000 (21:03 -0700)
committerDavid Blaikie <dblaikie@gmail.com>
Wed, 29 Apr 2020 05:31:17 +0000 (22:31 -0700)
clang/include/clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h
clang/lib/StaticAnalyzer/Core/SymbolManager.cpp

index d212e23..2c50599 100644 (file)
@@ -459,7 +459,8 @@ public:
 
 class SymbolManager {
   using DataSetTy = llvm::FoldingSet<SymExpr>;
-  using SymbolDependTy = llvm::DenseMap<SymbolRef, SymbolRefSmallVectorTy *>;
+  using SymbolDependTy =
+      llvm::DenseMap<SymbolRef, std::unique_ptr<SymbolRefSmallVectorTy>>;
 
   DataSetTy DataSet;
 
@@ -476,7 +477,6 @@ public:
   SymbolManager(ASTContext &ctx, BasicValueFactory &bv,
                 llvm::BumpPtrAllocator& bpalloc)
       : SymbolDependencies(16), BPAlloc(bpalloc), BV(bv), Ctx(ctx) {}
-  ~SymbolManager();
 
   static bool canSymbolicate(QualType T);
 
index e655aae..ff6cd21 100644 (file)
@@ -341,10 +341,6 @@ QualType SymbolRegionValue::getType() const {
   return R->getValueType();
 }
 
-SymbolManager::~SymbolManager() {
-  llvm::DeleteContainerSeconds(SymbolDependencies);
-}
-
 bool SymbolManager::canSymbolicate(QualType T) {
   T = T.getCanonicalType();
 
@@ -362,13 +358,9 @@ bool SymbolManager::canSymbolicate(QualType T) {
 
 void SymbolManager::addSymbolDependency(const SymbolRef Primary,
                                         const SymbolRef Dependent) {
-  SymbolDependTy::iterator I = SymbolDependencies.find(Primary);
-  SymbolRefSmallVectorTy *dependencies = nullptr;
-  if (I == SymbolDependencies.end()) {
-    dependencies = new SymbolRefSmallVectorTy();
-    SymbolDependencies[Primary] = dependencies;
-  } else {
-    dependencies = I->second;
+  auto &dependencies = SymbolDependencies[Primary];
+  if (!dependencies) {
+    dependencies = std::make_unique<SymbolRefSmallVectorTy>();
   }
   dependencies->push_back(Dependent);
 }
@@ -378,7 +370,7 @@ const SymbolRefSmallVectorTy *SymbolManager::getDependentSymbols(
   SymbolDependTy::const_iterator I = SymbolDependencies.find(Primary);
   if (I == SymbolDependencies.end())
     return nullptr;
-  return I->second;
+  return I->second.get();
 }
 
 void SymbolReaper::markDependentsLive(SymbolRef sym) {