blob: 86006c3df6449828b70d10737c1fc3caecd44f3d [file] [log] [blame]
Ted Kremenek32c3fa42009-07-21 21:03:30 +00001// SValuator.cpp - Basic class for all SValuator implementations --*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines SValuator, the base class for all (complete) SValuator
11// implementations.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/SValuator.h"
16#include "clang/Analysis/PathSensitive/GRState.h"
17
18using namespace clang;
19
Ted Kremenekff4264d2009-08-25 18:44:25 +000020
21SVal SValuator::EvalBinOp(const GRState *ST, BinaryOperator::Opcode Op,
22 SVal L, SVal R, QualType T) {
23
24 if (L.isUndef() || R.isUndef())
25 return UndefinedVal();
Mike Stump1eb44332009-09-09 15:08:12 +000026
Ted Kremenekff4264d2009-08-25 18:44:25 +000027 if (L.isUnknown() || R.isUnknown())
28 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000029
Ted Kremenekff4264d2009-08-25 18:44:25 +000030 if (isa<Loc>(L)) {
31 if (isa<Loc>(R))
32 return EvalBinOpLL(Op, cast<Loc>(L), cast<Loc>(R), T);
33
34 return EvalBinOpLN(ST, Op, cast<Loc>(L), cast<NonLoc>(R), T);
35 }
Mike Stump1eb44332009-09-09 15:08:12 +000036
Ted Kremenekff4264d2009-08-25 18:44:25 +000037 if (isa<Loc>(R)) {
38 // Support pointer arithmetic where the increment/decrement operand
Mike Stump1eb44332009-09-09 15:08:12 +000039 // is on the left and the pointer on the right.
Ted Kremenekff4264d2009-08-25 18:44:25 +000040 assert(Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
Mike Stump1eb44332009-09-09 15:08:12 +000041
Ted Kremenekff4264d2009-08-25 18:44:25 +000042 // Commute the operands.
43 return EvalBinOpLN(ST, Op, cast<Loc>(R), cast<NonLoc>(L), T);
44 }
45
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +000046 return EvalBinOpNN(ST, Op, cast<NonLoc>(L), cast<NonLoc>(R), T);
Ted Kremenekff4264d2009-08-25 18:44:25 +000047}
48
Ted Kremenek5b9bd212009-09-11 22:07:28 +000049DefinedOrUnknownSVal SValuator::EvalEQ(const GRState *ST,
50 DefinedOrUnknownSVal L,
51 DefinedOrUnknownSVal R) {
52 return cast<DefinedOrUnknownSVal>(EvalBinOp(ST, BinaryOperator::EQ, L, R,
53 ValMgr.getContext().IntTy));
54}
55
Mike Stump1eb44332009-09-09 15:08:12 +000056SValuator::CastResult SValuator::EvalCast(SVal val, const GRState *state,
Ted Kremenek32c3fa42009-07-21 21:03:30 +000057 QualType castTy, QualType originalTy){
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenek32c3fa42009-07-21 21:03:30 +000059 if (val.isUnknownOrUndef() || castTy == originalTy)
60 return CastResult(state, val);
Mike Stump1eb44332009-09-09 15:08:12 +000061
Ted Kremenek32c3fa42009-07-21 21:03:30 +000062 ASTContext &C = ValMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000063
Ted Kremenek32c3fa42009-07-21 21:03:30 +000064 // For const casts, just propagate the value.
Mike Stump1eb44332009-09-09 15:08:12 +000065 if (C.getCanonicalType(castTy).getUnqualifiedType() ==
Ted Kremenek32c3fa42009-07-21 21:03:30 +000066 C.getCanonicalType(originalTy).getUnqualifiedType())
67 return CastResult(state, val);
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenek32c3fa42009-07-21 21:03:30 +000069 // Check for casts from pointers to integers.
70 if (castTy->isIntegerType() && Loc::IsLocType(originalTy))
71 return CastResult(state, EvalCastL(cast<Loc>(val), castTy));
Mike Stump1eb44332009-09-09 15:08:12 +000072
Ted Kremenek32c3fa42009-07-21 21:03:30 +000073 // Check for casts from integers to pointers.
74 if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) {
75 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
76 // Just unpackage the lval and return it.
77 return CastResult(state, LV->getLoc());
78 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Ted Kremenek32c3fa42009-07-21 21:03:30 +000080 goto DispatchCast;
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Ted Kremenek32c3fa42009-07-21 21:03:30 +000083 // Just pass through function and block pointers.
84 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
85 assert(Loc::IsLocType(castTy));
86 return CastResult(state, val);
87 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Ted Kremenek32c3fa42009-07-21 21:03:30 +000089 // Check for casts from array type to another type.
90 if (originalTy->isArrayType()) {
91 // We will always decay to a pointer.
92 val = ValMgr.getStateManager().ArrayToPointer(cast<Loc>(val));
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenek32c3fa42009-07-21 21:03:30 +000094 // Are we casting from an array to a pointer? If so just pass on
95 // the decayed value.
96 if (castTy->isPointerType())
97 return CastResult(state, val);
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenek32c3fa42009-07-21 21:03:30 +000099 // Are we casting from an array to an integer? If so, cast the decayed
100 // pointer value to an integer.
101 assert(castTy->isIntegerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000103 // FIXME: Keep these here for now in case we decide soon that we
104 // need the original decayed type.
105 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
106 // QualType pointerTy = C.getPointerType(elemTy);
107 return CastResult(state, EvalCastL(cast<Loc>(val), castTy));
108 }
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000110 // Check for casts from a region to a specific type.
111 if (const MemRegion *R = val.getAsRegion()) {
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000112 // FIXME: We should handle the case where we strip off view layers to get
113 // to a desugared type.
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000115 assert(Loc::IsLocType(castTy));
116 // We get a symbolic function pointer for a dereference of a function
117 // pointer, but it is of function type. Example:
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000119 // struct FPRec {
Mike Stump1eb44332009-09-09 15:08:12 +0000120 // void (*my_func)(int * x);
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000121 // };
122 //
123 // int bar(int x);
124 //
125 // int f1_a(struct FPRec* foo) {
126 // int x;
127 // (*foo->my_func)(&x);
128 // return bar(x)+1; // no-warning
129 // }
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000131 assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() ||
132 originalTy->isBlockPointerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000134 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000136 // Delegate to store manager to get the result of casting a region
137 // to a different type.
138 const StoreManager::CastResult& Res = storeMgr.CastRegion(state, R, castTy);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000140 // Inspect the result. If the MemRegion* returned is NULL, this
141 // expression evaluates to UnknownVal.
142 R = Res.getRegion();
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000144 if (R)
145 return CastResult(Res.getState(), loc::MemRegionVal(R));
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000147 return CastResult(Res.getState(), UnknownVal());
148 }
Mike Stump1eb44332009-09-09 15:08:12 +0000149
150 // All other cases.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000151DispatchCast:
152 return CastResult(state,
Mike Stump1eb44332009-09-09 15:08:12 +0000153 isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000154 : EvalCastNL(cast<NonLoc>(val), castTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000155}
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000156
157SValuator::DefinedOrUnknownCastResult
158SValuator::EvalCast(DefinedOrUnknownSVal V, const GRState *ST,
159 QualType castTy, QualType originalType) {
160 SValuator::CastResult X = EvalCast((SVal) V, ST, castTy, originalType);
161 return DefinedOrUnknownCastResult(X.getState(),
162 cast<DefinedOrUnknownSVal>(X.getSVal()));
163}