blob: 64575b3c9783f1ed8aa023e408d3871d2717a08b [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
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
Zhongxing Xu14d23282010-03-01 06:56:52 +0000107 return ValMgr.getRegionValueSymbolVal(cast<TypedRegion>(R));
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000108}
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::ArrayToPointer(Loc Array) {
137 return Array;
138}
139
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000140Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
141 SVal initVal) {
142 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000143}
144
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000145Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
146 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000147}
148
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000149Store FlatStoreManager::InvalidateRegions(Store store,
150 const MemRegion * const *I,
151 const MemRegion * const *E,
152 const Expr *Ex, unsigned Count,
153 InvalidatedSymbols *IS,
154 bool invalidateGlobals) {
155 assert(false && "Not implemented");
156 return store;
157}
158
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000159Store FlatStoreManager::InvalidateRegion(Store store, const MemRegion *R,
160 const Expr *E, unsigned Count,
161 InvalidatedSymbols *IS) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000162 assert(false && "Not implemented");
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000163 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000164}
165
166void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
167 const char* nl, const char *sep) {
168}
169
170void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
171}
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000172
173Interval FlatStoreManager::RegionToInterval(const MemRegion *R) {
174 switch (R->getKind()) {
175 case MemRegion::VarRegionKind: {
Zhongxing Xu2a393db2010-02-08 06:00:22 +0000176 QualType T = cast<VarRegion>(R)->getValueType(Ctx);
177 uint64_t Size = Ctx.getTypeSize(T);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000178 return Interval(0, Size-1);
179 }
180 default:
Daniel Dunbar135da712010-02-08 20:24:21 +0000181 llvm_unreachable("Region kind unhandled.");
182 return Interval(0, 0);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000183 }
184}