2009-07-22 05:03:30 +08:00
|
|
|
// SValuator.cpp - Basic class for all SValuator implementations --*- C++ -*--//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines SValuator, the base class for all (complete) SValuator
|
|
|
|
// implementations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Analysis/PathSensitive/SValuator.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/GRState.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-08-26 02:44:25 +08:00
|
|
|
|
|
|
|
SVal SValuator::EvalBinOp(const GRState *ST, BinaryOperator::Opcode Op,
|
|
|
|
SVal L, SVal R, QualType T) {
|
|
|
|
|
|
|
|
if (L.isUndef() || R.isUndef())
|
|
|
|
return UndefinedVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 02:44:25 +08:00
|
|
|
if (L.isUnknown() || R.isUnknown())
|
|
|
|
return UnknownVal();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 02:44:25 +08:00
|
|
|
if (isa<Loc>(L)) {
|
|
|
|
if (isa<Loc>(R))
|
|
|
|
return EvalBinOpLL(Op, cast<Loc>(L), cast<Loc>(R), T);
|
|
|
|
|
|
|
|
return EvalBinOpLN(ST, Op, cast<Loc>(L), cast<NonLoc>(R), T);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 02:44:25 +08:00
|
|
|
if (isa<Loc>(R)) {
|
|
|
|
// Support pointer arithmetic where the increment/decrement operand
|
2009-09-09 23:08:12 +08:00
|
|
|
// is on the left and the pointer on the right.
|
2009-08-26 02:44:25 +08:00
|
|
|
assert(Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-26 02:44:25 +08:00
|
|
|
// Commute the operands.
|
|
|
|
return EvalBinOpLN(ST, Op, cast<Loc>(R), cast<NonLoc>(L), T);
|
|
|
|
}
|
|
|
|
|
2009-10-06 09:39:48 +08:00
|
|
|
return EvalBinOpNN(ST, Op, cast<NonLoc>(L), cast<NonLoc>(R), T);
|
2009-08-26 02:44:25 +08:00
|
|
|
}
|
|
|
|
|
2009-09-12 06:07:28 +08:00
|
|
|
DefinedOrUnknownSVal SValuator::EvalEQ(const GRState *ST,
|
|
|
|
DefinedOrUnknownSVal L,
|
|
|
|
DefinedOrUnknownSVal R) {
|
|
|
|
return cast<DefinedOrUnknownSVal>(EvalBinOp(ST, BinaryOperator::EQ, L, R,
|
|
|
|
ValMgr.getContext().IntTy));
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
SValuator::CastResult SValuator::EvalCast(SVal val, const GRState *state,
|
2009-07-22 05:03:30 +08:00
|
|
|
QualType castTy, QualType originalTy){
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
if (val.isUnknownOrUndef() || castTy == originalTy)
|
|
|
|
return CastResult(state, val);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
ASTContext &C = ValMgr.getContext();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// For const casts, just propagate the value.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (C.getCanonicalType(castTy).getUnqualifiedType() ==
|
2009-07-22 05:03:30 +08:00
|
|
|
C.getCanonicalType(originalTy).getUnqualifiedType())
|
|
|
|
return CastResult(state, val);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
// Check for casts from pointers to integers.
|
|
|
|
if (castTy->isIntegerType() && Loc::IsLocType(originalTy))
|
|
|
|
return CastResult(state, EvalCastL(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.
|
|
|
|
if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) {
|
|
|
|
if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
|
|
|
|
// Just unpackage the lval and return it.
|
|
|
|
return CastResult(state, LV->getLoc());
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
goto DispatchCast;
|
|
|
|
}
|
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()) {
|
|
|
|
assert(Loc::IsLocType(castTy));
|
|
|
|
return CastResult(state, val);
|
|
|
|
}
|
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.
|
|
|
|
val = ValMgr.getStateManager().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())
|
|
|
|
return CastResult(state, 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);
|
|
|
|
return CastResult(state, EvalCastL(cast<Loc>(val), castTy));
|
|
|
|
}
|
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
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
assert(Loc::IsLocType(castTy));
|
|
|
|
// 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
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() ||
|
|
|
|
originalTy->isBlockPointerType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
StoreManager &storeMgr = ValMgr.getStateManager().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
|
|
|
|
// evaluates to UnknownVal.
|
|
|
|
R = storeMgr.CastRegion(R, castTy);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-22 05:03:30 +08:00
|
|
|
if (R)
|
2009-10-14 14:55:01 +08:00
|
|
|
return CastResult(state, loc::MemRegionVal(R));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-14 14:55:01 +08:00
|
|
|
return CastResult(state, UnknownVal());
|
2009-07-22 05:03:30 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
// All other cases.
|
2009-07-22 05:03:30 +08:00
|
|
|
DispatchCast:
|
|
|
|
return CastResult(state,
|
2009-09-09 23:08:12 +08:00
|
|
|
isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy)
|
2009-07-22 05:03:30 +08:00
|
|
|
: EvalCastNL(cast<NonLoc>(val), castTy));
|
2009-07-25 07:12:58 +08:00
|
|
|
}
|
2009-09-12 06:07:28 +08:00
|
|
|
|
|
|
|
SValuator::DefinedOrUnknownCastResult
|
|
|
|
SValuator::EvalCast(DefinedOrUnknownSVal V, const GRState *ST,
|
|
|
|
QualType castTy, QualType originalType) {
|
|
|
|
SValuator::CastResult X = EvalCast((SVal) V, ST, castTy, originalType);
|
|
|
|
return DefinedOrUnknownCastResult(X.getState(),
|
|
|
|
cast<DefinedOrUnknownSVal>(X.getSVal()));
|
|
|
|
}
|