blob: 221ab064d30a8ab520cbf33e119cc1dd5b1017d5 [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 Kremenekb9cd9a72008-12-05 02:27:51 +000010// This file defines SymbolRef, 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*
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000036GRStateManager::RemoveDeadBindings(const GRState* state, 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 Kremenek0de2e0f2008-12-05 00:47:52 +000048 GRState NewState = *state;
Ted Kremenekee930122008-07-17 18:38:48 +000049
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000050 NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, Loc, Liveness,
51 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();
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000055 NewState.St = StoreMgr->RemoveDeadBindings(&NewState, Loc, Liveness,
56 RegionRoots, LSymbols, DSymbols);
Ted Kremenek80fcbb92008-08-17 03:10:22 +000057
Ted Kremenek0de2e0f2008-12-05 00:47:52 +000058 return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewState),
Zhongxing Xuc6b27d02008-08-29 14:52:36 +000059 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,
Ted Kremenek7012bf32008-11-12 19:21:30 +000076 SVal* InitVal, unsigned Count) {
Zhongxing Xu2378ba02008-08-21 22:34:01 +000077 Store OldStore = St->getStore();
Ted Kremenek7012bf32008-11-12 19:21:30 +000078 Store NewStore = StoreMgr->BindDecl(OldStore, VD, InitVal, Count);
Zhongxing Xu2378ba02008-08-21 22:34:01 +000079
Zhongxing Xu2378ba02008-08-21 22:34:01 +000080 if (NewStore == OldStore)
81 return St;
Ted Kremenek4fbc6412008-08-23 00:50:55 +000082
Zhongxing Xu2378ba02008-08-21 22:34:01 +000083 GRState NewSt = *St;
84 NewSt.St = NewStore;
85 return getPersistentState(NewSt);
86}
87
Ted Kremenekd83daa52008-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 Xuc88ca9d2008-11-07 10:38:33 +000094 const CompoundLiteralExpr* CL, SVal ILV) {
Ted Kremenekd83daa52008-10-27 21:54:31 +000095
96 Store oldStore = state->getStore();
Zhongxing Xuc88ca9d2008-11-07 10:38:33 +000097 Store newStore = StoreMgr->BindCompoundLiteral(oldStore, CL, ILV);
Ted Kremenekd83daa52008-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 Xu097fc982008-10-17 05:57:07 +0000107const GRState* GRStateManager::Unbind(const GRState* St, Loc LV) {
Ted Kremenekf22f8682008-07-10 22:03:41 +0000108 Store OldStore = St->getStore();
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000109 Store NewStore = StoreMgr->Remove(OldStore, LV);
Ted Kremenekf22f8682008-07-10 22:03:41 +0000110
111 if (NewStore == OldStore)
112 return St;
113
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000114 GRState NewSt = *St;
Ted Kremenekf22f8682008-07-10 22:03:41 +0000115 NewSt.St = NewStore;
116 return getPersistentState(NewSt);
117}
118
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000119const GRState* GRStateManager::getInitialState() {
Ted Kremenekad5d5c52008-02-26 23:37:01 +0000120
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000121 GRState StateImpl(EnvMgr.getInitialEnvironment(),
Zhongxing Xu44e00b02008-10-16 06:09:51 +0000122 StoreMgr->getInitialStore(),
Ted Kremenek80fcbb92008-08-17 03:10:22 +0000123 GDMFactory.GetEmptyMap());
Ted Kremeneke2fb8c72008-08-19 16:51:45 +0000124
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000125 return getPersistentState(StateImpl);
126}
127
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000128const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000129
130 llvm::FoldingSetNodeID ID;
131 State.Profile(ID);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000132 void* InsertPos;
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000133
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000134 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000135 return I;
136
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000137 GRState* I = (GRState*) Alloc.Allocate<GRState>();
138 new (I) GRState(State);
Ted Kremenek2d8dce32008-02-05 07:17:49 +0000139 StateSet.InsertNode(I, InsertPos);
140 return I;
141}
Ted Kremenek17c5f112008-02-11 19:21:59 +0000142
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000143
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000144//===----------------------------------------------------------------------===//
145// State pretty-printing.
146//===----------------------------------------------------------------------===//
Ted Kremenekd3656492008-03-11 18:57:24 +0000147
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000148void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
Zhongxing Xuc6b27d02008-08-29 14:52:36 +0000149 ConstraintManager& ConstraintMgr,
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000150 Printer** Beg, Printer** End,
Ted Kremenekbccfbcc2008-08-13 21:24:49 +0000151 const char* nl, const char* sep) const {
Ted Kremenek17c5f112008-02-11 19:21:59 +0000152
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000153 // Print the store.
154 StoreMgr.print(getStore(), Out, nl, sep);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000155
156 // Print Subexpression bindings.
Ted Kremenekbdff9a92008-08-19 22:24:03 +0000157 bool isFirst = true;
Ted Kremenek17c5f112008-02-11 19:21:59 +0000158
Ted Kremenek07baa252008-02-21 18:02:17 +0000159 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremenek17c5f112008-02-11 19:21:59 +0000160
161 if (isFirst) {
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000162 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremenek17c5f112008-02-11 19:21:59 +0000163 isFirst = false;
164 }
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000165 else { Out << nl; }
Ted Kremenek17c5f112008-02-11 19:21:59 +0000166
167 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000168 llvm::raw_os_ostream OutS(Out);
169 I.getKey()->printPretty(OutS);
170 OutS.flush();
Ted Kremenek17c5f112008-02-11 19:21:59 +0000171 Out << " : ";
172 I.getData().print(Out);
173 }
174
175 // Print block-expression bindings.
Ted Kremenek17c5f112008-02-11 19:21:59 +0000176 isFirst = true;
177
Ted Kremenek07baa252008-02-21 18:02:17 +0000178 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremenek17c5f112008-02-11 19:21:59 +0000179
180 if (isFirst) {
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000181 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremenek17c5f112008-02-11 19:21:59 +0000182 isFirst = false;
183 }
Ted Kremeneke1cfa992008-03-04 18:30:35 +0000184 else { Out << nl; }
Ted Kremenek17c5f112008-02-11 19:21:59 +0000185
186 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremenek7b6f67b2008-09-13 05:16:45 +0000187 llvm::raw_os_ostream OutS(Out);
188 I.getKey()->printPretty(OutS);
189 OutS.flush();
Ted Kremenek17c5f112008-02-11 19:21:59 +0000190 Out << " : ";
191 I.getData().print(Out);
192 }
193
Zhongxing Xuc6b27d02008-08-29 14:52:36 +0000194 ConstraintMgr.print(this, Out, nl, sep);
Ted Kremenekd3656492008-03-11 18:57:24 +0000195
Ted Kremenekbccfbcc2008-08-13 21:24:49 +0000196 // Print checker-specific data.
197 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremenek17c5f112008-02-11 19:21:59 +0000198}
Ted Kremenekc7469542008-07-17 23:15:45 +0000199
Ted Kremenekb0f2b9e2008-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 Xu44e00b02008-10-16 06:09:51 +0000211 St->print(Out, *Mgr->StoreMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000212}
213
Ted Kremenek4ae925c2008-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 Kremenekb0f2b9e2008-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 Xue4e038e2008-11-03 05:18:34 +0000236const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek4ae925c2008-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 Kremenekbbafa5b2008-07-22 00:46:16 +0000247
248//===----------------------------------------------------------------------===//
249// Queries.
250//===----------------------------------------------------------------------===//
251
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000252bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000253 const llvm::APSInt& Y) {
254
Zhongxing Xu097fc982008-10-17 05:57:07 +0000255 SVal V = GetSVal(state, Ex);
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000256
Zhongxing Xu097fc982008-10-17 05:57:07 +0000257 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000258 return X->getValue() == Y;
259
Zhongxing Xu097fc982008-10-17 05:57:07 +0000260 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000261 return X->getValue() == Y;
262
Zhongxing Xu097fc982008-10-17 05:57:07 +0000263 if (nonloc::SymbolVal* X = dyn_cast<nonloc::SymbolVal>(&V))
Zhongxing Xuc6b27d02008-08-29 14:52:36 +0000264 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000265
Zhongxing Xu097fc982008-10-17 05:57:07 +0000266 if (loc::SymbolVal* X = dyn_cast<loc::SymbolVal>(&V))
Zhongxing Xuc6b27d02008-08-29 14:52:36 +0000267 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000268
269 return false;
270}
271
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +0000272bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenekbbafa5b2008-07-22 00:46:16 +0000273 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
274}
Ted Kremenekbb7a3d92008-09-18 23:09:54 +0000275
276//===----------------------------------------------------------------------===//
277// Persistent values for indexing into the Generic Data Map.
278
279int GRState::NullDerefTag::TagInt = 0;
280