blob: fd2bbd06fb43c74632f0f91629c0f80dd7eca87c [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))
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.
Zhongxing Xu5ea95fc2010-01-05 09:27:03 +000065 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
66 if (C.hasSameUnqualifiedType(castTy, originalTy))
67 return CastResult(state, val);
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenekf6817042010-02-02 21:11:40 +000069 // Check for casts to real or complex numbers. We don't handle these at all
70 // right now.
71 if (castTy->isFloatingType() || castTy->isAnyComplexType())
72 return CastResult(state, UnknownVal());
73
74 // Check for casts from integers to integers.
Zhongxing Xu7b81e8f2010-01-14 03:45:06 +000075 if (castTy->isIntegerType() && originalTy->isIntegerType())
76 return CastResult(state, EvalCastNL(cast<NonLoc>(val), castTy));
77
Ted Kremenek32c3fa42009-07-21 21:03:30 +000078 // Check for casts from pointers to integers.
79 if (castTy->isIntegerType() && Loc::IsLocType(originalTy))
80 return CastResult(state, EvalCastL(cast<Loc>(val), castTy));
Mike Stump1eb44332009-09-09 15:08:12 +000081
Ted Kremenek32c3fa42009-07-21 21:03:30 +000082 // Check for casts from integers to pointers.
83 if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) {
84 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
Ted Kremenek5bbc8e72009-12-23 02:52:14 +000085 if (const MemRegion *R = LV->getLoc().getAsRegion()) {
86 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
87 R = storeMgr.CastRegion(R, castTy);
88 return R ? CastResult(state, loc::MemRegionVal(R))
89 : CastResult(state, UnknownVal());
90 }
Ted Kremenek32c3fa42009-07-21 21:03:30 +000091 return CastResult(state, LV->getLoc());
92 }
Ted Kremenek32c3fa42009-07-21 21:03:30 +000093 goto DispatchCast;
94 }
Mike Stump1eb44332009-09-09 15:08:12 +000095
Ted Kremenek32c3fa42009-07-21 21:03:30 +000096 // Just pass through function and block pointers.
97 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
98 assert(Loc::IsLocType(castTy));
99 return CastResult(state, val);
100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000102 // Check for casts from array type to another type.
103 if (originalTy->isArrayType()) {
104 // We will always decay to a pointer.
105 val = ValMgr.getStateManager().ArrayToPointer(cast<Loc>(val));
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000107 // Are we casting from an array to a pointer? If so just pass on
108 // the decayed value.
109 if (castTy->isPointerType())
110 return CastResult(state, val);
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000112 // Are we casting from an array to an integer? If so, cast the decayed
113 // pointer value to an integer.
114 assert(castTy->isIntegerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000116 // FIXME: Keep these here for now in case we decide soon that we
117 // need the original decayed type.
118 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
119 // QualType pointerTy = C.getPointerType(elemTy);
120 return CastResult(state, EvalCastL(cast<Loc>(val), castTy));
121 }
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000123 // Check for casts from a region to a specific type.
124 if (const MemRegion *R = val.getAsRegion()) {
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000125 // FIXME: We should handle the case where we strip off view layers to get
126 // to a desugared type.
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000128 assert(Loc::IsLocType(castTy));
129 // We get a symbolic function pointer for a dereference of a function
130 // pointer, but it is of function type. Example:
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000132 // struct FPRec {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 // void (*my_func)(int * x);
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000134 // };
135 //
136 // int bar(int x);
137 //
138 // int f1_a(struct FPRec* foo) {
139 // int x;
140 // (*foo->my_func)(&x);
141 // return bar(x)+1; // no-warning
142 // }
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000144 assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() ||
145 originalTy->isBlockPointerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000147 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000148
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000149 // Delegate to store manager to get the result of casting a region to a
150 // different type. If the MemRegion* returned is NULL, this expression
151 // evaluates to UnknownVal.
152 R = storeMgr.CastRegion(R, castTy);
Ted Kremenek5bbc8e72009-12-23 02:52:14 +0000153 return R ? CastResult(state, loc::MemRegionVal(R))
154 : CastResult(state, UnknownVal());
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000155 }
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000157DispatchCast:
Ted Kremenek5bbc8e72009-12-23 02:52:14 +0000158 // All other cases.
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000159 return CastResult(state,
Mike Stump1eb44332009-09-09 15:08:12 +0000160 isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy)
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000161 : EvalCastNL(cast<NonLoc>(val), castTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000162}
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000163
164SValuator::DefinedOrUnknownCastResult
165SValuator::EvalCast(DefinedOrUnknownSVal V, const GRState *ST,
166 QualType castTy, QualType originalType) {
167 SValuator::CastResult X = EvalCast((SVal) V, ST, castTy, originalType);
168 return DefinedOrUnknownCastResult(X.getState(),
169 cast<DefinedOrUnknownSVal>(X.getSVal()));
170}