[cfe-commits] r41797 - in /cfe/trunk: Analysis/DeadStores.cpp Analysis/LiveVariables.cpp Driver/ASTStreamers.cpp include/clang/Analysis/LiveVariables.h

Ted Kremenek kremenek at apple.com
Mon Sep 10 10:36:42 PDT 2007


Author: kremenek
Date: Mon Sep 10 12:36:42 2007
New Revision: 41797

URL: http://llvm.org/viewvc/llvm-project?rev=41797&view=rev
Log:
Fixed LiveVariables to no longer track the liveness of function pointers
that refer to direct function calls.

Modified interface of LiveVariables to only track liveness of VarDecls.
This cleans up a bunch of edge cases, and removed the bug just mentioned.

Modified:
    cfe/trunk/Analysis/DeadStores.cpp
    cfe/trunk/Analysis/LiveVariables.cpp
    cfe/trunk/Driver/ASTStreamers.cpp
    cfe/trunk/include/clang/Analysis/LiveVariables.h

Modified: cfe/trunk/Analysis/DeadStores.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Analysis/DeadStores.cpp?rev=41797&r1=41796&r2=41797&view=diff

==============================================================================
--- cfe/trunk/Analysis/DeadStores.cpp (original)
+++ cfe/trunk/Analysis/DeadStores.cpp Mon Sep 10 12:36:42 2007
@@ -36,29 +36,28 @@
         return;
       
       // Is this an assignment to a variable?
-      if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS())) {
+      if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(B->getLHS()))
         // Is the variable live?
-        if (!L.isLive(Live,DR->getDecl())) {
+        if (!L.isLive(Live,cast<VarDecl>(DR->getDecl()))) {
           SourceRange R = B->getRHS()->getSourceRange();
           PP.getDiagnostics().Report(DR->getSourceRange().Begin(),
                                      diag::warn_dead_store, 0, 0,
                                      &R,1);
                                                                         
         }
-      }
     }
     else if(DeclStmt* DS = dyn_cast<DeclStmt>(S)) {
       // Iterate through the decls.  Warn if any of them (which have
       // initializers) are not live.
-      for (Decl* D = DS->getDecl() ; D != NULL ; D = D->getNextDeclarator())
-        if (VarDecl* V = dyn_cast<VarDecl>(D))
-          if (Expr* E = V->getInit())
-            if (!L.isLive(Live,D)) {
-              SourceRange R = E->getSourceRange();
-              PP.getDiagnostics().Report(D->getLocation(),
-                                         diag::warn_dead_store, 0, 0,
-                                         &R,1);
-            }
+      for (VarDecl* V = cast<VarDecl>(DS->getDecl()); V != NULL ; 
+                    V = cast<VarDecl>(V->getNextDeclarator()))
+        if (Expr* E = V->getInit())
+          if (!L.isLive(Live,V)) {
+            SourceRange R = E->getSourceRange();
+            PP.getDiagnostics().Report(V->getLocation(),
+                                       diag::warn_dead_store, 0, 0,
+                                       &R,1);
+          }
     }
   }
 };

Modified: cfe/trunk/Analysis/LiveVariables.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Analysis/LiveVariables.cpp?rev=41797&r1=41796&r2=41797&view=diff

==============================================================================
--- cfe/trunk/Analysis/LiveVariables.cpp (original)
+++ cfe/trunk/Analysis/LiveVariables.cpp Mon Sep 10 12:36:42 2007
@@ -65,10 +65,12 @@
 }
 
 void RegisterDecls::Register(Decl* D) {
-  LiveVariables::VPair& VP = L.getVarInfoMap()[const_cast<const Decl*>(D)];
+  if (VarDecl* V = dyn_cast<VarDecl>(D)) {
+    LiveVariables::VPair& VP = L.getVarInfoMap()[V];
 
-  VP.V.AliveBlocks.reserve(cfg.getNumBlockIDs());
-  VP.Idx = L.getNumDecls()++;
+    VP.V.AliveBlocks.resize(cfg.getNumBlockIDs());
+    VP.Idx = L.getNumDecls()++;
+  }
 }
 
 void RegisterDecls::RegisterUsedDecls() {
@@ -135,7 +137,7 @@
   void VisitDeclStmt(DeclStmt* DS);
   void VisitUnaryOperator(UnaryOperator* U);
 
-  unsigned getIdx(const Decl* D) {
+  unsigned getIdx(const VarDecl* D) {
     LiveVariables::VarInfoMap& V = L.getVarInfoMap();
     LiveVariables::VarInfoMap::iterator I = V.find(D);
     assert (I != V.end());
@@ -144,7 +146,7 @@
   
   bool ProcessBlock(const CFGBlock* B);
   llvm::BitVector* getBlockEntryLiveness(const CFGBlock* B);
-  LiveVariables::VarInfo& KillVar(Decl* D);
+  LiveVariables::VarInfo& KillVar(VarDecl* D);
 };
 
 void LivenessTFuncs::VisitStmt(Stmt* S) {
@@ -163,7 +165,8 @@
     Observer->ObserveStmt(DR,L,Live);
     
   // Register a use of the variable.
-  Live.set(getIdx(DR->getDecl()));
+  if (VarDecl* V = dyn_cast<VarDecl>(DR->getDecl()))
+    Live.set(getIdx(V));
 }
 
 void LivenessTFuncs::VisitStmtExpr(StmtExpr* S) {
@@ -205,7 +208,8 @@
         }
         else if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(S)) {
           // Treat the --/++/& operator as a kill.
-          LiveVariables::VarInfo& V = KillVar(DR->getDecl());
+          LiveVariables::VarInfo& V = 
+            KillVar(cast<VarDecl>(DR->getDecl()));
 
           if (!blockPreviouslyProcessed)
             V.AddKill(CurrentStmt,DR); 
@@ -225,7 +229,7 @@
   }
 }
 
