blob: a5bc42958677d062b5cce2132df3391c7b55c613 [file] [log] [blame]
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001//= GRState*cpp - Path-Sens. "State" for tracking valuues -----*- C++ -*--=//
Ted Kremenek9153f732008-02-05 07:17:49 +00002//
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//
Ted Kremenek4adc81e2008-08-13 04:27:00 +000010// This file defines SymbolID, ExprBindKey, and GRState*
Ted Kremenek9153f732008-02-05 07:17:49 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremeneke7aa9a12008-08-17 02:59:30 +000014#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenek4adc81e2008-08-13 04:27:00 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek729a9a22008-07-17 23:15:45 +000016#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
Ted Kremenek05125f12008-08-27 23:13:01 +000017#include "llvm/ADT/SmallSet.h"
Chris Lattner405674c2008-08-23 22:23:37 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek05125f12008-08-27 23:13:01 +000019
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000020using namespace clang;
21
Ted Kremenek05125f12008-08-27 23:13:01 +000022// Give the vtable for ConstraintManager somewhere to live.
23ConstraintManager::~ConstraintManager() {}
24
Ted Kremenek1c72ef02008-08-16 00:49:49 +000025GRStateManager::~GRStateManager() {
26 for (std::vector<GRState::Printer*>::iterator I=Printers.begin(),
27 E=Printers.end(); I!=E; ++I)
28 delete *I;
29
30 for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
31 I!=E; ++I)
32 I->second.second(I->second.first);
33}
34
Ted Kremenek4adc81e2008-08-13 04:27:00 +000035const GRState*
36GRStateManager::RemoveDeadBindings(const GRState* St, Stmt* Loc,
Ted Kremenek1c72ef02008-08-16 00:49:49 +000037 const LiveVariables& Liveness,
38 DeadSymbolsTy& DSymbols) {
Ted Kremenekb87d9092008-02-08 19:17:19 +000039
40 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
41 // The roots are any Block-level exprs and Decls that our liveness algorithm
42 // tells us are live. We then see what Decls they may reference, and keep
43 // those around. This code more than likely can be made faster, and the
44 // frequency of which this method is called should be experimented with
Ted Kremenek9e240492008-10-04 05:50:14 +000045 // for optimum performance.
46 llvm::SmallVector<const MemRegion*, 10> RegionRoots;
Ted Kremenekf59bf482008-07-17 18:38:48 +000047 StoreManager::LiveSymbolsTy LSymbols;
Ted Kremenek4adc81e2008-08-13 04:27:00 +000048 GRState NewSt = *St;
Ted Kremenekf59bf482008-07-17 18:38:48 +000049
Ted Kremenek9e240492008-10-04 05:50:14 +000050 NewSt.Env =
51 EnvMgr.RemoveDeadBindings(NewSt.Env, Loc, Liveness, RegionRoots, LSymbols);
Ted Kremenek016f52f2008-02-08 21:10:02 +000052
Ted Kremenekf59bf482008-07-17 18:38:48 +000053 // Clean up the store.
54 DSymbols.clear();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000055 NewSt.St = StoreMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness,
Ted Kremenek9e240492008-10-04 05:50:14 +000056 RegionRoots, LSymbols, DSymbols);
Ted Kremenekffdbefd2008-08-17 03:10:22 +000057
Zhongxing Xu39cfed32008-08-29 14:52:36 +000058 return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewSt),
59 LSymbols, DSymbols);
Ted Kremenekb87d9092008-02-08 19:17:19 +000060}
Ted Kremenek862d5bb2008-02-06 00:54:14 +000061
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +000062const GRState* GRStateManager::BindLoc(const GRState* St, Loc LV, SVal V) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000063
Ted Kremenek4323a572008-07-10 22:03:41 +000064 Store OldStore = St->getStore();
Zhongxing Xu8485ec62008-10-21 06:27:32 +000065 Store NewStore = StoreMgr->Bind(OldStore, LV, V);
Ted Kremenek3271f8d2008-02-07 04:16:04 +000066
Ted Kremenek4323a572008-07-10 22:03:41 +000067 if (NewStore == OldStore)
68 return St;
Ted Kremenek692416c2008-02-18 22:57:02 +000069
Ted Kremenek4adc81e2008-08-13 04:27:00 +000070 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +000071 NewSt.St = NewStore;
72 return getPersistentState(NewSt);
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000073}
74
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +000075const GRState* GRStateManager::BindDecl(const GRState* St, const VarDecl* VD,
76 Expr* Ex, unsigned Count) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000077 Store OldStore = St->getStore();
78 Store NewStore;
79
80 if (Ex)
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +000081 NewStore = StoreMgr->BindDecl(OldStore, VD, Ex, GetSVal(St, Ex), Count);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000082 else
Zhongxing Xu8b2e05d2008-10-29 02:34:02 +000083 NewStore = StoreMgr->BindDecl(OldStore, VD, Ex);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000084
85 if (NewStore == OldStore)
86 return St;
Ted Kremeneke53c0692008-08-23 00:50:55 +000087
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000088 GRState NewSt = *St;
89 NewSt.St = NewStore;
90 return getPersistentState(NewSt);
91}
92
Ted Kremenek4f090272008-10-27 21:54:31 +000093/// BindCompoundLiteral - Return the store that has the bindings currently
94/// in 'store' plus the bindings for the CompoundLiteral. 'R' is the region
95/// for the compound literal and 'BegInit' and 'EndInit' represent an
96/// array of initializer values.
97const GRState*
98GRStateManager::BindCompoundLiteral(const GRState* state,
Zhongxing Xu4230da62008-11-03 05:18:34 +000099 const CompoundLiteralRegion* R,
Ted Kremenek4f090272008-10-27 21:54:31 +0000100 const SVal* BegInit, const SVal* EndInit) {
101
102 Store oldStore = state->getStore();
103 Store newStore = StoreMgr->BindCompoundLiteral(oldStore, R, BegInit, EndInit);
104
105 if (newStore == oldStore)
106 return state;
107
108 GRState newState = *state;
109 newState.St = newStore;
110 return getPersistentState(newState);
111}
112
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000113const GRState* GRStateManager::Unbind(const GRState* St, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000114 Store OldStore = St->getStore();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000115 Store NewStore = StoreMgr->Remove(OldStore, LV);
Ted Kremenek4323a572008-07-10 22:03:41 +0000116
117 if (NewStore == OldStore)
118 return St;
119
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000120 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +0000121 NewSt.St = NewStore;
122 return getPersistentState(NewSt);
123}
124
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000125const GRState* GRStateManager::getInitialState() {
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000126
Ted Kremenekcaa37242008-08-19 16:51:45 +0000127 GRState StateImpl(EnvMgr.getInitialEnvironment(),
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000128 StoreMgr->getInitialStore(),
Ted Kremenekffdbefd2008-08-17 03:10:22 +0000129 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +0000130
Ted Kremenek9153f732008-02-05 07:17:49 +0000131 return getPersistentState(StateImpl);
132}
133
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000134const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000135
136 llvm::FoldingSetNodeID ID;
137 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000138 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000139
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000140 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000141 return I;
142
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000143 GRState* I = (GRState*) Alloc.Allocate<GRState>();
144 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000145 StateSet.InsertNode(I, InsertPos);
146 return I;
147}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000148
Ted Kremenek59894f92008-03-04 18:30:35 +0000149
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000150//===----------------------------------------------------------------------===//
151// State pretty-printing.
152//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000153
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000154void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000155 ConstraintManager& ConstraintMgr,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000156 Printer** Beg, Printer** End,
Ted Kremenekae6814e2008-08-13 21:24:49 +0000157 const char* nl, const char* sep) const {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000158
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000159 // Print the store.
160 StoreMgr.print(getStore(), Out, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000161
162 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000163 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000164
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000165 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000166
167 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000168 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000169 isFirst = false;
170 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000171 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000172
173 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000174 llvm::raw_os_ostream OutS(Out);
175 I.getKey()->printPretty(OutS);
176 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000177 Out << " : ";
178 I.getData().print(Out);
179 }
180
181 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000182 isFirst = true;
183
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000184 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000185
186 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000187 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000188 isFirst = false;
189 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000190 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000191
192 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000193 llvm::raw_os_ostream OutS(Out);
194 I.getKey()->printPretty(OutS);
195 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000196 Out << " : ";
197 I.getData().print(Out);
198 }
199
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000200 ConstraintMgr.print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000201
Ted Kremenekae6814e2008-08-13 21:24:49 +0000202 // Print checker-specific data.
203 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000204}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000205
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000206void GRStateRef::printDOT(std::ostream& Out) const {
207 print(Out, "\\l", "\\|");
208}
209
210void GRStateRef::printStdErr() const {
211 print(*llvm::cerr);
212}
213
214void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{
215 GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0];
216 GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000217 St->print(Out, *Mgr->StoreMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000218}
219
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000220//===----------------------------------------------------------------------===//
221// Generic Data Map.
222//===----------------------------------------------------------------------===//
223
224void* const* GRState::FindGDM(void* K) const {
225 return GDM.lookup(K);
226}
227
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000228void*
229GRStateManager::FindGDMContext(void* K,
230 void* (*CreateContext)(llvm::BumpPtrAllocator&),
231 void (*DeleteContext)(void*)) {
232
233 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
234 if (!p.first) {
235 p.first = CreateContext(Alloc);
236 p.second = DeleteContext;
237 }
238
239 return p.first;
240}
241
Zhongxing Xu4230da62008-11-03 05:18:34 +0000242const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000243 GRState::GenericDataMap M1 = St->getGDM();
244 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
245
246 if (M1 == M2)
247 return St;
248
249 GRState NewSt = *St;
250 NewSt.GDM = M2;
251 return getPersistentState(NewSt);
252}
Ted Kremenek584def72008-07-22 00:46:16 +0000253
254//===----------------------------------------------------------------------===//
255// Queries.
256//===----------------------------------------------------------------------===//
257
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000258bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000259 const llvm::APSInt& Y) {
260
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000261 SVal V = GetSVal(state, Ex);
Ted Kremenek584def72008-07-22 00:46:16 +0000262
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000263 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000264 return X->getValue() == Y;
265
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000266 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000267 return X->getValue() == Y;
268
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000269 if (nonloc::SymbolVal* X = dyn_cast<nonloc::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000270 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000271
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000272 if (loc::SymbolVal* X = dyn_cast<loc::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000273 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000274
275 return false;
276}
277
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000278bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenek584def72008-07-22 00:46:16 +0000279 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
280}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000281
282//===----------------------------------------------------------------------===//
283// Persistent values for indexing into the Generic Data Map.
284
285int GRState::NullDerefTag::TagInt = 0;
286