blob: f6053dc418c79cd314ba5b8fc52409b97d19944d [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 Kremenek61f3e052008-04-03 04:42:52 +000020#include "clang/Analysis/PathSensitive/ValueState.h"
21#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 Kremenekdd598112008-04-02 07:05:46 +000030// Utility functions.
31//===----------------------------------------------------------------------===//
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000032
Chris Lattner3ae30f82008-04-06 04:22:39 +000033template <typename ITERATOR> inline
Ted Kremenek61f3e052008-04-03 04:42:52 +000034ExplodedNode<ValueState>* GetNode(ITERATOR I) {
Ted Kremenek503d6132008-04-02 05:15:22 +000035 return *I;
36}
37
Chris Lattner3ae30f82008-04-06 04:22:39 +000038template <> inline
Ted Kremenek61f3e052008-04-03 04:42:52 +000039ExplodedNode<ValueState>* GetNode(GRExprEngine::undef_arg_iterator I) {
Ted Kremenek503d6132008-04-02 05:15:22 +000040 return I->first;
41}
Ted Kremenekdd598112008-04-02 07:05:46 +000042
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000043template <typename ITER>
Ted Kremenek95cc1ba2008-04-18 20:54:29 +000044void GenericEmitWarnings(BugReporter& BR, BugType& D, ITER I, ITER E) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000045
46 for (; I != E; ++I) {
Ted Kremenekd2f642b2008-04-14 17:39:48 +000047 BugReport R(D, GetNode(I));
Ted Kremenek75840e12008-04-18 01:56:37 +000048 BR.EmitWarning(R);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000049 }
50}
51
52//===----------------------------------------------------------------------===//
53// Bug Descriptions.
54//===----------------------------------------------------------------------===//
55
56namespace {
57
Ted Kremenek95cc1ba2008-04-18 20:54:29 +000058class VISIBILITY_HIDDEN NullDeref : public BugTypeCacheLocation {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000059public:
60 virtual const char* getName() const {
61 return "null dereference";
62 }
63
64 virtual const char* getDescription() const {
65 return "Dereference of null pointer.";
66 }
67
68 virtual void EmitWarnings(BugReporter& BR) {
69 GRExprEngine& Eng = BR.getEngine();
70 GenericEmitWarnings(BR, *this, Eng.null_derefs_begin(),
71 Eng.null_derefs_end());
72 }
73};
74
Ted Kremenek95cc1ba2008-04-18 20:54:29 +000075class VISIBILITY_HIDDEN UndefDeref : public BugTypeCacheLocation {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000076public:
77 virtual const char* getName() const {
78 return "bad dereference";
79 }
80
81 virtual const char* getDescription() const {
82 return "Dereference of undefined value.";
83 }
84
85 virtual void EmitWarnings(BugReporter& BR) {
86 GRExprEngine& Eng = BR.getEngine();
87 GenericEmitWarnings(BR, *this, Eng.undef_derefs_begin(),
88 Eng.undef_derefs_end());
89 }
90};
91
Ted Kremenek95cc1ba2008-04-18 20:54:29 +000092class VISIBILITY_HIDDEN UndefBranch : public BugTypeCacheLocation {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +000093public:
94 virtual const char* getName() const {
95 return "uninitialized value";
96 }
97
98 virtual const char* getDescription() const {
99 return "Branch condition evaluates to an uninitialized value.";
100 }
101
Ted Kremenek5c454ab2008-05-05 15:56:53 +0000102 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000103};
104
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000105class VISIBILITY_HIDDEN DivZero : public BugTypeCacheLocation {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000106public:
107 virtual const char* getName() const {
108 return "divide-by-zero";
109 }
110
111 virtual const char* getDescription() const {
112 return "Division by zero/undefined value.";
113 }
114
115 virtual void EmitWarnings(BugReporter& BR) {
116 GRExprEngine& Eng = BR.getEngine();
117 GenericEmitWarnings(BR, *this, Eng.explicit_bad_divides_begin(),
118 Eng.explicit_bad_divides_end());
119 }
120};
121
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000122class VISIBILITY_HIDDEN UndefResult : public BugTypeCacheLocation {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000123public:
124 virtual const char* getName() const {
125 return "undefined result";
126 }
127
128 virtual const char* getDescription() const {
129 return "Result of operation is undefined.";
130 }
131
132 virtual void EmitWarnings(BugReporter& BR) {
133 GRExprEngine& Eng = BR.getEngine();
134 GenericEmitWarnings(BR, *this, Eng.undef_results_begin(),
Ted Kremenek4d35dac2008-04-10 16:05:13 +0000135 Eng.undef_results_end());
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000136 }
137};
138
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000139class VISIBILITY_HIDDEN BadCall : public BugTypeCacheLocation {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000140public:
141 virtual const char* getName() const {
142 return "invalid function call";
143 }
144
145 virtual const char* getDescription() const {
146 return "Called function is a NULL or undefined function pointer value.";
147 }
148
149 virtual void EmitWarnings(BugReporter& BR) {
150 GRExprEngine& Eng = BR.getEngine();
151 GenericEmitWarnings(BR, *this, Eng.bad_calls_begin(),
Ted Kremenek4d35dac2008-04-10 16:05:13 +0000152 Eng.bad_calls_end());
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000153 }
154};
155
156
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000157class VISIBILITY_HIDDEN BadArg : public BugTypeCacheLocation {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000158public:
159
160 virtual ~BadArg() {}
161
162 virtual const char* getName() const {
163 return "bad argument";
164 }
165
166 virtual const char* getDescription() const {
167 return "Pass-by-value argument in function is undefined.";
168 }
169
170 virtual void EmitWarnings(BugReporter& BR) {
171 GRExprEngine& Eng = BR.getEngine();
172
173 for (GRExprEngine::UndefArgsTy::iterator I = Eng.undef_arg_begin(),
174 E = Eng.undef_arg_end(); I!=E; ++I) {
175
176 // Generate a report for this bug.
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000177 RangedBugReport report(*this, I->first);
178 report.addRange(I->second->getSourceRange());
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000179
180 // Emit the warning.
Ted Kremenek75840e12008-04-18 01:56:37 +0000181 BR.EmitWarning(report);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000182 }
183
184 }
185};
186
187class VISIBILITY_HIDDEN BadMsgExprArg : public BadArg {
188public:
189 virtual const char* getName() const {
190 return "bad argument";
191 }
192
193 virtual const char* getDescription() const {
194 return "Pass-by-value argument in message expression is undefined.";
195 }
196
197 virtual void EmitWarnings(BugReporter& BR) {
198 GRExprEngine& Eng = BR.getEngine();
199
200 for (GRExprEngine::UndefArgsTy::iterator I=Eng.msg_expr_undef_arg_begin(),
Ted Kremenek4d35dac2008-04-10 16:05:13 +0000201 E = Eng.msg_expr_undef_arg_end(); I!=E; ++I) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000202
203 // Generate a report for this bug.
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000204 RangedBugReport report(*this, I->first);
205 report.addRange(I->second->getSourceRange());
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000206
207 // Emit the warning.
Ted Kremenek75840e12008-04-18 01:56:37 +0000208 BR.EmitWarning(report);
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000209 }
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000210 }
211};
212
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000213class VISIBILITY_HIDDEN BadReceiver : public BugTypeCacheLocation {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000214public:
215 virtual const char* getName() const {
216 return "bad receiver";
217 }
218
219 virtual const char* getDescription() const {
220 return "Receiver in message expression is an uninitialized value.";
221 }
222
223 virtual void EmitWarnings(BugReporter& BR) {
224 GRExprEngine& Eng = BR.getEngine();
225
226 for (GRExprEngine::UndefReceiversTy::iterator I=Eng.undef_receivers_begin(),
Argyrios Kyrtzidisafe10912008-04-15 16:30:10 +0000227 End = Eng.undef_receivers_end(); I!=End; ++I) {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000228
229 // Generate a report for this bug.
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000230 RangedBugReport report(*this, *I);
231
232 ExplodedNode<ValueState>* N = *I;
233 Stmt *S = cast<PostStmt>(N->getLocation()).getStmt();
234 Expr* E = cast<ObjCMessageExpr>(S)->getReceiver();
235 assert (E && "Receiver cannot be NULL");
236 report.addRange(E->getSourceRange());
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000237
238 // Emit the warning.
Ted Kremenek75840e12008-04-18 01:56:37 +0000239 BR.EmitWarning(report);
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000240 }
241 }
242};
243
Ted Kremenek95cc1ba2008-04-18 20:54:29 +0000244class VISIBILITY_HIDDEN RetStack : public BugTypeCacheLocation {
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000245public:
246 virtual const char* getName() const {
247 return "return of stack address";
248 }
249
250 virtual const char* getDescription() const {
251 return "Address of stack-allocated variable returned.";
252 }
253
254 virtual void EmitWarnings(BugReporter& BR) {
255 GRExprEngine& Eng = BR.getEngine();
256 GenericEmitWarnings(BR, *this, Eng.ret_stackaddr_begin(),
Ted Kremenek4d35dac2008-04-10 16:05:13 +0000257 Eng.ret_stackaddr_end());
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000258 }
259};
260
261} // end anonymous namespace
262
Ted Kremenek5c454ab2008-05-05 15:56:53 +0000263
264namespace {
265
266struct VISIBILITY_HIDDEN FindUndefExpr {
267 ValueStateManager& VM;
268 ValueState* St;
269
270 FindUndefExpr(ValueStateManager& V, ValueState* S) : VM(V), St(S) {}
271
272 Expr* FindExpr(Expr* Ex) {
273
274 if (!MatchesCriteria(Ex))
275 return 0;
276
277 for (Stmt::child_iterator I=Ex->child_begin(), E=Ex->child_end(); I!=E; ++I)
278 if (Expr* ExI = dyn_cast_or_null<Expr>(*I)) {
279 Expr* E2 = FindExpr(ExI);
280 if (E2) return E2;
281 }
282
283 return Ex;
284 }
285
286 bool MatchesCriteria(Expr* Ex) { return VM.GetRVal(St, Ex).isUndef(); }
287};
288
289} // end anonymous namespace
290
291
292void UndefBranch::EmitWarnings(BugReporter& BR) {
293
294 GRExprEngine& Eng = BR.getEngine();
295
296 for (GRExprEngine::undef_branch_iterator I=Eng.undef_branches_begin(),
297 E=Eng.undef_branches_end(); I!=E; ++I) {
298
299 // What's going on here: we want to highlight the subexpression of the
300 // condition that is the most likely source of the "uninitialized
301 // branch condition." We do a recursive walk of the condition's
302 // subexpressions and roughly look for the most nested subexpression
303 // that binds to Undefined. We then highlight that expression's range.
304
305 BlockEdge B = cast<BlockEdge>((*I)->getLocation());
306 Expr* Ex = cast<Expr>(B.getSrc()->getTerminatorCondition());
307 assert (Ex && "Block must have a terminator.");
308
309 // Get the predecessor node and check if is a PostStmt with the Stmt
310 // being the terminator condition. We want to inspect the state
311 // of that node instead because it will contain main information about
312 // the subexpressions.
313
314 assert (!(*I)->pred_empty());
315
316 // Note: any predecessor will do. They should have identical state,
317 // since all the BlockEdge did was act as an error sink since the value
318 // had to already be undefined.
319 ExplodedNode<ValueState> *N = *(*I)->pred_begin();
320 ProgramPoint P = N->getLocation();
321
322 ValueState* St = (*I)->getState();
323
324 if (PostStmt* PS = dyn_cast<PostStmt>(&P))
325 if (PS->getStmt() == Ex)
326 St = N->getState();
327
328 FindUndefExpr FindIt(Eng.getStateManager(), St);
329 Ex = FindIt.FindExpr(Ex);
330
331 RangedBugReport R(*this, *I);
332 R.addRange(Ex->getSourceRange());
333
334 BR.EmitWarning(R);
335 }
336}
337
338
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000339void GRSimpleVals::RegisterChecks(GRExprEngine& Eng) {
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000340
341 // Path-sensitive checks.
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000342 Eng.Register(new NullDeref());
343 Eng.Register(new UndefDeref());
344 Eng.Register(new UndefBranch());
345 Eng.Register(new DivZero());
346 Eng.Register(new UndefResult());
347 Eng.Register(new BadCall());
348 Eng.Register(new RetStack());
349 Eng.Register(new BadArg());
350 Eng.Register(new BadMsgExprArg());
351 Eng.Register(new BadReceiver());
352
Ted Kremenekd2f642b2008-04-14 17:39:48 +0000353 // Flow-sensitive checks.
354 Eng.Register(MakeDeadStoresChecker());
355
Ted Kremenek50a6d0c2008-04-09 21:41:14 +0000356 // Add extra checkers.
357
358 GRSimpleAPICheck* FoundationCheck =
359 CreateBasicObjCFoundationChecks(Eng.getContext(), &Eng.getStateManager());
360
361 Eng.AddObjCMessageExprCheck(FoundationCheck);
362}
363
Ted Kremenekdd598112008-04-02 07:05:46 +0000364//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +0000365// Transfer Function creation for External clients.
Ted Kremenek503d6132008-04-02 05:15:22 +0000366//===----------------------------------------------------------------------===//
Ted Kremenek61f3e052008-04-03 04:42:52 +0000367
Ted Kremenekd71ed262008-04-10 22:16:52 +0000368GRTransferFuncs* clang::MakeGRSimpleValsTF() { return new GRSimpleVals(); }
Ted Kremeneke01c9872008-02-14 22:36:46 +0000369
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000370//===----------------------------------------------------------------------===//
371// Transfer function for Casts.
372//===----------------------------------------------------------------------===//
373
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000374RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, NonLVal X, QualType T) {
Ted Kremenek692416c2008-02-18 22:57:02 +0000375
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000376 if (!isa<nonlval::ConcreteInt>(X))
377 return UnknownVal();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000378
379 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000380
381 llvm::APSInt V = cast<nonlval::ConcreteInt>(X).getValue();
Ted Kremenekc0c3f5d2008-04-30 20:17:27 +0000382 V.setIsUnsigned(T->isUnsignedIntegerType() || IsPointerType(T));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000383 V.extOrTrunc(Eng.getContext().getTypeSize(T));
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000384
Ted Kremenekc0c3f5d2008-04-30 20:17:27 +0000385 if (IsPointerType(T))
Ted Kremenek240f1f02008-03-07 20:13:31 +0000386 return lval::ConcreteInt(BasicVals.getValue(V));
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000387 else
Ted Kremenek240f1f02008-03-07 20:13:31 +0000388 return nonlval::ConcreteInt(BasicVals.getValue(V));
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000389}
390
391// Casts.
392
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000393RVal GRSimpleVals::EvalCast(GRExprEngine& Eng, LVal X, QualType T) {
Ted Kremenek692416c2008-02-18 22:57:02 +0000394
Ted Kremeneke8c2bde2008-04-30 21:10:19 +0000395 // Casts from pointers -> pointers, just return the lval.
396 //
397 // Casts from pointers -> references, just return the lval. These
398 // can be introduced by the frontend for corner cases, e.g
399 // casting from va_list* to __builtin_va_list&.
400 //
401 if (IsPointerType(T) || T->isReferenceType())
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000402 return X;
403
Ted Kremenek9ef1ec92008-02-21 18:43:30 +0000404 assert (T->isIntegerType());
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000405
406 if (!isa<lval::ConcreteInt>(X))
407 return UnknownVal();
408
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000409 BasicValueFactory& BasicVals = Eng.getBasicVals();
410
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000411 llvm::APSInt V = cast<lval::ConcreteInt>(X).getValue();
Ted Kremenekc0c3f5d2008-04-30 20:17:27 +0000412 V.setIsUnsigned(T->isUnsignedIntegerType() || IsPointerType(T));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000413 V.extOrTrunc(Eng.getContext().getTypeSize(T));
Ted Kremenekd59cccc2008-02-14 18:28:23 +0000414
Ted Kremenek240f1f02008-03-07 20:13:31 +0000415 return nonlval::ConcreteInt(BasicVals.getValue(V));
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000416}
417
418// Unary operators.
419
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000420RVal GRSimpleVals::EvalMinus(GRExprEngine& Eng, UnaryOperator* U, NonLVal X){
Ted Kremenek692416c2008-02-18 22:57:02 +0000421
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000422 switch (X.getSubKind()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000423
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000424 case nonlval::ConcreteIntKind:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000425 return cast<nonlval::ConcreteInt>(X).EvalMinus(Eng.getBasicVals(), U);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000426
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000427 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000428 return UnknownVal();
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000429 }
430}
431
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000432RVal GRSimpleVals::EvalComplement(GRExprEngine& Eng, NonLVal X) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000433
Ted Kremenek90e42032008-02-20 04:12:31 +0000434 switch (X.getSubKind()) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000435
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000436 case nonlval::ConcreteIntKind:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000437 return cast<nonlval::ConcreteInt>(X).EvalComplement(Eng.getBasicVals());
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000438
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000439 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000440 return UnknownVal();
Ted Kremenekc3f261d2008-02-14 18:40:24 +0000441 }
442}
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000443
444// Binary operators.
445
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000446RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
447 NonLVal L, NonLVal R) {
448
449 BasicValueFactory& BasicVals = Eng.getBasicVals();
450
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000451 while (1) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000452
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000453 switch (L.getSubKind()) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000454 default:
Ted Kremenek9258a642008-02-21 19:10:12 +0000455 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000456
457 case nonlval::ConcreteIntKind:
458
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000459 if (isa<nonlval::ConcreteInt>(R)) {
460 const nonlval::ConcreteInt& L_CI = cast<nonlval::ConcreteInt>(L);
461 const nonlval::ConcreteInt& R_CI = cast<nonlval::ConcreteInt>(R);
Ted Kremenek240f1f02008-03-07 20:13:31 +0000462 return L_CI.EvalBinOp(BasicVals, Op, R_CI);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000463 }
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000464 else {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000465 NonLVal tmp = R;
466 R = L;
467 L = tmp;
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000468 continue;
469 }
470
471 case nonlval::SymbolValKind: {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000472
473 if (isa<nonlval::ConcreteInt>(R)) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000474 const SymIntConstraint& C =
Ted Kremenek240f1f02008-03-07 20:13:31 +0000475 BasicVals.getConstraint(cast<nonlval::SymbolVal>(L).getSymbol(), Op,
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000476 cast<nonlval::ConcreteInt>(R).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000477
478 return nonlval::SymIntConstraintVal(C);
479 }
480 else
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000481 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000482 }
483 }
484 }
485}
486
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000487
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000488// Binary Operators (except assignments and comma).
489
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000490RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000491 LVal L, LVal R) {
Ted Kremenek692416c2008-02-18 22:57:02 +0000492
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000493 switch (Op) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000494
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000495 default:
496 return UnknownVal();
497
498 case BinaryOperator::EQ:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000499 return EvalEQ(Eng, L, R);
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000500
501 case BinaryOperator::NE:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000502 return EvalNE(Eng, L, R);
Ted Kremenekc6fbdcd2008-02-15 23:15:23 +0000503 }
504}
505
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000506// Pointer arithmetic.
507
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000508RVal GRSimpleVals::EvalBinOp(GRExprEngine& Eng, BinaryOperator::Opcode Op,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000509 LVal L, NonLVal R) {
510 return UnknownVal();
Ted Kremenekb640b3b2008-02-15 00:52:26 +0000511}
512
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000513// Equality operators for LVals.
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000514
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000515RVal GRSimpleVals::EvalEQ(GRExprEngine& Eng, LVal L, LVal R) {
516
517 BasicValueFactory& BasicVals = Eng.getBasicVals();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000518
519 switch (L.getSubKind()) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000520
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000521 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000522 assert(false && "EQ not implemented for this LVal.");
523 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000524
525 case lval::ConcreteIntKind:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000526
527 if (isa<lval::ConcreteInt>(R)) {
528 bool b = cast<lval::ConcreteInt>(L).getValue() ==
529 cast<lval::ConcreteInt>(R).getValue();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000530
Ted Kremenek240f1f02008-03-07 20:13:31 +0000531 return NonLVal::MakeIntTruthVal(BasicVals, b);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000532 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000533 else if (isa<lval::SymbolVal>(R)) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000534
535 const SymIntConstraint& C =
Ted Kremenek240f1f02008-03-07 20:13:31 +0000536 BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(),
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000537 BinaryOperator::EQ,
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000538 cast<lval::ConcreteInt>(L).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000539
540 return nonlval::SymIntConstraintVal(C);
541 }
542
543 break;
544
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000545 case lval::SymbolValKind: {
546
547 if (isa<lval::ConcreteInt>(R)) {
548 const SymIntConstraint& C =
Ted Kremenek240f1f02008-03-07 20:13:31 +0000549 BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(),
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000550 BinaryOperator::EQ,
551 cast<lval::ConcreteInt>(R).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000552
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000553 return nonlval::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000554 }
555
Ted Kremenekf700df22008-02-22 18:41:59 +0000556 // FIXME: Implement == for lval Symbols. This is mainly useful
557 // in iterator loops when traversing a buffer, e.g. while(z != zTerm).
558 // Since this is not useful for many checkers we'll punt on this for
559 // now.
560
561 return UnknownVal();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000562 }
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000563
Ted Kremenekc3b7f0e2008-04-30 16:07:22 +0000564 // FIXME: Different offsets can map to the same memory cell.
565 case lval::ArrayOffsetKind:
566 case lval::FieldOffsetKind:
567 // Fall-through.
568
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000569 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +0000570 case lval::FuncValKind:
571 case lval::GotoLabelKind:
Ted Kremenek240f1f02008-03-07 20:13:31 +0000572 return NonLVal::MakeIntTruthVal(BasicVals, L == R);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000573 }
574
Ted Kremenek240f1f02008-03-07 20:13:31 +0000575 return NonLVal::MakeIntTruthVal(BasicVals, false);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000576}
577
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000578RVal GRSimpleVals::EvalNE(GRExprEngine& Eng, LVal L, LVal R) {
Ted Kremenek692416c2008-02-18 22:57:02 +0000579
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000580 BasicValueFactory& BasicVals = Eng.getBasicVals();
581
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000582 switch (L.getSubKind()) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000583
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000584 default:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000585 assert(false && "NE not implemented for this LVal.");
586 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000587
588 case lval::ConcreteIntKind:
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000589
590 if (isa<lval::ConcreteInt>(R)) {
591 bool b = cast<lval::ConcreteInt>(L).getValue() !=
592 cast<lval::ConcreteInt>(R).getValue();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000593
Ted Kremenek240f1f02008-03-07 20:13:31 +0000594 return NonLVal::MakeIntTruthVal(BasicVals, b);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000595 }
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000596 else if (isa<lval::SymbolVal>(R)) {
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000597 const SymIntConstraint& C =
Ted Kremenek240f1f02008-03-07 20:13:31 +0000598 BasicVals.getConstraint(cast<lval::SymbolVal>(R).getSymbol(),
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000599 BinaryOperator::NE,
600 cast<lval::ConcreteInt>(L).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000601
602 return nonlval::SymIntConstraintVal(C);
603 }
604
605 break;
606
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000607 case lval::SymbolValKind: {
608 if (isa<lval::ConcreteInt>(R)) {
609 const SymIntConstraint& C =
Ted Kremenek240f1f02008-03-07 20:13:31 +0000610 BasicVals.getConstraint(cast<lval::SymbolVal>(L).getSymbol(),
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000611 BinaryOperator::NE,
612 cast<lval::ConcreteInt>(R).getValue());
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000613
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000614 return nonlval::SymIntConstraintVal(C);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000615 }
616
Ted Kremenekf700df22008-02-22 18:41:59 +0000617 // FIXME: Implement != for lval Symbols. This is mainly useful
618 // in iterator loops when traversing a buffer, e.g. while(z != zTerm).
619 // Since this is not useful for many checkers we'll punt on this for
620 // now.
621
622 return UnknownVal();
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000623
624 break;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000625 }
626
Ted Kremenekc3b7f0e2008-04-30 16:07:22 +0000627 // FIXME: Different offsets can map to the same memory cell.
628 case lval::ArrayOffsetKind:
629 case lval::FieldOffsetKind:
630 // Fall-through.
631
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000632 case lval::DeclValKind:
Ted Kremenekdc3936b2008-02-22 00:54:56 +0000633 case lval::FuncValKind:
634 case lval::GotoLabelKind:
Ted Kremenek240f1f02008-03-07 20:13:31 +0000635 return NonLVal::MakeIntTruthVal(BasicVals, L != R);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000636 }
637
Ted Kremenek240f1f02008-03-07 20:13:31 +0000638 return NonLVal::MakeIntTruthVal(BasicVals, true);
Ted Kremenek6cb0b542008-02-14 19:37:24 +0000639}
Ted Kremenek06747692008-02-26 23:04:29 +0000640
641//===----------------------------------------------------------------------===//
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000642// Transfer function for function calls.
Ted Kremenek06747692008-02-26 23:04:29 +0000643//===----------------------------------------------------------------------===//
644
Ted Kremenek330dddd2008-03-05 00:33:14 +0000645void GRSimpleVals::EvalCall(ExplodedNodeSet<ValueState>& Dst,
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000646 GRExprEngine& Eng,
Ted Kremenek330dddd2008-03-05 00:33:14 +0000647 GRStmtNodeBuilder<ValueState>& Builder,
Ted Kremenek186350f2008-04-23 20:12:28 +0000648 CallExpr* CE, RVal L,
Ted Kremenek330dddd2008-03-05 00:33:14 +0000649 ExplodedNode<ValueState>* Pred) {
650
Ted Kremenekf923a912008-03-12 21:04:07 +0000651 ValueStateManager& StateMgr = Eng.getStateManager();
652 ValueState* St = Builder.GetState(Pred);
Ted Kremenek06747692008-02-26 23:04:29 +0000653
654 // Invalidate all arguments passed in by reference (LVals).
655
656 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
657 I != E; ++I) {
658
Ted Kremenekf923a912008-03-12 21:04:07 +0000659 RVal V = StateMgr.GetRVal(St, *I);
Ted Kremenek06747692008-02-26 23:04:29 +0000660
661 if (isa<LVal>(V))
Ted Kremenekf923a912008-03-12 21:04:07 +0000662 St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal());
Ted Kremeneka5488462008-04-22 21:39:21 +0000663 else if (isa<nonlval::LValAsInteger>(V))
664 St = StateMgr.SetRVal(St, cast<nonlval::LValAsInteger>(V).getLVal(),
665 UnknownVal());
666
Ted Kremenek06747692008-02-26 23:04:29 +0000667 }
Ted Kremenekf923a912008-03-12 21:04:07 +0000668
669 // Make up a symbol for the return value of this function.
670
671 if (CE->getType() != Eng.getContext().VoidTy) {
672 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek361fa8e2008-03-12 21:45:47 +0000673 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
Ted Kremenekf923a912008-03-12 21:04:07 +0000674
Ted Kremenekc0c3f5d2008-04-30 20:17:27 +0000675 RVal X = IsPointerType(CE->getType())
Ted Kremenekf923a912008-03-12 21:04:07 +0000676 ? cast<RVal>(lval::SymbolVal(Sym))
677 : cast<RVal>(nonlval::SymbolVal(Sym));
678
679 St = StateMgr.SetRVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false);
680 }
Ted Kremenek330dddd2008-03-05 00:33:14 +0000681
Ted Kremenek0e561a32008-03-21 21:30:14 +0000682 Builder.MakeNode(Dst, CE, Pred, St);
Ted Kremenek06747692008-02-26 23:04:29 +0000683}
Ted Kremeneke695e1c2008-04-15 23:06:53 +0000684
685//===----------------------------------------------------------------------===//
686// Transfer function for Objective-C message expressions.
687//===----------------------------------------------------------------------===//
688
689void GRSimpleVals::EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst,
690 GRExprEngine& Eng,
691 GRStmtNodeBuilder<ValueState>& Builder,
692 ObjCMessageExpr* ME,
693 ExplodedNode<ValueState>* Pred) {
694
695
696 // The basic transfer function logic for message expressions does nothing.
697 // We just invalidate all arguments passed in by references.
698
699 ValueStateManager& StateMgr = Eng.getStateManager();
700 ValueState* St = Builder.GetState(Pred);
701
702 for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end();
703 I != E; ++I) {
704
705 RVal V = StateMgr.GetRVal(St, *I);
706
707 if (isa<LVal>(V))
708 St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal());
709 }
710
711 Builder.MakeNode(Dst, ME, Pred, St);
712}