blob: 542fc1b1078dc6c12861ce86fc2d1b375a735cc6 [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001// 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/Checker/PathSensitive/SValuator.h"
16#include "clang/Checker/PathSensitive/GRState.h"
17
18using namespace clang;
19
20
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();
26
27 if (L.isUnknown() || R.isUnknown())
28 return UnknownVal();
29
30 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 }
36
37 if (isa<Loc>(R)) {
38 // Support pointer arithmetic where the increment/decrement operand
39 // is on the left and the pointer on the right.
40 assert(Op == BinaryOperator::Add || Op == BinaryOperator::Sub);
41
42 // Commute the operands.
43 return EvalBinOpLN(ST, Op, cast<Loc>(R), cast<NonLoc>(L), T);
44 }
45
46 return EvalBinOpNN(ST, Op, cast<NonLoc>(L), cast<NonLoc>(R), T);
47}
48
49DefinedOrUnknownSVal 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
56SVal SValuator::EvalCast(SVal val, QualType castTy, QualType originalTy) {
57 if (val.isUnknownOrUndef() || castTy == originalTy)
58 return val;
59
60 ASTContext &C = ValMgr.getContext();
61
62 // For const casts, just propagate the value.
63 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
64 if (C.hasSameUnqualifiedType(castTy, originalTy))
65 return val;
66
67 // Check for casts to real or complex numbers. We don't handle these at all
68 // right now.
69 if (castTy->isFloatingType() || castTy->isAnyComplexType())
70 return UnknownVal();
71
72 // Check for casts from integers to integers.
73 if (castTy->isIntegerType() && originalTy->isIntegerType())
74 return EvalCastNL(cast<NonLoc>(val), castTy);
75
76 // Check for casts from pointers to integers.
77 if (castTy->isIntegerType() && Loc::IsLocType(originalTy))
78 return EvalCastL(cast<Loc>(val), castTy);
79
80 // Check for casts from integers to pointers.
81 if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) {
82 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
83 if (const MemRegion *R = LV->getLoc().getAsRegion()) {
84 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
85 R = storeMgr.CastRegion(R, castTy);
86 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
87 }
88 return LV->getLoc();
89 }
90 goto DispatchCast;
91 }
92
93 // Just pass through function and block pointers.
94 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
95 assert(Loc::IsLocType(castTy));
96 return val;
97 }
98
99 // 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));
103
104 // Are we casting from an array to a pointer? If so just pass on
105 // the decayed value.
106 if (castTy->isPointerType())
107 return val;
108
109 // Are we casting from an array to an integer? If so, cast the decayed
110 // pointer value to an integer.
111 assert(castTy->isIntegerType());
112
113 // 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);
117 return EvalCastL(cast<Loc>(val), castTy);
118 }
119
120 // Check for casts from a region to a specific type.
121 if (const MemRegion *R = val.getAsRegion()) {
122 // FIXME: We should handle the case where we strip off view layers to get
123 // to a desugared type.
124
125 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:
128
129 // struct FPRec {
130 // void (*my_func)(int * x);
131 // };
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 // }
140
141 assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() ||
142 originalTy->isBlockPointerType());
143
144 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
145
146 // 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);
150 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
151 }
152
153DispatchCast:
154 // All other cases.
155 return isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy)
156 : EvalCastNL(cast<NonLoc>(val), castTy);
157}