Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 1 | //=== FlatStore.cpp - Flat region-based 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 | #include "clang/Checker/PathSensitive/GRState.h" |
| 11 | #include "llvm/ADT/ImmutableIntervalMap.h" |
Daniel Dunbar | 135da71 | 2010-02-08 20:24:21 +0000 | [diff] [blame] | 12 | #include "llvm/Support/ErrorHandling.h" |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace clang; |
Zhongxing Xu | 36d02e0 | 2010-02-08 05:40:07 +0000 | [diff] [blame] | 15 | using llvm::Interval; |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 16 | |
| 17 | // The actual store type. |
| 18 | typedef llvm::ImmutableIntervalMap<SVal> BindingVal; |
| 19 | typedef llvm::ImmutableMap<const MemRegion *, BindingVal> RegionBindings; |
| 20 | |
| 21 | namespace { |
| 22 | class FlatStoreManager : public StoreManager { |
| 23 | RegionBindings::Factory RBFactory; |
| 24 | BindingVal::Factory BVFactory; |
| 25 | |
| 26 | public: |
| 27 | FlatStoreManager(GRStateManager &mgr) |
| 28 | : StoreManager(mgr), |
| 29 | RBFactory(mgr.getAllocator()), |
| 30 | BVFactory(mgr.getAllocator()) {} |
| 31 | |
Zhongxing Xu | 36d02e0 | 2010-02-08 05:40:07 +0000 | [diff] [blame] | 32 | SVal Retrieve(Store store, Loc L, QualType T); |
| 33 | Store Bind(Store store, Loc L, SVal val); |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 34 | Store Remove(Store St, Loc L); |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 35 | Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* cl, |
| 36 | const LocationContext *LC, SVal v); |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 37 | |
| 38 | Store getInitialStore(const LocationContext *InitLoc) { |
| 39 | return RBFactory.GetEmptyMap().getRoot(); |
| 40 | } |
| 41 | |
Zhongxing Xu | f5416bd | 2010-02-05 05:18:47 +0000 | [diff] [blame] | 42 | SubRegionMap *getSubRegionMap(Store store) { |
| 43 | return 0; |
| 44 | } |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 45 | |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 46 | SVal ArrayToPointer(Loc Array); |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 47 | Store RemoveDeadBindings(Store store, Stmt* Loc, |
| 48 | const StackFrameContext *LCtx, |
| 49 | SymbolReaper& SymReaper, |
Zhongxing Xu | 72119c4 | 2010-02-05 05:34:29 +0000 | [diff] [blame] | 50 | llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){ |
| 51 | return store; |
| 52 | } |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 53 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 54 | Store BindDecl(Store store, const VarRegion *VR, SVal initVal); |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 55 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 56 | Store BindDeclWithNoInit(Store store, const VarRegion *VR); |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 57 | |
| 58 | typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols; |
| 59 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 60 | Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E, |
| 61 | unsigned Count, InvalidatedSymbols *IS); |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 62 | |
| 63 | void print(Store store, llvm::raw_ostream& Out, const char* nl, |
| 64 | const char *sep); |
| 65 | void iterBindings(Store store, BindingsHandler& f); |
Zhongxing Xu | 36d02e0 | 2010-02-08 05:40:07 +0000 | [diff] [blame] | 66 | |
| 67 | private: |
| 68 | static RegionBindings getRegionBindings(Store store) { |
| 69 | return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store)); |
| 70 | } |
| 71 | |
| 72 | Interval RegionToInterval(const MemRegion *R); |
| 73 | |
| 74 | SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T); |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 75 | }; |
| 76 | } // end anonymous namespace |
| 77 | |
| 78 | StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) { |
| 79 | return new FlatStoreManager(StMgr); |
| 80 | } |
| 81 | |
Zhongxing Xu | 36d02e0 | 2010-02-08 05:40:07 +0000 | [diff] [blame] | 82 | SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) { |
| 83 | const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion(); |
| 84 | Interval I = RegionToInterval(R); |
| 85 | RegionBindings B = getRegionBindings(store); |
| 86 | const BindingVal *BV = B.lookup(R); |
| 87 | if (BV) { |
| 88 | const SVal *V = BVFactory.Lookup(*BV, I); |
| 89 | if (V) |
| 90 | return *V; |
| 91 | else |
| 92 | return RetrieveRegionWithNoBinding(R, T); |
| 93 | } |
| 94 | return RetrieveRegionWithNoBinding(R, T); |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Zhongxing Xu | 36d02e0 | 2010-02-08 05:40:07 +0000 | [diff] [blame] | 97 | SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R, |
| 98 | QualType T) { |
| 99 | if (R->hasStackNonParametersStorage()) |
| 100 | return UndefinedVal(); |
| 101 | else |
Zhongxing Xu | 14d2328 | 2010-03-01 06:56:52 +0000 | [diff] [blame] | 102 | return ValMgr.getRegionValueSymbolVal(cast<TypedRegion>(R)); |
Zhongxing Xu | 36d02e0 | 2010-02-08 05:40:07 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | Store FlatStoreManager::Bind(Store store, Loc L, SVal val) { |
| 106 | const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion(); |
| 107 | RegionBindings B = getRegionBindings(store); |
| 108 | const BindingVal *V = B.lookup(R); |
| 109 | |
| 110 | BindingVal BV = BVFactory.GetEmptyMap(); |
| 111 | if (V) |
| 112 | BV = *V; |
| 113 | |
| 114 | Interval I = RegionToInterval(R); |
| 115 | BV = BVFactory.Add(BV, I, val); |
| 116 | B = RBFactory.Add(B, R, BV); |
| 117 | return B.getRoot(); |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | Store FlatStoreManager::Remove(Store store, Loc L) { |
| 121 | return store; |
| 122 | } |
| 123 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 124 | Store FlatStoreManager::BindCompoundLiteral(Store store, |
| 125 | const CompoundLiteralExpr* cl, |
| 126 | const LocationContext *LC, |
| 127 | SVal v) { |
| 128 | return store; |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 131 | SVal FlatStoreManager::ArrayToPointer(Loc Array) { |
| 132 | return Array; |
| 133 | } |
| 134 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 135 | Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR, |
| 136 | SVal initVal) { |
| 137 | return store; |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 140 | Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) { |
| 141 | return store; |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Zhongxing Xu | b4a9c61 | 2010-02-05 05:06:13 +0000 | [diff] [blame] | 144 | Store FlatStoreManager::InvalidateRegion(Store store, const MemRegion *R, |
| 145 | const Expr *E, unsigned Count, |
| 146 | InvalidatedSymbols *IS) { |
| 147 | return store; |
Zhongxing Xu | 5d26bc0 | 2010-02-03 09:10:32 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | void FlatStoreManager::print(Store store, llvm::raw_ostream& Out, |
| 151 | const char* nl, const char *sep) { |
| 152 | } |
| 153 | |
| 154 | void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) { |
| 155 | } |
Zhongxing Xu | 36d02e0 | 2010-02-08 05:40:07 +0000 | [diff] [blame] | 156 | |
| 157 | Interval FlatStoreManager::RegionToInterval(const MemRegion *R) { |
| 158 | switch (R->getKind()) { |
| 159 | case MemRegion::VarRegionKind: { |
Zhongxing Xu | 2a393db | 2010-02-08 06:00:22 +0000 | [diff] [blame] | 160 | QualType T = cast<VarRegion>(R)->getValueType(Ctx); |
| 161 | uint64_t Size = Ctx.getTypeSize(T); |
Zhongxing Xu | 36d02e0 | 2010-02-08 05:40:07 +0000 | [diff] [blame] | 162 | return Interval(0, Size-1); |
| 163 | } |
| 164 | default: |
Daniel Dunbar | 135da71 | 2010-02-08 20:24:21 +0000 | [diff] [blame] | 165 | llvm_unreachable("Region kind unhandled."); |
| 166 | return Interval(0, 0); |
Zhongxing Xu | 36d02e0 | 2010-02-08 05:40:07 +0000 | [diff] [blame] | 167 | } |
| 168 | } |