blob: a69b611cc3d810c1a26b92609849e9cbcf4b17c5 [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)) {
75 const MemRegion *R = X->getRegion();
76
77 while (R) {
78 // Blast through region views.
79 if (const TypedViewRegion *View = dyn_cast<TypedViewRegion>(R)) {
80 R = View->getSuperRegion();
81 continue;
82 }
83
84 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
85 return SymR->getSymbol();
86
87 break;
88 }
89 }
90
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000091 return 0;
Ted Kremenek94c96982009-03-03 22:06:47 +000092}
93
94/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
Zhongxing Xu369f4472009-04-20 05:24:46 +000095/// Otherwise return 0.
96// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek94c96982009-03-03 22:06:47 +000097SymbolRef SVal::getAsSymbol() const {
98 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
99 return X->getSymbol();
100
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000101 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
102 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
103 return Y;
104
Ted Kremenek94c96982009-03-03 22:06:47 +0000105 return getAsLocSymbol();
106}
107
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000108/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
109/// return that expression. Otherwise return NULL.
110const SymExpr *SVal::getAsSymbolicExpression() const {
111 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
112 return X->getSymbolicExpression();
113
114 return getAsSymbol();
115}
116
117bool 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 Kremenekcf78b6a2008-02-06 22:50:25 +0000161//===----------------------------------------------------------------------===//
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000162// Other Iterators.
163//===----------------------------------------------------------------------===//
164
165nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
166 return getValue()->begin();
167}
168
169nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
170 return getValue()->end();
171}
172
173//===----------------------------------------------------------------------===//
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000174// Useful predicates.
175//===----------------------------------------------------------------------===//
176
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000177bool SVal::isZeroConstant() const {
178 if (isa<loc::ConcreteInt>(*this))
179 return cast<loc::ConcreteInt>(*this).getValue() == 0;
180 else if (isa<nonloc::ConcreteInt>(*this))
181 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000182 else
183 return false;
184}
185
186
187//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000188// Transfer function dispatch for Non-Locs.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000189//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000190
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000191SVal nonloc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000192 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000193 const nonloc::ConcreteInt& R) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000194
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000195 const llvm::APSInt* X =
196 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 nonloc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000200 else
201 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000202}
203
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000204 // Bitwise-Complement.
205
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000206nonloc::ConcreteInt
207nonloc::ConcreteInt::EvalComplement(BasicValueFactory& BasicVals) const {
Ted Kremenek240f1f02008-03-07 20:13:31 +0000208 return BasicVals.getValue(~getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000209}
210
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000211 // Unary Minus.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000212
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000213nonloc::ConcreteInt
214nonloc::ConcreteInt::EvalMinus(BasicValueFactory& BasicVals, UnaryOperator* U) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000215 assert (U->getType() == U->getSubExpr()->getType());
216 assert (U->getType()->isIntegerType());
Ted Kremenek240f1f02008-03-07 20:13:31 +0000217 return BasicVals.getValue(-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
Daniel Dunbar4a77edb2009-03-10 18:00:19 +0000243void SVal::printStdErr() const { print(llvm::errs()); }
Ted Kremenek2a502572008-02-12 21:37:56 +0000244
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000245void SVal::print(llvm::raw_ostream& Out) const {
246
247 switch (getBaseKind()) {
248
249 case UnknownKind:
250 Out << "Invalid"; break;
251
252 case NonLocKind:
253 cast<NonLoc>(this)->print(Out); break;
254
255 case LocKind:
256 cast<Loc>(this)->print(Out); break;
257
258 case UndefinedKind:
259 Out << "Undefined"; break;
260
261 default:
262 assert (false && "Invalid SVal.");
263 }
264}
265
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000266void NonLoc::print(llvm::raw_ostream& Out) const {
267
268 switch (getSubKind()) {
269
270 case nonloc::ConcreteIntKind:
271 Out << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
272
273 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
274 Out << 'U';
275
276 break;
277
278 case nonloc::SymbolValKind:
279 Out << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
280 break;
281
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000282 case nonloc::SymExprValKind: {
283 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
284 const SymExpr *SE = C.getSymbolicExpression();
285 Out << SE;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000286 break;
287 }
288
289 case nonloc::LocAsIntegerKind: {
290 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
291 C.getLoc().print(Out);
292 Out << " [as " << C.getNumBits() << " bit integer]";
293 break;
294 }
295
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000296 case nonloc::CompoundValKind: {
297 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000298 Out << " {";
299 bool first = true;
300 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
301 if (first) { Out << ' '; first = false; }
302 else Out << ", ";
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000303 (*I).print(Out);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000304 }
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000305 Out << " }";
306 break;
307 }
308
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000309 default:
310 assert (false && "Pretty-printed not implemented for this NonLoc.");
311 break;
312 }
313}
314
315void Loc::print(llvm::raw_ostream& Out) const {
316
317 switch (getSubKind()) {
318
319 case loc::ConcreteIntKind:
320 Out << cast<loc::ConcreteInt>(this)->getValue().getZExtValue()
321 << " (Loc)";
322 break;
323
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000324 case loc::GotoLabelKind:
325 Out << "&&"
326 << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
327 break;
328
329 case loc::MemRegionKind:
330 Out << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
331 break;
332
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000333 default:
334 assert (false && "Pretty-printing not implemented for this Loc.");
335 break;
336 }
337}