blob: 28b3fce050bb8fe7dbfbf513b951da7fa0a49936 [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001//= RValues.cpp - Abstract RValues for Path-Sens. Value Tracking -*- C++ -*-==//
2//
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//
10// This file defines SVal, Loc, and NonLoc, classes that represent
11// abstract r-values for use with path-sensitive value tracking.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Checker/PathSensitive/GRState.h"
16#include "clang/Basic/IdentifierTable.h"
17
18using namespace clang;
19using llvm::dyn_cast;
20using llvm::cast;
21using llvm::APSInt;
22
23//===----------------------------------------------------------------------===//
24// Symbol iteration within an SVal.
25//===----------------------------------------------------------------------===//
26
27
28//===----------------------------------------------------------------------===//
29// Utility methods.
30//===----------------------------------------------------------------------===//
31
32bool SVal::hasConjuredSymbol() const {
33 if (const nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(this)) {
34 SymbolRef sym = SV->getSymbol();
35 if (isa<SymbolConjured>(sym))
36 return true;
37 }
38
39 if (const loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(this)) {
40 const MemRegion *R = RV->getRegion();
41 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
42 SymbolRef sym = SR->getSymbol();
43 if (isa<SymbolConjured>(sym))
44 return true;
45 }
46 }
47
48 return false;
49}
50
51const FunctionDecl *SVal::getAsFunctionDecl() const {
52 if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(this)) {
53 const MemRegion* R = X->getRegion();
54 if (const FunctionTextRegion *CTR = R->getAs<FunctionTextRegion>())
55 return CTR->getDecl();
56 }
57
58 return NULL;
59}
60
61/// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
62/// wraps a symbol, return that SymbolRef. Otherwise return 0.
63// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
64SymbolRef SVal::getAsLocSymbol() const {
65 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
66 const MemRegion *R = X->StripCasts();
67 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
68 return SymR->getSymbol();
69 }
70 return NULL;
71}
72
73/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
74/// Otherwise return 0.
75// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
76SymbolRef SVal::getAsSymbol() const {
77 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
78 return X->getSymbol();
79
80 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
81 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
82 return Y;
83
84 return getAsLocSymbol();
85}
86
87/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
88/// return that expression. Otherwise return NULL.
89const SymExpr *SVal::getAsSymbolicExpression() const {
90 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
91 return X->getSymbolicExpression();
92
93 return getAsSymbol();
94}
95
96const MemRegion *SVal::getAsRegion() const {
97 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
98 return X->getRegion();
99
100 if (const nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(this)) {
101 return X->getLoc().getAsRegion();
102 }
103
104 return 0;
105}
106
107const MemRegion *loc::MemRegionVal::StripCasts() const {
108 const MemRegion *R = getRegion();
109 return R ? R->StripCasts() : NULL;
110}
111
112bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
113 return itr == X.itr;
114}
115
116bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
117 return itr != X.itr;
118}
119
120SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
121 itr.push_back(SE);
122 while (!isa<SymbolData>(itr.back())) expand();
123}
124
125SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
126 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
127 assert(isa<SymbolData>(itr.back()));
128 itr.pop_back();
129 if (!itr.empty())
130 while (!isa<SymbolData>(itr.back())) expand();
131 return *this;
132}
133
134SymbolRef SVal::symbol_iterator::operator*() {
135 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
136 return cast<SymbolData>(itr.back());
137}
138
139void SVal::symbol_iterator::expand() {
140 const SymExpr *SE = itr.back();
141 itr.pop_back();
142
143 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
144 itr.push_back(SIE->getLHS());
145 return;
146 }
147 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
148 itr.push_back(SSE->getLHS());
149 itr.push_back(SSE->getRHS());
150 return;
151 }
152
153 assert(false && "unhandled expansion case");
154}
155
156const void *nonloc::LazyCompoundVal::getStore() const {
157 return static_cast<const LazyCompoundValData*>(Data)->getStore();
158}
159
160const TypedRegion *nonloc::LazyCompoundVal::getRegion() const {
161 return static_cast<const LazyCompoundValData*>(Data)->getRegion();
162}
163
164//===----------------------------------------------------------------------===//
165// Other Iterators.
166//===----------------------------------------------------------------------===//
167
168nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
169 return getValue()->begin();
170}
171
172nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
173 return getValue()->end();
174}
175
176//===----------------------------------------------------------------------===//
177// Useful predicates.
178//===----------------------------------------------------------------------===//
179
180bool SVal::isConstant() const {
181 return isa<nonloc::ConcreteInt>(this) || isa<loc::ConcreteInt>(this);
182}
183
184bool SVal::isZeroConstant() const {
185 if (isa<loc::ConcreteInt>(*this))
186 return cast<loc::ConcreteInt>(*this).getValue() == 0;
187 else if (isa<nonloc::ConcreteInt>(*this))
188 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
189 else
190 return false;
191}
192
193
194//===----------------------------------------------------------------------===//
195// Transfer function dispatch for Non-Locs.
196//===----------------------------------------------------------------------===//
197
198SVal nonloc::ConcreteInt::evalBinOp(ValueManager &ValMgr,
199 BinaryOperator::Opcode Op,
200 const nonloc::ConcreteInt& R) const {
201 const llvm::APSInt* X =
202 ValMgr.getBasicValueFactory().EvaluateAPSInt(Op, getValue(), R.getValue());
203
204 if (X)
205 return nonloc::ConcreteInt(*X);
206 else
207 return UndefinedVal();
208}
209
210nonloc::ConcreteInt
211nonloc::ConcreteInt::evalComplement(ValueManager &ValMgr) const {
212 return ValMgr.makeIntVal(~getValue());
213}
214
215nonloc::ConcreteInt nonloc::ConcreteInt::evalMinus(ValueManager &ValMgr) const {
216 return ValMgr.makeIntVal(-getValue());
217}
218
219//===----------------------------------------------------------------------===//
220// Transfer function dispatch for Locs.
221//===----------------------------------------------------------------------===//
222
223SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
224 BinaryOperator::Opcode Op,
225 const loc::ConcreteInt& R) const {
226
227 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
228 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
229
230 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
231
232 if (X)
233 return loc::ConcreteInt(*X);
234 else
235 return UndefinedVal();
236}
237
238//===----------------------------------------------------------------------===//
239// Pretty-Printing.
240//===----------------------------------------------------------------------===//
241
242void SVal::dump() const { dumpToStream(llvm::errs()); }
243
244void SVal::dumpToStream(llvm::raw_ostream& os) const {
245 switch (getBaseKind()) {
246 case UnknownKind:
247 os << "Invalid";
248 break;
249 case NonLocKind:
250 cast<NonLoc>(this)->dumpToStream(os);
251 break;
252 case LocKind:
253 cast<Loc>(this)->dumpToStream(os);
254 break;
255 case UndefinedKind:
256 os << "Undefined";
257 break;
258 default:
259 assert (false && "Invalid SVal.");
260 }
261}
262
263void NonLoc::dumpToStream(llvm::raw_ostream& os) const {
264 switch (getSubKind()) {
265 case nonloc::ConcreteIntKind:
266 os << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
267 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
268 os << 'U';
269 break;
270 case nonloc::SymbolValKind:
271 os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
272 break;
273 case nonloc::SymExprValKind: {
274 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
275 const SymExpr *SE = C.getSymbolicExpression();
276 os << SE;
277 break;
278 }
279 case nonloc::LocAsIntegerKind: {
280 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
281 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
282 break;
283 }
284 case nonloc::CompoundValKind: {
285 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
286 os << "compoundVal{";
287 bool first = true;
288 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
289 if (first) {
290 os << ' '; first = false;
291 }
292 else
293 os << ", ";
294
295 (*I).dumpToStream(os);
296 }
297 os << "}";
298 break;
299 }
300 case nonloc::LazyCompoundValKind: {
301 const nonloc::LazyCompoundVal &C = *cast<nonloc::LazyCompoundVal>(this);
302 os << "lazyCompoundVal{" << (void*) C.getStore() << ',' << C.getRegion()
303 << '}';
304 break;
305 }
306 default:
307 assert (false && "Pretty-printed not implemented for this NonLoc.");
308 break;
309 }
310}
311
312void Loc::dumpToStream(llvm::raw_ostream& os) const {
313 switch (getSubKind()) {
314 case loc::ConcreteIntKind:
315 os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
316 break;
317 case loc::GotoLabelKind:
318 os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
319 break;
320 case loc::MemRegionKind:
321 os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
322 break;
323 default:
324 assert(false && "Pretty-printing not implemented for this Loc.");
325 break;
326 }
327}