blob: a7e15fc3fd73a1ce00494bf1187cef0e3550ffd9 [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
Ted Kremenek1309f9a2010-01-25 04:41:41 +000015#include "clang/Checker/PathSensitive/SValuator.h"
16#include "clang/Checker/PathSensitive/GRState.h"
Ted Kremenek32c3fa42009-07-21 21:03:30 +000017
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))
Jordy Roseeac4a002010-06-28 08:26:15 +000032 return EvalBinOpLL(ST, Op, cast<Loc>(L), cast<Loc>(R), T);
Ted Kremenekff4264d2009-08-25 18:44:25 +000033
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)) {
Jordy Roseeac4a002010-06-28 08:26:15 +000038 // Support pointer arithmetic where the addend is on the left
39 // and the pointer on the right.
40 assert(Op == BinaryOperator::Add);
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
Zhongxing Xu814e6b92010-02-04 04:56:43 +000056SVal SValuator::EvalCast(SVal val, QualType castTy, QualType originalTy) {
Ted Kremenek32c3fa42009-07-21 21:03:30 +000057 if (val.isUnknownOrUndef() || castTy == originalTy)
Zhongxing Xu814e6b92010-02-04 04:56:43 +000058 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000059
Ted Kremenek32c3fa42009-07-21 21:03:30 +000060 ASTContext &C = ValMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000061
Ted Kremenek32c3fa42009-07-21 21:03:30 +000062 // For const casts, just propagate the value.
Zhongxing Xu5ea95fc2010-01-05 09:27:03 +000063 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
64 if (C.hasSameUnqualifiedType(castTy, originalTy))
Zhongxing Xu814e6b92010-02-04 04:56:43 +000065 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Ted Kremenekf6817042010-02-02 21:11:40 +000067 // Check for casts to real or complex numbers. We don't handle these at all
68 // right now.
69 if (castTy->isFloatingType() || castTy->isAnyComplexType())
Zhongxing Xu814e6b92010-02-04 04:56:43 +000070 return UnknownVal();
Ted Kremenekf6817042010-02-02 21:11:40 +000071
72 // Check for casts from integers to integers.
Zhongxing Xu7b81e8f2010-01-14 03:45:06 +000073 if (castTy->isIntegerType() && originalTy->isIntegerType())
Zhongxing Xu814e6b92010-02-04 04:56:43 +000074 return EvalCastNL(cast<NonLoc>(val), castTy);
Zhongxing Xu7b81e8f2010-01-14 03:45:06 +000075
Ted Kremenek32c3fa42009-07-21 21:03:30 +000076 // Check for casts from pointers to integers.
77 if (castTy->isIntegerType() && Loc::IsLocType(originalTy))
Zhongxing Xu814e6b92010-02-04 04:56:43 +000078 return EvalCastL(cast<Loc>(val), castTy);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Ted Kremenek32c3fa42009-07-21 21:03:30 +000080 // Check for casts from integers to pointers.
81 if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) {
82 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
Ted Kremenek5bbc8e72009-12-23 02:52:14 +000083 if (const MemRegion *R = LV->getLoc().getAsRegion()) {
84 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
85 R = storeMgr.CastRegion(R, castTy);
Zhongxing Xu814e6b92010-02-04 04:56:43 +000086 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
Ted Kremenek5bbc8e72009-12-23 02:52:14 +000087 }
Zhongxing Xu814e6b92010-02-04 04:56:43 +000088 return LV->getLoc();
Ted Kremenek32c3fa42009-07-21 21:03:30 +000089 }
Ted Kremenek32c3fa42009-07-21 21:03:30 +000090 goto DispatchCast;
91 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremenek32c3fa42009-07-21 21:03:30 +000093 // Just pass through function and block pointers.
94 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
95 assert(Loc::IsLocType(castTy));
Zhongxing Xu814e6b92010-02-04 04:56:43 +000096 return val;
Ted Kremenek32c3fa42009-07-21 21:03:30 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenek32c3fa42009-07-21 21:03:30 +000099 // Check for casts from array type to another type.
100 if (originalTy->isArrayType()) {
101 // We will always decay to a pointer.
102 val = ValMgr.getStateManager().ArrayToPointer(cast<Loc>(val));
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000104 // Are we casting from an array to a pointer? If so just pass on
105 // the decayed value.
106 if (castTy->isPointerType())
Zhongxing Xu814e6b92010-02-04 04:56:43 +0000107 return val;
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000109 // Are we casting from an array to an integer? If so, cast the decayed
110 // pointer value to an integer.
111 assert(castTy->isIntegerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000113 // FIXME: Keep these here for now in case we decide soon that we
114 // need the original decayed type.
115 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
116 // QualType pointerTy = C.getPointerType(elemTy);
Zhongxing Xu814e6b92010-02-04 04:56:43 +0000117 return EvalCastL(cast<Loc>(val), castTy);
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000120 // Check for casts from a region to a specific type.
121 if (const MemRegion *R = val.getAsRegion()) {
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000122 // FIXME: We should handle the case where we strip off view layers to get
123 // to a desugared type.
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000125 assert(Loc::IsLocType(castTy));
126 // We get a symbolic function pointer for a dereference of a function
127 // pointer, but it is of function type. Example:
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000129 // struct FPRec {
Mike Stump1eb44332009-09-09 15:08:12 +0000130 // void (*my_func)(int * x);
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000131 // };
132 //
133 // int bar(int x);
134 //
135 // int f1_a(struct FPRec* foo) {
136 // int x;
137 // (*foo->my_func)(&x);
138 // return bar(x)+1; // no-warning
139 // }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000141 assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() ||
142 originalTy->isBlockPointerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000144 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000146 // Delegate to store manager to get the result of casting a region to a
147 // different type. If the MemRegion* returned is NULL, this expression
148 // evaluates to UnknownVal.
149 R = storeMgr.CastRegion(R, castTy);
Zhongxing Xu814e6b92010-02-04 04:56:43 +0000150 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000153DispatchCast:
Ted Kremenek5bbc8e72009-12-23 02:52:14 +0000154 // All other cases.
Zhongxing Xu814e6b92010-02-04 04:56:43 +0000155 return isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy)
156 : EvalCastNL(cast<NonLoc>(val), castTy);
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000157}