Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 1 | //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a basic region store model. In this model, we do have field |
| 11 | // sensitivity. But we assume nothing about the heap shape. So recursive data |
| 12 | // structures are largely ignored. Basically we do 1-limiting analysis. |
| 13 | // Parameter pointers are assumed with no aliasing. Pointee objects of |
| 14 | // parameters are created lazily. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | #include "clang/Analysis/PathSensitive/MemRegion.h" |
| 18 | #include "clang/Analysis/PathSensitive/GRState.h" |
| 19 | #include "clang/Analysis/Analyses/LiveVariables.h" |
| 20 | |
| 21 | #include "llvm/ADT/ImmutableMap.h" |
| 22 | #include "llvm/Support/Compiler.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame^] | 26 | typedef llvm::ImmutableMap<const MemRegion*, SVal> RegionBindingsTy; |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 27 | |
| 28 | namespace { |
| 29 | |
| 30 | class VISIBILITY_HIDDEN RegionStoreManager : public StoreManager { |
| 31 | RegionBindingsTy::Factory RBFactory; |
| 32 | GRStateManager& StateMgr; |
| 33 | MemRegionManager MRMgr; |
| 34 | |
| 35 | public: |
| 36 | RegionStoreManager(GRStateManager& mgr) |
| 37 | : StateMgr(mgr), MRMgr(StateMgr.getAllocator()) {} |
| 38 | |
| 39 | virtual ~RegionStoreManager() {} |
| 40 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame^] | 41 | Store SetSVal(Store St, Loc LV, SVal V); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 42 | |
| 43 | Store getInitialStore(); |
| 44 | |
| 45 | static inline RegionBindingsTy GetRegionBindings(Store store) { |
| 46 | return RegionBindingsTy(static_cast<const RegionBindingsTy::TreeTy*>(store)); |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | } // end anonymous namespace |
| 51 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame^] | 52 | Store RegionStoreManager::SetSVal(Store store, Loc LV, SVal V) { |
| 53 | assert(LV.getSubKind() == loc::MemRegionKind); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 54 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame^] | 55 | MemRegion* R = cast<loc::MemRegionVal>(LV).getRegion(); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 56 | |
| 57 | if (!R) |
| 58 | return store; |
| 59 | |
| 60 | RegionBindingsTy B = GetRegionBindings(store); |
| 61 | return V.isUnknown() |
| 62 | ? RBFactory.Remove(B, R).getRoot() |
| 63 | : RBFactory.Add(B, R, V).getRoot(); |
| 64 | } |
| 65 | |
| 66 | Store RegionStoreManager::getInitialStore() { |
| 67 | typedef LiveVariables::AnalysisDataTy LVDataTy; |
| 68 | LVDataTy& D = StateMgr.getLiveVariables().getAnalysisData(); |
| 69 | |
| 70 | Store St = RBFactory.GetEmptyMap().getRoot(); |
| 71 | |
| 72 | for (LVDataTy::decl_iterator I=D.begin_decl(), E=D.end_decl(); I != E; ++I) { |
| 73 | ScopedDecl* SD = const_cast<ScopedDecl*>(I->first); |
| 74 | |
| 75 | if (VarDecl* VD = dyn_cast<VarDecl>(SD)) { |
| 76 | // Punt on static variables for now. |
| 77 | if (VD->getStorageClass() == VarDecl::Static) |
| 78 | continue; |
| 79 | |
| 80 | QualType T = VD->getType(); |
| 81 | // Only handle pointers and integers for now. |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame^] | 82 | if (Loc::IsLocType(T) || T->isIntegerType()) { |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 83 | MemRegion* R = MRMgr.getVarRegion(VD); |
| 84 | // Initialize globals and parameters to symbolic values. |
| 85 | // Initialize local variables to undefined. |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame^] | 86 | SVal X = (VD->hasGlobalStorage() || isa<ParmVarDecl>(VD) || |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 87 | isa<ImplicitParamDecl>(VD)) |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame^] | 88 | ? SVal::GetSymbolValue(StateMgr.getSymbolManager(), VD) |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 89 | : UndefinedVal(); |
| 90 | |
Zhongxing Xu | 097fc98 | 2008-10-17 05:57:07 +0000 | [diff] [blame^] | 91 | St = SetSVal(St, loc::MemRegionVal(R), X); |
Zhongxing Xu | 79c57f8 | 2008-10-08 02:50:44 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | return St; |
| 96 | } |