blob: 0f4fe07bb70d0f0b785f30600e811b5ffaa6eca1 [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);
Jordy Roseeac4a002010-06-28 08:26:15 +000033 virtual SVal EvalBinOpLL(const GRState *state, BinaryOperator::Opcode op,
34 Loc lhs, Loc rhs, QualType resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +000035 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
Jordy Roseeac4a002010-06-28 08:26:15 +0000176static BinaryOperator::Opcode ReverseComparison(BinaryOperator::Opcode op) {
177 switch (op) {
178 default:
179 assert(false && "Invalid opcode.");
180 case BinaryOperator::LT: return BinaryOperator::GT;
181 case BinaryOperator::GT: return BinaryOperator::LT;
182 case BinaryOperator::LE: return BinaryOperator::GE;
183 case BinaryOperator::GE: return BinaryOperator::LE;
184 case BinaryOperator::EQ:
185 case BinaryOperator::NE:
186 return op;
Ted Kremeneke8391722009-06-26 00:25:05 +0000187 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000188}
189
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000190SVal SimpleSValuator::MakeSymIntVal(const SymExpr *LHS,
191 BinaryOperator::Opcode op,
192 const llvm::APSInt &RHS,
193 QualType resultTy) {
194 bool isIdempotent = false;
195
196 // Check for a few special cases with known reductions first.
197 switch (op) {
198 default:
199 // We can't reduce this case; just treat it normally.
200 break;
201 case BinaryOperator::Mul:
202 // a*0 and a*1
203 if (RHS == 0)
204 return ValMgr.makeIntVal(0, resultTy);
205 else if (RHS == 1)
206 isIdempotent = true;
207 break;
208 case BinaryOperator::Div:
209 // a/0 and a/1
210 if (RHS == 0)
211 // This is also handled elsewhere.
212 return UndefinedVal();
213 else if (RHS == 1)
214 isIdempotent = true;
215 break;
216 case BinaryOperator::Rem:
217 // a%0 and a%1
218 if (RHS == 0)
219 // This is also handled elsewhere.
220 return UndefinedVal();
221 else if (RHS == 1)
222 return ValMgr.makeIntVal(0, resultTy);
223 break;
224 case BinaryOperator::Add:
225 case BinaryOperator::Sub:
226 case BinaryOperator::Shl:
227 case BinaryOperator::Shr:
228 case BinaryOperator::Xor:
229 // a+0, a-0, a<<0, a>>0, a^0
230 if (RHS == 0)
231 isIdempotent = true;
232 break;
233 case BinaryOperator::And:
234 // a&0 and a&(~0)
235 if (RHS == 0)
236 return ValMgr.makeIntVal(0, resultTy);
237 else if (RHS.isAllOnesValue())
238 isIdempotent = true;
239 break;
240 case BinaryOperator::Or:
241 // a|0 and a|(~0)
242 if (RHS == 0)
243 isIdempotent = true;
244 else if (RHS.isAllOnesValue()) {
245 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
246 const llvm::APSInt &Result = BVF.Convert(resultTy, RHS);
247 return nonloc::ConcreteInt(Result);
248 }
249 break;
250 }
251
252 // Idempotent ops (like a*1) can still change the type of an expression.
253 // Wrap the LHS up in a NonLoc again and let EvalCastNL do the dirty work.
Benjamin Kramer24ada872010-06-20 10:20:36 +0000254 if (isIdempotent) {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000255 if (SymbolRef LHSSym = dyn_cast<SymbolData>(LHS))
256 return EvalCastNL(nonloc::SymbolVal(LHSSym), resultTy);
Benjamin Kramer24ada872010-06-20 10:20:36 +0000257 return EvalCastNL(nonloc::SymExprVal(LHS), resultTy);
258 }
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000259
260 // If we reach this point, the expression cannot be simplified.
261 // Make a SymExprVal for the entire thing.
262 return ValMgr.makeNonLoc(LHS, op, RHS, resultTy);
263}
264
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000265SVal SimpleSValuator::EvalBinOpNN(const GRState *state,
266 BinaryOperator::Opcode op,
Ted Kremeneke8391722009-06-26 00:25:05 +0000267 NonLoc lhs, NonLoc rhs,
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000268 QualType resultTy) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000269 // Handle trivial case where left-side and right-side are the same.
270 if (lhs == rhs)
271 switch (op) {
272 default:
273 break;
274 case BinaryOperator::EQ:
275 case BinaryOperator::LE:
276 case BinaryOperator::GE:
277 return ValMgr.makeTruthVal(true, resultTy);
278 case BinaryOperator::LT:
279 case BinaryOperator::GT:
280 case BinaryOperator::NE:
281 return ValMgr.makeTruthVal(false, resultTy);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000282 case BinaryOperator::Xor:
283 case BinaryOperator::Sub:
284 return ValMgr.makeIntVal(0, resultTy);
285 case BinaryOperator::Or:
286 case BinaryOperator::And:
287 return EvalCastNL(lhs, resultTy);
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000288 }
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Ted Kremeneke8391722009-06-26 00:25:05 +0000290 while (1) {
291 switch (lhs.getSubKind()) {
292 default:
Mike Stump1eb44332009-09-09 15:08:12 +0000293 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000294 case nonloc::LocAsIntegerKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000295 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
Ted Kremeneke8391722009-06-26 00:25:05 +0000296 switch (rhs.getSubKind()) {
297 case nonloc::LocAsIntegerKind:
Jordy Roseeac4a002010-06-28 08:26:15 +0000298 return EvalBinOpLL(state, op, lhsL,
299 cast<nonloc::LocAsInteger>(rhs).getLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +0000300 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000301 case nonloc::ConcreteIntKind: {
302 // Transform the integer into a location and compare.
303 ASTContext& Ctx = ValMgr.getContext();
304 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
305 i.setIsUnsigned(true);
306 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
Jordy Roseeac4a002010-06-28 08:26:15 +0000307 return EvalBinOpLL(state, op, lhsL, ValMgr.makeLoc(i), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000308 }
Mike Stump1eb44332009-09-09 15:08:12 +0000309 default:
Ted Kremeneke8391722009-06-26 00:25:05 +0000310 switch (op) {
311 case BinaryOperator::EQ:
312 return ValMgr.makeTruthVal(false, resultTy);
313 case BinaryOperator::NE:
314 return ValMgr.makeTruthVal(true, resultTy);
315 default:
316 // This case also handles pointer arithmetic.
317 return UnknownVal();
318 }
319 }
Mike Stump1eb44332009-09-09 15:08:12 +0000320 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000321 case nonloc::SymExprValKind: {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000322 nonloc::SymExprVal *selhs = cast<nonloc::SymExprVal>(&lhs);
323
324 // Only handle LHS of the form "$sym op constant", at least for now.
325 const SymIntExpr *symIntExpr =
326 dyn_cast<SymIntExpr>(selhs->getSymbolicExpression());
327
328 if (!symIntExpr)
Ted Kremeneke8391722009-06-26 00:25:05 +0000329 return UnknownVal();
330
Jordy Roseba0f61c2010-06-18 22:49:11 +0000331 // Is this a logical not? (!x is represented as x == 0.)
332 if (op == BinaryOperator::EQ && rhs.isZeroConstant()) {
333 // We know how to negate certain expressions. Simplify them here.
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Ted Kremeneke8391722009-06-26 00:25:05 +0000335 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
336 switch (opc) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000337 default:
338 // We don't know how to negate this operation.
339 // Just handle it as if it were a normal comparison to 0.
340 break;
341 case BinaryOperator::LAnd:
342 case BinaryOperator::LOr:
343 assert(false && "Logical operators handled by branching logic.");
344 return UnknownVal();
345 case BinaryOperator::Assign:
346 case BinaryOperator::MulAssign:
347 case BinaryOperator::DivAssign:
348 case BinaryOperator::RemAssign:
349 case BinaryOperator::AddAssign:
350 case BinaryOperator::SubAssign:
351 case BinaryOperator::ShlAssign:
352 case BinaryOperator::ShrAssign:
353 case BinaryOperator::AndAssign:
354 case BinaryOperator::XorAssign:
355 case BinaryOperator::OrAssign:
356 case BinaryOperator::Comma:
357 assert(false && "'=' and ',' operators handled by GRExprEngine.");
358 return UnknownVal();
359 case BinaryOperator::PtrMemD:
360 case BinaryOperator::PtrMemI:
361 assert(false && "Pointer arithmetic not handled here.");
362 return UnknownVal();
363 case BinaryOperator::LT:
364 case BinaryOperator::GT:
365 case BinaryOperator::LE:
366 case BinaryOperator::GE:
367 case BinaryOperator::EQ:
368 case BinaryOperator::NE:
369 // Negate the comparison and make a value.
370 opc = NegateComparison(opc);
371 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
372 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
373 symIntExpr->getRHS(), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000374 }
375 }
Jordy Roseba0f61c2010-06-18 22:49:11 +0000376
377 // For now, only handle expressions whose RHS is a constant.
378 const nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs);
379 if (!rhsInt)
380 return UnknownVal();
381
382 // If both the LHS and the current expression are additive,
383 // fold their constants.
384 if (BinaryOperator::isAdditiveOp(op)) {
385 BinaryOperator::Opcode lop = symIntExpr->getOpcode();
386 if (BinaryOperator::isAdditiveOp(lop)) {
387 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
Jordy Roseb4954a42010-06-21 20:15:15 +0000388
389 // resultTy may not be the best type to convert to, but it's
390 // probably the best choice in expressions with mixed type
391 // (such as x+1U+2LL). The rules for implicit conversions should
392 // choose a reasonable type to preserve the expression, and will
393 // at least match how the value is going to be used.
394 const llvm::APSInt &first =
395 BVF.Convert(resultTy, symIntExpr->getRHS());
396 const llvm::APSInt &second =
397 BVF.Convert(resultTy, rhsInt->getValue());
398
Jordy Roseba0f61c2010-06-18 22:49:11 +0000399 const llvm::APSInt *newRHS;
400 if (lop == op)
Jordy Roseb4954a42010-06-21 20:15:15 +0000401 newRHS = BVF.EvaluateAPSInt(BinaryOperator::Add, first, second);
Jordy Roseba0f61c2010-06-18 22:49:11 +0000402 else
Jordy Roseb4954a42010-06-21 20:15:15 +0000403 newRHS = BVF.EvaluateAPSInt(BinaryOperator::Sub, first, second);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000404 return MakeSymIntVal(symIntExpr->getLHS(), lop, *newRHS, resultTy);
Jordy Roseba0f61c2010-06-18 22:49:11 +0000405 }
406 }
407
408 // Otherwise, make a SymExprVal out of the expression.
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000409 return MakeSymIntVal(symIntExpr, op, rhsInt->getValue(), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000410 }
Mike Stump1eb44332009-09-09 15:08:12 +0000411 case nonloc::ConcreteIntKind: {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000412 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
413
Ted Kremeneke8391722009-06-26 00:25:05 +0000414 if (isa<nonloc::ConcreteInt>(rhs)) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000415 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000416 } else {
417 const llvm::APSInt& lhsValue = lhsInt.getValue();
418
Ted Kremeneke8391722009-06-26 00:25:05 +0000419 // Swap the left and right sides and flip the operator if doing so
420 // allows us to better reason about the expression (this is a form
421 // of expression canonicalization).
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000422 // While we're at it, catch some special cases for non-commutative ops.
Ted Kremeneke8391722009-06-26 00:25:05 +0000423 NonLoc tmp = rhs;
424 rhs = lhs;
425 lhs = tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Ted Kremeneke8391722009-06-26 00:25:05 +0000427 switch (op) {
Jordy Roseeac4a002010-06-28 08:26:15 +0000428 case BinaryOperator::LT:
429 case BinaryOperator::GT:
430 case BinaryOperator::LE:
431 case BinaryOperator::GE:
432 op = ReverseComparison(op);
433 continue;
Ted Kremeneke8391722009-06-26 00:25:05 +0000434 case BinaryOperator::EQ:
435 case BinaryOperator::NE:
436 case BinaryOperator::Add:
437 case BinaryOperator::Mul:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000438 case BinaryOperator::And:
439 case BinaryOperator::Xor:
440 case BinaryOperator::Or:
Ted Kremeneke8391722009-06-26 00:25:05 +0000441 continue;
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000442 case BinaryOperator::Shr:
443 if (lhsValue.isAllOnesValue() && lhsValue.isSigned())
444 // At this point lhs and rhs have been swapped.
445 return rhs;
446 // FALL-THROUGH
447 case BinaryOperator::Shl:
448 if (lhsValue == 0)
449 // At this point lhs and rhs have been swapped.
450 return rhs;
451 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000452 default:
453 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000454 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000455 }
456 }
457 case nonloc::SymbolValKind: {
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000458 nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs);
459 SymbolRef Sym = slhs->getSymbol();
460
Ted Kremenek9b020342009-10-17 07:39:35 +0000461 // Does the symbol simplify to a constant? If so, "fold" the constant
462 // by setting 'lhs' to a ConcreteInt and try again.
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000463 if (Sym->getType(ValMgr.getContext())->isIntegerType())
464 if (const llvm::APSInt *Constant = state->getSymVal(Sym)) {
Ted Kremenek9b020342009-10-17 07:39:35 +0000465 // The symbol evaluates to a constant. If necessary, promote the
466 // folded constant (LHS) to the result type.
467 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
468 const llvm::APSInt &lhs_I = BVF.Convert(resultTy, *Constant);
469 lhs = nonloc::ConcreteInt(lhs_I);
Ted Kremenekb5deae52009-10-16 20:46:24 +0000470
Ted Kremenek9b020342009-10-17 07:39:35 +0000471 // Also promote the RHS (if necessary).
472
473 // For shifts, it necessary promote the RHS to the result type.
474 if (BinaryOperator::isShiftOp(op))
475 continue;
476
477 // Other operators: do an implicit conversion. This shouldn't be
Ted Kremenekb5deae52009-10-16 20:46:24 +0000478 // necessary once we support truncation/extension of symbolic values.
Ted Kremenekb1d04222009-10-06 03:44:49 +0000479 if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){
Ted Kremenek9b020342009-10-17 07:39:35 +0000480 rhs = nonloc::ConcreteInt(BVF.Convert(resultTy, rhs_I->getValue()));
Ted Kremenekb1d04222009-10-06 03:44:49 +0000481 }
Ted Kremenek9b020342009-10-17 07:39:35 +0000482
483 continue;
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000484 }
485
Ted Kremeneke8391722009-06-26 00:25:05 +0000486 if (isa<nonloc::ConcreteInt>(rhs)) {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000487 return MakeSymIntVal(slhs->getSymbol(), op,
488 cast<nonloc::ConcreteInt>(rhs).getValue(),
489 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000490 }
491
492 return UnknownVal();
493 }
494 }
495 }
496}
497
Jordy Roseeac4a002010-06-28 08:26:15 +0000498// FIXME: all this logic will change if/when we have MemRegion::getLocation().
499SVal SimpleSValuator::EvalBinOpLL(const GRState *state,
500 BinaryOperator::Opcode op,
501 Loc lhs, Loc rhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000502 QualType resultTy) {
Jordy Roseeac4a002010-06-28 08:26:15 +0000503 // Only comparisons and subtractions are valid operations on two pointers.
504 // See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15].
505 assert(BinaryOperator::isComparisonOp(op) || op == BinaryOperator::Sub);
506
507 // Special cases for when both sides are identical.
508 if (lhs == rhs) {
509 switch (op) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000510 default:
Jordy Roseeac4a002010-06-28 08:26:15 +0000511 assert(false && "Unimplemented operation for two identical values");
Ted Kremeneke8391722009-06-26 00:25:05 +0000512 return UnknownVal();
Jordy Roseeac4a002010-06-28 08:26:15 +0000513 case BinaryOperator::Sub:
514 return ValMgr.makeZeroVal(resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000515 case BinaryOperator::EQ:
Jordy Roseeac4a002010-06-28 08:26:15 +0000516 case BinaryOperator::LE:
517 case BinaryOperator::GE:
518 return ValMgr.makeTruthVal(true, resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000519 case BinaryOperator::NE:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000520 case BinaryOperator::LT:
521 case BinaryOperator::GT:
Jordy Roseeac4a002010-06-28 08:26:15 +0000522 return ValMgr.makeTruthVal(false, resultTy);
523 }
524 }
525
526 switch (lhs.getSubKind()) {
527 default:
528 assert(false && "Ordering not implemented for this Loc.");
529 return UnknownVal();
530
531 case loc::GotoLabelKind:
532 // The only thing we know about labels is that they're non-null.
533 if (rhs.isZeroConstant()) {
534 switch (op) {
535 default:
536 break;
537 case BinaryOperator::Sub:
538 return EvalCastL(lhs, resultTy);
539 case BinaryOperator::EQ:
540 case BinaryOperator::LE:
541 case BinaryOperator::LT:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000542 return ValMgr.makeTruthVal(false, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000543 case BinaryOperator::NE:
544 case BinaryOperator::GT:
545 case BinaryOperator::GE:
546 return ValMgr.makeTruthVal(true, resultTy);
547 }
548 }
549 // There may be two labels for the same location, and a function region may
550 // have the same address as a label at the start of the function (depending
551 // on the ABI).
552 // FIXME: we can probably do a comparison against other MemRegions, though.
553 // FIXME: is there a way to tell if two labels refer to the same location?
554 return UnknownVal();
555
556 case loc::ConcreteIntKind: {
557 // If one of the operands is a symbol and the other is a constant,
558 // build an expression for use by the constraint manager.
559 if (SymbolRef rSym = rhs.getAsLocSymbol()) {
560 // We can only build expressions with symbols on the left,
561 // so we need a reversible operator.
562 if (!BinaryOperator::isComparisonOp(op))
563 return UnknownVal();
564
565 const llvm::APSInt &lVal = cast<loc::ConcreteInt>(lhs).getValue();
566 return ValMgr.makeNonLoc(rSym, ReverseComparison(op), lVal, resultTy);
567 }
568
569 // If both operands are constants, just perform the operation.
570 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
571 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
572 SVal ResultVal = cast<loc::ConcreteInt>(lhs).EvalBinOp(BVF, op, *rInt);
573 if (Loc *Result = dyn_cast<Loc>(&ResultVal))
574 return EvalCastL(*Result, resultTy);
575 else
576 return UnknownVal();
577 }
578
579 // Special case comparisons against NULL.
580 // This must come after the test if the RHS is a symbol, which is used to
581 // build constraints. The address of any non-symbolic region is guaranteed
582 // to be non-NULL, as is any label.
583 assert(isa<loc::MemRegionVal>(rhs) || isa<loc::GotoLabel>(rhs));
584 if (lhs.isZeroConstant()) {
585 switch (op) {
586 default:
587 break;
588 case BinaryOperator::EQ:
589 case BinaryOperator::GT:
590 case BinaryOperator::GE:
591 return ValMgr.makeTruthVal(false, resultTy);
592 case BinaryOperator::NE:
593 case BinaryOperator::LT:
594 case BinaryOperator::LE:
595 return ValMgr.makeTruthVal(true, resultTy);
596 }
597 }
598
599 // Comparing an arbitrary integer to a region or label address is
600 // completely unknowable.
601 return UnknownVal();
602 }
603 case loc::MemRegionKind: {
604 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
605 // If one of the operands is a symbol and the other is a constant,
606 // build an expression for use by the constraint manager.
607 if (SymbolRef lSym = lhs.getAsLocSymbol())
608 return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy);
609
610 // Special case comparisons to NULL.
611 // This must come after the test if the LHS is a symbol, which is used to
612 // build constraints. The address of any non-symbolic region is guaranteed
613 // to be non-NULL.
614 if (rInt->isZeroConstant()) {
615 switch (op) {
616 default:
617 break;
618 case BinaryOperator::Sub:
619 return EvalCastL(lhs, resultTy);
620 case BinaryOperator::EQ:
621 case BinaryOperator::LT:
622 case BinaryOperator::LE:
623 return ValMgr.makeTruthVal(false, resultTy);
624 case BinaryOperator::NE:
625 case BinaryOperator::GT:
626 case BinaryOperator::GE:
627 return ValMgr.makeTruthVal(true, resultTy);
628 }
629 }
630
631 // Comparing a region to an arbitrary integer is completely unknowable.
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000632 return UnknownVal();
Jordy Roseeac4a002010-06-28 08:26:15 +0000633 }
634
635 // Get both values as regions, if possible.
636 const MemRegion *LeftMR = lhs.getAsRegion();
637 assert(LeftMR && "MemRegionKind SVal doesn't have a region!");
638
639 const MemRegion *RightMR = rhs.getAsRegion();
640 if (!RightMR)
641 // The RHS is probably a label, which in theory could address a region.
642 // FIXME: we can probably make a more useful statement about non-code
643 // regions, though.
644 return UnknownVal();
645
646 // If both values wrap regions, see if they're from different base regions.
647 const MemRegion *LeftBase = LeftMR->getBaseRegion();
648 const MemRegion *RightBase = RightMR->getBaseRegion();
649 if (LeftBase != RightBase &&
650 !isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) {
651 switch (op) {
652 default:
653 return UnknownVal();
654 case BinaryOperator::EQ:
655 return ValMgr.makeTruthVal(false, resultTy);
656 case BinaryOperator::NE:
657 return ValMgr.makeTruthVal(true, resultTy);
658 }
659 }
660
661 // The two regions are from the same base region. See if they're both a
662 // type of region we know how to compare.
663
664 // FIXME: If/when there is a getAsRawOffset() for FieldRegions, this
665 // ElementRegion path and the FieldRegion path below should be unified.
666 if (const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR)) {
667 // First see if the right region is also an ElementRegion.
668 const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR);
669 if (!RightER)
670 return UnknownVal();
671
672 // Next, see if the two ERs have the same super-region and matching types.
673 // FIXME: This should do something useful even if the types don't match,
674 // though if both indexes are constant the RegionRawOffset path will
675 // give the correct answer.
676 if (LeftER->getSuperRegion() == RightER->getSuperRegion() &&
677 LeftER->getElementType() == RightER->getElementType()) {
678 // Get the left index and cast it to the correct type.
679 // If the index is unknown or undefined, bail out here.
680 SVal LeftIndexVal = LeftER->getIndex();
681 NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
682 if (!LeftIndex)
683 return UnknownVal();
684 LeftIndexVal = EvalCastNL(*LeftIndex, resultTy);
685 LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
686 if (!LeftIndex)
687 return UnknownVal();
688
689 // Do the same for the right index.
690 SVal RightIndexVal = RightER->getIndex();
691 NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
692 if (!RightIndex)
693 return UnknownVal();
694 RightIndexVal = EvalCastNL(*RightIndex, resultTy);
695 RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
696 if (!RightIndex)
697 return UnknownVal();
698
699 // Actually perform the operation.
700 // EvalBinOpNN expects the two indexes to already be the right type.
701 return EvalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy);
702 }
703
704 // If the element indexes aren't comparable, see if the raw offsets are.
705 RegionRawOffset LeftOffset = LeftER->getAsRawOffset();
706 RegionRawOffset RightOffset = RightER->getAsRawOffset();
707
708 if (LeftOffset.getRegion() != NULL &&
709 LeftOffset.getRegion() == RightOffset.getRegion()) {
710 int64_t left = LeftOffset.getByteOffset();
711 int64_t right = RightOffset.getByteOffset();
712
713 switch (op) {
714 default:
715 return UnknownVal();
716 case BinaryOperator::LT:
717 return ValMgr.makeTruthVal(left < right, resultTy);
718 case BinaryOperator::GT:
719 return ValMgr.makeTruthVal(left > right, resultTy);
720 case BinaryOperator::LE:
721 return ValMgr.makeTruthVal(left <= right, resultTy);
722 case BinaryOperator::GE:
723 return ValMgr.makeTruthVal(left >= right, resultTy);
724 case BinaryOperator::EQ:
725 return ValMgr.makeTruthVal(left == right, resultTy);
726 case BinaryOperator::NE:
727 return ValMgr.makeTruthVal(left != right, resultTy);
728 }
729 }
730
731 // If we get here, we have no way of comparing the ElementRegions.
732 return UnknownVal();
733 }
734
735 // See if both regions are fields of the same structure.
736 // FIXME: This doesn't handle nesting, inheritance, or Objective-C ivars.
737 if (const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR)) {
738 // Only comparisons are meaningful here!
739 if (!BinaryOperator::isComparisonOp(op))
740 return UnknownVal();
741
742 // First see if the right region is also a FieldRegion.
743 const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR);
744 if (!RightFR)
745 return UnknownVal();
746
747 // Next, see if the two FRs have the same super-region.
748 // FIXME: This doesn't handle casts yet, and simply stripping the casts
749 // doesn't help.
750 if (LeftFR->getSuperRegion() != RightFR->getSuperRegion())
751 return UnknownVal();
752
753 const FieldDecl *LeftFD = LeftFR->getDecl();
754 const FieldDecl *RightFD = RightFR->getDecl();
755 const RecordDecl *RD = LeftFD->getParent();
756
757 // Make sure the two FRs are from the same kind of record. Just in case!
758 // FIXME: This is probably where inheritance would be a problem.
759 if (RD != RightFD->getParent())
760 return UnknownVal();
761
762 // We know for sure that the two fields are not the same, since that
763 // would have given us the same SVal.
764 if (op == BinaryOperator::EQ)
765 return ValMgr.makeTruthVal(false, resultTy);
766 if (op == BinaryOperator::NE)
767 return ValMgr.makeTruthVal(true, resultTy);
768
769 // Iterate through the fields and see which one comes first.
770 // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field
771 // members and the units in which bit-fields reside have addresses that
772 // increase in the order in which they are declared."
773 bool leftFirst = (op == BinaryOperator::LT || op == BinaryOperator::LE);
774 for (RecordDecl::field_iterator I = RD->field_begin(),
775 E = RD->field_end(); I!=E; ++I) {
776 if (*I == LeftFD)
777 return ValMgr.makeTruthVal(leftFirst, resultTy);
778 if (*I == RightFD)
779 return ValMgr.makeTruthVal(!leftFirst, resultTy);
780 }
781
782 assert(false && "Fields not found in parent record's definition");
783 }
784
785 // If we get here, we have no way of comparing the regions.
786 return UnknownVal();
787 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000788 }
789}
790
791SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
792 BinaryOperator::Opcode op,
Mike Stump1eb44332009-09-09 15:08:12 +0000793 Loc lhs, NonLoc rhs, QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000794 // Special case: 'rhs' is an integer that has the same width as a pointer and
795 // we are using the integer location in a comparison. Normally this cannot be
796 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
797 // can generate comparisons that trigger this code.
798 // FIXME: Are all locations guaranteed to have pointer width?
Jordy Roseeac4a002010-06-28 08:26:15 +0000799 if (BinaryOperator::isComparisonOp(op)) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000800 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
801 const llvm::APSInt *x = &rhsInt->getValue();
802 ASTContext &ctx = ValMgr.getContext();
803 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
804 // Convert the signedness of the integer (if necessary).
805 if (x->isSigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000806 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
Ted Kremeneke8391722009-06-26 00:25:05 +0000807
Jordy Roseeac4a002010-06-28 08:26:15 +0000808 return EvalBinOpLL(state, op, lhs, loc::ConcreteInt(*x), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000809 }
810 }
811 }
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Ted Kremeneke8391722009-06-26 00:25:05 +0000813 // Delegate pointer arithmetic to the StoreManager.
Zhongxing Xu461147f2010-02-05 05:24:20 +0000814 return state->getStateManager().getStoreManager().EvalBinOp(op, lhs,
Ted Kremeneke8391722009-06-26 00:25:05 +0000815 rhs, resultTy);
816}