blob: 9163b2725273ae2b6567b50f04606d1a8a5aae76 [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 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
73/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
Zhongxing Xu369f4472009-04-20 05:24:46 +000074/// Otherwise return 0.
75// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek94c96982009-03-03 22:06:47 +000076SymbolRef SVal::getAsSymbol() const {
77 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
78 return X->getSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +000079
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000080 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
81 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
82 return Y;
Mike Stump1eb44332009-09-09 15:08:12 +000083
Ted Kremenek94c96982009-03-03 22:06:47 +000084 return getAsLocSymbol();
85}
86
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000087/// 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();
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000093 return getAsSymbol();
94}
95
Zhongxing Xuedb883c2009-06-30 11:52:40 +000096const MemRegion *SVal::getAsRegion() const {
97 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
98 return X->getRegion();
99
100 return 0;
101}
102
Zhongxing Xu479529e2009-11-10 02:17:20 +0000103const MemRegion *loc::MemRegionVal::StripCasts() const {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000104 const MemRegion *R = getRegion();
Zhongxing Xu479529e2009-11-10 02:17:20 +0000105 return R ? R->StripCasts() : NULL;
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000106}
107
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000108bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
109 return itr == X.itr;
110}
111
112bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
113 return itr != X.itr;
114}
115
116SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
117 itr.push_back(SE);
Mike Stump1eb44332009-09-09 15:08:12 +0000118 while (!isa<SymbolData>(itr.back())) expand();
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000119}
120
121SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
122 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
123 assert(isa<SymbolData>(itr.back()));
Mike Stump1eb44332009-09-09 15:08:12 +0000124 itr.pop_back();
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000125 if (!itr.empty())
126 while (!isa<SymbolData>(itr.back())) expand();
127 return *this;
128}
129
130SymbolRef SVal::symbol_iterator::operator*() {
131 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
132 return cast<SymbolData>(itr.back());
133}
134
135void SVal::symbol_iterator::expand() {
136 const SymExpr *SE = itr.back();
137 itr.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000139 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
140 itr.push_back(SIE->getLHS());
141 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000142 }
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000143 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
144 itr.push_back(SSE->getLHS());
145 itr.push_back(SSE->getRHS());
146 return;
147 }
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000149 assert(false && "unhandled expansion case");
150}
151
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000152const GRState *nonloc::LazyCompoundVal::getState() const {
153 return static_cast<const LazyCompoundValData*>(Data)->getState();
154}
155
156const TypedRegion *nonloc::LazyCompoundVal::getRegion() const {
157 return static_cast<const LazyCompoundValData*>(Data)->getRegion();
158}
159
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000160//===----------------------------------------------------------------------===//
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000161// Other Iterators.
162//===----------------------------------------------------------------------===//
163
164nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
165 return getValue()->begin();
166}
167
168nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
169 return getValue()->end();
170}
171
172//===----------------------------------------------------------------------===//
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000173// Useful predicates.
174//===----------------------------------------------------------------------===//
175
Zhongxing Xub10a7c22009-11-09 06:52:44 +0000176bool SVal::isConstant() const {
177 return isa<nonloc::ConcreteInt>(this) || isa<loc::ConcreteInt>(this);
178}
179
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000180bool SVal::isZeroConstant() const {
181 if (isa<loc::ConcreteInt>(*this))
182 return cast<loc::ConcreteInt>(*this).getValue() == 0;
183 else if (isa<nonloc::ConcreteInt>(*this))
184 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000185 else
186 return false;
187}
188
189
190//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000191// Transfer function dispatch for Non-Locs.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000192//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000193
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000194SVal nonloc::ConcreteInt::evalBinOp(ValueManager &ValMgr,
195 BinaryOperator::Opcode Op,
Mike Stump1eb44332009-09-09 15:08:12 +0000196 const nonloc::ConcreteInt& R) const {
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000197 const llvm::APSInt* X =
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000198 ValMgr.getBasicValueFactory().EvaluateAPSInt(Op, getValue(), R.getValue());
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000200 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000201 return nonloc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000202 else
203 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000204}
205
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000206nonloc::ConcreteInt
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000207nonloc::ConcreteInt::evalComplement(ValueManager &ValMgr) const {
208 return ValMgr.makeIntVal(~getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000209}
210
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000211nonloc::ConcreteInt nonloc::ConcreteInt::evalMinus(ValueManager &ValMgr) const {
212 return ValMgr.makeIntVal(-getValue());
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000213}
214
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000215//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000216// Transfer function dispatch for Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000217//===----------------------------------------------------------------------===//
218
Ted Kremenekccaad9d2008-10-30 17:53:23 +0000219SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
220 BinaryOperator::Opcode Op,
221 const loc::ConcreteInt& R) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000223 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
224 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Ted Kremenek240f1f02008-03-07 20:13:31 +0000226 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000228 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000229 return loc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000230 else
231 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000232}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000233
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000234//===----------------------------------------------------------------------===//
235// Pretty-Printing.
236//===----------------------------------------------------------------------===//
237
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000238void SVal::dump() const { dumpToStream(llvm::errs()); }
Ted Kremenek2a502572008-02-12 21:37:56 +0000239
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000240void SVal::dumpToStream(llvm::raw_ostream& os) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000241 switch (getBaseKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000242 case UnknownKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000243 os << "Invalid";
Mike Stump1eb44332009-09-09 15:08:12 +0000244 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000245 case NonLocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000246 cast<NonLoc>(this)->dumpToStream(os);
Mike Stump1eb44332009-09-09 15:08:12 +0000247 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000248 case LocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000249 cast<Loc>(this)->dumpToStream(os);
Mike Stump1eb44332009-09-09 15:08:12 +0000250 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000251 case UndefinedKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000252 os << "Undefined";
Mike Stump1eb44332009-09-09 15:08:12 +0000253 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000254 default:
255 assert (false && "Invalid SVal.");
256 }
257}
258
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000259void NonLoc::dumpToStream(llvm::raw_ostream& os) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000260 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000261 case nonloc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000262 os << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000263 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000264 os << 'U';
265 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000266 case nonloc::SymbolValKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000267 os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
Mike Stump1eb44332009-09-09 15:08:12 +0000268 break;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000269 case nonloc::SymExprValKind: {
270 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
271 const SymExpr *SE = C.getSymbolicExpression();
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000272 os << SE;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000273 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000274 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000275 case nonloc::LocAsIntegerKind: {
276 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000277 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000278 break;
279 }
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000280 case nonloc::CompoundValKind: {
281 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenek7b679522009-07-14 20:21:36 +0000282 os << "compoundVal{";
Ted Kremenekb8b41612008-10-30 18:35:10 +0000283 bool first = true;
284 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000285 if (first) {
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000286 os << ' '; first = false;
287 }
288 else
289 os << ", ";
290
291 (*I).dumpToStream(os);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000292 }
Ted Kremenek7b679522009-07-14 20:21:36 +0000293 os << "}";
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000294 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000295 }
296 case nonloc::LazyCompoundValKind: {
297 const nonloc::LazyCompoundVal &C = *cast<nonloc::LazyCompoundVal>(this);
298 os << "lazyCompoundVal{" << (void*) C.getState() << ',' << C.getRegion()
299 << '}';
300 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000301 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000302 default:
303 assert (false && "Pretty-printed not implemented for this NonLoc.");
304 break;
305 }
306}
307
Mike Stump1eb44332009-09-09 15:08:12 +0000308void Loc::dumpToStream(llvm::raw_ostream& os) const {
309 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000310 case loc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000311 os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
Mike Stump1eb44332009-09-09 15:08:12 +0000312 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000313 case loc::GotoLabelKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000314 os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000315 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000316 case loc::MemRegionKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000317 os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
Mike Stump1eb44332009-09-09 15:08:12 +0000318 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000319 default:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000320 assert(false && "Pretty-printing not implemented for this Loc.");
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000321 break;
322 }
323}