1 //= RValues.cpp - Abstract RValues for Path-Sens. Value Tracking -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines SVal, Loc, and NonLoc, classes that represent
11 // abstract r-values for use with path-sensitive value tracking.
13 //===----------------------------------------------------------------------===//
15 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
16 #include "clang/AST/ExprObjC.h"
17 #include "clang/Basic/IdentifierTable.h"
18 using namespace clang;
22 //===----------------------------------------------------------------------===//
23 // Symbol iteration within an SVal.
24 //===----------------------------------------------------------------------===//
27 //===----------------------------------------------------------------------===//
29 //===----------------------------------------------------------------------===//
31 bool SVal::hasConjuredSymbol() const {
32 if (const nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(this)) {
33 SymbolRef sym = SV->getSymbol();
34 if (isa<SymbolConjured>(sym))
38 if (const loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(this)) {
39 const MemRegion *R = RV->getRegion();
40 if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
41 SymbolRef sym = SR->getSymbol();
42 if (isa<SymbolConjured>(sym))
50 const FunctionDecl *SVal::getAsFunctionDecl() const {
51 if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(this)) {
52 const MemRegion* R = X->getRegion();
53 if (const FunctionTextRegion *CTR = R->getAs<FunctionTextRegion>())
54 return CTR->getDecl();
60 /// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
61 /// wraps a symbol, return that SymbolRef. Otherwise return 0.
62 // FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
63 SymbolRef SVal::getAsLocSymbol() const {
64 if (const nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(this))
65 return X->getLoc().getAsLocSymbol();
67 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
68 const MemRegion *R = X->stripCasts();
69 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
70 return SymR->getSymbol();
75 /// Get the symbol in the SVal or its base region.
76 SymbolRef SVal::getLocSymbolInBase() const {
77 const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this);
82 const MemRegion *R = X->getRegion();
84 while (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
85 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SR))
86 return SymR->getSymbol();
88 R = SR->getSuperRegion();
94 /// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
95 /// Otherwise return 0.
96 // FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
97 SymbolRef SVal::getAsSymbol() const {
98 if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
99 return X->getSymbol();
101 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
102 if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
105 return getAsLocSymbol();
108 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
109 /// return that expression. Otherwise return NULL.
110 const SymExpr *SVal::getAsSymbolicExpression() const {
111 if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
112 return X->getSymbolicExpression();
114 return getAsSymbol();
117 const MemRegion *SVal::getAsRegion() const {
118 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
119 return X->getRegion();
121 if (const nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(this)) {
122 return X->getLoc().getAsRegion();
128 const MemRegion *loc::MemRegionVal::stripCasts() const {
129 const MemRegion *R = getRegion();
130 return R ? R->StripCasts() : NULL;
133 bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
137 bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
141 SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
143 while (!isa<SymbolData>(itr.back())) expand();
146 SVal::symbol_iterator &SVal::symbol_iterator::operator++() {
147 assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
148 assert(isa<SymbolData>(itr.back()));
151 while (!isa<SymbolData>(itr.back())) expand();
155 SymbolRef SVal::symbol_iterator::operator*() {
156 assert(!itr.empty() && "attempting to dereference an 'end' iterator");
157 return cast<SymbolData>(itr.back());
160 void SVal::symbol_iterator::expand() {
161 const SymExpr *SE = itr.back();
164 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
165 itr.push_back(SIE->getLHS());
168 else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
169 itr.push_back(SSE->getLHS());
170 itr.push_back(SSE->getRHS());
174 llvm_unreachable("unhandled expansion case");
177 const void *nonloc::LazyCompoundVal::getStore() const {
178 return static_cast<const LazyCompoundValData*>(Data)->getStore();
181 const TypedRegion *nonloc::LazyCompoundVal::getRegion() const {
182 return static_cast<const LazyCompoundValData*>(Data)->getRegion();
185 //===----------------------------------------------------------------------===//
187 //===----------------------------------------------------------------------===//
189 nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
190 return getValue()->begin();
193 nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
194 return getValue()->end();
197 //===----------------------------------------------------------------------===//
198 // Useful predicates.
199 //===----------------------------------------------------------------------===//
201 bool SVal::isConstant() const {
202 return isa<nonloc::ConcreteInt>(this) || isa<loc::ConcreteInt>(this);
205 bool SVal::isConstant(int I) const {
206 if (isa<loc::ConcreteInt>(*this))
207 return cast<loc::ConcreteInt>(*this).getValue() == I;
208 else if (isa<nonloc::ConcreteInt>(*this))
209 return cast<nonloc::ConcreteInt>(*this).getValue() == I;
214 bool SVal::isZeroConstant() const {
215 return isConstant(0);
219 //===----------------------------------------------------------------------===//
220 // Transfer function dispatch for Non-Locs.
221 //===----------------------------------------------------------------------===//
223 SVal nonloc::ConcreteInt::evalBinOp(SValBuilder &svalBuilder,
224 BinaryOperator::Opcode Op,
225 const nonloc::ConcreteInt& R) const {
226 const llvm::APSInt* X =
227 svalBuilder.getBasicValueFactory().evalAPSInt(Op, getValue(), R.getValue());
230 return nonloc::ConcreteInt(*X);
232 return UndefinedVal();
236 nonloc::ConcreteInt::evalComplement(SValBuilder &svalBuilder) const {
237 return svalBuilder.makeIntVal(~getValue());
241 nonloc::ConcreteInt::evalMinus(SValBuilder &svalBuilder) const {
242 return svalBuilder.makeIntVal(-getValue());
245 //===----------------------------------------------------------------------===//
246 // Transfer function dispatch for Locs.
247 //===----------------------------------------------------------------------===//
249 SVal loc::ConcreteInt::evalBinOp(BasicValueFactory& BasicVals,
250 BinaryOperator::Opcode Op,
251 const loc::ConcreteInt& R) const {
253 assert (Op == BO_Add || Op == BO_Sub ||
254 (Op >= BO_LT && Op <= BO_NE));
256 const llvm::APSInt* X = BasicVals.evalAPSInt(Op, getValue(), R.getValue());
259 return loc::ConcreteInt(*X);
261 return UndefinedVal();
264 //===----------------------------------------------------------------------===//
266 //===----------------------------------------------------------------------===//
268 void SVal::dump() const { dumpToStream(llvm::errs()); }
270 void SVal::dumpToStream(raw_ostream &os) const {
271 switch (getBaseKind()) {
276 cast<NonLoc>(this)->dumpToStream(os);
279 cast<Loc>(this)->dumpToStream(os);
285 assert (false && "Invalid SVal.");
289 void NonLoc::dumpToStream(raw_ostream &os) const {
290 switch (getSubKind()) {
291 case nonloc::ConcreteIntKind: {
292 const nonloc::ConcreteInt& C = *cast<nonloc::ConcreteInt>(this);
293 if (C.getValue().isUnsigned())
294 os << C.getValue().getZExtValue();
296 os << C.getValue().getSExtValue();
297 os << ' ' << (C.getValue().isUnsigned() ? 'U' : 'S')
298 << C.getValue().getBitWidth() << 'b';
301 case nonloc::SymbolValKind:
302 os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
304 case nonloc::SymExprValKind: {
305 const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
306 const SymExpr *SE = C.getSymbolicExpression();
310 case nonloc::LocAsIntegerKind: {
311 const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
312 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
315 case nonloc::CompoundValKind: {
316 const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
317 os << "compoundVal{";
319 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
321 os << ' '; first = false;
326 (*I).dumpToStream(os);
331 case nonloc::LazyCompoundValKind: {
332 const nonloc::LazyCompoundVal &C = *cast<nonloc::LazyCompoundVal>(this);
333 os << "lazyCompoundVal{" << const_cast<void *>(C.getStore())
334 << ',' << C.getRegion()
339 assert (false && "Pretty-printed not implemented for this NonLoc.");
344 void Loc::dumpToStream(raw_ostream &os) const {
345 switch (getSubKind()) {
346 case loc::ConcreteIntKind:
347 os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
349 case loc::GotoLabelKind:
350 os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getName();
352 case loc::MemRegionKind:
353 os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
355 case loc::ObjCPropRefKind: {
356 const ObjCPropertyRefExpr *E = cast<loc::ObjCPropRef>(this)->getPropRefExpr();
358 if (E->isSuperReceiver())
360 else if (E->getBase())
363 if (E->isImplicitProperty())
364 os << E->getImplicitPropertyGetter()->getSelector().getAsString();
366 os << E->getExplicitProperty()->getName();
372 llvm_unreachable("Pretty-printing not implemented for this Loc.");