[cfe-commits] r95459 - in /cfe/trunk: lib/Analysis/AnalysisContext.cpp lib/Checker/MemRegion.cpp lib/Checker/RegionStore.cpp test/Analysis/misc-ps-region-store.m

Ted Kremenek kremenek at apple.com
Fri Feb 5 16:30:00 PST 2010


Author: kremenek
Date: Fri Feb  5 18:30:00 2010
New Revision: 95459

URL: http://llvm.org/viewvc/llvm-project?rev=95459&view=rev
Log:
Teach RegionStore::InvalidateRegions() to also invalidate static variables referenced by blocks.

Modified:
    cfe/trunk/lib/Analysis/AnalysisContext.cpp
    cfe/trunk/lib/Checker/MemRegion.cpp
    cfe/trunk/lib/Checker/RegionStore.cpp
    cfe/trunk/test/Analysis/misc-ps-region-store.m

Modified: cfe/trunk/lib/Analysis/AnalysisContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/AnalysisContext.cpp?rev=95459&r1=95458&r2=95459&view=diff

==============================================================================
--- cfe/trunk/lib/Analysis/AnalysisContext.cpp (original)
+++ cfe/trunk/lib/Analysis/AnalysisContext.cpp Fri Feb  5 18:30:00 2010
@@ -194,6 +194,7 @@
 class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
   BumpVector<const VarDecl*> &BEVals;
   BumpVectorContext &BC;
+  llvm::DenseMap<const VarDecl*, unsigned> Visited;
 public:
   FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
                             BumpVectorContext &bc)
@@ -204,10 +205,27 @@
       if (Stmt *child = *I)
         Visit(child);
   }
+
+  void VisitDeclRefExpr(const DeclRefExpr *DR) {
+    // Non-local variables are also directly modified.
+    if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
+      if (!VD->hasLocalStorage()) {
+        unsigned &flag = Visited[VD];
+        if (!flag) {
+          flag = 1;
+          BEVals.push_back(VD, BC);
+        }
+      }
+  }
   
   void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
-    if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
-      BEVals.push_back(VD, BC);
+    if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
+      unsigned &flag = Visited[VD];
+      if (!flag) {
+        flag = 1;
+        BEVals.push_back(VD, BC);
+      }
+    }
   }
 };  
 } // end anonymous namespace

Modified: cfe/trunk/lib/Checker/MemRegion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/MemRegion.cpp?rev=95459&r1=95458&r2=95459&view=diff

==============================================================================
--- cfe/trunk/lib/Checker/MemRegion.cpp (original)
+++ cfe/trunk/lib/Checker/MemRegion.cpp Fri Feb  5 18:30:00 2010
@@ -759,7 +759,7 @@
     const VarDecl *VD = *I;
     const VarRegion *VR = 0;
     
-    if (!VD->getAttr<BlocksAttr>())
+    if (!VD->getAttr<BlocksAttr>() && VD->hasLocalStorage())
       VR = MemMgr.getVarRegion(VD, this);
     else {
       if (LC)

Modified: cfe/trunk/lib/Checker/RegionStore.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Checker/RegionStore.cpp?rev=95459&r1=95458&r2=95459&view=diff

==============================================================================
--- cfe/trunk/lib/Checker/RegionStore.cpp (original)
+++ cfe/trunk/lib/Checker/RegionStore.cpp Fri Feb  5 18:30:00 2010
@@ -606,10 +606,11 @@
     // by reference.
     if (const BlockDataRegion *BR = dyn_cast<BlockDataRegion>(baseR)) {
       for (BlockDataRegion::referenced_vars_iterator
-           I = BR->referenced_vars_begin(), E = BR->referenced_vars_end() ;
-           I != E; ++I) {
-        const VarRegion *VR = *I;
-        if (VR->getDecl()->getAttr<BlocksAttr>())
+           BI = BR->referenced_vars_begin(), BE = BR->referenced_vars_end() ;
+           BI != BE; ++BI) {
+        const VarRegion *VR = *BI;
+        const VarDecl *VD = VR->getDecl();
+        if (VD->getAttr<BlocksAttr>() || !VD->hasLocalStorage())
           AddToWorkList(VR);
       }
       continue;

Modified: cfe/trunk/test/Analysis/misc-ps-region-store.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/misc-ps-region-store.m?rev=95459&r1=95458&r2=95459&view=diff

==============================================================================
--- cfe/trunk/test/Analysis/misc-ps-region-store.m (original)
+++ cfe/trunk/test/Analysis/misc-ps-region-store.m Fri Feb  5 18:30:00 2010
@@ -595,14 +595,50 @@
 typedef void (^RDar7582031CB)(void);
 @interface RDar7582031
 - rdar7582031:RDar7582031CB;
+- rdar7582031_b:RDar7582031CB;
 @end
 
+// Test with one block.
 unsigned rdar7582031(RDar7582031 *o) {
   __block unsigned x;
   [o rdar7582031:^{ x = 1; }];
   return x; // no-warning
 }
 
+// Test with two blocks.
+unsigned long rdar7582031_b(RDar7582031 *o) {
+  __block unsigned y;
+  __block unsigned long x;
+  [o rdar7582031:^{ y = 1; }];
+  [o rdar7582031_b:^{ x = 1LL; }];
+  return x + (unsigned long) y; // no-warning
+}
+
+// Show we get an error when 'o' is null because the message
+// expression has no effect.
+unsigned long rdar7582031_b2(RDar7582031 *o) {
+  __block unsigned y;
+  __block unsigned long x;
+  if (o)
+    return 1;
+  [o rdar7582031:^{ y = 1; }];
+  [o rdar7582031_b:^{ x = 1LL; }];
+  return x + (unsigned long) y; // expected-warning{{The left operand of '+' is a garbage value}}
+}
+
+// Show that we handle static variables also getting invalidated.
+void rdar7582031_aux(void (^)(void));
+RDar7582031 *rdar7582031_aux_2();
+
+unsigned rdar7582031_static() {  
+  static RDar7582031 *o = 0;
+  rdar7582031_aux(^{ o = rdar7582031_aux_2(); });
+  
+  __block unsigned x;
+  [o rdar7582031:^{ x = 1; }];
+  return x; // no-warning
+}
+
 //===----------------------------------------------------------------------===//
 // <rdar://problem/7462324> - Test that variables passed using __blocks
 //  are not treated as being uninitialized.





More information about the cfe-commits mailing list