From: Ted Kremenek Date: Tue, 29 Jan 2008 05:13:23 +0000 (+0000) Subject: Modified LiveVariables to perform all of its base initialization in the ctor, X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bffaa831478baf584e74dfb7917861e865d99640;p=clang Modified LiveVariables to perform all of its base initialization in the ctor, and now we require a FunctionDecl* object so that we can also keep track of all of the ParmDecls. Modified clients of LiveVariables to conform to the new interface. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46490 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Analysis/DeadStores.cpp b/Analysis/DeadStores.cpp index 926109e1b2..4475d495fb 100644 --- a/Analysis/DeadStores.cpp +++ b/Analysis/DeadStores.cpp @@ -76,8 +76,10 @@ public: namespace clang { -void CheckDeadStores(CFG& cfg, ASTContext &Ctx, Diagnostic &Diags) { - LiveVariables L(cfg); +void CheckDeadStores(CFG& cfg, FunctionDecl& FD, ASTContext &Ctx, + Diagnostic &Diags) { + + LiveVariables L(cfg, FD); L.runOnCFG(cfg); DeadStoreObs A(Ctx, Diags); L.runOnAllBlocks(cfg,&A); diff --git a/Analysis/GRConstants.cpp b/Analysis/GRConstants.cpp index a0a58db79a..103e6a0d74 100644 --- a/Analysis/GRConstants.cpp +++ b/Analysis/GRConstants.cpp @@ -577,7 +577,7 @@ protected: /// Liveness - live-variables information the ValueDecl* and block-level /// Expr* in the CFG. Used to prune out dead state. - LiveVariables* Liveness; + LiveVariables Liveness; /// Builder - The current GRNodeBuilder which is used when building the nodes /// for a given statement. @@ -600,17 +600,14 @@ protected: ASTContext& getContext() const { return G.getContext(); } public: - GRConstants(GraphTy& g) : G(g), Liveness(NULL), Builder(NULL), - ValMgr(G.getContext()), StmtEntryNode(NULL), CurrentStmt(NULL) { + GRConstants(GraphTy& g) : G(g), Liveness(G.getCFG(), G.getFunctionDecl()), + Builder(NULL), ValMgr(G.getContext()), StmtEntryNode(NULL), + CurrentStmt(NULL) { // Compute liveness information. - CFG& c = G.getCFG(); - Liveness = new LiveVariables(c); - Liveness->runOnCFG(c); - Liveness->runOnAllBlocks(c, NULL, true); + Liveness.runOnCFG(G.getCFG()); + Liveness.runOnAllBlocks(G.getCFG(), NULL, true); } - - ~GRConstants() { delete Liveness; } /// getCFG - Returns the CFG associated with this analysis. CFG& getCFG() { return G.getCFG(); } @@ -843,14 +840,14 @@ GRConstants::StateTy GRConstants::RemoveDeadBindings(Stmt* Loc, StateTy M) { // Remove old bindings for subexpressions and "dead" block-level expressions. for (; I!=E && !I.getKey().isDecl(); ++I) { - if (I.getKey().isSubExpr() || !Liveness->isLive(Loc,cast(I.getKey()))) + if (I.getKey().isSubExpr() || !Liveness.isLive(Loc,cast(I.getKey()))) M = StateMgr.Remove(M, I.getKey()); } // Remove bindings for "dead" decls. for (; I!=E && I.getKey().isDecl(); ++I) if (VarDecl* V = dyn_cast(cast(I.getKey()))) - if (!Liveness->isLive(Loc, V)) + if (!Liveness.isLive(Loc, V)) M = StateMgr.Remove(M, I.getKey()); return M; diff --git a/Analysis/LiveVariables.cpp b/Analysis/LiveVariables.cpp index b96f7fc4bb..c14b463be1 100644 --- a/Analysis/LiveVariables.cpp +++ b/Analysis/LiveVariables.cpp @@ -42,7 +42,15 @@ public: }; } // end anonymous namespace -void LiveVariables::InitializeValues(const CFG& cfg) { + +LiveVariables::LiveVariables(CFG& cfg, FunctionDecl& FD) { + getAnalysisData().setCFG(&cfg); + + for (FunctionDecl::param_iterator I=FD.param_begin(), E=FD.param_end(); + I !=E; ++I) + getAnalysisData().Register(*I); + + // Now register all the other VarDecls; RegisterDecls R(getAnalysisData()); cfg.VisitBlockStmts(R); } diff --git a/Driver/ASTConsumers.cpp b/Driver/ASTConsumers.cpp index 69f344cac4..b96a905c95 100644 --- a/Driver/ASTConsumers.cpp +++ b/Driver/ASTConsumers.cpp @@ -449,7 +449,7 @@ namespace { class CFGVisitor : public ASTConsumer { public: // CFG Visitor interface to be implemented by subclass. - virtual void VisitCFG(CFG& C) = 0; + virtual void VisitCFG(CFG& C, FunctionDecl& FD) = 0; virtual bool printFuncDeclStart() { return true; } virtual void HandleTopLevelDecl(Decl *D); @@ -468,7 +468,7 @@ void CFGVisitor::HandleTopLevelDecl(Decl *D) { } CFG *C = CFG::buildCFG(FD->getBody()); - VisitCFG(*C); + VisitCFG(*C, *FD); delete C; } @@ -481,7 +481,7 @@ namespace { public: CFGDumper(bool use_graphviz) : UseGraphviz(use_graphviz) {} - virtual void VisitCFG(CFG &C) { + virtual void VisitCFG(CFG& C, FunctionDecl&) { if (UseGraphviz) C.viewCFG(); else @@ -505,8 +505,8 @@ namespace { SM = &Context.getSourceManager(); } - virtual void VisitCFG(CFG& C) { - LiveVariables L(C); + virtual void VisitCFG(CFG& C, FunctionDecl& FD) { + LiveVariables L(C, FD); L.runOnCFG(C); L.dumpBlockLiveness(*SM); } @@ -530,7 +530,10 @@ namespace { Ctx = &Context; } - virtual void VisitCFG(CFG& C) { CheckDeadStores(C, *Ctx, Diags); } + virtual void VisitCFG(CFG& C, FunctionDecl& FD) { + CheckDeadStores(C, FD, *Ctx, Diags); + } + virtual bool printFuncDeclStart() { return false; } }; } // end anonymous namespace @@ -553,7 +556,10 @@ namespace { Ctx = &Context; } - virtual void VisitCFG(CFG& C) { CheckUninitializedValues(C, *Ctx, Diags); } + virtual void VisitCFG(CFG& C, FunctionDecl&) { + CheckUninitializedValues(C, *Ctx, Diags); + } + virtual bool printFuncDeclStart() { return false; } }; } // end anonymous namespace @@ -566,12 +572,12 @@ ASTConsumer *clang::CreateUnitValsChecker(Diagnostic &Diags) { // GRConstants - Perform intra-procedural, path-sensitive constant propagation. namespace { - class GRConstantsVisitor : public ASTConsumer { + class GRConstantsVisitor : public CFGVisitor { ASTContext* Ctx; public: virtual void Initialize(ASTContext &Context) { Ctx = &Context; } - virtual void HandleTopLevelDecl(Decl *D); + virtual void VisitCFG(CFG& C, FunctionDecl&); }; } // end anonymous namespace @@ -579,18 +585,8 @@ ASTConsumer* clang::CreateGRConstants() { return new GRConstantsVisitor(); } -void GRConstantsVisitor::HandleTopLevelDecl(Decl *D) { - FunctionDecl *FD = dyn_cast(D); - - if (!FD || !FD->getBody()) - return; - - DeclPrinter().PrintFunctionDeclStart(FD); - llvm::cerr << '\n'; - - CFG *C = CFG::buildCFG(FD->getBody()); - RunGRConstants(*C, *FD, *Ctx); - delete C; +void GRConstantsVisitor::VisitCFG(CFG& C, FunctionDecl& FD) { + RunGRConstants(C, FD, *Ctx); } //===----------------------------------------------------------------------===// diff --git a/include/clang/Analysis/Analyses/LiveVariables.h b/include/clang/Analysis/Analyses/LiveVariables.h index 19e8d80029..c048cbbc4e 100644 --- a/include/clang/Analysis/Analyses/LiveVariables.h +++ b/include/clang/Analysis/Analyses/LiveVariables.h @@ -64,7 +64,7 @@ class LiveVariables : public DataflowValues