blob: 6f480e8d46bf13322285535292abeaab33654930 [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 Kremenek94c96982009-03-03 22:06:47 +000076
77 while (R) {
78 // Blast through region views.
79 if (const TypedViewRegion *View = dyn_cast<TypedViewRegion>(R)) {
80 R = View->getSuperRegion();
81 continue;
82 }
Ted Kremenek94c96982009-03-03 22:06:47 +000083 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
84 return SymR->getSymbol();
85
86 break;
87 }
88 }
89
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000090 return 0;
Ted Kremenek94c96982009-03-03 22:06:47 +000091}
92
93/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
Zhongxing Xu369f4472009-04-20 05:24:46 +000094/// Otherwise return 0.
95// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
Ted Kremenek94c96982009-03-03 22:06:47 +000096SymbolRef SVal::getAsSymbol() const {
97 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
98 return X->getSymbol();
99
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000100 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
101 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
102 return Y;
103
Ted Kremenek94c96982009-03-03 22:06:47 +0000104 return getAsLocSymbol();
105}
106
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000107/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
108/// return that expression. Otherwise return NULL.
109const SymExpr *SVal::getAsSymbolicExpression() const {
110 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
111 return X->getSymbolicExpression();
112
113 return getAsSymbol();
114}
115
Zhongxing Xuedb883c2009-06-30 11:52:40 +0000116const MemRegion *SVal::getAsRegion() const {
117 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
118 return X->getRegion();
119
120 return 0;
121}
122
Ted Kremenek0e3ec3f2009-07-29 18:14:27 +0000123const MemRegion *loc::MemRegionVal::getBaseRegion() const {
124 const MemRegion *R = getRegion();
125 return R ? R->getBaseRegion() : NULL;
126}
127
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000128bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
129 return itr == X.itr;
130}
131
132bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
133 return itr != X.itr;
134}
135
136SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
137 itr.push_back(SE);
138 while (!isa<SymbolData>(itr.back())) expand();
139}
140
141SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
142 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
143 assert(isa<SymbolData>(itr.back()));
144 itr.pop_back();
145 if (!itr.empty())
146 while (!isa<SymbolData>(itr.back())) expand();
147 return *this;
148}
149
150SymbolRef SVal::symbol_iterator::operator*() {
151 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
152 return cast<SymbolData>(itr.back());
153}
154
155void SVal::symbol_iterator::expand() {
156 const SymExpr *SE = itr.back();
157 itr.pop_back();
158
159 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
160 itr.push_back(SIE->getLHS());
161 return;
162 }
163 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
164 itr.push_back(SSE->getLHS());
165 itr.push_back(SSE->getRHS());
166 return;
167 }
168
169 assert(false && "unhandled expansion case");
170}
171
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000172//===----------------------------------------------------------------------===//
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000173// Other Iterators.
174//===----------------------------------------------------------------------===//
175
176nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
177 return getValue()->begin();
178}
179
180nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
181 return getValue()->end();
182}
183
184//===----------------------------------------------------------------------===//
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000185// Useful predicates.
186//===----------------------------------------------------------------------===//
187
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000188bool SVal::isZeroConstant() const {
189 if (isa<loc::ConcreteInt>(*this))
190 return cast<loc::ConcreteInt>(*this).getValue() == 0;
191 else if (isa<nonloc::ConcreteInt>(*this))
192 return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000193 else
194 return false;
195}
196
197
198//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000199// Transfer function dispatch for Non-Locs.
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000200//===----------------------------------------------------------------------===//
Ted Kremeneka6e4d212008-02-01 06:36:40 +0000201
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000202SVal nonloc::ConcreteInt::evalBinOp(ValueManager &ValMgr,
203 BinaryOperator::Opcode Op,
204 const nonloc::ConcreteInt& R) const {
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000205 const llvm::APSInt* X =
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000206 ValMgr.getBasicValueFactory().EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000207
208 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000209 return nonloc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000210 else
211 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000212}
213
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000214nonloc::ConcreteInt
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000215nonloc::ConcreteInt::evalComplement(ValueManager &ValMgr) const {
216 return ValMgr.makeIntVal(~getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000217}
218
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000219nonloc::ConcreteInt nonloc::ConcreteInt::evalMinus(ValueManager &ValMgr) const {
220 return ValMgr.makeIntVal(-getValue());
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000221}
222
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000223//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000224// Transfer function dispatch for Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000225//===----------------------------------------------------------------------===//
226
Ted Kremenekccaad9d2008-10-30 17:53:23 +0000227SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
228 BinaryOperator::Opcode Op,
229 const loc::ConcreteInt& R) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000230
231 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
232 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
233
Ted Kremenek240f1f02008-03-07 20:13:31 +0000234 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000235
236 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000237 return loc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000238 else
239 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000240}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000241
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000242//===----------------------------------------------------------------------===//
243// Pretty-Printing.
244//===----------------------------------------------------------------------===//
245
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000246void SVal::dump() const { dumpToStream(llvm::errs()); }
Ted Kremenek2a502572008-02-12 21:37:56 +0000247
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000248void SVal::dumpToStream(llvm::raw_ostream& os) const {
249 switch (getBaseKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000250 case UnknownKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000251 os << "Invalid";
252 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000253 case NonLocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000254 cast<NonLoc>(this)->dumpToStream(os);
255 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000256 case LocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000257 cast<Loc>(this)->dumpToStream(os);
258 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000259 case UndefinedKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000260 os << "Undefined";
261 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000262 default:
263 assert (false && "Invalid SVal.");
264 }
265}
266
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000267void NonLoc::dumpToStream(llvm::raw_ostream& os) const {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000268 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000269 case nonloc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000270 os << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000271 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000272 os << 'U';
273 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000274 case nonloc::SymbolValKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000275 os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
276 break;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000277 case nonloc::SymExprValKind: {
278 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
279 const SymExpr *SE = C.getSymbolicExpression();
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000280 os << SE;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000281 break;
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000282 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000283 case nonloc::LocAsIntegerKind: {
284 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000285 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000286 break;
287 }
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000288 case nonloc::CompoundValKind: {
289 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenek7b679522009-07-14 20:21:36 +0000290 os << "compoundVal{";
Ted Kremenekb8b41612008-10-30 18:35:10 +0000291 bool first = true;
292 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000293 if (first) {
294 os << ' '; first = false;
295 }
296 else
297 os << ", ";
298
299 (*I).dumpToStream(os);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000300 }
Ted Kremenek7b679522009-07-14 20:21:36 +0000301 os << "}";
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000302 break;
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000303 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000304 default:
305 assert (false && "Pretty-printed not implemented for this NonLoc.");
306 break;
307 }
308}
309
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000310void Loc::dumpToStream(llvm::raw_ostream& os) const {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000311 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000312 case loc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000313 os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
314 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000315 case loc::GotoLabelKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000316 os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000317 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000318 case loc::MemRegionKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000319 os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
320 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000321 default:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000322 assert(false && "Pretty-printing not implemented for this Loc.");
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000323 break;
324 }
325}