blob: 07a54fb487369cbb560dd3878cb4eb4fcf8627f3 [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 Xu72119c42010-02-05 05:34:29 +000047 Store RemoveDeadBindings(Store store, Stmt* Loc, SymbolReaper& SymReaper,
48 llvm::SmallVectorImpl<const MemRegion*>& RegionRoots){
49 return store;
50 }
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000051
Zhongxing Xub4a9c612010-02-05 05:06:13 +000052 Store BindDecl(Store store, const VarRegion *VR, SVal initVal);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000053
Zhongxing Xub4a9c612010-02-05 05:06:13 +000054 Store BindDeclWithNoInit(Store store, const VarRegion *VR);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000055
56 typedef llvm::DenseSet<SymbolRef> InvalidatedSymbols;
57
Zhongxing Xub4a9c612010-02-05 05:06:13 +000058 Store InvalidateRegion(Store store, const MemRegion *R, const Expr *E,
59 unsigned Count, InvalidatedSymbols *IS);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000060
61 void print(Store store, llvm::raw_ostream& Out, const char* nl,
62 const char *sep);
63 void iterBindings(Store store, BindingsHandler& f);
Zhongxing Xu36d02e02010-02-08 05:40:07 +000064
65private:
66 static RegionBindings getRegionBindings(Store store) {
67 return RegionBindings(static_cast<const RegionBindings::TreeTy*>(store));
68 }
69
70 Interval RegionToInterval(const MemRegion *R);
71
72 SVal RetrieveRegionWithNoBinding(const MemRegion *R, QualType T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000073};
74} // end anonymous namespace
75
76StoreManager *clang::CreateFlatStoreManager(GRStateManager &StMgr) {
77 return new FlatStoreManager(StMgr);
78}
79
Zhongxing Xu36d02e02010-02-08 05:40:07 +000080SVal FlatStoreManager::Retrieve(Store store, Loc L, QualType T) {
81 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
82 Interval I = RegionToInterval(R);
83 RegionBindings B = getRegionBindings(store);
84 const BindingVal *BV = B.lookup(R);
85 if (BV) {
86 const SVal *V = BVFactory.Lookup(*BV, I);
87 if (V)
88 return *V;
89 else
90 return RetrieveRegionWithNoBinding(R, T);
91 }
92 return RetrieveRegionWithNoBinding(R, T);
Zhongxing Xu5d26bc02010-02-03 09:10:32 +000093}
94
Zhongxing Xu36d02e02010-02-08 05:40:07 +000095SVal FlatStoreManager::RetrieveRegionWithNoBinding(const MemRegion *R,
96 QualType T) {
97 if (R->hasStackNonParametersStorage())
98 return UndefinedVal();
99 else
Zhongxing Xu14d23282010-03-01 06:56:52 +0000100 return ValMgr.getRegionValueSymbolVal(cast<TypedRegion>(R));
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000101}
102
103Store FlatStoreManager::Bind(Store store, Loc L, SVal val) {
104 const MemRegion *R = cast<loc::MemRegionVal>(L).getRegion();
105 RegionBindings B = getRegionBindings(store);
106 const BindingVal *V = B.lookup(R);
107
108 BindingVal BV = BVFactory.GetEmptyMap();
109 if (V)
110 BV = *V;
111
112 Interval I = RegionToInterval(R);
113 BV = BVFactory.Add(BV, I, val);
114 B = RBFactory.Add(B, R, BV);
115 return B.getRoot();
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000116}
117
118Store FlatStoreManager::Remove(Store store, Loc L) {
119 return store;
120}
121
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000122Store FlatStoreManager::BindCompoundLiteral(Store store,
123 const CompoundLiteralExpr* cl,
124 const LocationContext *LC,
125 SVal v) {
126 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000127}
128
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000129SVal FlatStoreManager::ArrayToPointer(Loc Array) {
130 return Array;
131}
132
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000133Store FlatStoreManager::BindDecl(Store store, const VarRegion *VR,
134 SVal initVal) {
135 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000136}
137
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000138Store FlatStoreManager::BindDeclWithNoInit(Store store, const VarRegion *VR) {
139 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000140}
141
Zhongxing Xub4a9c612010-02-05 05:06:13 +0000142Store FlatStoreManager::InvalidateRegion(Store store, const MemRegion *R,
143 const Expr *E, unsigned Count,
144 InvalidatedSymbols *IS) {
145 return store;
Zhongxing Xu5d26bc02010-02-03 09:10:32 +0000146}
147
148void FlatStoreManager::print(Store store, llvm::raw_ostream& Out,
149 const char* nl, const char *sep) {
150}
151
152void FlatStoreManager::iterBindings(Store store, BindingsHandler& f) {
153}
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000154
155Interval FlatStoreManager::RegionToInterval(const MemRegion *R) {
156 switch (R->getKind()) {
157 case MemRegion::VarRegionKind: {
Zhongxing Xu2a393db2010-02-08 06:00:22 +0000158 QualType T = cast<VarRegion>(R)->getValueType(Ctx);
159 uint64_t Size = Ctx.getTypeSize(T);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000160 return Interval(0, Size-1);
161 }
162 default:
Daniel Dunbar135da712010-02-08 20:24:21 +0000163 llvm_unreachable("Region kind unhandled.");
164 return Interval(0, 0);
Zhongxing Xu36d02e02010-02-08 05:40:07 +0000165 }
166}