blob: 2af9ffa4a4d9a21b0f1d238bf569507da5ff4a76 [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 Xu17ddf1c2010-03-17 03:35:08 +000047 Store RemoveDeadBindings(Store store, Stmt* Loc,
48 const StackFrameContext *LCtx,
49 SymbolReaper& SymReaper,
Zhongxing Xu72119c42010-02-05 05:34:29 +000050 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
51 return store;
52 }
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);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000062
63 void print(Store store, llvm::raw_ostream& Out, const char* nl,
64 const char *sep);
65 void iterBindings(Store store, BindingsHandler& f);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000066
67private:
68 static RegionBindings getRegionBindings(Store store) {
69 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
70 }
71
72 Interval RegionToInterval(const MemRegion *R);
73
74 SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000075};
76} // end anonymous namespace
77
78StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) {
79 return new FlatStoreManager(StMgr);
80}
81
Zhongxing Xu36d02e02010-02-08 05:40:07 +000082SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
83 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
84 Interval I = RegionToInterval(R);
85 RegionBindings B = getRegionBindings(store);
86 const BindingVal *BV = B.lookup(R);
87 if (BV) {
88 const SVal *V = BVFactory.Lookup(*BV, I);
89 if (V)
90 return *V;
91 else
92 return RetrieveRegionWithNoBinding(R, T);
93 }
94 return RetrieveRegionWithNoBinding(R, T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000095}
96
Zhongxing Xu36d02e02010-02-08 05:40:07 +000097SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
98 QualType T) {
99 if (R->hasStackNonParametersStorage())
100 return UndefinedVal();
101 else
Zhongxing Xu14d23282010-03-01 06:56:52 +0000102 return ValMgr.getRegionValueSymbolVal(cast<TypedRegion>(R));
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000103}
104
105Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
106 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
107 RegionBindings B = getRegionBindings(store);
108 const BindingVal *V = B.lookup(R);
109
110 BindingVal BV = BVFactory.GetEmptyMap();
111 if (V)
112 BV = *V;
113
114 Interval I = RegionToInterval(R);
115 BV = BVFactory.Add(BV, I, val);
116 B = RBFactory.Add(B, R, BV);
117 return B.getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000118}
119
120Store FlatStoreManager::Remove(Store store, Loc L) {
121 return store;
122}
123
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000124Store FlatStoreManager::BindCompoundLiteral(Store store,
125 const CompoundLiteralExpr* cl,
126 const LocationContext *LC,
127 SVal v) {
128 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000129}
130
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000131SVal FlatStoreManager::ArrayToPointer(Loc Array) {
132 return Array;
133}
134
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000135Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
136 SVal initVal) {
137 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000138}
139
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000140Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
141 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000142}
143
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000144Store FlatStoreManager::InvalidateRegion(Store store, const MemRegion *R,
145 const Expr *E, unsigned Count,
146 InvalidatedSymbols *IS) {
147 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000148}
149
150void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
151 const char* nl, const char *sep) {
152}
153
154void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
155}
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000156
157Interval FlatStoreManager::RegionToInterval(const MemRegion *R) {
158 switch (R->getKind()) {
159 case MemRegion::VarRegionKind: {
Zhongxing Xu2a393db2010-02-08 06:00:22 +0000160 QualType T = cast<VarRegion>(R)->getValueType(Ctx);
161 uint64_t Size = Ctx.getTypeSize(T);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000162 return Interval(0, Size-1);
163 }
164 default:
Daniel Dunbar135da712010-02-08 20:24:21 +0000165 llvm_unreachable("Region kind unhandled.");
166 return Interval(0, 0);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000167 }
168}