2010-12-02 05:28:31 +08:00
|
|
|
// SValBuilder.cpp - Basic class for all SValBuilder implementations -*- C++ -*-
|
2009-07-22 05:03:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-12-02 05:28:31 +08:00
|
|
|
// This file defines SValBuilder, the base class for all (complete) SValBuilder
|
2009-07-22 05:03:30 +08:00
|
|
|
// implementations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-01-28 20:06:22 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
|
2009-07-22 05:03:30 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-07-22 05:03:30 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Basic SVal creation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-12-20 10:48:34 +08:00
|
|
|
void SValBuilder::anchor() { }
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) {
|
|
|
|
if (Loc::isLocType(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return makeNull();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (type->isIntegerType())
|
|
|
|
return makeIntVal(0, type);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
// FIXME: Handle floats.
|
|
|
|
// FIXME: Handle structs.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
|
2011-03-01 08:45:32 +08:00
|
|
|
const llvm::APSInt& rhs, QualType type) {
|
2010-12-02 15:49:45 +08:00
|
|
|
// The Environment ensures we always get a persistent APSInt in
|
|
|
|
// BasicValueFactory, so we don't need to get the APSInt from
|
|
|
|
// BasicValueFactory again.
|
2011-11-18 07:07:28 +08:00
|
|
|
assert(lhs);
|
2011-03-01 08:45:32 +08:00
|
|
|
assert(!Loc::isLocType(type));
|
2011-12-06 02:58:30 +08:00
|
|
|
return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2011-12-11 07:36:51 +08:00
|
|
|
NonLoc SValBuilder::makeNonLoc(const llvm::APSInt& lhs,
|
|
|
|
BinaryOperator::Opcode op, const SymExpr *rhs,
|
|
|
|
QualType type) {
|
|
|
|
assert(rhs);
|
|
|
|
assert(!Loc::isLocType(type));
|
|
|
|
return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type));
|
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op,
|
2011-03-01 08:45:32 +08:00
|
|
|
const SymExpr *rhs, QualType type) {
|
2011-11-18 07:07:28 +08:00
|
|
|
assert(lhs && rhs);
|
2011-12-09 11:34:02 +08:00
|
|
|
assert(haveSameType(lhs->getType(Context), rhs->getType(Context)) == true);
|
2011-03-01 08:45:32 +08:00
|
|
|
assert(!Loc::isLocType(type));
|
2011-12-06 02:58:30 +08:00
|
|
|
return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2011-12-07 07:12:27 +08:00
|
|
|
NonLoc SValBuilder::makeNonLoc(const SymExpr *operand,
|
|
|
|
QualType fromTy, QualType toTy) {
|
|
|
|
assert(operand);
|
|
|
|
assert(!Loc::isLocType(toTy));
|
|
|
|
return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy));
|
|
|
|
}
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal SValBuilder::convertToArrayIndex(SVal val) {
|
|
|
|
if (val.isUnknownOrUndef())
|
|
|
|
return val;
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
// Common case: we have an appropriately sized integer.
|
2011-03-01 08:45:32 +08:00
|
|
|
if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&val)) {
|
2010-12-02 15:49:45 +08:00
|
|
|
const llvm::APSInt& I = CI->getValue();
|
|
|
|
if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
|
2011-03-01 08:45:32 +08:00
|
|
|
return val;
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromNonLoc(cast<NonLoc>(val), ArrayIndexTy);
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2012-01-28 20:06:22 +08:00
|
|
|
nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){
|
|
|
|
return makeTruthVal(boolean->getValue());
|
|
|
|
}
|
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
DefinedOrUnknownSVal
|
2011-08-13 04:02:48 +08:00
|
|
|
SValBuilder::getRegionValueSymbolVal(const TypedValueRegion* region) {
|
2011-03-01 08:45:32 +08:00
|
|
|
QualType T = region->getValueType();
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym = SymMgr.getRegionValueSymbol(region);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(T))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2012-03-10 09:34:17 +08:00
|
|
|
DefinedOrUnknownSVal
|
|
|
|
SValBuilder::getConjuredSymbolVal(const void *symbolTag,
|
|
|
|
const Expr *expr,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
unsigned count) {
|
2011-03-01 08:45:32 +08:00
|
|
|
QualType T = expr->getType();
|
2012-02-18 07:13:45 +08:00
|
|
|
return getConjuredSymbolVal(symbolTag, expr, LCtx, T, count);
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2012-03-10 09:34:17 +08:00
|
|
|
DefinedOrUnknownSVal
|
|
|
|
SValBuilder::getConjuredSymbolVal(const void *symbolTag,
|
|
|
|
const Expr *expr,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
QualType type,
|
|
|
|
unsigned count) {
|
2011-03-01 08:45:32 +08:00
|
|
|
if (!SymbolManager::canSymbolicate(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2012-02-18 07:13:45 +08:00
|
|
|
SymbolRef sym = SymMgr.getConjuredSymbol(expr, LCtx, type, count, symbolTag);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (Loc::isLocType(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2012-03-10 09:34:17 +08:00
|
|
|
|
|
|
|
DefinedOrUnknownSVal
|
|
|
|
SValBuilder::getConjuredSymbolVal(const Stmt *stmt,
|
|
|
|
const LocationContext *LCtx,
|
|
|
|
QualType type,
|
|
|
|
unsigned visitCount) {
|
|
|
|
if (!SymbolManager::canSymbolicate(type))
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
SymbolRef sym = SymMgr.getConjuredSymbol(stmt, LCtx, type, visitCount);
|
|
|
|
|
|
|
|
if (Loc::isLocType(type))
|
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
|
|
|
|
const MemRegion *region,
|
|
|
|
const Expr *expr, QualType type,
|
|
|
|
unsigned count) {
|
|
|
|
assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym =
|
|
|
|
SymMgr.getMetadataSymbol(region, expr, type, count, symbolTag);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (Loc::isLocType(type))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
DefinedOrUnknownSVal
|
|
|
|
SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
|
2011-08-13 04:02:48 +08:00
|
|
|
const TypedValueRegion *region) {
|
2011-03-01 08:45:32 +08:00
|
|
|
QualType T = region->getValueType();
|
2010-12-02 15:49:45 +08:00
|
|
|
|
|
|
|
if (!SymbolManager::canSymbolicate(T))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
|
2010-12-02 15:49:45 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(T))
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
|
|
|
|
|
|
|
|
return nonloc::SymbolVal(sym);
|
|
|
|
}
|
|
|
|
|
2011-08-13 07:37:29 +08:00
|
|
|
DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) {
|
2011-03-01 08:45:32 +08:00
|
|
|
return loc::MemRegionVal(MemMgr.getFunctionTextRegion(func));
|
2010-12-02 15:49:45 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
|
|
|
|
CanQualType locTy,
|
|
|
|
const LocationContext *locContext) {
|
2010-12-02 15:49:45 +08:00
|
|
|
const BlockTextRegion *BC =
|
2011-10-24 09:32:45 +08:00
|
|
|
MemMgr.getBlockTextRegion(block, locTy, locContext->getAnalysisDeclContext());
|
2011-03-01 08:45:32 +08:00
|
|
|
const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext);
|
2010-12-02 15:49:45 +08:00
|
|
|
return loc::MemRegionVal(BD);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-08-26 02:44:25 +08:00
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal SValBuilder::makeGenericVal(ProgramStateRef State,
|
2011-11-18 07:07:28 +08:00
|
|
|
BinaryOperator::Opcode Op,
|
|
|
|
NonLoc LHS, NonLoc RHS,
|
|
|
|
QualType ResultTy) {
|
|
|
|
// If operands are tainted, create a symbol to ensure that we propagate taint.
|
|
|
|
if (State->isTainted(RHS) || State->isTainted(LHS)) {
|
|
|
|
const SymExpr *symLHS;
|
|
|
|
const SymExpr *symRHS;
|
|
|
|
|
|
|
|
if (const nonloc::ConcreteInt *rInt = dyn_cast<nonloc::ConcreteInt>(&RHS)) {
|
|
|
|
symLHS = LHS.getAsSymExpr();
|
|
|
|
return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
|
|
|
|
}
|
|
|
|
|
2011-12-11 07:36:51 +08:00
|
|
|
if (const nonloc::ConcreteInt *lInt = dyn_cast<nonloc::ConcreteInt>(&LHS)) {
|
|
|
|
symRHS = RHS.getAsSymExpr();
|
2011-12-12 02:43:40 +08:00
|
|
|
return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
|
2011-12-11 07:36:51 +08:00
|
|
|
}
|
|
|
|
|
2011-11-18 07:07:28 +08:00
|
|
|
symLHS = LHS.getAsSymExpr();
|
|
|
|
symRHS = RHS.getAsSymExpr();
|
|
|
|
return makeNonLoc(symLHS, Op, symRHS, ResultTy);
|
|
|
|
}
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
|
2011-03-01 08:45:32 +08:00
|
|
|
SVal lhs, SVal rhs, QualType type) {
|
2009-08-26 02:44:25 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (lhs.isUndef() || rhs.isUndef())
|
2009-08-26 02:44:25 +08:00
|
|
|
return UndefinedVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (lhs.isUnknown() || rhs.isUnknown())
|
2009-08-26 02:44:25 +08:00
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (isa<Loc>(lhs)) {
|
|
|
|
if (isa<Loc>(rhs))
|
|
|
|
return evalBinOpLL(state, op, cast<Loc>(lhs), cast<Loc>(rhs), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalBinOpLN(state, op, cast<Loc>(lhs), cast<NonLoc>(rhs), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
if (isa<Loc>(rhs)) {
|
2010-06-28 16:26:15 +08:00
|
|
|
// Support pointer arithmetic where the addend is on the left
|
|
|
|
// and the pointer on the right.
|
2011-03-01 08:45:32 +08:00
|
|
|
assert(op == BO_Add);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 02:44:25 +08:00
|
|
|
// Commute the operands.
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalBinOpLN(state, op, cast<Loc>(rhs), cast<NonLoc>(lhs), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
|
|
|
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalBinOpNN(state, op, cast<NonLoc>(lhs), cast<NonLoc>(rhs), type);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
|
|
|
|
2012-01-27 05:29:00 +08:00
|
|
|
DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state,
|
2011-03-01 08:45:32 +08:00
|
|
|
DefinedOrUnknownSVal lhs,
|
|
|
|
DefinedOrUnknownSVal rhs) {
|
|
|
|
return cast<DefinedOrUnknownSVal>(evalBinOp(state, BO_EQ, lhs, rhs,
|
2010-12-02 15:49:45 +08:00
|
|
|
Context.IntTy));
|
2009-09-12 06:07:28 +08:00
|
|
|
}
|
|
|
|
|
2012-01-13 08:56:55 +08:00
|
|
|
/// Recursively check if the pointer types are equal modulo const, volatile,
|
|
|
|
/// and restrict qualifiers. Assumes the input types are canonical.
|
|
|
|
/// TODO: This is based off of code in SemaCast; can we reuse it.
|
|
|
|
static bool haveSimilarTypes(ASTContext &Context, QualType T1,
|
|
|
|
QualType T2) {
|
|
|
|
while (Context.UnwrapSimilarPointerTypes(T1, T2)) {
|
|
|
|
Qualifiers Quals1, Quals2;
|
|
|
|
T1 = Context.getUnqualifiedArrayType(T1, Quals1);
|
|
|
|
T2 = Context.getUnqualifiedArrayType(T2, Quals2);
|
|
|
|
|
|
|
|
// Make sure that non cvr-qualifiers the other qualifiers (e.g., address
|
|
|
|
// spaces) are identical.
|
|
|
|
Quals1.removeCVRQualifiers();
|
|
|
|
Quals2.removeCVRQualifiers();
|
|
|
|
if (Quals1 != Quals2)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (T1 != T2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-11-26 15:15:40 +08:00
|
|
|
// FIXME: should rewrite according to the cast kind.
|
2010-12-02 05:57:22 +08:00
|
|
|
SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) {
|
2012-01-13 08:56:55 +08:00
|
|
|
castTy = Context.getCanonicalType(castTy);
|
|
|
|
originalTy = Context.getCanonicalType(originalTy);
|
2009-07-22 05:03:30 +08:00
|
|
|
if (val.isUnknownOrUndef() || castTy == originalTy)
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// For const casts, just propagate the value.
|
2010-01-05 17:27:03 +08:00
|
|
|
if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
|
2012-01-13 08:56:55 +08:00
|
|
|
if (haveSimilarTypes(Context, Context.getPointerType(castTy),
|
|
|
|
Context.getPointerType(originalTy)))
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2010-02-03 05:11:40 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from pointers to integers.
|
2011-02-17 05:13:32 +08:00
|
|
|
if (castTy->isIntegerType() && Loc::isLocType(originalTy))
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromLoc(cast<Loc>(val), castTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from integers to pointers.
|
2011-02-17 05:13:32 +08:00
|
|
|
if (Loc::isLocType(castTy) && originalTy->isIntegerType()) {
|
2009-07-22 05:03:30 +08:00
|
|
|
if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
|
2009-12-23 10:52:14 +08:00
|
|
|
if (const MemRegion *R = LV->getLoc().getAsRegion()) {
|
2010-12-02 15:49:45 +08:00
|
|
|
StoreManager &storeMgr = StateMgr.getStoreManager();
|
2011-02-12 03:48:15 +08:00
|
|
|
R = storeMgr.castRegion(R, castTy);
|
2010-02-04 12:56:43 +08:00
|
|
|
return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
|
2009-12-23 10:52:14 +08:00
|
|
|
}
|
2010-02-04 12:56:43 +08:00
|
|
|
return LV->getLoc();
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2011-12-07 07:12:27 +08:00
|
|
|
return dispatchCast(val, castTy);
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Just pass through function and block pointers.
|
|
|
|
if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
|
2011-02-17 05:13:32 +08:00
|
|
|
assert(Loc::isLocType(castTy));
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from array type to another type.
|
|
|
|
if (originalTy->isArrayType()) {
|
|
|
|
// We will always decay to a pointer.
|
2010-12-02 15:49:45 +08:00
|
|
|
val = StateMgr.ArrayToPointer(cast<Loc>(val));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Are we casting from an array to a pointer? If so just pass on
|
|
|
|
// the decayed value.
|
|
|
|
if (castTy->isPointerType())
|
2010-02-04 12:56:43 +08:00
|
|
|
return val;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Are we casting from an array to an integer? If so, cast the decayed
|
|
|
|
// pointer value to an integer.
|
|
|
|
assert(castTy->isIntegerType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// FIXME: Keep these here for now in case we decide soon that we
|
|
|
|
// need the original decayed type.
|
|
|
|
// QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
|
|
|
|
// QualType pointerTy = C.getPointerType(elemTy);
|
2011-03-01 08:45:32 +08:00
|
|
|
return evalCastFromLoc(cast<Loc>(val), castTy);
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from a region to a specific type.
|
|
|
|
if (const MemRegion *R = val.getAsRegion()) {
|
|
|
|
// FIXME: We should handle the case where we strip off view layers to get
|
|
|
|
// to a desugared type.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
if (!Loc::isLocType(castTy)) {
|
2010-11-16 04:09:42 +08:00
|
|
|
// FIXME: There can be gross cases where one casts the result of a function
|
|
|
|
// (that returns a pointer) to some other value that happens to fit
|
|
|
|
// within that pointer value. We currently have no good way to
|
|
|
|
// model such operations. When this happens, the underlying operation
|
|
|
|
// is that the caller is reasoning about bits. Conceptually we are
|
|
|
|
// layering a "view" of a location on top of those bits. Perhaps
|
|
|
|
// we need to be more lazy about mutual possible views, even on an
|
|
|
|
// SVal? This may be necessary for bit-level reasoning as well.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// We get a symbolic function pointer for a dereference of a function
|
|
|
|
// pointer, but it is of function type. Example:
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// struct FPRec {
|
2009-09-09 23:08:12 +08:00
|
|
|
// void (*my_func)(int * x);
|
2009-07-22 05:03:30 +08:00
|
|
|
// };
|
|
|
|
//
|
|
|
|
// int bar(int x);
|
|
|
|
//
|
|
|
|
// int f1_a(struct FPRec* foo) {
|
|
|
|
// int x;
|
|
|
|
// (*foo->my_func)(&x);
|
|
|
|
// return bar(x)+1; // no-warning
|
|
|
|
// }
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-17 05:13:32 +08:00
|
|
|
assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() ||
|
2011-02-19 16:03:18 +08:00
|
|
|
originalTy->isBlockPointerType() || castTy->isReferenceType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-12-02 15:49:45 +08:00
|
|
|
StoreManager &storeMgr = StateMgr.getStoreManager();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
// Delegate to store manager to get the result of casting a region to a
|
|
|
|
// different type. If the MemRegion* returned is NULL, this expression
|
2010-12-02 05:57:22 +08:00
|
|
|
// Evaluates to UnknownVal.
|
2011-02-12 03:48:15 +08:00
|
|
|
R = storeMgr.castRegion(R, castTy);
|
2010-02-04 12:56:43 +08:00
|
|
|
return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-07 07:12:27 +08:00
|
|
|
return dispatchCast(val, castTy);
|
2009-09-12 06:07:28 +08:00
|
|
|
}
|