1 //===- RValues.cpp - Abstract RValues for Path-Sens. Value Tracking -------===//
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/SVals.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/Type.h"
20 #include "clang/Basic/LLVM.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
33 using namespace clang;
36 //===----------------------------------------------------------------------===//
37 // Symbol iteration within an SVal.
38 //===----------------------------------------------------------------------===//
40 //===----------------------------------------------------------------------===//
42 //===----------------------------------------------------------------------===//
44 bool SVal::hasConjuredSymbol() const {
45 if (Optional<nonloc::SymbolVal> SV = getAs<nonloc::SymbolVal>()) {
46 SymbolRef sym = SV->getSymbol();
47 if (isa<SymbolConjured>(sym))
51 if (Optional<loc::MemRegionVal> RV = getAs<loc::MemRegionVal>()) {
52 const MemRegion *R = RV->getRegion();
53 if (const auto *SR = dyn_cast<SymbolicRegion>(R)) {
54 SymbolRef sym = SR->getSymbol();
55 if (isa<SymbolConjured>(sym))
63 const FunctionDecl *SVal::getAsFunctionDecl() const {
64 if (Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>()) {
65 const MemRegion* R = X->getRegion();
66 if (const FunctionCodeRegion *CTR = R->getAs<FunctionCodeRegion>())
67 if (const auto *FD = dyn_cast<FunctionDecl>(CTR->getDecl()))
71 if (auto X = getAs<nonloc::PointerToMember>()) {
72 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(X->getDecl()))
78 /// If this SVal is a location (subclasses Loc) and wraps a symbol,
79 /// return that SymbolRef. Otherwise return 0.
81 /// Implicit casts (ex: void* -> char*) can turn Symbolic region into Element
82 /// region. If that is the case, gets the underlining region.
83 /// When IncludeBaseRegions is set to true and the SubRegion is non-symbolic,
84 /// the first symbolic parent region is returned.
85 SymbolRef SVal::getAsLocSymbol(bool IncludeBaseRegions) const {
86 // FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
87 if (Optional<nonloc::LocAsInteger> X = getAs<nonloc::LocAsInteger>())
88 return X->getLoc().getAsLocSymbol();
90 if (Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>()) {
91 const MemRegion *R = X->getRegion();
92 if (const SymbolicRegion *SymR = IncludeBaseRegions ?
93 R->getSymbolicBase() :
94 dyn_cast<SymbolicRegion>(R->StripCasts()))
95 return SymR->getSymbol();
100 /// Get the symbol in the SVal or its base region.
101 SymbolRef SVal::getLocSymbolInBase() const {
102 Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>();
107 const MemRegion *R = X->getRegion();
109 while (const auto *SR = dyn_cast<SubRegion>(R)) {
110 if (const auto *SymR = dyn_cast<SymbolicRegion>(SR))
111 return SymR->getSymbol();
113 R = SR->getSuperRegion();
119 // TODO: The next 3 functions have to be simplified.
121 /// If this SVal wraps a symbol return that SymbolRef.
122 /// Otherwise, return 0.
124 /// Casts are ignored during lookup.
125 /// \param IncludeBaseRegions The boolean that controls whether the search
126 /// should continue to the base regions if the region is not symbolic.
127 SymbolRef SVal::getAsSymbol(bool IncludeBaseRegions) const {
128 // FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
129 if (Optional<nonloc::SymbolVal> X = getAs<nonloc::SymbolVal>())
130 return X->getSymbol();
132 return getAsLocSymbol(IncludeBaseRegions);
135 /// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
136 /// return that expression. Otherwise return NULL.
137 const SymExpr *SVal::getAsSymbolicExpression() const {
138 if (Optional<nonloc::SymbolVal> X = getAs<nonloc::SymbolVal>())
139 return X->getSymbol();
141 return getAsSymbol();
144 const SymExpr* SVal::getAsSymExpr() const {
145 const SymExpr* Sym = getAsSymbol();
147 Sym = getAsSymbolicExpression();
151 const MemRegion *SVal::getAsRegion() const {
152 if (Optional<loc::MemRegionVal> X = getAs<loc::MemRegionVal>())
153 return X->getRegion();
155 if (Optional<nonloc::LocAsInteger> X = getAs<nonloc::LocAsInteger>())
156 return X->getLoc().getAsRegion();
161 const MemRegion *loc::MemRegionVal::stripCasts(bool StripBaseCasts) const {
162 const MemRegion *R = getRegion();
163 return R ? R->StripCasts(StripBaseCasts) : nullptr;
166 const void *nonloc::LazyCompoundVal::getStore() const {
167 return static_cast<const LazyCompoundValData*>(Data)->getStore();
170 const TypedValueRegion *nonloc::LazyCompoundVal::getRegion() const {
171 return static_cast<const LazyCompoundValData*>(Data)->getRegion();
174 const DeclaratorDecl *nonloc::PointerToMember::getDecl() const {
175 const auto PTMD = this->getPTMData();
179 const DeclaratorDecl *DD = nullptr;
180 if (PTMD.is<const DeclaratorDecl *>())
181 DD = PTMD.get<const DeclaratorDecl *>();
183 DD = PTMD.get<const PointerToMemberData *>()->getDeclaratorDecl();
188 //===----------------------------------------------------------------------===//
190 //===----------------------------------------------------------------------===//
192 nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
193 return getValue()->begin();
196 nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
197 return getValue()->end();
200 nonloc::PointerToMember::iterator nonloc::PointerToMember::begin() const {
201 const PTMDataType PTMD = getPTMData();
202 if (PTMD.is<const DeclaratorDecl *>())
204 return PTMD.get<const PointerToMemberData *>()->begin();
207 nonloc::PointerToMember::iterator nonloc::PointerToMember::end() const {
208 const PTMDataType PTMD = getPTMData();
209 if (PTMD.is<const DeclaratorDecl *>())
211 return PTMD.get<const PointerToMemberData *>()->end();
214 //===----------------------------------------------------------------------===//
215 // Useful predicates.
216 //===----------------------------------------------------------------------===//
218 bool SVal::isConstant() const {
219 return getAs<nonloc::ConcreteInt>() || getAs<loc::ConcreteInt>();
222 bool SVal::isConstant(int I) const {
223 if (Optional<loc::ConcreteInt> LV = getAs<loc::ConcreteInt>())
224 return LV->getValue() == I;
225 if (Optional<nonloc::ConcreteInt> NV = getAs<nonloc::ConcreteInt>())
226 return NV->getValue() == I;
230 bool SVal::isZeroConstant() const {
231 return isConstant(0);
234 //===----------------------------------------------------------------------===//
235 // Transfer function dispatch for Non-Locs.
236 //===----------------------------------------------------------------------===//
238 SVal nonloc::ConcreteInt::evalBinOp(SValBuilder &svalBuilder,
239 BinaryOperator::Opcode Op,
240 const nonloc::ConcreteInt& R) const {
241 const llvm::APSInt* X =
242 svalBuilder.getBasicValueFactory().evalAPSInt(Op, getValue(), R.getValue());
245 return nonloc::ConcreteInt(*X);
247 return UndefinedVal();
251 nonloc::ConcreteInt::evalComplement(SValBuilder &svalBuilder) const {
252 return svalBuilder.makeIntVal(~getValue());
256 nonloc::ConcreteInt::evalMinus(SValBuilder &svalBuilder) const {
257 return svalBuilder.makeIntVal(-getValue());
260 //===----------------------------------------------------------------------===//
261 // Transfer function dispatch for Locs.
262 //===----------------------------------------------------------------------===//
264 SVal loc::ConcreteInt::evalBinOp(BasicValueFactory& BasicVals,
265 BinaryOperator::Opcode Op,
266 const loc::ConcreteInt& R) const {
267 assert(BinaryOperator::isComparisonOp(Op) || Op == BO_Sub);
269 const llvm::APSInt *X = BasicVals.evalAPSInt(Op, getValue(), R.getValue());
272 return nonloc::ConcreteInt(*X);
274 return UndefinedVal();
277 //===----------------------------------------------------------------------===//
279 //===----------------------------------------------------------------------===//
281 LLVM_DUMP_METHOD void SVal::dump() const { dumpToStream(llvm::errs()); }
283 void SVal::dumpToStream(raw_ostream &os) const {
284 switch (getBaseKind()) {
289 castAs<NonLoc>().dumpToStream(os);
292 castAs<Loc>().dumpToStream(os);
294 case UndefinedValKind:
300 void NonLoc::dumpToStream(raw_ostream &os) const {
301 switch (getSubKind()) {
302 case nonloc::ConcreteIntKind: {
303 const auto &Value = castAs<nonloc::ConcreteInt>().getValue();
304 os << Value << ' ' << (Value.isSigned() ? 'S' : 'U')
305 << Value.getBitWidth() << 'b';
308 case nonloc::SymbolValKind:
309 os << castAs<nonloc::SymbolVal>().getSymbol();
312 case nonloc::LocAsIntegerKind: {
313 const nonloc::LocAsInteger& C = castAs<nonloc::LocAsInteger>();
314 os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
317 case nonloc::CompoundValKind: {
318 const nonloc::CompoundVal& C = castAs<nonloc::CompoundVal>();
319 os << "compoundVal{";
321 for (const auto &I : C) {
323 os << ' '; first = false;
333 case nonloc::LazyCompoundValKind: {
334 const nonloc::LazyCompoundVal &C = castAs<nonloc::LazyCompoundVal>();
335 os << "lazyCompoundVal{" << const_cast<void *>(C.getStore())
336 << ',' << C.getRegion()
340 case nonloc::PointerToMemberKind: {
341 os << "pointerToMember{";
342 const nonloc::PointerToMember &CastRes =
343 castAs<nonloc::PointerToMember>();
344 if (CastRes.getDecl())
345 os << "|" << CastRes.getDecl()->getQualifiedNameAsString() << "|";
347 for (const auto &I : CastRes) {
349 os << ' '; first = false;
354 os << (*I).getType().getAsString();
361 assert(false && "Pretty-printed not implemented for this NonLoc.");
366 void Loc::dumpToStream(raw_ostream &os) const {
367 switch (getSubKind()) {
368 case loc::ConcreteIntKind:
369 os << castAs<loc::ConcreteInt>().getValue().getZExtValue() << " (Loc)";
371 case loc::GotoLabelKind:
372 os << "&&" << castAs<loc::GotoLabel>().getLabel()->getName();
374 case loc::MemRegionValKind:
375 os << '&' << castAs<loc::MemRegionVal>().getRegion()->getString();
378 llvm_unreachable("Pretty-printing not implemented for this Loc.");