blob: 48b25a71f9df04d8c23680c4514c90982dc69b2a [file] [log] [blame]
Ted Kremenek3ca94292008-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 Greif3a8edd82008-03-06 10:40:09 +000010// This file defines GRSimpleVals, a sub-class of GRTransferFuncs that
Ted Kremenek3ca94292008-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 Kremeneka4d60b62008-03-27 17:17:22 +000017#include "BasicObjCFoundationChecks.h"
Ted Kremenek744fb6d2008-04-02 22:03:53 +000018#include "clang/Basic/SourceManager.h"
Ted Kremenekc27815c2008-03-31 18:26:32 +000019#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +000020#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek505a36a2008-04-03 04:42:52 +000021#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek4a78c3a2008-04-10 22:16:52 +000022#include "clang/Analysis/LocalCheckers.h"
Ted Kremenek20d80062008-04-30 20:17:27 +000023#include "clang/Analysis/PathSensitive/GRExprEngine.h"
Ted Kremenek505a36a2008-04-03 04:42:52 +000024#include "llvm/Support/Compiler.h"
Ted Kremenekfd8352c2008-02-27 17:56:16 +000025#include <sstream>
Ted Kremenek3ca94292008-02-14 18:28:23 +000026
27using namespace clang;
28
Ted Kremenekd12d21c2008-04-02 07:05:46 +000029//===----------------------------------------------------------------------===//
Ted Kremenek4a78c3a2008-04-10 22:16:52 +000030// Transfer Function creation for External clients.
Ted Kremenek2c71d512008-04-02 05:15:22 +000031//===----------------------------------------------------------------------===//
Ted Kremenek505a36a2008-04-03 04:42:52 +000032
Ted Kremenek4a78c3a2008-04-10 22:16:52 +000033GRTransferFuncs* clang::MakeGRSimpleValsTF() { return new GRSimpleVals(); }
Ted Kremenekd3122cb2008-02-14 22:36:46 +000034
Ted Kremenek3ca94292008-02-14 18:28:23 +000035//===----------------------------------------------------------------------===//
36// Transfer function for Casts.
37//===----------------------------------------------------------------------===//
38
Zhongxing Xu27f17422008-10-17 05:57:07 +000039SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLoc X, QualType T) {
Ted Kremenek346169f2008-02-18 22:57:02 +000040
Zhongxing Xu27f17422008-10-17 05:57:07 +000041 if (!isa<nonloc::ConcreteInt>(X))
Ted Kremenek3ca94292008-02-14 18:28:23 +000042 return UnknownVal();
Ted Kremenek68d73d12008-03-12 01:21:45 +000043
Zhongxing Xu27f17422008-10-17 05:57:07 +000044 bool isLocType = Loc::IsLocType(T);
Ted Kremenekae543142008-07-16 00:23:49 +000045
Ted Kremenekf46ddda2008-07-15 23:17:54 +000046 // Only handle casts from integers to integers.
Zhongxing Xu27f17422008-10-17 05:57:07 +000047 if (!isLocType && !T->isIntegerType())
Ted Kremenekf46ddda2008-07-15 23:17:54 +000048 return UnknownVal();
49
Ted Kremenek68d73d12008-03-12 01:21:45 +000050 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenek3ca94292008-02-14 18:28:23 +000051
Zhongxing Xu27f17422008-10-17 05:57:07 +000052 llvm::APSInt V = cast<nonloc::ConcreteInt>(X).getValue();
53 V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T));
Ted Kremenek68d73d12008-03-12 01:21:45 +000054 V.extOrTrunc(Eng.getContext().getTypeSize(T));
Ted Kremenek3ca94292008-02-14 18:28:23 +000055
Zhongxing Xu27f17422008-10-17 05:57:07 +000056 if (isLocType)
57 return loc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenek3ca94292008-02-14 18:28:23 +000058 else
Zhongxing Xu27f17422008-10-17 05:57:07 +000059 return nonloc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenek3ca94292008-02-14 18:28:23 +000060}
61
62// Casts.
63
Zhongxing Xu27f17422008-10-17 05:57:07 +000064SVal GRSimpleVals::EvalCast(GRExprEngine& Eng, Loc X, QualType T) {
Ted Kremenek346169f2008-02-18 22:57:02 +000065
Ted Kremeneke0e8b532008-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 //
Zhongxing Xu27f17422008-10-17 05:57:07 +000072 if (Loc::IsLocType(T) || T->isReferenceType())
Ted Kremenek3ca94292008-02-14 18:28:23 +000073 return X;
74
Ted Kremenek7e4861b2008-02-21 18:43:30 +000075 assert (T->isIntegerType());
Ted Kremenek3ca94292008-02-14 18:28:23 +000076
Zhongxing Xu27f17422008-10-17 05:57:07 +000077 if (!isa<loc::ConcreteInt>(X))
Ted Kremenek3ca94292008-02-14 18:28:23 +000078 return UnknownVal();
79
Ted Kremenek68d73d12008-03-12 01:21:45 +000080 BasicValueFactory& BasicVals = Eng.getBasicVals();
81
Zhongxing Xu27f17422008-10-17 05:57:07 +000082 llvm::APSInt V = cast<loc::ConcreteInt>(X).getValue();
83 V.setIsUnsigned(T->isUnsignedIntegerType() || Loc::IsLocType(T));
Ted Kremenek68d73d12008-03-12 01:21:45 +000084 V.extOrTrunc(Eng.getContext().getTypeSize(T));
Ted Kremenek3ca94292008-02-14 18:28:23 +000085
Zhongxing Xu27f17422008-10-17 05:57:07 +000086 return nonloc::ConcreteInt(BasicVals.getValue(V));
Ted Kremenek4bad8f72008-02-14 18:40:24 +000087}
88
89// Unary operators.
90
Zhongxing Xu27f17422008-10-17 05:57:07 +000091SVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLoc X){
Ted Kremenek346169f2008-02-18 22:57:02 +000092
Ted Kremenek4bad8f72008-02-14 18:40:24 +000093 switch (X.getSubKind()) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +000094
Zhongxing Xu27f17422008-10-17 05:57:07 +000095 case nonloc::ConcreteIntKind:
96 return cast<nonloc::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U);
Ted Kremenek7f0639b2008-02-21 18:02:17 +000097
Ted Kremenek4bad8f72008-02-14 18:40:24 +000098 default:
Ted Kremenek7f0639b2008-02-21 18:02:17 +000099 return UnknownVal();
Ted Kremenek4bad8f72008-02-14 18:40:24 +0000100 }
101}
102
Zhongxing Xu27f17422008-10-17 05:57:07 +0000103SVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLoc X) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000104
Ted Kremenek34e83b82008-02-20 04:12:31 +0000105 switch (X.getSubKind()) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000106
Zhongxing Xu27f17422008-10-17 05:57:07 +0000107 case nonloc::ConcreteIntKind:
108 return cast<nonloc::ConcreteInt>(X).EvalComplement(Eng.getBasicVals());
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000109
Ted Kremenek4bad8f72008-02-14 18:40:24 +0000110 default:
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000111 return UnknownVal();
Ted Kremenek4bad8f72008-02-14 18:40:24 +0000112 }
113}
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000114
115// Binary operators.
116
Ted Kremenek6a62d902008-07-18 15:46:06 +0000117static unsigned char LNotOpMap[] = {
118 (unsigned char) BinaryOperator::GE, /* LT => GE */
119 (unsigned char) BinaryOperator::LE, /* GT => LE */
120 (unsigned char) BinaryOperator::GT, /* LE => GT */
121 (unsigned char) BinaryOperator::LT, /* GE => LT */
122 (unsigned char) BinaryOperator::NE, /* EQ => NE */
123 (unsigned char) BinaryOperator::EQ /* NE => EQ */
124};
125
Zhongxing Xu27f17422008-10-17 05:57:07 +0000126SVal GRSimpleVals::DetermEvalBinOpNN(GRStateManager& StateMgr,
Ted Kremenek91614e62008-07-18 15:27:58 +0000127 BinaryOperator::Opcode Op,
Zhongxing Xu27f17422008-10-17 05:57:07 +0000128 NonLoc L, NonLoc R) {
Ted Kremeneka5bf9cb2008-09-19 17:31:13 +0000129
Ted Kremenek8d6b42e2008-07-18 05:53:58 +0000130 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
Ted Kremeneka5bf9cb2008-09-19 17:31:13 +0000131 unsigned subkind = L.getSubKind();
Ted Kremenek68d73d12008-03-12 01:21:45 +0000132
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000133 while (1) {
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000134
Ted Kremeneka5bf9cb2008-09-19 17:31:13 +0000135 switch (subkind) {
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000136 default:
Ted Kremenekd21429a2008-02-21 19:10:12 +0000137 return UnknownVal();
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000138
Zhongxing Xu27f17422008-10-17 05:57:07 +0000139 case nonloc::SymIntConstraintValKind: {
Ted Kremenekb1c91bf2008-07-18 15:54:51 +0000140
141 // Logical not?
142 if (!(Op == BinaryOperator::EQ && R.isZeroConstant()))
143 return UnknownVal();
144
Ted Kremenek6a62d902008-07-18 15:46:06 +0000145 const SymIntConstraint& C =
Zhongxing Xu27f17422008-10-17 05:57:07 +0000146 cast<nonloc::SymIntConstraintVal>(L).getConstraint();
Ted Kremenek6a62d902008-07-18 15:46:06 +0000147
148 BinaryOperator::Opcode Opc = C.getOpcode();
Ted Kremenekb1c91bf2008-07-18 15:54:51 +0000149
Ted Kremenek6a62d902008-07-18 15:46:06 +0000150 if (Opc < BinaryOperator::LT || Opc > BinaryOperator::NE)
151 return UnknownVal();
152
153 // For comparison operators, translate the constraint by
154 // changing the opcode.
155
156 int idx = (unsigned) Opc - (unsigned) BinaryOperator::LT;
157
158 assert (idx >= 0 &&
159 (unsigned) idx < sizeof(LNotOpMap)/sizeof(unsigned char));
160
161 Opc = (BinaryOperator::Opcode) LNotOpMap[idx];
162
163 const SymIntConstraint& CNew =
164 BasicVals.getConstraint(C.getSymbol(), Opc, C.getInt());
165
Zhongxing Xu27f17422008-10-17 05:57:07 +0000166 return nonloc::SymIntConstraintVal(CNew);
Ted Kremenek6a62d902008-07-18 15:46:06 +0000167 }
168
Zhongxing Xu27f17422008-10-17 05:57:07 +0000169 case nonloc::ConcreteIntKind:
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000170
Zhongxing Xu27f17422008-10-17 05:57:07 +0000171 if (isa<nonloc::ConcreteInt>(R)) {
172 const nonloc::ConcreteInt& L_CI = cast<nonloc::ConcreteInt>(L);
173 const nonloc::ConcreteInt& R_CI = cast<nonloc::ConcreteInt>(R);
Ted Kremenek910e9de2008-03-07 20:13:31 +0000174 return L_CI.EvalBinOp(BasicVals, Op, R_CI);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000175 }
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000176 else {
Ted Kremeneka5bf9cb2008-09-19 17:31:13 +0000177 subkind = R.getSubKind();
Zhongxing Xu27f17422008-10-17 05:57:07 +0000178 NonLoc tmp = R;
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000179 R = L;
180 L = tmp;
Ted Kremeneka5bf9cb2008-09-19 17:31:13 +0000181
182 // Swap the operators.
183 switch (Op) {
184 case BinaryOperator::LT: Op = BinaryOperator::GT; break;
185 case BinaryOperator::GT: Op = BinaryOperator::LT; break;
186 case BinaryOperator::LE: Op = BinaryOperator::GE; break;
187 case BinaryOperator::GE: Op = BinaryOperator::LE; break;
188 default: break;
189 }
190
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000191 continue;
192 }
193
Zhongxing Xu27f17422008-10-17 05:57:07 +0000194 case nonloc::SymbolValKind:
195 if (isa<nonloc::ConcreteInt>(R)) {
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000196 const SymIntConstraint& C =
Zhongxing Xu27f17422008-10-17 05:57:07 +0000197 BasicVals.getConstraint(cast<nonloc::SymbolVal>(L).getSymbol(), Op,
198 cast<nonloc::ConcreteInt>(R).getValue());
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000199
Zhongxing Xu27f17422008-10-17 05:57:07 +0000200 return nonloc::SymIntConstraintVal(C);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000201 }
202 else
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000203 return UnknownVal();
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000204 }
205 }
206}
207
Ted Kremenekbc0ba392008-02-15 00:52:26 +0000208
Ted Kremenek6698cb82008-02-15 23:15:23 +0000209// Binary Operators (except assignments and comma).
210
Zhongxing Xu27f17422008-10-17 05:57:07 +0000211SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
212 Loc L, Loc R) {
Ted Kremenek346169f2008-02-18 22:57:02 +0000213
Ted Kremenek6698cb82008-02-15 23:15:23 +0000214 switch (Op) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000215
Ted Kremenek6698cb82008-02-15 23:15:23 +0000216 default:
217 return UnknownVal();
218
219 case BinaryOperator::EQ:
Ted Kremenek68d73d12008-03-12 01:21:45 +0000220 return EvalEQ(Eng, L, R);
Ted Kremenek6698cb82008-02-15 23:15:23 +0000221
222 case BinaryOperator::NE:
Ted Kremenek68d73d12008-03-12 01:21:45 +0000223 return EvalNE(Eng, L, R);
Ted Kremenek6698cb82008-02-15 23:15:23 +0000224 }
225}
226
Ted Kremenekbc0ba392008-02-15 00:52:26 +0000227// Pointer arithmetic.
228
Zhongxing Xu27f17422008-10-17 05:57:07 +0000229SVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
230 Loc L, NonLoc R) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000231 return UnknownVal();
Ted Kremenekbc0ba392008-02-15 00:52:26 +0000232}
233
Zhongxing Xu27f17422008-10-17 05:57:07 +0000234// Equality operators for Locs.
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000235
Zhongxing Xu27f17422008-10-17 05:57:07 +0000236SVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, Loc L, Loc R) {
Ted Kremenek68d73d12008-03-12 01:21:45 +0000237
238 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000239
240 switch (L.getSubKind()) {
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000241
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000242 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000243 assert(false && "EQ not implemented for this Loc.");
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000244 return UnknownVal();
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000245
Zhongxing Xu27f17422008-10-17 05:57:07 +0000246 case loc::ConcreteIntKind:
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000247
Zhongxing Xu27f17422008-10-17 05:57:07 +0000248 if (isa<loc::ConcreteInt>(R)) {
249 bool b = cast<loc::ConcreteInt>(L).getValue() ==
250 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000251
Zhongxing Xu27f17422008-10-17 05:57:07 +0000252 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000253 }
Zhongxing Xu27f17422008-10-17 05:57:07 +0000254 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000255
256 const SymIntConstraint& C =
Zhongxing Xu27f17422008-10-17 05:57:07 +0000257 BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000258 BinaryOperator::EQ,
Zhongxing Xu27f17422008-10-17 05:57:07 +0000259 cast<loc::ConcreteInt>(L).getValue());
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000260
Zhongxing Xu27f17422008-10-17 05:57:07 +0000261 return nonloc::SymIntConstraintVal(C);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000262 }
263
264 break;
265
Zhongxing Xu27f17422008-10-17 05:57:07 +0000266 case loc::SymbolValKind: {
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000267
Zhongxing Xu27f17422008-10-17 05:57:07 +0000268 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000269 const SymIntConstraint& C =
Zhongxing Xu27f17422008-10-17 05:57:07 +0000270 BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(),
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000271 BinaryOperator::EQ,
Zhongxing Xu27f17422008-10-17 05:57:07 +0000272 cast<loc::ConcreteInt>(R).getValue());
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000273
Zhongxing Xu27f17422008-10-17 05:57:07 +0000274 return nonloc::SymIntConstraintVal(C);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000275 }
276
Ted Kremenek43638a82008-02-22 18:41:59 +0000277 // FIXME: Implement == for lval Symbols. This is mainly useful
278 // in iterator loops when traversing a buffer, e.g. while(z != zTerm).
279 // Since this is not useful for many checkers we'll punt on this for
280 // now.
281
282 return UnknownVal();
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000283 }
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000284
Zhongxing Xu27f17422008-10-17 05:57:07 +0000285 case loc::MemRegionKind:
286 case loc::FuncValKind:
287 case loc::GotoLabelKind:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000288 return NonLoc::MakeIntTruthVal(BasicVals, L == R);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000289 }
290
Zhongxing Xu27f17422008-10-17 05:57:07 +0000291 return NonLoc::MakeIntTruthVal(BasicVals, false);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000292}
293
Zhongxing Xu27f17422008-10-17 05:57:07 +0000294SVal GRSimpleVals::EvalNE(GRExprEngine& Eng, Loc L, Loc R) {
Ted Kremenek346169f2008-02-18 22:57:02 +0000295
Ted Kremenek68d73d12008-03-12 01:21:45 +0000296 BasicValueFactory& BasicVals = Eng.getBasicVals();
297
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000298 switch (L.getSubKind()) {
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000299
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000300 default:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000301 assert(false && "NE not implemented for this Loc.");
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000302 return UnknownVal();
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000303
Zhongxing Xu27f17422008-10-17 05:57:07 +0000304 case loc::ConcreteIntKind:
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000305
Zhongxing Xu27f17422008-10-17 05:57:07 +0000306 if (isa<loc::ConcreteInt>(R)) {
307 bool b = cast<loc::ConcreteInt>(L).getValue() !=
308 cast<loc::ConcreteInt>(R).getValue();
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000309
Zhongxing Xu27f17422008-10-17 05:57:07 +0000310 return NonLoc::MakeIntTruthVal(BasicVals, b);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000311 }
Zhongxing Xu27f17422008-10-17 05:57:07 +0000312 else if (isa<loc::SymbolVal>(R)) {
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000313 const SymIntConstraint& C =
Zhongxing Xu27f17422008-10-17 05:57:07 +0000314 BasicVals.getConstraint(cast<loc::SymbolVal>(R).getSymbol(),
Ted Kremenek68d73d12008-03-12 01:21:45 +0000315 BinaryOperator::NE,
Zhongxing Xu27f17422008-10-17 05:57:07 +0000316 cast<loc::ConcreteInt>(L).getValue());
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000317
Zhongxing Xu27f17422008-10-17 05:57:07 +0000318 return nonloc::SymIntConstraintVal(C);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000319 }
320
321 break;
322
Zhongxing Xu27f17422008-10-17 05:57:07 +0000323 case loc::SymbolValKind: {
324 if (isa<loc::ConcreteInt>(R)) {
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000325 const SymIntConstraint& C =
Zhongxing Xu27f17422008-10-17 05:57:07 +0000326 BasicVals.getConstraint(cast<loc::SymbolVal>(L).getSymbol(),
Ted Kremenek68d73d12008-03-12 01:21:45 +0000327 BinaryOperator::NE,
Zhongxing Xu27f17422008-10-17 05:57:07 +0000328 cast<loc::ConcreteInt>(R).getValue());
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000329
Zhongxing Xu27f17422008-10-17 05:57:07 +0000330 return nonloc::SymIntConstraintVal(C);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000331 }
332
Ted Kremenek43638a82008-02-22 18:41:59 +0000333 // FIXME: Implement != for lval Symbols. This is mainly useful
334 // in iterator loops when traversing a buffer, e.g. while(z != zTerm).
335 // Since this is not useful for many checkers we'll punt on this for
336 // now.
337
338 return UnknownVal();
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000339
340 break;
Ted Kremenek7f0639b2008-02-21 18:02:17 +0000341 }
342
Zhongxing Xu27f17422008-10-17 05:57:07 +0000343 case loc::MemRegionKind:
344 case loc::FuncValKind:
345 case loc::GotoLabelKind:
Zhongxing Xu27f17422008-10-17 05:57:07 +0000346 return NonLoc::MakeIntTruthVal(BasicVals, L != R);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000347 }
348
Zhongxing Xu27f17422008-10-17 05:57:07 +0000349 return NonLoc::MakeIntTruthVal(BasicVals, true);
Ted Kremenekcf7cf8e2008-02-14 19:37:24 +0000350}
Ted Kremenek448538d2008-02-26 23:04:29 +0000351
352//===----------------------------------------------------------------------===//
Ted Kremenek667cacb2008-04-15 23:06:53 +0000353// Transfer function for function calls.
Ted Kremenek448538d2008-02-26 23:04:29 +0000354//===----------------------------------------------------------------------===//
355
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000356void GRSimpleVals::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek68d73d12008-03-12 01:21:45 +0000357 GRExprEngine& Eng,
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000358 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu27f17422008-10-17 05:57:07 +0000359 CallExpr* CE, SVal L,
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000360 ExplodedNode<GRState>* Pred) {
Ted Kremenekd5804b32008-03-05 00:33:14 +0000361
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000362 GRStateManager& StateMgr = Eng.getStateManager();
363 const GRState* St = Builder.GetState(Pred);
Ted Kremenek448538d2008-02-26 23:04:29 +0000364
Zhongxing Xu27f17422008-10-17 05:57:07 +0000365 // Invalidate all arguments passed in by reference (Locs).
Ted Kremenek448538d2008-02-26 23:04:29 +0000366
367 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
368 I != E; ++I) {
369
Zhongxing Xu27f17422008-10-17 05:57:07 +0000370 SVal V = StateMgr.GetSVal(St, *I);
Ted Kremenek448538d2008-02-26 23:04:29 +0000371
Zhongxing Xud7c44052008-10-27 09:00:08 +0000372 if (isa<loc::MemRegionVal>(V))
Zhongxing Xu27f17422008-10-17 05:57:07 +0000373 St = StateMgr.SetSVal(St, cast<Loc>(V), UnknownVal());
374 else if (isa<nonloc::LocAsInteger>(V))
375 St = StateMgr.SetSVal(St, cast<nonloc::LocAsInteger>(V).getLoc(),
Ted Kremenekc79c0592008-04-22 21:39:21 +0000376 UnknownVal());
377
Ted Kremenek448538d2008-02-26 23:04:29 +0000378 }
Ted Kremenek86f1d0c2008-03-12 21:04:07 +0000379
Ted Kremenek21387322008-10-17 22:23:12 +0000380 // Make up a symbol for the return value of this function.
381 // FIXME: We eventually should handle structs and other compound types
382 // that are returned by value.
383 QualType T = CE->getType();
384 if (T->isIntegerType() || Loc::IsLocType(T)) {
Ted Kremenek86f1d0c2008-03-12 21:04:07 +0000385 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremeneke2f6d6c2008-03-12 21:45:47 +0000386 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
Ted Kremenek86f1d0c2008-03-12 21:04:07 +0000387
Zhongxing Xu27f17422008-10-17 05:57:07 +0000388 SVal X = Loc::IsLocType(CE->getType())
389 ? cast<SVal>(loc::SymbolVal(Sym))
390 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek86f1d0c2008-03-12 21:04:07 +0000391
Zhongxing Xu27f17422008-10-17 05:57:07 +0000392 St = StateMgr.SetSVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenek86f1d0c2008-03-12 21:04:07 +0000393 }
Ted Kremenekd5804b32008-03-05 00:33:14 +0000394
Ted Kremenek181f7232008-03-21 21:30:14 +0000395 Builder.MakeNode(Dst, CE, Pred, St);
Ted Kremenek448538d2008-02-26 23:04:29 +0000396}
Ted Kremenek667cacb2008-04-15 23:06:53 +0000397
398//===----------------------------------------------------------------------===//
399// Transfer function for Objective-C message expressions.
400//===----------------------------------------------------------------------===//
401
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000402void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek667cacb2008-04-15 23:06:53 +0000403 GRExprEngine& Eng,
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000404 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek667cacb2008-04-15 23:06:53 +0000405 ObjCMessageExpr* ME,
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000406 ExplodedNode<GRState>* Pred) {
Ted Kremenek667cacb2008-04-15 23:06:53 +0000407
408
409 // The basic transfer function logic for message expressions does nothing.
410 // We just invalidate all arguments passed in by references.
411
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +0000412 GRStateManager& StateMgr = Eng.getStateManager();
413 const GRState* St = Builder.GetState(Pred);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000414
415 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
416 I != E; ++I) {
417
Zhongxing Xu27f17422008-10-17 05:57:07 +0000418 SVal V = StateMgr.GetSVal(St, *I);
Ted Kremenek667cacb2008-04-15 23:06:53 +0000419
Zhongxing Xu27f17422008-10-17 05:57:07 +0000420 if (isa<Loc>(V))
421 St = StateMgr.SetSVal(St, cast<Loc>(V), UnknownVal());
Ted Kremenek667cacb2008-04-15 23:06:53 +0000422 }
423
424 Builder.MakeNode(Dst, ME, Pred, St);
425}