blob: d756be70dc25e1037f15f315a0e296788f8d9ad1 [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 Kremenek1309f9a2010-01-25 04:41:41 +000015#include "clang/Checker/PathSensitive/GRState.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000016#include "clang/Basic/IdentifierTable.h"
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000017
18using namespace clang;
19using llvm::dyn_cast;
20using llvm::cast;
21using llvm::APSInt;
22
23//===----------------------------------------------------------------------===//
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000024// Symbol iteration within an SVal.
Ted Kremenek90e14812008-02-14 23:25:54 +000025//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +000026
Ted Kremenek718c4f72008-04-29 22:17:41 +000027
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000028//===----------------------------------------------------------------------===//
29// Utility methods.
30//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +000031
Zhongxing Xu264e9372009-05-12 10:10:00 +000032bool 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;
Zhongxing Xu264e9372009-05-12 10:10:00 +000045 }
46 }
47
48 return false;
49}
50
Ted Kremenekabd46e12009-08-28 04:49:15 +000051const FunctionDecl *SVal::getAsFunctionDecl() const {
Zhongxing Xu369f4472009-04-20 05:24:46 +000052 if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(this)) {
53 const MemRegion* R = X->getRegion();
Ted Kremenekeb1c7a02009-11-25 01:32:22 +000054 if (const FunctionTextRegion *CTR = R->getAs<FunctionTextRegion>())
Ted Kremenekabd46e12009-08-28 04:49:15 +000055 return CTR->getDecl();
Zhongxing Xu369f4472009-04-20 05:24:46 +000056 }
57
Ted Kremenekabd46e12009-08-28 04:49:15 +000058 return NULL;
Zhongxing Xu369f4472009-04-20 05:24:46 +000059}
60
Mike Stump1eb44332009-09-09 15:08:12 +000061/// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
Zhongxing Xu369f4472009-04-20 05:24:46 +000062/// wraps a symbol, return that SymbolRef. Otherwise return 0.
63// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek94c96982009-03-03 22:06:47 +000064SymbolRef SVal::getAsLocSymbol() const {
Ted Kremenek94c96982009-03-03 22:06:47 +000065 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
Zhongxing Xu479529e2009-11-10 02:17:20 +000066 const MemRegion *R = X->StripCasts();
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000067 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
68 return SymR->getSymbol();
Ted Kremenek94c96982009-03-03 22:06:47 +000069 }
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000070 return NULL;
Ted Kremenek94c96982009-03-03 22:06:47 +000071}
72
Zhongxing Xuc8023782010-03-10 04:58:55 +000073/// Get the symbol in the SVal or its base region.
74SymbolRef SVal::getLocSymbolInBase() const {
75 const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this);
76
77 if (!X)
78 return 0;
79
80 const MemRegion *R = X->getRegion();
81
82 while (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
83 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SR))
84 return SymR->getSymbol();
85 else
86 R = SR->getSuperRegion();
87 }
88
89 return 0;
90}
91
Ted Kremenek94c96982009-03-03 22:06:47 +000092/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
Zhongxing Xu369f4472009-04-20 05:24:46 +000093/// Otherwise return 0.
94// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek94c96982009-03-03 22:06:47 +000095SymbolRef SVal::getAsSymbol() const {
96 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
97 return X->getSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000099 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
100 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
101 return Y;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek94c96982009-03-03 22:06:47 +0000103 return getAsLocSymbol();
104}
105
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000106/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
107/// return that expression. Otherwise return NULL.
108const SymExpr *SVal::getAsSymbolicExpression() const {
109 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
110 return X->getSymbolicExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000112 return getAsSymbol();
113}
114
Zhongxing Xuedb883c2009-06-30 11:52:40 +0000115const MemRegion *SVal::getAsRegion() const {
116 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
117 return X->getRegion();
118
Zhongxing Xu604848a2010-01-11 06:52:53 +0000119 if (const nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(this)) {
120 return X->getLoc().getAsRegion();
121 }
122
Zhongxing Xuedb883c2009-06-30 11:52:40 +0000123 return 0;
124}
125
Zhongxing Xu479529e2009-11-10 02:17:20 +0000126const MemRegion *loc::MemRegionVal::StripCasts() const {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000127 const MemRegion *R = getRegion();
Zhongxing Xu479529e2009-11-10 02:17:20 +0000128 return R ? R->StripCasts() : NULL;
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000129}
130
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000131bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
132 return itr == X.itr;
133}
134
135bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
136 return itr != X.itr;
137}
138
139SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
140 itr.push_back(SE);
Mike Stump1eb44332009-09-09 15:08:12 +0000141 while (!isa<SymbolData>(itr.back())) expand();
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000142}
143
144SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
145 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
146 assert(isa<SymbolData>(itr.back()));
Mike Stump1eb44332009-09-09 15:08:12 +0000147 itr.pop_back();
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000148 if (!itr.empty())
149 while (!isa<SymbolData>(itr.back())) expand();
150 return *this;
151}
152
153SymbolRef SVal::symbol_iterator::operator*() {
154 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
155 return cast<SymbolData>(itr.back());
156}
157
158void SVal::symbol_iterator::expand() {
159 const SymExpr *SE = itr.back();
160 itr.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000162 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
163 itr.push_back(SIE->getLHS());
164 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000165 }
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000166 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
167 itr.push_back(SSE->getLHS());
168 itr.push_back(SSE->getRHS());
169 return;
170 }
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000172 assert(false && "unhandled expansion case");
173}
174
Zhongxing Xubfcaf802010-02-05 02:26:30 +0000175const void *nonloc::LazyCompoundVal::getStore() const {
176 return static_cast<const LazyCompoundValData*>(Data)->getStore();
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000177}
178
179const TypedRegion *nonloc::LazyCompoundVal::getRegion() const {
180 return static_cast<const LazyCompoundValData*>(Data)->getRegion();
181}
182
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000183//===----------------------------------------------------------------------===//
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000184// Other Iterators.
185//===----------------------------------------------------------------------===//
186
187nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
188 return getValue()->begin();
189}
190
191nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
192 return getValue()->end();
193}
194
195//===----------------------------------------------------------------------===//
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000196// Useful predicates.
197//===----------------------------------------------------------------------===//
198
Zhongxing Xub10a7c22009-11-09 06:52:44 +0000199bool SVal::isConstant() const {
200 return isa<nonloc::ConcreteInt>(this) || isa<loc::ConcreteInt>(this);
201}
202
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000203bool SVal::isZeroConstant() const {
204 if (isa<loc::ConcreteInt>(*this))
205 return cast<loc::ConcreteInt>(*this).getValue() == 0;
206 else if (isa<nonloc::ConcreteInt>(*this))
207 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000208 else
209 return false;
210}
211
212
213//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000214// Transfer function dispatch for Non-Locs.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000215//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000216
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000217SVal nonloc::ConcreteInt::evalBinOp(ValueManager &ValMgr,
218 BinaryOperator::Opcode Op,
Mike Stump1eb44332009-09-09 15:08:12 +0000219 const nonloc::ConcreteInt& R) const {
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000220 const llvm::APSInt* X =
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000221 ValMgr.getBasicValueFactory().EvaluateAPSInt(Op, getValue(), R.getValue());
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000223 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000224 return nonloc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000225 else
226 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000227}
228
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000229nonloc::ConcreteInt
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000230nonloc::ConcreteInt::evalComplement(ValueManager &ValMgr) const {
231 return ValMgr.makeIntVal(~getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000232}
233
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000234nonloc::ConcreteInt nonloc::ConcreteInt::evalMinus(ValueManager &ValMgr) const {
235 return ValMgr.makeIntVal(-getValue());
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000236}
237
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000238//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000239// Transfer function dispatch for Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000240//===----------------------------------------------------------------------===//
241
Ted Kremenekccaad9d2008-10-30 17:53:23 +0000242SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
243 BinaryOperator::Opcode Op,
244 const loc::ConcreteInt& R) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000246 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
247 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Ted Kremenek240f1f02008-03-07 20:13:31 +0000249 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Mike Stump1eb44332009-09-09 15:08:12 +0000250
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000251 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000252 return loc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000253 else
254 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000255}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000256
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000257//===----------------------------------------------------------------------===//
258// Pretty-Printing.
259//===----------------------------------------------------------------------===//
260
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000261void SVal::dump() const { dumpToStream(llvm::errs()); }
Ted Kremenek2a502572008-02-12 21:37:56 +0000262
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000263void SVal::dumpToStream(llvm::raw_ostream& os) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000264 switch (getBaseKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000265 case UnknownKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000266 os << "Invalid";
Mike Stump1eb44332009-09-09 15:08:12 +0000267 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000268 case NonLocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000269 cast<NonLoc>(this)->dumpToStream(os);
Mike Stump1eb44332009-09-09 15:08:12 +0000270 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000271 case LocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000272 cast<Loc>(this)->dumpToStream(os);
Mike Stump1eb44332009-09-09 15:08:12 +0000273 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000274 case UndefinedKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000275 os << "Undefined";
Mike Stump1eb44332009-09-09 15:08:12 +0000276 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000277 default:
278 assert (false && "Invalid SVal.");
279 }
280}
281
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000282void NonLoc::dumpToStream(llvm::raw_ostream& os) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000283 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000284 case nonloc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000285 os << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000286 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000287 os << 'U';
288 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000289 case nonloc::SymbolValKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000290 os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000291 break;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000292 case nonloc::SymExprValKind: {
293 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
294 const SymExpr *SE = C.getSymbolicExpression();
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000295 os << SE;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000296 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000297 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000298 case nonloc::LocAsIntegerKind: {
299 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000300 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000301 break;
302 }
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000303 case nonloc::CompoundValKind: {
304 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenek7b679522009-07-14 20:21:36 +0000305 os << "compoundVal{";
Ted Kremenekb8b41612008-10-30 18:35:10 +0000306 bool first = true;
307 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000308 if (first) {
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000309 os << ' '; first = false;
310 }
311 else
312 os << ", ";
313
314 (*I).dumpToStream(os);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000315 }
Ted Kremenek7b679522009-07-14 20:21:36 +0000316 os << "}";
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000317 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000318 }
319 case nonloc::LazyCompoundValKind: {
320 const nonloc::LazyCompoundVal &C = *cast<nonloc::LazyCompoundVal>(this);
Dan Gohmancb421fa2010-04-19 16:39:44 +0000321 os << "lazyCompoundVal{" << const_cast<void *>(C.getStore())
322 << ',' << C.getRegion()
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000323 << '}';
324 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000325 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000326 default:
327 assert (false && "Pretty-printed not implemented for this NonLoc.");
328 break;
329 }
330}
331
Mike Stump1eb44332009-09-09 15:08:12 +0000332void Loc::dumpToStream(llvm::raw_ostream& os) const {
333 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000334 case loc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000335 os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
Mike Stump1eb44332009-09-09 15:08:12 +0000336 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000337 case loc::GotoLabelKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000338 os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000339 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000340 case loc::MemRegionKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000341 os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
Mike Stump1eb44332009-09-09 15:08:12 +0000342 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000343 default:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000344 assert(false && "Pretty-printing not implemented for this Loc.");
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000345 break;
346 }
347}