blob: ca182708e13de342d5d86b8c4c91d7f4f87a8e2d [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 Xu36d02e02010-02-08 05:40:07 +000045 SVal getLValueVar(const VarDecl *VD, const LocationContext *LC) {
46 return loc::MemRegionVal(MRMgr.getVarRegion(VD, LC));
47 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000048
49 SVal getLValueString(const StringLiteral* sl);
50 SVal getLValueIvar(const ObjCIvarDecl* decl, SVal base);
51 SVal getLValueField(const FieldDecl* D, SVal Base);
52 SVal getLValueElement(QualType elementType, SVal offset, SVal Base);
53 SVal ArrayToPointer(Loc Array);
Zhongxing Xu72119c42010-02-05 05:34:29 +000054 Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
55 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
56 return store;
57 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000058
Zhongxing Xub4a9c612010-02-05 05:06:13 +000059 Store BindDecl(Store store, const VarRegion *VR, SVal initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000060
Zhongxing Xub4a9c612010-02-05 05:06:13 +000061 Store BindDeclWithNoInit(Store store, const VarRegion *VR);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000062
63 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
64
Zhongxing Xub4a9c612010-02-05 05:06:13 +000065 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
66 unsigned Count, InvalidatedSymbols *IS);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000067
68 void print(Store store, llvm::raw_ostream& Out, const char* nl,
69 const char *sep);
70 void iterBindings(Store store, BindingsHandler& f);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000071
72private:
73 static RegionBindings getRegionBindings(Store store) {
74 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
75 }
76
77 Interval RegionToInterval(const MemRegion *R);
78
79 SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000080};
81} // end anonymous namespace
82
83StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) {
84 return new FlatStoreManager(StMgr);
85}
86
Zhongxing Xu36d02e02010-02-08 05:40:07 +000087SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
88 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
89 Interval I = RegionToInterval(R);
90 RegionBindings B = getRegionBindings(store);
91 const BindingVal *BV = B.lookup(R);
92 if (BV) {
93 const SVal *V = BVFactory.Lookup(*BV, I);
94 if (V)
95 return *V;
96 else
97 return RetrieveRegionWithNoBinding(R, T);
98 }
99 return RetrieveRegionWithNoBinding(R, T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000100}
101
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000102SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
103 QualType T) {
104 if (R->hasStackNonParametersStorage())
105 return UndefinedVal();
106 else
107 return ValMgr.getRegionValueSymbolVal(R, T);
108}
109
110Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
111 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
112 RegionBindings B = getRegionBindings(store);
113 const BindingVal *V = B.lookup(R);
114
115 BindingVal BV = BVFactory.GetEmptyMap();
116 if (V)
117 BV = *V;
118
119 Interval I = RegionToInterval(R);
120 BV = BVFactory.Add(BV, I, val);
121 B = RBFactory.Add(B, R, BV);
122 return B.getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000123}
124
125Store FlatStoreManager::Remove(Store store, Loc L) {
126 return store;
127}
128
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000129Store FlatStoreManager::BindCompoundLiteral(Store store,
130 const CompoundLiteralExpr* cl,
131 const LocationContext *LC,
132 SVal v) {
133 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000134}
135
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000136SVal FlatStoreManager::getLValueString(const StringLiteral* sl) {
137 return UnknownVal();
138}
139
140SVal FlatStoreManager::getLValueIvar(const ObjCIvarDecl* decl, SVal base) {
141 return UnknownVal();
142}
143
144SVal FlatStoreManager::getLValueField(const FieldDecl* D, SVal Base) {
145 return UnknownVal();
146}
147
148SVal FlatStoreManager::getLValueElement(QualType elementType, SVal offset,
149 SVal Base) {
150 return UnknownVal();
151}
152
153SVal FlatStoreManager::ArrayToPointer(Loc Array) {
154 return Array;
155}
156
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000157Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
158 SVal initVal) {
159 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000160}
161
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000162Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
163 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000164}
165
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000166Store FlatStoreManager::InvalidateRegion(Store store, const MemRegion *R,
167 const Expr *E, unsigned Count,
168 InvalidatedSymbols *IS) {
169 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000170}
171
172void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
173 const char* nl, const char *sep) {
174}
175
176void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
177}
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000178
179Interval FlatStoreManager::RegionToInterval(const MemRegion *R) {
180 switch (R->getKind()) {
181 case MemRegion::VarRegionKind: {
182 QualType T = cast<VarRegion>(R)->getValueType(StateMgr.getContext());
183 uint64_t Size = StateMgr.getContext().getTypeSize(T);
184 return Interval(0, Size-1);
185 }
186 default:
187 assert(0 && "Region kind unhandled.");
188 }
189}