Mark CXXThisRegion in the current or parent stack frame context as live so that
their bindings are not removed.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@98705 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Checker/BasicStore.cpp b/lib/Checker/BasicStore.cpp
index 10136f3..7c53991 100644
--- a/lib/Checker/BasicStore.cpp
+++ b/lib/Checker/BasicStore.cpp
@@ -72,7 +72,9 @@
 
   /// RemoveDeadBindings - Scans a BasicStore of 'state' for dead values.
   ///  It updatees the GRState object in place with the values removed.
-  Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
+  Store RemoveDeadBindings(Store store, Stmt* Loc, 
+                           const StackFrameContext *LCtx,
+                           SymbolReaper& SymReaper,
                           llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
 
   void iterBindings(Store store, BindingsHandler& f);
@@ -250,6 +252,7 @@
 }
 
 Store BasicStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
+                                            const StackFrameContext *LCtx,
                                             SymbolReaper& SymReaper,
                            llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
 {
diff --git a/lib/Checker/FlatStore.cpp b/lib/Checker/FlatStore.cpp
index 07a54fb..2af9ffa 100644
--- a/lib/Checker/FlatStore.cpp
+++ b/lib/Checker/FlatStore.cpp
@@ -44,7 +44,9 @@
   }
 
   SVal ArrayToPointer(Loc Array);
-  Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
+  Store RemoveDeadBindings(Store store, Stmt* Loc, 
+                           const StackFrameContext *LCtx,
+                           SymbolReaper& SymReaper,
                          llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
     return store;
   }
diff --git a/lib/Checker/GRExprEngine.cpp b/lib/Checker/GRExprEngine.cpp
index e64ba94..3ace552 100644
--- a/lib/Checker/GRExprEngine.cpp
+++ b/lib/Checker/GRExprEngine.cpp
@@ -481,7 +481,9 @@
   SymbolReaper SymReaper(BasePred->getLocationContext(), SymMgr);
 
   CleanedState = AMgr.shouldPurgeDead()
-    ? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, SymReaper)
+    ? StateMgr.RemoveDeadBindings(EntryNode->getState(), CurrentStmt, 
+                         BasePred->getLocationContext()->getCurrentStackFrame(),
+                                  SymReaper)
     : EntryNode->getState();
 
   // Process any special transfer function for dead symbols.
diff --git a/lib/Checker/GRState.cpp b/lib/Checker/GRState.cpp
index 97ede1d..2defbcd 100644
--- a/lib/Checker/GRState.cpp
+++ b/lib/Checker/GRState.cpp
@@ -35,6 +35,7 @@
 
 const GRState*
 GRStateManager::RemoveDeadBindings(const GRState* state, Stmt* Loc,
+                                   const StackFrameContext *LCtx,
                                    SymbolReaper& SymReaper) {
 
   // This code essentially performs a "mark-and-sweep" of the VariableBindings.
@@ -50,7 +51,7 @@
                                            state, RegionRoots);
 
   // Clean up the store.
-  NewState.St = StoreMgr->RemoveDeadBindings(NewState.St, Loc, SymReaper, 
+  NewState.St = StoreMgr->RemoveDeadBindings(NewState.St, Loc, LCtx, SymReaper, 
                                              RegionRoots);
 
   return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewState),
diff --git a/lib/Checker/RegionStore.cpp b/lib/Checker/RegionStore.cpp
index 307ef78..c2b702a 100644
--- a/lib/Checker/RegionStore.cpp
+++ b/lib/Checker/RegionStore.cpp
@@ -354,7 +354,9 @@
 
   /// RemoveDeadBindings - Scans the RegionStore of 'state' for dead values.
   ///  It returns a new Store with these values removed.
-  Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
+  Store RemoveDeadBindings(Store store, Stmt* Loc, 
+                           const StackFrameContext *LCtx,
+                           SymbolReaper& SymReaper,
                           llvm::SmallVectorImpl<const MemRegion*>& RegionRoots);
 
   const GRState *EnterStackFrame(const GRState *state,
@@ -1678,12 +1680,14 @@
   llvm::SmallVector<const SymbolicRegion*, 12> Postponed;
   SymbolReaper &SymReaper;
   Stmt *Loc;
+  const StackFrameContext *CurrentLCtx;
+  
 public:
   RemoveDeadBindingsWorker(RegionStoreManager &rm, GRStateManager &stateMgr,
                            RegionBindings b, SymbolReaper &symReaper,
-                           Stmt *loc)
+                           Stmt *loc, const StackFrameContext *LCtx)
     : ClusterAnalysis<RemoveDeadBindingsWorker>(rm, stateMgr, b),
-      SymReaper(symReaper), Loc(loc) {}
+      SymReaper(symReaper), Loc(loc), CurrentLCtx(LCtx) {}
 
   // Called by ClusterAnalysis.
   void VisitAddedToCluster(const MemRegion *baseR, RegionCluster &C);
@@ -1713,6 +1717,15 @@
 
     return;
   }
+
+  // CXXThisRegion in the current or parent location context is live.
+  if (const CXXThisRegion *TR = dyn_cast<CXXThisRegion>(baseR)) {
+    const StackArgumentsSpaceRegion *StackReg = 
+      cast<StackArgumentsSpaceRegion>(TR->getSuperRegion());
+    const StackFrameContext *RegCtx = StackReg->getStackFrame();
+    if (RegCtx == CurrentLCtx || RegCtx->isParentOf(CurrentLCtx))
+      AddToWorkList(TR, C);
+  }
 }
 
 void RemoveDeadBindingsWorker::VisitCluster(const MemRegion *baseR,
@@ -1799,11 +1812,12 @@
 }
 
 Store RegionStoreManager::RemoveDeadBindings(Store store, Stmt* Loc,
+                                             const StackFrameContext *LCtx,
                                              SymbolReaper& SymReaper,
                            llvm::SmallVectorImpl<const MemRegion*>& RegionRoots)
 {
   RegionBindings B = GetRegionBindings(store);
-  RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, Loc);
+  RemoveDeadBindingsWorker W(*this, StateMgr, B, SymReaper, Loc, LCtx);
   W.GenerateClusters();
 
   // Enqueue the region roots onto the worklist.