blob: 83ab508117ecc6a758e8ac9d582e5872fd62389d [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*
Ted Kremenek2ed14be2008-12-05 00:47:52 +000036GRStateManager::RemoveDeadBindings(const GRState* state, 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 Kremenek2ed14be2008-12-05 00:47:52 +000048 GRState NewState = *state;
Ted Kremenekf59bf482008-07-17 18:38:48 +000049
Ted Kremenek2ed14be2008-12-05 00:47:52 +000050 NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, Loc, Liveness,
51 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();
Ted Kremenek2ed14be2008-12-05 00:47:52 +000055 NewState.St = StoreMgr->RemoveDeadBindings(&NewState, Loc, Liveness,
56 RegionRoots, LSymbols, DSymbols);
Ted Kremenekffdbefd2008-08-17 03:10:22 +000057
Ted Kremenek2ed14be2008-12-05 00:47:52 +000058 return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewState),
Zhongxing Xu39cfed32008-08-29 14:52:36 +000059 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,
Ted Kremenekd4931632008-11-12 19:21:30 +000076 SVal* InitVal, unsigned Count) {
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000077 Store OldStore = St->getStore();
Ted Kremenekd4931632008-11-12 19:21:30 +000078 Store NewStore = StoreMgr->BindDecl(OldStore, VD, InitVal, Count);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000079
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000080 if (NewStore == OldStore)
81 return St;
Ted Kremeneke53c0692008-08-23 00:50:55 +000082
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000083 GRState NewSt = *St;
84 NewSt.St = NewStore;
85 return getPersistentState(NewSt);
86}
87
Ted Kremenek4f090272008-10-27 21:54:31 +000088/// BindCompoundLiteral - Return the store that has the bindings currently
89/// in 'store' plus the bindings for the CompoundLiteral. 'R' is the region
90/// for the compound literal and 'BegInit' and 'EndInit' represent an
91/// array of initializer values.
92const GRState*
93GRStateManager::BindCompoundLiteral(const GRState* state,
Zhongxing Xuf22679e2008-11-07 10:38:33 +000094 const CompoundLiteralExpr* CL, SVal ILV) {
Ted Kremenek4f090272008-10-27 21:54:31 +000095
96 Store oldStore = state->getStore();
Zhongxing Xuf22679e2008-11-07 10:38:33 +000097 Store newStore = StoreMgr->BindCompoundLiteral(oldStore, CL, ILV);
Ted Kremenek4f090272008-10-27 21:54:31 +000098
99 if (newStore == oldStore)
100 return state;
101
102 GRState newState = *state;
103 newState.St = newStore;
104 return getPersistentState(newState);
105}
106
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000107const GRState* GRStateManager::Unbind(const GRState* St, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000108 Store OldStore = St->getStore();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000109 Store NewStore = StoreMgr->Remove(OldStore, LV);
Ted Kremenek4323a572008-07-10 22:03:41 +0000110
111 if (NewStore == OldStore)
112 return St;
113
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000114 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +0000115 NewSt.St = NewStore;
116 return getPersistentState(NewSt);
117}
118
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000119const GRState* GRStateManager::getInitialState() {
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000120
Ted Kremenekcaa37242008-08-19 16:51:45 +0000121 GRState StateImpl(EnvMgr.getInitialEnvironment(),
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000122 StoreMgr->getInitialStore(),
Ted Kremenekffdbefd2008-08-17 03:10:22 +0000123 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +0000124
Ted Kremenek9153f732008-02-05 07:17:49 +0000125 return getPersistentState(StateImpl);
126}
127
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000128const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000129
130 llvm::FoldingSetNodeID ID;
131 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000132 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000133
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000134 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000135 return I;
136
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000137 GRState* I = (GRState*) Alloc.Allocate<GRState>();
138 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000139 StateSet.InsertNode(I, InsertPos);
140 return I;
141}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000142
Ted Kremenek59894f92008-03-04 18:30:35 +0000143
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000144//===----------------------------------------------------------------------===//
145// State pretty-printing.
146//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000147
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000148void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000149 ConstraintManager& ConstraintMgr,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000150 Printer** Beg, Printer** End,
Ted Kremenekae6814e2008-08-13 21:24:49 +0000151 const char* nl, const char* sep) const {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000152
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000153 // Print the store.
154 StoreMgr.print(getStore(), Out, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000155
156 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000157 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000158
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000159 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000160
161 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000162 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000163 isFirst = false;
164 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000165 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000166
167 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000168 llvm::raw_os_ostream OutS(Out);
169 I.getKey()->printPretty(OutS);
170 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000171 Out << " : ";
172 I.getData().print(Out);
173 }
174
175 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000176 isFirst = true;
177
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000178 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000179
180 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000181 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000182 isFirst = false;
183 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000184 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000185
186 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000187 llvm::raw_os_ostream OutS(Out);
188 I.getKey()->printPretty(OutS);
189 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000190 Out << " : ";
191 I.getData().print(Out);
192 }
193
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000194 ConstraintMgr.print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000195
Ted Kremenekae6814e2008-08-13 21:24:49 +0000196 // Print checker-specific data.
197 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000198}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000199
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000200void GRStateRef::printDOT(std::ostream& Out) const {
201 print(Out, "\\l", "\\|");
202}
203
204void GRStateRef::printStdErr() const {
205 print(*llvm::cerr);
206}
207
208void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{
209 GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0];
210 GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000211 St->print(Out, *Mgr->StoreMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000212}
213
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000214//===----------------------------------------------------------------------===//
215// Generic Data Map.
216//===----------------------------------------------------------------------===//
217
218void* const* GRState::FindGDM(void* K) const {
219 return GDM.lookup(K);
220}
221
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000222void*
223GRStateManager::FindGDMContext(void* K,
224 void* (*CreateContext)(llvm::BumpPtrAllocator&),
225 void (*DeleteContext)(void*)) {
226
227 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
228 if (!p.first) {
229 p.first = CreateContext(Alloc);
230 p.second = DeleteContext;
231 }
232
233 return p.first;
234}
235
Zhongxing Xu4230da62008-11-03 05:18:34 +0000236const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000237 GRState::GenericDataMap M1 = St->getGDM();
238 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
239
240 if (M1 == M2)
241 return St;
242
243 GRState NewSt = *St;
244 NewSt.GDM = M2;
245 return getPersistentState(NewSt);
246}
Ted Kremenek584def72008-07-22 00:46:16 +0000247
248//===----------------------------------------------------------------------===//
249// Queries.
250//===----------------------------------------------------------------------===//
251
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000252bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000253 const llvm::APSInt& Y) {
254
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000255 SVal V = GetSVal(state, Ex);
Ted Kremenek584def72008-07-22 00:46:16 +0000256
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000257 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000258 return X->getValue() == Y;
259
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000260 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000261 return X->getValue() == Y;
262
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000263 if (nonloc::SymbolVal* X = dyn_cast<nonloc::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000264 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000265
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000266 if (loc::SymbolVal* X = dyn_cast<loc::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000267 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000268
269 return false;
270}
271
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000272bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenek584def72008-07-22 00:46:16 +0000273 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
274}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000275
276//===----------------------------------------------------------------------===//
277// Persistent values for indexing into the Generic Data Map.
278
279int GRState::NullDerefTag::TagInt = 0;
280