From: David Blaikie Date: Wed, 29 Apr 2020 03:54:57 +0000 (-0700) Subject: AnalysisDeclContext::ManagedAnalyses: Use unique_ptr to simplify memory management X-Git-Tag: llvmorg-12-init~7497 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e265f92b6e5e56b21fefdb83aec90f6e39c94857;p=platform%2Fupstream%2Fllvm.git AnalysisDeclContext::ManagedAnalyses: Use unique_ptr to simplify memory management --- diff --git a/clang/include/clang/Analysis/Analyses/LiveVariables.h b/clang/include/clang/Analysis/Analyses/LiveVariables.h index a46c35e..2e7dd5d 100644 --- a/clang/include/clang/Analysis/Analyses/LiveVariables.h +++ b/clang/include/clang/Analysis/Analyses/LiveVariables.h @@ -70,8 +70,8 @@ public: ~LiveVariables() override; /// Compute the liveness information for a given CFG. - static LiveVariables *computeLiveness(AnalysisDeclContext &analysisContext, - bool killAtAssign); + static std::unique_ptr + computeLiveness(AnalysisDeclContext &analysisContext, bool killAtAssign); /// Return true if a variable is live at the end of a /// specified block. @@ -97,7 +97,8 @@ public: void runOnAllBlocks(Observer &obs); - static LiveVariables *create(AnalysisDeclContext &analysisContext) { + static std::unique_ptr + create(AnalysisDeclContext &analysisContext) { return computeLiveness(analysisContext, true); } @@ -110,7 +111,8 @@ private: class RelaxedLiveVariables : public LiveVariables { public: - static LiveVariables *create(AnalysisDeclContext &analysisContext) { + static std::unique_ptr + 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 08fda09..1000298 100644 --- a/clang/include/clang/Analysis/Analyses/PostOrderCFGView.h +++ b/clang/include/clang/Analysis/Analyses/PostOrderCFGView.h @@ -108,7 +108,8 @@ public: // Used by AnalyisContext to construct this object. static const void *getTag(); - static PostOrderCFGView *create(AnalysisDeclContext &analysisContext); + static std::unique_ptr + create(AnalysisDeclContext &analysisContext); }; } // namespace clang diff --git a/clang/include/clang/Analysis/AnalysisDeclContext.h b/clang/include/clang/Analysis/AnalysisDeclContext.h index ed554fe..6fe1e27 100644 --- a/clang/include/clang/Analysis/AnalysisDeclContext.h +++ b/clang/include/clang/Analysis/AnalysisDeclContext.h @@ -191,18 +191,17 @@ public: /// necessary or nullptr if the analysis could not run. template T *getAnalysis() { const void *tag = T::getTag(); - ManagedAnalysis *&data = getAnalysisImpl(tag); - if (!data) { + std::unique_ptr &data = getAnalysisImpl(tag); + if (!data) data = T::create(*this); - } - return static_cast(data); + return static_cast(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 &getAnalysisImpl(const void *tag); LocationContextManager &getLocationContextManager(); }; diff --git a/clang/lib/Analysis/AnalysisDeclContext.cpp b/clang/lib/Analysis/AnalysisDeclContext.cpp index 96d5807..783de64 100644 --- a/clang/lib/Analysis/AnalysisDeclContext.cpp +++ b/clang/lib/Analysis/AnalysisDeclContext.cpp @@ -50,7 +50,7 @@ using namespace clang; -using ManagedAnalysisMap = llvm::DenseMap; +using ManagedAnalysisMap = llvm::DenseMap>; 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 &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 f910338..d24c40b 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::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(new LiveVariables(LV)); } void LiveVariables::dumpBlockLiveness(const SourceManager &M) { diff --git a/clang/lib/Analysis/PostOrderCFGView.cpp b/clang/lib/Analysis/PostOrderCFGView.cpp index f79d000..0c09c0f 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::create(AnalysisDeclContext &ctx) { const CFG *cfg = ctx.getCFG(); if (!cfg) return nullptr; - return new PostOrderCFGView(cfg); + return std::make_unique(cfg); } const void *PostOrderCFGView::getTag() { static int x; return &x; }