[cfe-commits] r141425 - in /cfe/trunk: include/clang/Analysis/Analyses/LiveVariables.h include/clang/Analysis/AnalysisContext.h include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h lib/Analysis/AnalysisContext.cpp lib/Analysis/LiveVariables.cpp lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp lib/StaticAnalyzer/Checkers/DebugCheckers.cpp lib/StaticAnalyzer/Core/SymbolManager.cpp lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Ted Kremenek kremenek at apple.com
Fri Oct 7 15:21:02 PDT 2011


Author: kremenek
Date: Fri Oct  7 17:21:02 2011
New Revision: 141425

URL: http://llvm.org/viewvc/llvm-project?rev=141425&view=rev
Log:
Remove AnalysisContext::getLiveVariables(), and introduce a templatized mechanism to lazily create analyses that are attached to AnalysisContext objects.

Modified:
    cfe/trunk/include/clang/Analysis/Analyses/LiveVariables.h
    cfe/trunk/include/clang/Analysis/AnalysisContext.h
    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h
    cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h
    cfe/trunk/lib/Analysis/AnalysisContext.cpp
    cfe/trunk/lib/Analysis/LiveVariables.cpp
    cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
    cfe/trunk/lib/StaticAnalyzer/Checkers/DebugCheckers.cpp
    cfe/trunk/lib/StaticAnalyzer/Core/SymbolManager.cpp
    cfe/trunk/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Modified: cfe/trunk/include/clang/Analysis/Analyses/LiveVariables.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/Analyses/LiveVariables.h?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/Analyses/LiveVariables.h (original)
+++ cfe/trunk/include/clang/Analysis/Analyses/LiveVariables.h Fri Oct  7 17:21:02 2011
@@ -14,6 +14,7 @@
 #ifndef LLVM_CLANG_LIVEVARIABLES_H
 #define LLVM_CLANG_LIVEVARIABLES_H
 
+#include "clang/Analysis/AnalysisContext.h"
 #include "clang/AST/Decl.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/ImmutableSet.h"
@@ -25,9 +26,8 @@
 class Stmt;
 class DeclRefExpr;
 class SourceManager;
-class AnalysisContext;
   
-class LiveVariables {
+class LiveVariables : public ManagedAnalysis {
 public:
   class LivenessValues {
   public:
@@ -67,11 +67,11 @@
   };    
 
 
-  ~LiveVariables();
+  virtual ~LiveVariables();
   
   /// Compute the liveness information for a given CFG.
   static LiveVariables *computeLiveness(AnalysisContext &analysisContext,
-                                          bool killAtAssign = true);
+                                        bool killAtAssign);
   
   /// Return true if a variable is live at the end of a
   /// specified block.
@@ -92,12 +92,27 @@
   void dumpBlockLiveness(const SourceManager& M);
 
   void runOnAllBlocks(Observer &obs);
-
+  
+  static LiveVariables *create(AnalysisContext &analysisContext) {
+    return computeLiveness(analysisContext, true);
+  }
+  
+  static const void *getTag();
+  
 private:
   LiveVariables(void *impl);
   void *impl;
 };
   
+class RelaxedLiveVariables : public LiveVariables {
+public:
+  static LiveVariables *create(AnalysisContext &analysisContext) {
+    return computeLiveness(analysisContext, false);
+  }
+  
+  static const void *getTag();
+};
+  
 } // end namespace clang
 
 #endif

Modified: cfe/trunk/include/clang/Analysis/AnalysisContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/AnalysisContext.h?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/AnalysisContext.h (original)
+++ cfe/trunk/include/clang/Analysis/AnalysisContext.h Fri Oct  7 17:21:02 2011
@@ -32,14 +32,35 @@
 class CFGReverseBlockReachabilityAnalysis;
 class CFGStmtMap;
 class LiveVariables;
+class ManagedAnalysis;
 class ParentMap;
 class PseudoConstantAnalysis;
 class ImplicitParamDecl;
 class LocationContextManager;
 class StackFrameContext;
-
+  
 namespace idx { class TranslationUnit; }
 
