blob: 5ac18a150655d26c71c494ffee52d0348055bda7 [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 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
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000191SVal nonloc::ConcreteInt::evalBinOp(ValueManager &ValMgr,
192 BinaryOperator::Opcode Op,
193 const nonloc::ConcreteInt& R) const {
Ted Kremenek75b0a1c2008-07-18 15:59:33 +0000194 const llvm::APSInt* X =
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000195 ValMgr.getBasicValueFactory().EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000196
197 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000198 return nonloc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000199 else
200 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000201}
202
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000203nonloc::ConcreteInt
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000204nonloc::ConcreteInt::evalComplement(ValueManager &ValMgr) const {
205 return ValMgr.makeIntVal(~getValue());
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000206}
207
Ted Kremenek6c07bdb2009-06-26 00:05:51 +0000208nonloc::ConcreteInt nonloc::ConcreteInt::evalMinus(ValueManager &ValMgr) const {
209 return ValMgr.makeIntVal(-getValue());
Ted Kremenekc5d3b4c2008-02-04 16:58:30 +0000210}
211
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000212//===----------------------------------------------------------------------===//
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000213// Transfer function dispatch for Locs.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000214//===----------------------------------------------------------------------===//
215
Ted Kremenekccaad9d2008-10-30 17:53:23 +0000216SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
217 BinaryOperator::Opcode Op,
218 const loc::ConcreteInt& R) const {
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000219
220 assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
221 (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
222
Ted Kremenek240f1f02008-03-07 20:13:31 +0000223 const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000224
225 if (X)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000226 return loc::ConcreteInt(*X);
Ted Kremenek8cc13ea2008-02-28 20:32:03 +0000227 else
228 return UndefinedVal();
Ted Kremenekcf78b6a2008-02-06 22:50:25 +0000229}
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000230
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000231//===----------------------------------------------------------------------===//
232// Pretty-Printing.
233//===----------------------------------------------------------------------===//
234
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000235void SVal::dump() const { dumpToStream(llvm::errs()); }
Ted Kremenek2a502572008-02-12 21:37:56 +0000236
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000237void SVal::dumpToStream(llvm::raw_ostream& os) const {
238 switch (getBaseKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000239 case UnknownKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000240 os << "Invalid";
241 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000242 case NonLocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000243 cast<NonLoc>(this)->dumpToStream(os);
244 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000245 case LocKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000246 cast<Loc>(this)->dumpToStream(os);
247 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000248 case UndefinedKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000249 os << "Undefined";
250 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000251 default:
252 assert (false && "Invalid SVal.");
253 }
254}
255
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000256void NonLoc::dumpToStream(llvm::raw_ostream& os) const {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000257 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000258 case nonloc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000259 os << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000260 if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000261 os << 'U';
262 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000263 case nonloc::SymbolValKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000264 os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
265 break;
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000266 case nonloc::SymExprValKind: {
267 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
268 const SymExpr *SE = C.getSymbolicExpression();
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000269 os << SE;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000270 break;
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000271 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000272 case nonloc::LocAsIntegerKind: {
273 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000274 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000275 break;
276 }
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000277 case nonloc::CompoundValKind: {
278 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
Ted Kremenek7b679522009-07-14 20:21:36 +0000279 os << "compoundVal{";
Ted Kremenekb8b41612008-10-30 18:35:10 +0000280 bool first = true;
281 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000282 if (first) {
283 os << ' '; first = false;
284 }
285 else
286 os << ", ";
287
288 (*I).dumpToStream(os);
Ted Kremenekb8b41612008-10-30 18:35:10 +0000289 }
Ted Kremenek7b679522009-07-14 20:21:36 +0000290 os << "}";
Ted Kremeneka6fac4e2008-10-30 18:01:28 +0000291 break;
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000292 }
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000293 default:
294 assert (false && "Pretty-printed not implemented for this NonLoc.");
295 break;
296 }
297}
298
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000299void Loc::dumpToStream(llvm::raw_ostream& os) const {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000300 switch (getSubKind()) {
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000301 case loc::ConcreteIntKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000302 os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
303 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000304 case loc::GotoLabelKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000305 os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000306 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000307 case loc::MemRegionKind:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000308 os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
309 break;
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000310 default:
Ted Kremenek6f9b3a42009-07-13 23:53:06 +0000311 assert(false && "Pretty-printing not implemented for this Loc.");
Zhongxing Xu9012bff2008-10-24 06:00:12 +0000312 break;
313 }
314}