blob: bcd303e8fbdaeb65226b103312d6368f542975c1 [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
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000014#include "clang/GR/PathSensitive/SValBuilder.h"
15#include "clang/GR/PathSensitive/GRState.h"
Ted Kremeneke8391722009-06-26 00:25:05 +000016
17using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000018using namespace ento;
Ted Kremeneke8391722009-06-26 00:25:05 +000019
20namespace {
Ted Kremenek846eabd2010-12-01 21:28:31 +000021class SimpleSValBuilder : public SValBuilder {
Ted Kremenek32c3fa42009-07-21 21:03:30 +000022protected:
Ted Kremenek9c149532010-12-01 21:57:22 +000023 virtual SVal evalCastNL(NonLoc val, QualType castTy);
24 virtual SVal evalCastL(Loc val, QualType castTy);
Ted Kremenek32c3fa42009-07-21 21:03:30 +000025
Ted Kremeneke8391722009-06-26 00:25:05 +000026public:
Ted Kremenekc8413fd2010-12-02 07:49:45 +000027 SimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
28 GRStateManager &stateMgr)
29 : SValBuilder(alloc, context, stateMgr) {}
Ted Kremenek846eabd2010-12-01 21:28:31 +000030 virtual ~SimpleSValBuilder() {}
Mike Stump1eb44332009-09-09 15:08:12 +000031
Ted Kremenek9c149532010-12-01 21:57:22 +000032 virtual SVal evalMinus(NonLoc val);
33 virtual SVal evalComplement(NonLoc val);
34 virtual SVal evalBinOpNN(const GRState *state, BinaryOperator::Opcode op,
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +000035 NonLoc lhs, NonLoc rhs, QualType resultTy);
Ted Kremenek9c149532010-12-01 21:57:22 +000036 virtual SVal evalBinOpLL(const GRState *state, BinaryOperator::Opcode op,
Jordy Roseeac4a002010-06-28 08:26:15 +000037 Loc lhs, Loc rhs, QualType resultTy);
Ted Kremenek9c149532010-12-01 21:57:22 +000038 virtual SVal evalBinOpLN(const GRState *state, BinaryOperator::Opcode op,
Ted Kremeneke8391722009-06-26 00:25:05 +000039 Loc lhs, NonLoc rhs, QualType resultTy);
Jordy Rose32f26562010-07-04 00:00:41 +000040
Ted Kremenek9c149532010-12-01 21:57:22 +000041 /// getKnownValue - evaluates a given SVal. If the SVal has only one possible
Jordy Rose32f26562010-07-04 00:00:41 +000042 /// (integer) value, that value is returned. Otherwise, returns NULL.
43 virtual const llvm::APSInt *getKnownValue(const GRState *state, SVal V);
Jordy Rose43fdb7f2010-06-20 04:56:29 +000044
45 SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op,
46 const llvm::APSInt &RHS, QualType resultTy);
Mike Stump1eb44332009-09-09 15:08:12 +000047};
Ted Kremeneke8391722009-06-26 00:25:05 +000048} // end anonymous namespace
49
Ted Kremenek9ef65372010-12-23 07:20:52 +000050SValBuilder *ento::createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
51 ASTContext &context,
52 GRStateManager &stateMgr) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +000053 return new SimpleSValBuilder(alloc, context, stateMgr);
Ted Kremeneke8391722009-06-26 00:25:05 +000054}
55
56//===----------------------------------------------------------------------===//
57// Transfer function for Casts.
58//===----------------------------------------------------------------------===//
59
Ted Kremenek9c149532010-12-01 21:57:22 +000060SVal SimpleSValBuilder::evalCastNL(NonLoc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000061
Ted Kremenekdd661142009-07-20 21:39:27 +000062 bool isLocType = Loc::IsLocType(castTy);
Mike Stump1eb44332009-09-09 15:08:12 +000063
Ted Kremenek9031dd72009-07-21 00:12:07 +000064 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) {
65 if (isLocType)
Ted Kremenekdd661142009-07-20 21:39:27 +000066 return LI->getLoc();
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ted Kremenekc50e6df2010-01-11 02:33:26 +000068 // FIXME: Correctly support promotions/truncations.
Ted Kremenekc8413fd2010-12-02 07:49:45 +000069 unsigned castSize = Context.getTypeSize(castTy);
Ted Kremenekc50e6df2010-01-11 02:33:26 +000070 if (castSize == LI->getNumBits())
Ted Kremenek9031dd72009-07-21 00:12:07 +000071 return val;
Ted Kremenekc8413fd2010-12-02 07:49:45 +000072 return makeLocAsInteger(LI->getLoc(), castSize);
Ted Kremenek9031dd72009-07-21 00:12:07 +000073 }
74
75 if (const SymExpr *se = val.getAsSymbolicExpression()) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +000076 QualType T = Context.getCanonicalType(se->getType(Context));
77 if (T == Context.getCanonicalType(castTy))
Ted Kremenek9031dd72009-07-21 00:12:07 +000078 return val;
Ted Kremenek80417472009-09-25 00:18:15 +000079
80 // FIXME: Remove this hack when we support symbolic truncation/extension.
81 // HACK: If both castTy and T are integers, ignore the cast. This is
82 // not a permanent solution. Eventually we want to precisely handle
83 // extension/truncation of symbolic integers. This prevents us from losing
84 // precision when we assign 'x = y' and 'y' is symbolic and x and y are
85 // different integer types.
86 if (T->isIntegerType() && castTy->isIntegerType())
87 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Ted Kremenek9031dd72009-07-21 00:12:07 +000089 return UnknownVal();
90 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremeneke8391722009-06-26 00:25:05 +000092 if (!isa<nonloc::ConcreteInt>(val))
93 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000094
Ted Kremeneke8391722009-06-26 00:25:05 +000095 // Only handle casts from integers to integers.
96 if (!isLocType && !castTy->isIntegerType())
97 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremeneke8391722009-06-26 00:25:05 +000099 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
100 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
Jay Foad9f71a8f2010-12-07 08:25:34 +0000101 i = i.extOrTrunc(Context.getTypeSize(castTy));
Ted Kremeneke8391722009-06-26 00:25:05 +0000102
103 if (isLocType)
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000104 return makeIntLocVal(i);
Ted Kremeneke8391722009-06-26 00:25:05 +0000105 else
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000106 return makeIntVal(i);
Ted Kremeneke8391722009-06-26 00:25:05 +0000107}
108
Ted Kremenek9c149532010-12-01 21:57:22 +0000109SVal SimpleSValBuilder::evalCastL(Loc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremeneke8391722009-06-26 00:25:05 +0000111 // Casts from pointers -> pointers, just return the lval.
112 //
113 // Casts from pointers -> references, just return the lval. These
114 // can be introduced by the frontend for corner cases, e.g
115 // casting from va_list* to __builtin_va_list&.
116 //
Ted Kremeneke8391722009-06-26 00:25:05 +0000117 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
118 return val;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Ted Kremeneke8391722009-06-26 00:25:05 +0000120 // FIXME: Handle transparent unions where a value can be "transparently"
121 // lifted into a union type.
122 if (castTy->isUnionType())
123 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenekd617b852010-04-16 17:54:33 +0000125 if (castTy->isIntegerType()) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000126 unsigned BitWidth = Context.getTypeSize(castTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000127
Ted Kremenekd617b852010-04-16 17:54:33 +0000128 if (!isa<loc::ConcreteInt>(val))
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000129 return makeLocAsInteger(val, BitWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Ted Kremenekd617b852010-04-16 17:54:33 +0000131 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
132 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
Jay Foad9f71a8f2010-12-07 08:25:34 +0000133 i = i.extOrTrunc(BitWidth);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000134 return makeIntVal(i);
Ted Kremenekd617b852010-04-16 17:54:33 +0000135 }
136
137 // All other cases: return 'UnknownVal'. This includes casting pointers
138 // to floats, which is probably badness it itself, but this is a good
139 // intermediate solution until we do something better.
140 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000141}
142
143//===----------------------------------------------------------------------===//
144// Transfer function for unary operators.
145//===----------------------------------------------------------------------===//
146
Ted Kremenek9c149532010-12-01 21:57:22 +0000147SVal SimpleSValBuilder::evalMinus(NonLoc val) {
Mike Stump1eb44332009-09-09 15:08:12 +0000148 switch (val.getSubKind()) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000149 case nonloc::ConcreteIntKind:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000150 return cast<nonloc::ConcreteInt>(val).evalMinus(*this);
Ted Kremeneke8391722009-06-26 00:25:05 +0000151 default:
152 return UnknownVal();
153 }
154}
155
Ted Kremenek9c149532010-12-01 21:57:22 +0000156SVal SimpleSValBuilder::evalComplement(NonLoc X) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000157 switch (X.getSubKind()) {
158 case nonloc::ConcreteIntKind:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000159 return cast<nonloc::ConcreteInt>(X).evalComplement(*this);
Ted Kremeneke8391722009-06-26 00:25:05 +0000160 default:
161 return UnknownVal();
162 }
163}
164
165//===----------------------------------------------------------------------===//
166// Transfer function for binary operators.
167//===----------------------------------------------------------------------===//
168
169static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
170 switch (op) {
171 default:
172 assert(false && "Invalid opcode.");
John McCall2de56d12010-08-25 11:45:40 +0000173 case BO_LT: return BO_GE;
174 case BO_GT: return BO_LE;
175 case BO_LE: return BO_GT;
176 case BO_GE: return BO_LT;
177 case BO_EQ: return BO_NE;
178 case BO_NE: return BO_EQ;
Ted Kremeneke8391722009-06-26 00:25:05 +0000179 }
180}
181
Jordy Roseeac4a002010-06-28 08:26:15 +0000182static BinaryOperator::Opcode ReverseComparison(BinaryOperator::Opcode op) {
183 switch (op) {
184 default:
185 assert(false && "Invalid opcode.");
John McCall2de56d12010-08-25 11:45:40 +0000186 case BO_LT: return BO_GT;
187 case BO_GT: return BO_LT;
188 case BO_LE: return BO_GE;
189 case BO_GE: return BO_LE;
190 case BO_EQ:
191 case BO_NE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000192 return op;
Ted Kremeneke8391722009-06-26 00:25:05 +0000193 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000194}
195
Ted Kremenek846eabd2010-12-01 21:28:31 +0000196SVal SimpleSValBuilder::MakeSymIntVal(const SymExpr *LHS,
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000197 BinaryOperator::Opcode op,
198 const llvm::APSInt &RHS,
199 QualType resultTy) {
200 bool isIdempotent = false;
201
202 // Check for a few special cases with known reductions first.
203 switch (op) {
204 default:
205 // We can't reduce this case; just treat it normally.
206 break;
John McCall2de56d12010-08-25 11:45:40 +0000207 case BO_Mul:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000208 // a*0 and a*1
209 if (RHS == 0)
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000210 return makeIntVal(0, resultTy);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000211 else if (RHS == 1)
212 isIdempotent = true;
213 break;
John McCall2de56d12010-08-25 11:45:40 +0000214 case BO_Div:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000215 // a/0 and a/1
216 if (RHS == 0)
217 // This is also handled elsewhere.
218 return UndefinedVal();
219 else if (RHS == 1)
220 isIdempotent = true;
221 break;
John McCall2de56d12010-08-25 11:45:40 +0000222 case BO_Rem:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000223 // a%0 and a%1
224 if (RHS == 0)
225 // This is also handled elsewhere.
226 return UndefinedVal();
227 else if (RHS == 1)
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000228 return makeIntVal(0, resultTy);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000229 break;
John McCall2de56d12010-08-25 11:45:40 +0000230 case BO_Add:
231 case BO_Sub:
232 case BO_Shl:
233 case BO_Shr:
234 case BO_Xor:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000235 // a+0, a-0, a<<0, a>>0, a^0
236 if (RHS == 0)
237 isIdempotent = true;
238 break;
John McCall2de56d12010-08-25 11:45:40 +0000239 case BO_And:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000240 // a&0 and a&(~0)
241 if (RHS == 0)
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000242 return makeIntVal(0, resultTy);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000243 else if (RHS.isAllOnesValue())
244 isIdempotent = true;
245 break;
John McCall2de56d12010-08-25 11:45:40 +0000246 case BO_Or:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000247 // a|0 and a|(~0)
248 if (RHS == 0)
249 isIdempotent = true;
250 else if (RHS.isAllOnesValue()) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000251 const llvm::APSInt &Result = BasicVals.Convert(resultTy, RHS);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000252 return nonloc::ConcreteInt(Result);
253 }
254 break;
255 }
256
257 // Idempotent ops (like a*1) can still change the type of an expression.
Ted Kremenek9c149532010-12-01 21:57:22 +0000258 // Wrap the LHS up in a NonLoc again and let evalCastNL do the dirty work.
Benjamin Kramer24ada872010-06-20 10:20:36 +0000259 if (isIdempotent) {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000260 if (SymbolRef LHSSym = dyn_cast<SymbolData>(LHS))
Ted Kremenek9c149532010-12-01 21:57:22 +0000261 return evalCastNL(nonloc::SymbolVal(LHSSym), resultTy);
262 return evalCastNL(nonloc::SymExprVal(LHS), resultTy);
Benjamin Kramer24ada872010-06-20 10:20:36 +0000263 }
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000264
265 // If we reach this point, the expression cannot be simplified.
266 // Make a SymExprVal for the entire thing.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000267 return makeNonLoc(LHS, op, RHS, resultTy);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000268}
269
Ted Kremenek9c149532010-12-01 21:57:22 +0000270SVal SimpleSValBuilder::evalBinOpNN(const GRState *state,
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000271 BinaryOperator::Opcode op,
Ted Kremeneke8391722009-06-26 00:25:05 +0000272 NonLoc lhs, NonLoc rhs,
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000273 QualType resultTy) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000274 // Handle trivial case where left-side and right-side are the same.
275 if (lhs == rhs)
276 switch (op) {
277 default:
278 break;
John McCall2de56d12010-08-25 11:45:40 +0000279 case BO_EQ:
280 case BO_LE:
281 case BO_GE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000282 return makeTruthVal(true, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000283 case BO_LT:
284 case BO_GT:
285 case BO_NE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000286 return makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000287 case BO_Xor:
288 case BO_Sub:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000289 return makeIntVal(0, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000290 case BO_Or:
291 case BO_And:
Ted Kremenek9c149532010-12-01 21:57:22 +0000292 return evalCastNL(lhs, resultTy);
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000293 }
Mike Stump1eb44332009-09-09 15:08:12 +0000294
Ted Kremeneke8391722009-06-26 00:25:05 +0000295 while (1) {
296 switch (lhs.getSubKind()) {
297 default:
Mike Stump1eb44332009-09-09 15:08:12 +0000298 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000299 case nonloc::LocAsIntegerKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000300 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
Ted Kremeneke8391722009-06-26 00:25:05 +0000301 switch (rhs.getSubKind()) {
302 case nonloc::LocAsIntegerKind:
Ted Kremenek9c149532010-12-01 21:57:22 +0000303 return evalBinOpLL(state, op, lhsL,
Jordy Roseeac4a002010-06-28 08:26:15 +0000304 cast<nonloc::LocAsInteger>(rhs).getLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +0000305 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000306 case nonloc::ConcreteIntKind: {
307 // Transform the integer into a location and compare.
Ted Kremeneke8391722009-06-26 00:25:05 +0000308 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
309 i.setIsUnsigned(true);
Jay Foad9f71a8f2010-12-07 08:25:34 +0000310 i = i.extOrTrunc(Context.getTypeSize(Context.VoidPtrTy));
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000311 return evalBinOpLL(state, op, lhsL, 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 Kremenekc8413fd2010-12-02 07:49:45 +0000316 return makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000317 case BO_NE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000318 return makeTruthVal(true, resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000319 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:
Argyrios Kyrtzidisd2592a32010-12-22 18:53:44 +0000361 assert(false && "'=' and ',' operators handled by ExprEngine.");
Jordy Roseba0f61c2010-06-18 22:49:11 +0000362 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);
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000375 assert(symIntExpr->getType(Context) == resultTy);
376 return makeNonLoc(symIntExpr->getLHS(), opc,
Jordy Roseba0f61c2010-06-18 22:49:11 +0000377 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)) {
Jordy Roseb4954a42010-06-21 20:15:15 +0000391 // resultTy may not be the best type to convert to, but it's
392 // probably the best choice in expressions with mixed type
393 // (such as x+1U+2LL). The rules for implicit conversions should
394 // choose a reasonable type to preserve the expression, and will
395 // at least match how the value is going to be used.
396 const llvm::APSInt &first =
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000397 BasicVals.Convert(resultTy, symIntExpr->getRHS());
Jordy Roseb4954a42010-06-21 20:15:15 +0000398 const llvm::APSInt &second =
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000399 BasicVals.Convert(resultTy, rhsInt->getValue());
Jordy Roseba0f61c2010-06-18 22:49:11 +0000400 const llvm::APSInt *newRHS;
401 if (lop == op)
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000402 newRHS = BasicVals.evalAPSInt(BO_Add, first, second);
Jordy Roseba0f61c2010-06-18 22:49:11 +0000403 else
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000404 newRHS = BasicVals.evalAPSInt(BO_Sub, first, second);
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000405 return MakeSymIntVal(symIntExpr->getLHS(), lop, *newRHS, resultTy);
Jordy Roseba0f61c2010-06-18 22:49:11 +0000406 }
407 }
408
409 // Otherwise, make a SymExprVal out of the expression.
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000410 return MakeSymIntVal(symIntExpr, op, rhsInt->getValue(), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412 case nonloc::ConcreteIntKind: {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000413 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
414
Ted Kremeneke8391722009-06-26 00:25:05 +0000415 if (isa<nonloc::ConcreteInt>(rhs)) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000416 return lhsInt.evalBinOp(*this, op, cast<nonloc::ConcreteInt>(rhs));
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000417 } else {
418 const llvm::APSInt& lhsValue = lhsInt.getValue();
419
Ted Kremeneke8391722009-06-26 00:25:05 +0000420 // Swap the left and right sides and flip the operator if doing so
421 // allows us to better reason about the expression (this is a form
422 // of expression canonicalization).
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000423 // While we're at it, catch some special cases for non-commutative ops.
Ted Kremeneke8391722009-06-26 00:25:05 +0000424 NonLoc tmp = rhs;
425 rhs = lhs;
426 lhs = tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Ted Kremeneke8391722009-06-26 00:25:05 +0000428 switch (op) {
John McCall2de56d12010-08-25 11:45:40 +0000429 case BO_LT:
430 case BO_GT:
431 case BO_LE:
432 case BO_GE:
Jordy Roseeac4a002010-06-28 08:26:15 +0000433 op = ReverseComparison(op);
434 continue;
John McCall2de56d12010-08-25 11:45:40 +0000435 case BO_EQ:
436 case BO_NE:
437 case BO_Add:
438 case BO_Mul:
439 case BO_And:
440 case BO_Xor:
441 case BO_Or:
Ted Kremeneke8391722009-06-26 00:25:05 +0000442 continue;
John McCall2de56d12010-08-25 11:45:40 +0000443 case BO_Shr:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000444 if (lhsValue.isAllOnesValue() && lhsValue.isSigned())
445 // At this point lhs and rhs have been swapped.
446 return rhs;
447 // FALL-THROUGH
John McCall2de56d12010-08-25 11:45:40 +0000448 case BO_Shl:
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000449 if (lhsValue == 0)
450 // At this point lhs and rhs have been swapped.
451 return rhs;
452 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000453 default:
454 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000455 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000456 }
457 }
458 case nonloc::SymbolValKind: {
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000459 nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs);
460 SymbolRef Sym = slhs->getSymbol();
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 Kremenekc8413fd2010-12-02 07:49:45 +0000463 if (Sym->getType(Context)->isIntegerType())
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000464 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.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000467 const llvm::APSInt &lhs_I = BasicVals.Convert(resultTy, *Constant);
Ted Kremenek9b020342009-10-17 07:39:35 +0000468 lhs = nonloc::ConcreteInt(lhs_I);
Ted Kremenekb5deae52009-10-16 20:46:24 +0000469
Ted Kremenek9b020342009-10-17 07:39:35 +0000470 // Also promote the RHS (if necessary).
471
Jordy Rosea277e772010-08-09 20:31:57 +0000472 // For shifts, it is not necessary to promote the RHS.
Ted Kremenek9b020342009-10-17 07:39:35 +0000473 if (BinaryOperator::isShiftOp(op))
474 continue;
475
476 // Other operators: do an implicit conversion. This shouldn't be
Ted Kremenekb5deae52009-10-16 20:46:24 +0000477 // necessary once we support truncation/extension of symbolic values.
Ted Kremenekb1d04222009-10-06 03:44:49 +0000478 if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000479 rhs = nonloc::ConcreteInt(BasicVals.Convert(resultTy,
480 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 }
Jordy Rosea277e772010-08-09 20:31:57 +0000485
486 // Is the RHS a symbol we can simplify?
487 if (const nonloc::SymbolVal *srhs = dyn_cast<nonloc::SymbolVal>(&rhs)) {
488 SymbolRef RSym = srhs->getSymbol();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000489 if (RSym->getType(Context)->isIntegerType()) {
Jordy Rosea277e772010-08-09 20:31:57 +0000490 if (const llvm::APSInt *Constant = state->getSymVal(RSym)) {
491 // The symbol evaluates to a constant.
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000492 const llvm::APSInt &rhs_I = BasicVals.Convert(resultTy, *Constant);
Jordy Rosea277e772010-08-09 20:31:57 +0000493 rhs = nonloc::ConcreteInt(rhs_I);
494 }
495 }
496 }
497
Ted Kremeneke8391722009-06-26 00:25:05 +0000498 if (isa<nonloc::ConcreteInt>(rhs)) {
Jordy Rose43fdb7f2010-06-20 04:56:29 +0000499 return MakeSymIntVal(slhs->getSymbol(), op,
500 cast<nonloc::ConcreteInt>(rhs).getValue(),
501 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000502 }
503
504 return UnknownVal();
505 }
506 }
507 }
508}
509
Jordy Roseeac4a002010-06-28 08:26:15 +0000510// FIXME: all this logic will change if/when we have MemRegion::getLocation().
Ted Kremenek9c149532010-12-01 21:57:22 +0000511SVal SimpleSValBuilder::evalBinOpLL(const GRState *state,
Jordy Roseeac4a002010-06-28 08:26:15 +0000512 BinaryOperator::Opcode op,
513 Loc lhs, Loc rhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000514 QualType resultTy) {
Jordy Roseeac4a002010-06-28 08:26:15 +0000515 // Only comparisons and subtractions are valid operations on two pointers.
516 // See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15].
Ted Kremenek9c149532010-12-01 21:57:22 +0000517 // However, if a pointer is casted to an integer, evalBinOpNN may end up
Jordy Rosea2741482010-06-30 01:35:20 +0000518 // calling this function with another operation (PR7527). We don't attempt to
519 // model this for now, but it could be useful, particularly when the
520 // "location" is actually an integer value that's been passed through a void*.
John McCall2de56d12010-08-25 11:45:40 +0000521 if (!(BinaryOperator::isComparisonOp(op) || op == BO_Sub))
Jordy Rosea2741482010-06-30 01:35:20 +0000522 return UnknownVal();
Jordy Roseeac4a002010-06-28 08:26:15 +0000523
524 // Special cases for when both sides are identical.
525 if (lhs == rhs) {
526 switch (op) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000527 default:
Jordy Roseeac4a002010-06-28 08:26:15 +0000528 assert(false && "Unimplemented operation for two identical values");
Ted Kremeneke8391722009-06-26 00:25:05 +0000529 return UnknownVal();
John McCall2de56d12010-08-25 11:45:40 +0000530 case BO_Sub:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000531 return makeZeroVal(resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000532 case BO_EQ:
533 case BO_LE:
534 case BO_GE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000535 return makeTruthVal(true, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000536 case BO_NE:
537 case BO_LT:
538 case BO_GT:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000539 return makeTruthVal(false, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000540 }
541 }
542
543 switch (lhs.getSubKind()) {
544 default:
545 assert(false && "Ordering not implemented for this Loc.");
546 return UnknownVal();
547
548 case loc::GotoLabelKind:
549 // The only thing we know about labels is that they're non-null.
550 if (rhs.isZeroConstant()) {
551 switch (op) {
552 default:
553 break;
John McCall2de56d12010-08-25 11:45:40 +0000554 case BO_Sub:
Ted Kremenek9c149532010-12-01 21:57:22 +0000555 return evalCastL(lhs, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000556 case BO_EQ:
557 case BO_LE:
558 case BO_LT:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000559 return makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000560 case BO_NE:
561 case BO_GT:
562 case BO_GE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000563 return makeTruthVal(true, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000564 }
565 }
566 // There may be two labels for the same location, and a function region may
567 // have the same address as a label at the start of the function (depending
568 // on the ABI).
569 // FIXME: we can probably do a comparison against other MemRegions, though.
570 // FIXME: is there a way to tell if two labels refer to the same location?
571 return UnknownVal();
572
573 case loc::ConcreteIntKind: {
574 // If one of the operands is a symbol and the other is a constant,
575 // build an expression for use by the constraint manager.
576 if (SymbolRef rSym = rhs.getAsLocSymbol()) {
577 // We can only build expressions with symbols on the left,
578 // so we need a reversible operator.
579 if (!BinaryOperator::isComparisonOp(op))
580 return UnknownVal();
581
582 const llvm::APSInt &lVal = cast<loc::ConcreteInt>(lhs).getValue();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000583 return makeNonLoc(rSym, ReverseComparison(op), lVal, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000584 }
585
586 // If both operands are constants, just perform the operation.
587 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000588 SVal ResultVal = cast<loc::ConcreteInt>(lhs).evalBinOp(BasicVals, op,
589 *rInt);
Jordy Roseeac4a002010-06-28 08:26:15 +0000590 if (Loc *Result = dyn_cast<Loc>(&ResultVal))
Ted Kremenek9c149532010-12-01 21:57:22 +0000591 return evalCastL(*Result, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000592 else
593 return UnknownVal();
594 }
595
596 // Special case comparisons against NULL.
597 // This must come after the test if the RHS is a symbol, which is used to
598 // build constraints. The address of any non-symbolic region is guaranteed
599 // to be non-NULL, as is any label.
600 assert(isa<loc::MemRegionVal>(rhs) || isa<loc::GotoLabel>(rhs));
601 if (lhs.isZeroConstant()) {
602 switch (op) {
603 default:
604 break;
John McCall2de56d12010-08-25 11:45:40 +0000605 case BO_EQ:
606 case BO_GT:
607 case BO_GE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000608 return makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000609 case BO_NE:
610 case BO_LT:
611 case BO_LE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000612 return makeTruthVal(true, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000613 }
614 }
615
616 // Comparing an arbitrary integer to a region or label address is
617 // completely unknowable.
618 return UnknownVal();
619 }
620 case loc::MemRegionKind: {
621 if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) {
622 // If one of the operands is a symbol and the other is a constant,
623 // build an expression for use by the constraint manager.
624 if (SymbolRef lSym = lhs.getAsLocSymbol())
625 return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy);
626
627 // Special case comparisons to NULL.
628 // This must come after the test if the LHS is a symbol, which is used to
629 // build constraints. The address of any non-symbolic region is guaranteed
630 // to be non-NULL.
631 if (rInt->isZeroConstant()) {
632 switch (op) {
633 default:
634 break;
John McCall2de56d12010-08-25 11:45:40 +0000635 case BO_Sub:
Ted Kremenek9c149532010-12-01 21:57:22 +0000636 return evalCastL(lhs, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000637 case BO_EQ:
638 case BO_LT:
639 case BO_LE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000640 return makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000641 case BO_NE:
642 case BO_GT:
643 case BO_GE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000644 return makeTruthVal(true, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000645 }
646 }
647
648 // Comparing a region to an arbitrary integer is completely unknowable.
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +0000649 return UnknownVal();
Jordy Roseeac4a002010-06-28 08:26:15 +0000650 }
651
652 // Get both values as regions, if possible.
653 const MemRegion *LeftMR = lhs.getAsRegion();
654 assert(LeftMR && "MemRegionKind SVal doesn't have a region!");
655
656 const MemRegion *RightMR = rhs.getAsRegion();
657 if (!RightMR)
658 // The RHS is probably a label, which in theory could address a region.
659 // FIXME: we can probably make a more useful statement about non-code
660 // regions, though.
661 return UnknownVal();
662
663 // If both values wrap regions, see if they're from different base regions.
664 const MemRegion *LeftBase = LeftMR->getBaseRegion();
665 const MemRegion *RightBase = RightMR->getBaseRegion();
666 if (LeftBase != RightBase &&
667 !isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) {
668 switch (op) {
669 default:
670 return UnknownVal();
John McCall2de56d12010-08-25 11:45:40 +0000671 case BO_EQ:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000672 return makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000673 case BO_NE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000674 return makeTruthVal(true, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000675 }
676 }
677
678 // The two regions are from the same base region. See if they're both a
679 // type of region we know how to compare.
680
681 // FIXME: If/when there is a getAsRawOffset() for FieldRegions, this
682 // ElementRegion path and the FieldRegion path below should be unified.
683 if (const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR)) {
684 // First see if the right region is also an ElementRegion.
685 const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR);
686 if (!RightER)
687 return UnknownVal();
688
689 // Next, see if the two ERs have the same super-region and matching types.
690 // FIXME: This should do something useful even if the types don't match,
691 // though if both indexes are constant the RegionRawOffset path will
692 // give the correct answer.
693 if (LeftER->getSuperRegion() == RightER->getSuperRegion() &&
694 LeftER->getElementType() == RightER->getElementType()) {
695 // Get the left index and cast it to the correct type.
696 // If the index is unknown or undefined, bail out here.
697 SVal LeftIndexVal = LeftER->getIndex();
698 NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
699 if (!LeftIndex)
700 return UnknownVal();
Ted Kremenek9c149532010-12-01 21:57:22 +0000701 LeftIndexVal = evalCastNL(*LeftIndex, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000702 LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal);
703 if (!LeftIndex)
704 return UnknownVal();
705
706 // Do the same for the right index.
707 SVal RightIndexVal = RightER->getIndex();
708 NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
709 if (!RightIndex)
710 return UnknownVal();
Ted Kremenek9c149532010-12-01 21:57:22 +0000711 RightIndexVal = evalCastNL(*RightIndex, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000712 RightIndex = dyn_cast<NonLoc>(&RightIndexVal);
713 if (!RightIndex)
714 return UnknownVal();
715
716 // Actually perform the operation.
Ted Kremenek9c149532010-12-01 21:57:22 +0000717 // evalBinOpNN expects the two indexes to already be the right type.
718 return evalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000719 }
720
721 // If the element indexes aren't comparable, see if the raw offsets are.
Zhongxing Xu7caf9b32010-08-02 04:56:14 +0000722 RegionRawOffset LeftOffset = LeftER->getAsArrayOffset();
723 RegionRawOffset RightOffset = RightER->getAsArrayOffset();
Jordy Roseeac4a002010-06-28 08:26:15 +0000724
725 if (LeftOffset.getRegion() != NULL &&
726 LeftOffset.getRegion() == RightOffset.getRegion()) {
727 int64_t left = LeftOffset.getByteOffset();
728 int64_t right = RightOffset.getByteOffset();
729
730 switch (op) {
731 default:
732 return UnknownVal();
John McCall2de56d12010-08-25 11:45:40 +0000733 case BO_LT:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000734 return makeTruthVal(left < right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000735 case BO_GT:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000736 return makeTruthVal(left > right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000737 case BO_LE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000738 return makeTruthVal(left <= right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000739 case BO_GE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000740 return makeTruthVal(left >= right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000741 case BO_EQ:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000742 return makeTruthVal(left == right, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000743 case BO_NE:
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000744 return makeTruthVal(left != right, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000745 }
746 }
747
748 // If we get here, we have no way of comparing the ElementRegions.
749 return UnknownVal();
750 }
751
752 // See if both regions are fields of the same structure.
753 // FIXME: This doesn't handle nesting, inheritance, or Objective-C ivars.
754 if (const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR)) {
755 // Only comparisons are meaningful here!
756 if (!BinaryOperator::isComparisonOp(op))
757 return UnknownVal();
758
759 // First see if the right region is also a FieldRegion.
760 const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR);
761 if (!RightFR)
762 return UnknownVal();
763
764 // Next, see if the two FRs have the same super-region.
765 // FIXME: This doesn't handle casts yet, and simply stripping the casts
766 // doesn't help.
767 if (LeftFR->getSuperRegion() != RightFR->getSuperRegion())
768 return UnknownVal();
769
770 const FieldDecl *LeftFD = LeftFR->getDecl();
771 const FieldDecl *RightFD = RightFR->getDecl();
772 const RecordDecl *RD = LeftFD->getParent();
773
774 // Make sure the two FRs are from the same kind of record. Just in case!
775 // FIXME: This is probably where inheritance would be a problem.
776 if (RD != RightFD->getParent())
777 return UnknownVal();
778
779 // We know for sure that the two fields are not the same, since that
780 // would have given us the same SVal.
John McCall2de56d12010-08-25 11:45:40 +0000781 if (op == BO_EQ)
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000782 return makeTruthVal(false, resultTy);
John McCall2de56d12010-08-25 11:45:40 +0000783 if (op == BO_NE)
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000784 return makeTruthVal(true, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000785
786 // Iterate through the fields and see which one comes first.
787 // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field
788 // members and the units in which bit-fields reside have addresses that
789 // increase in the order in which they are declared."
John McCall2de56d12010-08-25 11:45:40 +0000790 bool leftFirst = (op == BO_LT || op == BO_LE);
Jordy Roseeac4a002010-06-28 08:26:15 +0000791 for (RecordDecl::field_iterator I = RD->field_begin(),
792 E = RD->field_end(); I!=E; ++I) {
793 if (*I == LeftFD)
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000794 return makeTruthVal(leftFirst, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000795 if (*I == RightFD)
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000796 return makeTruthVal(!leftFirst, resultTy);
Jordy Roseeac4a002010-06-28 08:26:15 +0000797 }
798
799 assert(false && "Fields not found in parent record's definition");
800 }
801
802 // If we get here, we have no way of comparing the regions.
803 return UnknownVal();
804 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000805 }
806}
807
Ted Kremenek9c149532010-12-01 21:57:22 +0000808SVal SimpleSValBuilder::evalBinOpLN(const GRState *state,
Ted Kremeneke8391722009-06-26 00:25:05 +0000809 BinaryOperator::Opcode op,
Mike Stump1eb44332009-09-09 15:08:12 +0000810 Loc lhs, NonLoc rhs, QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000811 // Special case: 'rhs' is an integer that has the same width as a pointer and
812 // we are using the integer location in a comparison. Normally this cannot be
813 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
814 // can generate comparisons that trigger this code.
815 // FIXME: Are all locations guaranteed to have pointer width?
Jordy Roseeac4a002010-06-28 08:26:15 +0000816 if (BinaryOperator::isComparisonOp(op)) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000817 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
818 const llvm::APSInt *x = &rhsInt->getValue();
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000819 ASTContext &ctx = Context;
Ted Kremeneke8391722009-06-26 00:25:05 +0000820 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
821 // Convert the signedness of the integer (if necessary).
822 if (x->isSigned())
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000823 x = &getBasicValueFactory().getValue(*x, true);
Ted Kremeneke8391722009-06-26 00:25:05 +0000824
Ted Kremenek9c149532010-12-01 21:57:22 +0000825 return evalBinOpLL(state, op, lhs, loc::ConcreteInt(*x), resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000826 }
827 }
828 }
Ted Kremenek56af4462010-09-03 01:07:06 +0000829
830 // We are dealing with pointer arithmetic.
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Ted Kremenek56af4462010-09-03 01:07:06 +0000832 // Handle pointer arithmetic on constant values.
833 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
834 if (loc::ConcreteInt *lhsInt = dyn_cast<loc::ConcreteInt>(&lhs)) {
835 const llvm::APSInt &leftI = lhsInt->getValue();
836 assert(leftI.isUnsigned());
837 llvm::APSInt rightI(rhsInt->getValue(), /* isUnsigned */ true);
838
839 // Convert the bitwidth of rightI. This should deal with overflow
840 // since we are dealing with concrete values.
Jay Foad9f71a8f2010-12-07 08:25:34 +0000841 rightI = rightI.extOrTrunc(leftI.getBitWidth());
Ted Kremenek56af4462010-09-03 01:07:06 +0000842
843 // Offset the increment by the pointer size.
Ted Kremenek56af4462010-09-03 01:07:06 +0000844 llvm::APSInt Multiplicand(rightI.getBitWidth(), /* isUnsigned */ true);
845 rightI *= Multiplicand;
846
847 // Compute the adjusted pointer.
848 switch (op) {
849 case BO_Add:
850 rightI = leftI + rightI;
851 break;
852 case BO_Sub:
853 rightI = leftI - rightI;
854 break;
855 default:
856 llvm_unreachable("Invalid pointer arithmetic operation");
857 }
Ted Kremenekc8413fd2010-12-02 07:49:45 +0000858 return loc::ConcreteInt(getBasicValueFactory().getValue(rightI));
Ted Kremenek56af4462010-09-03 01:07:06 +0000859 }
860 }
861
862
863 // Delegate remaining pointer arithmetic to the StoreManager.
Ted Kremenek9c149532010-12-01 21:57:22 +0000864 return state->getStateManager().getStoreManager().evalBinOp(op, lhs,
Ted Kremeneke8391722009-06-26 00:25:05 +0000865 rhs, resultTy);
866}
Jordy Rose32f26562010-07-04 00:00:41 +0000867
Ted Kremenek846eabd2010-12-01 21:28:31 +0000868const llvm::APSInt *SimpleSValBuilder::getKnownValue(const GRState *state,
Jordy Rose32f26562010-07-04 00:00:41 +0000869 SVal V) {
870 if (V.isUnknownOrUndef())
871 return NULL;
872
873 if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V))
874 return &X->getValue();
875
876 if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V))
877 return &X->getValue();
878
879 if (SymbolRef Sym = V.getAsSymbol())
880 return state->getSymVal(Sym);
881
882 // FIXME: Add support for SymExprs.
883 return NULL;
884}