blob: 03b724779e9d201b3186f14be3a95d1d73bbdcb8 [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 Xu1c96b242008-10-17 05:57:07 +000062const GRState* GRStateManager::SetSVal(const GRState* St, Loc LV,
63 SVal V) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000064
Ted Kremenek4323a572008-07-10 22:03:41 +000065 Store OldStore = St->getStore();
Zhongxing Xu8485ec62008-10-21 06:27:32 +000066 Store NewStore = StoreMgr->Bind(OldStore, LV, V);
Ted Kremenek3271f8d2008-02-07 04:16:04 +000067
Ted Kremenek4323a572008-07-10 22:03:41 +000068 if (NewStore == OldStore)
69 return St;
Ted Kremenek692416c2008-02-18 22:57:02 +000070
Ted Kremenek4adc81e2008-08-13 04:27:00 +000071 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +000072 NewSt.St = NewStore;
73 return getPersistentState(NewSt);
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000074}
75
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000076const GRState* GRStateManager::AddDecl(const GRState* St, const VarDecl* VD,
77 Expr* Ex, unsigned Count) {
78 Store OldStore = St->getStore();
79 Store NewStore;
80
81 if (Ex)
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000082 NewStore = StoreMgr->AddDecl(OldStore, VD, Ex,
Zhongxing Xu1c96b242008-10-17 05:57:07 +000083 GetSVal(St, Ex), Count);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000084 else
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000085 NewStore = StoreMgr->AddDecl(OldStore, VD, Ex);
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000086
87 if (NewStore == OldStore)
88 return St;
Ted Kremeneke53c0692008-08-23 00:50:55 +000089
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000090 GRState NewSt = *St;
91 NewSt.St = NewStore;
92 return getPersistentState(NewSt);
93}
94
Ted Kremenek4f090272008-10-27 21:54:31 +000095/// BindCompoundLiteral - Return the store that has the bindings currently
96/// in 'store' plus the bindings for the CompoundLiteral. 'R' is the region
97/// for the compound literal and 'BegInit' and 'EndInit' represent an
98/// array of initializer values.
99const GRState*
100GRStateManager::BindCompoundLiteral(const GRState* state,
101 const CompoundLiteralRegion* R,
102 const SVal* BegInit, const SVal* EndInit) {
103
104 Store oldStore = state->getStore();
105 Store newStore = StoreMgr->BindCompoundLiteral(oldStore, R, BegInit, EndInit);
106
107 if (newStore == oldStore)
108 return state;
109
110 GRState newState = *state;
111 newState.St = newStore;
112 return getPersistentState(newState);
113}
114
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000115const GRState* GRStateManager::Unbind(const GRState* St, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000116 Store OldStore = St->getStore();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000117 Store NewStore = StoreMgr->Remove(OldStore, LV);
Ted Kremenek4323a572008-07-10 22:03:41 +0000118
119 if (NewStore == OldStore)
120 return St;
121
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000122 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +0000123 NewSt.St = NewStore;
124 return getPersistentState(NewSt);
125}
126
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000127const GRState* GRStateManager::getInitialState() {
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000128
Ted Kremenekcaa37242008-08-19 16:51:45 +0000129 GRState StateImpl(EnvMgr.getInitialEnvironment(),
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000130 StoreMgr->getInitialStore(),
Ted Kremenekffdbefd2008-08-17 03:10:22 +0000131 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +0000132
Ted Kremenek9153f732008-02-05 07:17:49 +0000133 return getPersistentState(StateImpl);
134}
135
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000136const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000137
138 llvm::FoldingSetNodeID ID;
139 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000140 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000141
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000142 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000143 return I;
144
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000145 GRState* I = (GRState*) Alloc.Allocate<GRState>();
146 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000147 StateSet.InsertNode(I, InsertPos);
148 return I;
149}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000150
Ted Kremenek59894f92008-03-04 18:30:35 +0000151
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000152//===----------------------------------------------------------------------===//
153// State pretty-printing.
154//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000155
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000156void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000157 ConstraintManager& ConstraintMgr,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000158 Printer** Beg, Printer** End,
Ted Kremenekae6814e2008-08-13 21:24:49 +0000159 const char* nl, const char* sep) const {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000160
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000161 // Print the store.
162 StoreMgr.print(getStore(), Out, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000163
164 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000165 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000166
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000167 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000168
169 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000170 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000171 isFirst = false;
172 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000173 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000174
175 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000176 llvm::raw_os_ostream OutS(Out);
177 I.getKey()->printPretty(OutS);
178 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000179 Out << " : ";
180 I.getData().print(Out);
181 }
182
183 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000184 isFirst = true;
185
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000186 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000187
188 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000189 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000190 isFirst = false;
191 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000192 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000193
194 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000195 llvm::raw_os_ostream OutS(Out);
196 I.getKey()->printPretty(OutS);
197 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000198 Out << " : ";
199 I.getData().print(Out);
200 }
201
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000202 ConstraintMgr.print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000203
Ted Kremenekae6814e2008-08-13 21:24:49 +0000204 // Print checker-specific data.
205 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000206}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000207
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000208void GRStateRef::printDOT(std::ostream& Out) const {
209 print(Out, "\\l", "\\|");
210}
211
212void GRStateRef::printStdErr() const {
213 print(*llvm::cerr);
214}
215
216void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{
217 GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0];
218 GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000219 St->print(Out, *Mgr->StoreMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000220}
221
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000222//===----------------------------------------------------------------------===//
223// Generic Data Map.
224//===----------------------------------------------------------------------===//
225
226void* const* GRState::FindGDM(void* K) const {
227 return GDM.lookup(K);
228}
229
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000230void*
231GRStateManager::FindGDMContext(void* K,
232 void* (*CreateContext)(llvm::BumpPtrAllocator&),
233 void (*DeleteContext)(void*)) {
234
235 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
236 if (!p.first) {
237 p.first = CreateContext(Alloc);
238 p.second = DeleteContext;
239 }
240
241 return p.first;
242}
243
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000244const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
245 GRState::GenericDataMap M1 = St->getGDM();
246 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
247
248 if (M1 == M2)
249 return St;
250
251 GRState NewSt = *St;
252 NewSt.GDM = M2;
253 return getPersistentState(NewSt);
254}
Ted Kremenek584def72008-07-22 00:46:16 +0000255
256//===----------------------------------------------------------------------===//
257// Queries.
258//===----------------------------------------------------------------------===//
259
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000260bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000261 const llvm::APSInt& Y) {
262
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000263 SVal V = GetSVal(state, Ex);
Ted Kremenek584def72008-07-22 00:46:16 +0000264
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000265 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000266 return X->getValue() == Y;
267
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000268 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000269 return X->getValue() == Y;
270
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000271 if (nonloc::SymbolVal* X = dyn_cast<nonloc::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000272 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000273
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000274 if (loc::SymbolVal* X = dyn_cast<loc::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000275 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000276
277 return false;
278}
279
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000280bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenek584def72008-07-22 00:46:16 +0000281 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
282}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000283
284//===----------------------------------------------------------------------===//
285// Persistent values for indexing into the Generic Data Map.
286
287int GRState::NullDerefTag::TagInt = 0;
288