blob: ea6f7a03d6915849f501f19cad1ef1ad3db9c9ba [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 Kremenek2dabd432008-12-05 02:27:51 +000010// This file defines SymbolRef, 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 Kremenek241677a2009-01-21 22:26:05 +000037 SymbolReaper& SymReaper) {
38
Ted Kremenekb87d9092008-02-08 19:17:19 +000039 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
40 // The roots are any Block-level exprs and Decls that our liveness algorithm
41 // tells us are live. We then see what Decls they may reference, and keep
42 // those around. This code more than likely can be made faster, and the
43 // frequency of which this method is called should be experimented with
Ted Kremenek9e240492008-10-04 05:50:14 +000044 // for optimum performance.
45 llvm::SmallVector<const MemRegion*, 10> RegionRoots;
Ted Kremenek2ed14be2008-12-05 00:47:52 +000046 GRState NewState = *state;
Ted Kremenekf59bf482008-07-17 18:38:48 +000047
Ted Kremenek241677a2009-01-21 22:26:05 +000048 NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, Loc, SymReaper,
49 RegionRoots);
Ted Kremenek016f52f2008-02-08 21:10:02 +000050
Ted Kremenekf59bf482008-07-17 18:38:48 +000051 // Clean up the store.
Ted Kremenek241677a2009-01-21 22:26:05 +000052 NewState.St = StoreMgr->RemoveDeadBindings(&NewState, Loc, SymReaper,
53 RegionRoots);
Ted Kremenekffdbefd2008-08-17 03:10:22 +000054
Ted Kremenek2ed14be2008-12-05 00:47:52 +000055 return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewState),
Ted Kremenek241677a2009-01-21 22:26:05 +000056 SymReaper);
Ted Kremenekb87d9092008-02-08 19:17:19 +000057}
Ted Kremenek862d5bb2008-02-06 00:54:14 +000058
Zhongxing Xu1c96b242008-10-17 05:57:07 +000059const GRState* GRStateManager::Unbind(const GRState* St, Loc LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +000060 Store OldStore = St->getStore();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000061 Store NewStore = StoreMgr->Remove(OldStore, LV);
Ted Kremenek4323a572008-07-10 22:03:41 +000062
63 if (NewStore == OldStore)
64 return St;
65
Ted Kremenek4adc81e2008-08-13 04:27:00 +000066 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +000067 NewSt.St = NewStore;
68 return getPersistentState(NewSt);
69}
70
Ted Kremenek4adc81e2008-08-13 04:27:00 +000071const GRState* GRStateManager::getInitialState() {
Ted Kremenek5a7b3822008-02-26 23:37:01 +000072
Ted Kremenekcaa37242008-08-19 16:51:45 +000073 GRState StateImpl(EnvMgr.getInitialEnvironment(),
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +000074 StoreMgr->getInitialStore(),
Ted Kremenekffdbefd2008-08-17 03:10:22 +000075 GDMFactory.GetEmptyMap());
Ted Kremenekcaa37242008-08-19 16:51:45 +000076
Ted Kremenek9153f732008-02-05 07:17:49 +000077 return getPersistentState(StateImpl);
78}
79
Ted Kremenek4adc81e2008-08-13 04:27:00 +000080const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +000081
82 llvm::FoldingSetNodeID ID;
83 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +000084 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +000085
Ted Kremenek4adc81e2008-08-13 04:27:00 +000086 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +000087 return I;
88
Ted Kremenek4adc81e2008-08-13 04:27:00 +000089 GRState* I = (GRState*) Alloc.Allocate<GRState>();
90 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +000091 StateSet.InsertNode(I, InsertPos);
92 return I;
93}
Ted Kremeneke7d22112008-02-11 19:21:59 +000094
Zhongxing Xu4193eca2008-12-20 06:32:12 +000095const GRState* GRStateManager::MakeStateWithStore(const GRState* St,
96 Store store) {
97 GRState NewSt = *St;
98 NewSt.St = store;
99 return getPersistentState(NewSt);
100}
101
Ted Kremenek59894f92008-03-04 18:30:35 +0000102
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000103//===----------------------------------------------------------------------===//
104// State pretty-printing.
105//===----------------------------------------------------------------------===//
Ted Kremenek461f9772008-03-11 18:57:24 +0000106
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000107void GRState::print(std::ostream& Out, StoreManager& StoreMgr,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000108 ConstraintManager& ConstraintMgr,
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000109 Printer** Beg, Printer** End,
Ted Kremenekae6814e2008-08-13 21:24:49 +0000110 const char* nl, const char* sep) const {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000111
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000112 // Print the store.
113 StoreMgr.print(getStore(), Out, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000114
115 // Print Subexpression bindings.
Ted Kremeneka622d8c2008-08-19 22:24:03 +0000116 bool isFirst = true;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000117
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000118 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000119
120 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000121 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000122 isFirst = false;
123 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000124 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000125
126 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000127 llvm::raw_os_ostream OutS(Out);
128 I.getKey()->printPretty(OutS);
129 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000130 Out << " : ";
131 I.getData().print(Out);
132 }
133
134 // Print block-expression bindings.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000135 isFirst = true;
136
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000137 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000138
139 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000140 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000141 isFirst = false;
142 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000143 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000144
145 Out << " (" << (void*) I.getKey() << ") ";
Ted Kremeneka95d3752008-09-13 05:16:45 +0000146 llvm::raw_os_ostream OutS(Out);
147 I.getKey()->printPretty(OutS);
148 OutS.flush();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000149 Out << " : ";
150 I.getData().print(Out);
151 }
152
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000153 ConstraintMgr.print(this, Out, nl, sep);
Ted Kremenek461f9772008-03-11 18:57:24 +0000154
Ted Kremenekae6814e2008-08-13 21:24:49 +0000155 // Print checker-specific data.
156 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000157}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000158
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000159void GRStateRef::printDOT(std::ostream& Out) const {
160 print(Out, "\\l", "\\|");
161}
162
163void GRStateRef::printStdErr() const {
164 print(*llvm::cerr);
165}
166
167void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{
168 GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0];
169 GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size();
Zhongxing Xu6d69b5d2008-10-16 06:09:51 +0000170 St->print(Out, *Mgr->StoreMgr, *Mgr->ConstraintMgr, beg, end, nl, sep);
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000171}
172
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000173//===----------------------------------------------------------------------===//
174// Generic Data Map.
175//===----------------------------------------------------------------------===//
176
177void* const* GRState::FindGDM(void* K) const {
178 return GDM.lookup(K);
179}
180
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000181void*
182GRStateManager::FindGDMContext(void* K,
183 void* (*CreateContext)(llvm::BumpPtrAllocator&),
184 void (*DeleteContext)(void*)) {
185
186 std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
187 if (!p.first) {
188 p.first = CreateContext(Alloc);
189 p.second = DeleteContext;
190 }
191
192 return p.first;
193}
194
Zhongxing Xu4230da62008-11-03 05:18:34 +0000195const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){
Ted Kremenek72cd17f2008-08-14 21:16:54 +0000196 GRState::GenericDataMap M1 = St->getGDM();
197 GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data);
198
199 if (M1 == M2)
200 return St;
201
202 GRState NewSt = *St;
203 NewSt.GDM = M2;
204 return getPersistentState(NewSt);
205}
Ted Kremenek584def72008-07-22 00:46:16 +0000206
207//===----------------------------------------------------------------------===//
208// Queries.
209//===----------------------------------------------------------------------===//
210
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000211bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000212 const llvm::APSInt& Y) {
213
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000214 SVal V = GetSVal(state, Ex);
Ted Kremenek584def72008-07-22 00:46:16 +0000215
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000216 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000217 return X->getValue() == Y;
218
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000219 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
Ted Kremenek584def72008-07-22 00:46:16 +0000220 return X->getValue() == Y;
221
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000222 if (nonloc::SymbolVal* X = dyn_cast<nonloc::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000223 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000224
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000225 if (loc::SymbolVal* X = dyn_cast<loc::SymbolVal>(&V))
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000226 return ConstraintMgr->isEqual(state, X->getSymbol(), Y);
Ted Kremenek584def72008-07-22 00:46:16 +0000227
228 return false;
229}
230
Ted Kremenek1c72ef02008-08-16 00:49:49 +0000231bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) {
Ted Kremenek584def72008-07-22 00:46:16 +0000232 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
233}
Ted Kremenek7360fda2008-09-18 23:09:54 +0000234
235//===----------------------------------------------------------------------===//
236// Persistent values for indexing into the Generic Data Map.
237
238int GRState::NullDerefTag::TagInt = 0;
239