blob: 51c537ecfadf04fb7ec22e2d1a7dd5a8be5bb275 [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"
Daniel Dunbar135da712010-02-08 20:24:21 +000012#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000013
14using namespace clang;
Zhongxing Xu36d02e02010-02-08 05:40:07 +000015using llvm::Interval;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000016
17// The actual store type.
18typedef llvm::ImmutableIntervalMap<SVal> BindingVal;
19typedef llvm::ImmutableMap<const MemRegion *, BindingVal> RegionBindings;
20
21namespace {
22class FlatStoreManager : public StoreManager {
23 RegionBindings::Factory RBFactory;
24 BindingVal::Factory BVFactory;
25
26public:
27 FlatStoreManager(GRStateManager &mgr)
28 : StoreManager(mgr),
29 RBFactory(mgr.getAllocator()),
30 BVFactory(mgr.getAllocator()) {}
31
Zhongxing Xu36d02e02010-02-08 05:40:07 +000032 SVal Retrieve(Store store, Loc L, QualType T);
33 Store Bind(Store store, Loc L, SVal val);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000034 Store Remove(Store St, Loc L);
Zhongxing Xub4a9c612010-02-05 05:06:13 +000035 Store BindCompoundLiteral(Store store, const CompoundLiteralExpr* cl,
36 const LocationContext *LC, SVal v);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000037
38 Store getInitialStore(const LocationContext *InitLoc) {
39 return RBFactory.GetEmptyMap().getRoot();
40 }
41
Zhongxing Xuf5416bd2010-02-05 05:18:47 +000042 SubRegionMap *getSubRegionMap(Store store) {
43 return 0;
44 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000045
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000046 SVal ArrayToPointer(Loc Array);
Jordy Rose7dadf792010-07-01 20:09:55 +000047 const GRState *RemoveDeadBindings(GRState &state,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +000048 const StackFrameContext *LCtx,
49 SymbolReaper& SymReaper,
Zhongxing Xu72119c42010-02-05 05:34:29 +000050 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
Zhongxing Xu95798982010-05-26 03:27:35 +000051 return StateMgr.getPersistentState(state);
Zhongxing Xu72119c42010-02-05 05:34:29 +000052 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000053
Zhongxing Xub4a9c612010-02-05 05:06:13 +000054 Store BindDecl(Store store, const VarRegion *VR, SVal initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000055
Zhongxing Xub4a9c612010-02-05 05:06:13 +000056 Store BindDeclWithNoInit(Store store, const VarRegion *VR);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000057
58 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
59
Zhongxing Xub4a9c612010-02-05 05:06:13 +000060 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
61 unsigned Count, InvalidatedSymbols *IS);
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000062
63 Store InvalidateRegions(Store store, const MemRegion * const *I,
64 const MemRegion * const *E, const Expr *Ex,
65 unsigned Count, InvalidatedSymbols *IS,
66 bool invalidateGlobals);
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
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000077 class RegionInterval {
78 public:
79 const MemRegion *R;
80 Interval I;
81 RegionInterval(const MemRegion *r, uint64_t s, uint64_t e) : R(r), I(s, e){}
82 };
83
84 RegionInterval RegionToInterval(const MemRegion *R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000085
86 SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000087};
88} // end anonymous namespace
89
90StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) {
91 return new FlatStoreManager(StMgr);
92}
93
Zhongxing Xu36d02e02010-02-08 05:40:07 +000094SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
95 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000096 RegionInterval RI = RegionToInterval(R);
97
98 assert(RI.R && "should handle regions with unknown interval");
99
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000100 RegionBindings B = getRegionBindings(store);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000101 const BindingVal *BV = B.lookup(RI.R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000102 if (BV) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000103 const SVal *V = BVFactory.Lookup(*BV, RI.I);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000104 if (V)
105 return *V;
106 else
107 return RetrieveRegionWithNoBinding(R, T);
108 }
109 return RetrieveRegionWithNoBinding(R, T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000110}
111
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000112SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
113 QualType T) {
114 if (R->hasStackNonParametersStorage())
115 return UndefinedVal();
116 else
Zhongxing Xu14d23282010-03-01 06:56:52 +0000117 return ValMgr.getRegionValueSymbolVal(cast<TypedRegion>(R));
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000118}
119
120Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
121 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
122 RegionBindings B = getRegionBindings(store);
123 const BindingVal *V = B.lookup(R);
124
125 BindingVal BV = BVFactory.GetEmptyMap();
126 if (V)
127 BV = *V;
128
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000129 RegionInterval RI = RegionToInterval(R);
130 assert(RI.R && "should handle regions with unknown interval");
131 BV = BVFactory.Add(BV, RI.I, val);
132 B = RBFactory.Add(B, RI.R, BV);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000133 return B.getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000134}
135
136Store FlatStoreManager::Remove(Store store, Loc L) {
137 return store;
138}
139
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000140Store FlatStoreManager::BindCompoundLiteral(Store store,
141 const CompoundLiteralExpr* cl,
142 const LocationContext *LC,
143 SVal v) {
144 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000145}
146
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000147SVal FlatStoreManager::ArrayToPointer(Loc Array) {
148 return Array;
149}
150
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000151Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
152 SVal initVal) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000153 return Bind(store, ValMgr.makeLoc(VR), initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000154}
155
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000156Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
157 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000158}
159
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000160Store FlatStoreManager::InvalidateRegions(Store store,
161 const MemRegion * const *I,
162 const MemRegion * const *E,
163 const Expr *Ex, unsigned Count,
164 InvalidatedSymbols *IS,
165 bool invalidateGlobals) {
166 assert(false && "Not implemented");
167 return store;
168}
169
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000170Store FlatStoreManager::InvalidateRegion(Store store, const MemRegion *R,
171 const Expr *E, unsigned Count,
172 InvalidatedSymbols *IS) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000173 assert(false && "Not implemented");
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000174 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000175}
176
177void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
178 const char* nl, const char *sep) {
179}
180
181void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
182}
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000183
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000184FlatStoreManager::RegionInterval
185FlatStoreManager::RegionToInterval(const MemRegion *R) {
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000186 switch (R->getKind()) {
187 case MemRegion::VarRegionKind: {
Zhongxing Xu2a393db2010-02-08 06:00:22 +0000188 QualType T = cast<VarRegion>(R)->getValueType(Ctx);
189 uint64_t Size = Ctx.getTypeSize(T);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000190 return RegionInterval(R, 0, Size-1);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000191 }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000192
193 case MemRegion::ElementRegionKind:
194 case MemRegion::FieldRegionKind: {
195 const TypedRegion *TR = cast<TypedRegion>(R);
196 RegionOffset Offset = TR->getAsOffset();
197 // We cannot compute offset for all ElementRegions, for example, elements
198 // with symbolic offsets.
199 if (!Offset.getRegion())
200 return RegionInterval(0, 0, 0);
201 uint64_t Start = Offset.getOffset();
202 uint64_t Size = Ctx.getTypeSize(TR->getValueType(Ctx));
203 return RegionInterval(Offset.getRegion(), Start, Start+Size);
204 }
205
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000206 default:
Daniel Dunbar135da712010-02-08 20:24:21 +0000207 llvm_unreachable("Region kind unhandled.");
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000208 return RegionInterval(0, 0, 0);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000209 }
210}