blob: fb1d74a99046c388aa08e94fbe52ac24cf59301b [file] [log] [blame]
Shih-wei Liaof8fd82b2010-02-10 11:10:31 -08001// 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/Checker/PathSensitive/SValuator.h"
15#include "clang/Checker/PathSensitive/GRState.h"
16
17using namespace clang;
18
19namespace {
20class SimpleSValuator : public SValuator {
21protected:
22 virtual SVal EvalCastNL(NonLoc val, QualType castTy);
23 virtual SVal EvalCastL(Loc val, QualType castTy);
24
25public:
26 SimpleSValuator(ValueManager &valMgr) : SValuator(valMgr) {}
27 virtual ~SimpleSValuator() {}
28
29 virtual SVal EvalMinus(NonLoc val);
30 virtual SVal EvalComplement(NonLoc val);
31 virtual SVal EvalBinOpNN(const GRState *state, BinaryOperator::Opcode op,
32 NonLoc lhs, NonLoc rhs, QualType resultTy);
33 virtual SVal EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
34 QualType resultTy);
35 virtual SVal EvalBinOpLN(const GRState *state, BinaryOperator::Opcode op,
36 Loc lhs, NonLoc rhs, QualType resultTy);
37};
38} // end anonymous namespace
39
40SValuator *clang::CreateSimpleSValuator(ValueManager &valMgr) {
41 return new SimpleSValuator(valMgr);
42}
43
44//===----------------------------------------------------------------------===//
45// Transfer function for Casts.
46//===----------------------------------------------------------------------===//
47
48SVal SimpleSValuator::EvalCastNL(NonLoc val, QualType castTy) {
49
50 bool isLocType = Loc::IsLocType(castTy);
51
52 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) {
53 if (isLocType)
54 return LI->getLoc();
55
56 // FIXME: Correctly support promotions/truncations.
57 ASTContext &Ctx = ValMgr.getContext();
58 unsigned castSize = Ctx.getTypeSize(castTy);
59 if (castSize == LI->getNumBits())
60 return val;
61
62 return ValMgr.makeLocAsInteger(LI->getLoc(), castSize);
63 }
64
65 if (const SymExpr *se = val.getAsSymbolicExpression()) {
66 ASTContext &Ctx = ValMgr.getContext();
67 QualType T = Ctx.getCanonicalType(se->getType(Ctx));
68 if (T == Ctx.getCanonicalType(castTy))
69 return val;
70
71 // FIXME: Remove this hack when we support symbolic truncation/extension.
72 // HACK: If both castTy and T are integers, ignore the cast. This is
73 // not a permanent solution. Eventually we want to precisely handle
74 // extension/truncation of symbolic integers. This prevents us from losing
75 // precision when we assign 'x = y' and 'y' is symbolic and x and y are
76 // different integer types.
77 if (T->isIntegerType() && castTy->isIntegerType())
78 return val;
79
80 return UnknownVal();
81 }
82
83 if (!isa<nonloc::ConcreteInt>(val))
84 return UnknownVal();
85
86 // Only handle casts from integers to integers.
87 if (!isLocType && !castTy->isIntegerType())
88 return UnknownVal();
89
90 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
91 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
92 i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy));
93
94 if (isLocType)
95 return ValMgr.makeIntLocVal(i);
96 else
97 return ValMgr.makeIntVal(i);
98}
99
100SVal SimpleSValuator::EvalCastL(Loc val, QualType castTy) {
101
102 // Casts from pointers -> pointers, just return the lval.
103 //
104 // Casts from pointers -> references, just return the lval. These
105 // can be introduced by the frontend for corner cases, e.g
106 // casting from va_list* to __builtin_va_list&.
107 //
108 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
109 return val;
110
111 // FIXME: Handle transparent unions where a value can be "transparently"
112 // lifted into a union type.
113 if (castTy->isUnionType())
114 return UnknownVal();
115
116 assert(castTy->isIntegerType());
117 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
118
119 if (!isa<loc::ConcreteInt>(val))
120 return ValMgr.makeLocAsInteger(val, BitWidth);
121
122 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
123 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
124 i.extOrTrunc(BitWidth);
125 return ValMgr.makeIntVal(i);
126}
127
128//===----------------------------------------------------------------------===//
129// Transfer function for unary operators.
130//===----------------------------------------------------------------------===//
131
132SVal SimpleSValuator::EvalMinus(NonLoc val) {
133 switch (val.getSubKind()) {
134 case nonloc::ConcreteIntKind:
135 return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr);
136 default:
137 return UnknownVal();
138 }
139}
140
141SVal SimpleSValuator::EvalComplement(NonLoc X) {
142 switch (X.getSubKind()) {
143 case nonloc::ConcreteIntKind:
144 return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr);
145 default:
146 return UnknownVal();
147 }
148}
149
150//===----------------------------------------------------------------------===//
151// Transfer function for binary operators.
152//===----------------------------------------------------------------------===//
153
154static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
155 switch (op) {
156 default:
157 assert(false && "Invalid opcode.");
158 case BinaryOperator::LT: return BinaryOperator::GE;
159 case BinaryOperator::GT: return BinaryOperator::LE;
160 case BinaryOperator::LE: return BinaryOperator::GT;
161 case BinaryOperator::GE: return BinaryOperator::LT;
162 case BinaryOperator::EQ: return BinaryOperator::NE;
163 case BinaryOperator::NE: return BinaryOperator::EQ;
164 }
165}
166
167// Equality operators for Locs.
168// FIXME: All this logic will be revamped when we have MemRegion::getLocation()
169// implemented.
170
171static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual,
172 QualType resultTy) {
173
174 switch (lhs.getSubKind()) {
175 default:
176 assert(false && "EQ/NE not implemented for this Loc.");
177 return UnknownVal();
178
179 case loc::ConcreteIntKind: {
180 if (SymbolRef rSym = rhs.getAsSymbol())
181 return ValMgr.makeNonLoc(rSym,
182 isEqual ? BinaryOperator::EQ
183 : BinaryOperator::NE,
184 cast<loc::ConcreteInt>(lhs).getValue(),
185 resultTy);
186 break;
187 }
188 case loc::MemRegionKind: {
189 if (SymbolRef lSym = lhs.getAsLocSymbol()) {
190 if (isa<loc::ConcreteInt>(rhs)) {
191 return ValMgr.makeNonLoc(lSym,
192 isEqual ? BinaryOperator::EQ
193 : BinaryOperator::NE,
194 cast<loc::ConcreteInt>(rhs).getValue(),
195 resultTy);
196 }
197 }
198 break;
199 }
200
201 case loc::GotoLabelKind:
202 break;
203 }
204
205 return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy);
206}
207
208SVal SimpleSValuator::EvalBinOpNN(const GRState *state,
209 BinaryOperator::Opcode op,
210 NonLoc lhs, NonLoc rhs,
211 QualType resultTy) {
212 // Handle trivial case where left-side and right-side are the same.
213 if (lhs == rhs)
214 switch (op) {
215 default:
216 break;
217 case BinaryOperator::EQ:
218 case BinaryOperator::LE:
219 case BinaryOperator::GE:
220 return ValMgr.makeTruthVal(true, resultTy);
221 case BinaryOperator::LT:
222 case BinaryOperator::GT:
223 case BinaryOperator::NE:
224 return ValMgr.makeTruthVal(false, resultTy);
225 }
226
227 while (1) {
228 switch (lhs.getSubKind()) {
229 default:
230 return UnknownVal();
231 case nonloc::LocAsIntegerKind: {
232 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
233 switch (rhs.getSubKind()) {
234 case nonloc::LocAsIntegerKind:
235 return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(),
236 resultTy);
237 case nonloc::ConcreteIntKind: {
238 // Transform the integer into a location and compare.
239 ASTContext& Ctx = ValMgr.getContext();
240 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
241 i.setIsUnsigned(true);
242 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
243 return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy);
244 }
245 default:
246 switch (op) {
247 case BinaryOperator::EQ:
248 return ValMgr.makeTruthVal(false, resultTy);
249 case BinaryOperator::NE:
250 return ValMgr.makeTruthVal(true, resultTy);
251 default:
252 // This case also handles pointer arithmetic.
253 return UnknownVal();
254 }
255 }
256 }
257 case nonloc::SymExprValKind: {
258 // Logical not?
259 if (!(op == BinaryOperator::EQ && rhs.isZeroConstant()))
260 return UnknownVal();
261
262 const SymExpr *symExpr =
263 cast<nonloc::SymExprVal>(lhs).getSymbolicExpression();
264
265 // Only handle ($sym op constant) for now.
266 if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(symExpr)) {
267 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
268 switch (opc) {
269 case BinaryOperator::LAnd:
270 case BinaryOperator::LOr:
271 assert(false && "Logical operators handled by branching logic.");
272 return UnknownVal();
273 case BinaryOperator::Assign:
274 case BinaryOperator::MulAssign:
275 case BinaryOperator::DivAssign:
276 case BinaryOperator::RemAssign:
277 case BinaryOperator::AddAssign:
278 case BinaryOperator::SubAssign:
279 case BinaryOperator::ShlAssign:
280 case BinaryOperator::ShrAssign:
281 case BinaryOperator::AndAssign:
282 case BinaryOperator::XorAssign:
283 case BinaryOperator::OrAssign:
284 case BinaryOperator::Comma:
285 assert(false && "'=' and ',' operators handled by GRExprEngine.");
286 return UnknownVal();
287 case BinaryOperator::PtrMemD:
288 case BinaryOperator::PtrMemI:
289 assert(false && "Pointer arithmetic not handled here.");
290 return UnknownVal();
291 case BinaryOperator::Mul:
292 case BinaryOperator::Div:
293 case BinaryOperator::Rem:
294 case BinaryOperator::Add:
295 case BinaryOperator::Sub:
296 case BinaryOperator::Shl:
297 case BinaryOperator::Shr:
298 case BinaryOperator::And:
299 case BinaryOperator::Xor:
300 case BinaryOperator::Or:
301 // Not handled yet.
302 return UnknownVal();
303 case BinaryOperator::LT:
304 case BinaryOperator::GT:
305 case BinaryOperator::LE:
306 case BinaryOperator::GE:
307 case BinaryOperator::EQ:
308 case BinaryOperator::NE:
309 opc = NegateComparison(opc);
310 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
311 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
312 symIntExpr->getRHS(), resultTy);
313 }
314 }
315 }
316 case nonloc::ConcreteIntKind: {
317 if (isa<nonloc::ConcreteInt>(rhs)) {
318 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
319 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
320 }
321 else {
322 // Swap the left and right sides and flip the operator if doing so
323 // allows us to better reason about the expression (this is a form
324 // of expression canonicalization).
325 NonLoc tmp = rhs;
326 rhs = lhs;
327 lhs = tmp;
328
329 switch (op) {
330 case BinaryOperator::LT: op = BinaryOperator::GT; continue;
331 case BinaryOperator::GT: op = BinaryOperator::LT; continue;
332 case BinaryOperator::LE: op = BinaryOperator::GE; continue;
333 case BinaryOperator::GE: op = BinaryOperator::LE; continue;
334 case BinaryOperator::EQ:
335 case BinaryOperator::NE:
336 case BinaryOperator::Add:
337 case BinaryOperator::Mul:
338 continue;
339 default:
340 return UnknownVal();
341 }
342 }
343 }
344 case nonloc::SymbolValKind: {
345 nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs);
346 SymbolRef Sym = slhs->getSymbol();
347
348 // Does the symbol simplify to a constant? If so, "fold" the constant
349 // by setting 'lhs' to a ConcreteInt and try again.
350 if (Sym->getType(ValMgr.getContext())->isIntegerType())
351 if (const llvm::APSInt *Constant = state->getSymVal(Sym)) {
352 // The symbol evaluates to a constant. If necessary, promote the
353 // folded constant (LHS) to the result type.
354 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
355 const llvm::APSInt &lhs_I = BVF.Convert(resultTy, *Constant);
356 lhs = nonloc::ConcreteInt(lhs_I);
357
358 // Also promote the RHS (if necessary).
359
360 // For shifts, it necessary promote the RHS to the result type.
361 if (BinaryOperator::isShiftOp(op))
362 continue;
363
364 // Other operators: do an implicit conversion. This shouldn't be
365 // necessary once we support truncation/extension of symbolic values.
366 if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){
367 rhs = nonloc::ConcreteInt(BVF.Convert(resultTy, rhs_I->getValue()));
368 }
369
370 continue;
371 }
372
373 if (isa<nonloc::ConcreteInt>(rhs)) {
374 return ValMgr.makeNonLoc(slhs->getSymbol(), op,
375 cast<nonloc::ConcreteInt>(rhs).getValue(),
376 resultTy);
377 }
378
379 return UnknownVal();
380 }
381 }
382 }
383}
384
385SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
386 QualType resultTy) {
387 switch (op) {
388 default:
389 return UnknownVal();
390 case BinaryOperator::EQ:
391 case BinaryOperator::NE:
392 return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy);
393 case BinaryOperator::LT:
394 case BinaryOperator::GT:
395 // FIXME: Generalize. For now, just handle the trivial case where
396 // the two locations are identical.
397 if (lhs == rhs)
398 return ValMgr.makeTruthVal(false, resultTy);
399 return UnknownVal();
400 }
401}
402
403SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
404 BinaryOperator::Opcode op,
405 Loc lhs, NonLoc rhs, QualType resultTy) {
406 // Special case: 'rhs' is an integer that has the same width as a pointer and
407 // we are using the integer location in a comparison. Normally this cannot be
408 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
409 // can generate comparisons that trigger this code.
410 // FIXME: Are all locations guaranteed to have pointer width?
411 if (BinaryOperator::isEqualityOp(op)) {
412 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
413 const llvm::APSInt *x = &rhsInt->getValue();
414 ASTContext &ctx = ValMgr.getContext();
415 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
416 // Convert the signedness of the integer (if necessary).
417 if (x->isSigned())
418 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
419
420 return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy);
421 }
422 }
423 }
424
425 // Delegate pointer arithmetic to the StoreManager.
426 return state->getStateManager().getStoreManager().EvalBinOp(op, lhs,
427 rhs, resultTy);
428}