blob: c85d03810a9acdd2bfa91af87ecdf1ff6bfe828b [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
Ted Kremenek1309f9a2010-01-25 04:41:41 +000014#include "clang/Checker/PathSensitive/SValuator.h"
15#include "clang/Checker/PathSensitive/GRState.h"
Ted Kremeneke8391722009-06-26 00:25:05 +000016
17using namespace clang;
18
19namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000020class SimpleSValuator : public SValuator {
Ted Kremenek32c3fa42009-07-21 21:03:30 +000021protected:
Mike Stump1eb44332009-09-09 15:08:12 +000022 virtual SVal EvalCastNL(NonLoc val, QualType castTy);
23 virtual SVal EvalCastL(Loc val, QualType castTy);
Ted Kremenek32c3fa42009-07-21 21:03:30 +000024
Ted Kremeneke8391722009-06-26 00:25:05 +000025public:
26 SimpleSValuator(ValueManager &valMgr) : SValuator(valMgr) {}
27 virtual ~SimpleSValuator() {}
Mike Stump1eb44332009-09-09 15:08:12 +000028
29 virtual SVal EvalMinus(NonLoc val);
30 virtual SVal EvalComplement(NonLoc val);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +000031 virtual SVal EvalBinOpNN(const GRState *state, BinaryOperator::Opcode op,
32 NonLoc lhs, NonLoc rhs, QualType resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +000033 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);
Mike Stump1eb44332009-09-09 15:08:12 +000037};
Ted Kremeneke8391722009-06-26 00:25:05 +000038} // end anonymous namespace
39
40SValuator *clang::CreateSimpleSValuator(ValueManager &valMgr) {
41 return new SimpleSValuator(valMgr);
42}
43
44//===----------------------------------------------------------------------===//
45// Transfer function for Casts.
46//===----------------------------------------------------------------------===//
47
Ted Kremenekdd661142009-07-20 21:39:27 +000048SVal SimpleSValuator::EvalCastNL(NonLoc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000049
Ted Kremenekdd661142009-07-20 21:39:27 +000050 bool isLocType = Loc::IsLocType(castTy);
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenek9031dd72009-07-21 00:12:07 +000052 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) {
53 if (isLocType)
Ted Kremenekdd661142009-07-20 21:39:27 +000054 return LI->getLoc();
Mike Stump1eb44332009-09-09 15:08:12 +000055
Ted Kremenekc50e6df2010-01-11 02:33:26 +000056 // FIXME: Correctly support promotions/truncations.
Mike Stump1eb44332009-09-09 15:08:12 +000057 ASTContext &Ctx = ValMgr.getContext();
Ted Kremenekc50e6df2010-01-11 02:33:26 +000058 unsigned castSize = Ctx.getTypeSize(castTy);
59 if (castSize == LI->getNumBits())
Ted Kremenek9031dd72009-07-21 00:12:07 +000060 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000061
Ted Kremenekc50e6df2010-01-11 02:33:26 +000062 return ValMgr.makeLocAsInteger(LI->getLoc(), castSize);
Ted Kremenek9031dd72009-07-21 00:12:07 +000063 }
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;
Ted Kremenek80417472009-09-25 00:18:15 +000070
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;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Ted Kremenek9031dd72009-07-21 00:12:07 +000080 return UnknownVal();
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Ted Kremeneke8391722009-06-26 00:25:05 +000083 if (!isa<nonloc::ConcreteInt>(val))
84 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000085
Ted Kremeneke8391722009-06-26 00:25:05 +000086 // Only handle casts from integers to integers.
87 if (!isLocType && !castTy->isIntegerType())
88 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000089
Ted Kremeneke8391722009-06-26 00:25:05 +000090 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
Ted Kremenek46537392009-07-16 01:33:37 +0000100SVal SimpleSValuator::EvalCastL(Loc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremeneke8391722009-06-26 00:25:05 +0000102 // 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 //
Ted Kremeneke8391722009-06-26 00:25:05 +0000108 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
109 return val;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremeneke8391722009-06-26 00:25:05 +0000111 // FIXME: Handle transparent unions where a value can be "transparently"
112 // lifted into a union type.
113 if (castTy->isUnionType())
114 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenekd617b852010-04-16 17:54:33 +0000116 if (castTy->isIntegerType()) {
117 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000118
Ted Kremenekd617b852010-04-16 17:54:33 +0000119 if (!isa<loc::ConcreteInt>(val))
120 return ValMgr.makeLocAsInteger(val, BitWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Ted Kremenekd617b852010-04-16 17:54:33 +0000122 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 // All other cases: return 'UnknownVal'. This includes casting pointers
129 // to floats, which is probably badness it itself, but this is a good
130 // intermediate solution until we do something better.
131 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000132}
133
134//===----------------------------------------------------------------------===//
135// Transfer function for unary operators.
136//===----------------------------------------------------------------------===//
137
138SVal SimpleSValuator::EvalMinus(NonLoc val) {
Mike Stump1eb44332009-09-09 15:08:12 +0000139 switch (val.getSubKind()) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000140 case nonloc::ConcreteIntKind:
141 return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr);
142 default:
143 return UnknownVal();
144 }
145}
146
147SVal SimpleSValuator::EvalComplement(NonLoc X) {
148 switch (X.getSubKind()) {
149 case nonloc::ConcreteIntKind:
150 return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr);
151 default:
152 return UnknownVal();
153 }
154}
155
156//===----------------------------------------------------------------------===//
157// Transfer function for binary operators.
158//===----------------------------------------------------------------------===//
159
160static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
161 switch (op) {
162 default:
163 assert(false && "Invalid opcode.");
164 case BinaryOperator::LT: return BinaryOperator::GE;
165 case BinaryOperator::GT: return BinaryOperator::LE;
166 case BinaryOperator::LE: return BinaryOperator::GT;
167 case BinaryOperator::GE: return BinaryOperator::LT;
168 case BinaryOperator::EQ: return BinaryOperator::NE;
169 case BinaryOperator::NE: return BinaryOperator::EQ;
170 }
171}
172
Mike Stump1eb44332009-09-09 15:08:12 +0000173// Equality operators for Locs.
Ted Kremeneke8391722009-06-26 00:25:05 +0000174// FIXME: All this logic will be revamped when we have MemRegion::getLocation()
175// implemented.
176
177static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual,
178 QualType resultTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremeneke8391722009-06-26 00:25:05 +0000180 switch (lhs.getSubKind()) {
181 default:
182 assert(false && "EQ/NE not implemented for this Loc.");
183 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Ted Kremeneke8391722009-06-26 00:25:05 +0000185 case loc::ConcreteIntKind: {
186 if (SymbolRef rSym = rhs.getAsSymbol())
187 return ValMgr.makeNonLoc(rSym,
188 isEqual ? BinaryOperator::EQ
189 : BinaryOperator::NE,
190 cast<loc::ConcreteInt>(lhs).getValue(),
191 resultTy);
192 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000193 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000194 case loc::MemRegionKind: {
195 if (SymbolRef lSym = lhs.getAsLocSymbol()) {
196 if (isa<loc::ConcreteInt>(rhs)) {
197 return ValMgr.makeNonLoc(lSym,
198 isEqual ? BinaryOperator::EQ
199 : BinaryOperator::NE,
200 cast<loc::ConcreteInt>(rhs).getValue(),
201 resultTy);
202 }
203 }
204 break;
205 }
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Ted Kremeneke8391722009-06-26 00:25:05 +0000207 case loc::GotoLabelKind:
208 break;
209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremeneke8391722009-06-26 00:25:05 +0000211 return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy);
212}
213
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000214SVal SimpleSValuator::EvalBinOpNN(const GRState *state,
215 BinaryOperator::Opcode op,
Ted Kremeneke8391722009-06-26 00:25:05 +0000216 NonLoc lhs, NonLoc rhs,
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000217 QualType resultTy) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000218 // Handle trivial case where left-side and right-side are the same.
219 if (lhs == rhs)
220 switch (op) {
221 default:
222 break;
223 case BinaryOperator::EQ:
224 case BinaryOperator::LE:
225 case BinaryOperator::GE:
226 return ValMgr.makeTruthVal(true, resultTy);
227 case BinaryOperator::LT:
228 case BinaryOperator::GT:
229 case BinaryOperator::NE:
230 return ValMgr.makeTruthVal(false, resultTy);
231 }
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Ted Kremeneke8391722009-06-26 00:25:05 +0000233 while (1) {
234 switch (lhs.getSubKind()) {
235 default:
Mike Stump1eb44332009-09-09 15:08:12 +0000236 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000237 case nonloc::LocAsIntegerKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000238 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
Ted Kremeneke8391722009-06-26 00:25:05 +0000239 switch (rhs.getSubKind()) {
240 case nonloc::LocAsIntegerKind:
241 return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +0000242 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000243 case nonloc::ConcreteIntKind: {
244 // Transform the integer into a location and compare.
245 ASTContext& Ctx = ValMgr.getContext();
246 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
247 i.setIsUnsigned(true);
248 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
249 return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy);
250 }
Mike Stump1eb44332009-09-09 15:08:12 +0000251 default:
Ted Kremeneke8391722009-06-26 00:25:05 +0000252 switch (op) {
253 case BinaryOperator::EQ:
254 return ValMgr.makeTruthVal(false, resultTy);
255 case BinaryOperator::NE:
256 return ValMgr.makeTruthVal(true, resultTy);
257 default:
258 // This case also handles pointer arithmetic.
259 return UnknownVal();
260 }
261 }
Mike Stump1eb44332009-09-09 15:08:12 +0000262 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000263 case nonloc::SymExprValKind: {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000264 nonloc::SymExprVal *selhs = cast<nonloc::SymExprVal>(&lhs);
265
266 // Only handle LHS of the form "$sym op constant", at least for now.
267 const SymIntExpr *symIntExpr =
268 dyn_cast<SymIntExpr>(selhs->getSymbolicExpression());
269
270 if (!symIntExpr)
Ted Kremeneke8391722009-06-26 00:25:05 +0000271 return UnknownVal();
272
Jordy Roseba0f61c2010-06-18 22:49:11 +0000273 // Is this a logical not? (!x is represented as x == 0.)
274 if (op == BinaryOperator::EQ && rhs.isZeroConstant()) {
275 // We know how to negate certain expressions. Simplify them here.
Mike Stump1eb44332009-09-09 15:08:12 +0000276
Ted Kremeneke8391722009-06-26 00:25:05 +0000277 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
278 switch (opc) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000279 default:
280 // We don't know how to negate this operation.
281 // Just handle it as if it were a normal comparison to 0.
282 break;
283 case BinaryOperator::LAnd:
284 case BinaryOperator::LOr:
285 assert(false && "Logical operators handled by branching logic.");
286 return UnknownVal();
287 case BinaryOperator::Assign:
288 case BinaryOperator::MulAssign:
289 case BinaryOperator::DivAssign:
290 case BinaryOperator::RemAssign:
291 case BinaryOperator::AddAssign:
292 case BinaryOperator::SubAssign:
293 case BinaryOperator::ShlAssign:
294 case BinaryOperator::ShrAssign:
295 case BinaryOperator::AndAssign:
296 case BinaryOperator::XorAssign:
297 case BinaryOperator::OrAssign:
298 case BinaryOperator::Comma:
299 assert(false && "'=' and ',' operators handled by GRExprEngine.");
300 return UnknownVal();
301 case BinaryOperator::PtrMemD:
302 case BinaryOperator::PtrMemI:
303 assert(false && "Pointer arithmetic not handled here.");
304 return UnknownVal();
305 case BinaryOperator::LT:
306 case BinaryOperator::GT:
307 case BinaryOperator::LE:
308 case BinaryOperator::GE:
309 case BinaryOperator::EQ:
310 case BinaryOperator::NE:
311 // Negate the comparison and make a value.
312 opc = NegateComparison(opc);
313 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
314 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
315 symIntExpr->getRHS(), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000316 }
317 }
Jordy Roseba0f61c2010-06-18 22:49:11 +0000318
319 // For now, only handle expressions whose RHS is a constant.
320 const nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs);
321 if (!rhsInt)
322 return UnknownVal();
323
324 // If both the LHS and the current expression are additive,
325 // fold their constants.
326 if (BinaryOperator::isAdditiveOp(op)) {
327 BinaryOperator::Opcode lop = symIntExpr->getOpcode();
328 if (BinaryOperator::isAdditiveOp(lop)) {
329 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
330 const llvm::APSInt *newRHS;
331 if (lop == op)
332 newRHS = BVF.EvaluateAPSInt(BinaryOperator::Add,
333 symIntExpr->getRHS(),
334 rhsInt->getValue());
335 else
336 newRHS = BVF.EvaluateAPSInt(BinaryOperator::Sub,
337 symIntExpr->getRHS(),
338 rhsInt->getValue());
339 return ValMgr.makeNonLoc(symIntExpr->getLHS(), lop, *newRHS,
340 resultTy);
341 }
342 }
343
344 // Otherwise, make a SymExprVal out of the expression.
345 return ValMgr.makeNonLoc(symIntExpr, op, rhsInt->getValue(), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347 case nonloc::ConcreteIntKind: {
Ted Kremeneke8391722009-06-26 00:25:05 +0000348 if (isa<nonloc::ConcreteInt>(rhs)) {
349 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
350 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
351 }
352 else {
353 // Swap the left and right sides and flip the operator if doing so
354 // allows us to better reason about the expression (this is a form
355 // of expression canonicalization).
356 NonLoc tmp = rhs;
357 rhs = lhs;
358 lhs = tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Ted Kremeneke8391722009-06-26 00:25:05 +0000360 switch (op) {
361 case BinaryOperator::LT: op = BinaryOperator::GT; continue;
362 case BinaryOperator::GT: op = BinaryOperator::LT; continue;
363 case BinaryOperator::LE: op = BinaryOperator::GE; continue;
364 case BinaryOperator::GE: op = BinaryOperator::LE; continue;
365 case BinaryOperator::EQ:
366 case BinaryOperator::NE:
367 case BinaryOperator::Add:
368 case BinaryOperator::Mul:
369 continue;
370 default:
371 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000372 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000373 }
374 }
375 case nonloc::SymbolValKind: {
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000376 nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs);
377 SymbolRef Sym = slhs->getSymbol();
378
Ted Kremenek9b020342009-10-17 07:39:35 +0000379 // Does the symbol simplify to a constant? If so, "fold" the constant
380 // by setting 'lhs' to a ConcreteInt and try again.
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000381 if (Sym->getType(ValMgr.getContext())->isIntegerType())
382 if (const llvm::APSInt *Constant = state->getSymVal(Sym)) {
Ted Kremenek9b020342009-10-17 07:39:35 +0000383 // The symbol evaluates to a constant. If necessary, promote the
384 // folded constant (LHS) to the result type.
385 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
386 const llvm::APSInt &lhs_I = BVF.Convert(resultTy, *Constant);
387 lhs = nonloc::ConcreteInt(lhs_I);
Ted Kremenekb5deae52009-10-16 20:46:24 +0000388
Ted Kremenek9b020342009-10-17 07:39:35 +0000389 // Also promote the RHS (if necessary).
390
391 // For shifts, it necessary promote the RHS to the result type.
392 if (BinaryOperator::isShiftOp(op))
393 continue;
394
395 // Other operators: do an implicit conversion. This shouldn't be
Ted Kremenekb5deae52009-10-16 20:46:24 +0000396 // necessary once we support truncation/extension of symbolic values.
Ted Kremenekb1d04222009-10-06 03:44:49 +0000397 if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){
Ted Kremenek9b020342009-10-17 07:39:35 +0000398 rhs = nonloc::ConcreteInt(BVF.Convert(resultTy, rhs_I->getValue()));
Ted Kremenekb1d04222009-10-06 03:44:49 +0000399 }
Ted Kremenek9b020342009-10-17 07:39:35 +0000400
401 continue;
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000402 }
403
Ted Kremeneke8391722009-06-26 00:25:05 +0000404 if (isa<nonloc::ConcreteInt>(rhs)) {
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000405 return ValMgr.makeNonLoc(slhs->getSymbol(), op,
Ted Kremeneke8391722009-06-26 00:25:05 +0000406 cast<nonloc::ConcreteInt>(rhs).getValue(),
407 resultTy);
408 }
409
410 return UnknownVal();
411 }
412 }
413 }
414}
415
416SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000417 QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000418 switch (op) {
419 default:
420 return UnknownVal();
421 case BinaryOperator::EQ:
422 case BinaryOperator::NE:
423 return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000424 case BinaryOperator::LT:
425 case BinaryOperator::GT:
426 // FIXME: Generalize. For now, just handle the trivial case where
427 // the two locations are identical.
428 if (lhs == rhs)
429 return ValMgr.makeTruthVal(false, resultTy);
430 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000431 }
432}
433
434SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
435 BinaryOperator::Opcode op,
Mike Stump1eb44332009-09-09 15:08:12 +0000436 Loc lhs, NonLoc rhs, QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000437 // Special case: 'rhs' is an integer that has the same width as a pointer and
438 // we are using the integer location in a comparison. Normally this cannot be
439 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
440 // can generate comparisons that trigger this code.
441 // FIXME: Are all locations guaranteed to have pointer width?
442 if (BinaryOperator::isEqualityOp(op)) {
443 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
444 const llvm::APSInt *x = &rhsInt->getValue();
445 ASTContext &ctx = ValMgr.getContext();
446 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
447 // Convert the signedness of the integer (if necessary).
448 if (x->isSigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000449 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
Ted Kremeneke8391722009-06-26 00:25:05 +0000450
451 return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy);
452 }
453 }
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Ted Kremeneke8391722009-06-26 00:25:05 +0000456 // Delegate pointer arithmetic to the StoreManager.
Zhongxing Xu461147f2010-02-05 05:24:20 +0000457 return state->getStateManager().getStoreManager().EvalBinOp(op, lhs,
Ted Kremeneke8391722009-06-26 00:25:05 +0000458 rhs, resultTy);
459}