blob: 22ccd7b8aa9199e59ffa245fd8dbaaf28ceb9cc8 [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 Kremenek3f34d802009-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 Kremenek9ef1ec92008-02-21 18:43:30 +000082 assert (T->isIntegerType());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +000083 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremeneke04a5cb2008-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 Kremenek00a3a5f2008-03-12 01:21:45 +000088
Zhongxing Xu1c96b242008-10-17 05:57:07 +000089 llvm::APSInt V = cast<loc::ConcreteInt>(X).getValue();
90 V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T));
Ted Kremeneke04a5cb2008-11-15 00:20:05 +000091 V.extOrTrunc(BitWidth);
Zhongxing Xu1c96b242008-10-17 05:57:07 +000092 return nonloc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenekc3f261d2008-02-14 18:40:24 +000093}
94
95// Unary operators.
96
Zhongxing Xu1c96b242008-10-17 05:57:07 +000097SVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLoc X){
Ted Kremenek692416c2008-02-18 22:57:02 +000098
Ted Kremenekc3f261d2008-02-14 18:40:24 +000099 switch (X.getSubKind()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000100
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000101 case nonloc::ConcreteIntKind:
102 return cast<nonloc::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000103
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000104 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000105 return UnknownVal();
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000106 }
107}
108
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000109SVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLoc X) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000110
Ted Kremenek90e42032008-02-20 04:12:31 +0000111 switch (X.getSubKind()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000112
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000113 case nonloc::ConcreteIntKind:
114 return cast<nonloc::ConcreteInt>(X).EvalComplement(Eng.getBasicVals());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000115
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000116 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000117 return UnknownVal();
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000118 }
119}
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000120
121// Binary operators.
122
Ted Kremenek1e38f852008-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 Kremeneke04a5cb2008-11-15 00:20:05 +0000132SVal GRSimpleVals::DetermEvalBinOpNN(GRExprEngine& Eng,
Ted Kremenekad8329e2008-07-18 15:27:58 +0000133 BinaryOperator::Opcode Op,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000134 NonLoc L, NonLoc R) {
Ted Kremenek26758752008-09-19 17:31:13 +0000135
Ted Kremeneke04a5cb2008-11-15 00:20:05 +0000136 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenek26758752008-09-19 17:31:13 +0000137 unsigned subkind = L.getSubKind();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000138
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000139 while (1) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000140
Ted Kremenek26758752008-09-19 17:31:13 +0000141 switch (subkind) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000142 default:
Ted Kremenek9258a642008-02-21 19:10:12 +0000143 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000144
Ted Kremeneke04a5cb2008-11-15 00:20:05 +0000145 case nonloc::LocAsIntegerKind: {
146 Loc LL = cast<nonloc::LocAsInteger>(L).getLoc();
147
148 switch (R.getSubKind()) {
149 case nonloc::LocAsIntegerKind:
150 return EvalBinOp(Eng, Op, LL,
151 cast<nonloc::LocAsInteger>(R).getLoc());
152
153 case nonloc::ConcreteIntKind: {
154 // Transform the integer into a location and compare.
155 ASTContext& Ctx = Eng.getContext();
156 llvm::APSInt V = cast<nonloc::ConcreteInt>(R).getValue();
157 V.setIsUnsigned(true);
158 V.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
159 return EvalBinOp(Eng, Op, LL,
160 loc::ConcreteInt(BasicVals.getValue(V)));
161 }
162
163 default:
164 switch (Op) {
165 case BinaryOperator::EQ:
166 return NonLoc::MakeIntTruthVal(BasicVals, false);
167 case BinaryOperator::NE:
168 return NonLoc::MakeIntTruthVal(BasicVals, true);
169 default:
170 // This case also handles pointer arithmetic.
171 return UnknownVal();
172 }
173 }
174 }
175
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000176 case nonloc::SymIntConstraintValKind: {
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000177
178 // Logical not?
179 if (!(Op == BinaryOperator::EQ && R.isZeroConstant()))
180 return UnknownVal();
181
Ted Kremenek1e38f852008-07-18 15:46:06 +0000182 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000183 cast<nonloc::SymIntConstraintVal>(L).getConstraint();
Ted Kremenek1e38f852008-07-18 15:46:06 +0000184
185 BinaryOperator::Opcode Opc = C.getOpcode();
Ted Kremenek40fc5c72008-07-18 15:54:51 +0000186
Ted Kremenek1e38f852008-07-18 15:46:06 +0000187 if (Opc < BinaryOperator::LT || Opc > BinaryOperator::NE)
188 return UnknownVal();
189
190 // For comparison operators, translate the constraint by
191 // changing the opcode.
192
193 int idx = (unsigned) Opc - (unsigned) BinaryOperator::LT;
194
195 assert (idx >= 0 &&
196 (unsigned) idx < sizeof(LNotOpMap)/sizeof(unsigned char));
197
198 Opc = (BinaryOperator::Opcode) LNotOpMap[idx];
199
200 const SymIntConstraint& CNew =
201 BasicVals.getConstraint(C.getSymbol(), Opc, C.getInt());
202
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000203 return nonloc::SymIntConstraintVal(CNew);
Ted Kremenek1e38f852008-07-18 15:46:06 +0000204 }
205
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000206 case nonloc::ConcreteIntKind:
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000207
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000208 if (isa<nonloc::ConcreteInt>(R)) {
209 const nonloc::ConcreteInt& L_CI = cast<nonloc::ConcreteInt>(L);
210 const nonloc::ConcreteInt& R_CI = cast<nonloc::ConcreteInt>(R);
Ted Kremenek240f1f02008-03-07 20:13:31 +0000211 return L_CI.EvalBinOp(BasicVals, Op, R_CI);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000212 }
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000213 else {
Ted Kremenek26758752008-09-19 17:31:13 +0000214 subkind = R.getSubKind();
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000215 NonLoc tmp = R;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000216 R = L;
217 L = tmp;
Ted Kremenek26758752008-09-19 17:31:13 +0000218
219 // Swap the operators.
220 switch (Op) {
221 case BinaryOperator::LT: Op = BinaryOperator::GT; break;
222 case BinaryOperator::GT: Op = BinaryOperator::LT; break;
223 case BinaryOperator::LE: Op = BinaryOperator::GE; break;
224 case BinaryOperator::GE: Op = BinaryOperator::LE; break;
225 default: break;
226 }
227
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000228 continue;
229 }
230
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000231 case nonloc::SymbolValKind:
232 if (isa<nonloc::ConcreteInt>(R)) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000233 if (Op >= BinaryOperator::LT && Op <= BinaryOperator::NE) {
234 const SymIntConstraint& C =
235 BasicVals.getConstraint(cast<nonloc::SymbolVal>(L).getSymbol(),
236 Op, cast<nonloc::ConcreteInt>(R).getValue());
237 return nonloc::SymIntConstraintVal(C);
238 } else {
239 return NonLoc::MakeVal(Eng.getSymbolManager(),
240 cast<nonloc::SymbolVal>(L).getSymbol(),
241 Op, cast<nonloc::ConcreteInt>(R).getValue());
242 }
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000243 }
244 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000245 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000246 }
247 }
248}
249
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000250
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000251// Binary Operators (except assignments and comma).
252
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000253SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
254 Loc L, Loc R) {
Ted Kremenek692416c2008-02-18 22:57:02 +0000255
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000256 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000257
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000258 default:
259 return UnknownVal();
260
261 case BinaryOperator::EQ:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000262 return EvalEQ(Eng, L, R);
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000263
264 case BinaryOperator::NE:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000265 return EvalNE(Eng, L, R);
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000266 }
267}
268
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000269// Pointer arithmetic.
Ted Kremenek214c6cb2009-03-09 20:35:15 +0000270static Loc StripViews(Loc X) {
271 if (isa<loc::MemRegionVal>(X)) {
272 const SymbolicRegion *Region =
273 cast<loc::MemRegionVal>(X).getRegion()->getAs<SymbolicRegion>();
274
275 if (Region)
276 return Loc::MakeVal(Region->getSymbol());
277 }
278
279 return X;
280}
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000281
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000282SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
283 Loc L, NonLoc R) {
Zhongxing Xu94aa6c12009-03-02 07:52:23 +0000284 // Delegate pointer arithmetic to store manager.
285 return Eng.getStoreManager().EvalBinOp(Op, L, R);
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000286}
287
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000288// Equality operators for Locs.
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000289
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000290SVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, Loc L, Loc R) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000291
292 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenek214c6cb2009-03-09 20:35:15 +0000293
294TryAgain:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000295 switch (L.getSubKind()) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000296
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000297 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000298 assert(false && "EQ not implemented for this Loc.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000299 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000300
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000301 case loc::ConcreteIntKind:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000302
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000303 if (isa<loc::ConcreteInt>(R)) {
304 bool b = cast<loc::ConcreteInt>(L).getValue() ==
305 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000306
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000307 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000308 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000309 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000310
311 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000312 BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000313 BinaryOperator::EQ,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000314 cast<loc::ConcreteInt>(L).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000315
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000316 return nonloc::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000317 }
318
319 break;
320
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000321 case loc::SymbolValKind: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000322
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000323 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000324 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000325 BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(),
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000326 BinaryOperator::EQ,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000327 cast<loc::ConcreteInt>(R).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000328
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000329 return nonloc::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000330 }
331
Ted Kremenekf700df22008-02-22 18:41:59 +0000332 // FIXME: Implement == for lval Symbols. This is mainly useful
333 // in iterator loops when traversing a buffer, e.g. while(z != zTerm).
334 // Since this is not useful for many checkers we'll punt on this for
335 // now.
336
337 return UnknownVal();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000338 }
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000339
Ted Kremenek214c6cb2009-03-09 20:35:15 +0000340 case loc::MemRegionKind: {
341 // See if 'L' and 'R' both wrap symbols.
342 Loc LTmp = StripViews(L);
343 Loc RTmp = StripViews(R);
344
345 if (LTmp != L || RTmp != R) {
346 L = LTmp;
347 R = RTmp;
348 goto TryAgain;
349 }
350 }
351
352 // Fall-through.
353
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000354 case loc::FuncValKind:
355 case loc::GotoLabelKind:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000356 return NonLoc::MakeIntTruthVal(BasicVals, L == R);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000357 }
358
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000359 return NonLoc::MakeIntTruthVal(BasicVals, false);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000360}
361
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000362SVal GRSimpleVals::EvalNE(GRExprEngine& Eng, Loc L, Loc R) {
Ted Kremenek692416c2008-02-18 22:57:02 +0000363
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000364 BasicValueFactory& BasicVals = Eng.getBasicVals();
365
Ted Kremenek214c6cb2009-03-09 20:35:15 +0000366TryAgain:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000367 switch (L.getSubKind()) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000368
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000369 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000370 assert(false && "NE not implemented for this Loc.");
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000371 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000372
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000373 case loc::ConcreteIntKind:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000374
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000375 if (isa<loc::ConcreteInt>(R)) {
376 bool b = cast<loc::ConcreteInt>(L).getValue() !=
377 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000378
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000379 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000380 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000381 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000382 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000383 BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000384 BinaryOperator::NE,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000385 cast<loc::ConcreteInt>(L).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000386
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000387 return nonloc::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000388 }
389
390 break;
Ted Kremenek214c6cb2009-03-09 20:35:15 +0000391
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000392 case loc::SymbolValKind: {
393 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000394 const SymIntConstraint& C =
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000395 BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(),
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000396 BinaryOperator::NE,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000397 cast<loc::ConcreteInt>(R).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000398
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000399 return nonloc::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000400 }
401
Ted Kremenekf700df22008-02-22 18:41:59 +0000402 // FIXME: Implement != for lval Symbols. This is mainly useful
403 // in iterator loops when traversing a buffer, e.g. while(z != zTerm).
404 // Since this is not useful for many checkers we'll punt on this for
405 // now.
406
407 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000408
409 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000410 }
411
Ted Kremenek214c6cb2009-03-09 20:35:15 +0000412 case loc::MemRegionKind: {
413 // See if 'L' and 'R' both wrap symbols.
414 Loc LTmp = StripViews(L);
415 Loc RTmp = StripViews(R);
416
417 if (LTmp != L || RTmp != R) {
418 L = LTmp;
419 R = RTmp;
420 goto TryAgain;
421 }
422 }
423
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000424 case loc::FuncValKind:
425 case loc::GotoLabelKind:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000426 return NonLoc::MakeIntTruthVal(BasicVals, L != R);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000427 }
428
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000429 return NonLoc::MakeIntTruthVal(BasicVals, true);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000430}
Ted Kremenek06747692008-02-26 23:04:29 +0000431
432//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000433// Transfer function for function calls.
Ted Kremenek06747692008-02-26 23:04:29 +0000434//===----------------------------------------------------------------------===//
435
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000436void GRSimpleVals::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000437 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000438 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000439 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000440 ExplodedNode<GRState>* Pred) {
Ted Kremenek330dddd2008-03-05 00:33:14 +0000441
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000442 GRStateManager& StateMgr = Eng.getStateManager();
443 const GRState* St = Builder.GetState(Pred);
Ted Kremenek06747692008-02-26 23:04:29 +0000444
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000445 // Invalidate all arguments passed in by reference (Locs).
Ted Kremenek06747692008-02-26 23:04:29 +0000446
447 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
448 I != E; ++I) {
449
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000450 SVal V = StateMgr.GetSVal(St, *I);
Ted Kremenek06747692008-02-26 23:04:29 +0000451
Zhongxing Xud03eea02008-10-27 09:00:08 +0000452 if (isa<loc::MemRegionVal>(V))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000453 St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal());
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000454 else if (isa<nonloc::LocAsInteger>(V))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000455 St = StateMgr.BindLoc(St, cast<nonloc::LocAsInteger>(V).getLoc(),
Ted Kremeneka5488462008-04-22 21:39:21 +0000456 UnknownVal());
457
Ted Kremenek06747692008-02-26 23:04:29 +0000458 }
Ted Kremenekf923a912008-03-12 21:04:07 +0000459
Ted Kremenekfd301942008-10-17 22:23:12 +0000460 // Make up a symbol for the return value of this function.
461 // FIXME: We eventually should handle structs and other compound types
462 // that are returned by value.
463 QualType T = CE->getType();
Ted Kremenek062e2f92008-11-13 06:10:40 +0000464 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenekf923a912008-03-12 21:04:07 +0000465 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +0000466 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
Ted Kremenekf923a912008-03-12 21:04:07 +0000467
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000468 SVal X = Loc::IsLocType(CE->getType())
469 ? cast<SVal>(loc::SymbolVal(Sym))
470 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenekf923a912008-03-12 21:04:07 +0000471
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000472 St = StateMgr.BindExpr(St, CE, X, Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenekf923a912008-03-12 21:04:07 +0000473 }
Ted Kremenek330dddd2008-03-05 00:33:14 +0000474
Ted Kremenek0e561a32008-03-21 21:30:14 +0000475 Builder.MakeNode(Dst, CE, Pred, St);
Ted Kremenek06747692008-02-26 23:04:29 +0000476}
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000477
478//===----------------------------------------------------------------------===//
479// Transfer function for Objective-C message expressions.
480//===----------------------------------------------------------------------===//
481
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000482void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000483 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000484 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000485 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000486 ExplodedNode<GRState>* Pred) {
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000487
488
489 // The basic transfer function logic for message expressions does nothing.
490 // We just invalidate all arguments passed in by references.
491
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000492 GRStateManager& StateMgr = Eng.getStateManager();
493 const GRState* St = Builder.GetState(Pred);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000494
495 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
496 I != E; ++I) {
497
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000498 SVal V = StateMgr.GetSVal(St, *I);
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000499
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000500 if (isa<Loc>(V))
Zhongxing Xu8cd5aae2008-10-30 05:33:54 +0000501 St = StateMgr.BindLoc(St, cast<Loc>(V), UnknownVal());
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000502 }
503
504 Builder.MakeNode(Dst, ME, Pred, St);
505}