blob: 5b24992118cd423ada6465566452b94d63fb156e [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].
Jordy Rosea2741482010-06-30 01:35:20 +0000505 // However, if a pointer is casted to an integer, EvalBinOpNN may end up
506 // calling this function with another operation (PR7527). We don't attempt to
507 // model this for now, but it could be useful, particularly when the
508 // "location" is actually an integer value that's been passed through a void*.
509 if (!(BinaryOperator::isComparisonOp(op) || op == BinaryOperator::Sub))
510 return UnknownVal();
Jordy Roseeac4a002010-06-28 08:26:15 +0000511
512 // Special cases for when both sides are identical.
513 if (lhs == rhs) {
514 switch (op) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000515 default:
Jordy Roseeac4a002010-06-28 08:26:15 +0000516 assert(false && "Unimplemented operation for two identical values");
Ted Kremeneke8391722009-06-26 00:25:05 +0000517 return UnknownVal();
Jordy Roseeac4a002010-06-28 08:26:15 +0000518 case BinaryOperator::Sub:
519 return ValMgr.makeZeroVal(resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000520 case BinaryOperator::EQ:
Jordy Roseeac4a002010-06-28 08:26:15 +0000521 case BinaryOperator::LE:
522 case BinaryOperator::GE:
523 return ValMgr.makeTruthVal(true, resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000524 case BinaryOperator::NE:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000525 case BinaryOperator::LT:
526 case BinaryOperator::GT:
Jordy Roseeac4a002010-06-28 08:26:15 +0000527 return ValMgr.makeTruthVal(false, resultTy);
528 }
529 }
530
531 switch (lhs.getSubKind()) {
532 default:
533 assert(false && "Ordering not implemented for this Loc.");
534 return UnknownVal();
535
536 case loc::GotoLabelKind:
537 // The only thing we know about labels is that they're non-null.
538 if (rhs.isZeroConstant()) {
539 switch (op) {
540 default:
541 break;
542 case BinaryOperator::Sub:
543 return EvalCastL(lhs, resultTy);
544 case BinaryOperator::EQ:
545 case BinaryOperator::LE:
546 case BinaryOperator::LT:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000547 return ValMgr.makeTruthVal(false, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000548 case BinaryOperator::NE:
549 case BinaryOperator::GT:
550 case BinaryOperator::GE:
551 return ValMgr.makeTruthVal(true, resultTy);
552 }
553 }
554 // There may be two labels for the same location, and a function region may
555 // have the same address as a label at the start of the function (depending
556 // on the ABI).
557 // FIXME: we can probably do a comparison against other MemRegions, though.
558 // FIXME: is there a way to tell if two labels refer to the same location?
559 return UnknownVal();
560
561 case loc::ConcreteIntKind: {
562 // If one of the operands is a symbol and the other is a constant,
563 // build an expression for use by the constraint manager.
564 if (SymbolRef rSym = rhs.getAsLocSymbol()) {
565 // We can only build expressions with symbols on the left,
566 // so we need a reversible operator.
567 if (!BinaryOperator::isComparisonOp(op))
568 return UnknownVal();
569
570 const llvm::APSInt &lVal = cast<loc::ConcreteInt>(lhs).getValue();
571 return ValMgr.makeNonLoc(rSym, ReverseComparison(op), lVal, resultTy);
572 }
573
574 // If both operands are constants, just perform the operation.
575 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
576 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
577 SVal ResultVal = cast<loc::ConcreteInt>(lhs).EvalBinOp(BVF, op, *rInt);
578 if (Loc *Result = dyn_cast<Loc>(&ResultVal))
579 return EvalCastL(*Result, resultTy);
580 else
581 return UnknownVal();
582 }
583
584 // Special case comparisons against NULL.
585 // This must come after the test if the RHS is a symbol, which is used to
586 // build constraints. The address of any non-symbolic region is guaranteed
587 // to be non-NULL, as is any label.
588 assert(isa<loc::MemRegionVal>(rhs) || isa<loc::GotoLabel>(rhs));
589 if (lhs.isZeroConstant()) {
590 switch (op) {
591 default:
592 break;
593 case BinaryOperator::EQ:
594 case BinaryOperator::GT:
595 case BinaryOperator::GE:
596 return ValMgr.makeTruthVal(false, resultTy);
597 case BinaryOperator::NE:
598 case BinaryOperator::LT:
599 case BinaryOperator::LE:
600 return ValMgr.makeTruthVal(true, resultTy);
601 }
602 }
603
604 // Comparing an arbitrary integer to a region or label address is
605 // completely unknowable.
606 return UnknownVal();
607 }
608 case loc::MemRegionKind: {
609 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
610 // If one of the operands is a symbol and the other is a constant,
611 // build an expression for use by the constraint manager.
612 if (SymbolRef lSym = lhs.getAsLocSymbol())
613 return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy);
614
615 // Special case comparisons to NULL.
616 // This must come after the test if the LHS is a symbol, which is used to
617 // build constraints. The address of any non-symbolic region is guaranteed
618 // to be non-NULL.
619 if (rInt->isZeroConstant()) {
620 switch (op) {
621 default:
622 break;
623 case BinaryOperator::Sub:
624 return EvalCastL(lhs, resultTy);
625 case BinaryOperator::EQ:
626 case BinaryOperator::LT:
627 case BinaryOperator::LE:
628 return ValMgr.makeTruthVal(false, resultTy);
629 case BinaryOperator::NE:
630 case BinaryOperator::GT:
631 case BinaryOperator::GE:
632 return ValMgr.makeTruthVal(true, resultTy);
633 }
634 }
635
636 // Comparing a region to an arbitrary integer is completely unknowable.
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000637 return UnknownVal();
Jordy Roseeac4a002010-06-28 08:26:15 +0000638 }
639
640 // Get both values as regions, if possible.
641 const MemRegion *LeftMR = lhs.getAsRegion();
642 assert(LeftMR && "MemRegionKind SVal doesn't have a region!");
643
644 const MemRegion *RightMR = rhs.getAsRegion();
645 if (!RightMR)
646 // The RHS is probably a label, which in theory could address a region.
647 // FIXME: we can probably make a more useful statement about non-code
648 // regions, though.
649 return UnknownVal();
650
651 // If both values wrap regions, see if they're from different base regions.
652 const MemRegion *LeftBase = LeftMR->getBaseRegion();
653 const MemRegion *RightBase = RightMR->getBaseRegion();
654 if (LeftBase != RightBase &&
655 !isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) {
656 switch (op) {
657 default:
658 return UnknownVal();
659 case BinaryOperator::EQ:
660 return ValMgr.makeTruthVal(false, resultTy);
661 case BinaryOperator::NE:
662 return ValMgr.makeTruthVal(true, resultTy);
663 }
664 }
665
666 // The two regions are from the same base region. See if they're both a
667 // type of region we know how to compare.
668
669 // FIXME: If/when there is a getAsRawOffset() for FieldRegions, this
670 // ElementRegion path and the FieldRegion path below should be unified.
671 if (const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR)) {
672 // First see if the right region is also an ElementRegion.
673 const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR);
674 if (!RightER)
675 return UnknownVal();
676
677 // Next, see if the two ERs have the same super-region and matching types.
678 // FIXME: This should do something useful even if the types don't match,
679 // though if both indexes are constant the RegionRawOffset path will
680 // give the correct answer.
681 if (LeftER->getSuperRegion() == RightER->getSuperRegion() &&
682 LeftER->getElementType() == RightER->getElementType()) {
683 // Get the left index and cast it to the correct type.
684 // If the index is unknown or undefined, bail out here.
685 SVal LeftIndexVal = LeftER->getIndex();
686 NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
687 if (!LeftIndex)
688 return UnknownVal();
689 LeftIndexVal = EvalCastNL(*LeftIndex, resultTy);
690 LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
691 if (!LeftIndex)
692 return UnknownVal();
693
694 // Do the same for the right index.
695 SVal RightIndexVal = RightER->getIndex();
696 NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
697 if (!RightIndex)
698 return UnknownVal();
699 RightIndexVal = EvalCastNL(*RightIndex, resultTy);
700 RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
701 if (!RightIndex)
702 return UnknownVal();
703
704 // Actually perform the operation.
705 // EvalBinOpNN expects the two indexes to already be the right type.
706 return EvalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy);
707 }
708
709 // If the element indexes aren't comparable, see if the raw offsets are.
710 RegionRawOffset LeftOffset = LeftER->getAsRawOffset();
711 RegionRawOffset RightOffset = RightER->getAsRawOffset();
712
713 if (LeftOffset.getRegion() != NULL &&
714 LeftOffset.getRegion() == RightOffset.getRegion()) {
715 int64_t left = LeftOffset.getByteOffset();
716 int64_t right = RightOffset.getByteOffset();
717
718 switch (op) {
719 default:
720 return UnknownVal();
721 case BinaryOperator::LT:
722 return ValMgr.makeTruthVal(left < right, resultTy);
723 case BinaryOperator::GT:
724 return ValMgr.makeTruthVal(left > right, resultTy);
725 case BinaryOperator::LE:
726 return ValMgr.makeTruthVal(left <= right, resultTy);
727 case BinaryOperator::GE:
728 return ValMgr.makeTruthVal(left >= right, resultTy);
729 case BinaryOperator::EQ:
730 return ValMgr.makeTruthVal(left == right, resultTy);
731 case BinaryOperator::NE:
732 return ValMgr.makeTruthVal(left != right, resultTy);
733 }
734 }
735
736 // If we get here, we have no way of comparing the ElementRegions.
737 return UnknownVal();
738 }
739
740 // See if both regions are fields of the same structure.
741 // FIXME: This doesn't handle nesting, inheritance, or Objective-C ivars.
742 if (const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR)) {
743 // Only comparisons are meaningful here!
744 if (!BinaryOperator::isComparisonOp(op))
745 return UnknownVal();
746
747 // First see if the right region is also a FieldRegion.
748 const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR);
749 if (!RightFR)
750 return UnknownVal();
751
752 // Next, see if the two FRs have the same super-region.
753 // FIXME: This doesn't handle casts yet, and simply stripping the casts
754 // doesn't help.
755 if (LeftFR->getSuperRegion() != RightFR->getSuperRegion())
756 return UnknownVal();
757
758 const FieldDecl *LeftFD = LeftFR->getDecl();
759 const FieldDecl *RightFD = RightFR->getDecl();
760 const RecordDecl *RD = LeftFD->getParent();
761
762 // Make sure the two FRs are from the same kind of record. Just in case!
763 // FIXME: This is probably where inheritance would be a problem.
764 if (RD != RightFD->getParent())
765 return UnknownVal();
766
767 // We know for sure that the two fields are not the same, since that
768 // would have given us the same SVal.
769 if (op == BinaryOperator::EQ)
770 return ValMgr.makeTruthVal(false, resultTy);
771 if (op == BinaryOperator::NE)
772 return ValMgr.makeTruthVal(true, resultTy);
773
774 // Iterate through the fields and see which one comes first.
775 // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field
776 // members and the units in which bit-fields reside have addresses that
777 // increase in the order in which they are declared."
778 bool leftFirst = (op == BinaryOperator::LT || op == BinaryOperator::LE);
779 for (RecordDecl::field_iterator I = RD->field_begin(),
780 E = RD->field_end(); I!=E; ++I) {
781 if (*I == LeftFD)
782 return ValMgr.makeTruthVal(leftFirst, resultTy);
783 if (*I == RightFD)
784 return ValMgr.makeTruthVal(!leftFirst, resultTy);
785 }
786
787 assert(false && "Fields not found in parent record's definition");
788 }
789
790 // If we get here, we have no way of comparing the regions.
791 return UnknownVal();
792 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000793 }
794}
795
796SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
797 BinaryOperator::Opcode op,
Mike Stump1eb44332009-09-09 15:08:12 +0000798 Loc lhs, NonLoc rhs, QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000799 // Special case: 'rhs' is an integer that has the same width as a pointer and
800 // we are using the integer location in a comparison. Normally this cannot be
801 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
802 // can generate comparisons that trigger this code.
803 // FIXME: Are all locations guaranteed to have pointer width?
Jordy Roseeac4a002010-06-28 08:26:15 +0000804 if (BinaryOperator::isComparisonOp(op)) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000805 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
806 const llvm::APSInt *x = &rhsInt->getValue();
807 ASTContext &ctx = ValMgr.getContext();
808 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
809 // Convert the signedness of the integer (if necessary).
810 if (x->isSigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000811 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
Ted Kremeneke8391722009-06-26 00:25:05 +0000812
Jordy Roseeac4a002010-06-28 08:26:15 +0000813 return EvalBinOpLL(state, op, lhs, loc::ConcreteInt(*x), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000814 }
815 }
816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Ted Kremeneke8391722009-06-26 00:25:05 +0000818 // Delegate pointer arithmetic to the StoreManager.
Zhongxing Xu461147f2010-02-05 05:24:20 +0000819 return state->getStateManager().getStoreManager().EvalBinOp(op, lhs,
Ted Kremeneke8391722009-06-26 00:25:05 +0000820 rhs, resultTy);
821}