-LiveVariables::VarInfo& LivenessTFuncs::KillVar(Decl* D) {
+LiveVariables::VarInfo& LivenessTFuncs::KillVar(VarDecl* D) {
   LiveVariables::VarInfoMap::iterator I =  L.getVarInfoMap().find(D);
   
   assert (I != L.getVarInfoMap().end() && 
@@ -247,7 +251,7 @@
   Stmt* LHS = B->getLHS();
   
   if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(LHS)) {
-    LiveVariables::VarInfo& V = KillVar(DR->getDecl());
+    LiveVariables::VarInfo& V = KillVar(cast<VarDecl>(DR->getDecl()));
     
     // We only need to register kills once, so we check if this block
     // has been previously processed.
@@ -272,7 +276,7 @@
   // in the sense that the value is obliterated, so we do not register
   // DeclStmts as a "kill site" for a variable.
   for (Decl* D = DS->getDecl(); D != NULL ; D = D->getNextDeclarator())
-    KillVar(D);
+    KillVar(cast<VarDecl>(D));
 }
 
 llvm::BitVector* LivenessTFuncs::getBlockEntryLiveness(const CFGBlock* B) {
@@ -385,7 +389,7 @@
 // liveness queries
 //
 
-bool LiveVariables::isLive(const CFGBlock* B, const Decl* D) const {
+bool LiveVariables::isLive(const CFGBlock* B, const VarDecl* D) const {
   BlockLivenessMap::const_iterator I = LiveAtBlockEntryMap.find(B);
   assert (I != LiveAtBlockEntryMap.end());
   
@@ -395,13 +399,13 @@
   return I->second[VI->second.Idx];
 }
 
-bool LiveVariables::isLive(llvm::BitVector& Live, const Decl* D) const {
+bool LiveVariables::isLive(llvm::BitVector& Live, const VarDecl* D) const {
   VarInfoMap::const_iterator VI = VarInfos.find(D);
   assert (VI != VarInfos.end());
   return Live[VI->second.Idx];
 }
 
-bool LiveVariables::KillsVar(const Stmt* S, const Decl* D) const {
+bool LiveVariables::KillsVar(const Stmt* S, const VarDecl* D) const {
   VarInfoMap::const_iterator VI = VarInfos.find(D);
   assert (VI != VarInfos.end());
   
@@ -413,13 +417,13 @@
   return false;        
 }
 
-LiveVariables::VarInfo& LiveVariables::getVarInfo(const Decl* D) {
+LiveVariables::VarInfo& LiveVariables::getVarInfo(const VarDecl* D) {
   VarInfoMap::iterator VI = VarInfos.find(D);
   assert (VI != VarInfos.end());
   return VI->second.V;
 }
 
-const LiveVariables::VarInfo& LiveVariables::getVarInfo(const Decl* D) const {
+const LiveVariables::VarInfo& LiveVariables::getVarInfo(const VarDecl* D) const{
   return const_cast<LiveVariables*>(this)->getVarInfo(D);
 }
 
@@ -465,4 +469,44 @@
             
     dumpLiveness(I->second,M);
   }
+
+  fprintf(stderr,"\n");
+}
+
+void LiveVariables::dumpVarLiveness(SourceManager& SM) const {
+  
+  for (VarInfoMap::iterator I = VarInfos.begin(), E=VarInfos.end(); I!=E; ++I) {      
+      SourceLocation PhysLoc = SM.getPhysicalLoc(I->first->getLocation());
+      
+    fprintf(stderr, "[ %s <%s:%u:%u> ]\n", 
+            I->first->getIdentifier()->getName(),
+            SM.getSourceName(PhysLoc),
+            SM.getLineNumber(PhysLoc),
+            SM.getColumnNumber(PhysLoc));
+            
+    I->second.V.Dump(SM);
+  } 
+}                                  
+
+void LiveVariables::VarInfo::Dump(SourceManager& SM) const {
+  fprintf(stderr,"  Blocks Alive:");
+  for (unsigned i = 0; i < AliveBlocks.size(); ++i) {
+    if (i % 5 == 0)
+      fprintf(stderr,"\n    ");
+
+    fprintf(stderr," B%d", i);
+  }
+  
+  fprintf(stderr,"\n  Kill Sites:\n");
+  for (KillsSet::const_iterator I = Kills.begin(), E = Kills.end(); I!=E; ++I) {
+    SourceLocation PhysLoc = 
+      SM.getPhysicalLoc(I->second->getSourceRange().Begin());
+      
+    fprintf(stderr, "    <%s:%u:%u>\n",
+            SM.getSourceName(PhysLoc),
+            SM.getLineNumber(PhysLoc),
+            SM.getColumnNumber(PhysLoc));
+  }
+  
+  fprintf(stderr,"\n");
 }
\ No newline at end of file

Modified: cfe/trunk/Driver/ASTStreamers.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Driver/ASTStreamers.cpp?rev=41797&r1=41796&r2=41797&view=diff

==============================================================================
--- cfe/trunk/Driver/ASTStreamers.cpp (original)
+++ cfe/trunk/Driver/ASTStreamers.cpp Mon Sep 10 12:36:42 2007
@@ -240,7 +240,8 @@
     virtual void VisitCFG(CFG& C) {
       LiveVariables L;
       L.runOnCFG(C);
-      L.dumpBlockLiveness(PP.getSourceManager());
+      L.dumpBlockLiveness(PP.getSourceManager());    
+      L.dumpVarLiveness(PP.getSourceManager());
     }
   };
 } // end anonymous namespace

Modified: cfe/trunk/include/clang/Analysis/LiveVariables.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/LiveVariables.h?rev=41797&r1=41796&r2=41797&view=diff

==============================================================================
--- cfe/trunk/include/clang/Analysis/LiveVariables.h (original)
+++ cfe/trunk/include/clang/Analysis/LiveVariables.h Mon Sep 10 12:36:42 2007
@@ -22,7 +22,7 @@
 
   class Stmt;
   class DeclRefExpr;
-  class Decl;
+  class VarDecl;
   class CFG;
   class CFGBlock;
   class SourceManager;
@@ -45,7 +45,7 @@
   ///  then V contains the liveness information after the execution of
   ///  the given block.
   virtual void ObserveBlockExit(const CFGBlock* B, LiveVariables& L,
-                              llvm::BitVector& V);  
+                                llvm::BitVector& V);
 };
 
 class LiveVariables {
@@ -70,7 +70,10 @@
     void AddKill(Stmt* S, DeclRefExpr* DR) {
       Kills.push_back(std::make_pair(const_cast<const Stmt*>(S),
                                      const_cast<const DeclRefExpr*>(DR)));
-    }                                 
+    }
+    
+    // Dump - prints VarInfo data to stderr.
+    void Dump(SourceManager& M) const;
   };
   
   struct VPair {
@@ -78,7 +81,7 @@
     unsigned Idx;
   };
   
-  typedef llvm::DenseMap<const Decl*, VPair > VarInfoMap;
+  typedef llvm::DenseMap<const VarDecl*, VPair > VarInfoMap;
   typedef llvm::DenseMap<const CFGBlock*, llvm::BitVector > BlockLivenessMap;
 
 public:
@@ -96,22 +99,22 @@
   
   /// KillsVar - Return true if the specified statement kills the
   ///  specified variable.
-  bool KillsVar(const Stmt* S, const Decl* D) const;
+  bool KillsVar(const Stmt* S, const VarDecl* D) const;
   
   /// IsLive - Return true if a variable is live at beginning of a specified
   //    block.
-  bool isLive(const CFGBlock* B, const Decl* D) const;
+  bool isLive(const CFGBlock* B, const VarDecl* D) const;
   
   /// IsLive - Return true if a variable is live according to the provided
   ///  livness bitvector.  This is typically used by classes that subclass
   ///  LiveVariablesObserver.
-  bool isLive(llvm::BitVector& V, const Decl* D) const;
+  bool isLive(llvm::BitVector& V, const VarDecl* D) const;
   
   /// getVarInfo - Return the liveness information associated with a given
   ///  variable.
-  VarInfo& getVarInfo(const Decl* D);
+  VarInfo& getVarInfo(const VarDecl* D);
 
-  const VarInfo& getVarInfo(const Decl* D) const;
+  const VarInfo& getVarInfo(const VarDecl* D) const;
   
   /// getVarInfoMap
   VarInfoMap& getVarInfoMap() { return VarInfos; }
@@ -124,6 +127,9 @@
   // dumpBlockLiveness
   void dumpBlockLiveness(SourceManager& M) const;
   
+  // dumpVarLiveness
+  void dumpVarLiveness(SourceManager& M) const;
+  
   // getLiveAtBlockEntryMap
   BlockLivenessMap& getLiveAtBlockEntryMap() { return LiveAtBlockEntryMap; }
 





More information about the cfe-commits mailing list