blob: ad057c2f098184af1486bbcce7071f85f232f093 [file] [log] [blame]
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001//= GRState*cpp - Path-Sens. "State" for tracking valuues -----*- C++ -*--=//
Ted Kremenek2d8dce32008-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 Kremenekabd89ac2008-08-13 04:27:00 +000010// This file defines SymbolID, ExprBindKey, and GRState*
Ted Kremenek2d8dce32008-02-05 07:17:49 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek67ff8b92008-08-17 02:59:30 +000014#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenekabd89ac2008-08-13 04:27:00 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenekc7469542008-07-17 23:15:45 +000016#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
Ted Kremenek8904fbe2008-08-27 23:13:01 +000017#include "llvm/ADT/SmallSet.h"
Chris Lattner1c8962e2008-08-23 22:23:37 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenek8904fbe2008-08-27 23:13:01 +000019
Ted Kremenek0eb0afa2008-02-04 21:59:22 +000020using namespace clang;
21
Ted Kremenek8904fbe2008-08-27 23:13:01 +000022// Give the vtable for ConstraintManager somewhere to live.
23ConstraintManager::~ConstraintManager() {}
24
Ted Kremenekb0f2b9e2008-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 Kremenekabd89ac2008-08-13 04:27:00 +000035const GRState*
36GRStateManager::RemoveDeadBindings(const GRState* St, Stmt* Loc,
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +000037 const LiveVariables& Liveness,
38 DeadSymbolsTy& DSymbols) {
Ted Kremenekb8958d62008-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 Kremenekb15eba42008-10-04 05:50:14 +000045 // for optimum performance.
46 llvm::SmallVector<const MemRegion*, 10> RegionRoots;
Ted Kremenekee930122008-07-17 18:38:48 +000047 StoreManager::LiveSymbolsTy LSymbols;
Ted Kremenekabd89ac2008-08-13 04:27:00 +000048 GRState NewSt = *St;
Ted Kremenekee930122008-07-17 18:38:48 +000049
Ted Kremenekb15eba42008-10-04 05:50:14 +000050 NewSt.Env =
51 EnvMgr.RemoveDeadBindings(NewSt.Env, Loc, Liveness, RegionRoots, LSymbols);
Ted Kremenek08cfd832008-02-08 21:10:02 +000052
Ted Kremenekee930122008-07-17 18:38:48 +000053 // Clean up the store.
54 DSymbols.clear();
Zhongxing Xu44e00b02008-10-16 06:09:51 +000055 NewSt.St = StoreMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness,
Ted Kremenekb15eba42008-10-04 05:50:14 +000056 RegionRoots, LSymbols, DSymbols);
Ted Kremenek80fcbb92008-08-17 03:10:22 +000057
Zhongxing Xuc6b27d02008-08-29 14:52:36 +000058 return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewSt),
59 LSymbols, DSymbols);
Ted Kremenekb8958d62008-02-08 19:17:19 +000060}
Ted Kremenek13f31562008-02-06 00:54:14 +000061
Zhongxing Xu696b3a82008-10-30 05:33:54 +000062const GRState* GRStateManager::BindLoc(const GRState* St, Loc LV, SVal V) {
Ted Kremenek07baa252008-02-21 18:02:17 +000063
Ted Kremenekf22f8682008-07-10 22:03:41 +000064 Store OldStore = St->getStore();
Zhongxing Xu73249322008-10-21 06:27:32 +000065 Store NewStore = StoreMgr->Bind(OldStore, LV, V);
Ted Kremenek9b32cd02008-02-07 04:16:04 +000066
Ted Kremenekf22f8682008-07-10 22:03:41 +000067 if (NewStore == OldStore)
68 return St;
Ted Kremenekbc965a62008-02-18 22:57:02 +000069
Ted Kremenekabd89ac2008-08-13 04:27:00 +000070 GRState NewSt = *St;
Ted Kremenekf22f8682008-07-10 22:03:41 +000071 NewSt.St = NewStore;
72 return getPersistentState(NewSt);
Ted Kremenek0eb0afa2008-02-04 21:59:22 +000073}
74
Zhongxing Xuf05e4ed2008-10-29 02:34:02 +000075const GRState* GRStateManager::BindDecl(const GRState* St, const VarDecl* VD,
76 Expr* Ex, unsigned Count) {
Zhongxing Xu2378ba02008-08-21 22:34:01 +000077 Store OldStore = St->getStore();
78 Store NewStore;
79
80 if (Ex)
Zhongxing Xuf05e4ed2008-10-29 02:34:02 +000081 NewStore = StoreMgr->BindDecl(OldStore, VD, Ex, GetSVal(St, Ex), Count);
Zhongxing Xu2378ba02008-08-21 22:34:01 +000082 else
Zhongxing Xuf05e4ed2008-10-29 02:34:02 +000083 NewStore = StoreMgr->BindDecl(OldStore, VD, Ex);
Zhongxing Xu2378ba02008-08-21 22:34:01 +000084
85 if (NewStore == OldStore)
86 return St;
Ted Kremenek4fbc6412008-08-23 00:50:55 +000087
Zhongxing Xu2378ba02008-08-21 22:34:01 +000088 GRState NewSt = *St;
89 NewSt.St = NewStore;
90 return getPersistentState(NewSt);
91}
92
Ted Kremenekd83daa52008-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,
99 const CompoundLiteralRegion* R,
100 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 Xu097fc982008-10-17 05:57:07 +0000113const GRState* GRStateManager::Unbind(const GRState* St, Loc LV) {
Ted Kremenekf22f8682008-07-10 22:03:41 +0000114 Store OldStore = St->getStore();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000115 Store NewStore = StoreMgr->Remove(OldStore, LV);
Ted Kremenekf22f8682008-07-10 22:03:41 +0000116
117 if (NewStore == OldStore)
118 return St;
119
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000120 GRState NewSt = *St;
Ted Kremenekf22f8682008-07-10 22:03:41 +0000121 NewSt.St = NewStore;
122 return getPersistentState(NewSt);
123}
124
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000125const GRState* GRStateManager::getInitialState() {
Ted Kremenekad5d5c52008-02-26 23:37:01 +0000126
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000127 GRState StateImpl(EnvMgr.getInitialEnvironment(),
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000128 StoreMgr->getInitialStore(),
Ted Kremenek80fcbb92008-08-17 03:10:22 +0000129 GDMFactory.GetEmptyMap());
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000130
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000131 return getPersistentState(StateImpl);
132}
133
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000134const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000135
136 llvm::FoldingSetNodeID ID;
137 State.Profile(ID);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000138 void* InsertPos;
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000139
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000140 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000141 return I;
142
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000143 GRState* I = (GRState*) Alloc.Allocate<GRState>();
144 new (I) GRState(State);
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000145 StateSet.InsertNode(I, InsertPos);
146 return I;
147}
Ted Kremenek17c5f112008-02-11 19:21:59 +0000148
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000149
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000150//===----------------------------------------------------------------------===//
151// State pretty-printing.
152//===----------------------------------------------------------------------===//
Ted Kremenekd3656492008-03-11 18:57:24 +0000153
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000154void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
Zhongxing Xuc6b27d02008-08-29 14:52:36 +0000155 ConstraintManager& ConstraintMgr,
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000156 Printer** Beg, Printer** End,
Ted Kremenekbccfbcc2008-08-13 21:24:49 +0000157 const char* nl, const char* sep) const {
Ted Kremenek17c5f112008-02-11 19:21:59 +0000158
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000159 // Print the store.
160 StoreMgr.print(getStore(), Out, nl, sep);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000161
162 // Print Subexpression bindings.
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000163 bool isFirst = true;
Ted Kremenek17c5f112008-02-11 19:21:59 +0000164
Ted Kremenek07baa252008-02-21 18:02:17 +0000165 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremenek17c5f112008-02-11 19:21:59 +0000166
167 if (isFirst) {
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000168 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremenek17c5f112008-02-11 19:21:59 +0000169 isFirst = false;
170 }
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000171 else { Out << nl; }
Ted Kremenek17c5f112008-02-11 19:21:59 +0000172
173 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000174 llvm::raw_os_ostream OutS(Out);
175 I.getKey()->printPretty(OutS);
176 OutS.flush();
Ted Kremenek17c5f112008-02-11 19:21:59 +0000177 Out << " : ";
178 I.getData().print(Out);
179 }
180
181 // Print block-expression bindings.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000182 isFirst = true;
183
Ted Kremenek07baa252008-02-21 18:02:17 +0000184 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremenek17c5f112008-02-11 19:21:59 +0000185
186 if (isFirst) {
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000187 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremenek17c5f112008-02-11 19:21:59 +0000188 isFirst = false;
189 }
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000190 else { Out << nl; }
Ted Kremenek17c5f112008-02-11 19:21:59 +0000191
192 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000193 llvm::raw_os_ostream OutS(Out);
194 I.getKey()->printPretty(OutS);
195 OutS.flush();
Ted Kremenek17c5f112008-02-11 19:21:59 +0000196 Out << " : ";
197 I.getData().print(Out);
198 }
199
Zhongxing Xuc6b27d02008-08-29 14:52:36 +0000200 ConstraintMgr.print(this, Out, nl, sep);
Ted Kremenekd3656492008-03-11 18:57:24 +0000201
Ted Kremenekbccfbcc2008-08-13 21:24:49 +0000202 // Print checker-specific data.
203 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000204}
Ted Kremenekc7469542008-07-17 23:15:45 +0000205
Ted Kremenekb0f2b9e2008-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 Xu44e00b02008-10-16 06:09:51 +0000217 St->print(Out, *Mgr->StoreMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000218}
219
Ted Kremenek4ae925c2008-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 Kremenekb0f2b9e2008-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
Ted Kremenek4ae925c2008-08-14 21:16:54 +0000242const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
243 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 Kremenekbbafa5b2008-07-22 00:46:16 +0000253
254//===----------------------------------------------------------------------===//
255// Queries.
256//===----------------------------------------------------------------------===//
257
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000258bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000259 const llvm::APSInt& Y) {
260
Zhongxing Xu097fc982008-10-17 05:57:07 +0000261 SVal V = GetSVal(state, Ex);
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000262
Zhongxing Xu097fc982008-10-17 05:57:07 +0000263 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000264 return X->getValue() == Y;
265
Zhongxing Xu097fc982008-10-17 05:57:07 +0000266 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000267 return X->getValue() == Y;
268
Zhongxing Xu097fc982008-10-17 05:57:07 +0000269 if (nonloc::SymbolVal* X = dyn_cast<nonloc::SymbolVal>(&V))
Zhongxing Xuc6b27d02008-08-29 14:52:36 +0000270 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000271
Zhongxing Xu097fc982008-10-17 05:57:07 +0000272 if (loc::SymbolVal* X = dyn_cast<loc::SymbolVal>(&V))
Zhongxing Xuc6b27d02008-08-29 14:52:36 +0000273 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000274
275 return false;
276}
277
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000278bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000279 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
280}
Ted Kremenekbb7a3d92008-09-18 23:09:54 +0000281
282//===----------------------------------------------------------------------===//
283// Persistent values for indexing into the Generic Data Map.
284
285int GRState::NullDerefTag::TagInt = 0;
286