2008-02-01 03:34:24 +08:00
|
|
|
//= RValues.cpp - Abstract RValues for Path-Sens. Value Tracking -*- C++ -*-==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-10-17 13:57:07 +08:00
|
|
|
// This file defines SVal, Loc, and NonLoc, classes that represent
|
2008-02-01 03:34:24 +08:00
|
|
|
// abstract r-values for use with path-sensitive value tracking.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-04 13:50:14 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRState.h"
|
2008-08-11 13:35:13 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2008-02-01 03:34:24 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using llvm::dyn_cast;
|
|
|
|
using llvm::cast;
|
|
|
|
using llvm::APSInt;
|
|
|
|
|
2008-02-15 07:25:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2009-03-26 11:35:11 +08:00
|
|
|
// Symbol iteration within an SVal.
|
2008-02-15 07:25:54 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-01 03:34:24 +08:00
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-07 06:50:25 +08:00
|
|
|
|
2009-05-12 18:10:00 +08:00
|
|
|
bool SVal::hasConjuredSymbol() const {
|
|
|
|
if (const nonloc::SymbolVal* SV = dyn_cast<nonloc::SymbolVal>(this)) {
|
|
|
|
SymbolRef sym = SV->getSymbol();
|
|
|
|
if (isa<SymbolConjured>(sym))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const loc::MemRegionVal *RV = dyn_cast<loc::MemRegionVal>(this)) {
|
|
|
|
const MemRegion *R = RV->getRegion();
|
|
|
|
if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) {
|
|
|
|
SymbolRef sym = SR->getSymbol();
|
|
|
|
if (isa<SymbolConjured>(sym))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-08-28 12:49:15 +08:00
|
|
|
const FunctionDecl *SVal::getAsFunctionDecl() const {
|
2009-04-20 13:24:46 +08:00
|
|
|
if (const loc::MemRegionVal* X = dyn_cast<loc::MemRegionVal>(this)) {
|
|
|
|
const MemRegion* R = X->getRegion();
|
2009-08-28 12:49:15 +08:00
|
|
|
if (const CodeTextRegion *CTR = R->getAs<CodeTextRegion>())
|
|
|
|
return CTR->getDecl();
|
2009-04-20 13:24:46 +08:00
|
|
|
}
|
|
|
|
|
2009-08-28 12:49:15 +08:00
|
|
|
return NULL;
|
2009-04-20 13:24:46 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// getAsLocSymbol - If this SVal is a location (subclasses Loc) and
|
2009-04-20 13:24:46 +08:00
|
|
|
/// wraps a symbol, return that SymbolRef. Otherwise return 0.
|
|
|
|
// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
|
2009-03-04 06:06:47 +08:00
|
|
|
SymbolRef SVal::getAsLocSymbol() const {
|
|
|
|
if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this)) {
|
2009-11-10 10:17:20 +08:00
|
|
|
const MemRegion *R = X->StripCasts();
|
2009-07-30 05:43:22 +08:00
|
|
|
if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(R))
|
|
|
|
return SymR->getSymbol();
|
2009-03-04 06:06:47 +08:00
|
|
|
}
|
2009-07-30 05:43:22 +08:00
|
|
|
return NULL;
|
2009-03-04 06:06:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getAsSymbol - If this Sval wraps a symbol return that SymbolRef.
|
2009-04-20 13:24:46 +08:00
|
|
|
/// Otherwise return 0.
|
|
|
|
// FIXME: should we consider SymbolRef wrapped in CodeTextRegion?
|
2009-03-04 06:06:47 +08:00
|
|
|
SymbolRef SVal::getAsSymbol() const {
|
|
|
|
if (const nonloc::SymbolVal *X = dyn_cast<nonloc::SymbolVal>(this))
|
|
|
|
return X->getSymbol();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
|
|
|
|
if (SymbolRef Y = dyn_cast<SymbolData>(X->getSymbolicExpression()))
|
|
|
|
return Y;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 06:06:47 +08:00
|
|
|
return getAsLocSymbol();
|
|
|
|
}
|
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
/// getAsSymbolicExpression - If this Sval wraps a symbolic expression then
|
|
|
|
/// return that expression. Otherwise return NULL.
|
|
|
|
const SymExpr *SVal::getAsSymbolicExpression() const {
|
|
|
|
if (const nonloc::SymExprVal *X = dyn_cast<nonloc::SymExprVal>(this))
|
|
|
|
return X->getSymbolicExpression();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
return getAsSymbol();
|
|
|
|
}
|
2009-06-30 19:52:40 +08:00
|
|
|
|
|
|
|
const MemRegion *SVal::getAsRegion() const {
|
|
|
|
if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(this))
|
|
|
|
return X->getRegion();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-26 11:35:11 +08:00
|
|
|
|
2009-11-10 10:17:20 +08:00
|
|
|
const MemRegion *loc::MemRegionVal::StripCasts() const {
|
2009-07-30 02:14:27 +08:00
|
|
|
const MemRegion *R = getRegion();
|
2009-11-10 10:17:20 +08:00
|
|
|
return R ? R->StripCasts() : NULL;
|
2009-07-30 02:14:27 +08:00
|
|
|
}
|
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
bool SVal::symbol_iterator::operator==(const symbol_iterator &X) const {
|
|
|
|
return itr == X.itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SVal::symbol_iterator::operator!=(const symbol_iterator &X) const {
|
|
|
|
return itr != X.itr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SVal::symbol_iterator::symbol_iterator(const SymExpr *SE) {
|
|
|
|
itr.push_back(SE);
|
2009-09-09 23:08:12 +08:00
|
|
|
while (!isa<SymbolData>(itr.back())) expand();
|
2009-03-26 11:35:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SVal::symbol_iterator& SVal::symbol_iterator::operator++() {
|
|
|
|
assert(!itr.empty() && "attempting to iterate on an 'end' iterator");
|
|
|
|
assert(isa<SymbolData>(itr.back()));
|
2009-09-09 23:08:12 +08:00
|
|
|
itr.pop_back();
|
2009-03-26 11:35:11 +08:00
|
|
|
if (!itr.empty())
|
|
|
|
while (!isa<SymbolData>(itr.back())) expand();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SymbolRef SVal::symbol_iterator::operator*() {
|
|
|
|
assert(!itr.empty() && "attempting to dereference an 'end' iterator");
|
|
|
|
return cast<SymbolData>(itr.back());
|
|
|
|
}
|
|
|
|
|
|
|
|
void SVal::symbol_iterator::expand() {
|
|
|
|
const SymExpr *SE = itr.back();
|
|
|
|
itr.pop_back();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
|
|
|
|
itr.push_back(SIE->getLHS());
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-03-26 11:35:11 +08:00
|
|
|
else if (const SymSymExpr *SSE = dyn_cast<SymSymExpr>(SE)) {
|
|
|
|
itr.push_back(SSE->getLHS());
|
|
|
|
itr.push_back(SSE->getRHS());
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-26 11:35:11 +08:00
|
|
|
assert(false && "unhandled expansion case");
|
|
|
|
}
|
|
|
|
|
2009-08-06 09:20:57 +08:00
|
|
|
const GRState *nonloc::LazyCompoundVal::getState() const {
|
|
|
|
return static_cast<const LazyCompoundValData*>(Data)->getState();
|
|
|
|
}
|
|
|
|
|
|
|
|
const TypedRegion *nonloc::LazyCompoundVal::getRegion() const {
|
|
|
|
return static_cast<const LazyCompoundValData*>(Data)->getRegion();
|
|
|
|
}
|
|
|
|
|
2008-10-31 02:01:28 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Other Iterators.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
nonloc::CompoundVal::iterator nonloc::CompoundVal::begin() const {
|
|
|
|
return getValue()->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
nonloc::CompoundVal::iterator nonloc::CompoundVal::end() const {
|
|
|
|
return getValue()->end();
|
|
|
|
}
|
|
|
|
|
2008-07-18 23:54:51 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Useful predicates.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-11-09 14:52:44 +08:00
|
|
|
bool SVal::isConstant() const {
|
|
|
|
return isa<nonloc::ConcreteInt>(this) || isa<loc::ConcreteInt>(this);
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
bool SVal::isZeroConstant() const {
|
|
|
|
if (isa<loc::ConcreteInt>(*this))
|
|
|
|
return cast<loc::ConcreteInt>(*this).getValue() == 0;
|
|
|
|
else if (isa<nonloc::ConcreteInt>(*this))
|
|
|
|
return cast<nonloc::ConcreteInt>(*this).getValue() == 0;
|
2008-07-18 23:54:51 +08:00
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-07 06:50:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-10-17 13:57:07 +08:00
|
|
|
// Transfer function dispatch for Non-Locs.
|
2008-02-07 06:50:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-26 08:05:51 +08:00
|
|
|
SVal nonloc::ConcreteInt::evalBinOp(ValueManager &ValMgr,
|
|
|
|
BinaryOperator::Opcode Op,
|
2009-09-09 23:08:12 +08:00
|
|
|
const nonloc::ConcreteInt& R) const {
|
2008-07-18 23:59:33 +08:00
|
|
|
const llvm::APSInt* X =
|
2009-06-26 08:05:51 +08:00
|
|
|
ValMgr.getBasicValueFactory().EvaluateAPSInt(Op, getValue(), R.getValue());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
if (X)
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::ConcreteInt(*X);
|
2008-02-29 04:32:03 +08:00
|
|
|
else
|
|
|
|
return UndefinedVal();
|
2008-02-07 06:50:25 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
nonloc::ConcreteInt
|
2009-06-26 08:05:51 +08:00
|
|
|
nonloc::ConcreteInt::evalComplement(ValueManager &ValMgr) const {
|
|
|
|
return ValMgr.makeIntVal(~getValue());
|
2008-02-07 06:50:25 +08:00
|
|
|
}
|
|
|
|
|
2009-06-26 08:05:51 +08:00
|
|
|
nonloc::ConcreteInt nonloc::ConcreteInt::evalMinus(ValueManager &ValMgr) const {
|
|
|
|
return ValMgr.makeIntVal(-getValue());
|
2008-02-05 00:58:30 +08:00
|
|
|
}
|
|
|
|
|
2008-02-07 06:50:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-10-17 13:57:07 +08:00
|
|
|
// Transfer function dispatch for Locs.
|
2008-02-07 06:50:25 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-02-05 00:58:30 +08:00
|
|
|
|
2008-10-31 01:53:23 +08:00
|
|
|
SVal loc::ConcreteInt::EvalBinOp(BasicValueFactory& BasicVals,
|
|
|
|
BinaryOperator::Opcode Op,
|
|
|
|
const loc::ConcreteInt& R) const {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-07 06:50:25 +08:00
|
|
|
assert (Op == BinaryOperator::Add || Op == BinaryOperator::Sub ||
|
|
|
|
(Op >= BinaryOperator::LT && Op <= BinaryOperator::NE));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-08 04:13:31 +08:00
|
|
|
const llvm::APSInt* X = BasicVals.EvaluateAPSInt(Op, getValue(), R.getValue());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-02-29 04:32:03 +08:00
|
|
|
if (X)
|
2008-10-17 13:57:07 +08:00
|
|
|
return loc::ConcreteInt(*X);
|
2008-02-29 04:32:03 +08:00
|
|
|
else
|
|
|
|
return UndefinedVal();
|
2008-02-01 03:34:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Pretty-Printing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-14 07:53:06 +08:00
|
|
|
void SVal::dump() const { dumpToStream(llvm::errs()); }
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2009-07-14 07:53:06 +08:00
|
|
|
void SVal::dumpToStream(llvm::raw_ostream& os) const {
|
2009-09-09 23:08:12 +08:00
|
|
|
switch (getBaseKind()) {
|
2008-10-24 14:00:12 +08:00
|
|
|
case UnknownKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << "Invalid";
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
case NonLocKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
cast<NonLoc>(this)->dumpToStream(os);
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
case LocKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
cast<Loc>(this)->dumpToStream(os);
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
case UndefinedKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << "Undefined";
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
default:
|
|
|
|
assert (false && "Invalid SVal.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-14 07:53:06 +08:00
|
|
|
void NonLoc::dumpToStream(llvm::raw_ostream& os) const {
|
2009-09-09 23:08:12 +08:00
|
|
|
switch (getSubKind()) {
|
2008-10-24 14:00:12 +08:00
|
|
|
case nonloc::ConcreteIntKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << cast<nonloc::ConcreteInt>(this)->getValue().getZExtValue();
|
2008-10-24 14:00:12 +08:00
|
|
|
if (cast<nonloc::ConcreteInt>(this)->getValue().isUnsigned())
|
2009-09-09 23:08:12 +08:00
|
|
|
os << 'U';
|
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
case nonloc::SymbolValKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << '$' << cast<nonloc::SymbolVal>(this)->getSymbol();
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2009-03-26 11:35:11 +08:00
|
|
|
case nonloc::SymExprValKind: {
|
|
|
|
const nonloc::SymExprVal& C = *cast<nonloc::SymExprVal>(this);
|
|
|
|
const SymExpr *SE = C.getSymbolicExpression();
|
2009-07-14 07:53:06 +08:00
|
|
|
os << SE;
|
2008-10-24 14:00:12 +08:00
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-10-24 14:00:12 +08:00
|
|
|
case nonloc::LocAsIntegerKind: {
|
|
|
|
const nonloc::LocAsInteger& C = *cast<nonloc::LocAsInteger>(this);
|
2009-07-14 07:53:06 +08:00
|
|
|
os << C.getLoc() << " [as " << C.getNumBits() << " bit integer]";
|
2008-10-24 14:00:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2008-10-31 02:01:28 +08:00
|
|
|
case nonloc::CompoundValKind: {
|
|
|
|
const nonloc::CompoundVal& C = *cast<nonloc::CompoundVal>(this);
|
2009-07-15 04:21:36 +08:00
|
|
|
os << "compoundVal{";
|
2008-10-31 02:35:10 +08:00
|
|
|
bool first = true;
|
|
|
|
for (nonloc::CompoundVal::iterator I=C.begin(), E=C.end(); I!=E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (first) {
|
2009-07-14 07:53:06 +08:00
|
|
|
os << ' '; first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
os << ", ";
|
|
|
|
|
|
|
|
(*I).dumpToStream(os);
|
2008-10-31 02:35:10 +08:00
|
|
|
}
|
2009-07-15 04:21:36 +08:00
|
|
|
os << "}";
|
2008-10-31 02:01:28 +08:00
|
|
|
break;
|
2009-08-06 09:20:57 +08:00
|
|
|
}
|
|
|
|
case nonloc::LazyCompoundValKind: {
|
|
|
|
const nonloc::LazyCompoundVal &C = *cast<nonloc::LazyCompoundVal>(this);
|
|
|
|
os << "lazyCompoundVal{" << (void*) C.getState() << ',' << C.getRegion()
|
|
|
|
<< '}';
|
|
|
|
break;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2008-10-24 14:00:12 +08:00
|
|
|
default:
|
|
|
|
assert (false && "Pretty-printed not implemented for this NonLoc.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void Loc::dumpToStream(llvm::raw_ostream& os) const {
|
|
|
|
switch (getSubKind()) {
|
2008-10-24 14:00:12 +08:00
|
|
|
case loc::ConcreteIntKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << cast<loc::ConcreteInt>(this)->getValue().getZExtValue() << " (Loc)";
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
case loc::GotoLabelKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << "&&" << cast<loc::GotoLabel>(this)->getLabel()->getID()->getName();
|
2008-10-24 14:00:12 +08:00
|
|
|
break;
|
|
|
|
case loc::MemRegionKind:
|
2009-07-14 07:53:06 +08:00
|
|
|
os << '&' << cast<loc::MemRegionVal>(this)->getRegion()->getString();
|
2009-09-09 23:08:12 +08:00
|
|
|
break;
|
2008-10-24 14:00:12 +08:00
|
|
|
default:
|
2009-07-14 07:53:06 +08:00
|
|
|
assert(false && "Pretty-printing not implemented for this Loc.");
|
2008-10-24 14:00:12 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|