blob: fe82276faa5a0fae690ea2132dafa1eb3a348d50 [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);
Jordy Rose43fdb7f2010-06-20 04:56:29 +000037
38 SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op,
39 const llvm::APSInt &RHS, QualType resultTy);
Mike Stump1eb44332009-09-09 15:08:12 +000040};
Ted Kremeneke8391722009-06-26 00:25:05 +000041} // end anonymous namespace
42
43SValuator *clang::CreateSimpleSValuator(ValueManager &valMgr) {
44 return new SimpleSValuator(valMgr);
45}
46
47//===----------------------------------------------------------------------===//
48// Transfer function for Casts.
49//===----------------------------------------------------------------------===//
50
Ted Kremenekdd661142009-07-20 21:39:27 +000051SVal SimpleSValuator::EvalCastNL(NonLoc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenekdd661142009-07-20 21:39:27 +000053 bool isLocType = Loc::IsLocType(castTy);
Mike Stump1eb44332009-09-09 15:08:12 +000054
Ted Kremenek9031dd72009-07-21 00:12:07 +000055 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) {
56 if (isLocType)
Ted Kremenekdd661142009-07-20 21:39:27 +000057 return LI->getLoc();
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenekc50e6df2010-01-11 02:33:26 +000059 // FIXME: Correctly support promotions/truncations.
Mike Stump1eb44332009-09-09 15:08:12 +000060 ASTContext &Ctx = ValMgr.getContext();
Ted Kremenekc50e6df2010-01-11 02:33:26 +000061 unsigned castSize = Ctx.getTypeSize(castTy);
62 if (castSize == LI->getNumBits())
Ted Kremenek9031dd72009-07-21 00:12:07 +000063 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Ted Kremenekc50e6df2010-01-11 02:33:26 +000065 return ValMgr.makeLocAsInteger(LI->getLoc(), castSize);
Ted Kremenek9031dd72009-07-21 00:12:07 +000066 }
67
68 if (const SymExpr *se = val.getAsSymbolicExpression()) {
69 ASTContext &Ctx = ValMgr.getContext();
70 QualType T = Ctx.getCanonicalType(se->getType(Ctx));
71 if (T == Ctx.getCanonicalType(castTy))
72 return val;
Ted Kremenek80417472009-09-25 00:18:15 +000073
74 // FIXME: Remove this hack when we support symbolic truncation/extension.
75 // HACK: If both castTy and T are integers, ignore the cast. This is
76 // not a permanent solution. Eventually we want to precisely handle
77 // extension/truncation of symbolic integers. This prevents us from losing
78 // precision when we assign 'x = y' and 'y' is symbolic and x and y are
79 // different integer types.
80 if (T->isIntegerType() && castTy->isIntegerType())
81 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Ted Kremenek9031dd72009-07-21 00:12:07 +000083 return UnknownVal();
84 }
Mike Stump1eb44332009-09-09 15:08:12 +000085
Ted Kremeneke8391722009-06-26 00:25:05 +000086 if (!isa<nonloc::ConcreteInt>(val))
87 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000088
Ted Kremeneke8391722009-06-26 00:25:05 +000089 // Only handle casts from integers to integers.
90 if (!isLocType && !castTy->isIntegerType())
91 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremeneke8391722009-06-26 00:25:05 +000093 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
94 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
95 i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy));
96
97 if (isLocType)
98 return ValMgr.makeIntLocVal(i);
99 else
100 return ValMgr.makeIntVal(i);
101}
102
Ted Kremenek46537392009-07-16 01:33:37 +0000103SVal SimpleSValuator::EvalCastL(Loc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremeneke8391722009-06-26 00:25:05 +0000105 // Casts from pointers -> pointers, just return the lval.
106 //
107 // Casts from pointers -> references, just return the lval. These
108 // can be introduced by the frontend for corner cases, e.g
109 // casting from va_list* to __builtin_va_list&.
110 //
Ted Kremeneke8391722009-06-26 00:25:05 +0000111 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
112 return val;
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremeneke8391722009-06-26 00:25:05 +0000114 // FIXME: Handle transparent unions where a value can be "transparently"
115 // lifted into a union type.
116 if (castTy->isUnionType())
117 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Ted Kremenekd617b852010-04-16 17:54:33 +0000119 if (castTy->isIntegerType()) {
120 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000121
Ted Kremenekd617b852010-04-16 17:54:33 +0000122 if (!isa<loc::ConcreteInt>(val))
123 return ValMgr.makeLocAsInteger(val, BitWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenekd617b852010-04-16 17:54:33 +0000125 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
126 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
127 i.extOrTrunc(BitWidth);
128 return ValMgr.makeIntVal(i);
129 }
130
131 // All other cases: return 'UnknownVal'. This includes casting pointers
132 // to floats, which is probably badness it itself, but this is a good
133 // intermediate solution until we do something better.
134 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000135}
136
137//===----------------------------------------------------------------------===//
138// Transfer function for unary operators.
139//===----------------------------------------------------------------------===//
140
141SVal SimpleSValuator::EvalMinus(NonLoc val) {
Mike Stump1eb44332009-09-09 15:08:12 +0000142 switch (val.getSubKind()) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000143 case nonloc::ConcreteIntKind:
144 return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr);
145 default:
146 return UnknownVal();
147 }
148}
149
150SVal SimpleSValuator::EvalComplement(NonLoc X) {
151 switch (X.getSubKind()) {
152 case nonloc::ConcreteIntKind:
153 return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr);
154 default:
155 return UnknownVal();
156 }
157}
158
159//===----------------------------------------------------------------------===//
160// Transfer function for binary operators.
161//===----------------------------------------------------------------------===//
162
163static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
164 switch (op) {
165 default:
166 assert(false && "Invalid opcode.");
167 case BinaryOperator::LT: return BinaryOperator::GE;
168 case BinaryOperator::GT: return BinaryOperator::LE;
169 case BinaryOperator::LE: return BinaryOperator::GT;
170 case BinaryOperator::GE: return BinaryOperator::LT;
171 case BinaryOperator::EQ: return BinaryOperator::NE;
172 case BinaryOperator::NE: return BinaryOperator::EQ;
173 }
174}
175
Mike Stump1eb44332009-09-09 15:08:12 +0000176// Equality operators for Locs.
Ted Kremeneke8391722009-06-26 00:25:05 +0000177// FIXME: All this logic will be revamped when we have MemRegion::getLocation()
178// implemented.
179
180static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual,
181 QualType resultTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremeneke8391722009-06-26 00:25:05 +0000183 switch (lhs.getSubKind()) {
184 default:
185 assert(false && "EQ/NE not implemented for this Loc.");
186 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Ted Kremeneke8391722009-06-26 00:25:05 +0000188 case loc::ConcreteIntKind: {
189 if (SymbolRef rSym = rhs.getAsSymbol())
190 return ValMgr.makeNonLoc(rSym,
191 isEqual ? BinaryOperator::EQ
192 : BinaryOperator::NE,
193 cast<loc::ConcreteInt>(lhs).getValue(),
194 resultTy);
195 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000196 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000197 case loc::MemRegionKind: {
198 if (SymbolRef lSym = lhs.getAsLocSymbol()) {
199 if (isa<loc::ConcreteInt>(rhs)) {
200 return ValMgr.makeNonLoc(lSym,
201 isEqual ? BinaryOperator::EQ
202 : BinaryOperator::NE,
203 cast<loc::ConcreteInt>(rhs).getValue(),
204 resultTy);
205 }
206 }
207 break;
208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremeneke8391722009-06-26 00:25:05 +0000210 case loc::GotoLabelKind:
211 break;
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremeneke8391722009-06-26 00:25:05 +0000214 return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy);
215}
216
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000217SVal SimpleSValuator::MakeSymIntVal(const SymExpr *LHS,
218 BinaryOperator::Opcode op,
219 const llvm::APSInt &RHS,
220 QualType resultTy) {
221 bool isIdempotent = false;
222
223 // Check for a few special cases with known reductions first.
224 switch (op) {
225 default:
226 // We can't reduce this case; just treat it normally.
227 break;
228 case BinaryOperator::Mul:
229 // a*0 and a*1
230 if (RHS == 0)
231 return ValMgr.makeIntVal(0, resultTy);
232 else if (RHS == 1)
233 isIdempotent = true;
234 break;
235 case BinaryOperator::Div:
236 // a/0 and a/1
237 if (RHS == 0)
238 // This is also handled elsewhere.
239 return UndefinedVal();
240 else if (RHS == 1)
241 isIdempotent = true;
242 break;
243 case BinaryOperator::Rem:
244 // a%0 and a%1
245 if (RHS == 0)
246 // This is also handled elsewhere.
247 return UndefinedVal();
248 else if (RHS == 1)
249 return ValMgr.makeIntVal(0, resultTy);
250 break;
251 case BinaryOperator::Add:
252 case BinaryOperator::Sub:
253 case BinaryOperator::Shl:
254 case BinaryOperator::Shr:
255 case BinaryOperator::Xor:
256 // a+0, a-0, a<<0, a>>0, a^0
257 if (RHS == 0)
258 isIdempotent = true;
259 break;
260 case BinaryOperator::And:
261 // a&0 and a&(~0)
262 if (RHS == 0)
263 return ValMgr.makeIntVal(0, resultTy);
264 else if (RHS.isAllOnesValue())
265 isIdempotent = true;
266 break;
267 case BinaryOperator::Or:
268 // a|0 and a|(~0)
269 if (RHS == 0)
270 isIdempotent = true;
271 else if (RHS.isAllOnesValue()) {
272 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
273 const llvm::APSInt &Result = BVF.Convert(resultTy, RHS);
274 return nonloc::ConcreteInt(Result);
275 }
276 break;
277 }
278
279 // Idempotent ops (like a*1) can still change the type of an expression.
280 // Wrap the LHS up in a NonLoc again and let EvalCastNL do the dirty work.
281 if (isIdempotent)
282 if (SymbolRef LHSSym = dyn_cast<SymbolData>(LHS))
283 return EvalCastNL(nonloc::SymbolVal(LHSSym), resultTy);
284 else
285 return EvalCastNL(nonloc::SymExprVal(LHS), resultTy);
286
287 // If we reach this point, the expression cannot be simplified.
288 // Make a SymExprVal for the entire thing.
289 return ValMgr.makeNonLoc(LHS, op, RHS, resultTy);
290}
291
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000292SVal SimpleSValuator::EvalBinOpNN(const GRState *state,
293 BinaryOperator::Opcode op,
Ted Kremeneke8391722009-06-26 00:25:05 +0000294 NonLoc lhs, NonLoc rhs,
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000295 QualType resultTy) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000296 // Handle trivial case where left-side and right-side are the same.
297 if (lhs == rhs)
298 switch (op) {
299 default:
300 break;
301 case BinaryOperator::EQ:
302 case BinaryOperator::LE:
303 case BinaryOperator::GE:
304 return ValMgr.makeTruthVal(true, resultTy);
305 case BinaryOperator::LT:
306 case BinaryOperator::GT:
307 case BinaryOperator::NE:
308 return ValMgr.makeTruthVal(false, resultTy);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000309 case BinaryOperator::Xor:
310 case BinaryOperator::Sub:
311 return ValMgr.makeIntVal(0, resultTy);
312 case BinaryOperator::Or:
313 case BinaryOperator::And:
314 return EvalCastNL(lhs, resultTy);
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Ted Kremeneke8391722009-06-26 00:25:05 +0000317 while (1) {
318 switch (lhs.getSubKind()) {
319 default:
Mike Stump1eb44332009-09-09 15:08:12 +0000320 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000321 case nonloc::LocAsIntegerKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000322 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
Ted Kremeneke8391722009-06-26 00:25:05 +0000323 switch (rhs.getSubKind()) {
324 case nonloc::LocAsIntegerKind:
325 return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +0000326 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000327 case nonloc::ConcreteIntKind: {
328 // Transform the integer into a location and compare.
329 ASTContext& Ctx = ValMgr.getContext();
330 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
331 i.setIsUnsigned(true);
332 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
333 return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy);
334 }
Mike Stump1eb44332009-09-09 15:08:12 +0000335 default:
Ted Kremeneke8391722009-06-26 00:25:05 +0000336 switch (op) {
337 case BinaryOperator::EQ:
338 return ValMgr.makeTruthVal(false, resultTy);
339 case BinaryOperator::NE:
340 return ValMgr.makeTruthVal(true, resultTy);
341 default:
342 // This case also handles pointer arithmetic.
343 return UnknownVal();
344 }
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000347 case nonloc::SymExprValKind: {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000348 nonloc::SymExprVal *selhs = cast<nonloc::SymExprVal>(&lhs);
349
350 // Only handle LHS of the form "$sym op constant", at least for now.
351 const SymIntExpr *symIntExpr =
352 dyn_cast<SymIntExpr>(selhs->getSymbolicExpression());
353
354 if (!symIntExpr)
Ted Kremeneke8391722009-06-26 00:25:05 +0000355 return UnknownVal();
356
Jordy Roseba0f61c2010-06-18 22:49:11 +0000357 // Is this a logical not? (!x is represented as x == 0.)
358 if (op == BinaryOperator::EQ && rhs.isZeroConstant()) {
359 // We know how to negate certain expressions. Simplify them here.
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Ted Kremeneke8391722009-06-26 00:25:05 +0000361 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
362 switch (opc) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000363 default:
364 // We don't know how to negate this operation.
365 // Just handle it as if it were a normal comparison to 0.
366 break;
367 case BinaryOperator::LAnd:
368 case BinaryOperator::LOr:
369 assert(false && "Logical operators handled by branching logic.");
370 return UnknownVal();
371 case BinaryOperator::Assign:
372 case BinaryOperator::MulAssign:
373 case BinaryOperator::DivAssign:
374 case BinaryOperator::RemAssign:
375 case BinaryOperator::AddAssign:
376 case BinaryOperator::SubAssign:
377 case BinaryOperator::ShlAssign:
378 case BinaryOperator::ShrAssign:
379 case BinaryOperator::AndAssign:
380 case BinaryOperator::XorAssign:
381 case BinaryOperator::OrAssign:
382 case BinaryOperator::Comma:
383 assert(false && "'=' and ',' operators handled by GRExprEngine.");
384 return UnknownVal();
385 case BinaryOperator::PtrMemD:
386 case BinaryOperator::PtrMemI:
387 assert(false && "Pointer arithmetic not handled here.");
388 return UnknownVal();
389 case BinaryOperator::LT:
390 case BinaryOperator::GT:
391 case BinaryOperator::LE:
392 case BinaryOperator::GE:
393 case BinaryOperator::EQ:
394 case BinaryOperator::NE:
395 // Negate the comparison and make a value.
396 opc = NegateComparison(opc);
397 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
398 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
399 symIntExpr->getRHS(), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000400 }
401 }
Jordy Roseba0f61c2010-06-18 22:49:11 +0000402
403 // For now, only handle expressions whose RHS is a constant.
404 const nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs);
405 if (!rhsInt)
406 return UnknownVal();
407
408 // If both the LHS and the current expression are additive,
409 // fold their constants.
410 if (BinaryOperator::isAdditiveOp(op)) {
411 BinaryOperator::Opcode lop = symIntExpr->getOpcode();
412 if (BinaryOperator::isAdditiveOp(lop)) {
413 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
414 const llvm::APSInt *newRHS;
415 if (lop == op)
416 newRHS = BVF.EvaluateAPSInt(BinaryOperator::Add,
417 symIntExpr->getRHS(),
418 rhsInt->getValue());
419 else
420 newRHS = BVF.EvaluateAPSInt(BinaryOperator::Sub,
421 symIntExpr->getRHS(),
422 rhsInt->getValue());
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000423 return MakeSymIntVal(symIntExpr->getLHS(), lop, *newRHS, resultTy);
Jordy Roseba0f61c2010-06-18 22:49:11 +0000424 }
425 }
426
427 // Otherwise, make a SymExprVal out of the expression.
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000428 return MakeSymIntVal(symIntExpr, op, rhsInt->getValue(), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000429 }
Mike Stump1eb44332009-09-09 15:08:12 +0000430 case nonloc::ConcreteIntKind: {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000431 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
432
Ted Kremeneke8391722009-06-26 00:25:05 +0000433 if (isa<nonloc::ConcreteInt>(rhs)) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000434 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000435 } else {
436 const llvm::APSInt& lhsValue = lhsInt.getValue();
437
Ted Kremeneke8391722009-06-26 00:25:05 +0000438 // Swap the left and right sides and flip the operator if doing so
439 // allows us to better reason about the expression (this is a form
440 // of expression canonicalization).
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000441 // While we're at it, catch some special cases for non-commutative ops.
Ted Kremeneke8391722009-06-26 00:25:05 +0000442 NonLoc tmp = rhs;
443 rhs = lhs;
444 lhs = tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Ted Kremeneke8391722009-06-26 00:25:05 +0000446 switch (op) {
447 case BinaryOperator::LT: op = BinaryOperator::GT; continue;
448 case BinaryOperator::GT: op = BinaryOperator::LT; continue;
449 case BinaryOperator::LE: op = BinaryOperator::GE; continue;
450 case BinaryOperator::GE: op = BinaryOperator::LE; continue;
451 case BinaryOperator::EQ:
452 case BinaryOperator::NE:
453 case BinaryOperator::Add:
454 case BinaryOperator::Mul:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000455 case BinaryOperator::And:
456 case BinaryOperator::Xor:
457 case BinaryOperator::Or:
Ted Kremeneke8391722009-06-26 00:25:05 +0000458 continue;
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000459 case BinaryOperator::Shr:
460 if (lhsValue.isAllOnesValue() && lhsValue.isSigned())
461 // At this point lhs and rhs have been swapped.
462 return rhs;
463 // FALL-THROUGH
464 case BinaryOperator::Shl:
465 if (lhsValue == 0)
466 // At this point lhs and rhs have been swapped.
467 return rhs;
468 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000469 default:
470 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000471 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000472 }
473 }
474 case nonloc::SymbolValKind: {
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000475 nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs);
476 SymbolRef Sym = slhs->getSymbol();
477
Ted Kremenek9b020342009-10-17 07:39:35 +0000478 // Does the symbol simplify to a constant? If so, "fold" the constant
479 // by setting 'lhs' to a ConcreteInt and try again.
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000480 if (Sym->getType(ValMgr.getContext())->isIntegerType())
481 if (const llvm::APSInt *Constant = state->getSymVal(Sym)) {
Ted Kremenek9b020342009-10-17 07:39:35 +0000482 // The symbol evaluates to a constant. If necessary, promote the
483 // folded constant (LHS) to the result type.
484 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
485 const llvm::APSInt &lhs_I = BVF.Convert(resultTy, *Constant);
486 lhs = nonloc::ConcreteInt(lhs_I);
Ted Kremenekb5deae52009-10-16 20:46:24 +0000487
Ted Kremenek9b020342009-10-17 07:39:35 +0000488 // Also promote the RHS (if necessary).
489
490 // For shifts, it necessary promote the RHS to the result type.
491 if (BinaryOperator::isShiftOp(op))
492 continue;
493
494 // Other operators: do an implicit conversion. This shouldn't be
Ted Kremenekb5deae52009-10-16 20:46:24 +0000495 // necessary once we support truncation/extension of symbolic values.
Ted Kremenekb1d04222009-10-06 03:44:49 +0000496 if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){
Ted Kremenek9b020342009-10-17 07:39:35 +0000497 rhs = nonloc::ConcreteInt(BVF.Convert(resultTy, rhs_I->getValue()));
Ted Kremenekb1d04222009-10-06 03:44:49 +0000498 }
Ted Kremenek9b020342009-10-17 07:39:35 +0000499
500 continue;
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000501 }
502
Ted Kremeneke8391722009-06-26 00:25:05 +0000503 if (isa<nonloc::ConcreteInt>(rhs)) {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000504 return MakeSymIntVal(slhs->getSymbol(), op,
505 cast<nonloc::ConcreteInt>(rhs).getValue(),
506 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000507 }
508
509 return UnknownVal();
510 }
511 }
512 }
513}
514
515SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000516 QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000517 switch (op) {
518 default:
519 return UnknownVal();
520 case BinaryOperator::EQ:
521 case BinaryOperator::NE:
522 return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy);
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000523 case BinaryOperator::LT:
524 case BinaryOperator::GT:
525 // FIXME: Generalize. For now, just handle the trivial case where
526 // the two locations are identical.
527 if (lhs == rhs)
528 return ValMgr.makeTruthVal(false, resultTy);
529 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000530 }
531}
532
533SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
534 BinaryOperator::Opcode op,
Mike Stump1eb44332009-09-09 15:08:12 +0000535 Loc lhs, NonLoc rhs, QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000536 // Special case: 'rhs' is an integer that has the same width as a pointer and
537 // we are using the integer location in a comparison. Normally this cannot be
538 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
539 // can generate comparisons that trigger this code.
540 // FIXME: Are all locations guaranteed to have pointer width?
541 if (BinaryOperator::isEqualityOp(op)) {
542 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
543 const llvm::APSInt *x = &rhsInt->getValue();
544 ASTContext &ctx = ValMgr.getContext();
545 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
546 // Convert the signedness of the integer (if necessary).
547 if (x->isSigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000548 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
Ted Kremeneke8391722009-06-26 00:25:05 +0000549
550 return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy);
551 }
552 }
553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Ted Kremeneke8391722009-06-26 00:25:05 +0000555 // Delegate pointer arithmetic to the StoreManager.
Zhongxing Xu461147f2010-02-05 05:24:20 +0000556 return state->getStateManager().getStoreManager().EvalBinOp(op, lhs,
Ted Kremeneke8391722009-06-26 00:25:05 +0000557 rhs, resultTy);
558}