blob: 860013872e490e04f69134578bce6f9c1b128e15 [file] [log] [blame]
Ted Kremenek25a484d2008-02-14 18:28:23 +00001// GRSimpleVals.cpp - Transfer functions for tracking simple values -*- 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//
Gabor Greif2224fcb2008-03-06 10:40:09 +000010// This file defines GRSimpleVals, a sub-class of GRTransferFuncs that
Ted Kremenek25a484d2008-02-14 18:28:23 +000011// provides transfer functions for performing simple value tracking with
12// limited support for symbolics.
13//
14//===----------------------------------------------------------------------===//
15
16#include "GRSimpleVals.h"
Ted Kremenek7a681942008-03-27 17:17:22 +000017#include "BasicObjCFoundationChecks.h"
Ted Kremenek07d46202008-04-02 22:03:53 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000019#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenekabd89ac2008-08-13 04:27:00 +000020#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek7d882202008-04-03 04:42:52 +000021#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekb1983ba2008-04-10 22:16:52 +000022#include "clang/Analysis/LocalCheckers.h"
Ted Kremenek7cefcac2008-04-30 20:17:27 +000023#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek7d882202008-04-03 04:42:52 +000024#include "llvm/Support/Compiler.h"
Ted Kremenek9cac66e2008-02-27 17:56:16 +000025#include <sstream>
Ted Kremenek25a484d2008-02-14 18:28:23 +000026
27using namespace clang;
28
Ted Kremenekd1d4d752008-04-02 07:05:46 +000029//===----------------------------------------------------------------------===//
Ted Kremenekb1983ba2008-04-10 22:16:52 +000030// Transfer Function creation for External clients.
Ted Kremenek139bf402008-04-02 05:15:22 +000031//===----------------------------------------------------------------------===//
Ted Kremenek7d882202008-04-03 04:42:52 +000032
Ted Kremenekb1983ba2008-04-10 22:16:52 +000033GRTransferFuncs* clang::MakeGRSimpleValsTF() { return new GRSimpleVals(); }
Ted Kremenek3862eb12008-02-14 22:36:46 +000034
Ted Kremenek25a484d2008-02-14 18:28:23 +000035//===----------------------------------------------------------------------===//
36// Transfer function for Casts.
37//===----------------------------------------------------------------------===//
38
Zhongxing Xu097fc982008-10-17 05:57:07 +000039SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLoc X, QualType T) {
Ted Kremenekbc965a62008-02-18 22:57:02 +000040
Zhongxing Xu097fc982008-10-17 05:57:07 +000041 if (!isa<nonloc::ConcreteInt>(X))
Ted Kremenek25a484d2008-02-14 18:28:23 +000042 return UnknownVal();
Ted Kremenekce3ed1e2008-03-12 01:21:45 +000043
Zhongxing Xu097fc982008-10-17 05:57:07 +000044 bool isLocType = Loc::IsLocType(T);
Ted Kremenek9ea2f772008-07-16 00:23:49 +000045
Ted Kremenekd9eb5922008-07-15 23:17:54 +000046 // Only handle casts from integers to integers.
Zhongxing Xu097fc982008-10-17 05:57:07 +000047 if (!isLocType && !T->isIntegerType())
Ted Kremenekd9eb5922008-07-15 23:17:54 +000048 return UnknownVal();
49
Ted Kremenekce3ed1e2008-03-12 01:21:45 +000050 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenek25a484d2008-02-14 18:28:23 +000051
Zhongxing Xu097fc982008-10-17 05:57:07 +000052 llvm::APSInt V = cast<nonloc::ConcreteInt>(X).getValue();
53 V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T));
Ted Kremenekce3ed1e2008-03-12 01:21:45 +000054 V.extOrTrunc(Eng.getContext().getTypeSize(T));
Ted Kremenek25a484d2008-02-14 18:28:23 +000055
Zhongxing Xu097fc982008-10-17 05:57:07 +000056 if (isLocType)
57 return loc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenek25a484d2008-02-14 18:28:23 +000058 else
Zhongxing Xu097fc982008-10-17 05:57:07 +000059 return nonloc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenek25a484d2008-02-14 18:28:23 +000060}
61
62// Casts.
63
Zhongxing Xu097fc982008-10-17 05:57:07 +000064SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, Loc X, QualType T) {
Ted Kremenekbc965a62008-02-18 22:57:02 +000065
Ted Kremenek31943062008-04-30 21:10:19 +000066 // Casts from pointers -> pointers, just return the lval.
67 //
68 // Casts from pointers -> references, just return the lval. These
69 // can be introduced by the frontend for corner cases, e.g
70 // casting from va_list* to __builtin_va_list&.
71 //
Ted Kremenek61b89eb2008-11-15 00:20:05 +000072 assert (!X.isUnknownOrUndef());
73
Zhongxing Xu097fc982008-10-17 05:57:07 +000074 if (Loc::IsLocType(T) || T->isReferenceType())
Ted Kremenek25a484d2008-02-14 18:28:23 +000075 return X;
76
Ted Kremenek2530b9e2009-02-10 05:42:58 +000077 // FIXME: Handle transparent unions where a value can be "transparently"
78 // lifted into a union type.
79 if (T->isUnionType())
80 return UnknownVal();
81
Ted Kremenek9cfda3f2008-02-21 18:43:30 +000082 assert (T->isIntegerType());
Ted Kremenekce3ed1e2008-03-12 01:21:45 +000083 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenek61b89eb2008-11-15 00:20:05 +000084 unsigned BitWidth = Eng.getContext().getTypeSize(T);
85
86 if (!isa<loc::ConcreteInt>(X))
87 return nonloc::LocAsInteger::Make(BasicVals, X, BitWidth);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +000088
Zhongxing Xu097fc982008-10-17 05:57:07 +000089 llvm::APSInt V = cast<loc::ConcreteInt>(X).getValue();
90 V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T));
Ted Kremenek61b89eb2008-11-15 00:20:05 +000091 V.extOrTrunc(BitWidth);
Zhongxing Xu097fc982008-10-17 05:57:07 +000092 return nonloc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenekc9922fd2008-02-14 18:40:24 +000093}
94
95// Unary operators.
96
Zhongxing Xu097fc982008-10-17 05:57:07 +000097SVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLoc X){
Ted Kremenekbc965a62008-02-18 22:57:02 +000098
Ted Kremenekc9922fd2008-02-14 18:40:24 +000099 switch (X.getSubKind()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000100
Zhongxing Xu097fc982008-10-17 05:57:07 +0000101 case nonloc::ConcreteIntKind:
102 return cast<nonloc::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U);
Ted Kremenek07baa252008-02-21 18:02:17 +0000103
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000104 default:
Ted Kremenek07baa252008-02-21 18:02:17 +0000105 return UnknownVal();
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000106 }
107}
108
Zhongxing Xu097fc982008-10-17 05:57:07 +0000109SVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLoc X) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000110
Ted Kremenek0cd96352008-02-20 04:12:31 +0000111 switch (X.getSubKind()) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000112
Zhongxing Xu097fc982008-10-17 05:57:07 +0000113 case nonloc::ConcreteIntKind:
114 return cast<nonloc::ConcreteInt>(X).EvalComplement(Eng.getBasicVals());
Ted Kremenek07baa252008-02-21 18:02:17 +0000115
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000116 default:
Ted Kremenek07baa252008-02-21 18:02:17 +0000117 return UnknownVal();
Ted Kremenekc9922fd2008-02-14 18:40:24 +0000118 }
119}
Ted Kremenekb1934132008-02-14 19:37:24 +0000120
121// Binary operators.
122
Ted Kremenek19c54072008-07-18 15:46:06 +0000123static unsigned char LNotOpMap[] = {
124 (unsigned char) BinaryOperator::GE, /* LT => GE */
125 (unsigned char) BinaryOperator::LE, /* GT => LE */
126 (unsigned char) BinaryOperator::GT, /* LE => GT */
127 (unsigned char) BinaryOperator::LT, /* GE => LT */
128 (unsigned char) BinaryOperator::NE, /* EQ => NE */
129 (unsigned char) BinaryOperator::EQ /* NE => EQ */
130};
131
Ted Kremenek61b89eb2008-11-15 00:20:05 +0000132SVal GRSimpleVals::DetermEvalBinOpNN(GRExprEngine& Eng,
Ted Kremenek589f8812008-07-18 15:27:58 +0000133 BinaryOperator::Opcode Op,
Ted Kremenek74556a12009-03-26 03:35:11 +0000134 NonLoc L, NonLoc R,
135 QualType T) {
Ted Kremenek90a94ec2008-09-19 17:31:13 +0000136
Ted Kremenek61b89eb2008-11-15 00:20:05 +0000137 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenek90a94ec2008-09-19 17:31:13 +0000138 unsigned subkind = L.getSubKind();
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000139
Ted Kremenek07baa252008-02-21 18:02:17 +0000140 while (1) {
Ted Kremenekb1934132008-02-14 19:37:24 +0000141
Ted Kremenek90a94ec2008-09-19 17:31:13 +0000142 switch (subkind) {
Ted Kremenekb1934132008-02-14 19:37:24 +0000143 default:
Ted Kremenekde89ec02008-02-21 19:10:12 +0000144 return UnknownVal();
Ted Kremenekb1934132008-02-14 19:37:24 +0000145
Ted Kremenek61b89eb2008-11-15 00:20:05 +0000146 case nonloc::LocAsIntegerKind: {
147 Loc LL = cast<nonloc::LocAsInteger>(L).getLoc();
148
149 switch (R.getSubKind()) {
150 case nonloc::LocAsIntegerKind:
151 return EvalBinOp(Eng, Op, LL,
152 cast<nonloc::LocAsInteger>(R).getLoc());
153
154 case nonloc::ConcreteIntKind: {
155 // Transform the integer into a location and compare.
156 ASTContext& Ctx = Eng.getContext();
157 llvm::APSInt V = cast<nonloc::ConcreteInt>(R).getValue();
158 V.setIsUnsigned(true);
159 V.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
160 return EvalBinOp(Eng, Op, LL,
161 loc::ConcreteInt(BasicVals.getValue(V)));
162 }
163
164 default:
165 switch (Op) {
166 case BinaryOperator::EQ:
167 return NonLoc::MakeIntTruthVal(BasicVals, false);
168 case BinaryOperator::NE:
169 return NonLoc::MakeIntTruthVal(BasicVals, true);
170 default:
171 // This case also handles pointer arithmetic.
172 return UnknownVal();
173 }
174 }
175 }
176
Ted Kremenek74556a12009-03-26 03:35:11 +0000177 case nonloc::SymExprValKind: {
Ted Kremenek99b4f432008-07-18 15:54:51 +0000178 // Logical not?
179 if (!(Op == BinaryOperator::EQ && R.isZeroConstant()))
180 return UnknownVal();
Ted Kremenek19c54072008-07-18 15:46:06 +0000181
Ted Kremenek74556a12009-03-26 03:35:11 +0000182 const SymExpr &SE=*cast<nonloc::SymExprVal>(L).getSymbolicExpression();
Ted Kremenek19c54072008-07-18 15:46:06 +0000183
Ted Kremenek74556a12009-03-26 03:35:11 +0000184 // Only handle ($sym op constant) for now.
185 if (const SymIntExpr *E = dyn_cast<SymIntExpr>(&SE)) {
186 BinaryOperator::Opcode Opc = E->getOpcode();
Ted Kremenek19c54072008-07-18 15:46:06 +0000187
Ted Kremenek74556a12009-03-26 03:35:11 +0000188 if (Opc < BinaryOperator::LT || Opc > BinaryOperator::NE)
189 return UnknownVal();
190
191 // For comparison operators, translate the constraint by
192 // changing the opcode.
193 int idx = (unsigned) Opc - (unsigned) BinaryOperator::LT;
Ted Kremenek19c54072008-07-18 15:46:06 +0000194
Ted Kremenek74556a12009-03-26 03:35:11 +0000195 assert (idx >= 0 &&
196 (unsigned) idx < sizeof(LNotOpMap)/sizeof(unsigned char));
Ted Kremenek19c54072008-07-18 15:46:06 +0000197
Ted Kremenek74556a12009-03-26 03:35:11 +0000198 Opc = (BinaryOperator::Opcode) LNotOpMap[idx];
199 assert(E->getType(Eng.getContext()) == T);
200 E = Eng.getSymbolManager().getSymIntExpr(E->getLHS(), Opc,
201 E->getRHS(), T);
202 return nonloc::SymExprVal(E);
203 }
Ted Kremenek19c54072008-07-18 15:46:06 +0000204
Ted Kremenek74556a12009-03-26 03:35:11 +0000205 return UnknownVal();
Ted Kremenek19c54072008-07-18 15:46:06 +0000206 }
207
Zhongxing Xu097fc982008-10-17 05:57:07 +0000208 case nonloc::ConcreteIntKind:
Ted Kremenekb1934132008-02-14 19:37:24 +0000209
Zhongxing Xu097fc982008-10-17 05:57:07 +0000210 if (isa<nonloc::ConcreteInt>(R)) {
211 const nonloc::ConcreteInt& L_CI = cast<nonloc::ConcreteInt>(L);
212 const nonloc::ConcreteInt& R_CI = cast<nonloc::ConcreteInt>(R);
Ted Kremenek8ad19872008-03-07 20:13:31 +0000213 return L_CI.EvalBinOp(BasicVals, Op, R_CI);
Ted Kremenekb1934132008-02-14 19:37:24 +0000214 }
Ted Kremenekb1934132008-02-14 19:37:24 +0000215 else {
Ted Kremenek90a94ec2008-09-19 17:31:13 +0000216 subkind = R.getSubKind();
Zhongxing Xu097fc982008-10-17 05:57:07 +0000217 NonLoc tmp = R;
Ted Kremenek07baa252008-02-21 18:02:17 +0000218 R = L;
219 L = tmp;
Ted Kremenek90a94ec2008-09-19 17:31:13 +0000220
221 // Swap the operators.
222 switch (Op) {
223 case BinaryOperator::LT: Op = BinaryOperator::GT; break;
224 case BinaryOperator::GT: Op = BinaryOperator::LT; break;
225 case BinaryOperator::LE: Op = BinaryOperator::GE; break;
226 case BinaryOperator::GE: Op = BinaryOperator::LE; break;
227 default: break;
228 }
229
Ted Kremenekb1934132008-02-14 19:37:24 +0000230 continue;
231 }
232
Zhongxing Xu097fc982008-10-17 05:57:07 +0000233 case nonloc::SymbolValKind:
Ted Kremenek84800be2009-04-10 18:11:44 +0000234 if (isa<nonloc::ConcreteInt>(R)) {
235 ValueManager &ValMgr = Eng.getValueManager();
236 return ValMgr.makeNonLoc(cast<nonloc::SymbolVal>(L).getSymbol(), Op,
237 cast<nonloc::ConcreteInt>(R).getValue(), T);
238 }
Ted Kremenekb1934132008-02-14 19:37:24 +0000239 else
Ted Kremenek07baa252008-02-21 18:02:17 +0000240 return UnknownVal();
Ted Kremenekb1934132008-02-14 19:37:24 +0000241 }
242 }
243}
244
Ted Kremenek775e0d82008-02-15 00:52:26 +0000245
Ted Kremenek521d9722008-02-15 23:15:23 +0000246// Binary Operators (except assignments and comma).
247
Zhongxing Xu097fc982008-10-17 05:57:07 +0000248SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
249 Loc L, Loc R) {
Ted Kremenekbc965a62008-02-18 22:57:02 +0000250
Ted Kremenek521d9722008-02-15 23:15:23 +0000251 switch (Op) {
Ted Kremenek07baa252008-02-21 18:02:17 +0000252
Ted Kremenek521d9722008-02-15 23:15:23 +0000253 default:
254 return UnknownVal();
255
256 case BinaryOperator::EQ:
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000257 return EvalEQ(Eng, L, R);
Ted Kremenek521d9722008-02-15 23:15:23 +0000258
259 case BinaryOperator::NE:
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000260 return EvalNE(Eng, L, R);
Ted Kremenek521d9722008-02-15 23:15:23 +0000261 }
262}
263
Zhongxing Xu097fc982008-10-17 05:57:07 +0000264SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
Ted Kremenek4e76b982009-04-29 16:03:27 +0000265 Loc L, NonLoc R) {
266
267 // Special case: 'R' is an integer that has the same width as a pointer and
268 // we are using the integer location in a comparison. Normally this cannot be
269 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
270 // can generate comparisons that trigger this code.
271 // FIXME: Are all locations guaranteed to have pointer width?
272 if (BinaryOperator::isEqualityOp(Op)) {
273 if (nonloc::ConcreteInt *RInt = dyn_cast<nonloc::ConcreteInt>(&R)) {
274 const llvm::APSInt &X = RInt->getValue();
275 ASTContext &C = Eng.getContext();
276 if (C.getTypeSize(C.VoidPtrTy) == X.getBitWidth())
277 return EvalBinOp(Eng, Op, L, loc::ConcreteInt(X));
278 }
279 }
280
Zhongxing Xu7ab4b4c2009-03-02 07:52:23 +0000281 // Delegate pointer arithmetic to store manager.
282 return Eng.getStoreManager().EvalBinOp(Op, L, R);
Ted Kremenek775e0d82008-02-15 00:52:26 +0000283}
284
Zhongxing Xu75f459c2009-04-09 07:39:46 +0000285// Equality operators for Locs.
286// FIXME: All this logic will be revamped when we have MemRegion::getLocation()
287// implemented.
Ted Kremenekb1934132008-02-14 19:37:24 +0000288
Zhongxing Xu097fc982008-10-17 05:57:07 +0000289SVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, Loc L, Loc R) {
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000290
291 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenek9a478822009-03-09 20:35:15 +0000292
Ted Kremenek07baa252008-02-21 18:02:17 +0000293 switch (L.getSubKind()) {
Ted Kremenekb1934132008-02-14 19:37:24 +0000294
Ted Kremenekb1934132008-02-14 19:37:24 +0000295 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000296 assert(false && "EQ not implemented for this Loc.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000297 return UnknownVal();
Ted Kremenekb1934132008-02-14 19:37:24 +0000298
Zhongxing Xu097fc982008-10-17 05:57:07 +0000299 case loc::ConcreteIntKind:
Ted Kremenek07baa252008-02-21 18:02:17 +0000300
Zhongxing Xu097fc982008-10-17 05:57:07 +0000301 if (isa<loc::ConcreteInt>(R)) {
302 bool b = cast<loc::ConcreteInt>(L).getValue() ==
303 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenekb1934132008-02-14 19:37:24 +0000304
Zhongxing Xu097fc982008-10-17 05:57:07 +0000305 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenekb1934132008-02-14 19:37:24 +0000306 }
Ted Kremenekb1934132008-02-14 19:37:24 +0000307
308 break;
309
Ted Kremenek9a478822009-03-09 20:35:15 +0000310 case loc::MemRegionKind: {
Zhongxing Xu75f459c2009-04-09 07:39:46 +0000311 if (SymbolRef LSym = L.getAsLocSymbol()) {
312 if (isa<loc::ConcreteInt>(R)) {
313 const SymIntExpr *SE =
314 Eng.getSymbolManager().getSymIntExpr(LSym, BinaryOperator::EQ,
315 cast<loc::ConcreteInt>(R).getValue(),
316 Eng.getContext().IntTy);
317
318 return nonloc::SymExprVal(SE);
319 }
Ted Kremenek9a478822009-03-09 20:35:15 +0000320 }
321 }
322
323 // Fall-through.
324
Zhongxing Xu097fc982008-10-17 05:57:07 +0000325 case loc::GotoLabelKind:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000326 return NonLoc::MakeIntTruthVal(BasicVals, L == R);
Ted Kremenekb1934132008-02-14 19:37:24 +0000327 }
328
Zhongxing Xu097fc982008-10-17 05:57:07 +0000329 return NonLoc::MakeIntTruthVal(BasicVals, false);
Ted Kremenekb1934132008-02-14 19:37:24 +0000330}
331
Zhongxing Xu097fc982008-10-17 05:57:07 +0000332SVal GRSimpleVals::EvalNE(GRExprEngine& Eng, Loc L, Loc R) {
Ted Kremenekbc965a62008-02-18 22:57:02 +0000333
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000334 BasicValueFactory& BasicVals = Eng.getBasicVals();
335
Ted Kremenek07baa252008-02-21 18:02:17 +0000336 switch (L.getSubKind()) {
Ted Kremenekb1934132008-02-14 19:37:24 +0000337
Ted Kremenekb1934132008-02-14 19:37:24 +0000338 default:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000339 assert(false && "NE not implemented for this Loc.");
Ted Kremenek07baa252008-02-21 18:02:17 +0000340 return UnknownVal();
Ted Kremenekb1934132008-02-14 19:37:24 +0000341
Zhongxing Xu097fc982008-10-17 05:57:07 +0000342 case loc::ConcreteIntKind:
Ted Kremenek07baa252008-02-21 18:02:17 +0000343
Zhongxing Xu097fc982008-10-17 05:57:07 +0000344 if (isa<loc::ConcreteInt>(R)) {
345 bool b = cast<loc::ConcreteInt>(L).getValue() !=
346 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenekb1934132008-02-14 19:37:24 +0000347
Zhongxing Xu097fc982008-10-17 05:57:07 +0000348 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenekb1934132008-02-14 19:37:24 +0000349 }
Ted Kremenek1efcc4a2009-03-28 19:59:33 +0000350 else if (SymbolRef Sym = R.getAsSymbol()) {
Ted Kremenek74556a12009-03-26 03:35:11 +0000351 const SymIntExpr * SE =
Ted Kremenek1efcc4a2009-03-28 19:59:33 +0000352 Eng.getSymbolManager().getSymIntExpr(Sym, BinaryOperator::NE,
353 cast<loc::ConcreteInt>(L).getValue(),
354 Eng.getContext().IntTy);
Ted Kremenek74556a12009-03-26 03:35:11 +0000355 return nonloc::SymExprVal(SE);
Ted Kremenekb1934132008-02-14 19:37:24 +0000356 }
357
358 break;
Ted Kremenek9a478822009-03-09 20:35:15 +0000359
Ted Kremenek9a478822009-03-09 20:35:15 +0000360 case loc::MemRegionKind: {
Zhongxing Xu75f459c2009-04-09 07:39:46 +0000361 if (SymbolRef LSym = L.getAsLocSymbol()) {
362 if (isa<loc::ConcreteInt>(R)) {
363 const SymIntExpr* SE =
364 Eng.getSymbolManager().getSymIntExpr(LSym, BinaryOperator::NE,
365 cast<loc::ConcreteInt>(R).getValue(),
366 Eng.getContext().IntTy);
367 return nonloc::SymExprVal(SE);
368 }
Ted Kremenek9a478822009-03-09 20:35:15 +0000369 }
Zhongxing Xu75f459c2009-04-09 07:39:46 +0000370 // Fall through:
Ted Kremenek9a478822009-03-09 20:35:15 +0000371 }
372
Zhongxing Xu097fc982008-10-17 05:57:07 +0000373 case loc::GotoLabelKind:
Zhongxing Xu097fc982008-10-17 05:57:07 +0000374 return NonLoc::MakeIntTruthVal(BasicVals, L != R);
Ted Kremenekb1934132008-02-14 19:37:24 +0000375 }
376
Zhongxing Xu097fc982008-10-17 05:57:07 +0000377 return NonLoc::MakeIntTruthVal(BasicVals, true);
Ted Kremenekb1934132008-02-14 19:37:24 +0000378}
Ted Kremenek348d7852008-02-26 23:04:29 +0000379
380//===----------------------------------------------------------------------===//
Ted Kremenekca5f6202008-04-15 23:06:53 +0000381// Transfer function for function calls.
Ted Kremenek348d7852008-02-26 23:04:29 +0000382//===----------------------------------------------------------------------===//
383
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000384void GRSimpleVals::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000385 GRExprEngine& Eng,
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000386 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu097fc982008-10-17 05:57:07 +0000387 CallExpr* CE, SVal L,
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000388 ExplodedNode<GRState>* Pred) {
Ted Kremenek3eea8dd2008-03-05 00:33:14 +0000389
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000390 GRStateManager& StateMgr = Eng.getStateManager();
391 const GRState* St = Builder.GetState(Pred);
Ted Kremenek348d7852008-02-26 23:04:29 +0000392
Zhongxing Xu097fc982008-10-17 05:57:07 +0000393 // Invalidate all arguments passed in by reference (Locs).
Ted Kremenek348d7852008-02-26 23:04:29 +0000394
395 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
396 I != E; ++I) {
397
Zhongxing Xu097fc982008-10-17 05:57:07 +0000398 SVal V = StateMgr.GetSVal(St, *I);
Ted Kremenek348d7852008-02-26 23:04:29 +0000399
Zhongxing Xue8fc4d62008-10-27 09:00:08 +0000400 if (isa<loc::MemRegionVal>(V))
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000401 St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal());
Zhongxing Xu097fc982008-10-17 05:57:07 +0000402 else if (isa<nonloc::LocAsInteger>(V))
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000403 St = StateMgr.BindLoc(St, cast<nonloc::LocAsInteger>(V).getLoc(),
Ted Kremenekbe621292008-04-22 21:39:21 +0000404 UnknownVal());
405
Ted Kremenek348d7852008-02-26 23:04:29 +0000406 }
Ted Kremenek1a531942008-03-12 21:04:07 +0000407
Ted Kremenek8f90e712008-10-17 22:23:12 +0000408 // Make up a symbol for the return value of this function.
409 // FIXME: We eventually should handle structs and other compound types
410 // that are returned by value.
411 QualType T = CE->getType();
Ted Kremenek79413a52008-11-13 06:10:40 +0000412 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenek1a531942008-03-12 21:04:07 +0000413 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremeneke4cb3c82009-04-09 22:22:44 +0000414 SVal X = Eng.getValueManager().getConjuredSymbolVal(CE, Count);
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000415 St = StateMgr.BindExpr(St, CE, X, Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenek1a531942008-03-12 21:04:07 +0000416 }
Ted Kremenek3eea8dd2008-03-05 00:33:14 +0000417
Ted Kremenekf10f2882008-03-21 21:30:14 +0000418 Builder.MakeNode(Dst, CE, Pred, St);
Ted Kremenek348d7852008-02-26 23:04:29 +0000419}
Ted Kremenekca5f6202008-04-15 23:06:53 +0000420
421//===----------------------------------------------------------------------===//
422// Transfer function for Objective-C message expressions.
423//===----------------------------------------------------------------------===//
424
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000425void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekca5f6202008-04-15 23:06:53 +0000426 GRExprEngine& Eng,
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000427 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekca5f6202008-04-15 23:06:53 +0000428 ObjCMessageExpr* ME,
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000429 ExplodedNode<GRState>* Pred) {
Ted Kremenekca5f6202008-04-15 23:06:53 +0000430
431
432 // The basic transfer function logic for message expressions does nothing.
433 // We just invalidate all arguments passed in by references.
434
Ted Kremenekabd89ac2008-08-13 04:27:00 +0000435 GRStateManager& StateMgr = Eng.getStateManager();
436 const GRState* St = Builder.GetState(Pred);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000437
438 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
439 I != E; ++I) {
440
Zhongxing Xu097fc982008-10-17 05:57:07 +0000441 SVal V = StateMgr.GetSVal(St, *I);
Ted Kremenekca5f6202008-04-15 23:06:53 +0000442
Zhongxing Xu097fc982008-10-17 05:57:07 +0000443 if (isa<Loc>(V))
Zhongxing Xu696b3a82008-10-30 05:33:54 +0000444 St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal());
Ted Kremenekca5f6202008-04-15 23:06:53 +0000445 }
446
447 Builder.MakeNode(Dst, ME, Pred, St);
448}