blob: b642857c50273fbeb7f50054d9774e7af8a6cb0d [file] [log] [blame]
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001//= 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//
Zhongxing Xu1c96b242008-10-17 05:57:07 +000010// This file defines SVal, Loc, and NonLoc, classes that represent
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000011// abstract r-values for use with path-sensitive value tracking.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek9e240492008-10-04 05:50:14 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000016#include "clang/Basic/IdentifierTable.h"
Ted Kremenekd70d0b02008-02-16 01:12:31 +000017#include "llvm/Support/Streams.h"
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000018
19using namespace clang;
20using llvm::dyn_cast;
21using llvm::cast;
22using llvm::APSInt;
23
24//===----------------------------------------------------------------------===//
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000025// Symbol iteration within an SVal.
Ted Kremenek90e14812008-02-14 23:25:54 +000026//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +000027
Ted Kremenek718c4f72008-04-29 22:17:41 +000028
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000029//===----------------------------------------------------------------------===//
30// Utility methods.
31//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +000032
Ted Kremenek94c96982009-03-03 22:06:47 +000033/// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
34/// wraps a symbol, return that SymbolRef. Otherwise return a SymbolRef
35/// where 'isValid()' returns false.
36SymbolRef SVal::getAsLocSymbol() const {
37 if (const loc::SymbolVal *X = dyn_cast<loc::SymbolVal>(this))
38 return X->getSymbol();
39
40 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
41 const MemRegion *R = X->getRegion();
42
43 while (R) {
44 // Blast through region views.
45 if (const TypedViewRegion *View = dyn_cast<TypedViewRegion>(R)) {
46 R = View->getSuperRegion();
47 continue;
48 }
49
50 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
51 return SymR->getSymbol();
52
53 break;
54 }
55 }
56
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000057 return 0;
Ted Kremenek94c96982009-03-03 22:06:47 +000058}
59
60/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
61/// Otherwise return a SymbolRef where 'isValid()' returns false.
62SymbolRef SVal::getAsSymbol() const {
63 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
64 return X->getSymbol();
65
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000066 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
67 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
68 return Y;
69
Ted Kremenek94c96982009-03-03 22:06:47 +000070 return getAsLocSymbol();
71}
72
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000073/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
74/// return that expression. Otherwise return NULL.
75const SymExpr *SVal::getAsSymbolicExpression() const {
76 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
77 return X->getSymbolicExpression();
78
79 return getAsSymbol();
80}
81
82bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
83 return itr == X.itr;
84}
85
86bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
87 return itr != X.itr;
88}
89
90SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
91 itr.push_back(SE);
92 while (!isa<SymbolData>(itr.back())) expand();
93}
94
95SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
96 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
97 assert(isa<SymbolData>(itr.back()));
98 itr.pop_back();
99 if (!itr.empty())
100 while (!isa<SymbolData>(itr.back())) expand();
101 return *this;
102}
103
104SymbolRef SVal::symbol_iterator::operator*() {
105 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
106 return cast<SymbolData>(itr.back());
107}
108
109void SVal::symbol_iterator::expand() {
110 const SymExpr *SE = itr.back();
111 itr.pop_back();
112
113 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
114 itr.push_back(SIE->getLHS());
115 return;
116 }
117 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
118 itr.push_back(SSE->getLHS());
119 itr.push_back(SSE->getRHS());
120 return;
121 }
122
123 assert(false && "unhandled expansion case");
124}
125
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000126//===----------------------------------------------------------------------===//
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000127// Other Iterators.
128//===----------------------------------------------------------------------===//
129
130nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
131 return getValue()->begin();
132}
133
134nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
135 return getValue()->end();
136}
137
138//===----------------------------------------------------------------------===//
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000139// Useful predicates.
140//===----------------------------------------------------------------------===//
141
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000142bool SVal::isZeroConstant() const {
143 if (isa<loc::ConcreteInt>(*this))
144 return cast<loc::ConcreteInt>(*this).getValue() == 0;
145 else if (isa<nonloc::ConcreteInt>(*this))
146 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000147 else
148 return false;
149}
150
151
152//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000153// Transfer function dispatch for Non-Locs.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000154//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000155
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000156SVal nonloc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000157 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000158 const nonloc::ConcreteInt& R) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000159
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000160 const llvm::APSInt* X =
161 BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000162
163 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000164 return nonloc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000165 else
166 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000167}
168
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000169 // Bitwise-Complement.
170
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000171nonloc::ConcreteInt
172nonloc::ConcreteInt::EvalComplement(BasicValueFactory& BasicVals) const {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000173 return BasicVals.getValue(~getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000174}
175
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000176 // Unary Minus.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000177
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000178nonloc::ConcreteInt
179nonloc::ConcreteInt::EvalMinus(BasicValueFactory& BasicVals, UnaryOperator* U) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000180 assert (U->getType() == U->getSubExpr()->getType());
181 assert (U->getType()->isIntegerType());
Ted Kremenek240f1f02008-03-07 20:13:31 +0000182 return BasicVals.getValue(-getValue());
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000183}
184
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000185//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000186// Transfer function dispatch for Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000187//===----------------------------------------------------------------------===//
188
Ted Kremenekccaad9d2008-10-30 17:53:23 +0000189SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
190 BinaryOperator::Opcode Op,
191 const loc::ConcreteInt& R) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000192
193 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
194 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
195
Ted Kremenek240f1f02008-03-07 20:13:31 +0000196 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000197
198 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000199 return loc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000200 else
201 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000202}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000203
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000204//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000205// Utility methods for constructing Non-Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000206//===----------------------------------------------------------------------===//
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000207
208NonLoc NonLoc::MakeVal(SymbolRef sym) {
209 return nonloc::SymbolVal(sym);
210}
211
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000212NonLoc NonLoc::MakeVal(SymbolManager& SymMgr, const SymExpr *lhs,
213 BinaryOperator::Opcode op, const APSInt& v, QualType T) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000214 // The Environment ensures we always get a persistent APSInt in
215 // BasicValueFactory, so we don't need to get the APSInt from
216 // BasicValueFactory again.
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000217 assert(!Loc::IsLocType(T));
218 return nonloc::SymExprVal(SymMgr.getSymIntExpr(lhs, op, v, T));
Zhongxing Xua129eb92009-03-25 05:58:37 +0000219}
220
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000221NonLoc NonLoc::MakeVal(SymbolManager& SymMgr, const SymExpr *lhs,
222 BinaryOperator::Opcode op, const SymExpr *rhs,
223QualType T) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000224 assert(SymMgr.getType(lhs) == SymMgr.getType(rhs));
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000225 assert(!Loc::IsLocType(T));
226 return nonloc::SymExprVal(SymMgr.getSymSymExpr(lhs, op, rhs, T));
Zhongxing Xua129eb92009-03-25 05:58:37 +0000227}
228
Ted Kremenek14553ab2009-01-30 00:08:43 +0000229NonLoc NonLoc::MakeIntVal(BasicValueFactory& BasicVals, uint64_t X,
230 bool isUnsigned) {
231 return nonloc::ConcreteInt(BasicVals.getIntValue(X, isUnsigned));
Zhongxing Xu6613d082008-11-24 02:18:56 +0000232}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000233
Zhongxing Xu8b862732008-11-24 09:38:21 +0000234NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, uint64_t X,
235 unsigned BitWidth, bool isUnsigned) {
236 return nonloc::ConcreteInt(BasicVals.getValue(X, BitWidth, isUnsigned));
237}
238
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000239NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, uint64_t X, QualType T) {
240 return nonloc::ConcreteInt(BasicVals.getValue(X, T));
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000241}
242
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000243NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, IntegerLiteral* I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000244
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000245 return nonloc::ConcreteInt(BasicVals.getValue(APSInt(I->getValue(),
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000246 I->getType()->isUnsignedIntegerType())));
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000247}
248
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000249NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, const llvm::APInt& I,
250 bool isUnsigned) {
251 return nonloc::ConcreteInt(BasicVals.getValue(I, isUnsigned));
252}
253
Zhongxing Xu8b862732008-11-24 09:38:21 +0000254NonLoc NonLoc::MakeVal(BasicValueFactory& BasicVals, const llvm::APSInt& I) {
255 return nonloc::ConcreteInt(BasicVals.getValue(I));
256}
257
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000258NonLoc NonLoc::MakeIntTruthVal(BasicValueFactory& BasicVals, bool b) {
259 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000260}
261
Ted Kremenek632e8b82008-10-30 17:44:46 +0000262NonLoc NonLoc::MakeCompoundVal(QualType T, llvm::ImmutableList<SVal> Vals,
Zhongxing Xu6764b722008-10-30 04:58:00 +0000263 BasicValueFactory& BasicVals) {
Ted Kremenek632e8b82008-10-30 17:44:46 +0000264 return nonloc::CompoundVal(BasicVals.getCompoundValData(T, Vals));
Zhongxing Xu6764b722008-10-30 04:58:00 +0000265}
266
Zhongxing Xua1718c72009-04-03 07:33:13 +0000267SVal SVal::GetRValueSymbolVal(SymbolManager& SymMgr, MemRegionManager& MRMgr,
268 const MemRegion* R) {
Ted Kremenek9ab6b9c2009-01-22 18:23:34 +0000269 SymbolRef sym = SymMgr.getRegionRValueSymbol(R);
270
Ted Kremenekec099f12009-03-18 22:10:22 +0000271 if (const TypedRegion* TR = dyn_cast<TypedRegion>(R)) {
272 QualType T = TR->getRValueType(SymMgr.getContext());
273
274 if (Loc::IsLocType(T))
Zhongxing Xua1718c72009-04-03 07:33:13 +0000275 return Loc::MakeVal(MRMgr.getSymbolicRegion(sym));
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000276
Ted Kremenekec099f12009-03-18 22:10:22 +0000277 // Only handle integers for now.
278 if (T->isIntegerType())
279 return NonLoc::MakeVal(sym);
280 }
281
282 return UnknownVal();
Zhongxing Xueabf7762008-11-19 11:03:17 +0000283}
284
Ted Kremenekbb9b2712009-03-20 20:10:45 +0000285SVal SVal::GetConjuredSymbolVal(SymbolManager &SymMgr, const Expr* E,
286 unsigned Count) {
287
288 QualType T = E->getType();
289
290 if (Loc::IsLocType(T)) {
291 SymbolRef Sym = SymMgr.getConjuredSymbol(E, Count);
292 return loc::SymbolVal(Sym);
293 }
294 else if (T->isIntegerType() && T->isScalarType()) {
295 SymbolRef Sym = SymMgr.getConjuredSymbol(E, Count);
296 return nonloc::SymbolVal(Sym);
297 }
298
299 return UnknownVal();
300}
301
Ted Kremenek632e8b82008-10-30 17:44:46 +0000302nonloc::LocAsInteger nonloc::LocAsInteger::Make(BasicValueFactory& Vals, Loc V,
303 unsigned Bits) {
304 return LocAsInteger(Vals.getPersistentSValWithData(V, Bits));
305}
306
Ted Kremenek2a502572008-02-12 21:37:56 +0000307//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000308// Utility methods for constructing Locs.
Ted Kremenek2a502572008-02-12 21:37:56 +0000309//===----------------------------------------------------------------------===//
310
Zhongxing Xu2fdf5552008-12-09 10:51:19 +0000311Loc Loc::MakeVal(const MemRegion* R) { return loc::MemRegionVal(R); }
312
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000313Loc Loc::MakeVal(AddrLabelExpr* E) { return loc::GotoLabel(E->getLabel()); }
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000314
Zhongxing Xu4193eca2008-12-20 06:32:12 +0000315Loc Loc::MakeVal(SymbolRef sym) { return loc::SymbolVal(sym); }
316
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000317//===----------------------------------------------------------------------===//
318// Pretty-Printing.
319//===----------------------------------------------------------------------===//
320
Daniel Dunbar4a77edb2009-03-10 18:00:19 +0000321void SVal::printStdErr() const { print(llvm::errs()); }
Ted Kremenek2a502572008-02-12 21:37:56 +0000322
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000323void SVal::print(std::ostream& Out) const {
Ted Kremenekb8b41612008-10-30 18:35:10 +0000324 llvm::raw_os_ostream out(Out);
325 print(out);
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000326}
327
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000328void SVal::print(llvm::raw_ostream& Out) const {
329
330 switch (getBaseKind()) {
331
332 case UnknownKind:
333 Out << "Invalid"; break;
334
335 case NonLocKind:
336 cast<NonLoc>(this)->print(Out); break;
337
338 case LocKind:
339 cast<Loc>(this)->print(Out); break;
340
341 case UndefinedKind:
342 Out << "Undefined"; break;
343
344 default:
345 assert (false && "Invalid SVal.");
346 }
347}
348
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000349void NonLoc::print(llvm::raw_ostream& Out) const {
350
351 switch (getSubKind()) {
352
353 case nonloc::ConcreteIntKind:
354 Out << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
355
356 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
357 Out << 'U';
358
359 break;
360
361 case nonloc::SymbolValKind:
362 Out << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
363 break;
364
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000365 case nonloc::SymExprValKind: {
366 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
367 const SymExpr *SE = C.getSymbolicExpression();
368 Out << SE;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000369 break;
370 }
371
372 case nonloc::LocAsIntegerKind: {
373 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
374 C.getLoc().print(Out);
375 Out << " [as " << C.getNumBits() << " bit integer]";
376 break;
377 }
378
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000379 case nonloc::CompoundValKind: {
380 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000381 Out << " {";
382 bool first = true;
383 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
384 if (first) { Out << ' '; first = false; }
385 else Out << ", ";
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000386 (*I).print(Out);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000387 }
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000388 Out << " }";
389 break;
390 }
391
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000392 default:
393 assert (false && "Pretty-printed not implemented for this NonLoc.");
394 break;
395 }
396}
397
398void Loc::print(llvm::raw_ostream& Out) const {
399
400 switch (getSubKind()) {
401
402 case loc::ConcreteIntKind:
403 Out << cast<loc::ConcreteInt>(this)->getValue().getZExtValue()
404 << " (Loc)";
405 break;
406
407 case loc::SymbolValKind:
408 Out << '$' << cast<loc::SymbolVal>(this)->getSymbol();
409 break;
410
411 case loc::GotoLabelKind:
412 Out << "&&"
413 << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
414 break;
415
416 case loc::MemRegionKind:
417 Out << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
418 break;
419
420 case loc::FuncValKind:
421 Out << "function "
422 << cast<loc::FuncVal>(this)->getDecl()->getIdentifier()->getName();
423 break;
424
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000425 default:
426 assert (false && "Pretty-printing not implemented for this Loc.");
427 break;
428 }
429}