blob: c597ba459e76af6155ebbdc1398c991460704db8 [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
Zhongxing Xu264e9372009-05-12 10:10:00 +000033bool SVal::hasConjuredSymbol() const {
34 if (const nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(this)) {
35 SymbolRef sym = SV->getSymbol();
36 if (isa<SymbolConjured>(sym))
37 return true;
38 }
39
40 if (const loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(this)) {
41 const MemRegion *R = RV->getRegion();
42 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
43 SymbolRef sym = SR->getSymbol();
44 if (isa<SymbolConjured>(sym))
45 return true;
46 } else if (const CodeTextRegion *CTR = dyn_cast<CodeTextRegion>(R)) {
47 if (CTR->isSymbolic()) {
48 SymbolRef sym = CTR->getSymbol();
49 if (isa<SymbolConjured>(sym))
50 return true;
51 }
52 }
53 }
54
55 return false;
56}
57
Zhongxing Xu369f4472009-04-20 05:24:46 +000058const FunctionDecl* SVal::getAsFunctionDecl() const {
Zhongxing Xu369f4472009-04-20 05:24:46 +000059 if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(this)) {
60 const MemRegion* R = X->getRegion();
Ted Kremenek4abbea62009-04-21 17:37:26 +000061 if (const CodeTextRegion* CTR = R->getAs<CodeTextRegion>()) {
Zhongxing Xu369f4472009-04-20 05:24:46 +000062 if (CTR->isDeclared())
63 return CTR->getDecl();
64 }
65 }
66
67 return 0;
68}
69
Ted Kremenek94c96982009-03-03 22:06:47 +000070/// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
Zhongxing Xu369f4472009-04-20 05:24:46 +000071/// wraps a symbol, return that SymbolRef. Otherwise return 0.
72// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek94c96982009-03-03 22:06:47 +000073SymbolRef SVal::getAsLocSymbol() const {
Ted Kremenek94c96982009-03-03 22:06:47 +000074 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +000075 const MemRegion *R = X->getBaseRegion();
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000076 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
77 return SymR->getSymbol();
Ted Kremenek94c96982009-03-03 22:06:47 +000078 }
Ted Kremenekf7a0cf42009-07-29 21:43:22 +000079 return NULL;
Ted Kremenek94c96982009-03-03 22:06:47 +000080}
81
82/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
Zhongxing Xu369f4472009-04-20 05:24:46 +000083/// Otherwise return 0.
84// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek94c96982009-03-03 22:06:47 +000085SymbolRef SVal::getAsSymbol() const {
86 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
87 return X->getSymbol();
88
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000089 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
90 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
91 return Y;
92
Ted Kremenek94c96982009-03-03 22:06:47 +000093 return getAsLocSymbol();
94}
95
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000096/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
97/// return that expression. Otherwise return NULL.
98const SymExpr *SVal::getAsSymbolicExpression() const {
99 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
100 return X->getSymbolicExpression();
101
102 return getAsSymbol();
103}
104
Zhongxing Xuedb883c2009-06-30 11:52:40 +0000105const MemRegion *SVal::getAsRegion() const {
106 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
107 return X->getRegion();
108
109 return 0;
110}
111
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000112const MemRegion *loc::MemRegionVal::getBaseRegion() const {
113 const MemRegion *R = getRegion();
114 return R ? R->getBaseRegion() : NULL;
115}
116
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000117bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
118 return itr == X.itr;
119}
120
121bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
122 return itr != X.itr;
123}
124
125SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
126 itr.push_back(SE);
127 while (!isa<SymbolData>(itr.back())) expand();
128}
129
130SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
131 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
132 assert(isa<SymbolData>(itr.back()));
133 itr.pop_back();
134 if (!itr.empty())
135 while (!isa<SymbolData>(itr.back())) expand();
136 return *this;
137}
138
139SymbolRef SVal::symbol_iterator::operator*() {
140 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
141 return cast<SymbolData>(itr.back());
142}
143
144void SVal::symbol_iterator::expand() {
145 const SymExpr *SE = itr.back();
146 itr.pop_back();
147
148 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
149 itr.push_back(SIE->getLHS());
150 return;
151 }
152 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
153 itr.push_back(SSE->getLHS());
154 itr.push_back(SSE->getRHS());
155 return;
156 }
157
158 assert(false && "unhandled expansion case");
159}
160
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000161const GRState *nonloc::LazyCompoundVal::getState() const {
162 return static_cast<const LazyCompoundValData*>(Data)->getState();
163}
164
165const TypedRegion *nonloc::LazyCompoundVal::getRegion() const {
166 return static_cast<const LazyCompoundValData*>(Data)->getRegion();
167}
168
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000169//===----------------------------------------------------------------------===//
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000170// Other Iterators.
171//===----------------------------------------------------------------------===//
172
173nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
174 return getValue()->begin();
175}
176
177nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
178 return getValue()->end();
179}
180
181//===----------------------------------------------------------------------===//
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000182// Useful predicates.
183//===----------------------------------------------------------------------===//
184
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000185bool SVal::isZeroConstant() const {
186 if (isa<loc::ConcreteInt>(*this))
187 return cast<loc::ConcreteInt>(*this).getValue() == 0;
188 else if (isa<nonloc::ConcreteInt>(*this))
189 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000190 else
191 return false;
192}
193
194
195//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000196// Transfer function dispatch for Non-Locs.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000197//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000198
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000199SVal nonloc::ConcreteInt::evalBinOp(ValueManager &ValMgr,
200 BinaryOperator::Opcode Op,
201 const nonloc::ConcreteInt& R) const {
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000202 const llvm::APSInt* X =
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000203 ValMgr.getBasicValueFactory().EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000204
205 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000206 return nonloc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000207 else
208 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000209}
210
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000211nonloc::ConcreteInt
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000212nonloc::ConcreteInt::evalComplement(ValueManager &ValMgr) const {
213 return ValMgr.makeIntVal(~getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000214}
215
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000216nonloc::ConcreteInt nonloc::ConcreteInt::evalMinus(ValueManager &ValMgr) const {
217 return ValMgr.makeIntVal(-getValue());
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000218}
219
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000220//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000221// Transfer function dispatch for Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000222//===----------------------------------------------------------------------===//
223
Ted Kremenekccaad9d2008-10-30 17:53:23 +0000224SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
225 BinaryOperator::Opcode Op,
226 const loc::ConcreteInt& R) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000227
228 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
229 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
230
Ted Kremenek240f1f02008-03-07 20:13:31 +0000231 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000232
233 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000234 return loc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000235 else
236 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000237}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000238
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000239//===----------------------------------------------------------------------===//
240// Pretty-Printing.
241//===----------------------------------------------------------------------===//
242
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000243void SVal::dump() const { dumpToStream(llvm::errs()); }
Ted Kremenek2a502572008-02-12 21:37:56 +0000244
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000245void SVal::dumpToStream(llvm::raw_ostream& os) const {
246 switch (getBaseKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000247 case UnknownKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000248 os << "Invalid";
249 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000250 case NonLocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000251 cast<NonLoc>(this)->dumpToStream(os);
252 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000253 case LocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000254 cast<Loc>(this)->dumpToStream(os);
255 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000256 case UndefinedKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000257 os << "Undefined";
258 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000259 default:
260 assert (false && "Invalid SVal.");
261 }
262}
263
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000264void NonLoc::dumpToStream(llvm::raw_ostream& os) const {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000265 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000266 case nonloc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000267 os << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000268 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000269 os << 'U';
270 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000271 case nonloc::SymbolValKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000272 os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
273 break;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000274 case nonloc::SymExprValKind: {
275 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
276 const SymExpr *SE = C.getSymbolicExpression();
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000277 os << SE;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000278 break;
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000279 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000280 case nonloc::LocAsIntegerKind: {
281 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000282 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000283 break;
284 }
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000285 case nonloc::CompoundValKind: {
286 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenek7b679522009-07-14 20:21:36 +0000287 os << "compoundVal{";
Ted Kremenekb8b41612008-10-30 18:35:10 +0000288 bool first = true;
289 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000290 if (first) {
291 os << ' '; first = false;
292 }
293 else
294 os << ", ";
295
296 (*I).dumpToStream(os);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000297 }
Ted Kremenek7b679522009-07-14 20:21:36 +0000298 os << "}";
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000299 break;
Ted Kremeneka5e81f12009-08-06 01:20:57 +0000300 }
301 case nonloc::LazyCompoundValKind: {
302 const nonloc::LazyCompoundVal &C = *cast<nonloc::LazyCompoundVal>(this);
303 os << "lazyCompoundVal{" << (void*) C.getState() << ',' << C.getRegion()
304 << '}';
305 break;
306 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000307 default:
308 assert (false && "Pretty-printed not implemented for this NonLoc.");
309 break;
310 }
311}
312
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000313void Loc::dumpToStream(llvm::raw_ostream& os) const {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000314 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000315 case loc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000316 os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
317 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000318 case loc::GotoLabelKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000319 os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000320 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000321 case loc::MemRegionKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000322 os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
323 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000324 default:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000325 assert(false && "Pretty-printing not implemented for this Loc.");
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000326 break;
327 }
328}