blob: b8cad7a5cc004cee2bb6d9c976cade52116d1b78 [file] [log] [blame]
Ted Kremenek846eabd2010-12-01 21:28:31 +00001// SimpleSValBuilder.cpp - A basic SValBuilder -----------------------*- C++ -*-
Ted Kremeneke8391722009-06-26 00:25:05 +00002//
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//
Ted Kremenek846eabd2010-12-01 21:28:31 +000010// This file defines SimpleSValBuilder, a basic implementation of SValBuilder.
Ted Kremeneke8391722009-06-26 00:25:05 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek846eabd2010-12-01 21:28:31 +000014#include "clang/Checker/PathSensitive/SValBuilder.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000015#include "clang/Checker/PathSensitive/GRState.h"
Ted Kremeneke8391722009-06-26 00:25:05 +000016
17using namespace clang;
18
19namespace {
Ted Kremenek846eabd2010-12-01 21:28:31 +000020class SimpleSValBuilder : public SValBuilder {
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:
Ted Kremenek846eabd2010-12-01 21:28:31 +000026 SimpleSValBuilder(ValueManager &valMgr) : SValBuilder(valMgr) {}
27 virtual ~SimpleSValBuilder() {}
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 Rose32f26562010-07-04 00:00:41 +000037
38 /// getKnownValue - Evaluates a given SVal. If the SVal has only one possible
39 /// (integer) value, that value is returned. Otherwise, returns NULL.
40 virtual const llvm::APSInt *getKnownValue(const GRState *state, SVal V);
Jordy Rose43fdb7f2010-06-20 04:56:29 +000041
42 SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op,
43 const llvm::APSInt &RHS, QualType resultTy);
Mike Stump1eb44332009-09-09 15:08:12 +000044};
Ted Kremeneke8391722009-06-26 00:25:05 +000045} // end anonymous namespace
46
Ted Kremenek846eabd2010-12-01 21:28:31 +000047SValBuilder *clang::createSimpleSValBuilder(ValueManager &valMgr) {
48 return new SimpleSValBuilder(valMgr);
Ted Kremeneke8391722009-06-26 00:25:05 +000049}
50
51//===----------------------------------------------------------------------===//
52// Transfer function for Casts.
53//===----------------------------------------------------------------------===//
54
Ted Kremenek846eabd2010-12-01 21:28:31 +000055SVal SimpleSValBuilder::EvalCastNL(NonLoc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000056
Ted Kremenekdd661142009-07-20 21:39:27 +000057 bool isLocType = Loc::IsLocType(castTy);
Mike Stump1eb44332009-09-09 15:08:12 +000058
Ted Kremenek9031dd72009-07-21 00:12:07 +000059 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) {
60 if (isLocType)
Ted Kremenekdd661142009-07-20 21:39:27 +000061 return LI->getLoc();
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenekc50e6df2010-01-11 02:33:26 +000063 // FIXME: Correctly support promotions/truncations.
Mike Stump1eb44332009-09-09 15:08:12 +000064 ASTContext &Ctx = ValMgr.getContext();
Ted Kremenekc50e6df2010-01-11 02:33:26 +000065 unsigned castSize = Ctx.getTypeSize(castTy);
66 if (castSize == LI->getNumBits())
Ted Kremenek9031dd72009-07-21 00:12:07 +000067 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000068
Ted Kremenekc50e6df2010-01-11 02:33:26 +000069 return ValMgr.makeLocAsInteger(LI->getLoc(), castSize);
Ted Kremenek9031dd72009-07-21 00:12:07 +000070 }
71
72 if (const SymExpr *se = val.getAsSymbolicExpression()) {
73 ASTContext &Ctx = ValMgr.getContext();
74 QualType T = Ctx.getCanonicalType(se->getType(Ctx));
75 if (T == Ctx.getCanonicalType(castTy))
76 return val;
Ted Kremenek80417472009-09-25 00:18:15 +000077
78 // FIXME: Remove this hack when we support symbolic truncation/extension.
79 // HACK: If both castTy and T are integers, ignore the cast. This is
80 // not a permanent solution. Eventually we want to precisely handle
81 // extension/truncation of symbolic integers. This prevents us from losing
82 // precision when we assign 'x = y' and 'y' is symbolic and x and y are
83 // different integer types.
84 if (T->isIntegerType() && castTy->isIntegerType())
85 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000086
Ted Kremenek9031dd72009-07-21 00:12:07 +000087 return UnknownVal();
88 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Ted Kremeneke8391722009-06-26 00:25:05 +000090 if (!isa<nonloc::ConcreteInt>(val))
91 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremeneke8391722009-06-26 00:25:05 +000093 // Only handle casts from integers to integers.
94 if (!isLocType && !castTy->isIntegerType())
95 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000096
Ted Kremeneke8391722009-06-26 00:25:05 +000097 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
98 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
99 i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy));
100
101 if (isLocType)
102 return ValMgr.makeIntLocVal(i);
103 else
104 return ValMgr.makeIntVal(i);
105}
106
Ted Kremenek846eabd2010-12-01 21:28:31 +0000107SVal SimpleSValBuilder::EvalCastL(Loc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000108
Ted Kremeneke8391722009-06-26 00:25:05 +0000109 // Casts from pointers -> pointers, just return the lval.
110 //
111 // Casts from pointers -> references, just return the lval. These
112 // can be introduced by the frontend for corner cases, e.g
113 // casting from va_list* to __builtin_va_list&.
114 //
Ted Kremeneke8391722009-06-26 00:25:05 +0000115 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
116 return val;
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Ted Kremeneke8391722009-06-26 00:25:05 +0000118 // FIXME: Handle transparent unions where a value can be "transparently"
119 // lifted into a union type.
120 if (castTy->isUnionType())
121 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Ted Kremenekd617b852010-04-16 17:54:33 +0000123 if (castTy->isIntegerType()) {
124 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000125
Ted Kremenekd617b852010-04-16 17:54:33 +0000126 if (!isa<loc::ConcreteInt>(val))
127 return ValMgr.makeLocAsInteger(val, BitWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Ted Kremenekd617b852010-04-16 17:54:33 +0000129 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
130 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
131 i.extOrTrunc(BitWidth);
132 return ValMgr.makeIntVal(i);
133 }
134
135 // All other cases: return 'UnknownVal'. This includes casting pointers
136 // to floats, which is probably badness it itself, but this is a good
137 // intermediate solution until we do something better.
138 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000139}
140
141//===----------------------------------------------------------------------===//
142// Transfer function for unary operators.
143//===----------------------------------------------------------------------===//
144
Ted Kremenek846eabd2010-12-01 21:28:31 +0000145SVal SimpleSValBuilder::EvalMinus(NonLoc val) {
Mike Stump1eb44332009-09-09 15:08:12 +0000146 switch (val.getSubKind()) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000147 case nonloc::ConcreteIntKind:
148 return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr);
149 default:
150 return UnknownVal();
151 }
152}
153
Ted Kremenek846eabd2010-12-01 21:28:31 +0000154SVal SimpleSValBuilder::EvalComplement(NonLoc X) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000155 switch (X.getSubKind()) {
156 case nonloc::ConcreteIntKind:
157 return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr);
158 default:
159 return UnknownVal();
160 }
161}
162
163//===----------------------------------------------------------------------===//
164// Transfer function for binary operators.
165//===----------------------------------------------------------------------===//
166
167static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
168 switch (op) {
169 default:
170 assert(false && "Invalid opcode.");
John McCall2de56d12010-08-25 11:45:40 +0000171 case BO_LT: return BO_GE;
172 case BO_GT: return BO_LE;
173 case BO_LE: return BO_GT;
174 case BO_GE: return BO_LT;
175 case BO_EQ: return BO_NE;
176 case BO_NE: return BO_EQ;
Ted Kremeneke8391722009-06-26 00:25:05 +0000177 }
178}
179
Jordy Roseeac4a002010-06-28 08:26:15 +0000180static BinaryOperator::Opcode ReverseComparison(BinaryOperator::Opcode op) {
181 switch (op) {
182 default:
183 assert(false && "Invalid opcode.");
John McCall2de56d12010-08-25 11:45:40 +0000184 case BO_LT: return BO_GT;
185 case BO_GT: return BO_LT;
186 case BO_LE: return BO_GE;
187 case BO_GE: return BO_LE;
188 case BO_EQ:
189 case BO_NE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000190 return op;
Ted Kremeneke8391722009-06-26 00:25:05 +0000191 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000192}
193
Ted Kremenek846eabd2010-12-01 21:28:31 +0000194SVal SimpleSValBuilder::MakeSymIntVal(const SymExpr *LHS,
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000195 BinaryOperator::Opcode op,
196 const llvm::APSInt &RHS,
197 QualType resultTy) {
198 bool isIdempotent = false;
199
200 // Check for a few special cases with known reductions first.
201 switch (op) {
202 default:
203 // We can't reduce this case; just treat it normally.
204 break;
John McCall2de56d12010-08-25 11:45:40 +0000205 case BO_Mul:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000206 // a*0 and a*1
207 if (RHS == 0)
208 return ValMgr.makeIntVal(0, resultTy);
209 else if (RHS == 1)
210 isIdempotent = true;
211 break;
John McCall2de56d12010-08-25 11:45:40 +0000212 case BO_Div:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000213 // a/0 and a/1
214 if (RHS == 0)
215 // This is also handled elsewhere.
216 return UndefinedVal();
217 else if (RHS == 1)
218 isIdempotent = true;
219 break;
John McCall2de56d12010-08-25 11:45:40 +0000220 case BO_Rem:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000221 // a%0 and a%1
222 if (RHS == 0)
223 // This is also handled elsewhere.
224 return UndefinedVal();
225 else if (RHS == 1)
226 return ValMgr.makeIntVal(0, resultTy);
227 break;
John McCall2de56d12010-08-25 11:45:40 +0000228 case BO_Add:
229 case BO_Sub:
230 case BO_Shl:
231 case BO_Shr:
232 case BO_Xor:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000233 // a+0, a-0, a<<0, a>>0, a^0
234 if (RHS == 0)
235 isIdempotent = true;
236 break;
John McCall2de56d12010-08-25 11:45:40 +0000237 case BO_And:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000238 // a&0 and a&(~0)
239 if (RHS == 0)
240 return ValMgr.makeIntVal(0, resultTy);
241 else if (RHS.isAllOnesValue())
242 isIdempotent = true;
243 break;
John McCall2de56d12010-08-25 11:45:40 +0000244 case BO_Or:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000245 // a|0 and a|(~0)
246 if (RHS == 0)
247 isIdempotent = true;
248 else if (RHS.isAllOnesValue()) {
249 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
250 const llvm::APSInt &Result = BVF.Convert(resultTy, RHS);
251 return nonloc::ConcreteInt(Result);
252 }
253 break;
254 }
255
256 // Idempotent ops (like a*1) can still change the type of an expression.
257 // Wrap the LHS up in a NonLoc again and let EvalCastNL do the dirty work.
Benjamin Kramer24ada872010-06-20 10:20:36 +0000258 if (isIdempotent) {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000259 if (SymbolRef LHSSym = dyn_cast<SymbolData>(LHS))
260 return EvalCastNL(nonloc::SymbolVal(LHSSym), resultTy);
Benjamin Kramer24ada872010-06-20 10:20:36 +0000261 return EvalCastNL(nonloc::SymExprVal(LHS), resultTy);
262 }
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000263
264 // If we reach this point, the expression cannot be simplified.
265 // Make a SymExprVal for the entire thing.
266 return ValMgr.makeNonLoc(LHS, op, RHS, resultTy);
267}
268
Ted Kremenek846eabd2010-12-01 21:28:31 +0000269SVal SimpleSValBuilder::EvalBinOpNN(const GRState *state,
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000270 BinaryOperator::Opcode op,
Ted Kremeneke8391722009-06-26 00:25:05 +0000271 NonLoc lhs, NonLoc rhs,
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000272 QualType resultTy) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000273 // Handle trivial case where left-side and right-side are the same.
274 if (lhs == rhs)
275 switch (op) {
276 default:
277 break;
John McCall2de56d12010-08-25 11:45:40 +0000278 case BO_EQ:
279 case BO_LE:
280 case BO_GE:
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000281 return ValMgr.makeTruthVal(true, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000282 case BO_LT:
283 case BO_GT:
284 case BO_NE:
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000285 return ValMgr.makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000286 case BO_Xor:
287 case BO_Sub:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000288 return ValMgr.makeIntVal(0, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000289 case BO_Or:
290 case BO_And:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000291 return EvalCastNL(lhs, resultTy);
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000292 }
Mike Stump1eb44332009-09-09 15:08:12 +0000293
Ted Kremeneke8391722009-06-26 00:25:05 +0000294 while (1) {
295 switch (lhs.getSubKind()) {
296 default:
Mike Stump1eb44332009-09-09 15:08:12 +0000297 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000298 case nonloc::LocAsIntegerKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000299 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
Ted Kremeneke8391722009-06-26 00:25:05 +0000300 switch (rhs.getSubKind()) {
301 case nonloc::LocAsIntegerKind:
Jordy Roseeac4a002010-06-28 08:26:15 +0000302 return EvalBinOpLL(state, op, lhsL,
303 cast<nonloc::LocAsInteger>(rhs).getLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +0000304 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000305 case nonloc::ConcreteIntKind: {
306 // Transform the integer into a location and compare.
307 ASTContext& Ctx = ValMgr.getContext();
308 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
309 i.setIsUnsigned(true);
310 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
Jordy Roseeac4a002010-06-28 08:26:15 +0000311 return EvalBinOpLL(state, op, lhsL, ValMgr.makeLoc(i), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000312 }
Mike Stump1eb44332009-09-09 15:08:12 +0000313 default:
Ted Kremeneke8391722009-06-26 00:25:05 +0000314 switch (op) {
John McCall2de56d12010-08-25 11:45:40 +0000315 case BO_EQ:
Ted Kremeneke8391722009-06-26 00:25:05 +0000316 return ValMgr.makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000317 case BO_NE:
Ted Kremeneke8391722009-06-26 00:25:05 +0000318 return ValMgr.makeTruthVal(true, resultTy);
319 default:
320 // This case also handles pointer arithmetic.
321 return UnknownVal();
322 }
323 }
Mike Stump1eb44332009-09-09 15:08:12 +0000324 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000325 case nonloc::SymExprValKind: {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000326 nonloc::SymExprVal *selhs = cast<nonloc::SymExprVal>(&lhs);
327
328 // Only handle LHS of the form "$sym op constant", at least for now.
329 const SymIntExpr *symIntExpr =
330 dyn_cast<SymIntExpr>(selhs->getSymbolicExpression());
331
332 if (!symIntExpr)
Ted Kremeneke8391722009-06-26 00:25:05 +0000333 return UnknownVal();
334
Jordy Roseba0f61c2010-06-18 22:49:11 +0000335 // Is this a logical not? (!x is represented as x == 0.)
John McCall2de56d12010-08-25 11:45:40 +0000336 if (op == BO_EQ && rhs.isZeroConstant()) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000337 // We know how to negate certain expressions. Simplify them here.
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Ted Kremeneke8391722009-06-26 00:25:05 +0000339 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
340 switch (opc) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000341 default:
342 // We don't know how to negate this operation.
343 // Just handle it as if it were a normal comparison to 0.
344 break;
John McCall2de56d12010-08-25 11:45:40 +0000345 case BO_LAnd:
346 case BO_LOr:
Jordy Roseba0f61c2010-06-18 22:49:11 +0000347 assert(false && "Logical operators handled by branching logic.");
348 return UnknownVal();
John McCall2de56d12010-08-25 11:45:40 +0000349 case BO_Assign:
350 case BO_MulAssign:
351 case BO_DivAssign:
352 case BO_RemAssign:
353 case BO_AddAssign:
354 case BO_SubAssign:
355 case BO_ShlAssign:
356 case BO_ShrAssign:
357 case BO_AndAssign:
358 case BO_XorAssign:
359 case BO_OrAssign:
360 case BO_Comma:
Jordy Roseba0f61c2010-06-18 22:49:11 +0000361 assert(false && "'=' and ',' operators handled by GRExprEngine.");
362 return UnknownVal();
John McCall2de56d12010-08-25 11:45:40 +0000363 case BO_PtrMemD:
364 case BO_PtrMemI:
Jordy Roseba0f61c2010-06-18 22:49:11 +0000365 assert(false && "Pointer arithmetic not handled here.");
366 return UnknownVal();
John McCall2de56d12010-08-25 11:45:40 +0000367 case BO_LT:
368 case BO_GT:
369 case BO_LE:
370 case BO_GE:
371 case BO_EQ:
372 case BO_NE:
Jordy Roseba0f61c2010-06-18 22:49:11 +0000373 // Negate the comparison and make a value.
374 opc = NegateComparison(opc);
375 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
376 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
377 symIntExpr->getRHS(), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000378 }
379 }
Jordy Roseba0f61c2010-06-18 22:49:11 +0000380
381 // For now, only handle expressions whose RHS is a constant.
382 const nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs);
383 if (!rhsInt)
384 return UnknownVal();
385
386 // If both the LHS and the current expression are additive,
387 // fold their constants.
388 if (BinaryOperator::isAdditiveOp(op)) {
389 BinaryOperator::Opcode lop = symIntExpr->getOpcode();
390 if (BinaryOperator::isAdditiveOp(lop)) {
391 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
Jordy Roseb4954a42010-06-21 20:15:15 +0000392
393 // resultTy may not be the best type to convert to, but it's
394 // probably the best choice in expressions with mixed type
395 // (such as x+1U+2LL). The rules for implicit conversions should
396 // choose a reasonable type to preserve the expression, and will
397 // at least match how the value is going to be used.
398 const llvm::APSInt &first =
399 BVF.Convert(resultTy, symIntExpr->getRHS());
400 const llvm::APSInt &second =
401 BVF.Convert(resultTy, rhsInt->getValue());
402
Jordy Roseba0f61c2010-06-18 22:49:11 +0000403 const llvm::APSInt *newRHS;
404 if (lop == op)
John McCall2de56d12010-08-25 11:45:40 +0000405 newRHS = BVF.EvaluateAPSInt(BO_Add, first, second);
Jordy Roseba0f61c2010-06-18 22:49:11 +0000406 else
John McCall2de56d12010-08-25 11:45:40 +0000407 newRHS = BVF.EvaluateAPSInt(BO_Sub, first, second);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000408 return MakeSymIntVal(symIntExpr->getLHS(), lop, *newRHS, resultTy);
Jordy Roseba0f61c2010-06-18 22:49:11 +0000409 }
410 }
411
412 // Otherwise, make a SymExprVal out of the expression.
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000413 return MakeSymIntVal(symIntExpr, op, rhsInt->getValue(), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000414 }
Mike Stump1eb44332009-09-09 15:08:12 +0000415 case nonloc::ConcreteIntKind: {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000416 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
417
Ted Kremeneke8391722009-06-26 00:25:05 +0000418 if (isa<nonloc::ConcreteInt>(rhs)) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000419 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000420 } else {
421 const llvm::APSInt& lhsValue = lhsInt.getValue();
422
Ted Kremeneke8391722009-06-26 00:25:05 +0000423 // Swap the left and right sides and flip the operator if doing so
424 // allows us to better reason about the expression (this is a form
425 // of expression canonicalization).
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000426 // While we're at it, catch some special cases for non-commutative ops.
Ted Kremeneke8391722009-06-26 00:25:05 +0000427 NonLoc tmp = rhs;
428 rhs = lhs;
429 lhs = tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Ted Kremeneke8391722009-06-26 00:25:05 +0000431 switch (op) {
John McCall2de56d12010-08-25 11:45:40 +0000432 case BO_LT:
433 case BO_GT:
434 case BO_LE:
435 case BO_GE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000436 op = ReverseComparison(op);
437 continue;
John McCall2de56d12010-08-25 11:45:40 +0000438 case BO_EQ:
439 case BO_NE:
440 case BO_Add:
441 case BO_Mul:
442 case BO_And:
443 case BO_Xor:
444 case BO_Or:
Ted Kremeneke8391722009-06-26 00:25:05 +0000445 continue;
John McCall2de56d12010-08-25 11:45:40 +0000446 case BO_Shr:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000447 if (lhsValue.isAllOnesValue() && lhsValue.isSigned())
448 // At this point lhs and rhs have been swapped.
449 return rhs;
450 // FALL-THROUGH
John McCall2de56d12010-08-25 11:45:40 +0000451 case BO_Shl:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000452 if (lhsValue == 0)
453 // At this point lhs and rhs have been swapped.
454 return rhs;
455 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000456 default:
457 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000458 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000459 }
460 }
461 case nonloc::SymbolValKind: {
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000462 nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs);
463 SymbolRef Sym = slhs->getSymbol();
Jordy Rosea277e772010-08-09 20:31:57 +0000464
465 ASTContext& Ctx = ValMgr.getContext();
466
Ted Kremenek9b020342009-10-17 07:39:35 +0000467 // Does the symbol simplify to a constant? If so, "fold" the constant
468 // by setting 'lhs' to a ConcreteInt and try again.
Jordy Rosea277e772010-08-09 20:31:57 +0000469 if (Sym->getType(Ctx)->isIntegerType())
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000470 if (const llvm::APSInt *Constant = state->getSymVal(Sym)) {
Ted Kremenek9b020342009-10-17 07:39:35 +0000471 // The symbol evaluates to a constant. If necessary, promote the
472 // folded constant (LHS) to the result type.
473 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
474 const llvm::APSInt &lhs_I = BVF.Convert(resultTy, *Constant);
475 lhs = nonloc::ConcreteInt(lhs_I);
Ted Kremenekb5deae52009-10-16 20:46:24 +0000476
Ted Kremenek9b020342009-10-17 07:39:35 +0000477 // Also promote the RHS (if necessary).
478
Jordy Rosea277e772010-08-09 20:31:57 +0000479 // For shifts, it is not necessary to promote the RHS.
Ted Kremenek9b020342009-10-17 07:39:35 +0000480 if (BinaryOperator::isShiftOp(op))
481 continue;
482
483 // Other operators: do an implicit conversion. This shouldn't be
Ted Kremenekb5deae52009-10-16 20:46:24 +0000484 // necessary once we support truncation/extension of symbolic values.
Ted Kremenekb1d04222009-10-06 03:44:49 +0000485 if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){
Ted Kremenek9b020342009-10-17 07:39:35 +0000486 rhs = nonloc::ConcreteInt(BVF.Convert(resultTy, rhs_I->getValue()));
Ted Kremenekb1d04222009-10-06 03:44:49 +0000487 }
Ted Kremenek9b020342009-10-17 07:39:35 +0000488
489 continue;
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000490 }
Jordy Rosea277e772010-08-09 20:31:57 +0000491
492 // Is the RHS a symbol we can simplify?
493 if (const nonloc::SymbolVal *srhs = dyn_cast<nonloc::SymbolVal>(&rhs)) {
494 SymbolRef RSym = srhs->getSymbol();
495 if (RSym->getType(Ctx)->isIntegerType()) {
496 if (const llvm::APSInt *Constant = state->getSymVal(RSym)) {
497 // The symbol evaluates to a constant.
498 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
499 const llvm::APSInt &rhs_I = BVF.Convert(resultTy, *Constant);
500 rhs = nonloc::ConcreteInt(rhs_I);
501 }
502 }
503 }
504
Ted Kremeneke8391722009-06-26 00:25:05 +0000505 if (isa<nonloc::ConcreteInt>(rhs)) {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000506 return MakeSymIntVal(slhs->getSymbol(), op,
507 cast<nonloc::ConcreteInt>(rhs).getValue(),
508 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000509 }
510
511 return UnknownVal();
512 }
513 }
514 }
515}
516
Jordy Roseeac4a002010-06-28 08:26:15 +0000517// FIXME: all this logic will change if/when we have MemRegion::getLocation().
Ted Kremenek846eabd2010-12-01 21:28:31 +0000518SVal SimpleSValBuilder::EvalBinOpLL(const GRState *state,
Jordy Roseeac4a002010-06-28 08:26:15 +0000519 BinaryOperator::Opcode op,
520 Loc lhs, Loc rhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000521 QualType resultTy) {
Jordy Roseeac4a002010-06-28 08:26:15 +0000522 // Only comparisons and subtractions are valid operations on two pointers.
523 // See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15].
Jordy Rosea2741482010-06-30 01:35:20 +0000524 // However, if a pointer is casted to an integer, EvalBinOpNN may end up
525 // calling this function with another operation (PR7527). We don't attempt to
526 // model this for now, but it could be useful, particularly when the
527 // "location" is actually an integer value that's been passed through a void*.
John McCall2de56d12010-08-25 11:45:40 +0000528 if (!(BinaryOperator::isComparisonOp(op) || op == BO_Sub))
Jordy Rosea2741482010-06-30 01:35:20 +0000529 return UnknownVal();
Jordy Roseeac4a002010-06-28 08:26:15 +0000530
531 // Special cases for when both sides are identical.
532 if (lhs == rhs) {
533 switch (op) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000534 default:
Jordy Roseeac4a002010-06-28 08:26:15 +0000535 assert(false && "Unimplemented operation for two identical values");
Ted Kremeneke8391722009-06-26 00:25:05 +0000536 return UnknownVal();
John McCall2de56d12010-08-25 11:45:40 +0000537 case BO_Sub:
Jordy Roseeac4a002010-06-28 08:26:15 +0000538 return ValMgr.makeZeroVal(resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000539 case BO_EQ:
540 case BO_LE:
541 case BO_GE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000542 return ValMgr.makeTruthVal(true, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000543 case BO_NE:
544 case BO_LT:
545 case BO_GT:
Jordy Roseeac4a002010-06-28 08:26:15 +0000546 return ValMgr.makeTruthVal(false, resultTy);
547 }
548 }
549
550 switch (lhs.getSubKind()) {
551 default:
552 assert(false && "Ordering not implemented for this Loc.");
553 return UnknownVal();
554
555 case loc::GotoLabelKind:
556 // The only thing we know about labels is that they're non-null.
557 if (rhs.isZeroConstant()) {
558 switch (op) {
559 default:
560 break;
John McCall2de56d12010-08-25 11:45:40 +0000561 case BO_Sub:
Jordy Roseeac4a002010-06-28 08:26:15 +0000562 return EvalCastL(lhs, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000563 case BO_EQ:
564 case BO_LE:
565 case BO_LT:
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000566 return ValMgr.makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000567 case BO_NE:
568 case BO_GT:
569 case BO_GE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000570 return ValMgr.makeTruthVal(true, resultTy);
571 }
572 }
573 // There may be two labels for the same location, and a function region may
574 // have the same address as a label at the start of the function (depending
575 // on the ABI).
576 // FIXME: we can probably do a comparison against other MemRegions, though.
577 // FIXME: is there a way to tell if two labels refer to the same location?
578 return UnknownVal();
579
580 case loc::ConcreteIntKind: {
581 // If one of the operands is a symbol and the other is a constant,
582 // build an expression for use by the constraint manager.
583 if (SymbolRef rSym = rhs.getAsLocSymbol()) {
584 // We can only build expressions with symbols on the left,
585 // so we need a reversible operator.
586 if (!BinaryOperator::isComparisonOp(op))
587 return UnknownVal();
588
589 const llvm::APSInt &lVal = cast<loc::ConcreteInt>(lhs).getValue();
590 return ValMgr.makeNonLoc(rSym, ReverseComparison(op), lVal, resultTy);
591 }
592
593 // If both operands are constants, just perform the operation.
594 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
595 BasicValueFactory &BVF = ValMgr.getBasicValueFactory();
596 SVal ResultVal = cast<loc::ConcreteInt>(lhs).EvalBinOp(BVF, op, *rInt);
597 if (Loc *Result = dyn_cast<Loc>(&ResultVal))
598 return EvalCastL(*Result, resultTy);
599 else
600 return UnknownVal();
601 }
602
603 // Special case comparisons against NULL.
604 // This must come after the test if the RHS is a symbol, which is used to
605 // build constraints. The address of any non-symbolic region is guaranteed
606 // to be non-NULL, as is any label.
607 assert(isa<loc::MemRegionVal>(rhs) || isa<loc::GotoLabel>(rhs));
608 if (lhs.isZeroConstant()) {
609 switch (op) {
610 default:
611 break;
John McCall2de56d12010-08-25 11:45:40 +0000612 case BO_EQ:
613 case BO_GT:
614 case BO_GE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000615 return ValMgr.makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000616 case BO_NE:
617 case BO_LT:
618 case BO_LE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000619 return ValMgr.makeTruthVal(true, resultTy);
620 }
621 }
622
623 // Comparing an arbitrary integer to a region or label address is
624 // completely unknowable.
625 return UnknownVal();
626 }
627 case loc::MemRegionKind: {
628 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
629 // If one of the operands is a symbol and the other is a constant,
630 // build an expression for use by the constraint manager.
631 if (SymbolRef lSym = lhs.getAsLocSymbol())
632 return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy);
633
634 // Special case comparisons to NULL.
635 // This must come after the test if the LHS is a symbol, which is used to
636 // build constraints. The address of any non-symbolic region is guaranteed
637 // to be non-NULL.
638 if (rInt->isZeroConstant()) {
639 switch (op) {
640 default:
641 break;
John McCall2de56d12010-08-25 11:45:40 +0000642 case BO_Sub:
Jordy Roseeac4a002010-06-28 08:26:15 +0000643 return EvalCastL(lhs, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000644 case BO_EQ:
645 case BO_LT:
646 case BO_LE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000647 return ValMgr.makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000648 case BO_NE:
649 case BO_GT:
650 case BO_GE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000651 return ValMgr.makeTruthVal(true, resultTy);
652 }
653 }
654
655 // Comparing a region to an arbitrary integer is completely unknowable.
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000656 return UnknownVal();
Jordy Roseeac4a002010-06-28 08:26:15 +0000657 }
658
659 // Get both values as regions, if possible.
660 const MemRegion *LeftMR = lhs.getAsRegion();
661 assert(LeftMR && "MemRegionKind SVal doesn't have a region!");
662
663 const MemRegion *RightMR = rhs.getAsRegion();
664 if (!RightMR)
665 // The RHS is probably a label, which in theory could address a region.
666 // FIXME: we can probably make a more useful statement about non-code
667 // regions, though.
668 return UnknownVal();
669
670 // If both values wrap regions, see if they're from different base regions.
671 const MemRegion *LeftBase = LeftMR->getBaseRegion();
672 const MemRegion *RightBase = RightMR->getBaseRegion();
673 if (LeftBase != RightBase &&
674 !isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) {
675 switch (op) {
676 default:
677 return UnknownVal();
John McCall2de56d12010-08-25 11:45:40 +0000678 case BO_EQ:
Jordy Roseeac4a002010-06-28 08:26:15 +0000679 return ValMgr.makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000680 case BO_NE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000681 return ValMgr.makeTruthVal(true, resultTy);
682 }
683 }
684
685 // The two regions are from the same base region. See if they're both a
686 // type of region we know how to compare.
687
688 // FIXME: If/when there is a getAsRawOffset() for FieldRegions, this
689 // ElementRegion path and the FieldRegion path below should be unified.
690 if (const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR)) {
691 // First see if the right region is also an ElementRegion.
692 const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR);
693 if (!RightER)
694 return UnknownVal();
695
696 // Next, see if the two ERs have the same super-region and matching types.
697 // FIXME: This should do something useful even if the types don't match,
698 // though if both indexes are constant the RegionRawOffset path will
699 // give the correct answer.
700 if (LeftER->getSuperRegion() == RightER->getSuperRegion() &&
701 LeftER->getElementType() == RightER->getElementType()) {
702 // Get the left index and cast it to the correct type.
703 // If the index is unknown or undefined, bail out here.
704 SVal LeftIndexVal = LeftER->getIndex();
705 NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
706 if (!LeftIndex)
707 return UnknownVal();
708 LeftIndexVal = EvalCastNL(*LeftIndex, resultTy);
709 LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
710 if (!LeftIndex)
711 return UnknownVal();
712
713 // Do the same for the right index.
714 SVal RightIndexVal = RightER->getIndex();
715 NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
716 if (!RightIndex)
717 return UnknownVal();
718 RightIndexVal = EvalCastNL(*RightIndex, resultTy);
719 RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
720 if (!RightIndex)
721 return UnknownVal();
722
723 // Actually perform the operation.
724 // EvalBinOpNN expects the two indexes to already be the right type.
725 return EvalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy);
726 }
727
728 // If the element indexes aren't comparable, see if the raw offsets are.
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000729 RegionRawOffset LeftOffset = LeftER->getAsArrayOffset();
730 RegionRawOffset RightOffset = RightER->getAsArrayOffset();
Jordy Roseeac4a002010-06-28 08:26:15 +0000731
732 if (LeftOffset.getRegion() != NULL &&
733 LeftOffset.getRegion() == RightOffset.getRegion()) {
734 int64_t left = LeftOffset.getByteOffset();
735 int64_t right = RightOffset.getByteOffset();
736
737 switch (op) {
738 default:
739 return UnknownVal();
John McCall2de56d12010-08-25 11:45:40 +0000740 case BO_LT:
Jordy Roseeac4a002010-06-28 08:26:15 +0000741 return ValMgr.makeTruthVal(left < right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000742 case BO_GT:
Jordy Roseeac4a002010-06-28 08:26:15 +0000743 return ValMgr.makeTruthVal(left > right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000744 case BO_LE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000745 return ValMgr.makeTruthVal(left <= right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000746 case BO_GE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000747 return ValMgr.makeTruthVal(left >= right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000748 case BO_EQ:
Jordy Roseeac4a002010-06-28 08:26:15 +0000749 return ValMgr.makeTruthVal(left == right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000750 case BO_NE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000751 return ValMgr.makeTruthVal(left != right, resultTy);
752 }
753 }
754
755 // If we get here, we have no way of comparing the ElementRegions.
756 return UnknownVal();
757 }
758
759 // See if both regions are fields of the same structure.
760 // FIXME: This doesn't handle nesting, inheritance, or Objective-C ivars.
761 if (const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR)) {
762 // Only comparisons are meaningful here!
763 if (!BinaryOperator::isComparisonOp(op))
764 return UnknownVal();
765
766 // First see if the right region is also a FieldRegion.
767 const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR);
768 if (!RightFR)
769 return UnknownVal();
770
771 // Next, see if the two FRs have the same super-region.
772 // FIXME: This doesn't handle casts yet, and simply stripping the casts
773 // doesn't help.
774 if (LeftFR->getSuperRegion() != RightFR->getSuperRegion())
775 return UnknownVal();
776
777 const FieldDecl *LeftFD = LeftFR->getDecl();
778 const FieldDecl *RightFD = RightFR->getDecl();
779 const RecordDecl *RD = LeftFD->getParent();
780
781 // Make sure the two FRs are from the same kind of record. Just in case!
782 // FIXME: This is probably where inheritance would be a problem.
783 if (RD != RightFD->getParent())
784 return UnknownVal();
785
786 // We know for sure that the two fields are not the same, since that
787 // would have given us the same SVal.
John McCall2de56d12010-08-25 11:45:40 +0000788 if (op == BO_EQ)
Jordy Roseeac4a002010-06-28 08:26:15 +0000789 return ValMgr.makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000790 if (op == BO_NE)
Jordy Roseeac4a002010-06-28 08:26:15 +0000791 return ValMgr.makeTruthVal(true, resultTy);
792
793 // Iterate through the fields and see which one comes first.
794 // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field
795 // members and the units in which bit-fields reside have addresses that
796 // increase in the order in which they are declared."
John McCall2de56d12010-08-25 11:45:40 +0000797 bool leftFirst = (op == BO_LT || op == BO_LE);
Jordy Roseeac4a002010-06-28 08:26:15 +0000798 for (RecordDecl::field_iterator I = RD->field_begin(),
799 E = RD->field_end(); I!=E; ++I) {
800 if (*I == LeftFD)
801 return ValMgr.makeTruthVal(leftFirst, resultTy);
802 if (*I == RightFD)
803 return ValMgr.makeTruthVal(!leftFirst, resultTy);
804 }
805
806 assert(false && "Fields not found in parent record's definition");
807 }
808
809 // If we get here, we have no way of comparing the regions.
810 return UnknownVal();
811 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000812 }
813}
814
Ted Kremenek846eabd2010-12-01 21:28:31 +0000815SVal SimpleSValBuilder::EvalBinOpLN(const GRState *state,
Ted Kremeneke8391722009-06-26 00:25:05 +0000816 BinaryOperator::Opcode op,
Mike Stump1eb44332009-09-09 15:08:12 +0000817 Loc lhs, NonLoc rhs, QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000818 // Special case: 'rhs' is an integer that has the same width as a pointer and
819 // we are using the integer location in a comparison. Normally this cannot be
820 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
821 // can generate comparisons that trigger this code.
822 // FIXME: Are all locations guaranteed to have pointer width?
Jordy Roseeac4a002010-06-28 08:26:15 +0000823 if (BinaryOperator::isComparisonOp(op)) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000824 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
825 const llvm::APSInt *x = &rhsInt->getValue();
826 ASTContext &ctx = ValMgr.getContext();
827 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
828 // Convert the signedness of the integer (if necessary).
829 if (x->isSigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000830 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
Ted Kremeneke8391722009-06-26 00:25:05 +0000831
Jordy Roseeac4a002010-06-28 08:26:15 +0000832 return EvalBinOpLL(state, op, lhs, loc::ConcreteInt(*x), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000833 }
834 }
835 }
Ted Kremenek56af4462010-09-03 01:07:06 +0000836
837 // We are dealing with pointer arithmetic.
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Ted Kremenek56af4462010-09-03 01:07:06 +0000839 // Handle pointer arithmetic on constant values.
840 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
841 if (loc::ConcreteInt *lhsInt = dyn_cast<loc::ConcreteInt>(&lhs)) {
842 const llvm::APSInt &leftI = lhsInt->getValue();
843 assert(leftI.isUnsigned());
844 llvm::APSInt rightI(rhsInt->getValue(), /* isUnsigned */ true);
845
846 // Convert the bitwidth of rightI. This should deal with overflow
847 // since we are dealing with concrete values.
848 rightI.extOrTrunc(leftI.getBitWidth());
849
850 // Offset the increment by the pointer size.
Ted Kremenek56af4462010-09-03 01:07:06 +0000851 llvm::APSInt Multiplicand(rightI.getBitWidth(), /* isUnsigned */ true);
852 rightI *= Multiplicand;
853
854 // Compute the adjusted pointer.
855 switch (op) {
856 case BO_Add:
857 rightI = leftI + rightI;
858 break;
859 case BO_Sub:
860 rightI = leftI - rightI;
861 break;
862 default:
863 llvm_unreachable("Invalid pointer arithmetic operation");
864 }
865 return loc::ConcreteInt(ValMgr.getBasicValueFactory().getValue(rightI));
866 }
867 }
868
869
870 // Delegate remaining pointer arithmetic to the StoreManager.
Zhongxing Xu461147f2010-02-05 05:24:20 +0000871 return state->getStateManager().getStoreManager().EvalBinOp(op, lhs,
Ted Kremeneke8391722009-06-26 00:25:05 +0000872 rhs, resultTy);
873}
Jordy Rose32f26562010-07-04 00:00:41 +0000874
Ted Kremenek846eabd2010-12-01 21:28:31 +0000875const llvm::APSInt *SimpleSValBuilder::getKnownValue(const GRState *state,
Jordy Rose32f26562010-07-04 00:00:41 +0000876 SVal V) {
877 if (V.isUnknownOrUndef())
878 return NULL;
879
880 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
881 return &X->getValue();
882
883 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
884 return &X->getValue();
885
886 if (SymbolRef Sym = V.getAsSymbol())
887 return state->getSymVal(Sym);
888
889 // FIXME: Add support for SymExprs.
890 return NULL;
891}