[cfe-commits] r98111 - in /cfe/trunk: lib/Analysis/AnalysisContext.cpp test/Analysis/dead-stores.c

Ted Kremenek kremenek at apple.com
Tue Mar 9 16:18:11 PST 2010


Author: kremenek
Date: Tue Mar  9 18:18:11 2010
New Revision: 98111

URL: http://llvm.org/viewvc/llvm-project?rev=98111&view=rev
Log:
When computing in AnalysisContext the variables referenced
by a block, also look at the contained blocks.

Modified:
    cfe/trunk/lib/Analysis/AnalysisContext.cpp
    cfe/trunk/test/Analysis/dead-stores.c

Modified: cfe/trunk/lib/Analysis/AnalysisContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/AnalysisContext.cpp?rev=98111&r1=98110&r2=98111&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/AnalysisContext.cpp (original)
+++ cfe/trunk/lib/Analysis/AnalysisContext.cpp Tue Mar  9 18:18:11 2010
@@ -12,15 +12,16 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/Analysis/CFG.h"
-#include "clang/Analysis/AnalysisContext.h"
-#include "clang/Analysis/Analyses/LiveVariables.h"
 #include "clang/AST/Decl.h"
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/ParentMap.h"
 #include "clang/AST/StmtVisitor.h"
+#include "clang/Analysis/Analyses/LiveVariables.h"
+#include "clang/Analysis/AnalysisContext.h"
+#include "clang/Analysis/CFG.h"
 #include "clang/Analysis/Support/BumpVector.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/Support/ErrorHandling.h"
 
 using namespace clang;
@@ -207,11 +208,17 @@
   BumpVector<const VarDecl*> &BEVals;
   BumpVectorContext &BC;
   llvm::DenseMap<const VarDecl*, unsigned> Visited;
+  llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
 public:
   FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
                             BumpVectorContext &bc)
   : BEVals(bevals), BC(bc) {}
-  
+
+  bool IsTrackedDecl(const VarDecl *VD) {
+    const DeclContext *DC = VD->getDeclContext();
+    return IgnoredContexts.count(DC) == 0;
+  }
+
   void VisitStmt(Stmt *S) {
     for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
       if (Stmt *child = *I)
@@ -229,16 +236,23 @@
         }
       }
   }
-  
+
   void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
     if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
       unsigned &flag = Visited[VD];
       if (!flag) {
         flag = 1;
-        BEVals.push_back(VD, BC);
+        if (IsTrackedDecl(VD))
+          BEVals.push_back(VD, BC);
       }
     }
   }
+
+  void VisitBlockExpr(BlockExpr *BR) {
+    // Blocks containing blocks can transitively capture more variables.
+    IgnoredContexts.insert(BR->getBlockDecl());
+    Visit(BR->getBlockDecl()->getBody());
+  }
 };  
 } // end anonymous namespace
 

Modified: cfe/trunk/test/Analysis/dead-stores.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/dead-stores.c?rev=98111&r1=98110&r2=98111&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/dead-stores.c (original)
+++ cfe/trunk/test/Analysis/dead-stores.c Tue Mar  9 18:18:11 2010
@@ -1,8 +1,8 @@
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code %s
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code %s
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code %s
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code %s
-// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code %s
+// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
+// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
+// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=basic -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
+// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=basic -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
+// RUN: %clang_cc1 -analyze -analyzer-experimental-internal-checks -analyzer-check-objc-mem -analyzer-store=region -analyzer-constraints=range -analyzer-check-dead-stores -fblocks -verify -Wno-unreachable-code -analyzer-opt-analyze-nested-blocks %s
 
 void f1() {
   int k, y;
@@ -377,7 +377,7 @@
   // FIXME: One day this should be reported as dead since 'z = x + y' is dead.
   int x = (y > 2); // no-warning
   ^ {
-    int z = x + y; // FIXME: Eventually this should be reported as a dead store.
+      int z = x + y; // expected-warning{{Value stored to 'z' during its initialization is never read}}
   }();  
 }
 
@@ -429,3 +429,17 @@
   return z; 
 }
 
+int f26_nestedblocks() {
+  int z;
+  z = 1;
+  __block int y = 0;
+  ^{
+    int k;
+    k = 1; // expected-warning{{Value stored to 'k' is never read}}
+    ^{
+        y = z + 1;
+     }();
+  }();
+  return y;
+}
+





More information about the cfe-commits mailing list