2008-02-15 02:28:23 +08:00
|
|
|
// GRSimpleVals.cpp - Transfer functions for tracking simple values -*- C++ -*--
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-06 18:40:09 +08:00
|
|
|
// This file defines GRSimpleVals, a sub-class of GRTransferFuncs that
|
2008-02-15 02:28:23 +08:00
|
|
|
// provides transfer functions for performing simple value tracking with
|
|
|
|
// limited support for symbolics.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "GRSimpleVals.h"
|
2008-03-28 01:17:22 +08:00
|
|
|
#include "BasicObjCFoundationChecks.h"
|
2008-04-03 06:03:53 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-04-01 02:26:32 +08:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
2008-08-13 12:27:00 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRState.h"
|
2008-04-03 12:42:52 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
2008-04-11 06:16:52 +08:00
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
2008-05-01 04:17:27 +08:00
|
|
|
#include "clang/Analysis/PathSensitive/GRExprEngine.h"
|
2008-04-03 12:42:52 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-02-28 01:56:16 +08:00
|
|
|
#include <sstream>
|
2008-02-15 02:28:23 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2008-04-02 13:15:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-11 06:16:52 +08:00
|
|
|
// Transfer Function creation for External clients.
|
2008-04-02 13:15:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-03 12:42:52 +08:00
|
|
|
|
2008-04-11 06:16:52 +08:00
|
|
|
GRTransferFuncs* clang::MakeGRSimpleValsTF() { return new GRSimpleVals(); }
|
2008-02-15 06:36:46 +08:00
|
|
|
|
2008-02-15 02:28:23 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for Casts.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLoc X, QualType T) {
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (!isa<nonloc::ConcreteInt>(X))
|
2008-02-15 02:28:23 +08:00
|
|
|
return UnknownVal();
|
2008-03-12 09:21:45 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
bool isLocType = Loc::IsLocType(T);
|
2008-07-16 08:23:49 +08:00
|
|
|
|
2008-07-16 07:17:54 +08:00
|
|
|
// Only handle casts from integers to integers.
|
2008-10-17 13:57:07 +08:00
|
|
|
if (!isLocType && !T->isIntegerType())
|
2008-07-16 07:17:54 +08:00
|
|
|
return UnknownVal();
|
|
|
|
|
2008-03-12 09:21:45 +08:00
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
2008-02-15 02:28:23 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
llvm::APSInt V = cast<nonloc::ConcreteInt>(X).getValue();
|
|
|
|
V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T));
|
2008-03-12 09:21:45 +08:00
|
|
|
V.extOrTrunc(Eng.getContext().getTypeSize(T));
|
2008-02-15 02:28:23 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isLocType)
|
|
|
|
return loc::ConcreteInt(BasicVals.getValue(V));
|
2008-02-15 02:28:23 +08:00
|
|
|
else
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::ConcreteInt(BasicVals.getValue(V));
|
2008-02-15 02:28:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Casts.
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, Loc X, QualType T) {
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2008-05-01 05:10:19 +08:00
|
|
|
// Casts from pointers -> pointers, just return the lval.
|
|
|
|
//
|
|
|
|
// Casts from pointers -> references, just return the lval. These
|
|
|
|
// can be introduced by the frontend for corner cases, e.g
|
|
|
|
// casting from va_list* to __builtin_va_list&.
|
|
|
|
//
|
2008-11-15 08:20:05 +08:00
|
|
|
assert (!X.isUnknownOrUndef());
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (Loc::IsLocType(T) || T->isReferenceType())
|
2008-02-15 02:28:23 +08:00
|
|
|
return X;
|
|
|
|
|
2008-02-22 02:43:30 +08:00
|
|
|
assert (T->isIntegerType());
|
2008-03-12 09:21:45 +08:00
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
2008-11-15 08:20:05 +08:00
|
|
|
unsigned BitWidth = Eng.getContext().getTypeSize(T);
|
|
|
|
|
|
|
|
if (!isa<loc::ConcreteInt>(X))
|
|
|
|
return nonloc::LocAsInteger::Make(BasicVals, X, BitWidth);
|
2008-03-12 09:21:45 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
llvm::APSInt V = cast<loc::ConcreteInt>(X).getValue();
|
|
|
|
V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T));
|
2008-11-15 08:20:05 +08:00
|
|
|
V.extOrTrunc(BitWidth);
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::ConcreteInt(BasicVals.getValue(V));
|
2008-02-15 02:40:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unary operators.
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLoc X){
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2008-02-15 02:40:24 +08:00
|
|
|
switch (X.getSubKind()) {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case nonloc::ConcreteIntKind:
|
|
|
|
return cast<nonloc::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U);
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-02-15 02:40:24 +08:00
|
|
|
default:
|
2008-02-22 02:02:17 +08:00
|
|
|
return UnknownVal();
|
2008-02-20 12:12:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLoc X) {
|
2008-02-20 12:12:31 +08:00
|
|
|
|
2008-02-15 02:40:24 +08:00
|
|
|
switch (X.getSubKind()) {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case nonloc::ConcreteIntKind:
|
|
|
|
return cast<nonloc::ConcreteInt>(X).EvalComplement(Eng.getBasicVals());
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-02-15 02:40:24 +08:00
|
|
|
default:
|
2008-02-22 02:02:17 +08:00
|
|
|
return UnknownVal();
|
2008-02-15 02:40:24 +08:00
|
|
|
}
|
|
|
|
}
|
2008-02-15 03:37:24 +08:00
|
|
|
|
|
|
|
// Binary operators.
|
|
|
|
|
2008-07-18 23:46:06 +08:00
|
|
|
static unsigned char LNotOpMap[] = {
|
|
|
|
(unsigned char) BinaryOperator::GE, /* LT => GE */
|
|
|
|
(unsigned char) BinaryOperator::LE, /* GT => LE */
|
|
|
|
(unsigned char) BinaryOperator::GT, /* LE => GT */
|
|
|
|
(unsigned char) BinaryOperator::LT, /* GE => LT */
|
|
|
|
(unsigned char) BinaryOperator::NE, /* EQ => NE */
|
|
|
|
(unsigned char) BinaryOperator::EQ /* NE => EQ */
|
|
|
|
};
|
|
|
|
|
2008-11-15 08:20:05 +08:00
|
|
|
SVal GRSimpleVals::DetermEvalBinOpNN(GRExprEngine& Eng,
|
2008-07-18 23:27:58 +08:00
|
|
|
BinaryOperator::Opcode Op,
|
2008-10-17 13:57:07 +08:00
|
|
|
NonLoc L, NonLoc R) {
|
2008-09-20 01:31:13 +08:00
|
|
|
|
2008-11-15 08:20:05 +08:00
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
2008-09-20 01:31:13 +08:00
|
|
|
unsigned subkind = L.getSubKind();
|
2008-03-12 09:21:45 +08:00
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
while (1) {
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-09-20 01:31:13 +08:00
|
|
|
switch (subkind) {
|
2008-02-15 03:37:24 +08:00
|
|
|
default:
|
2008-02-22 03:10:12 +08:00
|
|
|
return UnknownVal();
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-11-15 08:20:05 +08:00
|
|
|
case nonloc::LocAsIntegerKind: {
|
|
|
|
Loc LL = cast<nonloc::LocAsInteger>(L).getLoc();
|
|
|
|
|
|
|
|
switch (R.getSubKind()) {
|
|
|
|
case nonloc::LocAsIntegerKind:
|
|
|
|
return EvalBinOp(Eng, Op, LL,
|
|
|
|
cast<nonloc::LocAsInteger>(R).getLoc());
|
|
|
|
|
|
|
|
case nonloc::ConcreteIntKind: {
|
|
|
|
// Transform the integer into a location and compare.
|
|
|
|
ASTContext& Ctx = Eng.getContext();
|
|
|
|
llvm::APSInt V = cast<nonloc::ConcreteInt>(R).getValue();
|
|
|
|
V.setIsUnsigned(true);
|
|
|
|
V.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
|
|
|
|
return EvalBinOp(Eng, Op, LL,
|
|
|
|
loc::ConcreteInt(BasicVals.getValue(V)));
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
switch (Op) {
|
|
|
|
case BinaryOperator::EQ:
|
|
|
|
return NonLoc::MakeIntTruthVal(BasicVals, false);
|
|
|
|
case BinaryOperator::NE:
|
|
|
|
return NonLoc::MakeIntTruthVal(BasicVals, true);
|
|
|
|
default:
|
|
|
|
// This case also handles pointer arithmetic.
|
|
|
|
return UnknownVal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case nonloc::SymIntConstraintValKind: {
|
2008-07-18 23:54:51 +08:00
|
|
|
|
|
|
|
// Logical not?
|
|
|
|
if (!(Op == BinaryOperator::EQ && R.isZeroConstant()))
|
|
|
|
return UnknownVal();
|
|
|
|
|
2008-07-18 23:46:06 +08:00
|
|
|
const SymIntConstraint& C =
|
2008-10-17 13:57:07 +08:00
|
|
|
cast<nonloc::SymIntConstraintVal>(L).getConstraint();
|
2008-07-18 23:46:06 +08:00
|
|
|
|
|
|
|
BinaryOperator::Opcode Opc = C.getOpcode();
|
2008-07-18 23:54:51 +08:00
|
|
|
|
2008-07-18 23:46:06 +08:00
|
|
|
if (Opc < BinaryOperator::LT || Opc > BinaryOperator::NE)
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
// For comparison operators, translate the constraint by
|
|
|
|
// changing the opcode.
|
|
|
|
|
|
|
|
int idx = (unsigned) Opc - (unsigned) BinaryOperator::LT;
|
|
|
|
|
|
|
|
assert (idx >= 0 &&
|
|
|
|
(unsigned) idx < sizeof(LNotOpMap)/sizeof(unsigned char));
|
|
|
|
|
|
|
|
Opc = (BinaryOperator::Opcode) LNotOpMap[idx];
|
|
|
|
|
|
|
|
const SymIntConstraint& CNew =
|
|
|
|
BasicVals.getConstraint(C.getSymbol(), Opc, C.getInt());
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::SymIntConstraintVal(CNew);
|
2008-07-18 23:46:06 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case nonloc::ConcreteIntKind:
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<nonloc::ConcreteInt>(R)) {
|
|
|
|
const nonloc::ConcreteInt& L_CI = cast<nonloc::ConcreteInt>(L);
|
|
|
|
const nonloc::ConcreteInt& R_CI = cast<nonloc::ConcreteInt>(R);
|
2008-03-08 04:13:31 +08:00
|
|
|
return L_CI.EvalBinOp(BasicVals, Op, R_CI);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
else {
|
2008-09-20 01:31:13 +08:00
|
|
|
subkind = R.getSubKind();
|
2008-10-17 13:57:07 +08:00
|
|
|
NonLoc tmp = R;
|
2008-02-22 02:02:17 +08:00
|
|
|
R = L;
|
|
|
|
L = tmp;
|
2008-09-20 01:31:13 +08:00
|
|
|
|
|
|
|
// Swap the operators.
|
|
|
|
switch (Op) {
|
|
|
|
case BinaryOperator::LT: Op = BinaryOperator::GT; break;
|
|
|
|
case BinaryOperator::GT: Op = BinaryOperator::LT; break;
|
|
|
|
case BinaryOperator::LE: Op = BinaryOperator::GE; break;
|
|
|
|
case BinaryOperator::GE: Op = BinaryOperator::LE; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2008-02-15 03:37:24 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case nonloc::SymbolValKind:
|
|
|
|
if (isa<nonloc::ConcreteInt>(R)) {
|
2008-02-15 03:37:24 +08:00
|
|
|
const SymIntConstraint& C =
|
2008-10-17 13:57:07 +08:00
|
|
|
BasicVals.getConstraint(cast<nonloc::SymbolVal>(L).getSymbol(), Op,
|
|
|
|
cast<nonloc::ConcreteInt>(R).getValue());
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::SymIntConstraintVal(C);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
else
|
2008-02-22 02:02:17 +08:00
|
|
|
return UnknownVal();
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-15 08:52:26 +08:00
|
|
|
|
2008-02-16 07:15:23 +08:00
|
|
|
// Binary Operators (except assignments and comma).
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
|
|
|
|
Loc L, Loc R) {
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2008-02-16 07:15:23 +08:00
|
|
|
switch (Op) {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-02-16 07:15:23 +08:00
|
|
|
default:
|
|
|
|
return UnknownVal();
|
|
|
|
|
|
|
|
case BinaryOperator::EQ:
|
2008-03-12 09:21:45 +08:00
|
|
|
return EvalEQ(Eng, L, R);
|
2008-02-16 07:15:23 +08:00
|
|
|
|
|
|
|
case BinaryOperator::NE:
|
2008-03-12 09:21:45 +08:00
|
|
|
return EvalNE(Eng, L, R);
|
2008-02-16 07:15:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-15 08:52:26 +08:00
|
|
|
// Pointer arithmetic.
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
|
|
|
|
Loc L, NonLoc R) {
|
2008-02-22 02:02:17 +08:00
|
|
|
return UnknownVal();
|
2008-02-15 08:52:26 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
// Equality operators for Locs.
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, Loc L, Loc R) {
|
2008-03-12 09:21:45 +08:00
|
|
|
|
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
switch (L.getSubKind()) {
|
|
|
|
|
2008-02-15 03:37:24 +08:00
|
|
|
default:
|
2008-10-17 13:57:07 +08:00
|
|
|
assert(false && "EQ not implemented for this Loc.");
|
2008-02-22 02:02:17 +08:00
|
|
|
return UnknownVal();
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::ConcreteIntKind:
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<loc::ConcreteInt>(R)) {
|
|
|
|
bool b = cast<loc::ConcreteInt>(L).getValue() ==
|
|
|
|
cast<loc::ConcreteInt>(R).getValue();
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return NonLoc::MakeIntTruthVal(BasicVals, b);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
2008-10-17 13:57:07 +08:00
|
|
|
else if (isa<loc::SymbolVal>(R)) {
|
2008-02-15 03:37:24 +08:00
|
|
|
|
|
|
|
const SymIntConstraint& C =
|
2008-10-17 13:57:07 +08:00
|
|
|
BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
|
2008-02-15 03:37:24 +08:00
|
|
|
BinaryOperator::EQ,
|
2008-10-17 13:57:07 +08:00
|
|
|
cast<loc::ConcreteInt>(L).getValue());
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::SymIntConstraintVal(C);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::SymbolValKind: {
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<loc::ConcreteInt>(R)) {
|
2008-02-22 02:02:17 +08:00
|
|
|
const SymIntConstraint& C =
|
2008-10-17 13:57:07 +08:00
|
|
|
BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(),
|
2008-02-22 02:02:17 +08:00
|
|
|
BinaryOperator::EQ,
|
2008-10-17 13:57:07 +08:00
|
|
|
cast<loc::ConcreteInt>(R).getValue());
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::SymIntConstraintVal(C);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
|
2008-02-23 02:41:59 +08:00
|
|
|
// FIXME: Implement == for lval Symbols. This is mainly useful
|
|
|
|
// in iterator loops when traversing a buffer, e.g. while(z != zTerm).
|
|
|
|
// Since this is not useful for many checkers we'll punt on this for
|
|
|
|
// now.
|
|
|
|
|
|
|
|
return UnknownVal();
|
2008-02-22 02:02:17 +08:00
|
|
|
}
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::MemRegionKind:
|
|
|
|
case loc::FuncValKind:
|
|
|
|
case loc::GotoLabelKind:
|
|
|
|
return NonLoc::MakeIntTruthVal(BasicVals, L == R);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return NonLoc::MakeIntTruthVal(BasicVals, false);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal GRSimpleVals::EvalNE(GRExprEngine& Eng, Loc L, Loc R) {
|
2008-02-19 06:57:02 +08:00
|
|
|
|
2008-03-12 09:21:45 +08:00
|
|
|
BasicValueFactory& BasicVals = Eng.getBasicVals();
|
|
|
|
|
2008-02-22 02:02:17 +08:00
|
|
|
switch (L.getSubKind()) {
|
2008-02-15 03:37:24 +08:00
|
|
|
|
|
|
|
default:
|
2008-10-17 13:57:07 +08:00
|
|
|
assert(false && "NE not implemented for this Loc.");
|
2008-02-22 02:02:17 +08:00
|
|
|
return UnknownVal();
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::ConcreteIntKind:
|
2008-02-22 02:02:17 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<loc::ConcreteInt>(R)) {
|
|
|
|
bool b = cast<loc::ConcreteInt>(L).getValue() !=
|
|
|
|
cast<loc::ConcreteInt>(R).getValue();
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return NonLoc::MakeIntTruthVal(BasicVals, b);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
2008-10-17 13:57:07 +08:00
|
|
|
else if (isa<loc::SymbolVal>(R)) {
|
2008-02-15 03:37:24 +08:00
|
|
|
const SymIntConstraint& C =
|
2008-10-17 13:57:07 +08:00
|
|
|
BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
|
2008-03-12 09:21:45 +08:00
|
|
|
BinaryOperator::NE,
|
2008-10-17 13:57:07 +08:00
|
|
|
cast<loc::ConcreteInt>(L).getValue());
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::SymIntConstraintVal(C);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::SymbolValKind: {
|
|
|
|
if (isa<loc::ConcreteInt>(R)) {
|
2008-02-22 02:02:17 +08:00
|
|
|
const SymIntConstraint& C =
|
2008-10-17 13:57:07 +08:00
|
|
|
BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(),
|
2008-03-12 09:21:45 +08:00
|
|
|
BinaryOperator::NE,
|
2008-10-17 13:57:07 +08:00
|
|
|
cast<loc::ConcreteInt>(R).getValue());
|
2008-02-15 03:37:24 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return nonloc::SymIntConstraintVal(C);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
|
2008-02-23 02:41:59 +08:00
|
|
|
// FIXME: Implement != for lval Symbols. This is mainly useful
|
|
|
|
// in iterator loops when traversing a buffer, e.g. while(z != zTerm).
|
|
|
|
// Since this is not useful for many checkers we'll punt on this for
|
|
|
|
// now.
|
|
|
|
|
|
|
|
return UnknownVal();
|
2008-02-15 03:37:24 +08:00
|
|
|
|
|
|
|
break;
|
2008-02-22 02:02:17 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
case loc::MemRegionKind:
|
|
|
|
case loc::FuncValKind:
|
|
|
|
case loc::GotoLabelKind:
|
|
|
|
return NonLoc::MakeIntTruthVal(BasicVals, L != R);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
return NonLoc::MakeIntTruthVal(BasicVals, true);
|
2008-02-15 03:37:24 +08:00
|
|
|
}
|
2008-02-27 07:04:29 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-16 07:06:53 +08:00
|
|
|
// Transfer function for function calls.
|
2008-02-27 07:04:29 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
void GRSimpleVals::EvalCall(ExplodedNodeSet<GRState>& Dst,
|
2008-03-12 09:21:45 +08:00
|
|
|
GRExprEngine& Eng,
|
2008-08-13 12:27:00 +08:00
|
|
|
GRStmtNodeBuilder<GRState>& Builder,
|
2008-10-17 13:57:07 +08:00
|
|
|
CallExpr* CE, SVal L,
|
2008-08-13 12:27:00 +08:00
|
|
|
ExplodedNode<GRState>* Pred) {
|
2008-03-05 08:33:14 +08:00
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
GRStateManager& StateMgr = Eng.getStateManager();
|
|
|
|
const GRState* St = Builder.GetState(Pred);
|
2008-02-27 07:04:29 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
// Invalidate all arguments passed in by reference (Locs).
|
2008-02-27 07:04:29 +08:00
|
|
|
|
|
|
|
for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = StateMgr.GetSVal(St, *I);
|
2008-02-27 07:04:29 +08:00
|
|
|
|
2008-10-27 17:00:08 +08:00
|
|
|
if (isa<loc::MemRegionVal>(V))
|
2008-10-30 13:33:54 +08:00
|
|
|
St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal());
|
2008-10-17 13:57:07 +08:00
|
|
|
else if (isa<nonloc::LocAsInteger>(V))
|
2008-10-30 13:33:54 +08:00
|
|
|
St = StateMgr.BindLoc(St, cast<nonloc::LocAsInteger>(V).getLoc(),
|
2008-04-23 05:39:21 +08:00
|
|
|
UnknownVal());
|
|
|
|
|
2008-02-27 07:04:29 +08:00
|
|
|
}
|
2008-03-13 05:04:07 +08:00
|
|
|
|
2008-10-18 06:23:12 +08:00
|
|
|
// Make up a symbol for the return value of this function.
|
|
|
|
// FIXME: We eventually should handle structs and other compound types
|
|
|
|
// that are returned by value.
|
|
|
|
QualType T = CE->getType();
|
2008-11-13 14:10:40 +08:00
|
|
|
if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
|
2008-03-13 05:04:07 +08:00
|
|
|
unsigned Count = Builder.getCurrentBlockCount();
|
2008-12-05 10:27:51 +08:00
|
|
|
SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
|
2008-03-13 05:04:07 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal X = Loc::IsLocType(CE->getType())
|
|
|
|
? cast<SVal>(loc::SymbolVal(Sym))
|
|
|
|
: cast<SVal>(nonloc::SymbolVal(Sym));
|
2008-03-13 05:04:07 +08:00
|
|
|
|
2008-10-30 13:33:54 +08:00
|
|
|
St = StateMgr.BindExpr(St, CE, X, Eng.getCFG().isBlkExpr(CE), false);
|
2008-03-13 05:04:07 +08:00
|
|
|
}
|
2008-03-05 08:33:14 +08:00
|
|
|
|
2008-03-22 05:30:14 +08:00
|
|
|
Builder.MakeNode(Dst, CE, Pred, St);
|
2008-02-27 07:04:29 +08:00
|
|
|
}
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer function for Objective-C message expressions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
|
2008-04-16 07:06:53 +08:00
|
|
|
GRExprEngine& Eng,
|
2008-08-13 12:27:00 +08:00
|
|
|
GRStmtNodeBuilder<GRState>& Builder,
|
2008-04-16 07:06:53 +08:00
|
|
|
ObjCMessageExpr* ME,
|
2008-08-13 12:27:00 +08:00
|
|
|
ExplodedNode<GRState>* Pred) {
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
// The basic transfer function logic for message expressions does nothing.
|
|
|
|
// We just invalidate all arguments passed in by references.
|
|
|
|
|
2008-08-13 12:27:00 +08:00
|
|
|
GRStateManager& StateMgr = Eng.getStateManager();
|
|
|
|
const GRState* St = Builder.GetState(Pred);
|
2008-04-16 07:06:53 +08:00
|
|
|
|
|
|
|
for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
SVal V = StateMgr.GetSVal(St, *I);
|
2008-04-16 07:06:53 +08:00
|
|
|
|
2008-10-17 13:57:07 +08:00
|
|
|
if (isa<Loc>(V))
|
2008-10-30 13:33:54 +08:00
|
|
|
St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal());
|
2008-04-16 07:06:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Builder.MakeNode(Dst, ME, Pred, St);
|
|
|
|
}
|