blob: f113b2a29d8ccad28c5acb6b6a62ea0357565e98 [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
Ted Kremenekdcee3ce2010-07-01 20:16:50 +000060 Store InvalidateRegions(Store store, const MemRegion * const *I,
61 const MemRegion * const *E, const Expr *Ex,
62 unsigned Count, InvalidatedSymbols *IS,
63 bool invalidateGlobals);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000064
65 void print(Store store, llvm::raw_ostream& Out, const char* nl,
66 const char *sep);
67 void iterBindings(Store store, BindingsHandler& f);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000068
69private:
70 static RegionBindings getRegionBindings(Store store) {
71 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
72 }
73
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000074 class RegionInterval {
75 public:
76 const MemRegion *R;
77 Interval I;
Zhongxing Xue3273e72010-08-03 06:34:25 +000078 RegionInterval(const MemRegion *r, int64_t s, int64_t e) : R(r), I(s, e){}
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000079 };
80
81 RegionInterval RegionToInterval(const MemRegion *R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000082
83 SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000084};
85} // end anonymous namespace
86
87StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) {
88 return new FlatStoreManager(StMgr);
89}
90
Zhongxing Xu36d02e02010-02-08 05:40:07 +000091SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
92 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000093 RegionInterval RI = RegionToInterval(R);
94
95 assert(RI.R && "should handle regions with unknown interval");
96
Zhongxing Xu36d02e02010-02-08 05:40:07 +000097 RegionBindings B = getRegionBindings(store);
Zhongxing Xu7caf9b32010-08-02 04:56:14 +000098 const BindingVal *BV = B.lookup(RI.R);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000099 if (BV) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000100 const SVal *V = BVFactory.Lookup(*BV, RI.I);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000101 if (V)
102 return *V;
103 else
104 return RetrieveRegionWithNoBinding(R, T);
105 }
106 return RetrieveRegionWithNoBinding(R, T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000107}
108
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000109SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
110 QualType T) {
111 if (R->hasStackNonParametersStorage())
112 return UndefinedVal();
113 else
Zhongxing Xu14d23282010-03-01 06:56:52 +0000114 return ValMgr.getRegionValueSymbolVal(cast<TypedRegion>(R));
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000115}
116
117Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
118 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
119 RegionBindings B = getRegionBindings(store);
120 const BindingVal *V = B.lookup(R);
121
122 BindingVal BV = BVFactory.GetEmptyMap();
123 if (V)
124 BV = *V;
125
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000126 RegionInterval RI = RegionToInterval(R);
127 assert(RI.R && "should handle regions with unknown interval");
128 BV = BVFactory.Add(BV, RI.I, val);
129 B = RBFactory.Add(B, RI.R, BV);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000130 return B.getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000131}
132
133Store FlatStoreManager::Remove(Store store, Loc L) {
134 return store;
135}
136
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000137Store FlatStoreManager::BindCompoundLiteral(Store store,
138 const CompoundLiteralExpr* cl,
139 const LocationContext *LC,
140 SVal v) {
141 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000142}
143
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000144SVal FlatStoreManager::ArrayToPointer(Loc Array) {
145 return Array;
146}
147
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000148Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
149 SVal initVal) {
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000150 return Bind(store, ValMgr.makeLoc(VR), initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000151}
152
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000153Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
154 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000155}
156
Ted Kremenekdcee3ce2010-07-01 20:16:50 +0000157Store FlatStoreManager::InvalidateRegions(Store store,
158 const MemRegion * const *I,
159 const MemRegion * const *E,
160 const Expr *Ex, unsigned Count,
161 InvalidatedSymbols *IS,
162 bool invalidateGlobals) {
163 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 Xu2a393db2010-02-08 06:00:22 +0000178 QualType T = cast<VarRegion>(R)->getValueType(Ctx);
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();
191 int64_t Size = Ctx.getTypeSize(cast<TypedRegion>(R)->getValueType(Ctx));
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}