blob: fcb95f9385ccf7d2580e50bd0527b954d2b81c75 [file] [log] [blame]
Zhongxing Xu5d26bc02010-02-03 09:10:32 +00001//=== 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"
12
13using namespace clang;
Zhongxing Xu36d02e02010-02-08 05:40:07 +000014using llvm::Interval;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000015
16// The actual store type.
17typedef llvm::ImmutableIntervalMap<SVal> BindingVal;
18typedef llvm::ImmutableMap<const MemRegion *, BindingVal> RegionBindings;
19
20namespace {
21class FlatStoreManager : public StoreManager {
22 RegionBindings::Factory RBFactory;
23 BindingVal::Factory BVFactory;
24
25public:
26 FlatStoreManager(GRStateManager &mgr)
27 : StoreManager(mgr),
28 RBFactory(mgr.getAllocator()),
29 BVFactory(mgr.getAllocator()) {}
30
Zhongxing Xu36d02e02010-02-08 05:40:07 +000031 SVal Retrieve(Store store, Loc L, QualType T);
32 Store Bind(Store store, Loc L, SVal val);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000033 Store Remove(Store St, Loc L);
Zhongxing Xub4a9c612010-02-05 05:06:13 +000034 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* cl,
35 const LocationContext *LC, SVal v);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000036
37 Store getInitialStore(const LocationContext *InitLoc) {
38 return RBFactory.GetEmptyMap().getRoot();
39 }
40
Zhongxing Xuf5416bd2010-02-05 05:18:47 +000041 SubRegionMap *getSubRegionMap(Store store) {
42 return 0;
43 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000044
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000045 SVal ArrayToPointer(Loc Array);
Zhongxing Xu72119c42010-02-05 05:34:29 +000046 Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
47 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
48 return store;
49 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000050
Zhongxing Xub4a9c612010-02-05 05:06:13 +000051 Store BindDecl(Store store, const VarRegion *VR, SVal initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000052
Zhongxing Xub4a9c612010-02-05 05:06:13 +000053 Store BindDeclWithNoInit(Store store, const VarRegion *VR);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000054
55 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
56
Zhongxing Xub4a9c612010-02-05 05:06:13 +000057 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
58 unsigned Count, InvalidatedSymbols *IS);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000059
60 void print(Store store, llvm::raw_ostream& Out, const char* nl,
61 const char *sep);
62 void iterBindings(Store store, BindingsHandler& f);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000063
64private:
65 static RegionBindings getRegionBindings(Store store) {
66 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
67 }
68
69 Interval RegionToInterval(const MemRegion *R);
70
71 SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000072};
73} // end anonymous namespace
74
75StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) {
76 return new FlatStoreManager(StMgr);
77}
78
Zhongxing Xu36d02e02010-02-08 05:40:07 +000079SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
80 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
81 Interval I = RegionToInterval(R);
82 RegionBindings B = getRegionBindings(store);
83 const BindingVal *BV = B.lookup(R);
84 if (BV) {
85 const SVal *V = BVFactory.Lookup(*BV, I);
86 if (V)
87 return *V;
88 else
89 return RetrieveRegionWithNoBinding(R, T);
90 }
91 return RetrieveRegionWithNoBinding(R, T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000092}
93
Zhongxing Xu36d02e02010-02-08 05:40:07 +000094SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
95 QualType T) {
96 if (R->hasStackNonParametersStorage())
97 return UndefinedVal();
98 else
99 return ValMgr.getRegionValueSymbolVal(R, T);
100}
101
102Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
103 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
104 RegionBindings B = getRegionBindings(store);
105 const BindingVal *V = B.lookup(R);
106
107 BindingVal BV = BVFactory.GetEmptyMap();
108 if (V)
109 BV = *V;
110
111 Interval I = RegionToInterval(R);
112 BV = BVFactory.Add(BV, I, val);
113 B = RBFactory.Add(B, R, BV);
114 return B.getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000115}
116
117Store FlatStoreManager::Remove(Store store, Loc L) {
118 return store;
119}
120
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000121Store FlatStoreManager::BindCompoundLiteral(Store store,
122 const CompoundLiteralExpr* cl,
123 const LocationContext *LC,
124 SVal v) {
125 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000126}
127
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000128SVal FlatStoreManager::ArrayToPointer(Loc Array) {
129 return Array;
130}
131
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000132Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
133 SVal initVal) {
134 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000135}
136
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000137Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
138 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000139}
140
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000141Store FlatStoreManager::InvalidateRegion(Store store, const MemRegion *R,
142 const Expr *E, unsigned Count,
143 InvalidatedSymbols *IS) {
144 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000145}
146
147void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
148 const char* nl, const char *sep) {
149}
150
151void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
152}
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000153
154Interval FlatStoreManager::RegionToInterval(const MemRegion *R) {
155 switch (R->getKind()) {
156 case MemRegion::VarRegionKind: {
Zhongxing Xu2a393db2010-02-08 06:00:22 +0000157 QualType T = cast<VarRegion>(R)->getValueType(Ctx);
158 uint64_t Size = Ctx.getTypeSize(T);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000159 return Interval(0, Size-1);
160 }
161 default:
162 assert(0 && "Region kind unhandled.");
163 }
164}