+/// The base class of a hierarchy of objects representing analyses tied
+/// to AnalysisContext.
+class ManagedAnalysis {
+protected:
+  ManagedAnalysis() {}
+public:
+  virtual ~ManagedAnalysis();
+  
+  // Subclasses need to implement:
+  //
+  //  static const void *getTag();
+  //
+  // Which returns a fixed pointer address to distinguish classes of
+  // analysis objects.  They also need to implement:
+  //
+  //  static [Derived*] create(AnalysisContext &Ctx);
+  //
+  // which creates the analysis object given an AnalysisContext.
+};
+  
 /// AnalysisContext contains the context data for the function or method under
 /// analysis.
 class AnalysisContext {
@@ -67,6 +88,8 @@
   // FIXME: remove.
   llvm::DenseMap<const BlockDecl*,void*> *ReferencedBlockVars;
 
+  void *ManagedAnalyses;
+
 public:
   AnalysisContext(const Decl *d, idx::TranslationUnit *tu);
 
@@ -122,8 +145,6 @@
 
   ParentMap &getParentMap();
   PseudoConstantAnalysis *getPseudoConstantAnalysis();
-  LiveVariables *getLiveVariables();
-  LiveVariables *getRelaxedLiveVariables();
 
   typedef const VarDecl * const * referenced_decls_iterator;
 
@@ -133,6 +154,20 @@
   /// Return the ImplicitParamDecl* associated with 'self' if this
   /// AnalysisContext wraps an ObjCMethodDecl.  Returns NULL otherwise.
   const ImplicitParamDecl *getSelfDecl() const;
+  
+  /// Return the specified analysis object, lazily running the analysis if
+  /// necessary.  Return NULL if the analysis could not run.
+  template <typename T>
+  T *getAnalysis() {
+    const void *tag = T::getTag();
+    ManagedAnalysis *&data = getAnalysisImpl(tag);
+    if (!data) {
+      data = T::create(*this);
+    }
+    return static_cast<T*>(data);
+  }
+private:
+  ManagedAnalysis *&getAnalysisImpl(const void* tag);
 };
 
 class AnalysisContextManager {
@@ -196,8 +231,9 @@
 
   CFG *getCFG() const { return getAnalysisContext()->getCFG(); }
 
-  LiveVariables *getLiveVariables() const {
-    return getAnalysisContext()->getLiveVariables();
+  template <typename T>
+  T *getAnalysis() const {
+    return getAnalysisContext()->getAnalysis<T>();
   }
 
   ParentMap &getParentMap() const {

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h Fri Oct  7 17:21:02 2011
@@ -172,8 +172,9 @@
     return AnaCtxMgr.getContext(D)->getCFG();
   }
 
-  LiveVariables *getLiveVariables(Decl const *D) {
-    return AnaCtxMgr.getContext(D)->getLiveVariables();
+  template <typename T>
+  T *getAnalysis(Decl const *D) {
+    return AnaCtxMgr.getContext(D)->getAnalysis<T>();
   }
 
   ParentMap &getParentMap(Decl const *D) {

Modified: cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h (original)
+++ cfe/trunk/include/clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h Fri Oct  7 17:21:02 2011
@@ -138,8 +138,9 @@
 
   ParentMap &getParentMap() const {return getLocationContext()->getParentMap();}
 
-  LiveVariables &getLiveVariables() const { 
-    return *getLocationContext()->getLiveVariables(); 
+  template <typename T>
+  T &getAnalysis() const {
+    return *getLocationContext()->getAnalysis<T>();
   }
 
   const ProgramState *getState() const { return State; }

Modified: cfe/trunk/lib/Analysis/AnalysisContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/AnalysisContext.cpp?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/AnalysisContext.cpp (original)
+++ cfe/trunk/lib/Analysis/AnalysisContext.cpp Fri Oct  7 17:21:02 2011
@@ -30,6 +30,8 @@
 
 using namespace clang;
 
+typedef llvm::DenseMap<const void *, ManagedAnalysis *> ManagedAnalysisMap;
+
 AnalysisContext::AnalysisContext(const Decl *d,
                                  idx::TranslationUnit *tu,
                                  const CFG::BuildOptions &buildOptions)
@@ -38,7 +40,8 @@
     forcedBlkExprs(0),
     builtCFG(false),
     builtCompleteCFG(false),
-    ReferencedBlockVars(0)
+    ReferencedBlockVars(0),
+    ManagedAnalyses(0)
 {  
   cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
 }
@@ -49,7 +52,8 @@
   forcedBlkExprs(0),
   builtCFG(false),
   builtCompleteCFG(false),
-  ReferencedBlockVars(0)
+  ReferencedBlockVars(0),
+  ManagedAnalyses(0)
 {  
   cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
 }
@@ -176,18 +180,6 @@
   return PCA.get();
 }
 
-LiveVariables *AnalysisContext::getLiveVariables() {
-  if (!liveness)
-    liveness.reset(LiveVariables::computeLiveness(*this));
-  return liveness.get();
-}
-
-LiveVariables *AnalysisContext::getRelaxedLiveVariables() {
-  if (!relaxedLiveness)
-    relaxedLiveness.reset(LiveVariables::computeLiveness(*this, false));
-  return relaxedLiveness.get();
-}
-
 AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
                                                     idx::TranslationUnit *TU) {
   AnalysisContext *&AC = Contexts[D];
@@ -395,13 +387,29 @@
   return std::make_pair(V->begin(), V->end());
 }
 
+ManagedAnalysis *&AnalysisContext::getAnalysisImpl(const void *tag) {
+  if (!ManagedAnalyses)
+    ManagedAnalyses = new ManagedAnalysisMap();
+  ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
+  return (*M)[tag];
+}
+
 //===----------------------------------------------------------------------===//
 // Cleanup.
 //===----------------------------------------------------------------------===//
 
+ManagedAnalysis::~ManagedAnalysis() {}
+
 AnalysisContext::~AnalysisContext() {
   delete forcedBlkExprs;
   delete ReferencedBlockVars;
+  // Release the managed analyses.
+  if (ManagedAnalyses) {
+    ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
+    for (ManagedAnalysisMap::iterator I = M->begin(), E = M->end(); I!=E; ++I)
+      delete I->second;  
+    delete M;
+  }
 }
 
 AnalysisContextManager::~AnalysisContextManager() {

Modified: cfe/trunk/lib/Analysis/LiveVariables.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/LiveVariables.cpp?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/LiveVariables.cpp (original)
+++ cfe/trunk/lib/Analysis/LiveVariables.cpp Fri Oct  7 17:21:02 2011
@@ -670,3 +670,5 @@
   llvm::errs() << "\n";  
 }
 
+const void *LiveVariables::getTag() { static int x; return &x; }
+const void *RelaxedLiveVariables::getTag() { static int x; return &x; }

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/DeadStoresChecker.cpp Fri Oct  7 17:21:02 2011
@@ -348,7 +348,7 @@
 public:
   void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
                         BugReporter &BR) const {
-    if (LiveVariables *L = mgr.getLiveVariables(D)) {
+    if (LiveVariables *L = mgr.getAnalysis<LiveVariables>(D)) {
       CFG &cfg = *mgr.getCFG(D);
       AnalysisContext *AC = mgr.getAnalysisContext(D);
       ParentMap &pmap = mgr.getParentMap(D);

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/DebugCheckers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/DebugCheckers.cpp?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/DebugCheckers.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/DebugCheckers.cpp Fri Oct  7 17:21:02 2011
@@ -28,7 +28,7 @@
 public:
   void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
                         BugReporter &BR) const {
-    if (LiveVariables* L = mgr.getLiveVariables(D)) {
+    if (LiveVariables* L = mgr.getAnalysis<LiveVariables>(D)) {
       L->dumpBlockLiveness(mgr.getSourceManager());
     }
   }

Modified: cfe/trunk/lib/StaticAnalyzer/Core/SymbolManager.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/SymbolManager.cpp?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/SymbolManager.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/SymbolManager.cpp Fri Oct  7 17:21:02 2011
@@ -382,8 +382,7 @@
 }
 
 bool SymbolReaper::isLive(const Stmt *ExprVal) const {
-  return LCtx->getAnalysisContext()->getRelaxedLiveVariables()->
-      isLive(Loc, ExprVal);
+  return LCtx->getAnalysis<RelaxedLiveVariables>()->isLive(Loc, ExprVal);
 }
 
 bool SymbolReaper::isLive(const VarRegion *VR, bool includeStoreBindings) const{
@@ -391,8 +390,7 @@
   const StackFrameContext *CurrentContext = LCtx->getCurrentStackFrame();
 
   if (VarContext == CurrentContext) {
-    if (LCtx->getAnalysisContext()->getRelaxedLiveVariables()->
-          isLive(Loc, VR->getDecl()))
+    if (LCtx->getAnalysis<RelaxedLiveVariables>()->isLive(Loc, VR->getDecl()))
       return true;
 
     if (!includeStoreBindings)

Modified: cfe/trunk/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp?rev=141425&r1=141424&r2=141425&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp Fri Oct  7 17:21:02 2011
@@ -304,10 +304,9 @@
 
 static void ActionExprEngine(AnalysisConsumer &C, AnalysisManager &mgr,
                              Decl *D, bool ObjCGCEnabled) {
-  // Construct the analysis engine.  We first query for the LiveVariables
-  // information to see if the CFG is valid.
+  // Construct the analysis engine.  First check if the CFG is valid.
   // FIXME: Inter-procedural analysis will need to handle invalid CFGs.
-  if (!mgr.getLiveVariables(D))
+  if (!mgr.getCFG(D))
     return;
   ExprEngine Eng(mgr, ObjCGCEnabled);
 





More information about the cfe-commits mailing list