blob: 2f829e65da847730d06bb502f37a11ca9928a45a [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();
Ted Kremenek9e240492008-10-04 05:50:14 +000055 NewSt.St = StMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness,
56 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
Ted Kremenek4adc81e2008-08-13 04:27:00 +000062const GRState* GRStateManager::SetRVal(const GRState* St, LVal LV,
Ted Kremenek4323a572008-07-10 22:03:41 +000063 RVal V) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000064
Ted Kremenek4323a572008-07-10 22:03:41 +000065 Store OldStore = St->getStore();
66 Store NewStore = StMgr->SetRVal(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)
Ted Kremeneke53c0692008-08-23 00:50:55 +000082 NewStore = StMgr->AddDecl(OldStore, *this, VD, Ex,
Zhongxing Xubbe8ff42008-08-21 22:34:01 +000083 GetRVal(St, Ex), Count);
84 else
Ted Kremeneke53c0692008-08-23 00:50:55 +000085 NewStore = StMgr->AddDecl(OldStore, *this, 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 Kremenek4adc81e2008-08-13 04:27:00 +000095const GRState* GRStateManager::Unbind(const GRState* St, LVal LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +000096 Store OldStore = St->getStore();
97 Store NewStore = StMgr->Remove(OldStore, LV);
98
99 if (NewStore == OldStore)
100 return St;
101
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000102 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +0000103 NewSt.St = NewStore;
104 return getPersistentState(NewSt);
105}
106
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000107const GRState* GRStateManager::getInitialState() {
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000108
Ted Kremenekcaa37242008-08-19 16:51:45 +0000109 GRState StateImpl(EnvMgr.getInitialEnvironment(),
110 StMgr->getInitialStore(*this),
Ted Kremenekffdbefd2008-08-17 03:10:22 +0000111 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +0000112
Ted Kremenek9153f732008-02-05 07:17:49 +0000113 return getPersistentState(StateImpl);
114}
115
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000116const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000117
118 llvm::FoldingSetNodeID ID;
119 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000120 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000121
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000122 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000123 return I;
124
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000125 GRState* I = (GRState*) Alloc.Allocate<GRState>();
126 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000127 StateSet.InsertNode(I, InsertPos);
128 return I;
129}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000130
Ted Kremenek59894f92008-03-04 18:30:35 +0000131
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000132//===----------------------------------------------------------------------===//
133// State pretty-printing.
134//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000135
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000136void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000137 ConstraintManager& ConstraintMgr,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000138 Printer** Beg, Printer** End,
Ted Kremenekae6814e2008-08-13 21:24:49 +0000139 const char* nl, const char* sep) const {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000140
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000141 // Print the store.
142 StoreMgr.print(getStore(), Out, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000143
144 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000145 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000146
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000147 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000148
149 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000150 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000151 isFirst = false;
152 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000153 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000154
155 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000156 llvm::raw_os_ostream OutS(Out);
157 I.getKey()->printPretty(OutS);
158 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000159 Out << " : ";
160 I.getData().print(Out);
161 }
162
163 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000164 isFirst = true;
165
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000166 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000167
168 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000169 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000170 isFirst = false;
171 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000172 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000173
174 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000175 llvm::raw_os_ostream OutS(Out);
176 I.getKey()->printPretty(OutS);
177 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000178 Out << " : ";
179 I.getData().print(Out);
180 }
181
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000182 ConstraintMgr.print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000183
Ted Kremenekae6814e2008-08-13 21:24:49 +0000184 // Print checker-specific data.
185 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000186}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000187
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000188void GRStateRef::printDOT(std::ostream& Out) const {
189 print(Out, "\\l", "\\|");
190}
191
192void GRStateRef::printStdErr() const {
193 print(*llvm::cerr);
194}
195
196void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{
197 GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0];
198 GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000199 St->print(Out, *Mgr->StMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000200}
201
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000202//===----------------------------------------------------------------------===//
203// Generic Data Map.
204//===----------------------------------------------------------------------===//
205
206void* const* GRState::FindGDM(void* K) const {
207 return GDM.lookup(K);
208}
209
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000210void*
211GRStateManager::FindGDMContext(void* K,
212 void* (*CreateContext)(llvm::BumpPtrAllocator&),
213 void (*DeleteContext)(void*)) {
214
215 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
216 if (!p.first) {
217 p.first = CreateContext(Alloc);
218 p.second = DeleteContext;
219 }
220
221 return p.first;
222}
223
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000224const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
225 GRState::GenericDataMap M1 = St->getGDM();
226 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
227
228 if (M1 == M2)
229 return St;
230
231 GRState NewSt = *St;
232 NewSt.GDM = M2;
233 return getPersistentState(NewSt);
234}
Ted Kremenek584def72008-07-22 00:46:16 +0000235
236//===----------------------------------------------------------------------===//
237// Queries.
238//===----------------------------------------------------------------------===//
239
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000240bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000241 const llvm::APSInt& Y) {
242
Ted Kremenek584def72008-07-22 00:46:16 +0000243 RVal V = GetRVal(state, Ex);
244
245 if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V))
246 return X->getValue() == Y;
247
248 if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V))
249 return X->getValue() == Y;
250
251 if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000252 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000253
254 if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000255 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000256
257 return false;
258}
259
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000260bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenek584def72008-07-22 00:46:16 +0000261 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
262}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000263
264//===----------------------------------------------------------------------===//
265// Persistent values for indexing into the Generic Data Map.
266
267int GRState::NullDerefTag::TagInt = 0;
268