blob: 21c2ecf4b944a603e309c040340228f57ab5661c [file] [log] [blame]
Chris Lattnerbda0b622008-03-15 23:59:48 +00001// CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
Ted Kremenek2fff37e2008-03-06 00:08:09 +00002//
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 the methods for CFRefCount, which implements
Ted Kremenek2fff37e2008-03-06 00:08:09 +000011// a reference count checker for Core Foundation (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000015#include "GRSimpleVals.h"
Ted Kremenek072192b2008-04-30 23:47:44 +000016#include "clang/Basic/LangOptions.h"
Ted Kremenekc9fa2f72008-05-01 23:13:35 +000017#include "clang/Basic/SourceManager.h"
Ted Kremenek4adc81e2008-08-13 04:27:00 +000018#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenekb9d17f92008-08-17 03:20:02 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000020#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek2fff37e2008-03-06 00:08:09 +000021#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000022#include "clang/Analysis/PathDiagnostic.h"
23#include "clang/Analysis/PathSensitive/BugReporter.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000024#include "clang/AST/DeclObjC.h"
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000025#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/FoldingSet.h"
27#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek6d348932008-10-21 15:53:15 +000028#include "llvm/ADT/ImmutableList.h"
Ted Kremenek900a2d72008-05-07 18:36:45 +000029#include "llvm/ADT/StringExtras.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000030#include "llvm/Support/Compiler.h"
Ted Kremenek6ed9afc2008-05-16 18:33:44 +000031#include "llvm/ADT/STLExtras.h"
Ted Kremenekf3948042008-03-11 19:44:10 +000032#include <ostream>
Ted Kremenek2cf943a2008-04-18 04:55:01 +000033#include <sstream>
Ted Kremenek98530452008-08-12 20:41:56 +000034#include <stdarg.h>
Ted Kremenek2fff37e2008-03-06 00:08:09 +000035
36using namespace clang;
Ted Kremenek5c74d502008-10-24 21:18:08 +000037
38//===----------------------------------------------------------------------===//
39// Utility functions.
40//===----------------------------------------------------------------------===//
41
Ted Kremenek900a2d72008-05-07 18:36:45 +000042using llvm::CStrInCStrNoCase;
Ted Kremenek2fff37e2008-03-06 00:08:09 +000043
Ted Kremenek5c74d502008-10-24 21:18:08 +000044// The "fundamental rule" for naming conventions of methods:
45// (url broken into two lines)
46// http://developer.apple.com/documentation/Cocoa/Conceptual/
47// MemoryMgmt/Tasks/MemoryManagementRules.html
48//
49// "You take ownership of an object if you create it using a method whose name
50// begins with “alloc” or “new” or contains “copy” (for example, alloc,
51// newObject, or mutableCopy), or if you send it a retain message. You are
52// responsible for relinquishing ownership of objects you own using release
53// or autorelease. Any other time you receive an object, you must
54// not release it."
55//
56static bool followsFundamentalRule(const char* s) {
57 return CStrInCStrNoCase(s, "create") || CStrInCStrNoCase(s, "copy") ||
58 CStrInCStrNoCase(s, "new") == s || CStrInCStrNoCase(s, "alloc") == s;
59}
60
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000061//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +000062// Selector creation functions.
Ted Kremenek4fd88972008-04-17 18:12:53 +000063//===----------------------------------------------------------------------===//
64
Ted Kremenekb83e02e2008-05-01 18:31:44 +000065static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
Ted Kremenek4fd88972008-04-17 18:12:53 +000066 IdentifierInfo* II = &Ctx.Idents.get(name);
67 return Ctx.Selectors.getSelector(0, &II);
68}
69
Ted Kremenek9c32d082008-05-06 00:30:21 +000070static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
71 IdentifierInfo* II = &Ctx.Idents.get(name);
72 return Ctx.Selectors.getSelector(1, &II);
73}
74
Ted Kremenek553cf182008-06-25 21:21:56 +000075//===----------------------------------------------------------------------===//
76// Type querying functions.
77//===----------------------------------------------------------------------===//
78
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000079static bool isCFRefType(QualType T) {
80
81 if (!T->isPointerType())
82 return false;
83
Ted Kremenek553cf182008-06-25 21:21:56 +000084 // Check the typedef for the name "CF" and the substring "Ref".
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000085 TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
86
87 if (!TD)
88 return false;
89
90 const char* TDName = TD->getDecl()->getIdentifier()->getName();
91 assert (TDName);
92
93 if (TDName[0] != 'C' || TDName[1] != 'F')
94 return false;
95
96 if (strstr(TDName, "Ref") == 0)
97 return false;
98
99 return true;
100}
101
Ted Kremenek37d785b2008-07-15 16:50:12 +0000102static bool isCGRefType(QualType T) {
103
104 if (!T->isPointerType())
105 return false;
106
107 // Check the typedef for the name "CG" and the substring "Ref".
108 TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
109
110 if (!TD)
111 return false;
112
113 const char* TDName = TD->getDecl()->getIdentifier()->getName();
114 assert (TDName);
115
116 if (TDName[0] != 'C' || TDName[1] != 'G')
117 return false;
118
119 if (strstr(TDName, "Ref") == 0)
120 return false;
121
122 return true;
123}
124
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000125static bool isNSType(QualType T) {
126
127 if (!T->isPointerType())
128 return false;
129
130 ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
131
132 if (!OT)
133 return false;
134
135 const char* ClsName = OT->getDecl()->getIdentifier()->getName();
136 assert (ClsName);
137
138 if (ClsName[0] != 'N' || ClsName[1] != 'S')
139 return false;
140
141 return true;
142}
143
Ted Kremenek4fd88972008-04-17 18:12:53 +0000144//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +0000145// Primitives used for constructing summaries for function/method calls.
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000146//===----------------------------------------------------------------------===//
147
Ted Kremenek553cf182008-06-25 21:21:56 +0000148namespace {
149/// ArgEffect is used to summarize a function/method call's effect on a
150/// particular argument.
Ted Kremenek070a8252008-07-09 18:11:16 +0000151enum ArgEffect { IncRef, DecRef, DoNothing, DoNothingByRef,
152 StopTracking, MayEscape, SelfOwn, Autorelease };
Ted Kremenek553cf182008-06-25 21:21:56 +0000153
154/// ArgEffects summarizes the effects of a function/method call on all of
155/// its arguments.
156typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000157}
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000158
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000159namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000160template <> struct FoldingSetTrait<ArgEffects> {
161 static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
162 for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) {
163 ID.AddInteger(I->first);
164 ID.AddInteger((unsigned) I->second);
165 }
166 }
167};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000168} // end llvm namespace
169
170namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000171
172/// RetEffect is used to summarize a function/method call's behavior with
173/// respect to its return value.
174class VISIBILITY_HIDDEN RetEffect {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000175public:
Ted Kremeneka7344702008-06-23 18:02:52 +0000176 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
177 NotOwnedSymbol, ReceiverAlias };
Ted Kremenek553cf182008-06-25 21:21:56 +0000178
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000179private:
180 unsigned Data;
Ted Kremenek553cf182008-06-25 21:21:56 +0000181 RetEffect(Kind k, unsigned D = 0) { Data = (D << 3) | (unsigned) k; }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000182
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000183public:
Ted Kremenek553cf182008-06-25 21:21:56 +0000184
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000185 Kind getKind() const { return (Kind) (Data & 0x7); }
Ted Kremenek553cf182008-06-25 21:21:56 +0000186
187 unsigned getIndex() const {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000188 assert(getKind() == Alias);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000189 return Data >> 3;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000190 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000191
Ted Kremenek553cf182008-06-25 21:21:56 +0000192 static RetEffect MakeAlias(unsigned Idx) {
193 return RetEffect(Alias, Idx);
194 }
195 static RetEffect MakeReceiverAlias() {
196 return RetEffect(ReceiverAlias);
197 }
Ted Kremeneka7344702008-06-23 18:02:52 +0000198 static RetEffect MakeOwned(bool isAllocated = false) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000199 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol);
200 }
201 static RetEffect MakeNotOwned() {
202 return RetEffect(NotOwnedSymbol);
203 }
204 static RetEffect MakeNoRet() {
205 return RetEffect(NoRet);
Ted Kremeneka7344702008-06-23 18:02:52 +0000206 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000207
Ted Kremenek553cf182008-06-25 21:21:56 +0000208 operator Kind() const {
209 return getKind();
210 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000211
Ted Kremenek553cf182008-06-25 21:21:56 +0000212 void Profile(llvm::FoldingSetNodeID& ID) const {
213 ID.AddInteger(Data);
214 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000215};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000216
Ted Kremenek553cf182008-06-25 21:21:56 +0000217
218class VISIBILITY_HIDDEN RetainSummary : public llvm::FoldingSetNode {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000219 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
220 /// specifies the argument (starting from 0). This can be sparsely
221 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000222 ArgEffects* Args;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000223
224 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
225 /// do not have an entry in Args.
226 ArgEffect DefaultArgEffect;
227
Ted Kremenek553cf182008-06-25 21:21:56 +0000228 /// Receiver - If this summary applies to an Objective-C message expression,
229 /// this is the effect applied to the state of the receiver.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000230 ArgEffect Receiver;
Ted Kremenek553cf182008-06-25 21:21:56 +0000231
232 /// Ret - The effect on the return value. Used to indicate if the
233 /// function/method call returns a new tracked symbol, returns an
234 /// alias of one of the arguments in the call, and so on.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000235 RetEffect Ret;
Ted Kremenek553cf182008-06-25 21:21:56 +0000236
Ted Kremenek70a733e2008-07-18 17:24:20 +0000237 /// EndPath - Indicates that execution of this method/function should
238 /// terminate the simulation of a path.
239 bool EndPath;
240
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000241public:
242
Ted Kremenek1bffd742008-05-06 15:44:25 +0000243 RetainSummary(ArgEffects* A, RetEffect R, ArgEffect defaultEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000244 ArgEffect ReceiverEff, bool endpath = false)
245 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
246 EndPath(endpath) {}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000247
Ted Kremenek553cf182008-06-25 21:21:56 +0000248 /// getArg - Return the argument effect on the argument specified by
249 /// idx (starting from 0).
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000250 ArgEffect getArg(unsigned idx) const {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000251
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000252 if (!Args)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000253 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000254
255 // If Args is present, it is likely to contain only 1 element.
256 // Just do a linear search. Do it from the back because functions with
257 // large numbers of arguments will be tail heavy with respect to which
Ted Kremenek553cf182008-06-25 21:21:56 +0000258 // argument they actually modify with respect to the reference count.
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000259 for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend();
260 I!=E; ++I) {
261
262 if (idx > I->first)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000263 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000264
265 if (idx == I->first)
266 return I->second;
267 }
268
Ted Kremenek1bffd742008-05-06 15:44:25 +0000269 return DefaultArgEffect;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000270 }
271
Ted Kremenek553cf182008-06-25 21:21:56 +0000272 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000273 RetEffect getRetEffect() const {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000274 return Ret;
275 }
276
Ted Kremenek70a733e2008-07-18 17:24:20 +0000277 /// isEndPath - Returns true if executing the given method/function should
278 /// terminate the path.
279 bool isEndPath() const { return EndPath; }
280
Ted Kremenek553cf182008-06-25 21:21:56 +0000281 /// getReceiverEffect - Returns the effect on the receiver of the call.
282 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000283 ArgEffect getReceiverEffect() const {
284 return Receiver;
285 }
286
Ted Kremenek55499762008-06-17 02:43:46 +0000287 typedef ArgEffects::const_iterator ExprIterator;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000288
Ted Kremenek55499762008-06-17 02:43:46 +0000289 ExprIterator begin_args() const { return Args->begin(); }
290 ExprIterator end_args() const { return Args->end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000291
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000292 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000293 RetEffect RetEff, ArgEffect DefaultEff,
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000294 ArgEffect ReceiverEff, bool EndPath) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000295 ID.AddPointer(A);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000296 ID.Add(RetEff);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000297 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000298 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000299 ID.AddInteger((unsigned) EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000300 }
301
302 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000303 Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000304 }
305};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000306} // end anonymous namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000307
Ted Kremenek553cf182008-06-25 21:21:56 +0000308//===----------------------------------------------------------------------===//
309// Data structures for constructing summaries.
310//===----------------------------------------------------------------------===//
Ted Kremenek53301ba2008-06-24 03:49:48 +0000311
Ted Kremenek553cf182008-06-25 21:21:56 +0000312namespace {
313class VISIBILITY_HIDDEN ObjCSummaryKey {
314 IdentifierInfo* II;
315 Selector S;
316public:
317 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
318 : II(ii), S(s) {}
319
320 ObjCSummaryKey(ObjCInterfaceDecl* d, Selector s)
321 : II(d ? d->getIdentifier() : 0), S(s) {}
322
323 ObjCSummaryKey(Selector s)
324 : II(0), S(s) {}
325
326 IdentifierInfo* getIdentifier() const { return II; }
327 Selector getSelector() const { return S; }
328};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000329}
330
331namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000332template <> struct DenseMapInfo<ObjCSummaryKey> {
333 static inline ObjCSummaryKey getEmptyKey() {
334 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
335 DenseMapInfo<Selector>::getEmptyKey());
336 }
Ted Kremenek4f22a782008-06-23 23:30:29 +0000337
Ted Kremenek553cf182008-06-25 21:21:56 +0000338 static inline ObjCSummaryKey getTombstoneKey() {
339 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
340 DenseMapInfo<Selector>::getTombstoneKey());
341 }
342
343 static unsigned getHashValue(const ObjCSummaryKey &V) {
344 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
345 & 0x88888888)
346 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
347 & 0x55555555);
348 }
349
350 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
351 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
352 RHS.getIdentifier()) &&
353 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
354 RHS.getSelector());
355 }
356
357 static bool isPod() {
358 return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
359 DenseMapInfo<Selector>::isPod();
360 }
361};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000362} // end llvm namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000363
Ted Kremenek4f22a782008-06-23 23:30:29 +0000364namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000365class VISIBILITY_HIDDEN ObjCSummaryCache {
366 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
367 MapTy M;
368public:
369 ObjCSummaryCache() {}
370
371 typedef MapTy::iterator iterator;
372
373 iterator find(ObjCInterfaceDecl* D, Selector S) {
374
375 // Do a lookup with the (D,S) pair. If we find a match return
376 // the iterator.
377 ObjCSummaryKey K(D, S);
378 MapTy::iterator I = M.find(K);
379
380 if (I != M.end() || !D)
381 return I;
382
383 // Walk the super chain. If we find a hit with a parent, we'll end
384 // up returning that summary. We actually allow that key (null,S), as
385 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
386 // generate initial summaries without having to worry about NSObject
387 // being declared.
388 // FIXME: We may change this at some point.
389 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
390 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
391 break;
392
393 if (!C)
394 return I;
395 }
396
397 // Cache the summary with original key to make the next lookup faster
398 // and return the iterator.
399 M[K] = I->second;
400 return I;
401 }
402
Ted Kremenek98530452008-08-12 20:41:56 +0000403
Ted Kremenek553cf182008-06-25 21:21:56 +0000404 iterator find(Expr* Receiver, Selector S) {
405 return find(getReceiverDecl(Receiver), S);
406 }
407
408 iterator find(IdentifierInfo* II, Selector S) {
409 // FIXME: Class method lookup. Right now we dont' have a good way
410 // of going between IdentifierInfo* and the class hierarchy.
411 iterator I = M.find(ObjCSummaryKey(II, S));
412 return I == M.end() ? M.find(ObjCSummaryKey(S)) : I;
413 }
414
415 ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
416
417 const PointerType* PT = E->getType()->getAsPointerType();
418 if (!PT) return 0;
419
420 ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType());
421 if (!OI) return 0;
422
423 return OI ? OI->getDecl() : 0;
424 }
425
426 iterator end() { return M.end(); }
427
428 RetainSummary*& operator[](ObjCMessageExpr* ME) {
429
430 Selector S = ME->getSelector();
431
432 if (Expr* Receiver = ME->getReceiver()) {
433 ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
434 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
435 }
436
437 return M[ObjCSummaryKey(ME->getClassName(), S)];
438 }
439
440 RetainSummary*& operator[](ObjCSummaryKey K) {
441 return M[K];
442 }
443
444 RetainSummary*& operator[](Selector S) {
445 return M[ ObjCSummaryKey(S) ];
446 }
447};
448} // end anonymous namespace
449
450//===----------------------------------------------------------------------===//
451// Data structures for managing collections of summaries.
452//===----------------------------------------------------------------------===//
453
454namespace {
455class VISIBILITY_HIDDEN RetainSummaryManager {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000456
457 //==-----------------------------------------------------------------==//
458 // Typedefs.
459 //==-----------------------------------------------------------------==//
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000460
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000461 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> >
462 ArgEffectsSetTy;
463
464 typedef llvm::FoldingSet<RetainSummary>
465 SummarySetTy;
466
467 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
468 FuncSummariesTy;
469
Ted Kremenek4f22a782008-06-23 23:30:29 +0000470 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000471
472 //==-----------------------------------------------------------------==//
473 // Data.
474 //==-----------------------------------------------------------------==//
475
Ted Kremenek553cf182008-06-25 21:21:56 +0000476 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek377e2302008-04-29 05:33:51 +0000477 ASTContext& Ctx;
Ted Kremenek179064e2008-07-01 17:21:27 +0000478
Ted Kremenek070a8252008-07-09 18:11:16 +0000479 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
480 /// "CFDictionaryCreate".
481 IdentifierInfo* CFDictionaryCreateII;
482
Ted Kremenek553cf182008-06-25 21:21:56 +0000483 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek377e2302008-04-29 05:33:51 +0000484 const bool GCEnabled;
485
Ted Kremenek553cf182008-06-25 21:21:56 +0000486 /// SummarySet - A FoldingSet of uniqued summaries.
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000487 SummarySetTy SummarySet;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000488
Ted Kremenek553cf182008-06-25 21:21:56 +0000489 /// FuncSummaries - A map from FunctionDecls to summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000490 FuncSummariesTy FuncSummaries;
491
Ted Kremenek553cf182008-06-25 21:21:56 +0000492 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
493 /// to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000494 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000495
Ted Kremenek553cf182008-06-25 21:21:56 +0000496 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000497 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000498
Ted Kremenek553cf182008-06-25 21:21:56 +0000499 /// ArgEffectsSet - A FoldingSet of uniqued ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000500 ArgEffectsSetTy ArgEffectsSet;
501
Ted Kremenek553cf182008-06-25 21:21:56 +0000502 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
503 /// and all other data used by the checker.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000504 llvm::BumpPtrAllocator BPAlloc;
505
Ted Kremenek553cf182008-06-25 21:21:56 +0000506 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000507 ArgEffects ScratchArgs;
508
Ted Kremenek432af592008-05-06 18:11:36 +0000509 RetainSummary* StopSummary;
510
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000511 //==-----------------------------------------------------------------==//
512 // Methods.
513 //==-----------------------------------------------------------------==//
514
Ted Kremenek553cf182008-06-25 21:21:56 +0000515 /// getArgEffects - Returns a persistent ArgEffects object based on the
516 /// data in ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000517 ArgEffects* getArgEffects();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000518
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000519 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000520
521public:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000522 RetainSummary* getUnarySummary(FunctionDecl* FD, UnaryFuncKind func);
Ted Kremenek377e2302008-04-29 05:33:51 +0000523
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000524 RetainSummary* getNSSummary(FunctionDecl* FD, const char* FName);
525 RetainSummary* getCFSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000526 RetainSummary* getCGSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000527
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000528 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
529 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000530 RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000531
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000532 RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000533 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000534 ArgEffect DefaultEff = MayEscape,
535 bool isEndPath = false);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000536
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000537 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000538 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000539 ArgEffect DefaultEff = MayEscape) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000540 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek9c32d082008-05-06 00:30:21 +0000541 }
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000542
Ted Kremenek1bffd742008-05-06 15:44:25 +0000543 RetainSummary* getPersistentStopSummary() {
Ted Kremenek432af592008-05-06 18:11:36 +0000544 if (StopSummary)
545 return StopSummary;
546
547 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
548 StopTracking, StopTracking);
549
550 return StopSummary;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000551 }
Ted Kremenekb3095252008-05-06 04:20:12 +0000552
Ted Kremenek553cf182008-06-25 21:21:56 +0000553 RetainSummary* getInitMethodSummary(ObjCMessageExpr* ME);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000554
Ted Kremenek1f180c32008-06-23 22:21:20 +0000555 void InitializeClassMethodSummaries();
556 void InitializeMethodSummaries();
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000557
558private:
559
Ted Kremenek70a733e2008-07-18 17:24:20 +0000560 void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
561 RetainSummary* Summ) {
562 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
563 }
564
Ted Kremenek553cf182008-06-25 21:21:56 +0000565 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
566 ObjCClassMethodSummaries[S] = Summ;
567 }
568
569 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
570 ObjCMethodSummaries[S] = Summ;
571 }
572
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000573 void addInstMethSummary(const char* Cls, RetainSummary* Summ, va_list argp) {
Ted Kremenek70a733e2008-07-18 17:24:20 +0000574
Ted Kremenek9e476de2008-08-12 18:30:56 +0000575 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
576 llvm::SmallVector<IdentifierInfo*, 10> II;
577
578 while (const char* s = va_arg(argp, const char*))
579 II.push_back(&Ctx.Idents.get(s));
580
581 Selector S = Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek70a733e2008-07-18 17:24:20 +0000582 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
583 }
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000584
585 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
586 va_list argp;
587 va_start(argp, Summ);
588 addInstMethSummary(Cls, Summ, argp);
589 va_end(argp);
590 }
Ted Kremenek9e476de2008-08-12 18:30:56 +0000591
592 void addPanicSummary(const char* Cls, ...) {
593 RetainSummary* Summ = getPersistentSummary(0, RetEffect::MakeNoRet(),
594 DoNothing, DoNothing, true);
595 va_list argp;
596 va_start (argp, Cls);
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000597 addInstMethSummary(Cls, Summ, argp);
Ted Kremenek9e476de2008-08-12 18:30:56 +0000598 va_end(argp);
599 }
Ted Kremenek70a733e2008-07-18 17:24:20 +0000600
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000601public:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000602
603 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenek179064e2008-07-01 17:21:27 +0000604 : Ctx(ctx),
Ted Kremenek070a8252008-07-09 18:11:16 +0000605 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek553cf182008-06-25 21:21:56 +0000606 GCEnabled(gcenabled), StopSummary(0) {
607
608 InitializeClassMethodSummaries();
609 InitializeMethodSummaries();
610 }
Ted Kremenek377e2302008-04-29 05:33:51 +0000611
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000612 ~RetainSummaryManager();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000613
Ted Kremenekab592272008-06-24 03:56:45 +0000614 RetainSummary* getSummary(FunctionDecl* FD);
Ted Kremenek553cf182008-06-25 21:21:56 +0000615 RetainSummary* getMethodSummary(ObjCMessageExpr* ME, ObjCInterfaceDecl* ID);
Ted Kremenek1f180c32008-06-23 22:21:20 +0000616 RetainSummary* getClassMethodSummary(IdentifierInfo* ClsName, Selector S);
Ted Kremenekb3095252008-05-06 04:20:12 +0000617
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000618 bool isGCEnabled() const { return GCEnabled; }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000619};
620
621} // end anonymous namespace
622
623//===----------------------------------------------------------------------===//
624// Implementation of checker data structures.
625//===----------------------------------------------------------------------===//
626
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000627RetainSummaryManager::~RetainSummaryManager() {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000628
629 // FIXME: The ArgEffects could eventually be allocated from BPAlloc,
630 // mitigating the need to do explicit cleanup of the
631 // Argument-Effect summaries.
632
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000633 for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(),
634 E = ArgEffectsSet.end(); I!=E; ++I)
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000635 I->getValue().~ArgEffects();
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000636}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000637
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000638ArgEffects* RetainSummaryManager::getArgEffects() {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000639
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000640 if (ScratchArgs.empty())
641 return NULL;
642
643 // Compute a profile for a non-empty ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000644 llvm::FoldingSetNodeID profile;
645 profile.Add(ScratchArgs);
646 void* InsertPos;
647
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000648 // Look up the uniqued copy, or create a new one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000649 llvm::FoldingSetNodeWrapper<ArgEffects>* E =
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000650 ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000651
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000652 if (E) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000653 ScratchArgs.clear();
654 return &E->getValue();
655 }
656
657 E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
Ted Kremenek553cf182008-06-25 21:21:56 +0000658 BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000659
660 new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000661 ArgEffectsSet.InsertNode(E, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000662
663 ScratchArgs.clear();
664 return &E->getValue();
665}
666
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000667RetainSummary*
668RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000669 ArgEffect ReceiverEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000670 ArgEffect DefaultEff,
671 bool isEndPath) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000672
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000673 // Generate a profile for the summary.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000674 llvm::FoldingSetNodeID profile;
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000675 RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff,
676 isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000677
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000678 // Look up the uniqued summary, or create one if it doesn't exist.
679 void* InsertPos;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000680 RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000681
682 if (Summ)
683 return Summ;
684
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000685 // Create the summary and return it.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000686 Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek70a733e2008-07-18 17:24:20 +0000687 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000688 SummarySet.InsertNode(Summ, InsertPos);
689
690 return Summ;
691}
692
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000693//===----------------------------------------------------------------------===//
694// Summary creation for functions (largely uses of Core Foundation).
695//===----------------------------------------------------------------------===//
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000696
Ted Kremenekab592272008-06-24 03:56:45 +0000697RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000698
699 SourceLocation Loc = FD->getLocation();
700
701 if (!Loc.isFileID())
702 return NULL;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000703
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000704 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000705 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000706
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000707 if (I != FuncSummaries.end())
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000708 return I->second;
709
710 // No summary. Generate one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000711 const char* FName = FD->getIdentifier()->getName();
712
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000713 RetainSummary *S = 0;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000714
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000715 FunctionType* FT = dyn_cast<FunctionType>(FD->getType());
Ted Kremenek37d785b2008-07-15 16:50:12 +0000716
717 do {
718 if (FT) {
719
720 QualType T = FT->getResultType();
721
722 if (isCFRefType(T)) {
723 S = getCFSummary(FD, FName);
724 break;
725 }
726
727 if (isCGRefType(T)) {
728 S = getCGSummary(FD, FName );
729 break;
730 }
731 }
732
Ted Kremenek64e859a2008-10-22 20:54:52 +0000733 if (FName[0] == 'C') {
734 if (FName[1] == 'F')
735 S = getCFSummary(FD, FName);
736 else if (FName[1] == 'G')
737 S = getCGSummary(FD, FName);
738 }
Ted Kremenek37d785b2008-07-15 16:50:12 +0000739 else if (FName[0] == 'N' && FName[1] == 'S')
740 S = getNSSummary(FD, FName);
741 }
742 while (0);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000743
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000744 FuncSummaries[FD] = S;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000745 return S;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000746}
747
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000748RetainSummary* RetainSummaryManager::getNSSummary(FunctionDecl* FD,
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000749 const char* FName) {
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000750 FName += 2;
751
752 if (strcmp(FName, "MakeCollectable") == 0)
753 return getUnarySummary(FD, cfmakecollectable);
754
755 return 0;
756}
Ted Kremenek37d785b2008-07-15 16:50:12 +0000757
758static bool isRetain(FunctionDecl* FD, const char* FName) {
Ted Kremenek10161bf2008-07-15 17:43:41 +0000759 const char* loc = strstr(FName, "Retain");
760 return loc && loc[sizeof("Retain")-1] == '\0';
Ted Kremenek37d785b2008-07-15 16:50:12 +0000761}
762
763static bool isRelease(FunctionDecl* FD, const char* FName) {
Ted Kremenek10161bf2008-07-15 17:43:41 +0000764 const char* loc = strstr(FName, "Release");
765 return loc && loc[sizeof("Release")-1] == '\0';
Ted Kremenek37d785b2008-07-15 16:50:12 +0000766}
767
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000768RetainSummary* RetainSummaryManager::getCFSummary(FunctionDecl* FD,
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000769 const char* FName) {
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000770
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000771 if (FName[0] == 'C' && FName[1] == 'F')
772 FName += 2;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000773
Ted Kremenek37d785b2008-07-15 16:50:12 +0000774 if (isRetain(FD, FName))
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000775 return getUnarySummary(FD, cfretain);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000776
Ted Kremenek37d785b2008-07-15 16:50:12 +0000777 if (isRelease(FD, FName))
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000778 return getUnarySummary(FD, cfrelease);
Ted Kremenek070a8252008-07-09 18:11:16 +0000779
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000780 if (strcmp(FName, "MakeCollectable") == 0)
781 return getUnarySummary(FD, cfmakecollectable);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000782
783 return getCFCreateGetRuleSummary(FD, FName);
784}
785
786RetainSummary* RetainSummaryManager::getCGSummary(FunctionDecl* FD,
787 const char* FName) {
788
789 if (FName[0] == 'C' && FName[1] == 'G')
790 FName += 2;
791
792 if (isRelease(FD, FName))
793 return getUnarySummary(FD, cfrelease);
794
795 if (isRetain(FD, FName))
796 return getUnarySummary(FD, cfretain);
797
798 return getCFCreateGetRuleSummary(FD, FName);
799}
800
801RetainSummary*
802RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
803 const char* FName) {
804
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000805 if (strstr(FName, "Create") || strstr(FName, "Copy"))
806 return getCFSummaryCreateRule(FD);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000807
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000808 if (strstr(FName, "Get"))
809 return getCFSummaryGetRule(FD);
810
811 return 0;
812}
813
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000814RetainSummary*
815RetainSummaryManager::getUnarySummary(FunctionDecl* FD, UnaryFuncKind func) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000816
817 FunctionTypeProto* FT =
818 dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
819
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000820 if (FT) {
821
822 if (FT->getNumArgs() != 1)
823 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000824
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000825 TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr());
826
827 if (!ArgT)
828 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000829
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000830 if (!ArgT->isPointerType())
831 return NULL;
832 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000833
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000834 assert (ScratchArgs.empty());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000835
Ted Kremenek377e2302008-04-29 05:33:51 +0000836 switch (func) {
837 case cfretain: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000838 ScratchArgs.push_back(std::make_pair(0, IncRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000839 return getPersistentSummary(RetEffect::MakeAlias(0),
840 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000841 }
842
843 case cfrelease: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000844 ScratchArgs.push_back(std::make_pair(0, DecRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000845 return getPersistentSummary(RetEffect::MakeNoRet(),
846 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000847 }
848
849 case cfmakecollectable: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000850 if (GCEnabled)
851 ScratchArgs.push_back(std::make_pair(0, DecRef));
852
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000853 return getPersistentSummary(RetEffect::MakeAlias(0),
854 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000855 }
856
857 default:
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000858 assert (false && "Not a supported unary function.");
Ted Kremenek98530452008-08-12 20:41:56 +0000859 return 0;
Ted Kremenek940b1d82008-04-10 23:44:06 +0000860 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000861}
862
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000863RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000864
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000865 FunctionType* FT =
866 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000867
Ted Kremenek64e859a2008-10-22 20:54:52 +0000868 if (FT) {
869 QualType ResTy = FT->getResultType();
870
871 if (!isCFRefType(ResTy) && !isCGRefType(ResTy))
872 return getPersistentSummary(RetEffect::MakeNoRet());
873 }
874
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000875 assert (ScratchArgs.empty());
Ted Kremenek070a8252008-07-09 18:11:16 +0000876
877 if (FD->getIdentifier() == CFDictionaryCreateII) {
878 ScratchArgs.push_back(std::make_pair(1, DoNothingByRef));
879 ScratchArgs.push_back(std::make_pair(2, DoNothingByRef));
880 }
881
Ted Kremeneka7344702008-06-23 18:02:52 +0000882 return getPersistentSummary(RetEffect::MakeOwned(true));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000883}
884
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000885RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000886
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000887 FunctionType* FT =
888 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000889
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000890 if (FT) {
891 QualType RetTy = FT->getResultType();
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000892
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000893 // FIXME: For now we assume that all pointer types returned are referenced
894 // counted. Since this is the "Get" rule, we assume non-ownership, which
895 // works fine for things that are not reference counted. We do this because
896 // some generic data structures return "void*". We need something better
897 // in the future.
898
899 if (!isCFRefType(RetTy) && !RetTy->isPointerType())
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000900 return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000901 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000902
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000903 // FIXME: Add special-cases for functions that retain/release. For now
904 // just handle the default case.
905
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000906 assert (ScratchArgs.empty());
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000907 return getPersistentSummary(RetEffect::MakeNotOwned(), DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000908}
909
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000910//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000911// Summary creation for Selectors.
912//===----------------------------------------------------------------------===//
913
Ted Kremenek1bffd742008-05-06 15:44:25 +0000914RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000915RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) {
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000916 assert(ScratchArgs.empty());
917
918 RetainSummary* Summ =
Ted Kremenek9c32d082008-05-06 00:30:21 +0000919 getPersistentSummary(RetEffect::MakeReceiverAlias());
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000920
Ted Kremenek553cf182008-06-25 21:21:56 +0000921 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000922 return Summ;
923}
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000924
Ted Kremenek553cf182008-06-25 21:21:56 +0000925
Ted Kremenek1bffd742008-05-06 15:44:25 +0000926RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000927RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
928 ObjCInterfaceDecl* ID) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000929
930 Selector S = ME->getSelector();
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000931
Ted Kremenek553cf182008-06-25 21:21:56 +0000932 // Look up a summary in our summary cache.
933 ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000934
Ted Kremenek1f180c32008-06-23 22:21:20 +0000935 if (I != ObjCMethodSummaries.end())
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000936 return I->second;
Ted Kremenek553cf182008-06-25 21:21:56 +0000937
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000938 if (!ME->getType()->isPointerType())
939 return 0;
940
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000941 // "initXXX": pass-through for receiver.
942
943 const char* s = S.getIdentifierInfoForSlot(0)->getName();
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000944 assert (ScratchArgs.empty());
Ted Kremenekaee9e572008-05-06 06:09:09 +0000945
Ted Kremenek0327f772008-06-02 17:14:13 +0000946 if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0)
Ted Kremenek553cf182008-06-25 21:21:56 +0000947 return getInitMethodSummary(ME);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000948
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000949 // "copyXXX", "createXXX", "newXXX": allocators.
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000950
Ted Kremenek84060db2008-05-07 04:25:59 +0000951 if (!isNSType(ME->getReceiver()->getType()))
952 return 0;
953
Ted Kremenek9d1d5702008-10-24 21:22:44 +0000954 if (followsFundamentalRule(s)) {
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000955
956 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremeneka7344702008-06-23 18:02:52 +0000957 : RetEffect::MakeOwned(true);
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000958
959 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenek553cf182008-06-25 21:21:56 +0000960 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000961 return Summ;
962 }
Ted Kremenek1bffd742008-05-06 15:44:25 +0000963
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000964 return 0;
965}
966
Ted Kremenekc8395602008-05-06 21:26:51 +0000967RetainSummary*
Ted Kremenek1f180c32008-06-23 22:21:20 +0000968RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
969 Selector S) {
Ted Kremenekc8395602008-05-06 21:26:51 +0000970
Ted Kremenek553cf182008-06-25 21:21:56 +0000971 // FIXME: Eventually we should properly do class method summaries, but
972 // it requires us being able to walk the type hierarchy. Unfortunately,
973 // we cannot do this with just an IdentifierInfo* for the class name.
974
Ted Kremenekc8395602008-05-06 21:26:51 +0000975 // Look up a summary in our cache of Selectors -> Summaries.
Ted Kremenek553cf182008-06-25 21:21:56 +0000976 ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S);
Ted Kremenekc8395602008-05-06 21:26:51 +0000977
Ted Kremenek1f180c32008-06-23 22:21:20 +0000978 if (I != ObjCClassMethodSummaries.end())
Ted Kremenekc8395602008-05-06 21:26:51 +0000979 return I->second;
980
Ted Kremeneka22cc2f2008-05-06 23:07:13 +0000981 return 0;
Ted Kremenekc8395602008-05-06 21:26:51 +0000982}
983
Ted Kremenek1f180c32008-06-23 22:21:20 +0000984void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9c32d082008-05-06 00:30:21 +0000985
986 assert (ScratchArgs.empty());
987
Ted Kremeneka7344702008-06-23 18:02:52 +0000988 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
989 : RetEffect::MakeOwned(true);
990
Ted Kremenek9c32d082008-05-06 00:30:21 +0000991 RetainSummary* Summ = getPersistentSummary(E);
992
Ted Kremenek553cf182008-06-25 21:21:56 +0000993 // Create the summaries for "alloc", "new", and "allocWithZone:" for
994 // NSObject and its derivatives.
995 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
996 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
997 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Ted Kremenek70a733e2008-07-18 17:24:20 +0000998
999 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001000 addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"),
Ted Kremenek1a804482008-07-18 18:14:26 +00001001 GetNullarySelector("currentHandler", Ctx),
Ted Kremenek6d348932008-10-21 15:53:15 +00001002 getPersistentSummary(RetEffect::MakeNotOwned()));
1003
1004 // Create the [NSAutoreleasePool addObject:] summary.
1005 if (!isGCEnabled()) {
1006 ScratchArgs.push_back(std::make_pair(0, Autorelease));
1007 addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"),
1008 GetUnarySelector("addObject", Ctx),
1009 getPersistentSummary(RetEffect::MakeNoRet(),
1010 DoNothing, DoNothing));
1011 }
Ted Kremenek9c32d082008-05-06 00:30:21 +00001012}
1013
Ted Kremenek1f180c32008-06-23 22:21:20 +00001014void RetainSummaryManager::InitializeMethodSummaries() {
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001015
1016 assert (ScratchArgs.empty());
1017
Ted Kremenekc8395602008-05-06 21:26:51 +00001018 // Create the "init" selector. It just acts as a pass-through for the
1019 // receiver.
Ted Kremenek179064e2008-07-01 17:21:27 +00001020 RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias());
1021 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
Ted Kremenekc8395602008-05-06 21:26:51 +00001022
1023 // The next methods are allocators.
Ted Kremeneka7344702008-06-23 18:02:52 +00001024 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
1025 : RetEffect::MakeOwned(true);
1026
Ted Kremenek179064e2008-07-01 17:21:27 +00001027 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenekc8395602008-05-06 21:26:51 +00001028
1029 // Create the "copy" selector.
Ted Kremenek98530452008-08-12 20:41:56 +00001030 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ);
1031
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001032 // Create the "mutableCopy" selector.
Ted Kremenek553cf182008-06-25 21:21:56 +00001033 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001034
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001035 // Create the "retain" selector.
1036 E = RetEffect::MakeReceiverAlias();
1037 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001038 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001039
1040 // Create the "release" selector.
1041 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001042 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Ted Kremenek299e8152008-05-07 21:17:39 +00001043
1044 // Create the "drain" selector.
1045 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001046 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001047
1048 // Create the "autorelease" selector.
Ted Kremeneke19f4492008-06-30 16:57:41 +00001049 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : Autorelease);
Ted Kremenek553cf182008-06-25 21:21:56 +00001050 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001051
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001052 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek179064e2008-07-01 17:21:27 +00001053 RetainSummary *NSWindowSumm =
1054 getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn);
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001055
1056 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1057 "styleMask", "backing", "defer", NULL);
1058
1059 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1060 "styleMask", "backing", "defer", "screen", NULL);
1061
1062 // For NSPanel (which subclasses NSWindow), allocated objects are not
1063 // self-owned.
1064 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1065 "styleMask", "backing", "defer", NULL);
1066
1067 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1068 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek553cf182008-06-25 21:21:56 +00001069
Ted Kremenek70a733e2008-07-18 17:24:20 +00001070 // Create NSAssertionHandler summaries.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001071 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
1072 "lineNumber", "description", NULL);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001073
Ted Kremenek9e476de2008-08-12 18:30:56 +00001074 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1075 "file", "lineNumber", "description", NULL);
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001076}
1077
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001078//===----------------------------------------------------------------------===//
Ted Kremenek13922612008-04-16 20:40:59 +00001079// Reference-counting logic (typestate + counts).
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001080//===----------------------------------------------------------------------===//
1081
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001082namespace {
1083
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001084class VISIBILITY_HIDDEN RefVal {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001085public:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001086
Ted Kremenek4fd88972008-04-17 18:12:53 +00001087 enum Kind {
1088 Owned = 0, // Owning reference.
1089 NotOwned, // Reference is not owned by still valid (not freed).
1090 Released, // Object has been released.
1091 ReturnedOwned, // Returned object passes ownership to caller.
1092 ReturnedNotOwned, // Return object does not pass ownership to caller.
1093 ErrorUseAfterRelease, // Object used after released.
1094 ErrorReleaseNotOwned, // Release of an object that was not owned.
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001095 ErrorLeak, // A memory leak due to excessive reference counts.
1096 ErrorLeakReturned // A memory leak due to the returning method not having
1097 // the correct naming conventions.
Ted Kremenek4fd88972008-04-17 18:12:53 +00001098 };
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001099
Ted Kremenek4fd88972008-04-17 18:12:53 +00001100private:
1101
1102 Kind kind;
1103 unsigned Cnt;
Ted Kremenek553cf182008-06-25 21:21:56 +00001104 QualType T;
1105
1106 RefVal(Kind k, unsigned cnt, QualType t) : kind(k), Cnt(cnt), T(t) {}
1107 RefVal(Kind k, unsigned cnt = 0) : kind(k), Cnt(cnt) {}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001108
1109public:
Ted Kremenekdb863712008-04-16 22:32:20 +00001110
Ted Kremenek4fd88972008-04-17 18:12:53 +00001111 Kind getKind() const { return kind; }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001112
Ted Kremenek553cf182008-06-25 21:21:56 +00001113 unsigned getCount() const { return Cnt; }
1114 QualType getType() const { return T; }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001115
1116 // Useful predicates.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001117
Ted Kremenek73c750b2008-03-11 18:14:09 +00001118 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
1119
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001120 static bool isLeak(Kind k) { return k >= ErrorLeak; }
Ted Kremenekdb863712008-04-16 22:32:20 +00001121
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001122 bool isOwned() const {
1123 return getKind() == Owned;
1124 }
1125
Ted Kremenekdb863712008-04-16 22:32:20 +00001126 bool isNotOwned() const {
1127 return getKind() == NotOwned;
1128 }
1129
Ted Kremenek4fd88972008-04-17 18:12:53 +00001130 bool isReturnedOwned() const {
1131 return getKind() == ReturnedOwned;
1132 }
1133
1134 bool isReturnedNotOwned() const {
1135 return getKind() == ReturnedNotOwned;
1136 }
1137
1138 bool isNonLeakError() const {
1139 Kind k = getKind();
1140 return isError(k) && !isLeak(k);
1141 }
1142
1143 // State creation: normal state.
1144
Ted Kremenek553cf182008-06-25 21:21:56 +00001145 static RefVal makeOwned(QualType t, unsigned Count = 1) {
1146 return RefVal(Owned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001147 }
1148
Ted Kremenek553cf182008-06-25 21:21:56 +00001149 static RefVal makeNotOwned(QualType t, unsigned Count = 0) {
1150 return RefVal(NotOwned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001151 }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001152
1153 static RefVal makeReturnedOwned(unsigned Count) {
1154 return RefVal(ReturnedOwned, Count);
1155 }
1156
1157 static RefVal makeReturnedNotOwned() {
1158 return RefVal(ReturnedNotOwned);
1159 }
1160
Ted Kremenek4fd88972008-04-17 18:12:53 +00001161 // Comparison, profiling, and pretty-printing.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001162
Ted Kremenek4fd88972008-04-17 18:12:53 +00001163 bool operator==(const RefVal& X) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001164 return kind == X.kind && Cnt == X.Cnt && T == X.T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001165 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001166
Ted Kremenek553cf182008-06-25 21:21:56 +00001167 RefVal operator-(size_t i) const {
1168 return RefVal(getKind(), getCount() - i, getType());
1169 }
1170
1171 RefVal operator+(size_t i) const {
1172 return RefVal(getKind(), getCount() + i, getType());
1173 }
1174
1175 RefVal operator^(Kind k) const {
1176 return RefVal(k, getCount(), getType());
1177 }
1178
1179
Ted Kremenek4fd88972008-04-17 18:12:53 +00001180 void Profile(llvm::FoldingSetNodeID& ID) const {
1181 ID.AddInteger((unsigned) kind);
1182 ID.AddInteger(Cnt);
Ted Kremenek553cf182008-06-25 21:21:56 +00001183 ID.Add(T);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001184 }
1185
Ted Kremenekf3948042008-03-11 19:44:10 +00001186 void print(std::ostream& Out) const;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001187};
Ted Kremenekf3948042008-03-11 19:44:10 +00001188
1189void RefVal::print(std::ostream& Out) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001190 if (!T.isNull())
1191 Out << "Tracked Type:" << T.getAsString() << '\n';
1192
Ted Kremenekf3948042008-03-11 19:44:10 +00001193 switch (getKind()) {
1194 default: assert(false);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001195 case Owned: {
1196 Out << "Owned";
1197 unsigned cnt = getCount();
1198 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001199 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001200 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001201
Ted Kremenek61b9f872008-04-10 23:09:18 +00001202 case NotOwned: {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001203 Out << "NotOwned";
Ted Kremenek61b9f872008-04-10 23:09:18 +00001204 unsigned cnt = getCount();
1205 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001206 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001207 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001208
Ted Kremenek4fd88972008-04-17 18:12:53 +00001209 case ReturnedOwned: {
1210 Out << "ReturnedOwned";
1211 unsigned cnt = getCount();
1212 if (cnt) Out << " (+ " << cnt << ")";
1213 break;
1214 }
1215
1216 case ReturnedNotOwned: {
1217 Out << "ReturnedNotOwned";
1218 unsigned cnt = getCount();
1219 if (cnt) Out << " (+ " << cnt << ")";
1220 break;
1221 }
1222
Ted Kremenekf3948042008-03-11 19:44:10 +00001223 case Released:
1224 Out << "Released";
1225 break;
1226
Ted Kremenekdb863712008-04-16 22:32:20 +00001227 case ErrorLeak:
1228 Out << "Leaked";
1229 break;
1230
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001231 case ErrorLeakReturned:
1232 Out << "Leaked (Bad naming)";
1233 break;
1234
Ted Kremenekf3948042008-03-11 19:44:10 +00001235 case ErrorUseAfterRelease:
1236 Out << "Use-After-Release [ERROR]";
1237 break;
1238
1239 case ErrorReleaseNotOwned:
1240 Out << "Release of Not-Owned [ERROR]";
1241 break;
1242 }
1243}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001244
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001245} // end anonymous namespace
1246
1247//===----------------------------------------------------------------------===//
1248// RefBindings - State used to track object reference counts.
1249//===----------------------------------------------------------------------===//
1250
1251typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001252static int RefBIndex = 0;
1253
1254namespace clang {
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001255 template<>
1256 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
1257 static inline void* GDMIndex() { return &RefBIndex; }
1258 };
1259}
Ted Kremenek6d348932008-10-21 15:53:15 +00001260
1261//===----------------------------------------------------------------------===//
1262// ARBindings - State used to track objects in autorelease pools.
1263//===----------------------------------------------------------------------===//
1264
1265typedef llvm::ImmutableSet<SymbolID> ARPoolContents;
1266typedef llvm::ImmutableList< std::pair<SymbolID, ARPoolContents*> > ARBindings;
1267static int AutoRBIndex = 0;
1268
1269namespace clang {
1270 template<>
1271 struct GRStateTrait<ARBindings> : public GRStatePartialTrait<ARBindings> {
1272 static inline void* GDMIndex() { return &AutoRBIndex; }
1273 };
1274}
1275
Ted Kremenek13922612008-04-16 20:40:59 +00001276//===----------------------------------------------------------------------===//
1277// Transfer functions.
1278//===----------------------------------------------------------------------===//
1279
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001280namespace {
1281
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001282class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenek8dd56462008-04-18 03:39:05 +00001283public:
Ted Kremenek553cf182008-06-25 21:21:56 +00001284 // Type definitions.
Ted Kremenek8dd56462008-04-18 03:39:05 +00001285 typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolID> >
1286 ReleasesNotOwnedTy;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001287
Ted Kremenek8dd56462008-04-18 03:39:05 +00001288 typedef ReleasesNotOwnedTy UseAfterReleasesTy;
1289
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001290 typedef llvm::DenseMap<GRExprEngine::NodeTy*,
1291 std::vector<std::pair<SymbolID,bool> >*>
Ted Kremenekdb863712008-04-16 22:32:20 +00001292 LeaksTy;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001293
Ted Kremenekae6814e2008-08-13 21:24:49 +00001294 class BindingsPrinter : public GRState::Printer {
Ted Kremenekf3948042008-03-11 19:44:10 +00001295 public:
Ted Kremenekae6814e2008-08-13 21:24:49 +00001296 virtual void Print(std::ostream& Out, const GRState* state,
1297 const char* nl, const char* sep);
Ted Kremenekf3948042008-03-11 19:44:10 +00001298 };
Ted Kremenek8dd56462008-04-18 03:39:05 +00001299
1300private:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001301 RetainSummaryManager Summaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001302 const LangOptions& LOpts;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001303
Ted Kremenek9e476de2008-08-12 18:30:56 +00001304 UseAfterReleasesTy UseAfterReleases;
1305 ReleasesNotOwnedTy ReleasesNotOwned;
1306 LeaksTy Leaks;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001307
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001308 RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001309 RefVal::Kind& hasErr, RefBindings::Factory& RefBFactory);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001310
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001311 RefVal::Kind& Update(GRStateRef& state, SymbolID sym, RefVal V,
1312 ArgEffect E, RefVal::Kind& hasErr) {
1313
1314 state = state.set<RefBindings>(Update(state.get<RefBindings>(), sym, V,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001315 E, hasErr,
1316 state.get_context<RefBindings>()));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001317 return hasErr;
1318 }
1319
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001320 void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1321 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001322 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001323 ExplodedNode<GRState>* Pred,
1324 const GRState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001325 RefVal::Kind hasErr, SymbolID Sym);
Ted Kremenekdb863712008-04-16 22:32:20 +00001326
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001327 std::pair<GRStateRef, bool>
1328 HandleSymbolDeath(GRStateManager& VMgr, const GRState* St,
1329 const Decl* CD, SymbolID sid, RefVal V, bool& hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001330
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001331public:
Ted Kremenek13922612008-04-16 20:40:59 +00001332
Ted Kremenek78d46242008-07-22 16:21:24 +00001333 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek377e2302008-04-29 05:33:51 +00001334 : Summaries(Ctx, gcenabled),
Ted Kremenek9e476de2008-08-12 18:30:56 +00001335 LOpts(lopts) {}
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001336
Ted Kremenek8dd56462008-04-18 03:39:05 +00001337 virtual ~CFRefCount() {
1338 for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I)
1339 delete I->second;
1340 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001341
1342 virtual void RegisterChecks(GRExprEngine& Eng);
Ted Kremenekf3948042008-03-11 19:44:10 +00001343
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001344 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1345 Printers.push_back(new BindingsPrinter());
Ted Kremenekf3948042008-03-11 19:44:10 +00001346 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001347
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001348 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenek072192b2008-04-30 23:47:44 +00001349 const LangOptions& getLangOptions() const { return LOpts; }
1350
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001351 // Calls.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001352
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001353 void EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001354 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001355 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001356 Expr* Ex,
1357 Expr* Receiver,
1358 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001359 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001360 ExplodedNode<GRState>* Pred);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001361
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001362 virtual void EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek199e1a02008-03-12 21:06:49 +00001363 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001364 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001365 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001366 ExplodedNode<GRState>* Pred);
Ted Kremenekfa34b332008-04-09 01:10:13 +00001367
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001368
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001369 virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001370 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001371 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001372 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001373 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001374
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001375 bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001376 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001377 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001378 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001379 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001380
Ted Kremenek13922612008-04-16 20:40:59 +00001381 // Stores.
1382
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001383 virtual void EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001384 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001385 GRStmtNodeBuilder<GRState>& Builder,
1386 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001387 const GRState* St, SVal TargetLV, SVal Val);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001388 // End-of-path.
1389
1390 virtual void EvalEndPath(GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001391 GREndPathNodeBuilder<GRState>& Builder);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001392
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001393 virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001394 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001395 GRStmtNodeBuilder<GRState>& Builder,
1396 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001397 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001398 const GRState* St,
1399 const GRStateManager::DeadSymbolsTy& Dead);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001400 // Return statements.
1401
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001402 virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001403 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001404 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001405 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001406 ExplodedNode<GRState>* Pred);
Ted Kremenekcb612922008-04-18 19:23:43 +00001407
1408 // Assumptions.
1409
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001410 virtual const GRState* EvalAssume(GRStateManager& VMgr,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001411 const GRState* St, SVal Cond,
Ted Kremenek4323a572008-07-10 22:03:41 +00001412 bool Assumption, bool& isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00001413
Ted Kremenekfa34b332008-04-09 01:10:13 +00001414 // Error iterators.
1415
1416 typedef UseAfterReleasesTy::iterator use_after_iterator;
1417 typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
Ted Kremenek989d5192008-04-17 23:43:50 +00001418 typedef LeaksTy::iterator leaks_iterator;
Ted Kremenekfa34b332008-04-09 01:10:13 +00001419
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001420 use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
1421 use_after_iterator use_after_end() { return UseAfterReleases.end(); }
Ted Kremenekfa34b332008-04-09 01:10:13 +00001422
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001423 bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
1424 bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
Ted Kremenek989d5192008-04-17 23:43:50 +00001425
1426 leaks_iterator leaks_begin() { return Leaks.begin(); }
1427 leaks_iterator leaks_end() { return Leaks.end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001428};
1429
1430} // end anonymous namespace
1431
Ted Kremenek8dd56462008-04-18 03:39:05 +00001432
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001433
1434
Ted Kremenekae6814e2008-08-13 21:24:49 +00001435void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state,
1436 const char* nl, const char* sep) {
1437
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001438 RefBindings B = state->get<RefBindings>();
Ted Kremenekf3948042008-03-11 19:44:10 +00001439
Ted Kremenekae6814e2008-08-13 21:24:49 +00001440 if (!B.isEmpty())
Ted Kremenekf3948042008-03-11 19:44:10 +00001441 Out << sep << nl;
1442
1443 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1444 Out << (*I).first << " : ";
1445 (*I).second.print(Out);
1446 Out << nl;
1447 }
1448}
1449
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001450static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) {
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001451 return Summ ? Summ->getArg(idx) : MayEscape;
Ted Kremenekf9561e52008-04-11 20:23:24 +00001452}
1453
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001454static inline RetEffect GetRetEffect(RetainSummary* Summ) {
1455 return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
Ted Kremenekf9561e52008-04-11 20:23:24 +00001456}
1457
Ted Kremenek14993892008-05-06 02:41:27 +00001458static inline ArgEffect GetReceiverE(RetainSummary* Summ) {
1459 return Summ ? Summ->getReceiverEffect() : DoNothing;
1460}
1461
Ted Kremenek70a733e2008-07-18 17:24:20 +00001462static inline bool IsEndPath(RetainSummary* Summ) {
1463 return Summ ? Summ->isEndPath() : false;
1464}
1465
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001466void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1467 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001468 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001469 ExplodedNode<GRState>* Pred,
1470 const GRState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001471 RefVal::Kind hasErr, SymbolID Sym) {
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001472 Builder.BuildSinks = true;
1473 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
1474
1475 if (!N) return;
1476
1477 switch (hasErr) {
1478 default: assert(false);
1479 case RefVal::ErrorUseAfterRelease:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001480 UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001481 break;
1482
1483 case RefVal::ErrorReleaseNotOwned:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001484 ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001485 break;
1486 }
1487}
1488
Ted Kremenek553cf182008-06-25 21:21:56 +00001489/// GetReturnType - Used to get the return type of a message expression or
1490/// function call with the intention of affixing that type to a tracked symbol.
1491/// While the the return type can be queried directly from RetEx, when
1492/// invoking class methods we augment to the return type to be that of
1493/// a pointer to the class (as opposed it just being id).
1494static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) {
1495
1496 QualType RetTy = RetE->getType();
1497
1498 // FIXME: We aren't handling id<...>.
Chris Lattner8b51fd72008-07-26 22:36:27 +00001499 const PointerType* PT = RetTy->getAsPointerType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001500 if (!PT)
1501 return RetTy;
1502
1503 // If RetEx is not a message expression just return its type.
1504 // If RetEx is a message expression, return its types if it is something
1505 /// more specific than id.
1506
1507 ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE);
1508
1509 if (!ME || !Ctx.isObjCIdType(PT->getPointeeType()))
1510 return RetTy;
1511
1512 ObjCInterfaceDecl* D = ME->getClassInfo().first;
1513
1514 // At this point we know the return type of the message expression is id.
1515 // If we have an ObjCInterceDecl, we know this is a call to a class method
1516 // whose type we can resolve. In such cases, promote the return type to
1517 // Class*.
1518 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
1519}
1520
1521
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001522void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001523 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001524 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001525 Expr* Ex,
1526 Expr* Receiver,
1527 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001528 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001529 ExplodedNode<GRState>* Pred) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001530
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001531 // Get the state.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001532 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001533 ASTContext& Ctx = Eng.getStateManager().getContext();
Ted Kremenek14993892008-05-06 02:41:27 +00001534
1535 // Evaluate the effect of the arguments.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001536 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001537 unsigned idx = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001538 Expr* ErrorExpr = NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001539 SymbolID ErrorSym = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001540
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001541 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001542 SVal V = state.GetSVal(*I);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001543
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001544 if (isa<loc::SymbolVal>(V)) {
1545 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001546 if (RefBindings::data_type* T = state.get<RefBindings>(Sym))
1547 if (Update(state, Sym, *T, GetArgE(Summ, idx), hasErr)) {
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001548 ErrorExpr = *I;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001549 ErrorSym = Sym;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001550 break;
1551 }
Ted Kremenekb8873552008-04-11 20:51:02 +00001552 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001553 else if (isa<Loc>(V)) {
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001554#if 0
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001555 // Nuke all arguments passed by reference.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001556 StateMgr.Unbind(StVals, cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001557#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001558 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenek070a8252008-07-09 18:11:16 +00001559
1560 if (GetArgE(Summ, idx) == DoNothingByRef)
1561 continue;
1562
1563 // Invalidate the value of the variable passed by reference.
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001564
1565 // FIXME: Either this logic should also be replicated in GRSimpleVals
1566 // or should be pulled into a separate "constraint engine."
Ted Kremenek070a8252008-07-09 18:11:16 +00001567
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001568 // FIXME: We can have collisions on the conjured symbol if the
1569 // expression *I also creates conjured symbols. We probably want
1570 // to identify conjured symbols by an expression pair: the enclosing
1571 // expression (the context) and the expression itself. This should
Ted Kremenek070a8252008-07-09 18:11:16 +00001572 // disambiguate conjured symbols.
1573
1574 // Is the invalidated variable something that we were tracking?
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001575 SVal X = state.GetSVal(*MR);
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001576
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001577 if (isa<loc::SymbolVal>(X)) {
1578 SymbolID Sym = cast<loc::SymbolVal>(X).getSymbol();
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001579 state = state.remove<RefBindings>(Sym);
Ted Kremenek070a8252008-07-09 18:11:16 +00001580 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001581
Ted Kremenek993f1c72008-10-17 20:28:54 +00001582 const TypedRegion* R = dyn_cast<TypedRegion>(MR->getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +00001583 if (R) {
1584 // Set the value of the variable to be a conjured symbol.
1585 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001586 QualType T = R->getType(Ctx);
Ted Kremenek9e240492008-10-04 05:50:14 +00001587
Ted Kremenekfd301942008-10-17 22:23:12 +00001588 // FIXME: handle structs.
1589 if (T->isIntegerType() || Loc::IsLocType(T)) {
1590 SymbolID NewSym =
1591 Eng.getSymbolManager().getConjuredSymbol(*I, T, Count);
1592
1593 state = state.SetSVal(*MR,
1594 Loc::IsLocType(T)
1595 ? cast<SVal>(loc::SymbolVal(NewSym))
1596 : cast<SVal>(nonloc::SymbolVal(NewSym)));
1597 }
1598 else {
1599 state = state.SetSVal(*MR, UnknownVal());
1600 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001601 }
1602 else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001603 state = state.SetSVal(*MR, UnknownVal());
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001604 }
1605 else {
1606 // Nuke all other arguments passed by reference.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001607 state = state.Unbind(cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001608 }
1609#endif
Ted Kremenekb8873552008-04-11 20:51:02 +00001610 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001611 else if (isa<nonloc::LocAsInteger>(V))
1612 state = state.Unbind(cast<nonloc::LocAsInteger>(V).getLoc());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001613 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001614
Ted Kremenek553cf182008-06-25 21:21:56 +00001615 // Evaluate the effect on the message receiver.
Ted Kremenek14993892008-05-06 02:41:27 +00001616 if (!ErrorExpr && Receiver) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001617 SVal V = state.GetSVal(Receiver);
1618 if (isa<loc::SymbolVal>(V)) {
1619 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001620 if (const RefVal* T = state.get<RefBindings>(Sym))
1621 if (Update(state, Sym, *T, GetReceiverE(Summ), hasErr)) {
Ted Kremenek14993892008-05-06 02:41:27 +00001622 ErrorExpr = Receiver;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001623 ErrorSym = Sym;
Ted Kremenek14993892008-05-06 02:41:27 +00001624 }
Ted Kremenek14993892008-05-06 02:41:27 +00001625 }
1626 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001627
Ted Kremenek553cf182008-06-25 21:21:56 +00001628 // Process any errors.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001629 if (hasErr) {
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001630 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001631 hasErr, ErrorSym);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001632 return;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001633 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001634
Ted Kremenek70a733e2008-07-18 17:24:20 +00001635 // Consult the summary for the return value.
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001636 RetEffect RE = GetRetEffect(Summ);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001637
1638 switch (RE.getKind()) {
1639 default:
1640 assert (false && "Unhandled RetEffect."); break;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001641
Ted Kremenekfd301942008-10-17 22:23:12 +00001642 case RetEffect::NoRet: {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001643
Ted Kremenekf9561e52008-04-11 20:23:24 +00001644 // Make up a symbol for the return value (not reference counted).
Ted Kremenekb8873552008-04-11 20:51:02 +00001645 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
1646 // should compose behavior, not copy it.
Ted Kremenekf9561e52008-04-11 20:23:24 +00001647
Ted Kremenekfd301942008-10-17 22:23:12 +00001648 // FIXME: We eventually should handle structs and other compound types
1649 // that are returned by value.
1650
1651 QualType T = Ex->getType();
1652
1653 if (T->isIntegerType() || Loc::IsLocType(T)) {
Ted Kremenekf9561e52008-04-11 20:23:24 +00001654 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001655 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001656
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001657 SVal X = Loc::IsLocType(Ex->getType())
1658 ? cast<SVal>(loc::SymbolVal(Sym))
1659 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenekf9561e52008-04-11 20:23:24 +00001660
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001661 state = state.SetSVal(Ex, X, false);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001662 }
1663
Ted Kremenek940b1d82008-04-10 23:44:06 +00001664 break;
Ted Kremenekfd301942008-10-17 22:23:12 +00001665 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00001666
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001667 case RetEffect::Alias: {
Ted Kremenek553cf182008-06-25 21:21:56 +00001668 unsigned idx = RE.getIndex();
Ted Kremenek55499762008-06-17 02:43:46 +00001669 assert (arg_end >= arg_beg);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001670 assert (idx < (unsigned) (arg_end - arg_beg));
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001671 SVal V = state.GetSVal(*(arg_beg+idx));
1672 state = state.SetSVal(Ex, V, false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001673 break;
1674 }
1675
Ted Kremenek14993892008-05-06 02:41:27 +00001676 case RetEffect::ReceiverAlias: {
1677 assert (Receiver);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001678 SVal V = state.GetSVal(Receiver);
1679 state = state.SetSVal(Ex, V, false);
Ted Kremenek14993892008-05-06 02:41:27 +00001680 break;
1681 }
1682
Ted Kremeneka7344702008-06-23 18:02:52 +00001683 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001684 case RetEffect::OwnedSymbol: {
1685 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001686 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001687 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001688
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001689 state = state.set<RefBindings>(Sym, RefVal::makeOwned(RetT));
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001690 state = state.SetSVal(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001691
1692#if 0
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001693 RefBindings B = GetRefBindings(StImpl);
Ted Kremenek553cf182008-06-25 21:21:56 +00001694 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(RetT)));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001695#endif
1696
Ted Kremeneka7344702008-06-23 18:02:52 +00001697 // FIXME: Add a flag to the checker where allocations are allowed to fail.
1698 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol)
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001699 state = state.AddNE(Sym, Eng.getBasicVals().getZeroWithPtrWidth());
Ted Kremeneka7344702008-06-23 18:02:52 +00001700
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001701 break;
1702 }
1703
1704 case RetEffect::NotOwnedSymbol: {
1705 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001706 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001707 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001708
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001709 state = state.set<RefBindings>(Sym, RefVal::makeNotOwned(RetT));
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001710 state = state.SetSVal(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001711 break;
1712 }
1713 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001714
Ted Kremenek70a733e2008-07-18 17:24:20 +00001715 // Is this a sink?
1716 if (IsEndPath(Summ))
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001717 Builder.MakeSinkNode(Dst, Ex, Pred, state);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001718 else
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001719 Builder.MakeNode(Dst, Ex, Pred, state);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001720}
1721
1722
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001723void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001724 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001725 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001726 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001727 ExplodedNode<GRState>* Pred) {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001728
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001729 RetainSummary* Summ = !isa<loc::FuncVal>(L) ? 0
1730 : Summaries.getSummary(cast<loc::FuncVal>(L).getDecl());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001731
1732 EvalSummary(Dst, Eng, Builder, CE, 0, Summ,
1733 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenek2fff37e2008-03-06 00:08:09 +00001734}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001735
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001736void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001737 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001738 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001739 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001740 ExplodedNode<GRState>* Pred) {
Ted Kremenekb3095252008-05-06 04:20:12 +00001741 RetainSummary* Summ;
Ted Kremenek9040c652008-05-01 21:31:50 +00001742
Ted Kremenek553cf182008-06-25 21:21:56 +00001743 if (Expr* Receiver = ME->getReceiver()) {
1744 // We need the type-information of the tracked receiver object
1745 // Retrieve it from the state.
1746 ObjCInterfaceDecl* ID = 0;
1747
1748 // FIXME: Wouldn't it be great if this code could be reduced? It's just
1749 // a chain of lookups.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001750 const GRState* St = Builder.GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001751 SVal V = Eng.getStateManager().GetSVal(St, Receiver );
Ted Kremenek553cf182008-06-25 21:21:56 +00001752
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001753 if (isa<loc::SymbolVal>(V)) {
1754 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek553cf182008-06-25 21:21:56 +00001755
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001756 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001757 QualType Ty = T->getType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001758
1759 if (const PointerType* PT = Ty->getAsPointerType()) {
1760 QualType PointeeTy = PT->getPointeeType();
1761
1762 if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy))
1763 ID = IT->getDecl();
1764 }
1765 }
1766 }
1767
1768 Summ = Summaries.getMethodSummary(ME, ID);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001769
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001770 // Special-case: are we sending a mesage to "self"?
1771 // This is a hack. When we have full-IP this should be removed.
1772 if (!Summ) {
1773 ObjCMethodDecl* MD =
1774 dyn_cast<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl());
1775
1776 if (MD) {
1777 if (Expr* Receiver = ME->getReceiver()) {
1778 SVal X = Eng.getStateManager().GetSVal(St, Receiver);
1779 if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X))
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001780 if (L->getRegion() == Eng.getStateManager().getSelfRegion(St)) {
1781 // Create a summmary where all of the arguments "StopTracking".
1782 Summ = Summaries.getPersistentSummary(RetEffect::MakeNoRet(),
1783 DoNothing,
1784 StopTracking);
1785 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001786 }
1787 }
1788 }
Ted Kremenek553cf182008-06-25 21:21:56 +00001789 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001790 else
Ted Kremenek1f180c32008-06-23 22:21:20 +00001791 Summ = Summaries.getClassMethodSummary(ME->getClassName(),
1792 ME->getSelector());
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001793
Ted Kremenekb3095252008-05-06 04:20:12 +00001794 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ,
1795 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001796}
Ted Kremenekb3095252008-05-06 04:20:12 +00001797
Ted Kremenek13922612008-04-16 20:40:59 +00001798// Stores.
1799
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001800void CFRefCount::EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001801 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001802 GRStmtNodeBuilder<GRState>& Builder,
1803 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001804 const GRState* St, SVal TargetLV, SVal Val) {
Ted Kremenek13922612008-04-16 20:40:59 +00001805
1806 // Check if we have a binding for "Val" and if we are storing it to something
1807 // we don't understand or otherwise the value "escapes" the function.
1808
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001809 if (!isa<loc::SymbolVal>(Val))
Ted Kremenek13922612008-04-16 20:40:59 +00001810 return;
1811
1812 // Are we storing to something that causes the value to "escape"?
1813
1814 bool escapes = false;
1815
Ted Kremeneka496d162008-10-18 03:49:51 +00001816 // A value escapes in three possible cases (this may change):
1817 //
1818 // (1) we are binding to something that is not a memory region.
1819 // (2) we are binding to a memregion that does not have stack storage
1820 // (3) we are binding to a memregion with stack storage that the store
1821 // does not understand.
1822
1823 SymbolID Sym = cast<loc::SymbolVal>(Val).getSymbol();
1824 GRStateRef state(St, Eng.getStateManager());
1825
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001826 if (!isa<loc::MemRegionVal>(TargetLV))
Ted Kremenek13922612008-04-16 20:40:59 +00001827 escapes = true;
Ted Kremenek9e240492008-10-04 05:50:14 +00001828 else {
Ted Kremenek993f1c72008-10-17 20:28:54 +00001829 const MemRegion* R = cast<loc::MemRegionVal>(TargetLV).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00001830 escapes = !Eng.getStateManager().hasStackStorage(R);
Ted Kremeneka496d162008-10-18 03:49:51 +00001831
1832 if (!escapes) {
1833 // To test (3), generate a new state with the binding removed. If it is
1834 // the same state, then it escapes (since the store cannot represent
1835 // the binding).
1836 GRStateRef stateNew = state.SetSVal(cast<Loc>(TargetLV), Val);
1837 escapes = (stateNew == state);
1838 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001839 }
Ted Kremenek13922612008-04-16 20:40:59 +00001840
1841 if (!escapes)
1842 return;
Ted Kremeneka496d162008-10-18 03:49:51 +00001843
1844 // Do we have a reference count binding?
1845 // FIXME: Is this step even needed? We do blow away the binding anyway.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001846 if (!state.get<RefBindings>(Sym))
Ted Kremenek13922612008-04-16 20:40:59 +00001847 return;
1848
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001849 // Nuke the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001850 state = state.remove<RefBindings>(Sym);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001851
Ted Kremenek13922612008-04-16 20:40:59 +00001852 // Hand of the remaining logic to the parent implementation.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001853 GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, state, TargetLV, Val);
Ted Kremenekdb863712008-04-16 22:32:20 +00001854}
1855
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001856// End-of-path.
1857
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001858
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001859std::pair<GRStateRef,bool>
1860CFRefCount::HandleSymbolDeath(GRStateManager& VMgr,
1861 const GRState* St, const Decl* CD,
1862 SymbolID sid,
1863 RefVal V, bool& hasLeak) {
Ted Kremenekdb863712008-04-16 22:32:20 +00001864
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001865 GRStateRef state(St, VMgr);
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001866 assert (!V.isReturnedOwned() || CD &&
1867 "CodeDecl must be available for reporting ReturnOwned errors.");
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001868
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001869 if (V.isReturnedOwned() && V.getCount() == 0)
1870 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
1871 std::string s = MD->getSelector().getName();
1872 if (!followsFundamentalRule(s.c_str())) {
1873 hasLeak = true;
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001874 state = state.set<RefBindings>(sid, V ^ RefVal::ErrorLeakReturned);
1875 return std::make_pair(state, true);
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001876 }
1877 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001878
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001879 // All other cases.
1880
1881 hasLeak = V.isOwned() ||
1882 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001883
Ted Kremenekdb863712008-04-16 22:32:20 +00001884 if (!hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001885 return std::make_pair(state.remove<RefBindings>(sid), false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001886
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001887 return std::make_pair(state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak),
1888 false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001889}
1890
1891void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001892 GREndPathNodeBuilder<GRState>& Builder) {
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001893
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001894 const GRState* St = Builder.getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001895 RefBindings B = St->get<RefBindings>();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001896
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001897 llvm::SmallVector<std::pair<SymbolID, bool>, 10> Leaked;
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001898 const Decl* CodeDecl = &Eng.getGraph().getCodeDecl();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001899
Ted Kremenekdb863712008-04-16 22:32:20 +00001900 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1901 bool hasLeak = false;
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001902
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001903 std::pair<GRStateRef, bool> X =
1904 HandleSymbolDeath(Eng.getStateManager(), St, CodeDecl,
1905 (*I).first, (*I).second, hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001906
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001907 St = X.first;
1908 if (hasLeak) Leaked.push_back(std::make_pair((*I).first, X.second));
Ted Kremenekdb863712008-04-16 22:32:20 +00001909 }
Ted Kremenek652adc62008-04-24 23:57:27 +00001910
1911 if (Leaked.empty())
1912 return;
1913
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001914 ExplodedNode<GRState>* N = Builder.MakeNode(St);
Ted Kremenek4f285152008-04-18 16:30:14 +00001915
Ted Kremenek652adc62008-04-24 23:57:27 +00001916 if (!N)
Ted Kremenek4f285152008-04-18 16:30:14 +00001917 return;
Ted Kremenekcb612922008-04-18 19:23:43 +00001918
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001919 std::vector<std::pair<SymbolID,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek8dd56462008-04-18 03:39:05 +00001920 assert (!LeaksAtNode);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001921 LeaksAtNode = new std::vector<std::pair<SymbolID,bool> >();
Ted Kremenekdb863712008-04-16 22:32:20 +00001922
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001923 for (llvm::SmallVector<std::pair<SymbolID,bool>, 10>::iterator
1924 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek8dd56462008-04-18 03:39:05 +00001925 (*LeaksAtNode).push_back(*I);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001926}
1927
Ted Kremenek652adc62008-04-24 23:57:27 +00001928// Dead symbols.
1929
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001930void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001931 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001932 GRStmtNodeBuilder<GRState>& Builder,
1933 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001934 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001935 const GRState* St,
1936 const GRStateManager::DeadSymbolsTy& Dead) {
Ted Kremenek910e9992008-04-25 01:25:15 +00001937
Ted Kremenek652adc62008-04-24 23:57:27 +00001938 // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
1939
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001940 RefBindings B = St->get<RefBindings>();
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001941 llvm::SmallVector<std::pair<SymbolID,bool>, 10> Leaked;
Ted Kremenek652adc62008-04-24 23:57:27 +00001942
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001943 for (GRStateManager::DeadSymbolsTy::const_iterator
Ted Kremenek652adc62008-04-24 23:57:27 +00001944 I=Dead.begin(), E=Dead.end(); I!=E; ++I) {
1945
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001946 const RefVal* T = B.lookup(*I);
Ted Kremenek652adc62008-04-24 23:57:27 +00001947
1948 if (!T)
1949 continue;
1950
1951 bool hasLeak = false;
1952
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001953 std::pair<GRStateRef, bool> X
1954 = HandleSymbolDeath(Eng.getStateManager(), St, 0, *I, *T, hasLeak);
1955
1956 St = X.first;
Ted Kremenek652adc62008-04-24 23:57:27 +00001957
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001958 if (hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001959 Leaked.push_back(std::make_pair(*I,X.second));
Ted Kremenek652adc62008-04-24 23:57:27 +00001960 }
1961
1962 if (Leaked.empty())
1963 return;
1964
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001965 ExplodedNode<GRState>* N = Builder.MakeNode(Dst, S, Pred, St);
Ted Kremenek652adc62008-04-24 23:57:27 +00001966
1967 if (!N)
1968 return;
1969
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001970 std::vector<std::pair<SymbolID,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek652adc62008-04-24 23:57:27 +00001971 assert (!LeaksAtNode);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001972 LeaksAtNode = new std::vector<std::pair<SymbolID,bool> >();
Ted Kremenek652adc62008-04-24 23:57:27 +00001973
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001974 for (llvm::SmallVector<std::pair<SymbolID,bool>, 10>::iterator
1975 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek652adc62008-04-24 23:57:27 +00001976 (*LeaksAtNode).push_back(*I);
1977}
1978
Ted Kremenek4fd88972008-04-17 18:12:53 +00001979 // Return statements.
1980
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001981void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001982 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001983 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001984 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001985 ExplodedNode<GRState>* Pred) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001986
1987 Expr* RetE = S->getRetValue();
1988 if (!RetE) return;
1989
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001990 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001991 SVal V = state.GetSVal(RetE);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001992
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001993 if (!isa<loc::SymbolVal>(V))
Ted Kremenek4fd88972008-04-17 18:12:53 +00001994 return;
1995
1996 // Get the reference count binding (if any).
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001997 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001998 const RefVal* T = state.get<RefBindings>(Sym);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001999
2000 if (!T)
2001 return;
2002
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002003 // Change the reference count.
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002004 RefVal X = *T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00002005
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002006 switch (X.getKind()) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00002007 case RefVal::Owned: {
2008 unsigned cnt = X.getCount();
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002009 assert (cnt > 0);
2010 X = RefVal::makeReturnedOwned(cnt - 1);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002011 break;
2012 }
2013
2014 case RefVal::NotOwned: {
2015 unsigned cnt = X.getCount();
2016 X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
2017 : RefVal::makeReturnedNotOwned();
2018 break;
2019 }
2020
2021 default:
Ted Kremenek4fd88972008-04-17 18:12:53 +00002022 return;
2023 }
2024
2025 // Update the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002026 state = state.set<RefBindings>(Sym, X);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002027 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002028}
2029
Ted Kremenekcb612922008-04-18 19:23:43 +00002030// Assumptions.
2031
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002032const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr,
2033 const GRState* St,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002034 SVal Cond, bool Assumption,
Ted Kremenek4323a572008-07-10 22:03:41 +00002035 bool& isFeasible) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002036
2037 // FIXME: We may add to the interface of EvalAssume the list of symbols
2038 // whose assumptions have changed. For now we just iterate through the
2039 // bindings and check if any of the tracked symbols are NULL. This isn't
2040 // too bad since the number of symbols we will track in practice are
2041 // probably small and EvalAssume is only called at branches and a few
2042 // other places.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002043 RefBindings B = St->get<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002044
2045 if (B.isEmpty())
2046 return St;
2047
2048 bool changed = false;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002049
2050 GRStateRef state(St, VMgr);
2051 RefBindings::Factory& RefBFactory = state.get_context<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002052
2053 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002054 // Check if the symbol is null (or equal to any constant).
2055 // If this is the case, stop tracking the symbol.
Zhongxing Xu39cfed32008-08-29 14:52:36 +00002056 if (VMgr.getSymVal(St, I.getKey())) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002057 changed = true;
2058 B = RefBFactory.Remove(B, I.getKey());
2059 }
2060 }
2061
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002062 if (changed)
2063 state = state.set<RefBindings>(B);
Ted Kremenekcb612922008-04-18 19:23:43 +00002064
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002065 return state;
Ted Kremenekcb612922008-04-18 19:23:43 +00002066}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002067
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002068RefBindings CFRefCount::Update(RefBindings B, SymbolID sym,
2069 RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002070 RefVal::Kind& hasErr,
2071 RefBindings::Factory& RefBFactory) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002072
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002073 // FIXME: This dispatch can potentially be sped up by unifiying it into
2074 // a single switch statement. Opt for simplicity for now.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002075
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002076 switch (E) {
2077 default:
2078 assert (false && "Unhandled CFRef transition.");
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002079
2080 case MayEscape:
2081 if (V.getKind() == RefVal::Owned) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002082 V = V ^ RefVal::NotOwned;
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002083 break;
2084 }
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002085 // Fall-through.
Ted Kremenek070a8252008-07-09 18:11:16 +00002086 case DoNothingByRef:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002087 case DoNothing:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002088 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002089 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002090 hasErr = V.getKind();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002091 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002092 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002093 return B;
Ted Kremeneke19f4492008-06-30 16:57:41 +00002094
Ted Kremenek80d753f2008-07-01 00:01:02 +00002095 case Autorelease:
Ted Kremenek14993892008-05-06 02:41:27 +00002096 case StopTracking:
2097 return RefBFactory.Remove(B, sym);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002098
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002099 case IncRef:
2100 switch (V.getKind()) {
2101 default:
2102 assert(false);
2103
2104 case RefVal::Owned:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002105 case RefVal::NotOwned:
Ted Kremenek553cf182008-06-25 21:21:56 +00002106 V = V + 1;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002107 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002108 case RefVal::Released:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002109 if (isGCEnabled())
Ted Kremenek553cf182008-06-25 21:21:56 +00002110 V = V ^ RefVal::Owned;
Ted Kremenek65c91652008-04-29 05:44:10 +00002111 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002112 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek65c91652008-04-29 05:44:10 +00002113 hasErr = V.getKind();
2114 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002115 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002116 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002117 break;
2118
Ted Kremenek553cf182008-06-25 21:21:56 +00002119 case SelfOwn:
2120 V = V ^ RefVal::NotOwned;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002121 // Fall-through.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002122 case DecRef:
2123 switch (V.getKind()) {
2124 default:
2125 assert (false);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002126
Ted Kremenek553cf182008-06-25 21:21:56 +00002127 case RefVal::Owned:
2128 V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002129 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002130
Ted Kremenek553cf182008-06-25 21:21:56 +00002131 case RefVal::NotOwned:
2132 if (V.getCount() > 0)
2133 V = V - 1;
Ted Kremenek61b9f872008-04-10 23:09:18 +00002134 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002135 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002136 hasErr = V.getKind();
Ted Kremenek9e476de2008-08-12 18:30:56 +00002137 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002138 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002139
2140 case RefVal::Released:
Ted Kremenek553cf182008-06-25 21:21:56 +00002141 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002142 hasErr = V.getKind();
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002143 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002144 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002145 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002146 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002147 return RefBFactory.Add(B, sym, V);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002148}
2149
Ted Kremenekfa34b332008-04-09 01:10:13 +00002150//===----------------------------------------------------------------------===//
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002151// Error reporting.
Ted Kremenekfa34b332008-04-09 01:10:13 +00002152//===----------------------------------------------------------------------===//
2153
Ted Kremenek8dd56462008-04-18 03:39:05 +00002154namespace {
2155
2156 //===-------------===//
2157 // Bug Descriptions. //
2158 //===-------------===//
2159
Ted Kremenek95cc1ba2008-04-18 20:54:29 +00002160 class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002161 protected:
2162 CFRefCount& TF;
2163
2164 public:
2165 CFRefBug(CFRefCount& tf) : TF(tf) {}
Ted Kremenek072192b2008-04-30 23:47:44 +00002166
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002167 CFRefCount& getTF() { return TF; }
Ted Kremenek789deac2008-05-05 23:16:31 +00002168 const CFRefCount& getTF() const { return TF; }
2169
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002170 virtual bool isLeak() const { return false; }
Ted Kremenek8c036c72008-09-20 04:23:38 +00002171
2172 const char* getCategory() const {
Ted Kremenek062bae02008-09-27 22:02:42 +00002173 return "Memory (Core Foundation/Objective-C)";
Ted Kremenek8c036c72008-09-20 04:23:38 +00002174 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002175 };
2176
2177 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2178 public:
2179 UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
2180
2181 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002182 return "use-after-release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002183 }
2184 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002185 return "Reference-counted object is used after it is released.";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002186 }
2187
2188 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002189 };
2190
2191 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2192 public:
2193 BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
2194
2195 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002196 return "bad release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002197 }
2198 virtual const char* getDescription() const {
2199 return "Incorrect decrement of the reference count of a "
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002200 "CoreFoundation object: "
Ted Kremenek8dd56462008-04-18 03:39:05 +00002201 "The object is not owned at this point by the caller.";
2202 }
2203
2204 virtual void EmitWarnings(BugReporter& BR);
2205 };
2206
2207 class VISIBILITY_HIDDEN Leak : public CFRefBug {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002208 bool isReturn;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002209 public:
2210 Leak(CFRefCount& tf) : CFRefBug(tf) {}
2211
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002212 void setIsReturn(bool x) { isReturn = x; }
2213
Ted Kremenek8dd56462008-04-18 03:39:05 +00002214 virtual const char* getName() const {
Ted Kremenek432af592008-05-06 18:11:36 +00002215
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002216 if (!isReturn) {
2217 if (getTF().isGCEnabled())
2218 return "leak (GC)";
2219
2220 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
2221 return "leak (hybrid MM, non-GC)";
2222
2223 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
2224 return "leak";
2225 }
2226 else {
2227 if (getTF().isGCEnabled())
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002228 return "[naming convention] leak of returned object (GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002229
2230 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002231 return "[naming convention] leak of returned object (hybrid MM, "
2232 "non-GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002233
2234 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002235 return "[naming convention] leak of returned object";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002236 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002237 }
2238
2239 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002240 return "Object leaked";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002241 }
2242
2243 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002244 virtual void GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002245 virtual bool isLeak() const { return true; }
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002246 virtual bool isCached(BugReport& R);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002247 };
2248
2249 //===---------===//
2250 // Bug Reports. //
2251 //===---------===//
2252
2253 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
2254 SymbolID Sym;
2255 public:
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002256 CFRefReport(CFRefBug& D, ExplodedNode<GRState> *n, SymbolID sym)
Ted Kremenek8dd56462008-04-18 03:39:05 +00002257 : RangedBugReport(D, n), Sym(sym) {}
2258
2259 virtual ~CFRefReport() {}
2260
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002261 CFRefBug& getBugType() {
2262 return (CFRefBug&) RangedBugReport::getBugType();
2263 }
2264 const CFRefBug& getBugType() const {
2265 return (const CFRefBug&) RangedBugReport::getBugType();
2266 }
2267
2268 virtual void getRanges(BugReporter& BR, const SourceRange*& beg,
2269 const SourceRange*& end) {
2270
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002271 if (!getBugType().isLeak())
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002272 RangedBugReport::getRanges(BR, beg, end);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002273 else
2274 beg = end = 0;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002275 }
2276
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002277 SymbolID getSymbol() const { return Sym; }
2278
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002279 virtual PathDiagnosticPiece* getEndPath(BugReporter& BR,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002280 ExplodedNode<GRState>* N);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002281
Ted Kremenek072192b2008-04-30 23:47:44 +00002282 virtual std::pair<const char**,const char**> getExtraDescriptiveText();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002283
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002284 virtual PathDiagnosticPiece* VisitNode(ExplodedNode<GRState>* N,
2285 ExplodedNode<GRState>* PrevN,
2286 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002287 BugReporter& BR);
2288 };
2289
2290
2291} // end anonymous namespace
2292
2293void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002294 Eng.Register(new UseAfterRelease(*this));
2295 Eng.Register(new BadRelease(*this));
2296 Eng.Register(new Leak(*this));
2297}
2298
Ted Kremenek072192b2008-04-30 23:47:44 +00002299
2300static const char* Msgs[] = {
2301 "Code is compiled in garbage collection only mode" // GC only
2302 " (the bug occurs with garbage collection enabled).",
2303
2304 "Code is compiled without garbage collection.", // No GC.
2305
2306 "Code is compiled for use with and without garbage collection (GC)."
2307 " The bug occurs with GC enabled.", // Hybrid, with GC.
2308
2309 "Code is compiled for use with and without garbage collection (GC)."
2310 " The bug occurs in non-GC mode." // Hyrbird, without GC/
2311};
2312
2313std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2314 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
2315
2316 switch (TF.getLangOptions().getGCMode()) {
2317 default:
2318 assert(false);
Ted Kremenek31593ac2008-05-01 04:02:04 +00002319
2320 case LangOptions::GCOnly:
2321 assert (TF.isGCEnabled());
Ted Kremenek9e476de2008-08-12 18:30:56 +00002322 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2323
Ted Kremenek072192b2008-04-30 23:47:44 +00002324 case LangOptions::NonGC:
2325 assert (!TF.isGCEnabled());
Ted Kremenek072192b2008-04-30 23:47:44 +00002326 return std::make_pair(&Msgs[1], &Msgs[1]+1);
2327
2328 case LangOptions::HybridGC:
2329 if (TF.isGCEnabled())
2330 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2331 else
2332 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2333 }
2334}
2335
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002336PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<GRState>* N,
2337 ExplodedNode<GRState>* PrevN,
2338 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002339 BugReporter& BR) {
2340
2341 // Check if the type state has changed.
2342
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002343 const GRState* PrevSt = PrevN->getState();
2344 const GRState* CurrSt = N->getState();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002345
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002346 RefBindings PrevB = PrevSt->get<RefBindings>();
2347 RefBindings CurrB = CurrSt->get<RefBindings>();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002348
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002349 const RefVal* PrevT = PrevB.lookup(Sym);
2350 const RefVal* CurrT = CurrB.lookup(Sym);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002351
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002352 if (!CurrT)
2353 return NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002354
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002355 const char* Msg = NULL;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002356 const RefVal& CurrV = *CurrB.lookup(Sym);
Ted Kremenekce48e002008-05-05 17:53:17 +00002357
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002358 if (!PrevT) {
2359
Ted Kremenekce48e002008-05-05 17:53:17 +00002360 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2361
2362 if (CurrV.isOwned()) {
2363
2364 if (isa<CallExpr>(S))
2365 Msg = "Function call returns an object with a +1 retain count"
2366 " (owning reference).";
2367 else {
2368 assert (isa<ObjCMessageExpr>(S));
2369 Msg = "Method returns an object with a +1 retain count"
2370 " (owning reference).";
2371 }
2372 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002373 else {
2374 assert (CurrV.isNotOwned());
Ted Kremenekce48e002008-05-05 17:53:17 +00002375
2376 if (isa<CallExpr>(S))
2377 Msg = "Function call returns an object with a +0 retain count"
2378 " (non-owning reference).";
2379 else {
2380 assert (isa<ObjCMessageExpr>(S));
2381 Msg = "Method returns an object with a +0 retain count"
2382 " (non-owning reference).";
2383 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002384 }
Ted Kremenekce48e002008-05-05 17:53:17 +00002385
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002386 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2387 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2388
2389 if (Expr* Exp = dyn_cast<Expr>(S))
2390 P->addRange(Exp->getSourceRange());
2391
2392 return P;
2393 }
2394
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002395 // Determine if the typestate has changed.
2396 RefVal PrevV = *PrevB.lookup(Sym);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002397
2398 if (PrevV == CurrV)
2399 return NULL;
2400
2401 // The typestate has changed.
2402
2403 std::ostringstream os;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002404 std::string s;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002405
2406 switch (CurrV.getKind()) {
2407 case RefVal::Owned:
2408 case RefVal::NotOwned:
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002409
2410 if (PrevV.getCount() == CurrV.getCount())
2411 return 0;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002412
2413 if (PrevV.getCount() > CurrV.getCount())
2414 os << "Reference count decremented.";
2415 else
2416 os << "Reference count incremented.";
2417
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002418 if (unsigned Count = CurrV.getCount()) {
Ted Kremenekce48e002008-05-05 17:53:17 +00002419
2420 os << " Object has +" << Count;
Ted Kremenek79c140b2008-04-18 05:32:44 +00002421
Ted Kremenekce48e002008-05-05 17:53:17 +00002422 if (Count > 1)
2423 os << " retain counts.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002424 else
Ted Kremenekce48e002008-05-05 17:53:17 +00002425 os << " retain count.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002426 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002427
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002428 s = os.str();
2429 Msg = s.c_str();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002430
2431 break;
2432
2433 case RefVal::Released:
2434 Msg = "Object released.";
2435 break;
2436
2437 case RefVal::ReturnedOwned:
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002438 Msg = "Object returned to caller as an owning reference (single retain "
2439 "count transferred to caller).";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002440 break;
2441
2442 case RefVal::ReturnedNotOwned:
Ted Kremenekce48e002008-05-05 17:53:17 +00002443 Msg = "Object returned to caller with a +0 (non-owning) retain count.";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002444 break;
2445
2446 default:
2447 return NULL;
2448 }
2449
2450 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2451 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2452 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2453
2454 // Add the range by scanning the children of the statement for any bindings
2455 // to Sym.
2456
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002457 GRStateManager& VSM = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002458
2459 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
2460 if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002461 SVal X = VSM.GetSVal(CurrSt, Exp);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002462
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002463 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&X))
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002464 if (SV->getSymbol() == Sym) {
2465 P->addRange(Exp->getSourceRange()); break;
2466 }
2467 }
2468
2469 return P;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002470}
2471
Ted Kremenek9e240492008-10-04 05:50:14 +00002472namespace {
2473class VISIBILITY_HIDDEN FindUniqueBinding :
2474 public StoreManager::BindingsHandler {
2475 SymbolID Sym;
2476 MemRegion* Binding;
2477 bool First;
2478
2479 public:
2480 FindUniqueBinding(SymbolID sym) : Sym(sym), Binding(0), First(true) {}
2481
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002482 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal val) {
2483 if (const loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002484 if (SV->getSymbol() != Sym)
2485 return true;
2486 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002487 else if (const nonloc::SymbolVal* SV=dyn_cast<nonloc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002488 if (SV->getSymbol() != Sym)
2489 return true;
2490 }
2491 else
2492 return true;
2493
2494 if (Binding) {
2495 First = false;
2496 return false;
2497 }
2498 else
2499 Binding = R;
2500
2501 return true;
2502 }
2503
2504 operator bool() { return First && Binding; }
2505 MemRegion* getRegion() { return Binding; }
2506};
2507}
2508
2509static std::pair<ExplodedNode<GRState>*,MemRegion*>
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002510GetAllocationSite(GRStateManager* StateMgr, ExplodedNode<GRState>* N,
2511 SymbolID Sym) {
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002512
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002513 // Find both first node that referred to the tracked symbol and the
2514 // memory location that value was store to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002515 ExplodedNode<GRState>* Last = N;
Ted Kremenek9e240492008-10-04 05:50:14 +00002516 MemRegion* FirstBinding = 0;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002517
2518 while (N) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002519 const GRState* St = N->getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002520 RefBindings B = St->get<RefBindings>();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002521
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002522 if (!B.lookup(Sym))
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002523 break;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002524
2525 if (StateMgr) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002526 FindUniqueBinding FB(Sym);
2527 StateMgr->iterBindings(St, FB);
2528 if (FB) FirstBinding = FB.getRegion();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002529 }
2530
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002531 Last = N;
2532 N = N->pred_empty() ? NULL : *(N->pred_begin());
2533 }
2534
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002535 return std::make_pair(Last, FirstBinding);
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002536}
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002537
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002538PathDiagnosticPiece* CFRefReport::getEndPath(BugReporter& br,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002539 ExplodedNode<GRState>* EndN) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002540
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002541 GRBugReporter& BR = cast<GRBugReporter>(br);
2542
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002543 // Tell the BugReporter to report cases when the tracked symbol is
2544 // assigned to different variables, etc.
Ted Kremenekc0959972008-07-02 21:24:01 +00002545 cast<GRBugReporter>(BR).addNotableSymbol(Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002546
2547 if (!getBugType().isLeak())
Ted Kremeneke28565b2008-05-05 18:50:19 +00002548 return RangedBugReport::getEndPath(BR, EndN);
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002549
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002550 // We are a leak. Walk up the graph to get to the first node where the
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002551 // symbol appeared, and also get the first VarDecl that tracked object
2552 // is stored to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002553 ExplodedNode<GRState>* AllocNode = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +00002554 MemRegion* FirstBinding = 0;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002555
2556 llvm::tie(AllocNode, FirstBinding) =
2557 GetAllocationSite(&BR.getStateManager(), EndN, Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002558
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002559 // Get the allocate site.
2560 assert (AllocNode);
2561 Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002562
Ted Kremeneke28565b2008-05-05 18:50:19 +00002563 SourceManager& SMgr = BR.getContext().getSourceManager();
2564 unsigned AllocLine = SMgr.getLogicalLineNumber(FirstStmt->getLocStart());
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002565
Ted Kremeneke28565b2008-05-05 18:50:19 +00002566 // Get the leak site. We may have multiple ExplodedNodes (one with the
2567 // leak) that occur on the same line number; if the node with the leak
2568 // has any immediate predecessor nodes with the same line number, find
2569 // any transitive-successors that have a different statement and use that
2570 // line number instead. This avoids emiting a diagnostic like:
2571 //
2572 // // 'y' is leaked.
2573 // int x = foo(y);
2574 //
2575 // instead we want:
2576 //
2577 // int x = foo(y);
2578 // // 'y' is leaked.
2579
2580 Stmt* S = getStmt(BR); // This is the statement where the leak occured.
2581 assert (S);
2582 unsigned EndLine = SMgr.getLogicalLineNumber(S->getLocStart());
2583
2584 // Look in the *trimmed* graph at the immediate predecessor of EndN. Does
2585 // it occur on the same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002586 PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above;
Ted Kremeneke28565b2008-05-05 18:50:19 +00002587
2588 assert (!EndN->pred_empty()); // Not possible to have 0 predecessors.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002589 ExplodedNode<GRState> *Pred = *(EndN->pred_begin());
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002590 ProgramPoint PredPos = Pred->getLocation();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002591
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002592 if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) {
Ted Kremeneke28565b2008-05-05 18:50:19 +00002593
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002594 Stmt* SPred = PredPS->getStmt();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002595
2596 // Predecessor at same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002597 if (SMgr.getLogicalLineNumber(SPred->getLocStart()) != EndLine) {
2598 Hint = PathDiagnosticPiece::Below;
2599 S = SPred;
2600 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002601 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002602
2603 // Generate the diagnostic.
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002604 FullSourceLoc L( S->getLocStart(), SMgr);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002605 std::ostringstream os;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002606
Ted Kremeneke28565b2008-05-05 18:50:19 +00002607 os << "Object allocated on line " << AllocLine;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002608
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002609 if (FirstBinding)
Ted Kremenek9e240492008-10-04 05:50:14 +00002610 os << " and stored into '" << FirstBinding->getString() << '\'';
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002611
Ted Kremenek9e240492008-10-04 05:50:14 +00002612
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002613 // Get the retain count.
2614 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
2615
2616 if (RV->getKind() == RefVal::ErrorLeakReturned) {
2617 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BR.getGraph().getCodeDecl());
2618 os << " is returned from a method whose name ('"
2619 << MD.getSelector().getName()
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002620 << "') does not contain 'create' or 'copy' or otherwise starts with"
2621 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002622 " in the Memory Management Guide for Cocoa (object leaked).";
2623 }
2624 else
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002625 os << " is no longer referenced after this point and has a retain count of"
2626 " +"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002627 << RV->getCount() << " (object leaked).";
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002628
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002629 return new PathDiagnosticPiece(L, os.str(), Hint);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002630}
2631
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002632void UseAfterRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002633
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002634 for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
2635 E = TF.use_after_end(); I != E; ++I) {
2636
Ted Kremenek8dd56462008-04-18 03:39:05 +00002637 CFRefReport report(*this, I->first, I->second.second);
2638 report.addRange(I->second.first->getSourceRange());
Ted Kremenek75840e12008-04-18 01:56:37 +00002639 BR.EmitWarning(report);
Ted Kremenekfa34b332008-04-09 01:10:13 +00002640 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002641}
2642
2643void BadRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002644
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002645 for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
2646 E = TF.bad_release_end(); I != E; ++I) {
2647
Ted Kremenek8dd56462008-04-18 03:39:05 +00002648 CFRefReport report(*this, I->first, I->second.second);
2649 report.addRange(I->second.first->getSourceRange());
2650 BR.EmitWarning(report);
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002651 }
2652}
Ted Kremenekfa34b332008-04-09 01:10:13 +00002653
Ted Kremenek989d5192008-04-17 23:43:50 +00002654void Leak::EmitWarnings(BugReporter& BR) {
2655
2656 for (CFRefCount::leaks_iterator I = TF.leaks_begin(),
2657 E = TF.leaks_end(); I != E; ++I) {
2658
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002659 std::vector<std::pair<SymbolID, bool> >& SymV = *(I->second);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002660 unsigned n = SymV.size();
2661
2662 for (unsigned i = 0; i < n; ++i) {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002663 setIsReturn(SymV[i].second);
2664 CFRefReport report(*this, I->first, SymV[i].first);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002665 BR.EmitWarning(report);
2666 }
Ted Kremenek989d5192008-04-17 23:43:50 +00002667 }
2668}
2669
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002670void Leak::GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002671 for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end();
2672 I!=E; ++I)
2673 Nodes.push_back(I->first);
2674}
2675
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002676bool Leak::isCached(BugReport& R) {
2677
2678 // Most bug reports are cached at the location where they occured.
2679 // With leaks, we want to unique them by the location where they were
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002680 // allocated, and only report a single path.
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002681
2682 SymbolID Sym = static_cast<CFRefReport&>(R).getSymbol();
2683
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002684 ExplodedNode<GRState>* AllocNode =
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002685 GetAllocationSite(0, R.getEndNode(), Sym).first;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002686
2687 if (!AllocNode)
2688 return false;
2689
2690 return BugTypeCacheLocation::isCached(AllocNode->getLocation());
2691}
2692
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002693//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +00002694// Transfer function creation for external clients.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002695//===----------------------------------------------------------------------===//
2696
Ted Kremenek072192b2008-04-30 23:47:44 +00002697GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
2698 const LangOptions& lopts) {
Ted Kremenek78d46242008-07-22 16:21:24 +00002699 return new CFRefCount(Ctx, GCEnabled, lopts);
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +00002700}