blob: 57f9c6b27933965a426fea1cac2986c5baa2808c [file] [log] [blame]
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +00001//= GRState*cpp - Path-Sens. "State" for tracking valuues -----*- C++ -*--=//
Ted Kremenekd8902e02008-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 Kremenek5ab5a1b2008-08-13 04:27:00 +000010// This file defines SymbolID, ExprBindKey, and GRState*
Ted Kremenekd8902e02008-02-05 07:17:49 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekedd9a182008-08-17 02:59:30 +000014#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek9c32a1e2008-07-17 23:15:45 +000016#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
Ted Kremenekb5ef37f2008-08-27 23:13:01 +000017#include "llvm/ADT/SmallSet.h"
Chris Lattnera2e25e52008-08-23 22:23:37 +000018#include "llvm/Support/raw_ostream.h"
Ted Kremenekb5ef37f2008-08-27 23:13:01 +000019
Ted Kremenek7746a622008-02-04 21:59:22 +000020using namespace clang;
21
Ted Kremenekb5ef37f2008-08-27 23:13:01 +000022// Give the vtable for ConstraintManager somewhere to live.
23ConstraintManager::~ConstraintManager() {}
24
Ted Kremenekceba6ea2008-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 Kremenek5ab5a1b2008-08-13 04:27:00 +000035const GRState*
36GRStateManager::RemoveDeadBindings(const GRState* St, Stmt* Loc,
Ted Kremenekceba6ea2008-08-16 00:49:49 +000037 const LiveVariables& Liveness,
38 DeadSymbolsTy& DSymbols) {
Ted Kremenek80ff44f2008-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 Kremenek5ca90a22008-10-04 05:50:14 +000045 // for optimum performance.
46 llvm::SmallVector<const MemRegion*, 10> RegionRoots;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +000047 StoreManager::LiveSymbolsTy LSymbols;
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +000048 GRState NewSt = *St;
Ted Kremenek88a6b7f2008-07-17 18:38:48 +000049
Ted Kremenek5ca90a22008-10-04 05:50:14 +000050 NewSt.Env =
51 EnvMgr.RemoveDeadBindings(NewSt.Env, Loc, Liveness, RegionRoots, LSymbols);
Ted Kremenekb54312d2008-02-08 21:10:02 +000052
Ted Kremenek88a6b7f2008-07-17 18:38:48 +000053 // Clean up the store.
54 DSymbols.clear();
Zhongxing Xu232c7922008-10-16 06:09:51 +000055 NewSt.St = StoreMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness,
Ted Kremenek5ca90a22008-10-04 05:50:14 +000056 RegionRoots, LSymbols, DSymbols);
Ted Kremenek90d488f2008-08-17 03:10:22 +000057
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +000058 return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewSt),
59 LSymbols, DSymbols);
Ted Kremenek80ff44f2008-02-08 19:17:19 +000060}
Ted Kremenek03e7b552008-02-06 00:54:14 +000061
Zhongxing Xua15cfd42008-10-30 05:33:54 +000062const GRState* GRStateManager::BindLoc(const GRState* St, Loc LV, SVal V) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +000063
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000064 Store OldStore = St->getStore();
Zhongxing Xu8f6855e642008-10-21 06:27:32 +000065 Store NewStore = StoreMgr->Bind(OldStore, LV, V);
Ted Kremenek88da1de2008-02-07 04:16:04 +000066
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000067 if (NewStore == OldStore)
68 return St;
Ted Kremenek346169f2008-02-18 22:57:02 +000069
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +000070 GRState NewSt = *St;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +000071 NewSt.St = NewStore;
72 return getPersistentState(NewSt);
Ted Kremenek7746a622008-02-04 21:59:22 +000073}
74
Zhongxing Xu628ae872008-10-29 02:34:02 +000075const GRState* GRStateManager::BindDecl(const GRState* St, const VarDecl* VD,
76 Expr* Ex, unsigned Count) {
Zhongxing Xud95495f2008-08-21 22:34:01 +000077 Store OldStore = St->getStore();
78 Store NewStore;
79
80 if (Ex)
Zhongxing Xu628ae872008-10-29 02:34:02 +000081 NewStore = StoreMgr->BindDecl(OldStore, VD, Ex, GetSVal(St, Ex), Count);
Zhongxing Xud95495f2008-08-21 22:34:01 +000082 else
Zhongxing Xu628ae872008-10-29 02:34:02 +000083 NewStore = StoreMgr->BindDecl(OldStore, VD, Ex);
Zhongxing Xud95495f2008-08-21 22:34:01 +000084
85 if (NewStore == OldStore)
86 return St;
Ted Kremenek4e7713c2008-08-23 00:50:55 +000087
Zhongxing Xud95495f2008-08-21 22:34:01 +000088 GRState NewSt = *St;
89 NewSt.St = NewStore;
90 return getPersistentState(NewSt);
91}
92
Ted Kremenekbf263682008-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 Xu2c677c32008-11-07 10:38:33 +000099 const CompoundLiteralExpr* CL, SVal ILV) {
Ted Kremenekbf263682008-10-27 21:54:31 +0000100
101 Store oldStore = state->getStore();
Zhongxing Xu2c677c32008-11-07 10:38:33 +0000102 Store newStore = StoreMgr->BindCompoundLiteral(oldStore, CL, ILV);
Ted Kremenekbf263682008-10-27 21:54:31 +0000103
104 if (newStore == oldStore)
105 return state;
106
107 GRState newState = *state;
108 newState.St = newStore;
109 return getPersistentState(newState);
110}
111
Zhongxing Xu27f17422008-10-17 05:57:07 +0000112const GRState* GRStateManager::Unbind(const GRState* St, Loc LV) {
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000113 Store OldStore = St->getStore();
Zhongxing Xu232c7922008-10-16 06:09:51 +0000114 Store NewStore = StoreMgr->Remove(OldStore, LV);
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000115
116 if (NewStore == OldStore)
117 return St;
118
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000119 GRState NewSt = *St;
Ted Kremeneka7b8ffb2008-07-10 22:03:41 +0000120 NewSt.St = NewStore;
121 return getPersistentState(NewSt);
122}
123
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000124const GRState* GRStateManager::getInitialState() {
Ted Kremenek6217dff2008-02-26 23:37:01 +0000125
Ted Kremenek67102b22008-08-19 16:51:45 +0000126 GRState StateImpl(EnvMgr.getInitialEnvironment(),
Zhongxing Xu232c7922008-10-16 06:09:51 +0000127 StoreMgr->getInitialStore(),
Ted Kremenek90d488f2008-08-17 03:10:22 +0000128 GDMFactory.GetEmptyMap());
Ted Kremenek67102b22008-08-19 16:51:45 +0000129
Ted Kremenekd8902e02008-02-05 07:17:49 +0000130 return getPersistentState(StateImpl);
131}
132
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000133const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenekd8902e02008-02-05 07:17:49 +0000134
135 llvm::FoldingSetNodeID ID;
136 State.Profile(ID);
Ted Kremeneked3be172008-02-11 19:21:59 +0000137 void* InsertPos;
Ted Kremenekd8902e02008-02-05 07:17:49 +0000138
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000139 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenekd8902e02008-02-05 07:17:49 +0000140 return I;
141
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000142 GRState* I = (GRState*) Alloc.Allocate<GRState>();
143 new (I) GRState(State);
Ted Kremenekd8902e02008-02-05 07:17:49 +0000144 StateSet.InsertNode(I, InsertPos);
145 return I;
146}
Ted Kremeneked3be172008-02-11 19:21:59 +0000147
Ted Kremenek122a5292008-03-04 18:30:35 +0000148
Ted Kremenekceba6ea2008-08-16 00:49:49 +0000149//===----------------------------------------------------------------------===//
150// State pretty-printing.
151//===----------------------------------------------------------------------===//
Ted Kremenek827d0fc2008-03-11 18:57:24 +0000152
Ted Kremenek19edd212008-08-19 22:24:03 +0000153void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000154 ConstraintManager& ConstraintMgr,
Ted Kremenek19edd212008-08-19 22:24:03 +0000155 Printer** Beg, Printer** End,
Ted Kremenek16306102008-08-13 21:24:49 +0000156 const char* nl, const char* sep) const {
Ted Kremeneked3be172008-02-11 19:21:59 +0000157
Ted Kremenek19edd212008-08-19 22:24:03 +0000158 // Print the store.
159 StoreMgr.print(getStore(), Out, nl, sep);
Ted Kremeneked3be172008-02-11 19:21:59 +0000160
161 // Print Subexpression bindings.
Ted Kremenek19edd212008-08-19 22:24:03 +0000162 bool isFirst = true;
Ted Kremeneked3be172008-02-11 19:21:59 +0000163
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000164 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneked3be172008-02-11 19:21:59 +0000165
166 if (isFirst) {
Ted Kremenek122a5292008-03-04 18:30:35 +0000167 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneked3be172008-02-11 19:21:59 +0000168 isFirst = false;
169 }
Ted Kremenek122a5292008-03-04 18:30:35 +0000170 else { Out << nl; }
Ted Kremeneked3be172008-02-11 19:21:59 +0000171
172 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000173 llvm::raw_os_ostream OutS(Out);
174 I.getKey()->printPretty(OutS);
175 OutS.flush();
Ted Kremeneked3be172008-02-11 19:21:59 +0000176 Out << " : ";
177 I.getData().print(Out);
178 }
179
180 // Print block-expression bindings.
Ted Kremeneked3be172008-02-11 19:21:59 +0000181 isFirst = true;
182
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000183 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneked3be172008-02-11 19:21:59 +0000184
185 if (isFirst) {
Ted Kremenek122a5292008-03-04 18:30:35 +0000186 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneked3be172008-02-11 19:21:59 +0000187 isFirst = false;
188 }
Ted Kremenek122a5292008-03-04 18:30:35 +0000189 else { Out << nl; }
Ted Kremeneked3be172008-02-11 19:21:59 +0000190
191 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremenek2d470fc2008-09-13 05:16:45 +0000192 llvm::raw_os_ostream OutS(Out);
193 I.getKey()->printPretty(OutS);
194 OutS.flush();
Ted Kremeneked3be172008-02-11 19:21:59 +0000195 Out << " : ";
196 I.getData().print(Out);
197 }
198
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000199 ConstraintMgr.print(this, Out, nl, sep);
Ted Kremenek827d0fc2008-03-11 18:57:24 +0000200
Ted Kremenek16306102008-08-13 21:24:49 +0000201 // Print checker-specific data.
202 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremeneked3be172008-02-11 19:21:59 +0000203}
Ted Kremenek9c32a1e2008-07-17 23:15:45 +0000204
Ted Kremenekceba6ea2008-08-16 00:49:49 +0000205void GRStateRef::printDOT(std::ostream& Out) const {
206 print(Out, "\\l", "\\|");
207}
208
209void GRStateRef::printStdErr() const {
210 print(*llvm::cerr);
211}
212
213void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{
214 GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0];
215 GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size();
Zhongxing Xu232c7922008-10-16 06:09:51 +0000216 St->print(Out, *Mgr->StoreMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
Ted Kremenekceba6ea2008-08-16 00:49:49 +0000217}
218
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +0000219//===----------------------------------------------------------------------===//
220// Generic Data Map.
221//===----------------------------------------------------------------------===//
222
223void* const* GRState::FindGDM(void* K) const {
224 return GDM.lookup(K);
225}
226
Ted Kremenekceba6ea2008-08-16 00:49:49 +0000227void*
228GRStateManager::FindGDMContext(void* K,
229 void* (*CreateContext)(llvm::BumpPtrAllocator&),
230 void (*DeleteContext)(void*)) {
231
232 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
233 if (!p.first) {
234 p.first = CreateContext(Alloc);
235 p.second = DeleteContext;
236 }
237
238 return p.first;
239}
240
Zhongxing Xu8ea09cc2008-11-03 05:18:34 +0000241const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +0000242 GRState::GenericDataMap M1 = St->getGDM();
243 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
244
245 if (M1 == M2)
246 return St;
247
248 GRState NewSt = *St;
249 NewSt.GDM = M2;
250 return getPersistentState(NewSt);
251}
Ted Kremenek98f6e582008-07-22 00:46:16 +0000252
253//===----------------------------------------------------------------------===//
254// Queries.
255//===----------------------------------------------------------------------===//
256
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000257bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenekceba6ea2008-08-16 00:49:49 +0000258 const llvm::APSInt& Y) {
259
Zhongxing Xu27f17422008-10-17 05:57:07 +0000260 SVal V = GetSVal(state, Ex);
Ted Kremenek98f6e582008-07-22 00:46:16 +0000261
Zhongxing Xu27f17422008-10-17 05:57:07 +0000262 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek98f6e582008-07-22 00:46:16 +0000263 return X->getValue() == Y;
264
Zhongxing Xu27f17422008-10-17 05:57:07 +0000265 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek98f6e582008-07-22 00:46:16 +0000266 return X->getValue() == Y;
267
Zhongxing Xu27f17422008-10-17 05:57:07 +0000268 if (nonloc::SymbolVal* X = dyn_cast<nonloc::SymbolVal>(&V))
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000269 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek98f6e582008-07-22 00:46:16 +0000270
Zhongxing Xu27f17422008-10-17 05:57:07 +0000271 if (loc::SymbolVal* X = dyn_cast<loc::SymbolVal>(&V))
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000272 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek98f6e582008-07-22 00:46:16 +0000273
274 return false;
275}
276
Ted Kremenekceba6ea2008-08-16 00:49:49 +0000277bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenek98f6e582008-07-22 00:46:16 +0000278 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
279}
Ted Kremenekb42f4822008-09-18 23:09:54 +0000280
281//===----------------------------------------------------------------------===//
282// Persistent values for indexing into the Generic Data Map.
283
284int GRState::NullDerefTag::TagInt = 0;
285