blob: 0e56026bb4c873ef8e050a71a9b353205a619fa7 [file] [log] [blame]
Ted Kremenekac7c7242009-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 Kremenek3a459dc2009-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 Stump11289f42009-09-09 15:08:12 +000026
Ted Kremenek3a459dc2009-08-25 18:44:25 +000027 if (L.isUnknown() || R.isUnknown())
28 return UnknownVal();
Mike Stump11289f42009-09-09 15:08:12 +000029
Ted Kremenek3a459dc2009-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 Stump11289f42009-09-09 15:08:12 +000036
Ted Kremenek3a459dc2009-08-25 18:44:25 +000037 if (isa<Loc>(R)) {
38 // Support pointer arithmetic where the increment/decrement operand
Mike Stump11289f42009-09-09 15:08:12 +000039 // is on the left and the pointer on the right.
Ted Kremenek3a459dc2009-08-25 18:44:25 +000040 assert(Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
Mike Stump11289f42009-09-09 15:08:12 +000041
Ted Kremenek3a459dc2009-08-25 18:44:25 +000042 // Commute the operands.
43 return EvalBinOpLN(ST, Op, cast<Loc>(R), cast<NonLoc>(L), T);
44 }
45
Ted Kremenek8ec57712009-10-06 01:39:48 +000046 return EvalBinOpNN(ST, Op, cast<NonLoc>(L), cast<NonLoc>(R), T);
Ted Kremenek3a459dc2009-08-25 18:44:25 +000047}
48
Ted Kremenek7020eae2009-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 Stump11289f42009-09-09 15:08:12 +000056SValuator::CastResult SValuator::EvalCast(SVal val, const GRState *state,
Ted Kremenekac7c7242009-07-21 21:03:30 +000057 QualType castTy, QualType originalTy){
Mike Stump11289f42009-09-09 15:08:12 +000058
Ted Kremenekac7c7242009-07-21 21:03:30 +000059 if (val.isUnknownOrUndef() || castTy == originalTy)
60 return CastResult(state, val);
Mike Stump11289f42009-09-09 15:08:12 +000061
Ted Kremenekac7c7242009-07-21 21:03:30 +000062 ASTContext &C = ValMgr.getContext();
Mike Stump11289f42009-09-09 15:08:12 +000063
Ted Kremenekac7c7242009-07-21 21:03:30 +000064 // For const casts, just propagate the value.
Zhongxing Xu662ba692010-01-05 09:27:03 +000065 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
66 if (C.hasSameUnqualifiedType(castTy, originalTy))
67 return CastResult(state, val);
Mike Stump11289f42009-09-09 15:08:12 +000068
Ted Kremenekac7c7242009-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 Stump11289f42009-09-09 15:08:12 +000072
Ted Kremenekac7c7242009-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)) {
Ted Kremenek25e280b2009-12-23 02:52:14 +000076 if (const MemRegion *R = LV->getLoc().getAsRegion()) {
77 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
78 R = storeMgr.CastRegion(R, castTy);
79 return R ? CastResult(state, loc::MemRegionVal(R))
80 : CastResult(state, UnknownVal());
81 }
Ted Kremenekac7c7242009-07-21 21:03:30 +000082 return CastResult(state, LV->getLoc());
83 }
Ted Kremenekac7c7242009-07-21 21:03:30 +000084 goto DispatchCast;
85 }
Mike Stump11289f42009-09-09 15:08:12 +000086
Ted Kremenekac7c7242009-07-21 21:03:30 +000087 // Just pass through function and block pointers.
88 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
89 assert(Loc::IsLocType(castTy));
90 return CastResult(state, val);
91 }
Mike Stump11289f42009-09-09 15:08:12 +000092
Ted Kremenekac7c7242009-07-21 21:03:30 +000093 // Check for casts from array type to another type.
94 if (originalTy->isArrayType()) {
95 // We will always decay to a pointer.
96 val = ValMgr.getStateManager().ArrayToPointer(cast<Loc>(val));
Mike Stump11289f42009-09-09 15:08:12 +000097
Ted Kremenekac7c7242009-07-21 21:03:30 +000098 // Are we casting from an array to a pointer? If so just pass on
99 // the decayed value.
100 if (castTy->isPointerType())
101 return CastResult(state, val);
Mike Stump11289f42009-09-09 15:08:12 +0000102
Ted Kremenekac7c7242009-07-21 21:03:30 +0000103 // Are we casting from an array to an integer? If so, cast the decayed
104 // pointer value to an integer.
105 assert(castTy->isIntegerType());
Mike Stump11289f42009-09-09 15:08:12 +0000106
Ted Kremenekac7c7242009-07-21 21:03:30 +0000107 // FIXME: Keep these here for now in case we decide soon that we
108 // need the original decayed type.
109 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
110 // QualType pointerTy = C.getPointerType(elemTy);
111 return CastResult(state, EvalCastL(cast<Loc>(val), castTy));
112 }
Mike Stump11289f42009-09-09 15:08:12 +0000113
Ted Kremenekac7c7242009-07-21 21:03:30 +0000114 // Check for casts from a region to a specific type.
115 if (const MemRegion *R = val.getAsRegion()) {
Ted Kremenekac7c7242009-07-21 21:03:30 +0000116 // FIXME: We should handle the case where we strip off view layers to get
117 // to a desugared type.
Mike Stump11289f42009-09-09 15:08:12 +0000118
Ted Kremenekac7c7242009-07-21 21:03:30 +0000119 assert(Loc::IsLocType(castTy));
120 // We get a symbolic function pointer for a dereference of a function
121 // pointer, but it is of function type. Example:
Mike Stump11289f42009-09-09 15:08:12 +0000122
Ted Kremenekac7c7242009-07-21 21:03:30 +0000123 // struct FPRec {
Mike Stump11289f42009-09-09 15:08:12 +0000124 // void (*my_func)(int * x);
Ted Kremenekac7c7242009-07-21 21:03:30 +0000125 // };
126 //
127 // int bar(int x);
128 //
129 // int f1_a(struct FPRec* foo) {
130 // int x;
131 // (*foo->my_func)(&x);
132 // return bar(x)+1; // no-warning
133 // }
Mike Stump11289f42009-09-09 15:08:12 +0000134
Ted Kremenekac7c7242009-07-21 21:03:30 +0000135 assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() ||
136 originalTy->isBlockPointerType());
Mike Stump11289f42009-09-09 15:08:12 +0000137
Ted Kremenekac7c7242009-07-21 21:03:30 +0000138 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
Mike Stump11289f42009-09-09 15:08:12 +0000139
Zhongxing Xu86794812009-10-14 06:55:01 +0000140 // Delegate to store manager to get the result of casting a region to a
141 // different type. If the MemRegion* returned is NULL, this expression
142 // evaluates to UnknownVal.
143 R = storeMgr.CastRegion(R, castTy);
Ted Kremenek25e280b2009-12-23 02:52:14 +0000144 return R ? CastResult(state, loc::MemRegionVal(R))
145 : CastResult(state, UnknownVal());
Ted Kremenekac7c7242009-07-21 21:03:30 +0000146 }
Mike Stump11289f42009-09-09 15:08:12 +0000147
Ted Kremenekac7c7242009-07-21 21:03:30 +0000148DispatchCast:
Ted Kremenek25e280b2009-12-23 02:52:14 +0000149 // All other cases.
Ted Kremenekac7c7242009-07-21 21:03:30 +0000150 return CastResult(state,
Mike Stump11289f42009-09-09 15:08:12 +0000151 isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy)
Ted Kremenekac7c7242009-07-21 21:03:30 +0000152 : EvalCastNL(cast<NonLoc>(val), castTy));
Owen Andersonb7a2fe62009-07-24 23:12:58 +0000153}
Ted Kremenek7020eae2009-09-11 22:07:28 +0000154
155SValuator::DefinedOrUnknownCastResult
156SValuator::EvalCast(DefinedOrUnknownSVal V, const GRState *ST,
157 QualType castTy, QualType originalType) {
158 SValuator::CastResult X = EvalCast((SVal) V, ST, castTy, originalType);
159 return DefinedOrUnknownCastResult(X.getState(),
160 cast<DefinedOrUnknownSVal>(X.getSVal()));
161}