blob: 7c986a71df503f49447643038c94ab97cefa833d [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);
Zhongxing Xu5e4cb342010-08-15 12:45:09 +000047 Store RemoveDeadBindings(Store store, const StackFrameContext *LCtx,
Zhongxing Xu17ddf1c2010-03-17 03:35:08 +000048 SymbolReaper& SymReaper,
Zhongxing Xu72119c42010-02-05 05:34:29 +000049 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
Zhongxing Xu5e4cb342010-08-15 12:45:09 +000050 return store;
Zhongxing Xu72119c42010-02-05 05:34:29 +000051 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000052
Zhongxing Xub4a9c612010-02-05 05:06:13 +000053 Store BindDecl(Store store, const VarRegion *VR, SVal initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000054
Zhongxing Xub4a9c612010-02-05 05:06:13 +000055 Store BindDeclWithNoInit(Store store, const VarRegion *VR);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000056
57 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
58
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000059 Store InvalidateRegions(Store store, const MemRegion * const *I,
60 const MemRegion * const *E, const Expr *Ex,
61 unsigned Count, InvalidatedSymbols *IS,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +000062 bool invalidateGlobals, InvalidatedRegions *Regions);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000063
64 void print(Store store, llvm::raw_ostream& Out, const char* nl,
65 const char *sep);
66 void iterBindings(Store store, BindingsHandler& f);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000067
68private:
69 static RegionBindings getRegionBindings(Store store) {
70 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
71 }
72
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000073 class RegionInterval {
74 public:
75 const MemRegion *R;
76 Interval I;
Zhongxing Xue3273e72010-08-03 06:34:25 +000077 RegionInterval(const MemRegion *r, int64_t s, int64_t e) : R(r), I(s, e){}
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000078 };
79
80 RegionInterval RegionToInterval(const MemRegion *R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000081
82 SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000083};
84} // end anonymous namespace
85
86StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) {
87 return new FlatStoreManager(StMgr);
88}
89
Zhongxing Xu36d02e02010-02-08 05:40:07 +000090SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
91 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000092 RegionInterval RI = RegionToInterval(R);
93
94 assert(RI.R && "should handle regions with unknown interval");
95
Zhongxing Xu36d02e02010-02-08 05:40:07 +000096 RegionBindings B = getRegionBindings(store);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000097 const BindingVal *BV = B.lookup(RI.R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000098 if (BV) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000099 const SVal *V = BVFactory.Lookup(*BV, RI.I);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000100 if (V)
101 return *V;
102 else
103 return RetrieveRegionWithNoBinding(R, T);
104 }
105 return RetrieveRegionWithNoBinding(R, T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000106}
107
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000108SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
109 QualType T) {
110 if (R->hasStackNonParametersStorage())
111 return UndefinedVal();
112 else
Zhongxing Xu14d23282010-03-01 06:56:52 +0000113 return ValMgr.getRegionValueSymbolVal(cast<TypedRegion>(R));
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000114}
115
116Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
117 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
118 RegionBindings B = getRegionBindings(store);
119 const BindingVal *V = B.lookup(R);
120
121 BindingVal BV = BVFactory.GetEmptyMap();
122 if (V)
123 BV = *V;
124
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000125 RegionInterval RI = RegionToInterval(R);
126 assert(RI.R && "should handle regions with unknown interval");
127 BV = BVFactory.Add(BV, RI.I, val);
128 B = RBFactory.Add(B, RI.R, BV);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000129 return B.getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000130}
131
132Store FlatStoreManager::Remove(Store store, Loc L) {
133 return store;
134}
135
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000136Store FlatStoreManager::BindCompoundLiteral(Store store,
137 const CompoundLiteralExpr* cl,
138 const LocationContext *LC,
139 SVal v) {
140 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000141}
142
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000143SVal FlatStoreManager::ArrayToPointer(Loc Array) {
144 return Array;
145}
146
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000147Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
148 SVal initVal) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000149 return Bind(store, ValMgr.makeLoc(VR), initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000150}
151
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000152Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
153 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000154}
155
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000156Store FlatStoreManager::InvalidateRegions(Store store,
Jordy Rosec2b7dfa2010-08-14 20:44:32 +0000157 const MemRegion * const *I,
158 const MemRegion * const *E,
159 const Expr *Ex, unsigned Count,
160 InvalidatedSymbols *IS,
161 bool invalidateGlobals,
162 InvalidatedRegions *Regions) {
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000163 assert(false && "Not implemented");
164 return store;
165}
166
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000167void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
168 const char* nl, const char *sep) {
169}
170
171void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
172}
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000173
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000174FlatStoreManager::RegionInterval
175FlatStoreManager::RegionToInterval(const MemRegion *R) {
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000176 switch (R->getKind()) {
177 case MemRegion::VarRegionKind: {
Zhongxing Xu018220c2010-08-11 06:10:55 +0000178 QualType T = cast<VarRegion>(R)->getValueType();
Zhongxing Xue3273e72010-08-03 06:34:25 +0000179 int64_t Size = Ctx.getTypeSize(T);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000180 return RegionInterval(R, 0, Size-1);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000181 }
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000182
183 case MemRegion::ElementRegionKind:
184 case MemRegion::FieldRegionKind: {
Zhongxing Xue8882332010-08-03 04:52:05 +0000185 RegionOffset Offset = R->getAsOffset();
186 // We cannot compute offset for all regions, for example, elements
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000187 // with symbolic offsets.
188 if (!Offset.getRegion())
189 return RegionInterval(0, 0, 0);
Zhongxing Xue3273e72010-08-03 06:34:25 +0000190 int64_t Start = Offset.getOffset();
Zhongxing Xu018220c2010-08-11 06:10:55 +0000191 int64_t Size = Ctx.getTypeSize(cast<TypedRegion>(R)->getValueType());
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000192 return RegionInterval(Offset.getRegion(), Start, Start+Size);
193 }
194
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000195 default:
Daniel Dunbar135da712010-02-08 20:24:21 +0000196 llvm_unreachable("Region kind unhandled.");
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000197 return RegionInterval(0, 0, 0);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000198 }
199}