[clang] e265f92 - AnalysisDeclContext::ManagedAnalyses: Use unique_ptr to simplify memory management

David Blaikie via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 28 22:39:14 PDT 2020


Author: David Blaikie
Date: 2020-04-28T22:31:16-07:00
New Revision: e265f92b6e5e56b21fefdb83aec90f6e39c94857

URL: https://github.com/llvm/llvm-project/commit/e265f92b6e5e56b21fefdb83aec90f6e39c94857
DIFF: https://github.com/llvm/llvm-project/commit/e265f92b6e5e56b21fefdb83aec90f6e39c94857.diff

LOG: AnalysisDeclContext::ManagedAnalyses: Use unique_ptr to simplify memory management

Added: 
    

Modified: 
    clang/include/clang/Analysis/Analyses/LiveVariables.h
    clang/include/clang/Analysis/Analyses/PostOrderCFGView.h
    clang/include/clang/Analysis/AnalysisDeclContext.h
    clang/lib/Analysis/AnalysisDeclContext.cpp
    clang/lib/Analysis/LiveVariables.cpp
    clang/lib/Analysis/PostOrderCFGView.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Analysis/Analyses/LiveVariables.h b/clang/include/clang/Analysis/Analyses/LiveVariables.h
index a46c35ee5b30..2e7dd5d81678 100644
--- a/clang/include/clang/Analysis/Analyses/LiveVariables.h
+++ b/clang/include/clang/Analysis/Analyses/LiveVariables.h
@@ -70,8 +70,8 @@ class LiveVariables : public ManagedAnalysis {
   ~LiveVariables() override;
 
   /// Compute the liveness information for a given CFG.
-  static LiveVariables *computeLiveness(AnalysisDeclContext &analysisContext,
-                                        bool killAtAssign);
+  static std::unique_ptr<LiveVariables>
+  computeLiveness(AnalysisDeclContext &analysisContext, bool killAtAssign);
 
   /// Return true if a variable is live at the end of a
   /// specified block.
@@ -97,7 +97,8 @@ class LiveVariables : public ManagedAnalysis {
 
   void runOnAllBlocks(Observer &obs);
 
-  static LiveVariables *create(AnalysisDeclContext &analysisContext) {
+  static std::unique_ptr<LiveVariables>
+  create(AnalysisDeclContext &analysisContext) {
     return computeLiveness(analysisContext, true);
   }
 
@@ -110,7 +111,8 @@ class LiveVariables : public ManagedAnalysis {
 
 class RelaxedLiveVariables : public LiveVariables {
 public:
-  static LiveVariables *create(AnalysisDeclContext &analysisContext) {
+  static std::unique_ptr<LiveVariables>
+  create(AnalysisDeclContext &analysisContext) {
     return computeLiveness(analysisContext, false);
   }
 

diff  --git a/clang/include/clang/Analysis/Analyses/PostOrderCFGView.h b/clang/include/clang/Analysis/Analyses/PostOrderCFGView.h
index 08fda0982df4..100029894560 100644
--- a/clang/include/clang/Analysis/Analyses/PostOrderCFGView.h
+++ b/clang/include/clang/Analysis/Analyses/PostOrderCFGView.h
@@ -108,7 +108,8 @@ class PostOrderCFGView : public ManagedAnalysis {
   // Used by AnalyisContext to construct this object.
   static const void *getTag();
 
-  static PostOrderCFGView *create(AnalysisDeclContext &analysisContext);
+  static std::unique_ptr<PostOrderCFGView>
+  create(AnalysisDeclContext &analysisContext);
 };
 
 } // namespace clang

diff  --git a/clang/include/clang/Analysis/AnalysisDeclContext.h b/clang/include/clang/Analysis/AnalysisDeclContext.h
index ed554feedead..6fe1e27bda75 100644
--- a/clang/include/clang/Analysis/AnalysisDeclContext.h
+++ b/clang/include/clang/Analysis/AnalysisDeclContext.h
@@ -191,18 +191,17 @@ class AnalysisDeclContext {
   /// necessary or nullptr if the analysis could not run.
   template <typename T> T *getAnalysis() {
     const void *tag = T::getTag();
-    ManagedAnalysis *&data = getAnalysisImpl(tag);
-    if (!data) {
+    std::unique_ptr<ManagedAnalysis> &data = getAnalysisImpl(tag);
+    if (!data)
       data = T::create(*this);
-    }
-    return static_cast<T *>(data);
+    return static_cast<T *>(data.get());
   }
 
   /// \returns Whether the root namespace of \p D is the \c std C++ namespace.
   static bool isInStdNamespace(const Decl *D);
 
 private:
-  ManagedAnalysis *&getAnalysisImpl(const void *tag);
+  std::unique_ptr<ManagedAnalysis> &getAnalysisImpl(const void *tag);
 
   LocationContextManager &getLocationContextManager();
 };

diff  --git a/clang/lib/Analysis/AnalysisDeclContext.cpp b/clang/lib/Analysis/AnalysisDeclContext.cpp
index 96d5807bcdfc..783de6442645 100644
--- a/clang/lib/Analysis/AnalysisDeclContext.cpp
+++ b/clang/lib/Analysis/AnalysisDeclContext.cpp
@@ -50,7 +50,7 @@
 
 using namespace clang;
 
-using ManagedAnalysisMap = llvm::DenseMap<const void *, ManagedAnalysis *>;
+using ManagedAnalysisMap = llvm::DenseMap<const void *, std::unique_ptr<ManagedAnalysis>>;
 
 AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *ADCMgr,
                                          const Decl *D,
@@ -617,7 +617,7 @@ AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
   return llvm::make_range(V->begin(), V->end());
 }
 
-ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
+std::unique_ptr<ManagedAnalysis> &AnalysisDeclContext::getAnalysisImpl(const void *tag) {
   if (!ManagedAnalyses)
     ManagedAnalyses = new ManagedAnalysisMap();
   ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
@@ -633,12 +633,7 @@ ManagedAnalysis::~ManagedAnalysis() = default;
 AnalysisDeclContext::~AnalysisDeclContext() {
   delete forcedBlkExprs;
   delete ReferencedBlockVars;
-  // Release the managed analyses.
-  if (ManagedAnalyses) {
-    ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
-    llvm::DeleteContainerSeconds(*M);
-    delete M;
-  }
+  delete (ManagedAnalysisMap*) ManagedAnalyses;
 }
 
 LocationContext::~LocationContext() = default;

diff  --git a/clang/lib/Analysis/LiveVariables.cpp b/clang/lib/Analysis/LiveVariables.cpp
index f910338b0ad3..d24c40b457b4 100644
--- a/clang/lib/Analysis/LiveVariables.cpp
+++ b/clang/lib/Analysis/LiveVariables.cpp
@@ -490,9 +490,8 @@ LiveVariables::~LiveVariables() {
   delete (LiveVariablesImpl*) impl;
 }
 
-LiveVariables *
-LiveVariables::computeLiveness(AnalysisDeclContext &AC,
-                                 bool killAtAssign) {
+std::unique_ptr<LiveVariables>
+LiveVariables::computeLiveness(AnalysisDeclContext &AC, bool killAtAssign) {
 
   // No CFG?  Bail out.
   CFG *cfg = AC.getCFG();
@@ -565,7 +564,7 @@ LiveVariables::computeLiveness(AnalysisDeclContext &AC,
     worklist.enqueuePredecessors(block);
   }
 
-  return new LiveVariables(LV);
+  return std::unique_ptr<LiveVariables>(new LiveVariables(LV));
 }
 
 void LiveVariables::dumpBlockLiveness(const SourceManager &M) {

diff  --git a/clang/lib/Analysis/PostOrderCFGView.cpp b/clang/lib/Analysis/PostOrderCFGView.cpp
index f79d0007cb3d..0c09c0f97ff6 100644
--- a/clang/lib/Analysis/PostOrderCFGView.cpp
+++ b/clang/lib/Analysis/PostOrderCFGView.cpp
@@ -29,11 +29,12 @@ PostOrderCFGView::PostOrderCFGView(const CFG *cfg) {
   }
 }
 
-PostOrderCFGView *PostOrderCFGView::create(AnalysisDeclContext &ctx) {
+std::unique_ptr<PostOrderCFGView>
+PostOrderCFGView::create(AnalysisDeclContext &ctx) {
   const CFG *cfg = ctx.getCFG();
   if (!cfg)
     return nullptr;
-  return new PostOrderCFGView(cfg);
+  return std::make_unique<PostOrderCFGView>(cfg);
 }
 
 const void *PostOrderCFGView::getTag() { static int x; return &x; }


        


More information about the cfe-commits mailing list