blob: b2f2e966444e5f96a9069a8d0cb3d6ac94b5b446 [file] [log] [blame]
Ted Kremenek72197902008-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 Xu097fc982008-10-17 05:57:07 +000010// This file defines SVal, Loc, and NonLoc, classes that represent
Ted Kremenek72197902008-01-31 19:34:24 +000011// abstract r-values for use with path-sensitive value tracking.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb15eba42008-10-04 05:50:14 +000015#include "clang/Analysis/PathSensitive/GRState.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000016#include "clang/Basic/IdentifierTable.h"
Ted Kremenek72197902008-01-31 19:34:24 +000017
18using namespace clang;
19using llvm::dyn_cast;
20using llvm::cast;
21using llvm::APSInt;
22
23//===----------------------------------------------------------------------===//
Ted Kremenek74556a12009-03-26 03:35:11 +000024// Symbol iteration within an SVal.
Ted Kremenek0e39dcf2008-02-14 23:25:54 +000025//===----------------------------------------------------------------------===//
Ted Kremenek6e24a802008-02-01 06:36:40 +000026
Ted Kremenek465f25a2008-04-29 22:17:41 +000027
Ted Kremenek74556a12009-03-26 03:35:11 +000028//===----------------------------------------------------------------------===//
29// Utility methods.
30//===----------------------------------------------------------------------===//
Ted Kremenek6e24a802008-02-01 06:36:40 +000031
Zhongxing Xu35edd9a2009-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;
45 } else if (const CodeTextRegion *CTR = dyn_cast<CodeTextRegion>(R)) {
46 if (CTR->isSymbolic()) {
47 SymbolRef sym = CTR->getSymbol();
48 if (isa<SymbolConjured>(sym))
49 return true;
50 }
51 }
52 }
53
54 return false;
55}
56
Zhongxing Xucac107a2009-04-20 05:24:46 +000057const FunctionDecl* SVal::getAsFunctionDecl() const {
Zhongxing Xucac107a2009-04-20 05:24:46 +000058 if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(this)) {
59 const MemRegion* R = X->getRegion();
Ted Kremenekba7be1a2009-04-21 17:37:26 +000060 if (const CodeTextRegion* CTR = R->getAs<CodeTextRegion>()) {
Zhongxing Xucac107a2009-04-20 05:24:46 +000061 if (CTR->isDeclared())
62 return CTR->getDecl();
63 }
64 }
65
66 return 0;
67}
68
Ted Kremenek9577c1e2009-03-03 22:06:47 +000069/// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
Zhongxing Xucac107a2009-04-20 05:24:46 +000070/// wraps a symbol, return that SymbolRef. Otherwise return 0.
71// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek9577c1e2009-03-03 22:06:47 +000072SymbolRef SVal::getAsLocSymbol() const {
Ted Kremenek9577c1e2009-03-03 22:06:47 +000073 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
Ted Kremenekce563192009-07-29 18:14:27 +000074 const MemRegion *R = X->getBaseRegion();
Ted Kremenekd492ebc2009-07-29 21:43:22 +000075 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
76 return SymR->getSymbol();
Ted Kremenek9577c1e2009-03-03 22:06:47 +000077 }
Ted Kremenekd492ebc2009-07-29 21:43:22 +000078 return NULL;
Ted Kremenek9577c1e2009-03-03 22:06:47 +000079}
80
81/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
Zhongxing Xucac107a2009-04-20 05:24:46 +000082/// Otherwise return 0.
83// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek9577c1e2009-03-03 22:06:47 +000084SymbolRef SVal::getAsSymbol() const {
85 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
86 return X->getSymbol();
87
Ted Kremenek74556a12009-03-26 03:35:11 +000088 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
89 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
90 return Y;
91
Ted Kremenek9577c1e2009-03-03 22:06:47 +000092 return getAsLocSymbol();
93}
94
Ted Kremenek74556a12009-03-26 03:35:11 +000095/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
96/// return that expression. Otherwise return NULL.
97const SymExpr *SVal::getAsSymbolicExpression() const {
98 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
99 return X->getSymbolicExpression();
100
101 return getAsSymbol();
102}
103
Zhongxing Xu08b72bd2009-06-30 11:52:40 +0000104const MemRegion *SVal::getAsRegion() const {
105 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
106 return X->getRegion();
107
108 return 0;
109}
110
Ted Kremenekce563192009-07-29 18:14:27 +0000111const MemRegion *loc::MemRegionVal::getBaseRegion() const {
112 const MemRegion *R = getRegion();
113 return R ? R->getBaseRegion() : NULL;
114}
115
Ted Kremenek74556a12009-03-26 03:35:11 +0000116bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
117 return itr == X.itr;
118}
119
120bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
121 return itr != X.itr;
122}
123
124SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
125 itr.push_back(SE);
126 while (!isa<SymbolData>(itr.back())) expand();
127}
128
129SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
130 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
131 assert(isa<SymbolData>(itr.back()));
132 itr.pop_back();
133 if (!itr.empty())
134 while (!isa<SymbolData>(itr.back())) expand();
135 return *this;
136}
137
138SymbolRef SVal::symbol_iterator::operator*() {
139 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
140 return cast<SymbolData>(itr.back());
141}
142
143void SVal::symbol_iterator::expand() {
144 const SymExpr *SE = itr.back();
145 itr.pop_back();
146
147 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
148 itr.push_back(SIE->getLHS());
149 return;
150 }
151 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
152 itr.push_back(SSE->getLHS());
153 itr.push_back(SSE->getRHS());
154 return;
155 }
156
157 assert(false && "unhandled expansion case");
158}
159
Ted Kremenekf1172ab2009-08-06 01:20:57 +0000160const GRState *nonloc::LazyCompoundVal::getState() const {
161 return static_cast<const LazyCompoundValData*>(Data)->getState();
162}
163
164const TypedRegion *nonloc::LazyCompoundVal::getRegion() const {
165 return static_cast<const LazyCompoundValData*>(Data)->getRegion();
166}
167
Ted Kremenek15cb0782008-02-06 22:50:25 +0000168//===----------------------------------------------------------------------===//
Ted Kremenekb667c9f2008-10-30 18:01:28 +0000169// Other Iterators.
170//===----------------------------------------------------------------------===//
171
172nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
173 return getValue()->begin();
174}
175
176nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
177 return getValue()->end();
178}
179
180//===----------------------------------------------------------------------===//
Ted Kremenek99b4f432008-07-18 15:54:51 +0000181// Useful predicates.
182//===----------------------------------------------------------------------===//
183
Zhongxing Xu097fc982008-10-17 05:57:07 +0000184bool SVal::isZeroConstant() const {
185 if (isa<loc::ConcreteInt>(*this))
186 return cast<loc::ConcreteInt>(*this).getValue() == 0;
187 else if (isa<nonloc::ConcreteInt>(*this))
188 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek99b4f432008-07-18 15:54:51 +0000189 else
190 return false;
191}
192
193
194//===----------------------------------------------------------------------===//
Zhongxing Xu097fc982008-10-17 05:57:07 +0000195// Transfer function dispatch for Non-Locs.
Ted Kremenek15cb0782008-02-06 22:50:25 +0000196//===----------------------------------------------------------------------===//
Ted Kremenek6e24a802008-02-01 06:36:40 +0000197
Ted Kremenekd1c53ff2009-06-26 00:05:51 +0000198SVal nonloc::ConcreteInt::evalBinOp(ValueManager &ValMgr,
199 BinaryOperator::Opcode Op,
200 const nonloc::ConcreteInt& R) const {
Ted Kremenek43a281c2008-07-18 15:59:33 +0000201 const llvm::APSInt* X =
Ted Kremenekd1c53ff2009-06-26 00:05:51 +0000202 ValMgr.getBasicValueFactory().EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenekc2d07202008-02-28 20:32:03 +0000203
204 if (X)
Zhongxing Xu097fc982008-10-17 05:57:07 +0000205 return nonloc::ConcreteInt(*X);
Ted Kremenekc2d07202008-02-28 20:32:03 +0000206 else
207 return UndefinedVal();
Ted Kremenek15cb0782008-02-06 22:50:25 +0000208}
209
Zhongxing Xu097fc982008-10-17 05:57:07 +0000210nonloc::ConcreteInt
Ted Kremenekd1c53ff2009-06-26 00:05:51 +0000211nonloc::ConcreteInt::evalComplement(ValueManager &ValMgr) const {
212 return ValMgr.makeIntVal(~getValue());
Ted Kremenek15cb0782008-02-06 22:50:25 +0000213}
214
Ted Kremenekd1c53ff2009-06-26 00:05:51 +0000215nonloc::ConcreteInt nonloc::ConcreteInt::evalMinus(ValueManager &ValMgr) const {
216 return ValMgr.makeIntVal(-getValue());
Ted Kremenek2cb46642008-02-04 16:58:30 +0000217}
218
Ted Kremenek72197902008-01-31 19:34:24 +0000219//===----------------------------------------------------------------------===//
Zhongxing Xu097fc982008-10-17 05:57:07 +0000220// Transfer function dispatch for Locs.
Ted Kremenek72197902008-01-31 19:34:24 +0000221//===----------------------------------------------------------------------===//
222
Ted Kremenekda3e12d2008-10-30 17:53:23 +0000223SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
224 BinaryOperator::Opcode Op,
225 const loc::ConcreteInt& R) const {
Ted Kremenek15cb0782008-02-06 22:50:25 +0000226
227 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
228 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
229
Ted Kremenek8ad19872008-03-07 20:13:31 +0000230 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenekc2d07202008-02-28 20:32:03 +0000231
232 if (X)
Zhongxing Xu097fc982008-10-17 05:57:07 +0000233 return loc::ConcreteInt(*X);
Ted Kremenekc2d07202008-02-28 20:32:03 +0000234 else
235 return UndefinedVal();
Ted Kremenek15cb0782008-02-06 22:50:25 +0000236}
Ted Kremenek72197902008-01-31 19:34:24 +0000237
Ted Kremenek72197902008-01-31 19:34:24 +0000238//===----------------------------------------------------------------------===//
239// Pretty-Printing.
240//===----------------------------------------------------------------------===//
241
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000242void SVal::dump() const { dumpToStream(llvm::errs()); }
Ted Kremenekad884682008-02-12 21:37:56 +0000243
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000244void SVal::dumpToStream(llvm::raw_ostream& os) const {
245 switch (getBaseKind()) {
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000246 case UnknownKind:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000247 os << "Invalid";
248 break;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000249 case NonLocKind:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000250 cast<NonLoc>(this)->dumpToStream(os);
251 break;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000252 case LocKind:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000253 cast<Loc>(this)->dumpToStream(os);
254 break;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000255 case UndefinedKind:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000256 os << "Undefined";
257 break;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000258 default:
259 assert (false && "Invalid SVal.");
260 }
261}
262
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000263void NonLoc::dumpToStream(llvm::raw_ostream& os) const {
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000264 switch (getSubKind()) {
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000265 case nonloc::ConcreteIntKind:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000266 os << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000267 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000268 os << 'U';
269 break;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000270 case nonloc::SymbolValKind:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000271 os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
272 break;
Ted Kremenek74556a12009-03-26 03:35:11 +0000273 case nonloc::SymExprValKind: {
274 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
275 const SymExpr *SE = C.getSymbolicExpression();
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000276 os << SE;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000277 break;
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000278 }
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000279 case nonloc::LocAsIntegerKind: {
280 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000281 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000282 break;
283 }
Ted Kremenekb667c9f2008-10-30 18:01:28 +0000284 case nonloc::CompoundValKind: {
285 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenek92022bc2009-07-14 20:21:36 +0000286 os << "compoundVal{";
Ted Kremenekc1c2bbf2008-10-30 18:35:10 +0000287 bool first = true;
288 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000289 if (first) {
290 os << ' '; first = false;
291 }
292 else
293 os << ", ";
294
295 (*I).dumpToStream(os);
Ted Kremenekc1c2bbf2008-10-30 18:35:10 +0000296 }
Ted Kremenek92022bc2009-07-14 20:21:36 +0000297 os << "}";
Ted Kremenekb667c9f2008-10-30 18:01:28 +0000298 break;
Ted Kremenekf1172ab2009-08-06 01:20:57 +0000299 }
300 case nonloc::LazyCompoundValKind: {
301 const nonloc::LazyCompoundVal &C = *cast<nonloc::LazyCompoundVal>(this);
302 os << "lazyCompoundVal{" << (void*) C.getState() << ',' << C.getRegion()
303 << '}';
304 break;
305 }
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000306 default:
307 assert (false && "Pretty-printed not implemented for this NonLoc.");
308 break;
309 }
310}
311
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000312void Loc::dumpToStream(llvm::raw_ostream& os) const {
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000313 switch (getSubKind()) {
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000314 case loc::ConcreteIntKind:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000315 os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
316 break;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000317 case loc::GotoLabelKind:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000318 os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000319 break;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000320 case loc::MemRegionKind:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000321 os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
322 break;
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000323 default:
Ted Kremenek42d8d0e2009-07-13 23:53:06 +0000324 assert(false && "Pretty-printing not implemented for this Loc.");
Zhongxing Xuc44f5562008-10-24 06:00:12 +0000325 break;
326 }
327}