blob: 76a8bc782eb57e65c0837cfa426417bb7e0ff519 [file] [log] [blame]
Ted Kremeneke8391722009-06-26 00:25:05 +00001// SimpleSValuator.cpp - A basic SValuator ------------------------*- 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 SimpleSValuator, a basic implementation of SValuator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/PathSensitive/SValuator.h"
15#include "clang/Analysis/PathSensitive/GRState.h"
16#include "llvm/Support/Compiler.h"
17
18using namespace clang;
19
20namespace {
21class VISIBILITY_HIDDEN SimpleSValuator : public SValuator {
22public:
23 SimpleSValuator(ValueManager &valMgr) : SValuator(valMgr) {}
24 virtual ~SimpleSValuator() {}
25
26 virtual SVal EvalCast(NonLoc val, QualType castTy);
27 virtual SVal EvalCast(Loc val, QualType castTy);
28 virtual SVal EvalMinus(NonLoc val);
29 virtual SVal EvalComplement(NonLoc val);
30 virtual SVal EvalBinOpNN(BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs,
31 QualType resultTy);
32 virtual SVal EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
33 QualType resultTy);
34 virtual SVal EvalBinOpLN(const GRState *state, BinaryOperator::Opcode op,
35 Loc lhs, NonLoc rhs, QualType resultTy);
36};
37} // end anonymous namespace
38
39SValuator *clang::CreateSimpleSValuator(ValueManager &valMgr) {
40 return new SimpleSValuator(valMgr);
41}
42
43//===----------------------------------------------------------------------===//
44// Transfer function for Casts.
45//===----------------------------------------------------------------------===//
46
47SVal SimpleSValuator::EvalCast(NonLoc val, QualType castTy) {
48 if (!isa<nonloc::ConcreteInt>(val))
49 return UnknownVal();
50
51 bool isLocType = Loc::IsLocType(castTy);
52
53 // Only handle casts from integers to integers.
54 if (!isLocType && !castTy->isIntegerType())
55 return UnknownVal();
56
57 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
58 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
59 i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy));
60
61 if (isLocType)
62 return ValMgr.makeIntLocVal(i);
63 else
64 return ValMgr.makeIntVal(i);
65}
66
67SVal SimpleSValuator::EvalCast(Loc val, QualType castTy) {
68
69 // Casts from pointers -> pointers, just return the lval.
70 //
71 // Casts from pointers -> references, just return the lval. These
72 // can be introduced by the frontend for corner cases, e.g
73 // casting from va_list* to __builtin_va_list&.
74 //
75 assert(!val.isUnknownOrUndef());
76
77 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
78 return val;
79
80 // FIXME: Handle transparent unions where a value can be "transparently"
81 // lifted into a union type.
82 if (castTy->isUnionType())
83 return UnknownVal();
84
85 assert(castTy->isIntegerType());
86 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
87
88 if (!isa<loc::ConcreteInt>(val))
89 return ValMgr.makeLocAsInteger(val, BitWidth);
90
91 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
92 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
93 i.extOrTrunc(BitWidth);
94 return ValMgr.makeIntVal(i);
95}
96
97//===----------------------------------------------------------------------===//
98// Transfer function for unary operators.
99//===----------------------------------------------------------------------===//
100
101SVal SimpleSValuator::EvalMinus(NonLoc val) {
102 switch (val.getSubKind()) {
103 case nonloc::ConcreteIntKind:
104 return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr);
105 default:
106 return UnknownVal();
107 }
108}
109
110SVal SimpleSValuator::EvalComplement(NonLoc X) {
111 switch (X.getSubKind()) {
112 case nonloc::ConcreteIntKind:
113 return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr);
114 default:
115 return UnknownVal();
116 }
117}
118
119//===----------------------------------------------------------------------===//
120// Transfer function for binary operators.
121//===----------------------------------------------------------------------===//
122
123static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
124 switch (op) {
125 default:
126 assert(false && "Invalid opcode.");
127 case BinaryOperator::LT: return BinaryOperator::GE;
128 case BinaryOperator::GT: return BinaryOperator::LE;
129 case BinaryOperator::LE: return BinaryOperator::GT;
130 case BinaryOperator::GE: return BinaryOperator::LT;
131 case BinaryOperator::EQ: return BinaryOperator::NE;
132 case BinaryOperator::NE: return BinaryOperator::EQ;
133 }
134}
135
136// Equality operators for Locs.
137// FIXME: All this logic will be revamped when we have MemRegion::getLocation()
138// implemented.
139
140static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual,
141 QualType resultTy) {
142
143 switch (lhs.getSubKind()) {
144 default:
145 assert(false && "EQ/NE not implemented for this Loc.");
146 return UnknownVal();
147
148 case loc::ConcreteIntKind: {
149 if (SymbolRef rSym = rhs.getAsSymbol())
150 return ValMgr.makeNonLoc(rSym,
151 isEqual ? BinaryOperator::EQ
152 : BinaryOperator::NE,
153 cast<loc::ConcreteInt>(lhs).getValue(),
154 resultTy);
155 break;
156 }
157 case loc::MemRegionKind: {
158 if (SymbolRef lSym = lhs.getAsLocSymbol()) {
159 if (isa<loc::ConcreteInt>(rhs)) {
160 return ValMgr.makeNonLoc(lSym,
161 isEqual ? BinaryOperator::EQ
162 : BinaryOperator::NE,
163 cast<loc::ConcreteInt>(rhs).getValue(),
164 resultTy);
165 }
166 }
167 break;
168 }
169
170 case loc::GotoLabelKind:
171 break;
172 }
173
174 return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy);
175}
176
177SVal SimpleSValuator::EvalBinOpNN(BinaryOperator::Opcode op,
178 NonLoc lhs, NonLoc rhs,
179 QualType resultTy) {
180 while (1) {
181 switch (lhs.getSubKind()) {
182 default:
183 return UnknownVal();
184 case nonloc::LocAsIntegerKind: {
185 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
186 switch (rhs.getSubKind()) {
187 case nonloc::LocAsIntegerKind:
188 return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(),
189 resultTy);
190 case nonloc::ConcreteIntKind: {
191 // Transform the integer into a location and compare.
192 ASTContext& Ctx = ValMgr.getContext();
193 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
194 i.setIsUnsigned(true);
195 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
196 return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy);
197 }
198 default:
199 switch (op) {
200 case BinaryOperator::EQ:
201 return ValMgr.makeTruthVal(false, resultTy);
202 case BinaryOperator::NE:
203 return ValMgr.makeTruthVal(true, resultTy);
204 default:
205 // This case also handles pointer arithmetic.
206 return UnknownVal();
207 }
208 }
209 }
210 case nonloc::SymExprValKind: {
211 // Logical not?
212 if (!(op == BinaryOperator::EQ && rhs.isZeroConstant()))
213 return UnknownVal();
214
215 const SymExpr *symExpr =
216 cast<nonloc::SymExprVal>(lhs).getSymbolicExpression();
217
218 // Only handle ($sym op constant) for now.
219 if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(symExpr)) {
220 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
221 switch (opc) {
222 case BinaryOperator::LAnd:
223 case BinaryOperator::LOr:
224 assert(false && "Logical operators handled by branching logic.");
225 return UnknownVal();
226 case BinaryOperator::Assign:
227 case BinaryOperator::MulAssign:
228 case BinaryOperator::DivAssign:
229 case BinaryOperator::RemAssign:
230 case BinaryOperator::AddAssign:
231 case BinaryOperator::SubAssign:
232 case BinaryOperator::ShlAssign:
233 case BinaryOperator::ShrAssign:
234 case BinaryOperator::AndAssign:
235 case BinaryOperator::XorAssign:
236 case BinaryOperator::OrAssign:
237 case BinaryOperator::Comma:
238 assert(false && "'=' and ',' operators handled by GRExprEngine.");
239 return UnknownVal();
240 case BinaryOperator::PtrMemD:
241 case BinaryOperator::PtrMemI:
242 assert(false && "Pointer arithmetic not handled here.");
243 return UnknownVal();
244 case BinaryOperator::Mul:
245 case BinaryOperator::Div:
246 case BinaryOperator::Rem:
247 case BinaryOperator::Add:
248 case BinaryOperator::Sub:
249 case BinaryOperator::Shl:
250 case BinaryOperator::Shr:
251 case BinaryOperator::And:
252 case BinaryOperator::Xor:
253 case BinaryOperator::Or:
254 // Not handled yet.
255 return UnknownVal();
256 case BinaryOperator::LT:
257 case BinaryOperator::GT:
258 case BinaryOperator::LE:
259 case BinaryOperator::GE:
260 case BinaryOperator::EQ:
261 case BinaryOperator::NE:
262 opc = NegateComparison(opc);
263 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
264 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
265 symIntExpr->getRHS(), resultTy);
266 }
267 }
268 }
269 case nonloc::ConcreteIntKind: {
270 if (isa<nonloc::ConcreteInt>(rhs)) {
271 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
272 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
273 }
274 else {
275 // Swap the left and right sides and flip the operator if doing so
276 // allows us to better reason about the expression (this is a form
277 // of expression canonicalization).
278 NonLoc tmp = rhs;
279 rhs = lhs;
280 lhs = tmp;
281
282 switch (op) {
283 case BinaryOperator::LT: op = BinaryOperator::GT; continue;
284 case BinaryOperator::GT: op = BinaryOperator::LT; continue;
285 case BinaryOperator::LE: op = BinaryOperator::GE; continue;
286 case BinaryOperator::GE: op = BinaryOperator::LE; continue;
287 case BinaryOperator::EQ:
288 case BinaryOperator::NE:
289 case BinaryOperator::Add:
290 case BinaryOperator::Mul:
291 continue;
292 default:
293 return UnknownVal();
294 }
295 }
296 }
297 case nonloc::SymbolValKind: {
298 if (isa<nonloc::ConcreteInt>(rhs)) {
299 return ValMgr.makeNonLoc(cast<nonloc::SymbolVal>(lhs).getSymbol(), op,
300 cast<nonloc::ConcreteInt>(rhs).getValue(),
301 resultTy);
302 }
303
304 return UnknownVal();
305 }
306 }
307 }
308}
309
310SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
311 QualType resultTy) {
312 switch (op) {
313 default:
314 return UnknownVal();
315 case BinaryOperator::EQ:
316 case BinaryOperator::NE:
317 return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy);
318 }
319}
320
321SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
322 BinaryOperator::Opcode op,
323 Loc lhs, NonLoc rhs, QualType resultTy) {
324 // Special case: 'rhs' is an integer that has the same width as a pointer and
325 // we are using the integer location in a comparison. Normally this cannot be
326 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
327 // can generate comparisons that trigger this code.
328 // FIXME: Are all locations guaranteed to have pointer width?
329 if (BinaryOperator::isEqualityOp(op)) {
330 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
331 const llvm::APSInt *x = &rhsInt->getValue();
332 ASTContext &ctx = ValMgr.getContext();
333 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
334 // Convert the signedness of the integer (if necessary).
335 if (x->isSigned())
336 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
337
338 return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy);
339 }
340 }
341 }
342
343 // Delegate pointer arithmetic to the StoreManager.
344 return state->getStateManager().getStoreManager().EvalBinOp(state, op, lhs,
345 rhs, resultTy);
346}