blob: 2542cfdd3c66068d68937aebe7e06abfe46a88dd [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();
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(Op, cast<NonLoc>(L), cast<NonLoc>(R), T);
47}
48
Ted Kremenek32c3fa42009-07-21 21:03:30 +000049SValuator::CastResult SValuator::EvalCast(SVal val, const GRState *state,
50 QualType castTy, QualType originalTy){
51
52 if (val.isUnknownOrUndef() || castTy == originalTy)
53 return CastResult(state, val);
54
55 ASTContext &C = ValMgr.getContext();
56
57 // For const casts, just propagate the value.
58 if (C.getCanonicalType(castTy).getUnqualifiedType() ==
59 C.getCanonicalType(originalTy).getUnqualifiedType())
60 return CastResult(state, val);
61
62 // Check for casts from pointers to integers.
63 if (castTy->isIntegerType() && Loc::IsLocType(originalTy))
64 return CastResult(state, EvalCastL(cast<Loc>(val), castTy));
65
66 // Check for casts from integers to pointers.
67 if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) {
68 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
69 // Just unpackage the lval and return it.
70 return CastResult(state, LV->getLoc());
71 }
72
73 goto DispatchCast;
74 }
75
76 // Just pass through function and block pointers.
77 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
78 assert(Loc::IsLocType(castTy));
79 return CastResult(state, val);
80 }
81
82 // Check for casts from array type to another type.
83 if (originalTy->isArrayType()) {
84 // We will always decay to a pointer.
85 val = ValMgr.getStateManager().ArrayToPointer(cast<Loc>(val));
86
87 // Are we casting from an array to a pointer? If so just pass on
88 // the decayed value.
89 if (castTy->isPointerType())
90 return CastResult(state, val);
91
92 // Are we casting from an array to an integer? If so, cast the decayed
93 // pointer value to an integer.
94 assert(castTy->isIntegerType());
95
96 // FIXME: Keep these here for now in case we decide soon that we
97 // need the original decayed type.
98 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
99 // QualType pointerTy = C.getPointerType(elemTy);
100 return CastResult(state, EvalCastL(cast<Loc>(val), castTy));
101 }
102
103 // Check for casts from a region to a specific type.
104 if (const MemRegion *R = val.getAsRegion()) {
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000105 // FIXME: We should handle the case where we strip off view layers to get
106 // to a desugared type.
107
108 assert(Loc::IsLocType(castTy));
109 // We get a symbolic function pointer for a dereference of a function
110 // pointer, but it is of function type. Example:
111
112 // struct FPRec {
113 // void (*my_func)(int * x);
114 // };
115 //
116 // int bar(int x);
117 //
118 // int f1_a(struct FPRec* foo) {
119 // int x;
120 // (*foo->my_func)(&x);
121 // return bar(x)+1; // no-warning
122 // }
123
124 assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() ||
125 originalTy->isBlockPointerType());
126
127 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
128
129 // Delegate to store manager to get the result of casting a region
130 // to a different type.
131 const StoreManager::CastResult& Res = storeMgr.CastRegion(state, R, castTy);
132
133 // Inspect the result. If the MemRegion* returned is NULL, this
134 // expression evaluates to UnknownVal.
135 R = Res.getRegion();
136
137 if (R)
138 return CastResult(Res.getState(), loc::MemRegionVal(R));
139
140 return CastResult(Res.getState(), UnknownVal());
141 }
142
143 // All other cases.
144DispatchCast:
145 return CastResult(state,
146 isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy)
147 : EvalCastNL(cast<NonLoc>(val), castTy));
Owen Anderson4a28d5d2009-07-24 23:12:58 +0000148}