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 #include "llvm/Support/raw_ostream.h"
19 using namespace clang;
23 //===----------------------------------------------------------------------===//
24 // Symbol iteration within an SVal.
25 //===----------------------------------------------------------------------===//
28 //===----------------------------------------------------------------------===//
30 //===----------------------------------------------------------------------===//
32 bool SVal::hasConjuredSymbol() const {
33 if (Optional<nonloc::SymbolVal> SV = getAs<nonloc::SymbolVal>()) {
34 SymbolRef sym = SV->getSymbol();
35 if (isa<SymbolConjured>(sym))
39 if (Optional<loc::MemRegionVal> RV = getAs<loc::MemRegionVal>()) {
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))
51 const FunctionDecl *SVal::getAsFunctionDecl() const {
52 if (Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>()) {
53 const MemRegion* R = X->getRegion();
54 if (const FunctionTextRegion *CTR = R->getAs<FunctionTextRegion>())
55 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CTR->getDecl()))
62 /// \brief If this SVal is a location (subclasses Loc) and wraps a symbol,
63 /// return that SymbolRef. Otherwise return 0.
65 /// Implicit casts (ex: void* -> char*) can turn Symbolic region into Element
66 /// region. If that is the case, gets the underlining region.
67 /// When IncludeBaseRegions is set to true and the SubRegion is non-symbolic,
68 /// the first symbolic parent region is returned.
69 SymbolRef SVal::getAsLocSymbol(bool IncludeBaseRegions) const {
70 // FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
71 if (Optional<nonloc::LocAsInteger> X = getAs<nonloc::LocAsInteger>())
72 return X->getLoc().getAsLocSymbol();
74 if (Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>()) {
75 const MemRegion *R = X->getRegion();
76 if (const SymbolicRegion *SymR = IncludeBaseRegions ?
77 R->getSymbolicBase() :
78 dyn_cast<SymbolicRegion>(R->StripCasts()))
79 return SymR->getSymbol();
84 /// Get the symbol in the SVal or its base region.
85 SymbolRef SVal::getLocSymbolInBase() const {
86 Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>();
91 const MemRegion *R = X->getRegion();
93 while (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
94 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SR))
95 return SymR->getSymbol();
97 R = SR->getSuperRegion();
103 // TODO: The next 3 functions have to be simplified.
105 /// \brief If this SVal wraps a symbol return that SymbolRef.
106 /// Otherwise, return 0.
108 /// Casts are ignored during lookup.
109 /// \param IncludeBaseRegions The boolean that controls whether the search
110 /// should continue to the base regions if the region is not symbolic.
111 SymbolRef SVal::getAsSymbol(bool IncludeBaseRegion) const {
112 // FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
113 if (Optional<nonloc::SymbolVal> X = getAs<nonloc::SymbolVal>())
114 return X->getSymbol();
116 return getAsLocSymbol(IncludeBaseRegion);
119 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
120 /// return that expression. Otherwise return NULL.
121 const SymExpr *SVal::getAsSymbolicExpression() const {
122 if (Optional<nonloc::SymbolVal> X = getAs<nonloc::SymbolVal>())
123 return X->getSymbol();
125 return getAsSymbol();
128 const SymExpr* SVal::getAsSymExpr() const {
129 const SymExpr* Sym = getAsSymbol();
131 Sym = getAsSymbolicExpression();
135 const MemRegion *SVal::getAsRegion() const {
136 if (Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>())
137 return X->getRegion();
139 if (Optional<nonloc::LocAsInteger> X = getAs<nonloc::LocAsInteger>())
140 return X->getLoc().getAsRegion();
145 const MemRegion *loc::MemRegionVal::stripCasts(bool StripBaseCasts) const {
146 const MemRegion *R = getRegion();
147 return R ? R->StripCasts(StripBaseCasts) : NULL;
150 const void *nonloc::LazyCompoundVal::getStore() const {
151 return static_cast<const LazyCompoundValData*>(Data)->getStore();
154 const TypedValueRegion *nonloc::LazyCompoundVal::getRegion() const {
155 return static_cast<const LazyCompoundValData*>(Data)->getRegion();
158 //===----------------------------------------------------------------------===//
160 //===----------------------------------------------------------------------===//
162 nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
163 return getValue()->begin();
166 nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
167 return getValue()->end();
170 //===----------------------------------------------------------------------===//
171 // Useful predicates.
172 //===----------------------------------------------------------------------===//
174 bool SVal::isConstant() const {
175 return getAs<nonloc::ConcreteInt>() || getAs<loc::ConcreteInt>();
178 bool SVal::isConstant(int I) const {
179 if (Optional<loc::ConcreteInt> LV = getAs<loc::ConcreteInt>())
180 return LV->getValue() == I;
181 if (Optional<nonloc::ConcreteInt> NV = getAs<nonloc::ConcreteInt>())
182 return NV->getValue() == I;
186 bool SVal::isZeroConstant() const {
187 return isConstant(0);
191 //===----------------------------------------------------------------------===//
192 // Transfer function dispatch for Non-Locs.
193 //===----------------------------------------------------------------------===//
195 SVal nonloc::ConcreteInt::evalBinOp(SValBuilder &svalBuilder,
196 BinaryOperator::Opcode Op,
197 const nonloc::ConcreteInt& R) const {
198 const llvm::APSInt* X =
199 svalBuilder.getBasicValueFactory().evalAPSInt(Op, getValue(), R.getValue());
202 return nonloc::ConcreteInt(*X);
204 return UndefinedVal();
208 nonloc::ConcreteInt::evalComplement(SValBuilder &svalBuilder) const {
209 return svalBuilder.makeIntVal(~getValue());
213 nonloc::ConcreteInt::evalMinus(SValBuilder &svalBuilder) const {
214 return svalBuilder.makeIntVal(-getValue());
217 //===----------------------------------------------------------------------===//
218 // Transfer function dispatch for Locs.
219 //===----------------------------------------------------------------------===//
221 SVal loc::ConcreteInt::evalBinOp(BasicValueFactory& BasicVals,
222 BinaryOperator::Opcode Op,
223 const loc::ConcreteInt& R) const {
225 assert(BinaryOperator::isComparisonOp(Op) || Op == BO_Sub);
227 const llvm::APSInt *X = BasicVals.evalAPSInt(Op, getValue(), R.getValue());
230 return nonloc::ConcreteInt(*X);
232 return UndefinedVal();
235 //===----------------------------------------------------------------------===//
237 //===----------------------------------------------------------------------===//
239 void SVal::dump() const { dumpToStream(llvm::errs()); }
241 void SVal::dumpToStream(raw_ostream &os) const {
242 switch (getBaseKind()) {
247 castAs<NonLoc>().dumpToStream(os);
250 castAs<Loc>().dumpToStream(os);
258 void NonLoc::dumpToStream(raw_ostream &os) const {
259 switch (getSubKind()) {
260 case nonloc::ConcreteIntKind: {
261 const nonloc::ConcreteInt& C = castAs<nonloc::ConcreteInt>();
262 if (C.getValue().isUnsigned())
263 os << C.getValue().getZExtValue();
265 os << C.getValue().getSExtValue();
266 os << ' ' << (C.getValue().isUnsigned() ? 'U' : 'S')
267 << C.getValue().getBitWidth() << 'b';
270 case nonloc::SymbolValKind: {
271 os << castAs<nonloc::SymbolVal>().getSymbol();
274 case nonloc::LocAsIntegerKind: {
275 const nonloc::LocAsInteger& C = castAs<nonloc::LocAsInteger>();
276 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
279 case nonloc::CompoundValKind: {
280 const nonloc::CompoundVal& C = castAs<nonloc::CompoundVal>();
281 os << "compoundVal{";
283 for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
285 os << ' '; first = false;
290 (*I).dumpToStream(os);
295 case nonloc::LazyCompoundValKind: {
296 const nonloc::LazyCompoundVal &C = castAs<nonloc::LazyCompoundVal>();
297 os << "lazyCompoundVal{" << const_cast<void *>(C.getStore())
298 << ',' << C.getRegion()
303 assert (false && "Pretty-printed not implemented for this NonLoc.");
308 void Loc::dumpToStream(raw_ostream &os) const {
309 switch (getSubKind()) {
310 case loc::ConcreteIntKind:
311 os << castAs<loc::ConcreteInt>().getValue().getZExtValue() << " (Loc)";
313 case loc::GotoLabelKind:
314 os << "&&" << castAs<loc::GotoLabel>().getLabel()->getName();
316 case loc::MemRegionKind:
317 os << '&' << castAs<loc::MemRegionVal>().getRegion()->getString();
320 llvm_unreachable("Pretty-printing not implemented for this Loc.");