blob: 783bccc7d73c86e1fffea9961f2afd5ed3532d00 [file] [log] [blame]
Ted Kremenekd59cccc2008-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 Greif843e9342008-03-06 10:40:09 +000010// This file defines GRSimpleVals, a sub-class of GRTransferFuncs that
Ted Kremenekd59cccc2008-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 Kremenek52755612008-03-27 17:17:22 +000017#include "BasicObjCFoundationChecks.h"
Ted Kremenek87abc032008-04-02 22:03:53 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000019#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek4adc81e2008-08-13 04:27:00 +000020#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000021#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenekd71ed262008-04-10 22:16:52 +000022#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekc0c3f5d2008-04-30 20:17:27 +000023#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek61f3e052008-04-03 04:42:52 +000024#include "llvm/Support/Compiler.h"
Ted Kremenek5c061212008-02-27 17:56:16 +000025#include <sstream>
Ted Kremenekd59cccc2008-02-14 18:28:23 +000026
27using namespace clang;
28
Ted Kremenekdd598112008-04-02 07:05:46 +000029//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +000030// Transfer Function creation for External clients.
Ted Kremenek503d6132008-04-02 05:15:22 +000031//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +000032
Ted Kremenekd71ed262008-04-10 22:16:52 +000033GRTransferFuncs* clang::MakeGRSimpleValsTF() { return new GRSimpleVals(); }
Ted Kremeneke01c9872008-02-14 22:36:46 +000034
Ted Kremenekd59cccc2008-02-14 18:28:23 +000035//===----------------------------------------------------------------------===//
36// Transfer function for Casts.
37//===----------------------------------------------------------------------===//
38
Zhongxing Xu1c96b242008-10-17 05:57:07 +000039SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLoc X, QualType T) {
Ted Kremenek692416c2008-02-18 22:57:02 +000040
Zhongxing Xu1c96b242008-10-17 05:57:07 +000041 if (!isa<nonloc::ConcreteInt>(X))
Ted Kremenekd59cccc2008-02-14 18:28:23 +000042 return UnknownVal();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +000043
Zhongxing Xu1c96b242008-10-17 05:57:07 +000044 bool isLocType = Loc::IsLocType(T);
Ted Kremenekcd512dc2008-07-16 00:23:49 +000045
Ted Kremenekf496ee12008-07-15 23:17:54 +000046 // Only handle casts from integers to integers.
Zhongxing Xu1c96b242008-10-17 05:57:07 +000047 if (!isLocType && !T->isIntegerType())
Ted Kremenekf496ee12008-07-15 23:17:54 +000048 return UnknownVal();
49
Ted Kremenek00a3a5f2008-03-12 01:21:45 +000050 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenekd59cccc2008-02-14 18:28:23 +000051
Zhongxing Xu1c96b242008-10-17 05:57:07 +000052 llvm::APSInt V = cast<nonloc::ConcreteInt>(X).getValue();
53 V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +000054 V.extOrTrunc(Eng.getContext().getTypeSize(T));
Ted Kremenekd59cccc2008-02-14 18:28:23 +000055
Zhongxing Xu1c96b242008-10-17 05:57:07 +000056 if (isLocType)
57 return loc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenekd59cccc2008-02-14 18:28:23 +000058 else
Zhongxing Xu1c96b242008-10-17 05:57:07 +000059 return nonloc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenekd59cccc2008-02-14 18:28:23 +000060}
61
62// Casts.
63
Zhongxing Xu1c96b242008-10-17 05:57:07 +000064SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, Loc X, QualType T) {
Ted Kremenek692416c2008-02-18 22:57:02 +000065
Ted Kremeneke8c2bde2008-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 Kremeneke04a5cb2008-11-15 00:20:05 +000072 assert (!X.isUnknownOrUndef());
73
Zhongxing Xu1c96b242008-10-17 05:57:07 +000074 if (Loc::IsLocType(T) || T->isReferenceType())
Ted Kremenekd59cccc2008-02-14 18:28:23 +000075 return X;
76
Ted Kremenek9ef1ec92008-02-21 18:43:30 +000077 assert (T->isIntegerType());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +000078 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremeneke04a5cb2008-11-15 00:20:05 +000079 unsigned BitWidth = Eng.getContext().getTypeSize(T);
80
81 if (!isa<loc::ConcreteInt>(X))
82 return nonloc::LocAsInteger::Make(BasicVals, X, BitWidth);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +000083
Zhongxing Xu1c96b242008-10-17 05:57:07 +000084 llvm::APSInt V = cast<loc::ConcreteInt>(X).getValue();
85 V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +000086 V.extOrTrunc(BitWidth);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000087 return nonloc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenekc3f261d2008-02-14 18:40:24 +000088}
89
90// Unary operators.
91
Zhongxing Xu1c96b242008-10-17 05:57:07 +000092SVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLoc X){
Ted Kremenek692416c2008-02-18 22:57:02 +000093
Ted Kremenekc3f261d2008-02-14 18:40:24 +000094 switch (X.getSubKind()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000095
Zhongxing Xu1c96b242008-10-17 05:57:07 +000096 case nonloc::ConcreteIntKind:
97 return cast<nonloc::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000098
Ted Kremenekc3f261d2008-02-14 18:40:24 +000099 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000100 return UnknownVal();
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000101 }
102}
103
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000104SVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLoc X) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000105
Ted Kremenek90e42032008-02-20 04:12:31 +0000106 switch (X.getSubKind()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000107
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000108 case nonloc::ConcreteIntKind:
109 return cast<nonloc::ConcreteInt>(X).EvalComplement(Eng.getBasicVals());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000110
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000111 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000112 return UnknownVal();
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000113 }
114}
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000115
116// Binary operators.
117
Ted Kremenek1e38f852008-07-18 15:46:06 +0000118static unsigned char LNotOpMap[] = {
119 (unsigned char) BinaryOperator::GE, /* LT => GE */
120 (unsigned char) BinaryOperator::LE, /* GT => LE */
121 (unsigned char) BinaryOperator::GT, /* LE => GT */
122 (unsigned char) BinaryOperator::LT, /* GE => LT */
123 (unsigned char) BinaryOperator::NE, /* EQ => NE */
124 (unsigned char) BinaryOperator::EQ /* NE => EQ */
125};
126
Ted Kremeneke04a5cb2008-11-15 00:20:05 +0000127SVal GRSimpleVals::DetermEvalBinOpNN(GRExprEngine& Eng,
Ted Kremenekad8329e2008-07-18 15:27:58 +0000128 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000129 NonLoc L, NonLoc R) {
Ted Kremenek26758752008-09-19 17:31:13 +0000130
Ted Kremeneke04a5cb2008-11-15 00:20:05 +0000131 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenek26758752008-09-19 17:31:13 +0000132 unsigned subkind = L.getSubKind();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000133
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000134 while (1) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000135
Ted Kremenek26758752008-09-19 17:31:13 +0000136 switch (subkind) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000137 default:
Ted Kremenek9258a642008-02-21 19:10:12 +0000138 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000139
Ted Kremeneke04a5cb2008-11-15 00:20:05 +0000140 case nonloc::LocAsIntegerKind: {
141 Loc LL = cast<nonloc::LocAsInteger>(L).getLoc();
142
143 switch (R.getSubKind()) {
144 case nonloc::LocAsIntegerKind:
145 return EvalBinOp(Eng, Op, LL,
146 cast<nonloc::LocAsInteger>(R).getLoc());
147
148 case nonloc::ConcreteIntKind: {
149 // Transform the integer into a location and compare.
150 ASTContext& Ctx = Eng.getContext();
151 llvm::APSInt V = cast<nonloc::ConcreteInt>(R).getValue();
152 V.setIsUnsigned(true);
153 V.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
154 return EvalBinOp(Eng, Op, LL,
155 loc::ConcreteInt(BasicVals.getValue(V)));
156 }
157
158 default:
159 switch (Op) {
160 case BinaryOperator::EQ:
161 return NonLoc::MakeIntTruthVal(BasicVals, false);
162 case BinaryOperator::NE:
163 return NonLoc::MakeIntTruthVal(BasicVals, true);
164 default:
165 // This case also handles pointer arithmetic.
166 return UnknownVal();
167 }
168 }
169 }
170
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000171 case nonloc::SymIntConstraintValKind: {
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000172
173 // Logical not?
174 if (!(Op == BinaryOperator::EQ && R.isZeroConstant()))
175 return UnknownVal();
176
Ted Kremenek1e38f852008-07-18 15:46:06 +0000177 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000178 cast<nonloc::SymIntConstraintVal>(L).getConstraint();
Ted Kremenek1e38f852008-07-18 15:46:06 +0000179
180 BinaryOperator::Opcode Opc = C.getOpcode();
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000181
Ted Kremenek1e38f852008-07-18 15:46:06 +0000182 if (Opc < BinaryOperator::LT || Opc > BinaryOperator::NE)
183 return UnknownVal();
184
185 // For comparison operators, translate the constraint by
186 // changing the opcode.
187
188 int idx = (unsigned) Opc - (unsigned) BinaryOperator::LT;
189
190 assert (idx >= 0 &&
191 (unsigned) idx < sizeof(LNotOpMap)/sizeof(unsigned char));
192
193 Opc = (BinaryOperator::Opcode) LNotOpMap[idx];
194
195 const SymIntConstraint& CNew =
196 BasicVals.getConstraint(C.getSymbol(), Opc, C.getInt());
197
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000198 return nonloc::SymIntConstraintVal(CNew);
Ted Kremenek1e38f852008-07-18 15:46:06 +0000199 }
200
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000201 case nonloc::ConcreteIntKind:
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000202
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000203 if (isa<nonloc::ConcreteInt>(R)) {
204 const nonloc::ConcreteInt& L_CI = cast<nonloc::ConcreteInt>(L);
205 const nonloc::ConcreteInt& R_CI = cast<nonloc::ConcreteInt>(R);
Ted Kremenek240f1f02008-03-07 20:13:31 +0000206 return L_CI.EvalBinOp(BasicVals, Op, R_CI);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000207 }
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000208 else {
Ted Kremenek26758752008-09-19 17:31:13 +0000209 subkind = R.getSubKind();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000210 NonLoc tmp = R;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000211 R = L;
212 L = tmp;
Ted Kremenek26758752008-09-19 17:31:13 +0000213
214 // Swap the operators.
215 switch (Op) {
216 case BinaryOperator::LT: Op = BinaryOperator::GT; break;
217 case BinaryOperator::GT: Op = BinaryOperator::LT; break;
218 case BinaryOperator::LE: Op = BinaryOperator::GE; break;
219 case BinaryOperator::GE: Op = BinaryOperator::LE; break;
220 default: break;
221 }
222
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000223 continue;
224 }
225
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000226 case nonloc::SymbolValKind:
227 if (isa<nonloc::ConcreteInt>(R)) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000228 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000229 BasicVals.getConstraint(cast<nonloc::SymbolVal>(L).getSymbol(), Op,
230 cast<nonloc::ConcreteInt>(R).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000231
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000232 return nonloc::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000233 }
234 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000235 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000236 }
237 }
238}
239
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000240
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000241// Binary Operators (except assignments and comma).
242
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000243SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
244 Loc L, Loc R) {
Ted Kremenek692416c2008-02-18 22:57:02 +0000245
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000246 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000247
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000248 default:
249 return UnknownVal();
250
251 case BinaryOperator::EQ:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000252 return EvalEQ(Eng, L, R);
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000253
254 case BinaryOperator::NE:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000255 return EvalNE(Eng, L, R);
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000256 }
257}
258
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000259// Pointer arithmetic.
260
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000261SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
262 Loc L, NonLoc R) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000263 return UnknownVal();
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000264}
265
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000266// Equality operators for Locs.
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000267
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000268SVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, Loc L, Loc R) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000269
270 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000271
272 switch (L.getSubKind()) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000273
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000274 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000275 assert(false && "EQ not implemented for this Loc.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000276 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000277
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000278 case loc::ConcreteIntKind:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000279
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000280 if (isa<loc::ConcreteInt>(R)) {
281 bool b = cast<loc::ConcreteInt>(L).getValue() ==
282 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000283
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000284 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000285 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000286 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000287
288 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000289 BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000290 BinaryOperator::EQ,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000291 cast<loc::ConcreteInt>(L).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000292
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000293 return nonloc::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000294 }
295
296 break;
297
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000298 case loc::SymbolValKind: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000299
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000300 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000301 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000302 BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(),
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000303 BinaryOperator::EQ,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000304 cast<loc::ConcreteInt>(R).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000305
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000306 return nonloc::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000307 }
308
Ted Kremenekf700df22008-02-22 18:41:59 +0000309 // FIXME: Implement == for lval Symbols. This is mainly useful
310 // in iterator loops when traversing a buffer, e.g. while(z != zTerm).
311 // Since this is not useful for many checkers we'll punt on this for
312 // now.
313
314 return UnknownVal();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000315 }
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000316
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000317 case loc::MemRegionKind:
318 case loc::FuncValKind:
319 case loc::GotoLabelKind:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000320 return NonLoc::MakeIntTruthVal(BasicVals, L == R);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000321 }
322
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000323 return NonLoc::MakeIntTruthVal(BasicVals, false);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000324}
325
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000326SVal GRSimpleVals::EvalNE(GRExprEngine& Eng, Loc L, Loc R) {
Ted Kremenek692416c2008-02-18 22:57:02 +0000327
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000328 BasicValueFactory& BasicVals = Eng.getBasicVals();
329
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000330 switch (L.getSubKind()) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000331
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000332 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000333 assert(false && "NE not implemented for this Loc.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000334 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000335
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000336 case loc::ConcreteIntKind:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000337
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000338 if (isa<loc::ConcreteInt>(R)) {
339 bool b = cast<loc::ConcreteInt>(L).getValue() !=
340 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000341
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000342 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000343 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000344 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000345 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000346 BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000347 BinaryOperator::NE,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000348 cast<loc::ConcreteInt>(L).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000349
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000350 return nonloc::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000351 }
352
353 break;
354
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000355 case loc::SymbolValKind: {
356 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000357 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000358 BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(),
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000359 BinaryOperator::NE,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000360 cast<loc::ConcreteInt>(R).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000361
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000362 return nonloc::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000363 }
364
Ted Kremenekf700df22008-02-22 18:41:59 +0000365 // FIXME: Implement != for lval Symbols. This is mainly useful
366 // in iterator loops when traversing a buffer, e.g. while(z != zTerm).
367 // Since this is not useful for many checkers we'll punt on this for
368 // now.
369
370 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000371
372 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000373 }
374
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000375 case loc::MemRegionKind:
376 case loc::FuncValKind:
377 case loc::GotoLabelKind:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000378 return NonLoc::MakeIntTruthVal(BasicVals, L != R);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000379 }
380
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000381 return NonLoc::MakeIntTruthVal(BasicVals, true);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000382}
Ted Kremenek06747692008-02-26 23:04:29 +0000383
384//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000385// Transfer function for function calls.
Ted Kremenek06747692008-02-26 23:04:29 +0000386//===----------------------------------------------------------------------===//
387
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000388void GRSimpleVals::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000389 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000390 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000391 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000392 ExplodedNode<GRState>* Pred) {
Ted Kremenek330dddd2008-03-05 00:33:14 +0000393
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000394 GRStateManager& StateMgr = Eng.getStateManager();
395 const GRState* St = Builder.GetState(Pred);
Ted Kremenek06747692008-02-26 23:04:29 +0000396
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000397 // Invalidate all arguments passed in by reference (Locs).
Ted Kremenek06747692008-02-26 23:04:29 +0000398
399 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
400 I != E; ++I) {
401
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000402 SVal V = StateMgr.GetSVal(St, *I);
Ted Kremenek06747692008-02-26 23:04:29 +0000403
Zhongxing Xud03eea02008-10-27 09:00:08 +0000404 if (isa<loc::MemRegionVal>(V))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000405 St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal());
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000406 else if (isa<nonloc::LocAsInteger>(V))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000407 St = StateMgr.BindLoc(St, cast<nonloc::LocAsInteger>(V).getLoc(),
Ted Kremeneka5488462008-04-22 21:39:21 +0000408 UnknownVal());
409
Ted Kremenek06747692008-02-26 23:04:29 +0000410 }
Ted Kremenekf923a912008-03-12 21:04:07 +0000411
Ted Kremenekfd301942008-10-17 22:23:12 +0000412 // Make up a symbol for the return value of this function.
413 // FIXME: We eventually should handle structs and other compound types
414 // that are returned by value.
415 QualType T = CE->getType();
Ted Kremenek062e2f92008-11-13 06:10:40 +0000416 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenekf923a912008-03-12 21:04:07 +0000417 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000418 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
Ted Kremenekf923a912008-03-12 21:04:07 +0000419
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000420 SVal X = Loc::IsLocType(CE->getType())
421 ? cast<SVal>(loc::SymbolVal(Sym))
422 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenekf923a912008-03-12 21:04:07 +0000423
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000424 St = StateMgr.BindExpr(St, CE, X, Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenekf923a912008-03-12 21:04:07 +0000425 }
Ted Kremenek330dddd2008-03-05 00:33:14 +0000426
Ted Kremenek0e561a32008-03-21 21:30:14 +0000427 Builder.MakeNode(Dst, CE, Pred, St);
Ted Kremenek06747692008-02-26 23:04:29 +0000428}
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000429
430//===----------------------------------------------------------------------===//
431// Transfer function for Objective-C message expressions.
432//===----------------------------------------------------------------------===//
433
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000434void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000435 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000436 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000437 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000438 ExplodedNode<GRState>* Pred) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000439
440
441 // The basic transfer function logic for message expressions does nothing.
442 // We just invalidate all arguments passed in by references.
443
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000444 GRStateManager& StateMgr = Eng.getStateManager();
445 const GRState* St = Builder.GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000446
447 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
448 I != E; ++I) {
449
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000450 SVal V = StateMgr.GetSVal(St, *I);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000451
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000452 if (isa<Loc>(V))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000453 St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000454 }
455
456 Builder.MakeNode(Dst, ME, Pred, St);
457}