2010-12-01 21:28:31 +00:00
|
|
|
// SValBuilder.cpp - Basic class for all SValBuilder implementations -*- C++ -*-
|
2009-07-21 21:03:30 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2010-12-01 21:28:31 +00:00
|
|
|
// This file defines SValBuilder, the base class for all (complete) SValBuilder
|
2009-07-21 21:03:30 +00:00
|
|
|
// implementations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-01 21:28:31 +00:00
|
|
|
#include "clang/Checker/PathSensitive/SValBuilder.h"
|
2010-01-25 04:41:41 +00:00
|
|
|
#include "clang/Checker/PathSensitive/GRState.h"
|
2009-07-21 21:03:30 +00:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2009-08-25 18:44:25 +00:00
|
|
|
|
2010-12-01 21:57:22 +00:00
|
|
|
SVal SValBuilder::evalBinOp(const GRState *ST, BinaryOperator::Opcode Op,
|
2009-08-25 18:44:25 +00:00
|
|
|
SVal L, SVal R, QualType T) {
|
|
|
|
|
|
|
|
if (L.isUndef() || R.isUndef())
|
|
|
|
return UndefinedVal();
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-08-25 18:44:25 +00:00
|
|
|
if (L.isUnknown() || R.isUnknown())
|
|
|
|
return UnknownVal();
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-08-25 18:44:25 +00:00
|
|
|
if (isa<Loc>(L)) {
|
|
|
|
if (isa<Loc>(R))
|
2010-12-01 21:57:22 +00:00
|
|
|
return evalBinOpLL(ST, Op, cast<Loc>(L), cast<Loc>(R), T);
|
2009-08-25 18:44:25 +00:00
|
|
|
|
2010-12-01 21:57:22 +00:00
|
|
|
return evalBinOpLN(ST, Op, cast<Loc>(L), cast<NonLoc>(R), T);
|
2009-08-25 18:44:25 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-08-25 18:44:25 +00:00
|
|
|
if (isa<Loc>(R)) {
|
2010-06-28 08:26:15 +00:00
|
|
|
// Support pointer arithmetic where the addend is on the left
|
|
|
|
// and the pointer on the right.
|
2010-08-25 11:45:40 +00:00
|
|
|
assert(Op == BO_Add);
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-08-25 18:44:25 +00:00
|
|
|
// Commute the operands.
|
2010-12-01 21:57:22 +00:00
|
|
|
return evalBinOpLN(ST, Op, cast<Loc>(R), cast<NonLoc>(L), T);
|
2009-08-25 18:44:25 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 21:57:22 +00:00
|
|
|
return evalBinOpNN(ST, Op, cast<NonLoc>(L), cast<NonLoc>(R), T);
|
2009-08-25 18:44:25 +00:00
|
|
|
}
|
|
|
|
|
2010-12-01 21:57:22 +00:00
|
|
|
DefinedOrUnknownSVal SValBuilder::evalEQ(const GRState *ST,
|
2009-09-11 22:07:28 +00:00
|
|
|
DefinedOrUnknownSVal L,
|
|
|
|
DefinedOrUnknownSVal R) {
|
2010-12-01 21:57:22 +00:00
|
|
|
return cast<DefinedOrUnknownSVal>(evalBinOp(ST, BO_EQ, L, R,
|
2009-09-11 22:07:28 +00:00
|
|
|
ValMgr.getContext().IntTy));
|
|
|
|
}
|
|
|
|
|
2010-11-26 07:15:40 +00:00
|
|
|
// FIXME: should rewrite according to the cast kind.
|
2010-12-01 21:57:22 +00:00
|
|
|
SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) {
|
2009-07-21 21:03:30 +00:00
|
|
|
if (val.isUnknownOrUndef() || castTy == originalTy)
|
2010-02-04 04:56:43 +00:00
|
|
|
return val;
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
ASTContext &C = ValMgr.getContext();
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
// For const casts, just propagate the value.
|
2010-01-05 09:27:03 +00:00
|
|
|
if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
|
|
|
|
if (C.hasSameUnqualifiedType(castTy, originalTy))
|
2010-02-04 04:56:43 +00:00
|
|
|
return val;
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2010-02-02 21:11:40 +00:00
|
|
|
// Check for casts to real or complex numbers. We don't handle these at all
|
|
|
|
// right now.
|
|
|
|
if (castTy->isFloatingType() || castTy->isAnyComplexType())
|
2010-02-04 04:56:43 +00:00
|
|
|
return UnknownVal();
|
2010-02-02 21:11:40 +00:00
|
|
|
|
|
|
|
// Check for casts from integers to integers.
|
2010-01-14 03:45:06 +00:00
|
|
|
if (castTy->isIntegerType() && originalTy->isIntegerType())
|
2010-12-01 21:57:22 +00:00
|
|
|
return evalCastNL(cast<NonLoc>(val), castTy);
|
2010-01-14 03:45:06 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
// Check for casts from pointers to integers.
|
|
|
|
if (castTy->isIntegerType() && Loc::IsLocType(originalTy))
|
2010-12-01 21:57:22 +00:00
|
|
|
return evalCastL(cast<Loc>(val), castTy);
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
// Check for casts from integers to pointers.
|
|
|
|
if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) {
|
|
|
|
if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
|
2009-12-23 02:52:14 +00:00
|
|
|
if (const MemRegion *R = LV->getLoc().getAsRegion()) {
|
|
|
|
StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
|
|
|
|
R = storeMgr.CastRegion(R, castTy);
|
2010-02-04 04:56:43 +00:00
|
|
|
return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
|
2009-12-23 02:52:14 +00:00
|
|
|
}
|
2010-02-04 04:56:43 +00:00
|
|
|
return LV->getLoc();
|
2009-07-21 21:03:30 +00:00
|
|
|
}
|
|
|
|
goto DispatchCast;
|
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
// Just pass through function and block pointers.
|
|
|
|
if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
|
|
|
|
assert(Loc::IsLocType(castTy));
|
2010-02-04 04:56:43 +00:00
|
|
|
return val;
|
2009-07-21 21:03:30 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00: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 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
// Are we casting from an array to a pointer? If so just pass on
|
|
|
|
// the decayed value.
|
|
|
|
if (castTy->isPointerType())
|
2010-02-04 04:56:43 +00:00
|
|
|
return val;
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00: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 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00: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);
|
2010-12-01 21:57:22 +00:00
|
|
|
return evalCastL(cast<Loc>(val), castTy);
|
2009-07-21 21:03:30 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00: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 15:08:12 +00:00
|
|
|
|
2010-11-15 20:09:42 +00:00
|
|
|
if (!Loc::IsLocType(castTy)) {
|
|
|
|
// 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-21 21:03:30 +00:00
|
|
|
// We get a symbolic function pointer for a dereference of a function
|
|
|
|
// pointer, but it is of function type. Example:
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
// struct FPRec {
|
2009-09-09 15:08:12 +00:00
|
|
|
// void (*my_func)(int * x);
|
2009-07-21 21:03:30 +00: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 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() ||
|
|
|
|
originalTy->isBlockPointerType());
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-10-14 06:55:01 +00: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-01 21:57:22 +00:00
|
|
|
// Evaluates to UnknownVal.
|
2009-10-14 06:55:01 +00:00
|
|
|
R = storeMgr.CastRegion(R, castTy);
|
2010-02-04 04:56:43 +00:00
|
|
|
return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
|
2009-07-21 21:03:30 +00:00
|
|
|
}
|
2009-09-09 15:08:12 +00:00
|
|
|
|
2009-07-21 21:03:30 +00:00
|
|
|
DispatchCast:
|
2009-12-23 02:52:14 +00:00
|
|
|
// All other cases.
|
2010-12-01 21:57:22 +00:00
|
|
|
return isa<Loc>(val) ? evalCastL(cast<Loc>(val), castTy)
|
|
|
|
: evalCastNL(cast<NonLoc>(val), castTy);
|
2009-09-11 22:07:28 +00:00
|
|
|
}
|