blob: 9639ad98fa69eedcfed1ea8fbdf61ec0e498ad15 [file] [log] [blame]
Chris Lattner7a513132008-03-15 23:59:48 +00001// CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
Ted Kremenekea6507f2008-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 Greif3a8edd82008-03-06 10:40:09 +000010// This file defines the methods for CFRefCount, which implements
Ted Kremenekea6507f2008-03-06 00:08:09 +000011// a reference count checker for Core Foundation (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenekb0f87c42008-04-30 23:47:44 +000015#include "clang/Basic/LangOptions.h"
Ted Kremenek1097b4c2008-05-01 23:13:35 +000016#include "clang/Basic/SourceManager.h"
Ted Kremeneke68c0fc2009-02-14 01:43:44 +000017#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek87aab6c2008-08-17 03:20:02 +000018#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenekc27815c2008-03-31 18:26:32 +000019#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenekea6507f2008-03-06 00:08:09 +000020#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekce8e8812008-04-09 01:10:13 +000021#include "clang/Analysis/PathDiagnostic.h"
22#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek4e9d4b52009-02-14 03:16:10 +000023#include "clang/Analysis/PathSensitive/SymbolManager.h"
Ted Kremenek1642bda2009-06-26 00:05:51 +000024#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
Ted Kremenek70a87882009-11-25 22:17:44 +000025#include "clang/Analysis/PathSensitive/CheckerVisitor.h"
Mike Stump11289f42009-09-09 15:08:12 +000026#include "clang/AST/DeclObjC.h"
Ted Kremenekf89dcda2009-11-26 02:38:19 +000027#include "clang/AST/StmtVisitor.h"
Ted Kremenek819e9b62008-03-11 06:39:11 +000028#include "llvm/ADT/DenseMap.h"
29#include "llvm/ADT/FoldingSet.h"
30#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek0747e7e2008-10-21 15:53:15 +000031#include "llvm/ADT/ImmutableList.h"
Ted Kremenekb6cbf282008-05-07 18:36:45 +000032#include "llvm/ADT/StringExtras.h"
Ted Kremenekc812b232008-05-16 18:33:44 +000033#include "llvm/ADT/STLExtras.h"
Ted Kremenek9551ab62008-08-12 20:41:56 +000034#include <stdarg.h>
Ted Kremenekea6507f2008-03-06 00:08:09 +000035
36using namespace clang;
Ted Kremenek01acb622008-10-24 21:18:08 +000037
38//===----------------------------------------------------------------------===//
39// Utility functions.
40//===----------------------------------------------------------------------===//
41
Ted Kremenek01acb622008-10-24 21:18:08 +000042// The "fundamental rule" for naming conventions of methods:
43// (url broken into two lines)
44// http://developer.apple.com/documentation/Cocoa/Conceptual/
45// MemoryMgmt/Tasks/MemoryManagementRules.html
46//
47// "You take ownership of an object if you create it using a method whose name
Mike Stump11289f42009-09-09 15:08:12 +000048// begins with "alloc" or "new" or contains "copy" (for example, alloc,
Ted Kremenek01acb622008-10-24 21:18:08 +000049// newObject, or mutableCopy), or if you send it a retain message. You are
50// responsible for relinquishing ownership of objects you own using release
51// or autorelease. Any other time you receive an object, you must
52// not release it."
53//
Ted Kremenek8a73c712009-02-21 05:13:43 +000054
55using llvm::CStrInCStrNoCase;
Ted Kremenek97ad7b62009-02-21 18:26:02 +000056using llvm::StringsEqualNoCase;
Ted Kremenek8a73c712009-02-21 05:13:43 +000057
58enum NamingConvention { NoConvention, CreateRule, InitRule };
59
60static inline bool isWordEnd(char ch, char prev, char next) {
61 return ch == '\0'
62 || (islower(prev) && isupper(ch)) // xxxC
63 || (isupper(prev) && isupper(ch) && islower(next)) // XXCreate
64 || !isalpha(ch);
65}
Mike Stump11289f42009-09-09 15:08:12 +000066
67static inline const char* parseWord(const char* s) {
Ted Kremenek8a73c712009-02-21 05:13:43 +000068 char ch = *s, prev = '\0';
69 assert(ch != '\0');
70 char next = *(s+1);
71 while (!isWordEnd(ch, prev, next)) {
72 prev = ch;
73 ch = next;
74 next = *((++s)+1);
75 }
76 return s;
77}
78
Ted Kremenek32819772009-05-15 15:49:00 +000079static NamingConvention deriveNamingConvention(Selector S) {
80 IdentifierInfo *II = S.getIdentifierInfoForSlot(0);
Mike Stump11289f42009-09-09 15:08:12 +000081
Ted Kremenek32819772009-05-15 15:49:00 +000082 if (!II)
83 return NoConvention;
Mike Stump11289f42009-09-09 15:08:12 +000084
Daniel Dunbar9d9aa162009-10-17 18:12:45 +000085 const char *s = II->getNameStart();
Mike Stump11289f42009-09-09 15:08:12 +000086
Ted Kremenek8a73c712009-02-21 05:13:43 +000087 // A method/function name may contain a prefix. We don't know it is there,
88 // however, until we encounter the first '_'.
89 bool InPossiblePrefix = true;
90 bool AtBeginning = true;
91 NamingConvention C = NoConvention;
Mike Stump11289f42009-09-09 15:08:12 +000092
Ted Kremenek8a73c712009-02-21 05:13:43 +000093 while (*s != '\0') {
94 // Skip '_'.
95 if (*s == '_') {
96 if (InPossiblePrefix) {
Ted Kremenek90c953e2009-10-20 00:13:00 +000097 // If we already have a convention, return it. Otherwise, skip
98 // the prefix as if it wasn't there.
99 if (C != NoConvention)
100 break;
101
Ted Kremenek8a73c712009-02-21 05:13:43 +0000102 InPossiblePrefix = false;
103 AtBeginning = true;
Ted Kremenek90c953e2009-10-20 00:13:00 +0000104 assert(C == NoConvention);
Ted Kremenek8a73c712009-02-21 05:13:43 +0000105 }
106 ++s;
107 continue;
108 }
Mike Stump11289f42009-09-09 15:08:12 +0000109
Ted Kremenek8a73c712009-02-21 05:13:43 +0000110 // Skip numbers, ':', etc.
111 if (!isalpha(*s)) {
112 ++s;
113 continue;
114 }
Mike Stump11289f42009-09-09 15:08:12 +0000115
Ted Kremenek8a73c712009-02-21 05:13:43 +0000116 const char *wordEnd = parseWord(s);
117 assert(wordEnd > s);
118 unsigned len = wordEnd - s;
Mike Stump11289f42009-09-09 15:08:12 +0000119
Ted Kremenek8a73c712009-02-21 05:13:43 +0000120 switch (len) {
121 default:
122 break;
123 case 3:
124 // Methods starting with 'new' follow the create rule.
Ted Kremenek97ad7b62009-02-21 18:26:02 +0000125 if (AtBeginning && StringsEqualNoCase("new", s, len))
Mike Stump11289f42009-09-09 15:08:12 +0000126 C = CreateRule;
Ted Kremenek8a73c712009-02-21 05:13:43 +0000127 break;
128 case 4:
129 // Methods starting with 'alloc' or contain 'copy' follow the
130 // create rule
Ted Kremenek340fd2d2009-03-13 20:27:06 +0000131 if (C == NoConvention && StringsEqualNoCase("copy", s, len))
Ted Kremenek8a73c712009-02-21 05:13:43 +0000132 C = CreateRule;
133 else // Methods starting with 'init' follow the init rule.
Ted Kremenek97ad7b62009-02-21 18:26:02 +0000134 if (AtBeginning && StringsEqualNoCase("init", s, len))
Ted Kremenek340fd2d2009-03-13 20:27:06 +0000135 C = InitRule;
136 break;
137 case 5:
138 if (AtBeginning && StringsEqualNoCase("alloc", s, len))
139 C = CreateRule;
Ted Kremenek8a73c712009-02-21 05:13:43 +0000140 break;
141 }
Mike Stump11289f42009-09-09 15:08:12 +0000142
Ted Kremenek8a73c712009-02-21 05:13:43 +0000143 // If we aren't in the prefix and have a derived convention then just
144 // return it now.
145 if (!InPossiblePrefix && C != NoConvention)
146 return C;
147
148 AtBeginning = false;
149 s = wordEnd;
150 }
151
152 // We will get here if there wasn't more than one word
153 // after the prefix.
154 return C;
155}
156
Ted Kremenek32819772009-05-15 15:49:00 +0000157static bool followsFundamentalRule(Selector S) {
158 return deriveNamingConvention(S) == CreateRule;
Ted Kremenek2855a932008-11-05 16:54:44 +0000159}
160
Ted Kremenek223a7d52009-04-29 23:03:22 +0000161static const ObjCMethodDecl*
Mike Stump11289f42009-09-09 15:08:12 +0000162ResolveToInterfaceMethodDecl(const ObjCMethodDecl *MD) {
Ted Kremenek223a7d52009-04-29 23:03:22 +0000163 ObjCInterfaceDecl *ID =
164 const_cast<ObjCInterfaceDecl*>(MD->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +0000165
Ted Kremenek223a7d52009-04-29 23:03:22 +0000166 return MD->isInstanceMethod()
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000167 ? ID->lookupInstanceMethod(MD->getSelector())
168 : ID->lookupClassMethod(MD->getSelector());
Ted Kremenek2855a932008-11-05 16:54:44 +0000169}
Ted Kremenek01acb622008-10-24 21:18:08 +0000170
Ted Kremenek884a8992009-05-08 23:09:42 +0000171namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000172class GenericNodeBuilder {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000173 GRStmtNodeBuilder *SNB;
Ted Kremenek884a8992009-05-08 23:09:42 +0000174 Stmt *S;
175 const void *tag;
Zhongxing Xu107f7592009-08-06 12:48:26 +0000176 GREndPathNodeBuilder *ENB;
Ted Kremenek884a8992009-05-08 23:09:42 +0000177public:
Zhongxing Xu107f7592009-08-06 12:48:26 +0000178 GenericNodeBuilder(GRStmtNodeBuilder &snb, Stmt *s,
Ted Kremenek884a8992009-05-08 23:09:42 +0000179 const void *t)
180 : SNB(&snb), S(s), tag(t), ENB(0) {}
Zhongxing Xu107f7592009-08-06 12:48:26 +0000181
182 GenericNodeBuilder(GREndPathNodeBuilder &enb)
Ted Kremenek884a8992009-05-08 23:09:42 +0000183 : SNB(0), S(0), tag(0), ENB(&enb) {}
Mike Stump11289f42009-09-09 15:08:12 +0000184
Zhongxing Xu107f7592009-08-06 12:48:26 +0000185 ExplodedNode *MakeNode(const GRState *state, ExplodedNode *Pred) {
Ted Kremenek884a8992009-05-08 23:09:42 +0000186 if (SNB)
Mike Stump11289f42009-09-09 15:08:12 +0000187 return SNB->generateNode(PostStmt(S, Pred->getLocationContext(), tag),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000188 state, Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000189
Ted Kremenek884a8992009-05-08 23:09:42 +0000190 assert(ENB);
Ted Kremenek9ec08aa2009-05-09 00:44:07 +0000191 return ENB->generateNode(state, Pred);
Ted Kremenek884a8992009-05-08 23:09:42 +0000192 }
193};
194} // end anonymous namespace
195
Ted Kremenekc8bef6a2008-04-09 23:49:11 +0000196//===----------------------------------------------------------------------===//
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000197// Type querying functions.
198//===----------------------------------------------------------------------===//
199
Ted Kremenek7e904222009-01-12 21:45:02 +0000200static bool isRefType(QualType RetTy, const char* prefix,
201 ASTContext* Ctx = 0, const char* name = 0) {
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenek95d18192009-05-12 04:53:03 +0000203 // Recursively walk the typedef stack, allowing typedefs of reference types.
Daniel Dunbaracb5a4b2009-10-17 18:12:53 +0000204 while (TypedefType* TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
Daniel Dunbar07d07852009-10-18 21:17:35 +0000205 llvm::StringRef TDName = TD->getDecl()->getIdentifier()->getName();
Daniel Dunbaracb5a4b2009-10-17 18:12:53 +0000206 if (TDName.startswith(prefix) && TDName.endswith("Ref"))
207 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000208
Daniel Dunbaracb5a4b2009-10-17 18:12:53 +0000209 RetTy = TD->getDecl()->getUnderlyingType();
Ted Kremenek7e904222009-01-12 21:45:02 +0000210 }
211
212 if (!Ctx || !name)
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000213 return false;
Ted Kremenek7e904222009-01-12 21:45:02 +0000214
215 // Is the type void*?
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000216 const PointerType* PT = RetTy->getAs<PointerType>();
Ted Kremenek7e904222009-01-12 21:45:02 +0000217 if (!(PT->getPointeeType().getUnqualifiedType() == Ctx->VoidTy))
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000218 return false;
Ted Kremenek7e904222009-01-12 21:45:02 +0000219
220 // Does the name start with the prefix?
Daniel Dunbar9d9aa162009-10-17 18:12:45 +0000221 return llvm::StringRef(name).startswith(prefix);
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000222}
223
Ted Kremeneka506fec2008-04-17 18:12:53 +0000224//===----------------------------------------------------------------------===//
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000225// Primitives used for constructing summaries for function/method calls.
Ted Kremenekc8bef6a2008-04-09 23:49:11 +0000226//===----------------------------------------------------------------------===//
227
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000228/// ArgEffect is used to summarize a function/method call's effect on a
229/// particular argument.
Ted Kremenekea072e32009-03-17 19:42:23 +0000230enum ArgEffect { Autorelease, Dealloc, DecRef, DecRefMsg, DoNothing,
231 DoNothingByRef, IncRefMsg, IncRef, MakeCollectable, MayEscape,
232 NewAutoreleasePool, SelfOwn, StopTracking };
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000233
Ted Kremenek819e9b62008-03-11 06:39:11 +0000234namespace llvm {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000235template <> struct FoldingSetTrait<ArgEffect> {
236static inline void Profile(const ArgEffect X, FoldingSetNodeID& ID) {
237 ID.AddInteger((unsigned) X);
238}
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000239};
Ted Kremenek819e9b62008-03-11 06:39:11 +0000240} // end llvm namespace
241
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000242/// ArgEffects summarizes the effects of a function/method call on all of
243/// its arguments.
244typedef llvm::ImmutableMap<unsigned,ArgEffect> ArgEffects;
245
Ted Kremenek819e9b62008-03-11 06:39:11 +0000246namespace {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000247
248/// RetEffect is used to summarize a function/method call's behavior with
Mike Stump11289f42009-09-09 15:08:12 +0000249/// respect to its return value.
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000250class RetEffect {
Ted Kremenek819e9b62008-03-11 06:39:11 +0000251public:
Ted Kremenekab4a8b52008-06-23 18:02:52 +0000252 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
Ted Kremenek1272f702009-05-12 20:06:54 +0000253 NotOwnedSymbol, GCNotOwnedSymbol, ReceiverAlias,
254 OwnedWhenTrackedReceiver };
Mike Stump11289f42009-09-09 15:08:12 +0000255
256 enum ObjKind { CF, ObjC, AnyObj };
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000257
Ted Kremenek819e9b62008-03-11 06:39:11 +0000258private:
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000259 Kind K;
260 ObjKind O;
261 unsigned index;
262
263 RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {}
264 RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {}
Mike Stump11289f42009-09-09 15:08:12 +0000265
Ted Kremenek819e9b62008-03-11 06:39:11 +0000266public:
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000267 Kind getKind() const { return K; }
268
269 ObjKind getObjKind() const { return O; }
Mike Stump11289f42009-09-09 15:08:12 +0000270
271 unsigned getIndex() const {
Ted Kremenek819e9b62008-03-11 06:39:11 +0000272 assert(getKind() == Alias);
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000273 return index;
Ted Kremenek819e9b62008-03-11 06:39:11 +0000274 }
Mike Stump11289f42009-09-09 15:08:12 +0000275
Ted Kremenek223a7d52009-04-29 23:03:22 +0000276 bool isOwned() const {
Ted Kremenek1272f702009-05-12 20:06:54 +0000277 return K == OwnedSymbol || K == OwnedAllocatedSymbol ||
278 K == OwnedWhenTrackedReceiver;
Ted Kremenek223a7d52009-04-29 23:03:22 +0000279 }
Mike Stump11289f42009-09-09 15:08:12 +0000280
Ted Kremenek1272f702009-05-12 20:06:54 +0000281 static RetEffect MakeOwnedWhenTrackedReceiver() {
282 return RetEffect(OwnedWhenTrackedReceiver, ObjC);
283 }
Mike Stump11289f42009-09-09 15:08:12 +0000284
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000285 static RetEffect MakeAlias(unsigned Idx) {
286 return RetEffect(Alias, Idx);
287 }
288 static RetEffect MakeReceiverAlias() {
289 return RetEffect(ReceiverAlias);
Mike Stump11289f42009-09-09 15:08:12 +0000290 }
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000291 static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) {
292 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o);
Mike Stump11289f42009-09-09 15:08:12 +0000293 }
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000294 static RetEffect MakeNotOwned(ObjKind o) {
295 return RetEffect(NotOwnedSymbol, o);
Ted Kremeneke6633562009-04-27 19:14:45 +0000296 }
297 static RetEffect MakeGCNotOwned() {
298 return RetEffect(GCNotOwnedSymbol, ObjC);
299 }
Mike Stump11289f42009-09-09 15:08:12 +0000300
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000301 static RetEffect MakeNoRet() {
302 return RetEffect(NoRet);
Ted Kremenekab4a8b52008-06-23 18:02:52 +0000303 }
Mike Stump11289f42009-09-09 15:08:12 +0000304
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000305 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000306 ID.AddInteger((unsigned)K);
307 ID.AddInteger((unsigned)O);
308 ID.AddInteger(index);
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000309 }
Ted Kremenek819e9b62008-03-11 06:39:11 +0000310};
Mike Stump11289f42009-09-09 15:08:12 +0000311
Ted Kremeneka2968e52009-11-13 01:54:21 +0000312//===----------------------------------------------------------------------===//
313// Reference-counting logic (typestate + counts).
314//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +0000315
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000316class RefVal {
Ted Kremeneka2968e52009-11-13 01:54:21 +0000317public:
318 enum Kind {
319 Owned = 0, // Owning reference.
320 NotOwned, // Reference is not owned by still valid (not freed).
321 Released, // Object has been released.
322 ReturnedOwned, // Returned object passes ownership to caller.
323 ReturnedNotOwned, // Return object does not pass ownership to caller.
324 ERROR_START,
325 ErrorDeallocNotOwned, // -dealloc called on non-owned object.
326 ErrorDeallocGC, // Calling -dealloc with GC enabled.
327 ErrorUseAfterRelease, // Object used after released.
328 ErrorReleaseNotOwned, // Release of an object that was not owned.
329 ERROR_LEAK_START,
330 ErrorLeak, // A memory leak due to excessive reference counts.
331 ErrorLeakReturned, // A memory leak due to the returning method not having
332 // the correct naming conventions.
333 ErrorGCLeakReturned,
334 ErrorOverAutorelease,
335 ErrorReturnedNotOwned
336 };
337
338private:
339 Kind kind;
340 RetEffect::ObjKind okind;
341 unsigned Cnt;
342 unsigned ACnt;
343 QualType T;
344
345 RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t)
346 : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {}
347
348 RefVal(Kind k, unsigned cnt = 0)
349 : kind(k), okind(RetEffect::AnyObj), Cnt(cnt), ACnt(0) {}
350
351public:
352 Kind getKind() const { return kind; }
353
354 RetEffect::ObjKind getObjKind() const { return okind; }
355
356 unsigned getCount() const { return Cnt; }
357 unsigned getAutoreleaseCount() const { return ACnt; }
358 unsigned getCombinedCounts() const { return Cnt + ACnt; }
359 void clearCounts() { Cnt = 0; ACnt = 0; }
360 void setCount(unsigned i) { Cnt = i; }
361 void setAutoreleaseCount(unsigned i) { ACnt = i; }
362
363 QualType getType() const { return T; }
364
365 // Useful predicates.
366
367 static bool isError(Kind k) { return k >= ERROR_START; }
368
369 static bool isLeak(Kind k) { return k >= ERROR_LEAK_START; }
370
371 bool isOwned() const {
372 return getKind() == Owned;
373 }
374
375 bool isNotOwned() const {
376 return getKind() == NotOwned;
377 }
378
379 bool isReturnedOwned() const {
380 return getKind() == ReturnedOwned;
381 }
382
383 bool isReturnedNotOwned() const {
384 return getKind() == ReturnedNotOwned;
385 }
386
387 bool isNonLeakError() const {
388 Kind k = getKind();
389 return isError(k) && !isLeak(k);
390 }
391
392 static RefVal makeOwned(RetEffect::ObjKind o, QualType t,
393 unsigned Count = 1) {
394 return RefVal(Owned, o, Count, 0, t);
395 }
396
397 static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t,
398 unsigned Count = 0) {
399 return RefVal(NotOwned, o, Count, 0, t);
400 }
401
402 // Comparison, profiling, and pretty-printing.
403
404 bool operator==(const RefVal& X) const {
405 return kind == X.kind && Cnt == X.Cnt && T == X.T && ACnt == X.ACnt;
406 }
407
408 RefVal operator-(size_t i) const {
409 return RefVal(getKind(), getObjKind(), getCount() - i,
410 getAutoreleaseCount(), getType());
411 }
412
413 RefVal operator+(size_t i) const {
414 return RefVal(getKind(), getObjKind(), getCount() + i,
415 getAutoreleaseCount(), getType());
416 }
417
418 RefVal operator^(Kind k) const {
419 return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(),
420 getType());
421 }
422
423 RefVal autorelease() const {
424 return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1,
425 getType());
426 }
427
428 void Profile(llvm::FoldingSetNodeID& ID) const {
429 ID.AddInteger((unsigned) kind);
430 ID.AddInteger(Cnt);
431 ID.AddInteger(ACnt);
432 ID.Add(T);
433 }
434
435 void print(llvm::raw_ostream& Out) const;
436};
437
438void RefVal::print(llvm::raw_ostream& Out) const {
439 if (!T.isNull())
440 Out << "Tracked Type:" << T.getAsString() << '\n';
441
442 switch (getKind()) {
443 default: assert(false);
444 case Owned: {
445 Out << "Owned";
446 unsigned cnt = getCount();
447 if (cnt) Out << " (+ " << cnt << ")";
448 break;
449 }
450
451 case NotOwned: {
452 Out << "NotOwned";
453 unsigned cnt = getCount();
454 if (cnt) Out << " (+ " << cnt << ")";
455 break;
456 }
457
458 case ReturnedOwned: {
459 Out << "ReturnedOwned";
460 unsigned cnt = getCount();
461 if (cnt) Out << " (+ " << cnt << ")";
462 break;
463 }
464
465 case ReturnedNotOwned: {
466 Out << "ReturnedNotOwned";
467 unsigned cnt = getCount();
468 if (cnt) Out << " (+ " << cnt << ")";
469 break;
470 }
471
472 case Released:
473 Out << "Released";
474 break;
475
476 case ErrorDeallocGC:
477 Out << "-dealloc (GC)";
478 break;
479
480 case ErrorDeallocNotOwned:
481 Out << "-dealloc (not-owned)";
482 break;
483
484 case ErrorLeak:
485 Out << "Leaked";
486 break;
487
488 case ErrorLeakReturned:
489 Out << "Leaked (Bad naming)";
490 break;
491
492 case ErrorGCLeakReturned:
493 Out << "Leaked (GC-ed at return)";
494 break;
495
496 case ErrorUseAfterRelease:
497 Out << "Use-After-Release [ERROR]";
498 break;
499
500 case ErrorReleaseNotOwned:
501 Out << "Release of Not-Owned [ERROR]";
502 break;
503
504 case RefVal::ErrorOverAutorelease:
505 Out << "Over autoreleased";
506 break;
507
508 case RefVal::ErrorReturnedNotOwned:
509 Out << "Non-owned object returned instead of owned";
510 break;
511 }
512
513 if (ACnt) {
514 Out << " [ARC +" << ACnt << ']';
515 }
516}
517} //end anonymous namespace
518
519//===----------------------------------------------------------------------===//
520// RefBindings - State used to track object reference counts.
521//===----------------------------------------------------------------------===//
522
523typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
Ted Kremeneka2968e52009-11-13 01:54:21 +0000524
525namespace clang {
526 template<>
527 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
Ted Kremenek3c557182009-11-13 01:58:01 +0000528 static void* GDMIndex() {
529 static int RefBIndex = 0;
530 return &RefBIndex;
531 }
Ted Kremeneka2968e52009-11-13 01:54:21 +0000532 };
533}
534
535//===----------------------------------------------------------------------===//
536// Summaries
537//===----------------------------------------------------------------------===//
538
539namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000540class RetainSummary {
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000541 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
542 /// specifies the argument (starting from 0). This can be sparsely
543 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000544 ArgEffects Args;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000546 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
547 /// do not have an entry in Args.
548 ArgEffect DefaultArgEffect;
Mike Stump11289f42009-09-09 15:08:12 +0000549
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000550 /// Receiver - If this summary applies to an Objective-C message expression,
551 /// this is the effect applied to the state of the receiver.
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000552 ArgEffect Receiver;
Mike Stump11289f42009-09-09 15:08:12 +0000553
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000554 /// Ret - The effect on the return value. Used to indicate if the
555 /// function/method call returns a new tracked symbol, returns an
556 /// alias of one of the arguments in the call, and so on.
Ted Kremenek819e9b62008-03-11 06:39:11 +0000557 RetEffect Ret;
Mike Stump11289f42009-09-09 15:08:12 +0000558
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000559 /// EndPath - Indicates that execution of this method/function should
560 /// terminate the simulation of a path.
561 bool EndPath;
Mike Stump11289f42009-09-09 15:08:12 +0000562
Ted Kremenek819e9b62008-03-11 06:39:11 +0000563public:
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000564 RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff,
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000565 ArgEffect ReceiverEff, bool endpath = false)
566 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
Mike Stump11289f42009-09-09 15:08:12 +0000567 EndPath(endpath) {}
568
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000569 /// getArg - Return the argument effect on the argument specified by
570 /// idx (starting from 0).
Ted Kremenekbf9d8042008-03-11 17:48:22 +0000571 ArgEffect getArg(unsigned idx) const {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000572 if (const ArgEffect *AE = Args.lookup(idx))
573 return *AE;
Mike Stump11289f42009-09-09 15:08:12 +0000574
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000575 return DefaultArgEffect;
Ted Kremenekbf9d8042008-03-11 17:48:22 +0000576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000578 /// setDefaultArgEffect - Set the default argument effect.
579 void setDefaultArgEffect(ArgEffect E) {
580 DefaultArgEffect = E;
581 }
Mike Stump11289f42009-09-09 15:08:12 +0000582
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000583 /// setArg - Set the argument effect on the argument specified by idx.
584 void setArgEffect(ArgEffects::Factory& AF, unsigned idx, ArgEffect E) {
585 Args = AF.Add(Args, idx, E);
586 }
Mike Stump11289f42009-09-09 15:08:12 +0000587
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000588 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000589 RetEffect getRetEffect() const { return Ret; }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000591 /// setRetEffect - Set the effect of the return value of the call.
592 void setRetEffect(RetEffect E) { Ret = E; }
Mike Stump11289f42009-09-09 15:08:12 +0000593
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000594 /// isEndPath - Returns true if executing the given method/function should
595 /// terminate the path.
596 bool isEndPath() const { return EndPath; }
Mike Stump11289f42009-09-09 15:08:12 +0000597
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000598 /// getReceiverEffect - Returns the effect on the receiver of the call.
599 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000600 ArgEffect getReceiverEffect() const { return Receiver; }
Mike Stump11289f42009-09-09 15:08:12 +0000601
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000602 /// setReceiverEffect - Set the effect on the receiver of the call.
603 void setReceiverEffect(ArgEffect E) { Receiver = E; }
Mike Stump11289f42009-09-09 15:08:12 +0000604
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000605 typedef ArgEffects::iterator ExprIterator;
Mike Stump11289f42009-09-09 15:08:12 +0000606
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000607 ExprIterator begin_args() const { return Args.begin(); }
608 ExprIterator end_args() const { return Args.end(); }
Mike Stump11289f42009-09-09 15:08:12 +0000609
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000610 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects A,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000611 RetEffect RetEff, ArgEffect DefaultEff,
Ted Kremenekf7faa422008-07-18 17:39:56 +0000612 ArgEffect ReceiverEff, bool EndPath) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000613 ID.Add(A);
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000614 ID.Add(RetEff);
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000615 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000616 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenekf7faa422008-07-18 17:39:56 +0000617 ID.AddInteger((unsigned) EndPath);
Ted Kremenek819e9b62008-03-11 06:39:11 +0000618 }
Mike Stump11289f42009-09-09 15:08:12 +0000619
Ted Kremenek819e9b62008-03-11 06:39:11 +0000620 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf7faa422008-07-18 17:39:56 +0000621 Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
Ted Kremenek819e9b62008-03-11 06:39:11 +0000622 }
623};
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000624} // end anonymous namespace
Ted Kremenek819e9b62008-03-11 06:39:11 +0000625
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000626//===----------------------------------------------------------------------===//
627// Data structures for constructing summaries.
628//===----------------------------------------------------------------------===//
Ted Kremenekb1d13292008-06-24 03:49:48 +0000629
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000630namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000631class ObjCSummaryKey {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000632 IdentifierInfo* II;
633 Selector S;
Mike Stump11289f42009-09-09 15:08:12 +0000634public:
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000635 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
636 : II(ii), S(s) {}
637
Ted Kremenek223a7d52009-04-29 23:03:22 +0000638 ObjCSummaryKey(const ObjCInterfaceDecl* d, Selector s)
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000639 : II(d ? d->getIdentifier() : 0), S(s) {}
Ted Kremenek5801f652009-05-13 18:16:01 +0000640
641 ObjCSummaryKey(const ObjCInterfaceDecl* d, IdentifierInfo *ii, Selector s)
642 : II(d ? d->getIdentifier() : ii), S(s) {}
Mike Stump11289f42009-09-09 15:08:12 +0000643
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000644 ObjCSummaryKey(Selector s)
645 : II(0), S(s) {}
Mike Stump11289f42009-09-09 15:08:12 +0000646
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000647 IdentifierInfo* getIdentifier() const { return II; }
648 Selector getSelector() const { return S; }
649};
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000650}
651
652namespace llvm {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000653template <> struct DenseMapInfo<ObjCSummaryKey> {
654 static inline ObjCSummaryKey getEmptyKey() {
655 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
656 DenseMapInfo<Selector>::getEmptyKey());
657 }
Mike Stump11289f42009-09-09 15:08:12 +0000658
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000659 static inline ObjCSummaryKey getTombstoneKey() {
660 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
Mike Stump11289f42009-09-09 15:08:12 +0000661 DenseMapInfo<Selector>::getTombstoneKey());
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000662 }
Mike Stump11289f42009-09-09 15:08:12 +0000663
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000664 static unsigned getHashValue(const ObjCSummaryKey &V) {
665 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
Mike Stump11289f42009-09-09 15:08:12 +0000666 & 0x88888888)
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000667 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
668 & 0x55555555);
669 }
Mike Stump11289f42009-09-09 15:08:12 +0000670
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000671 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
672 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
673 RHS.getIdentifier()) &&
674 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
675 RHS.getSelector());
676 }
Mike Stump11289f42009-09-09 15:08:12 +0000677
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000678};
Chris Lattner2f3da9b2009-12-15 07:26:51 +0000679template <>
680struct isPodLike<ObjCSummaryKey> { static const bool value = true; };
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000681} // end llvm namespace
Mike Stump11289f42009-09-09 15:08:12 +0000682
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000683namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000684class ObjCSummaryCache {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000685 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
686 MapTy M;
687public:
688 ObjCSummaryCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000689
Ted Kremenek8be51382009-07-21 23:27:57 +0000690 RetainSummary* find(const ObjCInterfaceDecl* D, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +0000691 Selector S) {
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000692 // Lookup the method using the decl for the class @interface. If we
693 // have no decl, lookup using the class name.
694 return D ? find(D, S) : find(ClsName, S);
695 }
Mike Stump11289f42009-09-09 15:08:12 +0000696
697 RetainSummary* find(const ObjCInterfaceDecl* D, Selector S) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000698 // Do a lookup with the (D,S) pair. If we find a match return
699 // the iterator.
700 ObjCSummaryKey K(D, S);
701 MapTy::iterator I = M.find(K);
Mike Stump11289f42009-09-09 15:08:12 +0000702
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000703 if (I != M.end() || !D)
Ted Kremenek8be51382009-07-21 23:27:57 +0000704 return I->second;
Mike Stump11289f42009-09-09 15:08:12 +0000705
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000706 // Walk the super chain. If we find a hit with a parent, we'll end
707 // up returning that summary. We actually allow that key (null,S), as
708 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
709 // generate initial summaries without having to worry about NSObject
710 // being declared.
711 // FIXME: We may change this at some point.
712 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
713 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
714 break;
Mike Stump11289f42009-09-09 15:08:12 +0000715
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000716 if (!C)
Ted Kremenek8be51382009-07-21 23:27:57 +0000717 return NULL;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000718 }
Mike Stump11289f42009-09-09 15:08:12 +0000719
720 // Cache the summary with original key to make the next lookup faster
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000721 // and return the iterator.
Ted Kremenek8be51382009-07-21 23:27:57 +0000722 RetainSummary *Summ = I->second;
723 M[K] = Summ;
724 return Summ;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Ted Kremenek9551ab62008-08-12 20:41:56 +0000727
Ted Kremenek8be51382009-07-21 23:27:57 +0000728 RetainSummary* find(Expr* Receiver, Selector S) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000729 return find(getReceiverDecl(Receiver), S);
730 }
Mike Stump11289f42009-09-09 15:08:12 +0000731
Ted Kremenek8be51382009-07-21 23:27:57 +0000732 RetainSummary* find(IdentifierInfo* II, Selector S) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000733 // FIXME: Class method lookup. Right now we dont' have a good way
734 // of going between IdentifierInfo* and the class hierarchy.
Ted Kremenek8be51382009-07-21 23:27:57 +0000735 MapTy::iterator I = M.find(ObjCSummaryKey(II, S));
Mike Stump11289f42009-09-09 15:08:12 +0000736
Ted Kremenek8be51382009-07-21 23:27:57 +0000737 if (I == M.end())
738 I = M.find(ObjCSummaryKey(S));
Mike Stump11289f42009-09-09 15:08:12 +0000739
Ted Kremenek8be51382009-07-21 23:27:57 +0000740 return I == M.end() ? NULL : I->second;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000741 }
Mike Stump11289f42009-09-09 15:08:12 +0000742
743 const ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
Steve Naroff7cae42b2009-07-10 23:34:53 +0000744 if (const ObjCObjectPointerType* PT =
John McCall9dd450b2009-09-21 23:43:11 +0000745 E->getType()->getAs<ObjCObjectPointerType>())
Steve Naroff7cae42b2009-07-10 23:34:53 +0000746 return PT->getInterfaceDecl();
747
748 return NULL;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000749 }
Mike Stump11289f42009-09-09 15:08:12 +0000750
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000751 RetainSummary*& operator[](ObjCMessageExpr* ME) {
Mike Stump11289f42009-09-09 15:08:12 +0000752
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000753 Selector S = ME->getSelector();
Mike Stump11289f42009-09-09 15:08:12 +0000754
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000755 if (Expr* Receiver = ME->getReceiver()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +0000756 const ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000757 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
758 }
Mike Stump11289f42009-09-09 15:08:12 +0000759
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000760 return M[ObjCSummaryKey(ME->getClassName(), S)];
761 }
Mike Stump11289f42009-09-09 15:08:12 +0000762
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000763 RetainSummary*& operator[](ObjCSummaryKey K) {
764 return M[K];
765 }
Mike Stump11289f42009-09-09 15:08:12 +0000766
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000767 RetainSummary*& operator[](Selector S) {
768 return M[ ObjCSummaryKey(S) ];
769 }
Mike Stump11289f42009-09-09 15:08:12 +0000770};
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000771} // end anonymous namespace
772
773//===----------------------------------------------------------------------===//
774// Data structures for managing collections of summaries.
775//===----------------------------------------------------------------------===//
776
777namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +0000778class RetainSummaryManager {
Ted Kremenek00daccd2008-05-05 22:11:16 +0000779
780 //==-----------------------------------------------------------------==//
781 // Typedefs.
782 //==-----------------------------------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +0000783
Ted Kremenek00daccd2008-05-05 22:11:16 +0000784 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
785 FuncSummariesTy;
Mike Stump11289f42009-09-09 15:08:12 +0000786
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000787 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Mike Stump11289f42009-09-09 15:08:12 +0000788
Ted Kremenek00daccd2008-05-05 22:11:16 +0000789 //==-----------------------------------------------------------------==//
790 // Data.
791 //==-----------------------------------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +0000792
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000793 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek4b7ca772008-04-29 05:33:51 +0000794 ASTContext& Ctx;
Ted Kremenekab54e512008-07-01 17:21:27 +0000795
Ted Kremenekae529272008-07-09 18:11:16 +0000796 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
797 /// "CFDictionaryCreate".
798 IdentifierInfo* CFDictionaryCreateII;
Mike Stump11289f42009-09-09 15:08:12 +0000799
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000800 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek4b7ca772008-04-29 05:33:51 +0000801 const bool GCEnabled;
Mike Stump11289f42009-09-09 15:08:12 +0000802
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000803 /// FuncSummaries - A map from FunctionDecls to summaries.
Mike Stump11289f42009-09-09 15:08:12 +0000804 FuncSummariesTy FuncSummaries;
805
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000806 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
807 /// to summaries.
Ted Kremenekea736c52008-06-23 22:21:20 +0000808 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenek00daccd2008-05-05 22:11:16 +0000809
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000810 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenekea736c52008-06-23 22:21:20 +0000811 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenek00daccd2008-05-05 22:11:16 +0000812
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000813 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
814 /// and all other data used by the checker.
Ted Kremenek00daccd2008-05-05 22:11:16 +0000815 llvm::BumpPtrAllocator BPAlloc;
Mike Stump11289f42009-09-09 15:08:12 +0000816
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000817 /// AF - A factory for ArgEffects objects.
Mike Stump11289f42009-09-09 15:08:12 +0000818 ArgEffects::Factory AF;
819
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000820 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenek00daccd2008-05-05 22:11:16 +0000821 ArgEffects ScratchArgs;
Mike Stump11289f42009-09-09 15:08:12 +0000822
Ted Kremenek9157fbb2009-05-07 23:40:42 +0000823 /// ObjCAllocRetE - Default return effect for methods returning Objective-C
824 /// objects.
825 RetEffect ObjCAllocRetE;
Ted Kremeneka03705c2009-06-05 23:18:01 +0000826
Mike Stump11289f42009-09-09 15:08:12 +0000827 /// ObjCInitRetE - Default return effect for init methods returning
Ted Kremenek815fbb62009-08-20 05:13:36 +0000828 /// Objective-C objects.
Ted Kremeneka03705c2009-06-05 23:18:01 +0000829 RetEffect ObjCInitRetE;
Mike Stump11289f42009-09-09 15:08:12 +0000830
Ted Kremenekff606a12009-05-04 04:57:00 +0000831 RetainSummary DefaultSummary;
Ted Kremenek10427bd2008-05-06 18:11:36 +0000832 RetainSummary* StopSummary;
Mike Stump11289f42009-09-09 15:08:12 +0000833
Ted Kremenek00daccd2008-05-05 22:11:16 +0000834 //==-----------------------------------------------------------------==//
835 // Methods.
836 //==-----------------------------------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +0000837
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000838 /// getArgEffects - Returns a persistent ArgEffects object based on the
839 /// data in ScratchArgs.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000840 ArgEffects getArgEffects();
Ted Kremenek819e9b62008-03-11 06:39:11 +0000841
Mike Stump11289f42009-09-09 15:08:12 +0000842 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
843
Ted Kremenekcc3d1882008-10-23 01:56:15 +0000844public:
Ted Kremenek1272f702009-05-12 20:06:54 +0000845 RetEffect getObjAllocRetEffect() const { return ObjCAllocRetE; }
846
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000847 RetainSummary *getDefaultSummary() {
848 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
849 return new (Summ) RetainSummary(DefaultSummary);
850 }
Mike Stump11289f42009-09-09 15:08:12 +0000851
Ted Kremenek82157a12009-02-23 16:51:39 +0000852 RetainSummary* getUnarySummary(const FunctionType* FT, UnaryFuncKind func);
Mike Stump11289f42009-09-09 15:08:12 +0000853
Ted Kremenek00daccd2008-05-05 22:11:16 +0000854 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
Mike Stump11289f42009-09-09 15:08:12 +0000855 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek7e904222009-01-12 21:45:02 +0000856 RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
Mike Stump11289f42009-09-09 15:08:12 +0000857
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000858 RetainSummary* getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000859 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000860 ArgEffect DefaultEff = MayEscape,
861 bool isEndPath = false);
Ted Kremenek3700b762008-10-29 04:07:07 +0000862
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000863 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000864 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek1df2f3a2008-05-22 17:31:13 +0000865 ArgEffect DefaultEff = MayEscape) {
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000866 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek0806f912008-05-06 00:30:21 +0000867 }
Mike Stump11289f42009-09-09 15:08:12 +0000868
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000869 RetainSummary *getPersistentStopSummary() {
Ted Kremenek10427bd2008-05-06 18:11:36 +0000870 if (StopSummary)
871 return StopSummary;
Mike Stump11289f42009-09-09 15:08:12 +0000872
Ted Kremenek10427bd2008-05-06 18:11:36 +0000873 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
874 StopTracking, StopTracking);
Ted Kremenek3700b762008-10-29 04:07:07 +0000875
Ted Kremenek10427bd2008-05-06 18:11:36 +0000876 return StopSummary;
Mike Stump11289f42009-09-09 15:08:12 +0000877 }
Ted Kremenek015c3562008-05-06 04:20:12 +0000878
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000879 RetainSummary *getInitMethodSummary(QualType RetTy);
Ted Kremenek3d1e9722008-05-05 23:55:01 +0000880
Ted Kremenekea736c52008-06-23 22:21:20 +0000881 void InitializeClassMethodSummaries();
882 void InitializeMethodSummaries();
Mike Stump11289f42009-09-09 15:08:12 +0000883
Ted Kremenekb4cf4a52009-05-03 04:42:10 +0000884 bool isTrackedObjCObjectType(QualType T);
Ted Kremenek4b59ccb2009-05-03 06:08:32 +0000885 bool isTrackedCFObjectType(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000886
Ted Kremenekcc3d1882008-10-23 01:56:15 +0000887private:
Mike Stump11289f42009-09-09 15:08:12 +0000888
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000889 void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
890 RetainSummary* Summ) {
891 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
892 }
Mike Stump11289f42009-09-09 15:08:12 +0000893
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000894 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
895 ObjCClassMethodSummaries[S] = Summ;
896 }
Mike Stump11289f42009-09-09 15:08:12 +0000897
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000898 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
899 ObjCMethodSummaries[S] = Summ;
900 }
Ted Kremenek00dfe302009-03-04 23:30:42 +0000901
902 void addClassMethSummary(const char* Cls, const char* nullaryName,
903 RetainSummary *Summ) {
904 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
905 Selector S = GetNullarySelector(nullaryName, Ctx);
906 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
907 }
Mike Stump11289f42009-09-09 15:08:12 +0000908
Ted Kremenekdce78462009-02-25 02:54:57 +0000909 void addInstMethSummary(const char* Cls, const char* nullaryName,
910 RetainSummary *Summ) {
911 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
912 Selector S = GetNullarySelector(nullaryName, Ctx);
913 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
914 }
Mike Stump11289f42009-09-09 15:08:12 +0000915
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000916 Selector generateSelector(va_list argp) {
Ted Kremenek050b91c2008-08-12 18:30:56 +0000917 llvm::SmallVector<IdentifierInfo*, 10> II;
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000918
Ted Kremenek050b91c2008-08-12 18:30:56 +0000919 while (const char* s = va_arg(argp, const char*))
920 II.push_back(&Ctx.Idents.get(s));
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000921
Mike Stump11289f42009-09-09 15:08:12 +0000922 return Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000923 }
Mike Stump11289f42009-09-09 15:08:12 +0000924
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000925 void addMethodSummary(IdentifierInfo *ClsII, ObjCMethodSummariesTy& Summaries,
926 RetainSummary* Summ, va_list argp) {
927 Selector S = generateSelector(argp);
928 Summaries[ObjCSummaryKey(ClsII, S)] = Summ;
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000929 }
Mike Stump11289f42009-09-09 15:08:12 +0000930
Ted Kremenek3f13f592008-08-12 18:48:50 +0000931 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
932 va_list argp;
933 va_start(argp, Summ);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000934 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
Mike Stump11289f42009-09-09 15:08:12 +0000935 va_end(argp);
Ted Kremenek3f13f592008-08-12 18:48:50 +0000936 }
Mike Stump11289f42009-09-09 15:08:12 +0000937
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000938 void addClsMethSummary(const char* Cls, RetainSummary* Summ, ...) {
939 va_list argp;
940 va_start(argp, Summ);
941 addMethodSummary(&Ctx.Idents.get(Cls),ObjCClassMethodSummaries, Summ, argp);
942 va_end(argp);
943 }
Mike Stump11289f42009-09-09 15:08:12 +0000944
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000945 void addClsMethSummary(IdentifierInfo *II, RetainSummary* Summ, ...) {
946 va_list argp;
947 va_start(argp, Summ);
948 addMethodSummary(II, ObjCClassMethodSummaries, Summ, argp);
949 va_end(argp);
950 }
951
Ted Kremenek050b91c2008-08-12 18:30:56 +0000952 void addPanicSummary(const char* Cls, ...) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000953 RetainSummary* Summ = getPersistentSummary(AF.GetEmptyMap(),
954 RetEffect::MakeNoRet(),
Ted Kremenek050b91c2008-08-12 18:30:56 +0000955 DoNothing, DoNothing, true);
956 va_list argp;
957 va_start (argp, Cls);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000958 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
Ted Kremenek050b91c2008-08-12 18:30:56 +0000959 va_end(argp);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000960 }
Mike Stump11289f42009-09-09 15:08:12 +0000961
Ted Kremenek819e9b62008-03-11 06:39:11 +0000962public:
Mike Stump11289f42009-09-09 15:08:12 +0000963
Ted Kremenek00daccd2008-05-05 22:11:16 +0000964 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenekab54e512008-07-01 17:21:27 +0000965 : Ctx(ctx),
Ted Kremenekae529272008-07-09 18:11:16 +0000966 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000967 GCEnabled(gcenabled), AF(BPAlloc), ScratchArgs(AF.GetEmptyMap()),
Ted Kremenek9157fbb2009-05-07 23:40:42 +0000968 ObjCAllocRetE(gcenabled ? RetEffect::MakeGCNotOwned()
969 : RetEffect::MakeOwned(RetEffect::ObjC, true)),
Ted Kremenekea675cf2009-06-11 18:17:24 +0000970 ObjCInitRetE(gcenabled ? RetEffect::MakeGCNotOwned()
971 : RetEffect::MakeOwnedWhenTrackedReceiver()),
Ted Kremenekff606a12009-05-04 04:57:00 +0000972 DefaultSummary(AF.GetEmptyMap() /* per-argument effects (none) */,
973 RetEffect::MakeNoRet() /* return effect */,
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000974 MayEscape, /* default argument effect */
975 DoNothing /* receiver effect */),
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000976 StopSummary(0) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000977
978 InitializeClassMethodSummaries();
979 InitializeMethodSummaries();
980 }
Mike Stump11289f42009-09-09 15:08:12 +0000981
Ted Kremenek00daccd2008-05-05 22:11:16 +0000982 ~RetainSummaryManager();
Mike Stump11289f42009-09-09 15:08:12 +0000983
984 RetainSummary* getSummary(FunctionDecl* FD);
985
Ted Kremeneka2968e52009-11-13 01:54:21 +0000986 RetainSummary *getInstanceMethodSummary(const ObjCMessageExpr *ME,
987 const GRState *state,
988 const LocationContext *LC);
989
990 RetainSummary* getInstanceMethodSummary(const ObjCMessageExpr* ME,
Ted Kremenek223a7d52009-04-29 23:03:22 +0000991 const ObjCInterfaceDecl* ID) {
Ted Kremenek38724302009-04-29 17:09:14 +0000992 return getInstanceMethodSummary(ME->getSelector(), ME->getClassName(),
Mike Stump11289f42009-09-09 15:08:12 +0000993 ID, ME->getMethodDecl(), ME->getType());
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000994 }
Mike Stump11289f42009-09-09 15:08:12 +0000995
Ted Kremenek38724302009-04-29 17:09:14 +0000996 RetainSummary* getInstanceMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +0000997 const ObjCInterfaceDecl* ID,
998 const ObjCMethodDecl *MD,
999 QualType RetTy);
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001000
1001 RetainSummary *getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +00001002 const ObjCInterfaceDecl *ID,
1003 const ObjCMethodDecl *MD,
1004 QualType RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001005
Ted Kremeneka2968e52009-11-13 01:54:21 +00001006 RetainSummary *getClassMethodSummary(const ObjCMessageExpr *ME) {
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001007 return getClassMethodSummary(ME->getSelector(), ME->getClassName(),
1008 ME->getClassInfo().first,
1009 ME->getMethodDecl(), ME->getType());
1010 }
Ted Kremenek99fe1692009-04-29 17:17:48 +00001011
1012 /// getMethodSummary - This version of getMethodSummary is used to query
1013 /// the summary for the current method being analyzed.
Ted Kremenek223a7d52009-04-29 23:03:22 +00001014 RetainSummary *getMethodSummary(const ObjCMethodDecl *MD) {
1015 // FIXME: Eventually this should be unneeded.
Ted Kremenek223a7d52009-04-29 23:03:22 +00001016 const ObjCInterfaceDecl *ID = MD->getClassInterface();
Ted Kremenekb2a143f2009-04-30 05:41:14 +00001017 Selector S = MD->getSelector();
Ted Kremenek99fe1692009-04-29 17:17:48 +00001018 IdentifierInfo *ClsName = ID->getIdentifier();
1019 QualType ResultTy = MD->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +00001020
1021 // Resolve the method decl last.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001022 if (const ObjCMethodDecl *InterfaceMD = ResolveToInterfaceMethodDecl(MD))
Ted Kremenek497df912009-04-30 05:47:23 +00001023 MD = InterfaceMD;
Mike Stump11289f42009-09-09 15:08:12 +00001024
Ted Kremenek99fe1692009-04-29 17:17:48 +00001025 if (MD->isInstanceMethod())
1026 return getInstanceMethodSummary(S, ClsName, ID, MD, ResultTy);
1027 else
1028 return getClassMethodSummary(S, ClsName, ID, MD, ResultTy);
1029 }
Mike Stump11289f42009-09-09 15:08:12 +00001030
Ted Kremenek223a7d52009-04-29 23:03:22 +00001031 RetainSummary* getCommonMethodSummary(const ObjCMethodDecl* MD,
1032 Selector S, QualType RetTy);
1033
Ted Kremenekc2de7272009-05-09 02:58:13 +00001034 void updateSummaryFromAnnotations(RetainSummary &Summ,
1035 const ObjCMethodDecl *MD);
1036
1037 void updateSummaryFromAnnotations(RetainSummary &Summ,
1038 const FunctionDecl *FD);
1039
Ted Kremenek00daccd2008-05-05 22:11:16 +00001040 bool isGCEnabled() const { return GCEnabled; }
Mike Stump11289f42009-09-09 15:08:12 +00001041
Ted Kremenek1d9a2672009-05-04 05:31:22 +00001042 RetainSummary *copySummary(RetainSummary *OldSumm) {
1043 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
1044 new (Summ) RetainSummary(*OldSumm);
1045 return Summ;
Mike Stump11289f42009-09-09 15:08:12 +00001046 }
Ted Kremenek819e9b62008-03-11 06:39:11 +00001047};
Mike Stump11289f42009-09-09 15:08:12 +00001048
Ted Kremenek819e9b62008-03-11 06:39:11 +00001049} // end anonymous namespace
1050
1051//===----------------------------------------------------------------------===//
1052// Implementation of checker data structures.
1053//===----------------------------------------------------------------------===//
1054
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001055RetainSummaryManager::~RetainSummaryManager() {}
Ted Kremenek819e9b62008-03-11 06:39:11 +00001056
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001057ArgEffects RetainSummaryManager::getArgEffects() {
1058 ArgEffects AE = ScratchArgs;
1059 ScratchArgs = AF.GetEmptyMap();
1060 return AE;
Ted Kremenek68d73d12008-03-12 01:21:45 +00001061}
1062
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001063RetainSummary*
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001064RetainSummaryManager::getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001065 ArgEffect ReceiverEff,
Ted Kremenek3b2294c2008-07-18 17:24:20 +00001066 ArgEffect DefaultEff,
Mike Stump11289f42009-09-09 15:08:12 +00001067 bool isEndPath) {
Ted Kremenekf7141592008-04-24 17:22:33 +00001068 // Create the summary and return it.
Ted Kremenek1bff64e2009-05-04 04:30:18 +00001069 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek3b2294c2008-07-18 17:24:20 +00001070 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenek68d73d12008-03-12 01:21:45 +00001071 return Summ;
1072}
1073
Ted Kremenek00daccd2008-05-05 22:11:16 +00001074//===----------------------------------------------------------------------===//
Ted Kremenek1d92d2c2009-01-07 00:39:56 +00001075// Predicates.
1076//===----------------------------------------------------------------------===//
1077
Ted Kremenekb4cf4a52009-05-03 04:42:10 +00001078bool RetainSummaryManager::isTrackedObjCObjectType(QualType Ty) {
Steve Naroff79d12152009-07-16 15:41:00 +00001079 if (!Ty->isObjCObjectPointerType())
Ted Kremenek1d92d2c2009-01-07 00:39:56 +00001080 return false;
1081
John McCall9dd450b2009-09-21 23:43:11 +00001082 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +00001083
Steve Naroff7cae42b2009-07-10 23:34:53 +00001084 // Can be true for objects with the 'NSObject' attribute.
1085 if (!PT)
Ted Kremenek37467812009-04-23 22:11:07 +00001086 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001087
Steve Naroff7cae42b2009-07-10 23:34:53 +00001088 // We assume that id<..>, id, and "Class" all represent tracked objects.
1089 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
1090 PT->isObjCClassType())
1091 return true;
Ted Kremenek1d92d2c2009-01-07 00:39:56 +00001092
Mike Stump11289f42009-09-09 15:08:12 +00001093 // Does the interface subclass NSObject?
1094 // FIXME: We can memoize here if this gets too expensive.
1095 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Ted Kremenek1d92d2c2009-01-07 00:39:56 +00001096
Ted Kremeneke4302ee2009-05-16 01:38:01 +00001097 // Assume that anything declared with a forward declaration and no
1098 // @interface subclasses NSObject.
1099 if (ID->isForwardDecl())
1100 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001101
Ted Kremeneke4302ee2009-05-16 01:38:01 +00001102 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
1103
Ted Kremenek1d92d2c2009-01-07 00:39:56 +00001104 for ( ; ID ; ID = ID->getSuperClass())
1105 if (ID->getIdentifier() == NSObjectII)
1106 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001107
Ted Kremenek1d92d2c2009-01-07 00:39:56 +00001108 return false;
1109}
1110
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001111bool RetainSummaryManager::isTrackedCFObjectType(QualType T) {
1112 return isRefType(T, "CF") || // Core Foundation.
1113 isRefType(T, "CG") || // Core Graphics.
1114 isRefType(T, "DADisk") || // Disk Arbitration API.
1115 isRefType(T, "DADissenter") ||
1116 isRefType(T, "DASessionRef");
1117}
1118
Ted Kremenek1d92d2c2009-01-07 00:39:56 +00001119//===----------------------------------------------------------------------===//
Ted Kremenek00daccd2008-05-05 22:11:16 +00001120// Summary creation for functions (largely uses of Core Foundation).
1121//===----------------------------------------------------------------------===//
Ted Kremenek68d73d12008-03-12 01:21:45 +00001122
Ted Kremenek7e904222009-01-12 21:45:02 +00001123static bool isRetain(FunctionDecl* FD, const char* FName) {
1124 const char* loc = strstr(FName, "Retain");
1125 return loc && loc[sizeof("Retain")-1] == '\0';
1126}
1127
1128static bool isRelease(FunctionDecl* FD, const char* FName) {
1129 const char* loc = strstr(FName, "Release");
1130 return loc && loc[sizeof("Release")-1] == '\0';
1131}
1132
Ted Kremenekf890bfe2008-06-24 03:56:45 +00001133RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenekf7141592008-04-24 17:22:33 +00001134 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenek00daccd2008-05-05 22:11:16 +00001135 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenek00daccd2008-05-05 22:11:16 +00001136 if (I != FuncSummaries.end())
Ted Kremenekf7141592008-04-24 17:22:33 +00001137 return I->second;
1138
Ted Kremenekdf76e6d2009-05-04 15:34:07 +00001139 // No summary? Generate one.
Ted Kremenek7e904222009-01-12 21:45:02 +00001140 RetainSummary *S = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001141
Ted Kremenekfa89e2f2008-07-15 16:50:12 +00001142 do {
Ted Kremenek7e904222009-01-12 21:45:02 +00001143 // We generate "stop" summaries for implicitly defined functions.
1144 if (FD->isImplicit()) {
1145 S = getPersistentStopSummary();
1146 break;
Ted Kremenekfa89e2f2008-07-15 16:50:12 +00001147 }
Mike Stump11289f42009-09-09 15:08:12 +00001148
John McCall9dd450b2009-09-21 23:43:11 +00001149 // [PR 3337] Use 'getAs<FunctionType>' to strip away any typedefs on the
Ted Kremenek86afde32009-01-16 18:40:33 +00001150 // function's type.
John McCall9dd450b2009-09-21 23:43:11 +00001151 const FunctionType* FT = FD->getType()->getAs<FunctionType>();
Daniel Dunbar2c422dc92009-10-18 20:26:12 +00001152 const char* FName = FD->getIdentifier()->getNameStart();
Mike Stump11289f42009-09-09 15:08:12 +00001153
Ted Kremenek5f968932009-03-05 22:11:14 +00001154 // Strip away preceding '_'. Doing this here will effect all the checks
1155 // down below.
1156 while (*FName == '_') ++FName;
Mike Stump11289f42009-09-09 15:08:12 +00001157
Ted Kremenek7e904222009-01-12 21:45:02 +00001158 // Inspect the result type.
1159 QualType RetTy = FT->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +00001160
Ted Kremenek7e904222009-01-12 21:45:02 +00001161 // FIXME: This should all be refactored into a chain of "summary lookup"
1162 // filters.
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +00001163 assert(ScratchArgs.isEmpty());
1164
Ted Kremenekea675cf2009-06-11 18:17:24 +00001165 switch (strlen(FName)) {
1166 default: break;
Ted Kremenek80816ac2009-10-13 22:55:33 +00001167 case 14:
1168 if (!memcmp(FName, "pthread_create", 14)) {
1169 // Part of: <rdar://problem/7299394>. This will be addressed
1170 // better with IPA.
1171 S = getPersistentStopSummary();
1172 }
1173 break;
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001174
Ted Kremenekea675cf2009-06-11 18:17:24 +00001175 case 17:
1176 // Handle: id NSMakeCollectable(CFTypeRef)
1177 if (!memcmp(FName, "NSMakeCollectable", 17)) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00001178 S = (RetTy->isObjCIdType())
Ted Kremenekea675cf2009-06-11 18:17:24 +00001179 ? getUnarySummary(FT, cfmakecollectable)
1180 : getPersistentStopSummary();
1181 }
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001182 else if (!memcmp(FName, "IOBSDNameMatching", 17) ||
1183 !memcmp(FName, "IOServiceMatching", 17)) {
1184 // Part of <rdar://problem/6961230>. (IOKit)
1185 // This should be addressed using a API table.
1186 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
1187 DoNothing, DoNothing);
1188 }
Ted Kremenekea675cf2009-06-11 18:17:24 +00001189 break;
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001190
1191 case 21:
1192 if (!memcmp(FName, "IOServiceNameMatching", 21)) {
1193 // Part of <rdar://problem/6961230>. (IOKit)
1194 // This should be addressed using a API table.
1195 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
1196 DoNothing, DoNothing);
1197 }
1198 break;
1199
1200 case 24:
1201 if (!memcmp(FName, "IOServiceAddNotification", 24)) {
1202 // Part of <rdar://problem/6961230>. (IOKit)
1203 // This should be addressed using a API table.
1204 ScratchArgs = AF.Add(ScratchArgs, 2, DecRef);
Ted Kremenek55adb822009-10-15 22:25:12 +00001205 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,DoNothing);
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001206 }
1207 break;
Mike Stump11289f42009-09-09 15:08:12 +00001208
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001209 case 25:
1210 if (!memcmp(FName, "IORegistryEntryIDMatching", 25)) {
1211 // Part of <rdar://problem/6961230>. (IOKit)
1212 // This should be addressed using a API table.
1213 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
1214 DoNothing, DoNothing);
1215 }
1216 break;
Mike Stump11289f42009-09-09 15:08:12 +00001217
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001218 case 26:
1219 if (!memcmp(FName, "IOOpenFirmwarePathMatching", 26)) {
1220 // Part of <rdar://problem/6961230>. (IOKit)
1221 // This should be addressed using a API table.
1222 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
Mike Stump11289f42009-09-09 15:08:12 +00001223 DoNothing, DoNothing);
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001224 }
1225 break;
1226
Ted Kremenekea675cf2009-06-11 18:17:24 +00001227 case 27:
1228 if (!memcmp(FName, "IOServiceGetMatchingService", 27)) {
1229 // Part of <rdar://problem/6961230>.
1230 // This should be addressed using a API table.
Ted Kremenekea675cf2009-06-11 18:17:24 +00001231 ScratchArgs = AF.Add(ScratchArgs, 1, DecRef);
Mike Stump11289f42009-09-09 15:08:12 +00001232 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenekea675cf2009-06-11 18:17:24 +00001233 }
1234 break;
1235
1236 case 28:
1237 if (!memcmp(FName, "IOServiceGetMatchingServices", 28)) {
1238 // FIXES: <rdar://problem/6326900>
1239 // This should be addressed using a API table. This strcmp is also
1240 // a little gross, but there is no need to super optimize here.
Ted Kremenekea675cf2009-06-11 18:17:24 +00001241 ScratchArgs = AF.Add(ScratchArgs, 1, DecRef);
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +00001242 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1243 DoNothing);
1244 }
1245 else if (!memcmp(FName, "CVPixelBufferCreateWithBytes", 28)) {
1246 // FIXES: <rdar://problem/7283567>
1247 // Eventually this can be improved by recognizing that the pixel
1248 // buffer passed to CVPixelBufferCreateWithBytes is released via
1249 // a callback and doing full IPA to make sure this is done correctly.
Ted Kremenek43edaa82009-11-03 05:39:12 +00001250 // FIXME: This function has an out parameter that returns an
1251 // allocated object.
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +00001252 ScratchArgs = AF.Add(ScratchArgs, 7, StopTracking);
1253 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1254 DoNothing);
Ted Kremenekea675cf2009-06-11 18:17:24 +00001255 }
1256 break;
Ted Kremenekd1b67db2009-11-03 05:34:07 +00001257
1258 case 29:
1259 if (!memcmp(FName, "CGBitmapContextCreateWithData", 29)) {
1260 // FIXES: <rdar://problem/7358899>
1261 // Eventually this can be improved by recognizing that 'releaseInfo'
1262 // passed to CGBitmapContextCreateWithData is released via
1263 // a callback and doing full IPA to make sure this is done correctly.
1264 ScratchArgs = AF.Add(ScratchArgs, 8, StopTracking);
Ted Kremenek43edaa82009-11-03 05:39:12 +00001265 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
1266 DoNothing,DoNothing);
Ted Kremenekd1b67db2009-11-03 05:34:07 +00001267 }
1268 break;
Mike Stump11289f42009-09-09 15:08:12 +00001269
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001270 case 32:
1271 if (!memcmp(FName, "IOServiceAddMatchingNotification", 32)) {
1272 // Part of <rdar://problem/6961230>.
1273 // This should be addressed using a API table.
1274 ScratchArgs = AF.Add(ScratchArgs, 2, DecRef);
Mike Stump11289f42009-09-09 15:08:12 +00001275 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001276 }
1277 break;
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +00001278
1279 case 34:
1280 if (!memcmp(FName, "CVPixelBufferCreateWithPlanarBytes", 34)) {
1281 // FIXES: <rdar://problem/7283567>
1282 // Eventually this can be improved by recognizing that the pixel
1283 // buffer passed to CVPixelBufferCreateWithPlanarBytes is released
1284 // via a callback and doing full IPA to make sure this is done
1285 // correctly.
1286 ScratchArgs = AF.Add(ScratchArgs, 12, StopTracking);
1287 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1288 DoNothing);
1289 }
1290 break;
Ted Kremenekea675cf2009-06-11 18:17:24 +00001291 }
Mike Stump11289f42009-09-09 15:08:12 +00001292
Ted Kremenekea675cf2009-06-11 18:17:24 +00001293 // Did we get a summary?
1294 if (S)
1295 break;
Ted Kremenek211094d2009-03-17 22:43:44 +00001296
1297 // Enable this code once the semantics of NSDeallocateObject are resolved
1298 // for GC. <rdar://problem/6619988>
1299#if 0
1300 // Handle: NSDeallocateObject(id anObject);
1301 // This method does allow 'nil' (although we don't check it now).
Mike Stump11289f42009-09-09 15:08:12 +00001302 if (strcmp(FName, "NSDeallocateObject") == 0) {
Ted Kremenek211094d2009-03-17 22:43:44 +00001303 return RetTy == Ctx.VoidTy
1304 ? getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, Dealloc)
1305 : getPersistentStopSummary();
1306 }
1307#endif
Ted Kremenek7e904222009-01-12 21:45:02 +00001308
1309 if (RetTy->isPointerType()) {
1310 // For CoreFoundation ('CF') types.
1311 if (isRefType(RetTy, "CF", &Ctx, FName)) {
1312 if (isRetain(FD, FName))
1313 S = getUnarySummary(FT, cfretain);
1314 else if (strstr(FName, "MakeCollectable"))
1315 S = getUnarySummary(FT, cfmakecollectable);
Mike Stump11289f42009-09-09 15:08:12 +00001316 else
Ted Kremenek7e904222009-01-12 21:45:02 +00001317 S = getCFCreateGetRuleSummary(FD, FName);
1318
1319 break;
1320 }
1321
1322 // For CoreGraphics ('CG') types.
1323 if (isRefType(RetTy, "CG", &Ctx, FName)) {
1324 if (isRetain(FD, FName))
1325 S = getUnarySummary(FT, cfretain);
1326 else
1327 S = getCFCreateGetRuleSummary(FD, FName);
1328
1329 break;
1330 }
1331
1332 // For the Disk Arbitration API (DiskArbitration/DADisk.h)
1333 if (isRefType(RetTy, "DADisk") ||
1334 isRefType(RetTy, "DADissenter") ||
1335 isRefType(RetTy, "DASessionRef")) {
1336 S = getCFCreateGetRuleSummary(FD, FName);
1337 break;
1338 }
Mike Stump11289f42009-09-09 15:08:12 +00001339
Ted Kremenek7e904222009-01-12 21:45:02 +00001340 break;
1341 }
1342
1343 // Check for release functions, the only kind of functions that we care
1344 // about that don't return a pointer type.
1345 if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) {
Ted Kremenek5f968932009-03-05 22:11:14 +00001346 // Test for 'CGCF'.
1347 if (FName[1] == 'G' && FName[2] == 'C' && FName[3] == 'F')
1348 FName += 4;
1349 else
1350 FName += 2;
Mike Stump11289f42009-09-09 15:08:12 +00001351
Ted Kremenek5f968932009-03-05 22:11:14 +00001352 if (isRelease(FD, FName))
Ted Kremenek7e904222009-01-12 21:45:02 +00001353 S = getUnarySummary(FT, cfrelease);
1354 else {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001355 assert (ScratchArgs.isEmpty());
Ted Kremeneked90de42009-01-29 22:45:13 +00001356 // Remaining CoreFoundation and CoreGraphics functions.
1357 // We use to assume that they all strictly followed the ownership idiom
1358 // and that ownership cannot be transferred. While this is technically
1359 // correct, many methods allow a tracked object to escape. For example:
1360 //
Mike Stump11289f42009-09-09 15:08:12 +00001361 // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...);
Ted Kremeneked90de42009-01-29 22:45:13 +00001362 // CFDictionaryAddValue(y, key, x);
Mike Stump11289f42009-09-09 15:08:12 +00001363 // CFRelease(x);
Ted Kremeneked90de42009-01-29 22:45:13 +00001364 // ... it is okay to use 'x' since 'y' has a reference to it
1365 //
1366 // We handle this and similar cases with the follow heuristic. If the
Ted Kremenekd982f002009-08-20 00:57:22 +00001367 // function name contains "InsertValue", "SetValue", "AddValue",
1368 // "AppendValue", or "SetAttribute", then we assume that arguments may
1369 // "escape." This means that something else holds on to the object,
1370 // allowing it be used even after its local retain count drops to 0.
Ted Kremeneked90de42009-01-29 22:45:13 +00001371 ArgEffect E = (CStrInCStrNoCase(FName, "InsertValue") ||
1372 CStrInCStrNoCase(FName, "AddValue") ||
Ted Kremenek0ca23d32009-02-05 22:34:53 +00001373 CStrInCStrNoCase(FName, "SetValue") ||
Ted Kremenekd982f002009-08-20 00:57:22 +00001374 CStrInCStrNoCase(FName, "AppendValue") ||
1375 CStrInCStrNoCase(FName, "SetAttribute"))
Ted Kremeneked90de42009-01-29 22:45:13 +00001376 ? MayEscape : DoNothing;
Mike Stump11289f42009-09-09 15:08:12 +00001377
Ted Kremeneked90de42009-01-29 22:45:13 +00001378 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E);
Ted Kremenek7e904222009-01-12 21:45:02 +00001379 }
1380 }
Ted Kremenekfa89e2f2008-07-15 16:50:12 +00001381 }
1382 while (0);
Mike Stump11289f42009-09-09 15:08:12 +00001383
Ted Kremenek1d9a2672009-05-04 05:31:22 +00001384 if (!S)
1385 S = getDefaultSummary();
Ted Kremenekf7141592008-04-24 17:22:33 +00001386
Ted Kremenekc2de7272009-05-09 02:58:13 +00001387 // Annotations override defaults.
1388 assert(S);
1389 updateSummaryFromAnnotations(*S, FD);
Mike Stump11289f42009-09-09 15:08:12 +00001390
Ted Kremenek00daccd2008-05-05 22:11:16 +00001391 FuncSummaries[FD] = S;
Mike Stump11289f42009-09-09 15:08:12 +00001392 return S;
Ted Kremenekea6507f2008-03-06 00:08:09 +00001393}
1394
Ted Kremenekfa89e2f2008-07-15 16:50:12 +00001395RetainSummary*
1396RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
1397 const char* FName) {
Mike Stump11289f42009-09-09 15:08:12 +00001398
Ted Kremenek875db812008-05-05 16:51:50 +00001399 if (strstr(FName, "Create") || strstr(FName, "Copy"))
1400 return getCFSummaryCreateRule(FD);
Mike Stump11289f42009-09-09 15:08:12 +00001401
Ted Kremenek875db812008-05-05 16:51:50 +00001402 if (strstr(FName, "Get"))
1403 return getCFSummaryGetRule(FD);
Mike Stump11289f42009-09-09 15:08:12 +00001404
Ted Kremenekff606a12009-05-04 04:57:00 +00001405 return getDefaultSummary();
Ted Kremenek875db812008-05-05 16:51:50 +00001406}
1407
Ted Kremenek00daccd2008-05-05 22:11:16 +00001408RetainSummary*
Ted Kremenek82157a12009-02-23 16:51:39 +00001409RetainSummaryManager::getUnarySummary(const FunctionType* FT,
1410 UnaryFuncKind func) {
1411
Ted Kremenek7e904222009-01-12 21:45:02 +00001412 // Sanity check that this is *really* a unary function. This can
1413 // happen if people do weird things.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001414 const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT);
Ted Kremenek7e904222009-01-12 21:45:02 +00001415 if (!FTP || FTP->getNumArgs() != 1)
1416 return getPersistentStopSummary();
Mike Stump11289f42009-09-09 15:08:12 +00001417
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001418 assert (ScratchArgs.isEmpty());
Mike Stump11289f42009-09-09 15:08:12 +00001419
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001420 switch (func) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001421 case cfretain: {
1422 ScratchArgs = AF.Add(ScratchArgs, 0, IncRef);
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00001423 return getPersistentSummary(RetEffect::MakeAlias(0),
1424 DoNothing, DoNothing);
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001425 }
Mike Stump11289f42009-09-09 15:08:12 +00001426
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001427 case cfrelease: {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001428 ScratchArgs = AF.Add(ScratchArgs, 0, DecRef);
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00001429 return getPersistentSummary(RetEffect::MakeNoRet(),
1430 DoNothing, DoNothing);
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001431 }
Mike Stump11289f42009-09-09 15:08:12 +00001432
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001433 case cfmakecollectable: {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001434 ScratchArgs = AF.Add(ScratchArgs, 0, MakeCollectable);
Mike Stump11289f42009-09-09 15:08:12 +00001435 return getPersistentSummary(RetEffect::MakeAlias(0),DoNothing, DoNothing);
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001436 }
Mike Stump11289f42009-09-09 15:08:12 +00001437
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001438 default:
Ted Kremenek875db812008-05-05 16:51:50 +00001439 assert (false && "Not a supported unary function.");
Ted Kremenekff606a12009-05-04 04:57:00 +00001440 return getDefaultSummary();
Ted Kremenek4b772092008-04-10 23:44:06 +00001441 }
Ted Kremenek68d73d12008-03-12 01:21:45 +00001442}
1443
Ted Kremenek00daccd2008-05-05 22:11:16 +00001444RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001445 assert (ScratchArgs.isEmpty());
Mike Stump11289f42009-09-09 15:08:12 +00001446
Ted Kremenekae529272008-07-09 18:11:16 +00001447 if (FD->getIdentifier() == CFDictionaryCreateII) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001448 ScratchArgs = AF.Add(ScratchArgs, 1, DoNothingByRef);
1449 ScratchArgs = AF.Add(ScratchArgs, 2, DoNothingByRef);
Ted Kremenekae529272008-07-09 18:11:16 +00001450 }
Mike Stump11289f42009-09-09 15:08:12 +00001451
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001452 return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenek68d73d12008-03-12 01:21:45 +00001453}
1454
Ted Kremenek00daccd2008-05-05 22:11:16 +00001455RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Mike Stump11289f42009-09-09 15:08:12 +00001456 assert (ScratchArgs.isEmpty());
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001457 return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF),
1458 DoNothing, DoNothing);
Ted Kremenek68d73d12008-03-12 01:21:45 +00001459}
1460
Ted Kremenek819e9b62008-03-11 06:39:11 +00001461//===----------------------------------------------------------------------===//
Ted Kremenek00daccd2008-05-05 22:11:16 +00001462// Summary creation for Selectors.
1463//===----------------------------------------------------------------------===//
1464
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001465RetainSummary*
Ted Kremenek0b50fb12009-04-29 05:04:30 +00001466RetainSummaryManager::getInitMethodSummary(QualType RetTy) {
Mike Stump11289f42009-09-09 15:08:12 +00001467 assert(ScratchArgs.isEmpty());
Ted Kremenek1272f702009-05-12 20:06:54 +00001468 // 'init' methods conceptually return a newly allocated object and claim
Mike Stump11289f42009-09-09 15:08:12 +00001469 // the receiver.
Ted Kremenek1272f702009-05-12 20:06:54 +00001470 if (isTrackedObjCObjectType(RetTy) || isTrackedCFObjectType(RetTy))
Ted Kremeneka03705c2009-06-05 23:18:01 +00001471 return getPersistentSummary(ObjCInitRetE, DecRefMsg);
Mike Stump11289f42009-09-09 15:08:12 +00001472
Ted Kremenek1272f702009-05-12 20:06:54 +00001473 return getDefaultSummary();
Ted Kremenek3d1e9722008-05-05 23:55:01 +00001474}
Ted Kremenekc2de7272009-05-09 02:58:13 +00001475
1476void
1477RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1478 const FunctionDecl *FD) {
1479 if (!FD)
1480 return;
1481
Ted Kremenekea675cf2009-06-11 18:17:24 +00001482 QualType RetTy = FD->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +00001483
Ted Kremenekc2de7272009-05-09 02:58:13 +00001484 // Determine if there is a special return effect for this method.
Ted Kremenekea1c2212009-06-05 23:00:33 +00001485 if (isTrackedObjCObjectType(RetTy)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001486 if (FD->getAttr<NSReturnsRetainedAttr>()) {
Ted Kremenekc2de7272009-05-09 02:58:13 +00001487 Summ.setRetEffect(ObjCAllocRetE);
1488 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001489 else if (FD->getAttr<CFReturnsRetainedAttr>()) {
Ted Kremenekea1c2212009-06-05 23:00:33 +00001490 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenekea675cf2009-06-11 18:17:24 +00001491 }
1492 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001493 else if (RetTy->getAs<PointerType>()) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001494 if (FD->getAttr<CFReturnsRetainedAttr>()) {
Ted Kremenekc2de7272009-05-09 02:58:13 +00001495 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
1496 }
1497 }
1498}
1499
1500void
1501RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1502 const ObjCMethodDecl *MD) {
1503 if (!MD)
1504 return;
1505
Ted Kremenek0578e432009-07-06 18:30:43 +00001506 bool isTrackedLoc = false;
Mike Stump11289f42009-09-09 15:08:12 +00001507
Ted Kremenekc2de7272009-05-09 02:58:13 +00001508 // Determine if there is a special return effect for this method.
1509 if (isTrackedObjCObjectType(MD->getResultType())) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001510 if (MD->getAttr<NSReturnsRetainedAttr>()) {
Ted Kremenekc2de7272009-05-09 02:58:13 +00001511 Summ.setRetEffect(ObjCAllocRetE);
Ted Kremenek0578e432009-07-06 18:30:43 +00001512 return;
Ted Kremenekc2de7272009-05-09 02:58:13 +00001513 }
Mike Stump11289f42009-09-09 15:08:12 +00001514
Ted Kremenek0578e432009-07-06 18:30:43 +00001515 isTrackedLoc = true;
Ted Kremenekc2de7272009-05-09 02:58:13 +00001516 }
Mike Stump11289f42009-09-09 15:08:12 +00001517
Ted Kremenek0578e432009-07-06 18:30:43 +00001518 if (!isTrackedLoc)
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001519 isTrackedLoc = MD->getResultType()->getAs<PointerType>() != NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001520
Ted Kremenek0578e432009-07-06 18:30:43 +00001521 if (isTrackedLoc && MD->getAttr<CFReturnsRetainedAttr>())
1522 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenekc2de7272009-05-09 02:58:13 +00001523}
1524
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001525RetainSummary*
Ted Kremenek223a7d52009-04-29 23:03:22 +00001526RetainSummaryManager::getCommonMethodSummary(const ObjCMethodDecl* MD,
1527 Selector S, QualType RetTy) {
Ted Kremenek6a966b22009-04-24 21:56:17 +00001528
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001529 if (MD) {
Ted Kremenek6e86caf2009-04-24 18:00:17 +00001530 // Scan the method decl for 'void*' arguments. These should be treated
1531 // as 'StopTracking' because they are often used with delegates.
1532 // Delegates are a frequent form of false positives with the retain
1533 // count checker.
1534 unsigned i = 0;
1535 for (ObjCMethodDecl::param_iterator I = MD->param_begin(),
1536 E = MD->param_end(); I != E; ++I, ++i)
1537 if (ParmVarDecl *PD = *I) {
1538 QualType Ty = Ctx.getCanonicalType(PD->getType());
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001539 if (Ty.getLocalUnqualifiedType() == Ctx.VoidPtrTy)
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001540 ScratchArgs = AF.Add(ScratchArgs, i, StopTracking);
Ted Kremenek6e86caf2009-04-24 18:00:17 +00001541 }
1542 }
Mike Stump11289f42009-09-09 15:08:12 +00001543
Ted Kremenek6a966b22009-04-24 21:56:17 +00001544 // Any special effect for the receiver?
1545 ArgEffect ReceiverEff = DoNothing;
Mike Stump11289f42009-09-09 15:08:12 +00001546
Ted Kremenek6a966b22009-04-24 21:56:17 +00001547 // If one of the arguments in the selector has the keyword 'delegate' we
1548 // should stop tracking the reference count for the receiver. This is
1549 // because the reference count is quite possibly handled by a delegate
1550 // method.
1551 if (S.isKeywordSelector()) {
1552 const std::string &str = S.getAsString();
1553 assert(!str.empty());
1554 if (CStrInCStrNoCase(&str[0], "delegate:")) ReceiverEff = StopTracking;
1555 }
Mike Stump11289f42009-09-09 15:08:12 +00001556
Ted Kremenek60746a02009-04-23 23:08:22 +00001557 // Look for methods that return an owned object.
Mike Stump11289f42009-09-09 15:08:12 +00001558 if (isTrackedObjCObjectType(RetTy)) {
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001559 // EXPERIMENTAL: Assume the Cocoa conventions for all objects returned
1560 // by instance methods.
Ted Kremenek32819772009-05-15 15:49:00 +00001561 RetEffect E = followsFundamentalRule(S)
1562 ? ObjCAllocRetE : RetEffect::MakeNotOwned(RetEffect::ObjC);
Mike Stump11289f42009-09-09 15:08:12 +00001563
1564 return getPersistentSummary(E, ReceiverEff, MayEscape);
Ted Kremenek6e86caf2009-04-24 18:00:17 +00001565 }
Mike Stump11289f42009-09-09 15:08:12 +00001566
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001567 // Look for methods that return an owned core foundation object.
1568 if (isTrackedCFObjectType(RetTy)) {
Ted Kremenek32819772009-05-15 15:49:00 +00001569 RetEffect E = followsFundamentalRule(S)
1570 ? RetEffect::MakeOwned(RetEffect::CF, true)
1571 : RetEffect::MakeNotOwned(RetEffect::CF);
Mike Stump11289f42009-09-09 15:08:12 +00001572
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001573 return getPersistentSummary(E, ReceiverEff, MayEscape);
1574 }
Mike Stump11289f42009-09-09 15:08:12 +00001575
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001576 if (ScratchArgs.isEmpty() && ReceiverEff == DoNothing)
Ted Kremenekff606a12009-05-04 04:57:00 +00001577 return getDefaultSummary();
Mike Stump11289f42009-09-09 15:08:12 +00001578
Ted Kremenek1d9a2672009-05-04 05:31:22 +00001579 return getPersistentSummary(RetEffect::MakeNoRet(), ReceiverEff, MayEscape);
Ted Kremenek60746a02009-04-23 23:08:22 +00001580}
1581
1582RetainSummary*
Ted Kremeneka2968e52009-11-13 01:54:21 +00001583RetainSummaryManager::getInstanceMethodSummary(const ObjCMessageExpr *ME,
1584 const GRState *state,
1585 const LocationContext *LC) {
1586
1587 // We need the type-information of the tracked receiver object
1588 // Retrieve it from the state.
1589 const Expr *Receiver = ME->getReceiver();
1590 const ObjCInterfaceDecl* ID = 0;
1591
1592 // FIXME: Is this really working as expected? There are cases where
1593 // we just use the 'ID' from the message expression.
1594 SVal receiverV = state->getSValAsScalarOrLoc(Receiver);
1595
1596 // FIXME: Eventually replace the use of state->get<RefBindings> with
1597 // a generic API for reasoning about the Objective-C types of symbolic
1598 // objects.
1599 if (SymbolRef Sym = receiverV.getAsLocSymbol())
1600 if (const RefVal *T = state->get<RefBindings>(Sym))
1601 if (const ObjCObjectPointerType* PT =
1602 T->getType()->getAs<ObjCObjectPointerType>())
1603 ID = PT->getInterfaceDecl();
1604
1605 // FIXME: this is a hack. This may or may not be the actual method
1606 // that is called.
1607 if (!ID) {
1608 if (const ObjCObjectPointerType *PT =
1609 Receiver->getType()->getAs<ObjCObjectPointerType>())
1610 ID = PT->getInterfaceDecl();
1611 }
1612
1613 // FIXME: The receiver could be a reference to a class, meaning that
1614 // we should use the class method.
1615 RetainSummary *Summ = getInstanceMethodSummary(ME, ID);
1616
1617 // Special-case: are we sending a mesage to "self"?
1618 // This is a hack. When we have full-IP this should be removed.
1619 if (isa<ObjCMethodDecl>(LC->getDecl())) {
1620 if (const loc::MemRegionVal *L = dyn_cast<loc::MemRegionVal>(&receiverV)) {
1621 // Get the region associated with 'self'.
1622 if (const ImplicitParamDecl *SelfDecl = LC->getSelfDecl()) {
1623 SVal SelfVal = state->getSVal(state->getRegion(SelfDecl, LC));
1624 if (L->StripCasts() == SelfVal.getAsRegion()) {
1625 // Update the summary to make the default argument effect
1626 // 'StopTracking'.
1627 Summ = copySummary(Summ);
1628 Summ->setDefaultArgEffect(StopTracking);
1629 }
1630 }
1631 }
1632 }
1633
1634 return Summ ? Summ : getDefaultSummary();
1635}
1636
1637RetainSummary*
Ted Kremenek38724302009-04-29 17:09:14 +00001638RetainSummaryManager::getInstanceMethodSummary(Selector S,
1639 IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +00001640 const ObjCInterfaceDecl* ID,
1641 const ObjCMethodDecl *MD,
Ted Kremenek38724302009-04-29 17:09:14 +00001642 QualType RetTy) {
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001643
Ted Kremenek0b50fb12009-04-29 05:04:30 +00001644 // Look up a summary in our summary cache.
Ted Kremenek8be51382009-07-21 23:27:57 +00001645 RetainSummary *Summ = ObjCMethodSummaries.find(ID, ClsName, S);
Mike Stump11289f42009-09-09 15:08:12 +00001646
Ted Kremenek8be51382009-07-21 23:27:57 +00001647 if (!Summ) {
1648 assert(ScratchArgs.isEmpty());
Mike Stump11289f42009-09-09 15:08:12 +00001649
Ted Kremenek8be51382009-07-21 23:27:57 +00001650 // "initXXX": pass-through for receiver.
1651 if (deriveNamingConvention(S) == InitRule)
1652 Summ = getInitMethodSummary(RetTy);
1653 else
1654 Summ = getCommonMethodSummary(MD, S, RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001655
Ted Kremenek8be51382009-07-21 23:27:57 +00001656 // Annotations override defaults.
1657 updateSummaryFromAnnotations(*Summ, MD);
Mike Stump11289f42009-09-09 15:08:12 +00001658
Ted Kremenek8be51382009-07-21 23:27:57 +00001659 // Memoize the summary.
1660 ObjCMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1661 }
Mike Stump11289f42009-09-09 15:08:12 +00001662
Ted Kremenekf27110f2009-04-23 19:11:35 +00001663 return Summ;
Ted Kremenek3d1e9722008-05-05 23:55:01 +00001664}
1665
Ted Kremenek767d0742008-05-06 21:26:51 +00001666RetainSummary*
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001667RetainSummaryManager::getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +00001668 const ObjCInterfaceDecl *ID,
1669 const ObjCMethodDecl *MD,
1670 QualType RetTy) {
Ted Kremenek8a5ad392009-04-24 17:50:11 +00001671
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001672 assert(ClsName && "Class name must be specified.");
Mike Stump11289f42009-09-09 15:08:12 +00001673 RetainSummary *Summ = ObjCClassMethodSummaries.find(ID, ClsName, S);
1674
Ted Kremenek8be51382009-07-21 23:27:57 +00001675 if (!Summ) {
1676 Summ = getCommonMethodSummary(MD, S, RetTy);
1677 // Annotations override defaults.
1678 updateSummaryFromAnnotations(*Summ, MD);
1679 // Memoize the summary.
1680 ObjCClassMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1681 }
Mike Stump11289f42009-09-09 15:08:12 +00001682
Ted Kremenekf27110f2009-04-23 19:11:35 +00001683 return Summ;
Ted Kremenek767d0742008-05-06 21:26:51 +00001684}
1685
Mike Stump11289f42009-09-09 15:08:12 +00001686void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9157fbb2009-05-07 23:40:42 +00001687 assert(ScratchArgs.isEmpty());
1688 RetainSummary* Summ = getPersistentSummary(ObjCAllocRetE);
Mike Stump11289f42009-09-09 15:08:12 +00001689
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001690 // Create the summaries for "alloc", "new", and "allocWithZone:" for
1691 // NSObject and its derivatives.
1692 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
1693 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
1694 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001695
1696 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek55adb822009-10-15 22:25:12 +00001697 addClassMethSummary("NSAssertionHandler", "currentHandler",
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001698 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC)));
Mike Stump11289f42009-09-09 15:08:12 +00001699
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001700 // Create the [NSAutoreleasePool addObject:] summary.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001701 ScratchArgs = AF.Add(ScratchArgs, 0, Autorelease);
Ted Kremenek55adb822009-10-15 22:25:12 +00001702 addClassMethSummary("NSAutoreleasePool", "addObject",
1703 getPersistentSummary(RetEffect::MakeNoRet(),
1704 DoNothing, Autorelease));
Mike Stump11289f42009-09-09 15:08:12 +00001705
Ted Kremenek4e45d802009-10-15 22:26:21 +00001706 // Create a summary for [NSCursor dragCopyCursor].
1707 addClassMethSummary("NSCursor", "dragCopyCursor",
1708 getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1709 DoNothing));
1710
Ted Kremenek8a5ad392009-04-24 17:50:11 +00001711 // Create the summaries for [NSObject performSelector...]. We treat
1712 // these as 'stop tracking' for the arguments because they are often
1713 // used for delegates that can release the object. When we have better
1714 // inter-procedural analysis we can potentially do something better. This
1715 // workaround is to remove false positives.
1716 Summ = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, StopTracking);
1717 IdentifierInfo *NSObjectII = &Ctx.Idents.get("NSObject");
1718 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1719 "afterDelay", NULL);
1720 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1721 "afterDelay", "inModes", NULL);
1722 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1723 "withObject", "waitUntilDone", NULL);
1724 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1725 "withObject", "waitUntilDone", "modes", NULL);
1726 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1727 "withObject", "waitUntilDone", NULL);
1728 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1729 "withObject", "waitUntilDone", "modes", NULL);
1730 addClsMethSummary(NSObjectII, Summ, "performSelectorInBackground",
1731 "withObject", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001732
Ted Kremenekf9fa3cb2009-05-14 21:29:16 +00001733 // Specially handle NSData.
1734 RetainSummary *dataWithBytesNoCopySumm =
1735 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC), DoNothing,
1736 DoNothing);
1737 addClsMethSummary("NSData", dataWithBytesNoCopySumm,
1738 "dataWithBytesNoCopy", "length", NULL);
1739 addClsMethSummary("NSData", dataWithBytesNoCopySumm,
1740 "dataWithBytesNoCopy", "length", "freeWhenDone", NULL);
Ted Kremenek0806f912008-05-06 00:30:21 +00001741}
1742
Ted Kremenekea736c52008-06-23 22:21:20 +00001743void RetainSummaryManager::InitializeMethodSummaries() {
Mike Stump11289f42009-09-09 15:08:12 +00001744
1745 assert (ScratchArgs.isEmpty());
1746
Ted Kremenek767d0742008-05-06 21:26:51 +00001747 // Create the "init" selector. It just acts as a pass-through for the
1748 // receiver.
Mike Stump11289f42009-09-09 15:08:12 +00001749 RetainSummary *InitSumm = getPersistentSummary(ObjCInitRetE, DecRefMsg);
Ted Kremenek815fbb62009-08-20 05:13:36 +00001750 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
1751
1752 // awakeAfterUsingCoder: behaves basically like an 'init' method. It
1753 // claims the receiver and returns a retained object.
1754 addNSObjectMethSummary(GetUnarySelector("awakeAfterUsingCoder", Ctx),
1755 InitSumm);
Mike Stump11289f42009-09-09 15:08:12 +00001756
Ted Kremenek767d0742008-05-06 21:26:51 +00001757 // The next methods are allocators.
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001758 RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE);
Mike Stump11289f42009-09-09 15:08:12 +00001759 RetainSummary *CFAllocSumm =
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001760 getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Mike Stump11289f42009-09-09 15:08:12 +00001761
1762 // Create the "copy" selector.
1763 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), AllocSumm);
Ted Kremenek9551ab62008-08-12 20:41:56 +00001764
Ted Kremenekbe7c56e2008-05-06 00:38:54 +00001765 // Create the "mutableCopy" selector.
Ted Kremenek10369122009-05-20 22:39:57 +00001766 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), AllocSumm);
Mike Stump11289f42009-09-09 15:08:12 +00001767
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001768 // Create the "retain" selector.
Ted Kremenek9157fbb2009-05-07 23:40:42 +00001769 RetEffect E = RetEffect::MakeReceiverAlias();
Ted Kremenek10369122009-05-20 22:39:57 +00001770 RetainSummary *Summ = getPersistentSummary(E, IncRefMsg);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001771 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001772
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001773 // Create the "release" selector.
Ted Kremenekf68490a2009-02-18 18:54:33 +00001774 Summ = getPersistentSummary(E, DecRefMsg);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001775 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001776
Ted Kremenekbcdb4682008-05-07 21:17:39 +00001777 // Create the "drain" selector.
1778 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001779 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001780
Ted Kremenekea072e32009-03-17 19:42:23 +00001781 // Create the -dealloc summary.
1782 Summ = getPersistentSummary(RetEffect::MakeNoRet(), Dealloc);
1783 addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ);
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001784
1785 // Create the "autorelease" selector.
Ted Kremenekc7832092009-01-28 21:44:40 +00001786 Summ = getPersistentSummary(E, Autorelease);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001787 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001788
Ted Kremenek50db3d02009-02-23 17:45:03 +00001789 // Specially handle NSAutoreleasePool.
Ted Kremenekdce78462009-02-25 02:54:57 +00001790 addInstMethSummary("NSAutoreleasePool", "init",
Ted Kremenek50db3d02009-02-23 17:45:03 +00001791 getPersistentSummary(RetEffect::MakeReceiverAlias(),
Ted Kremenekdce78462009-02-25 02:54:57 +00001792 NewAutoreleasePool));
Mike Stump11289f42009-09-09 15:08:12 +00001793
1794 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremeneke73f2822009-02-23 02:51:29 +00001795 // FIXME: For now we opt for false negatives with NSWindow, as these objects
1796 // self-own themselves. However, they only do this once they are displayed.
1797 // Thus, we need to track an NSWindow's display status.
1798 // This is tracked in <rdar://problem/6062711>.
Ted Kremenek00dfe302009-03-04 23:30:42 +00001799 // See also http://llvm.org/bugs/show_bug.cgi?id=3714.
Ted Kremenek1272f702009-05-12 20:06:54 +00001800 RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet(),
1801 StopTracking,
1802 StopTracking);
Mike Stump11289f42009-09-09 15:08:12 +00001803
Ted Kremenek751e7e32009-04-03 19:02:51 +00001804 addClassMethSummary("NSWindow", "alloc", NoTrackYet);
1805
Ted Kremenek00dfe302009-03-04 23:30:42 +00001806#if 0
Ted Kremenek1272f702009-05-12 20:06:54 +00001807 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001808 "styleMask", "backing", "defer", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001809
Ted Kremenek1272f702009-05-12 20:06:54 +00001810 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001811 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek00dfe302009-03-04 23:30:42 +00001812#endif
Mike Stump11289f42009-09-09 15:08:12 +00001813
Ted Kremenek3f13f592008-08-12 18:48:50 +00001814 // For NSPanel (which subclasses NSWindow), allocated objects are not
1815 // self-owned.
Ted Kremenek751e7e32009-04-03 19:02:51 +00001816 // FIXME: For now we don't track NSPanels. object for the same reason
1817 // as for NSWindow objects.
1818 addClassMethSummary("NSPanel", "alloc", NoTrackYet);
Mike Stump11289f42009-09-09 15:08:12 +00001819
Ted Kremenek1272f702009-05-12 20:06:54 +00001820#if 0
1821 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001822 "styleMask", "backing", "defer", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001823
Ted Kremenek1272f702009-05-12 20:06:54 +00001824 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001825 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek1272f702009-05-12 20:06:54 +00001826#endif
Mike Stump11289f42009-09-09 15:08:12 +00001827
Ted Kremenek501ba032009-05-18 23:14:34 +00001828 // Don't track allocated autorelease pools yet, as it is okay to prematurely
1829 // exit a method.
1830 addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001831
Ted Kremenek3b2294c2008-07-18 17:24:20 +00001832 // Create NSAssertionHandler summaries.
Ted Kremenek050b91c2008-08-12 18:30:56 +00001833 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
Mike Stump11289f42009-09-09 15:08:12 +00001834 "lineNumber", "description", NULL);
1835
Ted Kremenek050b91c2008-08-12 18:30:56 +00001836 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1837 "file", "lineNumber", "description", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001838
Ted Kremenek10369122009-05-20 22:39:57 +00001839 // Create summaries QCRenderer/QCView -createSnapShotImageOfType:
1840 addInstMethSummary("QCRenderer", AllocSumm,
1841 "createSnapshotImageOfType", NULL);
1842 addInstMethSummary("QCView", AllocSumm,
1843 "createSnapshotImageOfType", NULL);
1844
Ted Kremenek96aa1462009-06-15 20:58:58 +00001845 // Create summaries for CIContext, 'createCGImage' and
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001846 // 'createCGLayerWithSize'. These objects are CF objects, and are not
1847 // automatically garbage collected.
1848 addInstMethSummary("CIContext", CFAllocSumm,
Ted Kremenek10369122009-05-20 22:39:57 +00001849 "createCGImage", "fromRect", NULL);
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001850 addInstMethSummary("CIContext", CFAllocSumm,
Mike Stump11289f42009-09-09 15:08:12 +00001851 "createCGImage", "fromRect", "format", "colorSpace", NULL);
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001852 addInstMethSummary("CIContext", CFAllocSumm, "createCGLayerWithSize",
Ted Kremenek96aa1462009-06-15 20:58:58 +00001853 "info", NULL);
Ted Kremenekbe7c56e2008-05-06 00:38:54 +00001854}
1855
Ted Kremenek00daccd2008-05-05 22:11:16 +00001856//===----------------------------------------------------------------------===//
Ted Kremenekc52f9392009-02-24 19:15:11 +00001857// AutoreleaseBindings - State used to track objects in autorelease pools.
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001858//===----------------------------------------------------------------------===//
1859
Ted Kremenekc52f9392009-02-24 19:15:11 +00001860typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts;
1861typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents;
1862typedef llvm::ImmutableList<SymbolRef> ARStack;
Ted Kremenek50db3d02009-02-23 17:45:03 +00001863
Ted Kremenekc52f9392009-02-24 19:15:11 +00001864static int AutoRCIndex = 0;
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001865static int AutoRBIndex = 0;
1866
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001867namespace { class AutoreleasePoolContents {}; }
1868namespace { class AutoreleaseStack {}; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001869
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001870namespace clang {
Ted Kremenekdce78462009-02-25 02:54:57 +00001871template<> struct GRStateTrait<AutoreleaseStack>
Ted Kremenekc52f9392009-02-24 19:15:11 +00001872 : public GRStatePartialTrait<ARStack> {
Mike Stump11289f42009-09-09 15:08:12 +00001873 static inline void* GDMIndex() { return &AutoRBIndex; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001874};
1875
1876template<> struct GRStateTrait<AutoreleasePoolContents>
1877 : public GRStatePartialTrait<ARPoolContents> {
Mike Stump11289f42009-09-09 15:08:12 +00001878 static inline void* GDMIndex() { return &AutoRCIndex; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001879};
1880} // end clang namespace
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001881
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001882static SymbolRef GetCurrentAutoreleasePool(const GRState* state) {
1883 ARStack stack = state->get<AutoreleaseStack>();
1884 return stack.isEmpty() ? SymbolRef() : stack.getHead();
1885}
1886
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001887static const GRState * SendAutorelease(const GRState *state,
1888 ARCounts::Factory &F, SymbolRef sym) {
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001889
1890 SymbolRef pool = GetCurrentAutoreleasePool(state);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001891 const ARCounts *cnts = state->get<AutoreleasePoolContents>(pool);
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001892 ARCounts newCnts(0);
Mike Stump11289f42009-09-09 15:08:12 +00001893
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001894 if (cnts) {
1895 const unsigned *cnt = (*cnts).lookup(sym);
1896 newCnts = F.Add(*cnts, sym, cnt ? *cnt + 1 : 1);
1897 }
1898 else
1899 newCnts = F.Add(F.GetEmptyMap(), sym, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001900
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001901 return state->set<AutoreleasePoolContents>(pool, newCnts);
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001902}
1903
Ted Kremenek71454892008-04-16 20:40:59 +00001904//===----------------------------------------------------------------------===//
1905// Transfer functions.
1906//===----------------------------------------------------------------------===//
1907
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001908namespace {
Mike Stump11289f42009-09-09 15:08:12 +00001909
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00001910class CFRefCount : public GRTransferFuncs {
Ted Kremenek396f4362008-04-18 03:39:05 +00001911public:
Ted Kremenek16306102008-08-13 21:24:49 +00001912 class BindingsPrinter : public GRState::Printer {
Ted Kremenek2a723e62008-03-11 19:44:10 +00001913 public:
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001914 virtual void Print(llvm::raw_ostream& Out, const GRState* state,
Ted Kremenek16306102008-08-13 21:24:49 +00001915 const char* nl, const char* sep);
Ted Kremenek2a723e62008-03-11 19:44:10 +00001916 };
Ted Kremenek396f4362008-04-18 03:39:05 +00001917
1918private:
Zhongxing Xu107f7592009-08-06 12:48:26 +00001919 typedef llvm::DenseMap<const ExplodedNode*, const RetainSummary*>
Mike Stump11289f42009-09-09 15:08:12 +00001920 SummaryLogTy;
Ted Kremenek48d16452009-02-18 03:48:14 +00001921
Mike Stump11289f42009-09-09 15:08:12 +00001922 RetainSummaryManager Summaries;
Ted Kremenek48d16452009-02-18 03:48:14 +00001923 SummaryLogTy SummaryLog;
Ted Kremenek00daccd2008-05-05 22:11:16 +00001924 const LangOptions& LOpts;
Ted Kremenekc52f9392009-02-24 19:15:11 +00001925 ARCounts::Factory ARCountFactory;
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001926
Ted Kremenek400aae72009-02-05 06:50:21 +00001927 BugType *useAfterRelease, *releaseNotOwned;
Ted Kremenekea072e32009-03-17 19:42:23 +00001928 BugType *deallocGC, *deallocNotOwned;
Ted Kremenek400aae72009-02-05 06:50:21 +00001929 BugType *leakWithinFunction, *leakAtReturn;
Ted Kremenekd35272f2009-05-09 00:10:05 +00001930 BugType *overAutorelease;
Ted Kremenekdee56e32009-05-10 06:25:57 +00001931 BugType *returnNotOwnedForOwned;
Ted Kremenek400aae72009-02-05 06:50:21 +00001932 BugReporter *BR;
Mike Stump11289f42009-09-09 15:08:12 +00001933
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001934 const GRState * Update(const GRState * state, SymbolRef sym, RefVal V, ArgEffect E,
Ted Kremenekc52f9392009-02-24 19:15:11 +00001935 RefVal::Kind& hasErr);
1936
Zhongxing Xu20227f72009-08-06 01:32:16 +00001937 void ProcessNonLeakError(ExplodedNodeSet& Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001938 GRStmtNodeBuilder& Builder,
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00001939 Expr* NodeExpr, Expr* ErrorExpr,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001940 ExplodedNode* Pred,
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +00001941 const GRState* St,
Ted Kremenekd8242f12008-12-05 02:27:51 +00001942 RefVal::Kind hasErr, SymbolRef Sym);
Mike Stump11289f42009-09-09 15:08:12 +00001943
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001944 const GRState * HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek884a8992009-05-08 23:09:42 +00001945 llvm::SmallVectorImpl<SymbolRef> &Leaked);
Mike Stump11289f42009-09-09 15:08:12 +00001946
Zhongxing Xu20227f72009-08-06 01:32:16 +00001947 ExplodedNode* ProcessLeaks(const GRState * state,
Ted Kremenek884a8992009-05-08 23:09:42 +00001948 llvm::SmallVectorImpl<SymbolRef> &Leaked,
1949 GenericNodeBuilder &Builder,
1950 GRExprEngine &Eng,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001951 ExplodedNode *Pred = 0);
Mike Stump11289f42009-09-09 15:08:12 +00001952
1953public:
Ted Kremenek1f352db2008-07-22 16:21:24 +00001954 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001955 : Summaries(Ctx, gcenabled),
Ted Kremenekea072e32009-03-17 19:42:23 +00001956 LOpts(lopts), useAfterRelease(0), releaseNotOwned(0),
1957 deallocGC(0), deallocNotOwned(0),
Ted Kremenekdee56e32009-05-10 06:25:57 +00001958 leakWithinFunction(0), leakAtReturn(0), overAutorelease(0),
1959 returnNotOwnedForOwned(0), BR(0) {}
Mike Stump11289f42009-09-09 15:08:12 +00001960
Ted Kremenek400aae72009-02-05 06:50:21 +00001961 virtual ~CFRefCount() {}
Mike Stump11289f42009-09-09 15:08:12 +00001962
Ted Kremenek0fbbb082009-11-03 23:30:34 +00001963 void RegisterChecks(GRExprEngine &Eng);
Mike Stump11289f42009-09-09 15:08:12 +00001964
Ted Kremenekceba6ea2008-08-16 00:49:49 +00001965 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1966 Printers.push_back(new BindingsPrinter());
Ted Kremenek2a723e62008-03-11 19:44:10 +00001967 }
Mike Stump11289f42009-09-09 15:08:12 +00001968
Ted Kremenek00daccd2008-05-05 22:11:16 +00001969 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenekb0f87c42008-04-30 23:47:44 +00001970 const LangOptions& getLangOptions() const { return LOpts; }
Mike Stump11289f42009-09-09 15:08:12 +00001971
Zhongxing Xu20227f72009-08-06 01:32:16 +00001972 const RetainSummary *getSummaryOfNode(const ExplodedNode *N) const {
Ted Kremenek48d16452009-02-18 03:48:14 +00001973 SummaryLogTy::const_iterator I = SummaryLog.find(N);
1974 return I == SummaryLog.end() ? 0 : I->second;
1975 }
Mike Stump11289f42009-09-09 15:08:12 +00001976
Ted Kremenek819e9b62008-03-11 06:39:11 +00001977 // Calls.
Ted Kremenek00daccd2008-05-05 22:11:16 +00001978
Zhongxing Xu20227f72009-08-06 01:32:16 +00001979 void EvalSummary(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00001980 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001981 GRStmtNodeBuilder& Builder,
Ted Kremenek00daccd2008-05-05 22:11:16 +00001982 Expr* Ex,
1983 Expr* Receiver,
Ted Kremenekff606a12009-05-04 04:57:00 +00001984 const RetainSummary& Summ,
Ted Kremenek5bee5c42009-12-03 08:25:47 +00001985 const MemRegion *Callee,
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00001986 ExprIterator arg_beg, ExprIterator arg_end,
Zhongxing Xuaf353292009-12-02 05:49:12 +00001987 ExplodedNode* Pred, const GRState *state);
Mike Stump11289f42009-09-09 15:08:12 +00001988
Zhongxing Xu20227f72009-08-06 01:32:16 +00001989 virtual void EvalCall(ExplodedNodeSet& Dst,
Ted Kremenek626bd2d2008-03-12 21:06:49 +00001990 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001991 GRStmtNodeBuilder& Builder,
Zhongxing Xu27f17422008-10-17 05:57:07 +00001992 CallExpr* CE, SVal L,
Mike Stump11289f42009-09-09 15:08:12 +00001993 ExplodedNode* Pred);
1994
1995
Zhongxing Xu20227f72009-08-06 01:32:16 +00001996 virtual void EvalObjCMessageExpr(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001997 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001998 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001999 ObjCMessageExpr* ME,
Zhongxing Xuaf353292009-12-02 05:49:12 +00002000 ExplodedNode* Pred,
2001 const GRState *state);
Mike Stump11289f42009-09-09 15:08:12 +00002002
Zhongxing Xu20227f72009-08-06 01:32:16 +00002003 bool EvalObjCMessageExprAux(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00002004 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002005 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00002006 ObjCMessageExpr* ME,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002007 ExplodedNode* Pred);
Ted Kremenek748c7ce2008-04-15 23:44:31 +00002008
Mike Stump11289f42009-09-09 15:08:12 +00002009 // Stores.
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00002010 virtual void EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val);
2011
Ted Kremenek8784a7c2008-04-11 22:25:11 +00002012 // End-of-path.
Mike Stump11289f42009-09-09 15:08:12 +00002013
Ted Kremenek8784a7c2008-04-11 22:25:11 +00002014 virtual void EvalEndPath(GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002015 GREndPathNodeBuilder& Builder);
Mike Stump11289f42009-09-09 15:08:12 +00002016
Zhongxing Xu20227f72009-08-06 01:32:16 +00002017 virtual void EvalDeadSymbols(ExplodedNodeSet& Dst,
Ted Kremenekb0daf2f2008-04-24 23:57:27 +00002018 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002019 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002020 ExplodedNode* Pred,
Ted Kremenek16fbfe62009-01-21 22:26:05 +00002021 Stmt* S, const GRState* state,
2022 SymbolReaper& SymReaper);
Mike Stump11289f42009-09-09 15:08:12 +00002023
Zhongxing Xu20227f72009-08-06 01:32:16 +00002024 std::pair<ExplodedNode*, const GRState *>
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002025 HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002026 ExplodedNode* Pred, GRExprEngine &Eng,
Ted Kremenekd35272f2009-05-09 00:10:05 +00002027 SymbolRef Sym, RefVal V, bool &stop);
Ted Kremeneka506fec2008-04-17 18:12:53 +00002028 // Return statements.
Mike Stump11289f42009-09-09 15:08:12 +00002029
Zhongxing Xu20227f72009-08-06 01:32:16 +00002030 virtual void EvalReturn(ExplodedNodeSet& Dst,
Ted Kremeneka506fec2008-04-17 18:12:53 +00002031 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002032 GRStmtNodeBuilder& Builder,
Ted Kremeneka506fec2008-04-17 18:12:53 +00002033 ReturnStmt* S,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002034 ExplodedNode* Pred);
Ted Kremenek4d837282008-04-18 19:23:43 +00002035
2036 // Assumptions.
2037
Ted Kremenekf9906842009-06-18 22:57:13 +00002038 virtual const GRState *EvalAssume(const GRState* state, SVal condition,
2039 bool assumption);
Ted Kremenek819e9b62008-03-11 06:39:11 +00002040};
2041
2042} // end anonymous namespace
2043
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002044static void PrintPool(llvm::raw_ostream &Out, SymbolRef Sym,
2045 const GRState *state) {
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002046 Out << ' ';
Ted Kremenek3e31c262009-03-26 03:35:11 +00002047 if (Sym)
2048 Out << Sym->getSymbolID();
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002049 else
2050 Out << "<pool>";
2051 Out << ":{";
Mike Stump11289f42009-09-09 15:08:12 +00002052
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002053 // Get the contents of the pool.
2054 if (const ARCounts *cnts = state->get<AutoreleasePoolContents>(Sym))
2055 for (ARCounts::iterator J=cnts->begin(), EJ=cnts->end(); J != EJ; ++J)
2056 Out << '(' << J.getKey() << ',' << J.getData() << ')';
2057
Mike Stump11289f42009-09-09 15:08:12 +00002058 Out << '}';
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002059}
Ted Kremenek396f4362008-04-18 03:39:05 +00002060
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002061void CFRefCount::BindingsPrinter::Print(llvm::raw_ostream& Out,
2062 const GRState* state,
Ted Kremenek16306102008-08-13 21:24:49 +00002063 const char* nl, const char* sep) {
Mike Stump11289f42009-09-09 15:08:12 +00002064
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00002065 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00002066
Ted Kremenek16306102008-08-13 21:24:49 +00002067 if (!B.isEmpty())
Ted Kremenek2a723e62008-03-11 19:44:10 +00002068 Out << sep << nl;
Mike Stump11289f42009-09-09 15:08:12 +00002069
Ted Kremenek2a723e62008-03-11 19:44:10 +00002070 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
2071 Out << (*I).first << " : ";
2072 (*I).second.print(Out);
2073 Out << nl;
2074 }
Mike Stump11289f42009-09-09 15:08:12 +00002075
Ted Kremenekdce78462009-02-25 02:54:57 +00002076 // Print the autorelease stack.
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002077 Out << sep << nl << "AR pool stack:";
Ted Kremenekdce78462009-02-25 02:54:57 +00002078 ARStack stack = state->get<AutoreleaseStack>();
Mike Stump11289f42009-09-09 15:08:12 +00002079
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002080 PrintPool(Out, SymbolRef(), state); // Print the caller's pool.
2081 for (ARStack::iterator I=stack.begin(), E=stack.end(); I!=E; ++I)
2082 PrintPool(Out, *I, state);
2083
2084 Out << nl;
Ted Kremenek2a723e62008-03-11 19:44:10 +00002085}
2086
Ted Kremenek6bd78702009-04-29 18:50:19 +00002087//===----------------------------------------------------------------------===//
2088// Error reporting.
2089//===----------------------------------------------------------------------===//
2090
2091namespace {
Mike Stump11289f42009-09-09 15:08:12 +00002092
Ted Kremenek6bd78702009-04-29 18:50:19 +00002093 //===-------------===//
2094 // Bug Descriptions. //
Mike Stump11289f42009-09-09 15:08:12 +00002095 //===-------------===//
2096
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002097 class CFRefBug : public BugType {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002098 protected:
2099 CFRefCount& TF;
Mike Stump11289f42009-09-09 15:08:12 +00002100
Benjamin Kramer63415532009-11-29 18:27:55 +00002101 CFRefBug(CFRefCount* tf, llvm::StringRef name)
Mike Stump11289f42009-09-09 15:08:12 +00002102 : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {}
Ted Kremenek6bd78702009-04-29 18:50:19 +00002103 public:
Mike Stump11289f42009-09-09 15:08:12 +00002104
Ted Kremenek6bd78702009-04-29 18:50:19 +00002105 CFRefCount& getTF() { return TF; }
2106 const CFRefCount& getTF() const { return TF; }
Mike Stump11289f42009-09-09 15:08:12 +00002107
Ted Kremenek6bd78702009-04-29 18:50:19 +00002108 // FIXME: Eventually remove.
2109 virtual const char* getDescription() const = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002110
Ted Kremenek6bd78702009-04-29 18:50:19 +00002111 virtual bool isLeak() const { return false; }
2112 };
Mike Stump11289f42009-09-09 15:08:12 +00002113
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002114 class UseAfterRelease : public CFRefBug {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002115 public:
2116 UseAfterRelease(CFRefCount* tf)
2117 : CFRefBug(tf, "Use-after-release") {}
Mike Stump11289f42009-09-09 15:08:12 +00002118
Ted Kremenek6bd78702009-04-29 18:50:19 +00002119 const char* getDescription() const {
2120 return "Reference-counted object is used after it is released";
Mike Stump11289f42009-09-09 15:08:12 +00002121 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002122 };
Mike Stump11289f42009-09-09 15:08:12 +00002123
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002124 class BadRelease : public CFRefBug {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002125 public:
2126 BadRelease(CFRefCount* tf) : CFRefBug(tf, "Bad release") {}
Mike Stump11289f42009-09-09 15:08:12 +00002127
Ted Kremenek6bd78702009-04-29 18:50:19 +00002128 const char* getDescription() const {
Ted Kremenek5c22e112009-10-01 17:31:50 +00002129 return "Incorrect decrement of the reference count of an object that is "
2130 "not owned at this point by the caller";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002131 }
2132 };
Mike Stump11289f42009-09-09 15:08:12 +00002133
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002134 class DeallocGC : public CFRefBug {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002135 public:
Ted Kremenekd35272f2009-05-09 00:10:05 +00002136 DeallocGC(CFRefCount *tf)
2137 : CFRefBug(tf, "-dealloc called while using garbage collection") {}
Mike Stump11289f42009-09-09 15:08:12 +00002138
Ted Kremenek6bd78702009-04-29 18:50:19 +00002139 const char *getDescription() const {
Ted Kremenekd35272f2009-05-09 00:10:05 +00002140 return "-dealloc called while using garbage collection";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002141 }
2142 };
Mike Stump11289f42009-09-09 15:08:12 +00002143
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002144 class DeallocNotOwned : public CFRefBug {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002145 public:
Ted Kremenekd35272f2009-05-09 00:10:05 +00002146 DeallocNotOwned(CFRefCount *tf)
2147 : CFRefBug(tf, "-dealloc sent to non-exclusively owned object") {}
Mike Stump11289f42009-09-09 15:08:12 +00002148
Ted Kremenek6bd78702009-04-29 18:50:19 +00002149 const char *getDescription() const {
2150 return "-dealloc sent to object that may be referenced elsewhere";
2151 }
Mike Stump11289f42009-09-09 15:08:12 +00002152 };
2153
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002154 class OverAutorelease : public CFRefBug {
Ted Kremenekd35272f2009-05-09 00:10:05 +00002155 public:
Mike Stump11289f42009-09-09 15:08:12 +00002156 OverAutorelease(CFRefCount *tf) :
Ted Kremenekd35272f2009-05-09 00:10:05 +00002157 CFRefBug(tf, "Object sent -autorelease too many times") {}
Mike Stump11289f42009-09-09 15:08:12 +00002158
Ted Kremenekd35272f2009-05-09 00:10:05 +00002159 const char *getDescription() const {
Ted Kremenek3978f792009-05-10 05:11:21 +00002160 return "Object sent -autorelease too many times";
Ted Kremenekd35272f2009-05-09 00:10:05 +00002161 }
2162 };
Mike Stump11289f42009-09-09 15:08:12 +00002163
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002164 class ReturnedNotOwnedForOwned : public CFRefBug {
Ted Kremenekdee56e32009-05-10 06:25:57 +00002165 public:
2166 ReturnedNotOwnedForOwned(CFRefCount *tf) :
2167 CFRefBug(tf, "Method should return an owned object") {}
Mike Stump11289f42009-09-09 15:08:12 +00002168
Ted Kremenekdee56e32009-05-10 06:25:57 +00002169 const char *getDescription() const {
2170 return "Object with +0 retain counts returned to caller where a +1 "
2171 "(owning) retain count is expected";
2172 }
2173 };
Mike Stump11289f42009-09-09 15:08:12 +00002174
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002175 class Leak : public CFRefBug {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002176 const bool isReturn;
2177 protected:
Benjamin Kramer63415532009-11-29 18:27:55 +00002178 Leak(CFRefCount* tf, llvm::StringRef name, bool isRet)
Ted Kremenek6bd78702009-04-29 18:50:19 +00002179 : CFRefBug(tf, name), isReturn(isRet) {}
2180 public:
Mike Stump11289f42009-09-09 15:08:12 +00002181
Ted Kremenek6bd78702009-04-29 18:50:19 +00002182 const char* getDescription() const { return ""; }
Mike Stump11289f42009-09-09 15:08:12 +00002183
Ted Kremenek6bd78702009-04-29 18:50:19 +00002184 bool isLeak() const { return true; }
2185 };
Mike Stump11289f42009-09-09 15:08:12 +00002186
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002187 class LeakAtReturn : public Leak {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002188 public:
Benjamin Kramer63415532009-11-29 18:27:55 +00002189 LeakAtReturn(CFRefCount* tf, llvm::StringRef name)
Ted Kremenek6bd78702009-04-29 18:50:19 +00002190 : Leak(tf, name, true) {}
2191 };
Mike Stump11289f42009-09-09 15:08:12 +00002192
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002193 class LeakWithinFunction : public Leak {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002194 public:
Benjamin Kramer63415532009-11-29 18:27:55 +00002195 LeakWithinFunction(CFRefCount* tf, llvm::StringRef name)
Ted Kremenek6bd78702009-04-29 18:50:19 +00002196 : Leak(tf, name, false) {}
Mike Stump11289f42009-09-09 15:08:12 +00002197 };
2198
Ted Kremenek6bd78702009-04-29 18:50:19 +00002199 //===---------===//
2200 // Bug Reports. //
2201 //===---------===//
Mike Stump11289f42009-09-09 15:08:12 +00002202
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002203 class CFRefReport : public RangedBugReport {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002204 protected:
2205 SymbolRef Sym;
2206 const CFRefCount &TF;
2207 public:
2208 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002209 ExplodedNode *n, SymbolRef sym)
Ted Kremenek3978f792009-05-10 05:11:21 +00002210 : RangedBugReport(D, D.getDescription(), n), Sym(sym), TF(tf) {}
2211
2212 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Benjamin Kramer63415532009-11-29 18:27:55 +00002213 ExplodedNode *n, SymbolRef sym, llvm::StringRef endText)
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00002214 : RangedBugReport(D, D.getDescription(), endText, n), Sym(sym), TF(tf) {}
Mike Stump11289f42009-09-09 15:08:12 +00002215
Ted Kremenek6bd78702009-04-29 18:50:19 +00002216 virtual ~CFRefReport() {}
Mike Stump11289f42009-09-09 15:08:12 +00002217
Ted Kremenek6bd78702009-04-29 18:50:19 +00002218 CFRefBug& getBugType() {
2219 return (CFRefBug&) RangedBugReport::getBugType();
2220 }
2221 const CFRefBug& getBugType() const {
2222 return (const CFRefBug&) RangedBugReport::getBugType();
2223 }
Mike Stump11289f42009-09-09 15:08:12 +00002224
Zhongxing Xu7864b9ea2009-08-18 08:58:41 +00002225 virtual void getRanges(const SourceRange*& beg, const SourceRange*& end) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002226 if (!getBugType().isLeak())
Zhongxing Xu7864b9ea2009-08-18 08:58:41 +00002227 RangedBugReport::getRanges(beg, end);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002228 else
2229 beg = end = 0;
2230 }
Mike Stump11289f42009-09-09 15:08:12 +00002231
Ted Kremenek6bd78702009-04-29 18:50:19 +00002232 SymbolRef getSymbol() const { return Sym; }
Mike Stump11289f42009-09-09 15:08:12 +00002233
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002234 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002235 const ExplodedNode* N);
Mike Stump11289f42009-09-09 15:08:12 +00002236
Ted Kremenek6bd78702009-04-29 18:50:19 +00002237 std::pair<const char**,const char**> getExtraDescriptiveText();
Mike Stump11289f42009-09-09 15:08:12 +00002238
Zhongxing Xu20227f72009-08-06 01:32:16 +00002239 PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
2240 const ExplodedNode* PrevN,
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002241 BugReporterContext& BRC);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002242 };
Ted Kremenek3978f792009-05-10 05:11:21 +00002243
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002244 class CFRefLeakReport : public CFRefReport {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002245 SourceLocation AllocSite;
2246 const MemRegion* AllocBinding;
2247 public:
2248 CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002249 ExplodedNode *n, SymbolRef sym,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002250 GRExprEngine& Eng);
Mike Stump11289f42009-09-09 15:08:12 +00002251
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002252 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002253 const ExplodedNode* N);
Mike Stump11289f42009-09-09 15:08:12 +00002254
Ted Kremenek6bd78702009-04-29 18:50:19 +00002255 SourceLocation getLocation() const { return AllocSite; }
Mike Stump11289f42009-09-09 15:08:12 +00002256 };
Ted Kremenek6bd78702009-04-29 18:50:19 +00002257} // end anonymous namespace
2258
Mike Stump11289f42009-09-09 15:08:12 +00002259
Ted Kremenek6bd78702009-04-29 18:50:19 +00002260
2261static const char* Msgs[] = {
2262 // GC only
Mike Stump11289f42009-09-09 15:08:12 +00002263 "Code is compiled to only use garbage collection",
Ted Kremenek6bd78702009-04-29 18:50:19 +00002264 // No GC.
2265 "Code is compiled to use reference counts",
2266 // Hybrid, with GC.
2267 "Code is compiled to use either garbage collection (GC) or reference counts"
Mike Stump11289f42009-09-09 15:08:12 +00002268 " (non-GC). The bug occurs with GC enabled",
Ted Kremenek6bd78702009-04-29 18:50:19 +00002269 // Hybrid, without GC
2270 "Code is compiled to use either garbage collection (GC) or reference counts"
2271 " (non-GC). The bug occurs in non-GC mode"
2272};
2273
2274std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2275 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
Mike Stump11289f42009-09-09 15:08:12 +00002276
Ted Kremenek6bd78702009-04-29 18:50:19 +00002277 switch (TF.getLangOptions().getGCMode()) {
2278 default:
2279 assert(false);
Mike Stump11289f42009-09-09 15:08:12 +00002280
Ted Kremenek6bd78702009-04-29 18:50:19 +00002281 case LangOptions::GCOnly:
2282 assert (TF.isGCEnabled());
Mike Stump11289f42009-09-09 15:08:12 +00002283 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2284
Ted Kremenek6bd78702009-04-29 18:50:19 +00002285 case LangOptions::NonGC:
2286 assert (!TF.isGCEnabled());
2287 return std::make_pair(&Msgs[1], &Msgs[1]+1);
Mike Stump11289f42009-09-09 15:08:12 +00002288
Ted Kremenek6bd78702009-04-29 18:50:19 +00002289 case LangOptions::HybridGC:
2290 if (TF.isGCEnabled())
2291 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2292 else
2293 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2294 }
2295}
2296
2297static inline bool contains(const llvm::SmallVectorImpl<ArgEffect>& V,
2298 ArgEffect X) {
2299 for (llvm::SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end();
2300 I!=E; ++I)
2301 if (*I == X) return true;
Mike Stump11289f42009-09-09 15:08:12 +00002302
Ted Kremenek6bd78702009-04-29 18:50:19 +00002303 return false;
2304}
2305
Zhongxing Xu20227f72009-08-06 01:32:16 +00002306PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode* N,
2307 const ExplodedNode* PrevN,
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002308 BugReporterContext& BRC) {
Mike Stump11289f42009-09-09 15:08:12 +00002309
Ted Kremenek051a03d2009-05-13 07:12:33 +00002310 if (!isa<PostStmt>(N->getLocation()))
2311 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002312
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002313 // Check if the type state has changed.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002314 const GRState *PrevSt = PrevN->getState();
2315 const GRState *CurrSt = N->getState();
Mike Stump11289f42009-09-09 15:08:12 +00002316
2317 const RefVal* CurrT = CurrSt->get<RefBindings>(Sym);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002318 if (!CurrT) return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002319
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002320 const RefVal &CurrV = *CurrT;
2321 const RefVal *PrevT = PrevSt->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002322
Ted Kremenek6bd78702009-04-29 18:50:19 +00002323 // Create a string buffer to constain all the useful things we want
2324 // to tell the user.
2325 std::string sbuf;
2326 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +00002327
Ted Kremenek6bd78702009-04-29 18:50:19 +00002328 // This is the allocation site since the previous node had no bindings
2329 // for this symbol.
2330 if (!PrevT) {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002331 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002332
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002333 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002334 // Get the name of the callee (if it is available).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002335 SVal X = CurrSt->getSValAsScalarOrLoc(CE->getCallee());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002336 if (const FunctionDecl* FD = X.getAsFunctionDecl())
2337 os << "Call to function '" << FD->getNameAsString() <<'\'';
2338 else
Mike Stump11289f42009-09-09 15:08:12 +00002339 os << "function call";
2340 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002341 else {
2342 assert (isa<ObjCMessageExpr>(S));
2343 os << "Method";
2344 }
Mike Stump11289f42009-09-09 15:08:12 +00002345
Ted Kremenek6bd78702009-04-29 18:50:19 +00002346 if (CurrV.getObjKind() == RetEffect::CF) {
2347 os << " returns a Core Foundation object with a ";
2348 }
2349 else {
2350 assert (CurrV.getObjKind() == RetEffect::ObjC);
2351 os << " returns an Objective-C object with a ";
2352 }
Mike Stump11289f42009-09-09 15:08:12 +00002353
Ted Kremenek6bd78702009-04-29 18:50:19 +00002354 if (CurrV.isOwned()) {
2355 os << "+1 retain count (owning reference).";
Mike Stump11289f42009-09-09 15:08:12 +00002356
Ted Kremenek6bd78702009-04-29 18:50:19 +00002357 if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) {
2358 assert(CurrV.getObjKind() == RetEffect::CF);
2359 os << " "
2360 "Core Foundation objects are not automatically garbage collected.";
2361 }
2362 }
2363 else {
2364 assert (CurrV.isNotOwned());
2365 os << "+0 retain count (non-owning reference).";
2366 }
Mike Stump11289f42009-09-09 15:08:12 +00002367
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002368 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002369 return new PathDiagnosticEventPiece(Pos, os.str());
2370 }
Mike Stump11289f42009-09-09 15:08:12 +00002371
Ted Kremenek6bd78702009-04-29 18:50:19 +00002372 // Gather up the effects that were performed on the object at this
2373 // program point
2374 llvm::SmallVector<ArgEffect, 2> AEffects;
Mike Stump11289f42009-09-09 15:08:12 +00002375
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002376 if (const RetainSummary *Summ =
2377 TF.getSummaryOfNode(BRC.getNodeResolver().getOriginalNode(N))) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002378 // We only have summaries attached to nodes after evaluating CallExpr and
2379 // ObjCMessageExprs.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002380 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002381
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002382 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002383 // Iterate through the parameter expressions and see if the symbol
2384 // was ever passed as an argument.
2385 unsigned i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002386
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002387 for (CallExpr::const_arg_iterator AI=CE->arg_begin(), AE=CE->arg_end();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002388 AI!=AE; ++AI, ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002389
Ted Kremenek6bd78702009-04-29 18:50:19 +00002390 // Retrieve the value of the argument. Is it the symbol
2391 // we are interested in?
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002392 if (CurrSt->getSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym)
Ted Kremenek6bd78702009-04-29 18:50:19 +00002393 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002394
Ted Kremenek6bd78702009-04-29 18:50:19 +00002395 // We have an argument. Get the effect!
2396 AEffects.push_back(Summ->getArg(i));
2397 }
2398 }
Mike Stump11289f42009-09-09 15:08:12 +00002399 else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002400 if (const Expr *receiver = ME->getReceiver())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002401 if (CurrSt->getSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002402 // The symbol we are tracking is the receiver.
2403 AEffects.push_back(Summ->getReceiverEffect());
2404 }
2405 }
2406 }
Mike Stump11289f42009-09-09 15:08:12 +00002407
Ted Kremenek6bd78702009-04-29 18:50:19 +00002408 do {
2409 // Get the previous type state.
2410 RefVal PrevV = *PrevT;
Mike Stump11289f42009-09-09 15:08:12 +00002411
Ted Kremenek6bd78702009-04-29 18:50:19 +00002412 // Specially handle -dealloc.
2413 if (!TF.isGCEnabled() && contains(AEffects, Dealloc)) {
2414 // Determine if the object's reference count was pushed to zero.
2415 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
2416 // We may not have transitioned to 'release' if we hit an error.
2417 // This case is handled elsewhere.
2418 if (CurrV.getKind() == RefVal::Released) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002419 assert(CurrV.getCombinedCounts() == 0);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002420 os << "Object released by directly sending the '-dealloc' message";
2421 break;
2422 }
2423 }
Mike Stump11289f42009-09-09 15:08:12 +00002424
Ted Kremenek6bd78702009-04-29 18:50:19 +00002425 // Specially handle CFMakeCollectable and friends.
2426 if (contains(AEffects, MakeCollectable)) {
2427 // Get the name of the function.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002428 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002429 SVal X = CurrSt->getSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002430 const FunctionDecl* FD = X.getAsFunctionDecl();
2431 const std::string& FName = FD->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +00002432
Ted Kremenek6bd78702009-04-29 18:50:19 +00002433 if (TF.isGCEnabled()) {
2434 // Determine if the object's reference count was pushed to zero.
2435 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
Mike Stump11289f42009-09-09 15:08:12 +00002436
Ted Kremenek6bd78702009-04-29 18:50:19 +00002437 os << "In GC mode a call to '" << FName
2438 << "' decrements an object's retain count and registers the "
2439 "object with the garbage collector. ";
Mike Stump11289f42009-09-09 15:08:12 +00002440
Ted Kremenek6bd78702009-04-29 18:50:19 +00002441 if (CurrV.getKind() == RefVal::Released) {
2442 assert(CurrV.getCount() == 0);
2443 os << "Since it now has a 0 retain count the object can be "
2444 "automatically collected by the garbage collector.";
2445 }
2446 else
2447 os << "An object must have a 0 retain count to be garbage collected. "
2448 "After this call its retain count is +" << CurrV.getCount()
2449 << '.';
2450 }
Mike Stump11289f42009-09-09 15:08:12 +00002451 else
Ted Kremenek6bd78702009-04-29 18:50:19 +00002452 os << "When GC is not enabled a call to '" << FName
2453 << "' has no effect on its argument.";
Mike Stump11289f42009-09-09 15:08:12 +00002454
Ted Kremenek6bd78702009-04-29 18:50:19 +00002455 // Nothing more to say.
2456 break;
2457 }
Mike Stump11289f42009-09-09 15:08:12 +00002458
2459 // Determine if the typestate has changed.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002460 if (!(PrevV == CurrV))
2461 switch (CurrV.getKind()) {
2462 case RefVal::Owned:
2463 case RefVal::NotOwned:
Mike Stump11289f42009-09-09 15:08:12 +00002464
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002465 if (PrevV.getCount() == CurrV.getCount()) {
2466 // Did an autorelease message get sent?
2467 if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount())
2468 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002469
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00002470 assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount());
Ted Kremenek3978f792009-05-10 05:11:21 +00002471 os << "Object sent -autorelease message";
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002472 break;
2473 }
Mike Stump11289f42009-09-09 15:08:12 +00002474
Ted Kremenek6bd78702009-04-29 18:50:19 +00002475 if (PrevV.getCount() > CurrV.getCount())
2476 os << "Reference count decremented.";
2477 else
2478 os << "Reference count incremented.";
Mike Stump11289f42009-09-09 15:08:12 +00002479
Ted Kremenek6bd78702009-04-29 18:50:19 +00002480 if (unsigned Count = CurrV.getCount())
2481 os << " The object now has a +" << Count << " retain count.";
Mike Stump11289f42009-09-09 15:08:12 +00002482
Ted Kremenek6bd78702009-04-29 18:50:19 +00002483 if (PrevV.getKind() == RefVal::Released) {
2484 assert(TF.isGCEnabled() && CurrV.getCount() > 0);
2485 os << " The object is not eligible for garbage collection until the "
2486 "retain count reaches 0 again.";
2487 }
Mike Stump11289f42009-09-09 15:08:12 +00002488
Ted Kremenek6bd78702009-04-29 18:50:19 +00002489 break;
Mike Stump11289f42009-09-09 15:08:12 +00002490
Ted Kremenek6bd78702009-04-29 18:50:19 +00002491 case RefVal::Released:
2492 os << "Object released.";
2493 break;
Mike Stump11289f42009-09-09 15:08:12 +00002494
Ted Kremenek6bd78702009-04-29 18:50:19 +00002495 case RefVal::ReturnedOwned:
2496 os << "Object returned to caller as an owning reference (single retain "
2497 "count transferred to caller).";
2498 break;
Mike Stump11289f42009-09-09 15:08:12 +00002499
Ted Kremenek6bd78702009-04-29 18:50:19 +00002500 case RefVal::ReturnedNotOwned:
2501 os << "Object returned to caller with a +0 (non-owning) retain count.";
2502 break;
Mike Stump11289f42009-09-09 15:08:12 +00002503
Ted Kremenek6bd78702009-04-29 18:50:19 +00002504 default:
2505 return NULL;
2506 }
Mike Stump11289f42009-09-09 15:08:12 +00002507
Ted Kremenek6bd78702009-04-29 18:50:19 +00002508 // Emit any remaining diagnostics for the argument effects (if any).
2509 for (llvm::SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(),
2510 E=AEffects.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002511
Ted Kremenek6bd78702009-04-29 18:50:19 +00002512 // A bunch of things have alternate behavior under GC.
2513 if (TF.isGCEnabled())
2514 switch (*I) {
2515 default: break;
2516 case Autorelease:
2517 os << "In GC mode an 'autorelease' has no effect.";
2518 continue;
2519 case IncRefMsg:
2520 os << "In GC mode the 'retain' message has no effect.";
2521 continue;
2522 case DecRefMsg:
2523 os << "In GC mode the 'release' message has no effect.";
2524 continue;
2525 }
2526 }
Mike Stump11289f42009-09-09 15:08:12 +00002527 } while (0);
2528
Ted Kremenek6bd78702009-04-29 18:50:19 +00002529 if (os.str().empty())
2530 return 0; // We have nothing to say!
Ted Kremenek051a03d2009-05-13 07:12:33 +00002531
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002532 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002533 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002534 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(Pos, os.str());
Mike Stump11289f42009-09-09 15:08:12 +00002535
Ted Kremenek6bd78702009-04-29 18:50:19 +00002536 // Add the range by scanning the children of the statement for any bindings
2537 // to Sym.
Mike Stump11289f42009-09-09 15:08:12 +00002538 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002539 I!=E; ++I)
2540 if (const Expr* Exp = dyn_cast_or_null<Expr>(*I))
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002541 if (CurrSt->getSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002542 P->addRange(Exp->getSourceRange());
2543 break;
2544 }
Mike Stump11289f42009-09-09 15:08:12 +00002545
Ted Kremenek6bd78702009-04-29 18:50:19 +00002546 return P;
2547}
2548
2549namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00002550 class FindUniqueBinding :
Ted Kremenek6bd78702009-04-29 18:50:19 +00002551 public StoreManager::BindingsHandler {
2552 SymbolRef Sym;
2553 const MemRegion* Binding;
2554 bool First;
Mike Stump11289f42009-09-09 15:08:12 +00002555
Ted Kremenek6bd78702009-04-29 18:50:19 +00002556 public:
2557 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
Mike Stump11289f42009-09-09 15:08:12 +00002558
Ted Kremenek6bd78702009-04-29 18:50:19 +00002559 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
2560 SVal val) {
Mike Stump11289f42009-09-09 15:08:12 +00002561
2562 SymbolRef SymV = val.getAsSymbol();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002563 if (!SymV || SymV != Sym)
2564 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002565
Ted Kremenek6bd78702009-04-29 18:50:19 +00002566 if (Binding) {
2567 First = false;
2568 return false;
2569 }
2570 else
2571 Binding = R;
Mike Stump11289f42009-09-09 15:08:12 +00002572
2573 return true;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002574 }
Mike Stump11289f42009-09-09 15:08:12 +00002575
Ted Kremenek6bd78702009-04-29 18:50:19 +00002576 operator bool() { return First && Binding; }
2577 const MemRegion* getRegion() { return Binding; }
Mike Stump11289f42009-09-09 15:08:12 +00002578 };
Ted Kremenek6bd78702009-04-29 18:50:19 +00002579}
2580
Zhongxing Xu20227f72009-08-06 01:32:16 +00002581static std::pair<const ExplodedNode*,const MemRegion*>
2582GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode* N,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002583 SymbolRef Sym) {
Mike Stump11289f42009-09-09 15:08:12 +00002584
Ted Kremenek6bd78702009-04-29 18:50:19 +00002585 // Find both first node that referred to the tracked symbol and the
2586 // memory location that value was store to.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002587 const ExplodedNode* Last = N;
Mike Stump11289f42009-09-09 15:08:12 +00002588 const MemRegion* FirstBinding = 0;
2589
Ted Kremenek6bd78702009-04-29 18:50:19 +00002590 while (N) {
2591 const GRState* St = N->getState();
2592 RefBindings B = St->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00002593
Ted Kremenek6bd78702009-04-29 18:50:19 +00002594 if (!B.lookup(Sym))
2595 break;
Mike Stump11289f42009-09-09 15:08:12 +00002596
Ted Kremenek6bd78702009-04-29 18:50:19 +00002597 FindUniqueBinding FB(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002598 StateMgr.iterBindings(St, FB);
2599 if (FB) FirstBinding = FB.getRegion();
2600
Ted Kremenek6bd78702009-04-29 18:50:19 +00002601 Last = N;
Mike Stump11289f42009-09-09 15:08:12 +00002602 N = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002603 }
Mike Stump11289f42009-09-09 15:08:12 +00002604
Ted Kremenek6bd78702009-04-29 18:50:19 +00002605 return std::make_pair(Last, FirstBinding);
2606}
2607
2608PathDiagnosticPiece*
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002609CFRefReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002610 const ExplodedNode* EndN) {
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002611 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenek6bd78702009-04-29 18:50:19 +00002612 // assigned to different variables, etc.
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002613 BRC.addNotableSymbol(Sym);
2614 return RangedBugReport::getEndPath(BRC, EndN);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002615}
2616
2617PathDiagnosticPiece*
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002618CFRefLeakReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002619 const ExplodedNode* EndN){
Mike Stump11289f42009-09-09 15:08:12 +00002620
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002621 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenek6bd78702009-04-29 18:50:19 +00002622 // assigned to different variables, etc.
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002623 BRC.addNotableSymbol(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002624
Ted Kremenek6bd78702009-04-29 18:50:19 +00002625 // We are reporting a leak. Walk up the graph to get to the first node where
2626 // the symbol appeared, and also get the first VarDecl that tracked object
2627 // is stored to.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002628 const ExplodedNode* AllocNode = 0;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002629 const MemRegion* FirstBinding = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002630
Ted Kremenek6bd78702009-04-29 18:50:19 +00002631 llvm::tie(AllocNode, FirstBinding) =
Ted Kremenek8c8fb482009-05-08 23:32:51 +00002632 GetAllocationSite(BRC.getStateManager(), EndN, Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002633
2634 // Get the allocate site.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002635 assert(AllocNode);
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002636 const Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002637
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002638 SourceManager& SMgr = BRC.getSourceManager();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002639 unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart());
Mike Stump11289f42009-09-09 15:08:12 +00002640
Ted Kremenek6bd78702009-04-29 18:50:19 +00002641 // Compute an actual location for the leak. Sometimes a leak doesn't
2642 // occur at an actual statement (e.g., transition between blocks; end
2643 // of function) so we need to walk the graph and compute a real location.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002644 const ExplodedNode* LeakN = EndN;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002645 PathDiagnosticLocation L;
Mike Stump11289f42009-09-09 15:08:12 +00002646
Ted Kremenek6bd78702009-04-29 18:50:19 +00002647 while (LeakN) {
2648 ProgramPoint P = LeakN->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002649
Ted Kremenek6bd78702009-04-29 18:50:19 +00002650 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
2651 L = PathDiagnosticLocation(PS->getStmt()->getLocStart(), SMgr);
2652 break;
2653 }
2654 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
2655 if (const Stmt* Term = BE->getSrc()->getTerminator()) {
2656 L = PathDiagnosticLocation(Term->getLocStart(), SMgr);
2657 break;
2658 }
2659 }
Mike Stump11289f42009-09-09 15:08:12 +00002660
Ted Kremenek6bd78702009-04-29 18:50:19 +00002661 LeakN = LeakN->succ_empty() ? 0 : *(LeakN->succ_begin());
2662 }
Mike Stump11289f42009-09-09 15:08:12 +00002663
Ted Kremenek6bd78702009-04-29 18:50:19 +00002664 if (!L.isValid()) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002665 const Decl &D = EndN->getCodeDecl();
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002666 L = PathDiagnosticLocation(D.getBodyRBrace(), SMgr);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002667 }
Mike Stump11289f42009-09-09 15:08:12 +00002668
Ted Kremenek6bd78702009-04-29 18:50:19 +00002669 std::string sbuf;
2670 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +00002671
Ted Kremenek6bd78702009-04-29 18:50:19 +00002672 os << "Object allocated on line " << AllocLine;
Mike Stump11289f42009-09-09 15:08:12 +00002673
Ted Kremenek6bd78702009-04-29 18:50:19 +00002674 if (FirstBinding)
Mike Stump11289f42009-09-09 15:08:12 +00002675 os << " and stored into '" << FirstBinding->getString() << '\'';
2676
Ted Kremenek6bd78702009-04-29 18:50:19 +00002677 // Get the retain count.
2678 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002679
Ted Kremenek6bd78702009-04-29 18:50:19 +00002680 if (RV->getKind() == RefVal::ErrorLeakReturned) {
2681 // FIXME: Per comments in rdar://6320065, "create" only applies to CF
2682 // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership
2683 // to the caller for NS objects.
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002684 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002685 os << " is returned from a method whose name ('"
Ted Kremenek223a7d52009-04-29 23:03:22 +00002686 << MD.getSelector().getAsString()
Ted Kremenek6bd78702009-04-29 18:50:19 +00002687 << "') does not contain 'copy' or otherwise starts with"
2688 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenekd6bef2e2009-04-29 22:25:52 +00002689 " in the Memory Management Guide for Cocoa (object leaked)";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002690 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00002691 else if (RV->getKind() == RefVal::ErrorGCLeakReturned) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002692 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremenekdee56e32009-05-10 06:25:57 +00002693 os << " and returned from method '" << MD.getSelector().getAsString()
Ted Kremenek1f8e4342009-05-10 16:52:15 +00002694 << "' is potentially leaked when using garbage collection. Callers "
2695 "of this method do not expect a returned object with a +1 retain "
2696 "count since they expect the object to be managed by the garbage "
2697 "collector";
Ted Kremenekdee56e32009-05-10 06:25:57 +00002698 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002699 else
2700 os << " is no longer referenced after this point and has a retain count of"
Ted Kremenekd6bef2e2009-04-29 22:25:52 +00002701 " +" << RV->getCount() << " (object leaked)";
Mike Stump11289f42009-09-09 15:08:12 +00002702
Ted Kremenek6bd78702009-04-29 18:50:19 +00002703 return new PathDiagnosticEventPiece(L, os.str());
2704}
2705
Ted Kremenek6bd78702009-04-29 18:50:19 +00002706CFRefLeakReport::CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002707 ExplodedNode *n,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002708 SymbolRef sym, GRExprEngine& Eng)
Mike Stump11289f42009-09-09 15:08:12 +00002709: CFRefReport(D, tf, n, sym) {
2710
Ted Kremenek6bd78702009-04-29 18:50:19 +00002711 // Most bug reports are cached at the location where they occured.
2712 // With leaks, we want to unique them by the location where they were
2713 // allocated, and only report a single path. To do this, we need to find
2714 // the allocation site of a piece of tracked memory, which we do via a
2715 // call to GetAllocationSite. This will walk the ExplodedGraph backwards.
2716 // Note that this is *not* the trimmed graph; we are guaranteed, however,
2717 // that all ancestor nodes that represent the allocation site have the
2718 // same SourceLocation.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002719 const ExplodedNode* AllocNode = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002720
Ted Kremenek6bd78702009-04-29 18:50:19 +00002721 llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding.
Ted Kremenek8c8fb482009-05-08 23:32:51 +00002722 GetAllocationSite(Eng.getStateManager(), getEndNode(), getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00002723
Ted Kremenek6bd78702009-04-29 18:50:19 +00002724 // Get the SourceLocation for the allocation site.
2725 ProgramPoint P = AllocNode->getLocation();
2726 AllocSite = cast<PostStmt>(P).getStmt()->getLocStart();
Mike Stump11289f42009-09-09 15:08:12 +00002727
Ted Kremenek6bd78702009-04-29 18:50:19 +00002728 // Fill in the description of the bug.
2729 Description.clear();
2730 llvm::raw_string_ostream os(Description);
2731 SourceManager& SMgr = Eng.getContext().getSourceManager();
2732 unsigned AllocLine = SMgr.getInstantiationLineNumber(AllocSite);
Ted Kremenekf1e76672009-05-02 19:05:19 +00002733 os << "Potential leak ";
2734 if (tf.isGCEnabled()) {
2735 os << "(when using garbage collection) ";
Mike Stump11289f42009-09-09 15:08:12 +00002736 }
Ted Kremenekf1e76672009-05-02 19:05:19 +00002737 os << "of an object allocated on line " << AllocLine;
Mike Stump11289f42009-09-09 15:08:12 +00002738
Ted Kremenek6bd78702009-04-29 18:50:19 +00002739 // FIXME: AllocBinding doesn't get populated for RegionStore yet.
2740 if (AllocBinding)
2741 os << " and stored into '" << AllocBinding->getString() << '\'';
2742}
2743
2744//===----------------------------------------------------------------------===//
2745// Main checker logic.
2746//===----------------------------------------------------------------------===//
2747
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002748/// GetReturnType - Used to get the return type of a message expression or
2749/// function call with the intention of affixing that type to a tracked symbol.
2750/// While the the return type can be queried directly from RetEx, when
2751/// invoking class methods we augment to the return type to be that of
2752/// a pointer to the class (as opposed it just being id).
Steve Naroff7cae42b2009-07-10 23:34:53 +00002753static QualType GetReturnType(const Expr* RetE, ASTContext& Ctx) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002754 QualType RetTy = RetE->getType();
Steve Naroff7cae42b2009-07-10 23:34:53 +00002755 // If RetE is not a message expression just return its type.
2756 // If RetE is a message expression, return its types if it is something
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002757 /// more specific than id.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002758 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
John McCall9dd450b2009-09-21 23:43:11 +00002759 if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
Mike Stump11289f42009-09-09 15:08:12 +00002760 if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
Steve Naroff7cae42b2009-07-10 23:34:53 +00002761 PT->isObjCClassType()) {
2762 // At this point we know the return type of the message expression is
2763 // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
2764 // is a call to a class method whose type we can resolve. In such
2765 // cases, promote the return type to XXX* (where XXX is the class).
Mike Stump11289f42009-09-09 15:08:12 +00002766 const ObjCInterfaceDecl *D = ME->getClassInfo().first;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002767 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
2768 }
Mike Stump11289f42009-09-09 15:08:12 +00002769
Steve Naroff7cae42b2009-07-10 23:34:53 +00002770 return RetTy;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002771}
2772
Zhongxing Xu20227f72009-08-06 01:32:16 +00002773void CFRefCount::EvalSummary(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002774 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002775 GRStmtNodeBuilder& Builder,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002776 Expr* Ex,
2777 Expr* Receiver,
Ted Kremenekff606a12009-05-04 04:57:00 +00002778 const RetainSummary& Summ,
Ted Kremenek5bee5c42009-12-03 08:25:47 +00002779 const MemRegion *Callee,
Zhongxing Xuac129432009-04-20 05:24:46 +00002780 ExprIterator arg_beg, ExprIterator arg_end,
Zhongxing Xuaf353292009-12-02 05:49:12 +00002781 ExplodedNode* Pred, const GRState *state) {
Ted Kremenek821537e2008-05-06 02:41:27 +00002782
2783 // Evaluate the effect of the arguments.
Ted Kremenek8cb96e92008-04-16 04:28:53 +00002784 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek68d73d12008-03-12 01:21:45 +00002785 unsigned idx = 0;
Ted Kremenek988990f2008-04-11 18:40:51 +00002786 Expr* ErrorExpr = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002787 SymbolRef ErrorSym = 0;
2788
Ted Kremeneke5716cba2009-12-03 03:27:11 +00002789 llvm::SmallVector<const MemRegion*, 10> RegionsToInvalidate;
2790
Mike Stump11289f42009-09-09 15:08:12 +00002791 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
2792 SVal V = state->getSValAsScalarOrLoc(*I);
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002793 SymbolRef Sym = V.getAsLocSymbol();
Ted Kremenek804fc232009-03-04 00:13:50 +00002794
Ted Kremenek3e31c262009-03-26 03:35:11 +00002795 if (Sym)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002796 if (RefBindings::data_type* T = state->get<RefBindings>(Sym)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002797 state = Update(state, Sym, *T, Summ.getArg(idx), hasErr);
Ted Kremenekc52f9392009-02-24 19:15:11 +00002798 if (hasErr) {
Ted Kremenek988990f2008-04-11 18:40:51 +00002799 ErrorExpr = *I;
Ted Kremenek4963d112008-07-07 16:21:19 +00002800 ErrorSym = Sym;
Ted Kremenek988990f2008-04-11 18:40:51 +00002801 break;
Mike Stump11289f42009-09-09 15:08:12 +00002802 }
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002803 continue;
Ted Kremenekc52f9392009-02-24 19:15:11 +00002804 }
Ted Kremenekae529272008-07-09 18:11:16 +00002805
Ted Kremenekf9539d02009-09-22 04:48:39 +00002806 tryAgain:
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002807 if (isa<Loc>(V)) {
2808 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002809 if (Summ.getArg(idx) == DoNothingByRef)
Ted Kremenekae529272008-07-09 18:11:16 +00002810 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002811
2812 // Invalidate the value of the variable passed by reference.
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002813 const MemRegion *R = MR->getRegion();
Ted Kremeneke5716cba2009-12-03 03:27:11 +00002814
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002815 // Are we dealing with an ElementRegion? If the element type is
2816 // a basic integer type (e.g., char, int) and the underying region
2817 // is a variable region then strip off the ElementRegion.
2818 // FIXME: We really need to think about this for the general case
2819 // as sometimes we are reasoning about arrays and other times
2820 // about (char*), etc., is just a form of passing raw bytes.
2821 // e.g., void *p = alloca(); foo((char*)p);
2822 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
2823 // Checking for 'integral type' is probably too promiscuous, but
2824 // we'll leave it in for now until we have a systematic way of
2825 // handling all of these cases. Eventually we need to come up
2826 // with an interface to StoreManager so that this logic can be
2827 // approriately delegated to the respective StoreManagers while
2828 // still allowing us to do checker-specific logic (e.g.,
2829 // invalidating reference counts), probably via callbacks.
2830 if (ER->getElementType()->isIntegralType()) {
2831 const MemRegion *superReg = ER->getSuperRegion();
2832 if (isa<VarRegion>(superReg) || isa<FieldRegion>(superReg) ||
2833 isa<ObjCIvarRegion>(superReg))
2834 R = cast<TypedRegion>(superReg);
Ted Kremenek0626df42009-05-06 18:19:24 +00002835 }
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002836 // FIXME: What about layers of ElementRegions?
2837 }
Ted Kremeneke5716cba2009-12-03 03:27:11 +00002838
2839 // Mark this region for invalidation. We batch invalidate regions
2840 // below for efficiency.
2841 RegionsToInvalidate.push_back(R);
2842 continue;
Ted Kremenek4d851462008-07-03 23:26:32 +00002843 }
2844 else {
2845 // Nuke all other arguments passed by reference.
Ted Kremenekf9539d02009-09-22 04:48:39 +00002846 // FIXME: is this necessary or correct? This handles the non-Region
2847 // cases. Is it ever valid to store to these?
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002848 state = state->unbindLoc(cast<Loc>(V));
Ted Kremenek4d851462008-07-03 23:26:32 +00002849 }
Ted Kremenek0a86fdb2008-04-11 20:51:02 +00002850 }
Ted Kremenekf9539d02009-09-22 04:48:39 +00002851 else if (isa<nonloc::LocAsInteger>(V)) {
2852 // If we are passing a location wrapped as an integer, unwrap it and
2853 // invalidate the values referred by the location.
2854 V = cast<nonloc::LocAsInteger>(V).getLoc();
2855 goto tryAgain;
2856 }
Mike Stump11289f42009-09-09 15:08:12 +00002857 }
Ted Kremeneke5716cba2009-12-03 03:27:11 +00002858
Ted Kremenek5bee5c42009-12-03 08:25:47 +00002859 // Block calls result in all captured values passed-via-reference to be
2860 // invalidated.
2861 if (const BlockDataRegion *BR = dyn_cast_or_null<BlockDataRegion>(Callee)) {
2862 RegionsToInvalidate.push_back(BR);
2863 }
2864
Ted Kremeneke5716cba2009-12-03 03:27:11 +00002865 // Invalidate regions we designed for invalidation use the batch invalidation
2866 // API.
2867 if (!RegionsToInvalidate.empty()) {
2868 // FIXME: We can have collisions on the conjured symbol if the
2869 // expression *I also creates conjured symbols. We probably want
2870 // to identify conjured symbols by an expression pair: the enclosing
2871 // expression (the context) and the expression itself. This should
2872 // disambiguate conjured symbols.
2873 unsigned Count = Builder.getCurrentBlockCount();
2874 StoreManager& StoreMgr = Eng.getStateManager().getStoreManager();
2875
2876
2877 StoreManager::InvalidatedSymbols IS;
2878 state = StoreMgr.InvalidateRegions(state, RegionsToInvalidate.data(),
2879 RegionsToInvalidate.data() +
2880 RegionsToInvalidate.size(),
2881 Ex, Count, &IS);
2882 for (StoreManager::InvalidatedSymbols::iterator I = IS.begin(),
2883 E = IS.end(); I!=E; ++I) {
2884 // Remove any existing reference-count binding.
2885 state = state->remove<RefBindings>(*I);
2886 }
2887 }
Mike Stump11289f42009-09-09 15:08:12 +00002888
2889 // Evaluate the effect on the message receiver.
Ted Kremenek821537e2008-05-06 02:41:27 +00002890 if (!ErrorExpr && Receiver) {
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002891 SymbolRef Sym = state->getSValAsScalarOrLoc(Receiver).getAsLocSymbol();
Ted Kremenek3e31c262009-03-26 03:35:11 +00002892 if (Sym) {
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002893 if (const RefVal* T = state->get<RefBindings>(Sym)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002894 state = Update(state, Sym, *T, Summ.getReceiverEffect(), hasErr);
Ted Kremenekc52f9392009-02-24 19:15:11 +00002895 if (hasErr) {
Ted Kremenek821537e2008-05-06 02:41:27 +00002896 ErrorExpr = Receiver;
Ted Kremenek4963d112008-07-07 16:21:19 +00002897 ErrorSym = Sym;
Ted Kremenek821537e2008-05-06 02:41:27 +00002898 }
Ted Kremenekc52f9392009-02-24 19:15:11 +00002899 }
Ted Kremenek821537e2008-05-06 02:41:27 +00002900 }
2901 }
Mike Stump11289f42009-09-09 15:08:12 +00002902
2903 // Process any errors.
Ted Kremenek8cb96e92008-04-16 04:28:53 +00002904 if (hasErr) {
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00002905 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek396f4362008-04-18 03:39:05 +00002906 hasErr, ErrorSym);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002907 return;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00002908 }
Mike Stump11289f42009-09-09 15:08:12 +00002909
2910 // Consult the summary for the return value.
Ted Kremenekff606a12009-05-04 04:57:00 +00002911 RetEffect RE = Summ.getRetEffect();
Mike Stump11289f42009-09-09 15:08:12 +00002912
Ted Kremenek1272f702009-05-12 20:06:54 +00002913 if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
2914 assert(Receiver);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002915 SVal V = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenek1272f702009-05-12 20:06:54 +00002916 bool found = false;
2917 if (SymbolRef Sym = V.getAsLocSymbol())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002918 if (state->get<RefBindings>(Sym)) {
Ted Kremenek1272f702009-05-12 20:06:54 +00002919 found = true;
2920 RE = Summaries.getObjAllocRetEffect();
2921 }
2922
2923 if (!found)
2924 RE = RetEffect::MakeNoRet();
Mike Stump11289f42009-09-09 15:08:12 +00002925 }
2926
Ted Kremenek68d73d12008-03-12 01:21:45 +00002927 switch (RE.getKind()) {
2928 default:
2929 assert (false && "Unhandled RetEffect."); break;
Mike Stump11289f42009-09-09 15:08:12 +00002930
2931 case RetEffect::NoRet: {
Ted Kremenek831f3272008-04-11 20:23:24 +00002932 // Make up a symbol for the return value (not reference counted).
Ted Kremenek1642bda2009-06-26 00:05:51 +00002933 // FIXME: Most of this logic is not specific to the retain/release
2934 // checker.
Mike Stump11289f42009-09-09 15:08:12 +00002935
Ted Kremenek21387322008-10-17 22:23:12 +00002936 // FIXME: We eventually should handle structs and other compound types
2937 // that are returned by value.
Mike Stump11289f42009-09-09 15:08:12 +00002938
Ted Kremenek21387322008-10-17 22:23:12 +00002939 QualType T = Ex->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002940
Ted Kremenek16866d62008-11-13 06:10:40 +00002941 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenek831f3272008-04-11 20:23:24 +00002942 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekf2489ea2009-04-09 22:22:44 +00002943 ValueManager &ValMgr = Eng.getValueManager();
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002944 SVal X = ValMgr.getConjuredSymbolVal(NULL, Ex, T, Count);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002945 state = state->BindExpr(Ex, X, false);
Mike Stump11289f42009-09-09 15:08:12 +00002946 }
2947
Ted Kremenek4b772092008-04-10 23:44:06 +00002948 break;
Ted Kremenek21387322008-10-17 22:23:12 +00002949 }
Mike Stump11289f42009-09-09 15:08:12 +00002950
Ted Kremenek68d73d12008-03-12 01:21:45 +00002951 case RetEffect::Alias: {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002952 unsigned idx = RE.getIndex();
Ted Kremenek08e17112008-06-17 02:43:46 +00002953 assert (arg_end >= arg_beg);
Ted Kremenek00daccd2008-05-05 22:11:16 +00002954 assert (idx < (unsigned) (arg_end - arg_beg));
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002955 SVal V = state->getSValAsScalarOrLoc(*(arg_beg+idx));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002956 state = state->BindExpr(Ex, V, false);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002957 break;
2958 }
Mike Stump11289f42009-09-09 15:08:12 +00002959
Ted Kremenek821537e2008-05-06 02:41:27 +00002960 case RetEffect::ReceiverAlias: {
2961 assert (Receiver);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002962 SVal V = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002963 state = state->BindExpr(Ex, V, false);
Ted Kremenek821537e2008-05-06 02:41:27 +00002964 break;
2965 }
Mike Stump11289f42009-09-09 15:08:12 +00002966
Ted Kremenekab4a8b52008-06-23 18:02:52 +00002967 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek68d73d12008-03-12 01:21:45 +00002968 case RetEffect::OwnedSymbol: {
2969 unsigned Count = Builder.getCurrentBlockCount();
Mike Stump11289f42009-09-09 15:08:12 +00002970 ValueManager &ValMgr = Eng.getValueManager();
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002971 SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002972 QualType RetT = GetReturnType(Ex, ValMgr.getContext());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002973 state = state->set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(),
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002974 RetT));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002975 state = state->BindExpr(Ex, ValMgr.makeLoc(Sym), false);
Ted Kremenek0b891a32009-03-09 22:46:49 +00002976
2977 // FIXME: Add a flag to the checker where allocations are assumed to
2978 // *not fail.
2979#if 0
Ted Kremenek2e561dd2009-01-28 22:27:59 +00002980 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) {
2981 bool isFeasible;
2982 state = state.Assume(loc::SymbolVal(Sym), true, isFeasible);
Mike Stump11289f42009-09-09 15:08:12 +00002983 assert(isFeasible && "Cannot assume fresh symbol is non-null.");
Ted Kremenek2e561dd2009-01-28 22:27:59 +00002984 }
Ted Kremenek0b891a32009-03-09 22:46:49 +00002985#endif
Mike Stump11289f42009-09-09 15:08:12 +00002986
Ted Kremenek68d73d12008-03-12 01:21:45 +00002987 break;
2988 }
Mike Stump11289f42009-09-09 15:08:12 +00002989
Ted Kremeneke6633562009-04-27 19:14:45 +00002990 case RetEffect::GCNotOwnedSymbol:
Ted Kremenek68d73d12008-03-12 01:21:45 +00002991 case RetEffect::NotOwnedSymbol: {
2992 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002993 ValueManager &ValMgr = Eng.getValueManager();
2994 SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002995 QualType RetT = GetReturnType(Ex, ValMgr.getContext());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002996 state = state->set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002997 RetT));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002998 state = state->BindExpr(Ex, ValMgr.makeLoc(Sym), false);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002999 break;
3000 }
3001 }
Mike Stump11289f42009-09-09 15:08:12 +00003002
Ted Kremenekd84fff62009-02-18 02:00:25 +00003003 // Generate a sink node if we are at the end of a path.
Zhongxing Xu107f7592009-08-06 12:48:26 +00003004 ExplodedNode *NewNode =
Ted Kremenekff606a12009-05-04 04:57:00 +00003005 Summ.isEndPath() ? Builder.MakeSinkNode(Dst, Ex, Pred, state)
3006 : Builder.MakeNode(Dst, Ex, Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003007
Ted Kremenekd84fff62009-02-18 02:00:25 +00003008 // Annotate the edge with summary we used.
Ted Kremenekff606a12009-05-04 04:57:00 +00003009 if (NewNode) SummaryLog[NewNode] = &Summ;
Ted Kremenek00daccd2008-05-05 22:11:16 +00003010}
3011
3012
Zhongxing Xu20227f72009-08-06 01:32:16 +00003013void CFRefCount::EvalCall(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00003014 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003015 GRStmtNodeBuilder& Builder,
Zhongxing Xu27f17422008-10-17 05:57:07 +00003016 CallExpr* CE, SVal L,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003017 ExplodedNode* Pred) {
Ted Kremeneke6a27802009-11-25 01:35:18 +00003018
3019 RetainSummary *Summ = 0;
3020
3021 // FIXME: Better support for blocks. For now we stop tracking anything
3022 // that is passed to blocks.
3023 // FIXME: Need to handle variables that are "captured" by the block.
Ted Kremenekb63ad7a2009-11-25 23:53:07 +00003024 if (dyn_cast_or_null<BlockDataRegion>(L.getAsRegion())) {
Ted Kremeneke6a27802009-11-25 01:35:18 +00003025 Summ = Summaries.getPersistentStopSummary();
3026 }
3027 else {
3028 const FunctionDecl* FD = L.getAsFunctionDecl();
3029 Summ = !FD ? Summaries.getDefaultSummary() :
3030 Summaries.getSummary(const_cast<FunctionDecl*>(FD));
3031 }
Mike Stump11289f42009-09-09 15:08:12 +00003032
Ted Kremenekff606a12009-05-04 04:57:00 +00003033 assert(Summ);
Ted Kremenek5bee5c42009-12-03 08:25:47 +00003034 EvalSummary(Dst, Eng, Builder, CE, 0, *Summ, L.getAsRegion(),
Zhongxing Xuaf353292009-12-02 05:49:12 +00003035 CE->arg_begin(), CE->arg_end(), Pred, Builder.GetState(Pred));
Ted Kremenekea6507f2008-03-06 00:08:09 +00003036}
Ted Kremenek819e9b62008-03-11 06:39:11 +00003037
Zhongxing Xu20227f72009-08-06 01:32:16 +00003038void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003039 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003040 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003041 ObjCMessageExpr* ME,
Zhongxing Xuaf353292009-12-02 05:49:12 +00003042 ExplodedNode* Pred,
3043 const GRState *state) {
Ted Kremeneka2968e52009-11-13 01:54:21 +00003044 RetainSummary *Summ =
3045 ME->getReceiver()
Zhongxing Xuaf353292009-12-02 05:49:12 +00003046 ? Summaries.getInstanceMethodSummary(ME, state,Pred->getLocationContext())
Ted Kremeneka2968e52009-11-13 01:54:21 +00003047 : Summaries.getClassMethodSummary(ME);
Mike Stump11289f42009-09-09 15:08:12 +00003048
Ted Kremeneka2968e52009-11-13 01:54:21 +00003049 assert(Summ && "RetainSummary is null");
Ted Kremenek5bee5c42009-12-03 08:25:47 +00003050 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), *Summ, NULL,
Zhongxing Xuaf353292009-12-02 05:49:12 +00003051 ME->arg_begin(), ME->arg_end(), Pred, state);
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003052}
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003053
3054namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00003055class StopTrackingCallback : public SymbolVisitor {
Ted Kremenek89a303c2009-06-18 00:49:02 +00003056 const GRState *state;
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003057public:
Ted Kremenek89a303c2009-06-18 00:49:02 +00003058 StopTrackingCallback(const GRState *st) : state(st) {}
3059 const GRState *getState() const { return state; }
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003060
3061 bool VisitSymbol(SymbolRef sym) {
Ted Kremenek89a303c2009-06-18 00:49:02 +00003062 state = state->remove<RefBindings>(sym);
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003063 return true;
3064 }
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003065};
3066} // end anonymous namespace
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003067
Mike Stump11289f42009-09-09 15:08:12 +00003068
3069void CFRefCount::EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val) {
3070 // Are we storing to something that causes the value to "escape"?
Ted Kremenek71454892008-04-16 20:40:59 +00003071 bool escapes = false;
Mike Stump11289f42009-09-09 15:08:12 +00003072
Ted Kremeneke86755e2008-10-18 03:49:51 +00003073 // A value escapes in three possible cases (this may change):
3074 //
3075 // (1) we are binding to something that is not a memory region.
3076 // (2) we are binding to a memregion that does not have stack storage
3077 // (3) we are binding to a memregion with stack storage that the store
Mike Stump11289f42009-09-09 15:08:12 +00003078 // does not understand.
Ted Kremenek89a303c2009-06-18 00:49:02 +00003079 const GRState *state = B.getState();
Ted Kremeneke86755e2008-10-18 03:49:51 +00003080
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003081 if (!isa<loc::MemRegionVal>(location))
Ted Kremenek71454892008-04-16 20:40:59 +00003082 escapes = true;
Ted Kremenek5ca90a22008-10-04 05:50:14 +00003083 else {
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003084 const MemRegion* R = cast<loc::MemRegionVal>(location).getRegion();
Ted Kremenek404b1322009-06-23 18:05:21 +00003085 escapes = !R->hasStackStorage();
Mike Stump11289f42009-09-09 15:08:12 +00003086
Ted Kremeneke86755e2008-10-18 03:49:51 +00003087 if (!escapes) {
3088 // To test (3), generate a new state with the binding removed. If it is
3089 // the same state, then it escapes (since the store cannot represent
3090 // the binding).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003091 escapes = (state == (state->bindLoc(cast<Loc>(location), UnknownVal())));
Ted Kremeneke86755e2008-10-18 03:49:51 +00003092 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +00003093 }
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003094
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003095 // If our store can represent the binding and we aren't storing to something
3096 // that doesn't have local storage then just return and have the simulation
3097 // state continue as is.
3098 if (!escapes)
3099 return;
Ted Kremeneke86755e2008-10-18 03:49:51 +00003100
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003101 // Otherwise, find all symbols referenced by 'val' that we are tracking
3102 // and stop tracking them.
Ted Kremenek89a303c2009-06-18 00:49:02 +00003103 B.MakeNode(state->scanReachableSymbols<StopTrackingCallback>(val).getState());
Ted Kremenekcbf4c612008-04-16 22:32:20 +00003104}
3105
Ted Kremeneka506fec2008-04-17 18:12:53 +00003106 // Return statements.
3107
Zhongxing Xu20227f72009-08-06 01:32:16 +00003108void CFRefCount::EvalReturn(ExplodedNodeSet& Dst,
Ted Kremeneka506fec2008-04-17 18:12:53 +00003109 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003110 GRStmtNodeBuilder& Builder,
Ted Kremeneka506fec2008-04-17 18:12:53 +00003111 ReturnStmt* S,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003112 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00003113
Ted Kremeneka506fec2008-04-17 18:12:53 +00003114 Expr* RetE = S->getRetValue();
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003115 if (!RetE)
Ted Kremeneka506fec2008-04-17 18:12:53 +00003116 return;
Mike Stump11289f42009-09-09 15:08:12 +00003117
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003118 const GRState *state = Builder.GetState(Pred);
3119 SymbolRef Sym = state->getSValAsScalarOrLoc(RetE).getAsLocSymbol();
Mike Stump11289f42009-09-09 15:08:12 +00003120
Ted Kremenek3e31c262009-03-26 03:35:11 +00003121 if (!Sym)
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003122 return;
Mike Stump11289f42009-09-09 15:08:12 +00003123
Ted Kremeneka506fec2008-04-17 18:12:53 +00003124 // Get the reference count binding (if any).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003125 const RefVal* T = state->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00003126
Ted Kremeneka506fec2008-04-17 18:12:53 +00003127 if (!T)
3128 return;
Mike Stump11289f42009-09-09 15:08:12 +00003129
3130 // Change the reference count.
3131 RefVal X = *T;
3132
3133 switch (X.getKind()) {
3134 case RefVal::Owned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00003135 unsigned cnt = X.getCount();
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003136 assert (cnt > 0);
Ted Kremenek3978f792009-05-10 05:11:21 +00003137 X.setCount(cnt - 1);
3138 X = X ^ RefVal::ReturnedOwned;
Ted Kremeneka506fec2008-04-17 18:12:53 +00003139 break;
3140 }
Mike Stump11289f42009-09-09 15:08:12 +00003141
Ted Kremeneka506fec2008-04-17 18:12:53 +00003142 case RefVal::NotOwned: {
3143 unsigned cnt = X.getCount();
Ted Kremenek3978f792009-05-10 05:11:21 +00003144 if (cnt) {
3145 X.setCount(cnt - 1);
3146 X = X ^ RefVal::ReturnedOwned;
3147 }
3148 else {
3149 X = X ^ RefVal::ReturnedNotOwned;
3150 }
Ted Kremeneka506fec2008-04-17 18:12:53 +00003151 break;
3152 }
Mike Stump11289f42009-09-09 15:08:12 +00003153
3154 default:
Ted Kremeneka506fec2008-04-17 18:12:53 +00003155 return;
3156 }
Mike Stump11289f42009-09-09 15:08:12 +00003157
Ted Kremeneka506fec2008-04-17 18:12:53 +00003158 // Update the binding.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003159 state = state->set<RefBindings>(Sym, X);
Ted Kremenek6bd78702009-04-29 18:50:19 +00003160 Pred = Builder.MakeNode(Dst, S, Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003161
Ted Kremenekb4e27a12009-04-30 05:51:50 +00003162 // Did we cache out?
3163 if (!Pred)
3164 return;
Mike Stump11289f42009-09-09 15:08:12 +00003165
Ted Kremenek3978f792009-05-10 05:11:21 +00003166 // Update the autorelease counts.
3167 static unsigned autoreleasetag = 0;
3168 GenericNodeBuilder Bd(Builder, S, &autoreleasetag);
3169 bool stop = false;
3170 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state , Bd, Pred, Eng, Sym,
3171 X, stop);
Mike Stump11289f42009-09-09 15:08:12 +00003172
Ted Kremenek3978f792009-05-10 05:11:21 +00003173 // Did we cache out?
3174 if (!Pred || stop)
3175 return;
Mike Stump11289f42009-09-09 15:08:12 +00003176
Ted Kremenek3978f792009-05-10 05:11:21 +00003177 // Get the updated binding.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003178 T = state->get<RefBindings>(Sym);
Ted Kremenek3978f792009-05-10 05:11:21 +00003179 assert(T);
3180 X = *T;
Mike Stump11289f42009-09-09 15:08:12 +00003181
Ted Kremenek6bd78702009-04-29 18:50:19 +00003182 // Any leaks or other errors?
3183 if (X.isReturnedOwned() && X.getCount() == 0) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00003184 Decl const *CD = &Pred->getCodeDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003185 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00003186 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
Ted Kremenekdee56e32009-05-10 06:25:57 +00003187 RetEffect RE = Summ.getRetEffect();
3188 bool hasError = false;
3189
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003190 if (RE.getKind() != RetEffect::NoRet) {
3191 if (isGCEnabled() && RE.getObjKind() == RetEffect::ObjC) {
3192 // Things are more complicated with garbage collection. If the
3193 // returned object is suppose to be an Objective-C object, we have
3194 // a leak (as the caller expects a GC'ed object) because no
3195 // method should return ownership unless it returns a CF object.
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003196 hasError = true;
Ted Kremenek8070b822009-10-14 23:58:34 +00003197 X = X ^ RefVal::ErrorGCLeakReturned;
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003198 }
3199 else if (!RE.isOwned()) {
3200 // Either we are using GC and the returned object is a CF type
3201 // or we aren't using GC. In either case, we expect that the
Mike Stump11289f42009-09-09 15:08:12 +00003202 // enclosing method is expected to return ownership.
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003203 hasError = true;
3204 X = X ^ RefVal::ErrorLeakReturned;
3205 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00003206 }
Mike Stump11289f42009-09-09 15:08:12 +00003207
3208 if (hasError) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00003209 // Generate an error node.
Ted Kremenekdee56e32009-05-10 06:25:57 +00003210 static int ReturnOwnLeakTag = 0;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003211 state = state->set<RefBindings>(Sym, X);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003212 ExplodedNode *N =
Zhongxing Xue1190f72009-08-15 03:17:38 +00003213 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3214 &ReturnOwnLeakTag), state, Pred);
Ted Kremenekdee56e32009-05-10 06:25:57 +00003215 if (N) {
3216 CFRefReport *report =
Ted Kremenekb4e27a12009-04-30 05:51:50 +00003217 new CFRefLeakReport(*static_cast<CFRefBug*>(leakAtReturn), *this,
3218 N, Sym, Eng);
3219 BR->EmitReport(report);
3220 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00003221 }
Mike Stump11289f42009-09-09 15:08:12 +00003222 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00003223 }
3224 else if (X.isReturnedNotOwned()) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00003225 Decl const *CD = &Pred->getCodeDecl();
Ted Kremenekdee56e32009-05-10 06:25:57 +00003226 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
3227 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
3228 if (Summ.getRetEffect().isOwned()) {
3229 // Trying to return a not owned object to a caller expecting an
3230 // owned object.
Mike Stump11289f42009-09-09 15:08:12 +00003231
Ted Kremenekdee56e32009-05-10 06:25:57 +00003232 static int ReturnNotOwnedForOwnedTag = 0;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003233 state = state->set<RefBindings>(Sym, X ^ RefVal::ErrorReturnedNotOwned);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003234 if (ExplodedNode *N =
Zhongxing Xue1190f72009-08-15 03:17:38 +00003235 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3236 &ReturnNotOwnedForOwnedTag),
3237 state, Pred)) {
Ted Kremenekdee56e32009-05-10 06:25:57 +00003238 CFRefReport *report =
3239 new CFRefReport(*static_cast<CFRefBug*>(returnNotOwnedForOwned),
3240 *this, N, Sym);
3241 BR->EmitReport(report);
3242 }
3243 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00003244 }
3245 }
Ted Kremeneka506fec2008-04-17 18:12:53 +00003246}
3247
Ted Kremenek4d837282008-04-18 19:23:43 +00003248// Assumptions.
3249
Ted Kremenekf9906842009-06-18 22:57:13 +00003250const GRState* CFRefCount::EvalAssume(const GRState *state,
3251 SVal Cond, bool Assumption) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003252
3253 // FIXME: We may add to the interface of EvalAssume the list of symbols
3254 // whose assumptions have changed. For now we just iterate through the
3255 // bindings and check if any of the tracked symbols are NULL. This isn't
Mike Stump11289f42009-09-09 15:08:12 +00003256 // too bad since the number of symbols we will track in practice are
Ted Kremenek4d837282008-04-18 19:23:43 +00003257 // probably small and EvalAssume is only called at branches and a few
3258 // other places.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003259 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003260
Ted Kremenek4d837282008-04-18 19:23:43 +00003261 if (B.isEmpty())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003262 return state;
Mike Stump11289f42009-09-09 15:08:12 +00003263
3264 bool changed = false;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003265 RefBindings::Factory& RefBFactory = state->get_context<RefBindings>();
Ted Kremenek4d837282008-04-18 19:23:43 +00003266
Mike Stump11289f42009-09-09 15:08:12 +00003267 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003268 // Check if the symbol is null (or equal to any constant).
3269 // If this is the case, stop tracking the symbol.
Ted Kremenekf9906842009-06-18 22:57:13 +00003270 if (state->getSymVal(I.getKey())) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003271 changed = true;
3272 B = RefBFactory.Remove(B, I.getKey());
3273 }
3274 }
Mike Stump11289f42009-09-09 15:08:12 +00003275
Ted Kremenek87aab6c2008-08-17 03:20:02 +00003276 if (changed)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003277 state = state->set<RefBindings>(B);
Mike Stump11289f42009-09-09 15:08:12 +00003278
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00003279 return state;
Ted Kremenek4d837282008-04-18 19:23:43 +00003280}
Ted Kremenek819e9b62008-03-11 06:39:11 +00003281
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003282const GRState * CFRefCount::Update(const GRState * state, SymbolRef sym,
Ted Kremenekc52f9392009-02-24 19:15:11 +00003283 RefVal V, ArgEffect E,
3284 RefVal::Kind& hasErr) {
Ted Kremenekf68490a2009-02-18 18:54:33 +00003285
3286 // In GC mode [... release] and [... retain] do nothing.
3287 switch (E) {
3288 default: break;
3289 case IncRefMsg: E = isGCEnabled() ? DoNothing : IncRef; break;
3290 case DecRefMsg: E = isGCEnabled() ? DoNothing : DecRef; break;
Ted Kremenek10452892009-02-18 21:57:45 +00003291 case MakeCollectable: E = isGCEnabled() ? DecRef : DoNothing; break;
Mike Stump11289f42009-09-09 15:08:12 +00003292 case NewAutoreleasePool: E = isGCEnabled() ? DoNothing :
Ted Kremenek50db3d02009-02-23 17:45:03 +00003293 NewAutoreleasePool; break;
Ted Kremenekf68490a2009-02-18 18:54:33 +00003294 }
Mike Stump11289f42009-09-09 15:08:12 +00003295
Ted Kremenekea072e32009-03-17 19:42:23 +00003296 // Handle all use-after-releases.
3297 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
3298 V = V ^ RefVal::ErrorUseAfterRelease;
3299 hasErr = V.getKind();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003300 return state->set<RefBindings>(sym, V);
Mike Stump11289f42009-09-09 15:08:12 +00003301 }
3302
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003303 switch (E) {
3304 default:
3305 assert (false && "Unhandled CFRef transition.");
Mike Stump11289f42009-09-09 15:08:12 +00003306
Ted Kremenekea072e32009-03-17 19:42:23 +00003307 case Dealloc:
3308 // Any use of -dealloc in GC is *bad*.
3309 if (isGCEnabled()) {
3310 V = V ^ RefVal::ErrorDeallocGC;
3311 hasErr = V.getKind();
3312 break;
3313 }
Mike Stump11289f42009-09-09 15:08:12 +00003314
Ted Kremenekea072e32009-03-17 19:42:23 +00003315 switch (V.getKind()) {
3316 default:
3317 assert(false && "Invalid case.");
3318 case RefVal::Owned:
3319 // The object immediately transitions to the released state.
3320 V = V ^ RefVal::Released;
3321 V.clearCounts();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003322 return state->set<RefBindings>(sym, V);
Ted Kremenekea072e32009-03-17 19:42:23 +00003323 case RefVal::NotOwned:
3324 V = V ^ RefVal::ErrorDeallocNotOwned;
3325 hasErr = V.getKind();
3326 break;
Mike Stump11289f42009-09-09 15:08:12 +00003327 }
Ted Kremenekea072e32009-03-17 19:42:23 +00003328 break;
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003329
Ted Kremenek8ec8cf02009-02-25 23:11:49 +00003330 case NewAutoreleasePool:
3331 assert(!isGCEnabled());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003332 return state->add<AutoreleaseStack>(sym);
Mike Stump11289f42009-09-09 15:08:12 +00003333
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003334 case MayEscape:
3335 if (V.getKind() == RefVal::Owned) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003336 V = V ^ RefVal::NotOwned;
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003337 break;
3338 }
Ted Kremenekea072e32009-03-17 19:42:23 +00003339
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003340 // Fall-through.
Mike Stump11289f42009-09-09 15:08:12 +00003341
Ted Kremenekae529272008-07-09 18:11:16 +00003342 case DoNothingByRef:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003343 case DoNothing:
Ted Kremenekc52f9392009-02-24 19:15:11 +00003344 return state;
Ted Kremeneka0e071c2008-06-30 16:57:41 +00003345
Ted Kremenekc7832092009-01-28 21:44:40 +00003346 case Autorelease:
Ted Kremenekea072e32009-03-17 19:42:23 +00003347 if (isGCEnabled())
3348 return state;
Mike Stump11289f42009-09-09 15:08:12 +00003349
Ted Kremenek8c3f0042009-03-20 17:34:15 +00003350 // Update the autorelease counts.
3351 state = SendAutorelease(state, ARCountFactory, sym);
Ted Kremenek3a0516b2009-05-08 20:01:42 +00003352 V = V.autorelease();
Ted Kremenek2d0ff622009-05-09 01:50:57 +00003353 break;
Ted Kremenekd35272f2009-05-09 00:10:05 +00003354
Ted Kremenek821537e2008-05-06 02:41:27 +00003355 case StopTracking:
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003356 return state->remove<RefBindings>(sym);
Ted Kremenek050b91c2008-08-12 18:30:56 +00003357
Mike Stump11289f42009-09-09 15:08:12 +00003358 case IncRef:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003359 switch (V.getKind()) {
3360 default:
3361 assert(false);
3362
3363 case RefVal::Owned:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003364 case RefVal::NotOwned:
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003365 V = V + 1;
Mike Stump11289f42009-09-09 15:08:12 +00003366 break;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003367 case RefVal::Released:
Ted Kremenekea072e32009-03-17 19:42:23 +00003368 // Non-GC cases are handled above.
3369 assert(isGCEnabled());
3370 V = (V ^ RefVal::Owned) + 1;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003371 break;
Mike Stump11289f42009-09-09 15:08:12 +00003372 }
Ted Kremenek4b772092008-04-10 23:44:06 +00003373 break;
Mike Stump11289f42009-09-09 15:08:12 +00003374
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003375 case SelfOwn:
3376 V = V ^ RefVal::NotOwned;
Ted Kremenekf68490a2009-02-18 18:54:33 +00003377 // Fall-through.
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003378 case DecRef:
3379 switch (V.getKind()) {
3380 default:
Ted Kremenekea072e32009-03-17 19:42:23 +00003381 // case 'RefVal::Released' handled above.
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003382 assert (false);
Ted Kremenek050b91c2008-08-12 18:30:56 +00003383
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003384 case RefVal::Owned:
Ted Kremenek551747f2009-02-18 22:57:22 +00003385 assert(V.getCount() > 0);
3386 if (V.getCount() == 1) V = V ^ RefVal::Released;
3387 V = V - 1;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003388 break;
Mike Stump11289f42009-09-09 15:08:12 +00003389
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003390 case RefVal::NotOwned:
3391 if (V.getCount() > 0)
3392 V = V - 1;
Ted Kremenek3c03d522008-04-10 23:09:18 +00003393 else {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003394 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003395 hasErr = V.getKind();
Mike Stump11289f42009-09-09 15:08:12 +00003396 }
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003397 break;
Mike Stump11289f42009-09-09 15:08:12 +00003398
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003399 case RefVal::Released:
Ted Kremenekea072e32009-03-17 19:42:23 +00003400 // Non-GC cases are handled above.
3401 assert(isGCEnabled());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003402 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003403 hasErr = V.getKind();
Mike Stump11289f42009-09-09 15:08:12 +00003404 break;
3405 }
Ted Kremenek4b772092008-04-10 23:44:06 +00003406 break;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003407 }
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003408 return state->set<RefBindings>(sym, V);
Ted Kremenek819e9b62008-03-11 06:39:11 +00003409}
3410
Ted Kremenekce8e8812008-04-09 01:10:13 +00003411//===----------------------------------------------------------------------===//
Ted Kremenek400aae72009-02-05 06:50:21 +00003412// Handle dead symbols and end-of-path.
3413//===----------------------------------------------------------------------===//
3414
Zhongxing Xu20227f72009-08-06 01:32:16 +00003415std::pair<ExplodedNode*, const GRState *>
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003416CFRefCount::HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003417 ExplodedNode* Pred,
Ted Kremenekd35272f2009-05-09 00:10:05 +00003418 GRExprEngine &Eng,
3419 SymbolRef Sym, RefVal V, bool &stop) {
Mike Stump11289f42009-09-09 15:08:12 +00003420
Ted Kremenekd35272f2009-05-09 00:10:05 +00003421 unsigned ACnt = V.getAutoreleaseCount();
3422 stop = false;
3423
3424 // No autorelease counts? Nothing to be done.
3425 if (!ACnt)
3426 return std::make_pair(Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003427
3428 assert(!isGCEnabled() && "Autorelease counts in GC mode?");
Ted Kremenekd35272f2009-05-09 00:10:05 +00003429 unsigned Cnt = V.getCount();
Mike Stump11289f42009-09-09 15:08:12 +00003430
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003431 // FIXME: Handle sending 'autorelease' to already released object.
3432
3433 if (V.getKind() == RefVal::ReturnedOwned)
3434 ++Cnt;
Mike Stump11289f42009-09-09 15:08:12 +00003435
Ted Kremenekd35272f2009-05-09 00:10:05 +00003436 if (ACnt <= Cnt) {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003437 if (ACnt == Cnt) {
3438 V.clearCounts();
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003439 if (V.getKind() == RefVal::ReturnedOwned)
3440 V = V ^ RefVal::ReturnedNotOwned;
3441 else
3442 V = V ^ RefVal::NotOwned;
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003443 }
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003444 else {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003445 V.setCount(Cnt - ACnt);
3446 V.setAutoreleaseCount(0);
3447 }
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003448 state = state->set<RefBindings>(Sym, V);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003449 ExplodedNode *N = Bd.MakeNode(state, Pred);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003450 stop = (N == 0);
3451 return std::make_pair(N, state);
Mike Stump11289f42009-09-09 15:08:12 +00003452 }
Ted Kremenekd35272f2009-05-09 00:10:05 +00003453
3454 // Woah! More autorelease counts then retain counts left.
3455 // Emit hard error.
3456 stop = true;
3457 V = V ^ RefVal::ErrorOverAutorelease;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003458 state = state->set<RefBindings>(Sym, V);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003459
Zhongxing Xu20227f72009-08-06 01:32:16 +00003460 if (ExplodedNode *N = Bd.MakeNode(state, Pred)) {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003461 N->markAsSink();
Mike Stump11289f42009-09-09 15:08:12 +00003462
Ted Kremenek3978f792009-05-10 05:11:21 +00003463 std::string sbuf;
3464 llvm::raw_string_ostream os(sbuf);
Ted Kremenek4785e412009-05-15 06:02:08 +00003465 os << "Object over-autoreleased: object was sent -autorelease";
Ted Kremenek3978f792009-05-10 05:11:21 +00003466 if (V.getAutoreleaseCount() > 1)
3467 os << V.getAutoreleaseCount() << " times";
3468 os << " but the object has ";
3469 if (V.getCount() == 0)
3470 os << "zero (locally visible)";
3471 else
3472 os << "+" << V.getCount();
3473 os << " retain counts";
Mike Stump11289f42009-09-09 15:08:12 +00003474
Ted Kremenekd35272f2009-05-09 00:10:05 +00003475 CFRefReport *report =
3476 new CFRefReport(*static_cast<CFRefBug*>(overAutorelease),
Benjamin Kramer63415532009-11-29 18:27:55 +00003477 *this, N, Sym, os.str());
Ted Kremenekd35272f2009-05-09 00:10:05 +00003478 BR->EmitReport(report);
3479 }
Mike Stump11289f42009-09-09 15:08:12 +00003480
Zhongxing Xu20227f72009-08-06 01:32:16 +00003481 return std::make_pair((ExplodedNode*)0, state);
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003482}
Ted Kremenek884a8992009-05-08 23:09:42 +00003483
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003484const GRState *
3485CFRefCount::HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek884a8992009-05-08 23:09:42 +00003486 llvm::SmallVectorImpl<SymbolRef> &Leaked) {
Mike Stump11289f42009-09-09 15:08:12 +00003487
3488 bool hasLeak = V.isOwned() ||
Ted Kremenek884a8992009-05-08 23:09:42 +00003489 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Mike Stump11289f42009-09-09 15:08:12 +00003490
Ted Kremenek884a8992009-05-08 23:09:42 +00003491 if (!hasLeak)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003492 return state->remove<RefBindings>(sid);
Mike Stump11289f42009-09-09 15:08:12 +00003493
Ted Kremenek884a8992009-05-08 23:09:42 +00003494 Leaked.push_back(sid);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003495 return state->set<RefBindings>(sid, V ^ RefVal::ErrorLeak);
Ted Kremenek884a8992009-05-08 23:09:42 +00003496}
3497
Zhongxing Xu20227f72009-08-06 01:32:16 +00003498ExplodedNode*
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003499CFRefCount::ProcessLeaks(const GRState * state,
Ted Kremenek884a8992009-05-08 23:09:42 +00003500 llvm::SmallVectorImpl<SymbolRef> &Leaked,
3501 GenericNodeBuilder &Builder,
3502 GRExprEngine& Eng,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003503 ExplodedNode *Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00003504
Ted Kremenek884a8992009-05-08 23:09:42 +00003505 if (Leaked.empty())
3506 return Pred;
Mike Stump11289f42009-09-09 15:08:12 +00003507
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003508 // Generate an intermediate node representing the leak point.
Zhongxing Xu20227f72009-08-06 01:32:16 +00003509 ExplodedNode *N = Builder.MakeNode(state, Pred);
Mike Stump11289f42009-09-09 15:08:12 +00003510
Ted Kremenek884a8992009-05-08 23:09:42 +00003511 if (N) {
3512 for (llvm::SmallVectorImpl<SymbolRef>::iterator
3513 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00003514
3515 CFRefBug *BT = static_cast<CFRefBug*>(Pred ? leakWithinFunction
Ted Kremenek884a8992009-05-08 23:09:42 +00003516 : leakAtReturn);
3517 assert(BT && "BugType not initialized.");
3518 CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, *I, Eng);
3519 BR->EmitReport(report);
3520 }
3521 }
Mike Stump11289f42009-09-09 15:08:12 +00003522
Ted Kremenek884a8992009-05-08 23:09:42 +00003523 return N;
3524}
3525
Ted Kremenek400aae72009-02-05 06:50:21 +00003526void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003527 GREndPathNodeBuilder& Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00003528
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003529 const GRState *state = Builder.getState();
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003530 GenericNodeBuilder Bd(Builder);
Mike Stump11289f42009-09-09 15:08:12 +00003531 RefBindings B = state->get<RefBindings>();
Zhongxing Xu20227f72009-08-06 01:32:16 +00003532 ExplodedNode *Pred = 0;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003533
3534 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekd35272f2009-05-09 00:10:05 +00003535 bool stop = false;
3536 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3537 (*I).first,
Mike Stump11289f42009-09-09 15:08:12 +00003538 (*I).second, stop);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003539
3540 if (stop)
3541 return;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003542 }
Mike Stump11289f42009-09-09 15:08:12 +00003543
3544 B = state->get<RefBindings>();
3545 llvm::SmallVector<SymbolRef, 10> Leaked;
3546
Ted Kremenek884a8992009-05-08 23:09:42 +00003547 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
3548 state = HandleSymbolDeath(state, (*I).first, (*I).second, Leaked);
3549
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003550 ProcessLeaks(state, Leaked, Bd, Eng, Pred);
Ted Kremenek400aae72009-02-05 06:50:21 +00003551}
3552
Zhongxing Xu20227f72009-08-06 01:32:16 +00003553void CFRefCount::EvalDeadSymbols(ExplodedNodeSet& Dst,
Ted Kremenek400aae72009-02-05 06:50:21 +00003554 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003555 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003556 ExplodedNode* Pred,
Ted Kremenek400aae72009-02-05 06:50:21 +00003557 Stmt* S,
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003558 const GRState* state,
Ted Kremenek400aae72009-02-05 06:50:21 +00003559 SymbolReaper& SymReaper) {
Ted Kremenek884a8992009-05-08 23:09:42 +00003560
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003561 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003562
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003563 // Update counts from autorelease pools
3564 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3565 E = SymReaper.dead_end(); I != E; ++I) {
3566 SymbolRef Sym = *I;
3567 if (const RefVal* T = B.lookup(Sym)){
3568 // Use the symbol as the tag.
3569 // FIXME: This might not be as unique as we would like.
3570 GenericNodeBuilder Bd(Builder, S, Sym);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003571 bool stop = false;
3572 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3573 Sym, *T, stop);
3574 if (stop)
3575 return;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003576 }
3577 }
Mike Stump11289f42009-09-09 15:08:12 +00003578
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003579 B = state->get<RefBindings>();
Ted Kremenek884a8992009-05-08 23:09:42 +00003580 llvm::SmallVector<SymbolRef, 10> Leaked;
Mike Stump11289f42009-09-09 15:08:12 +00003581
Ted Kremenek400aae72009-02-05 06:50:21 +00003582 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00003583 E = SymReaper.dead_end(); I != E; ++I) {
Ted Kremenek884a8992009-05-08 23:09:42 +00003584 if (const RefVal* T = B.lookup(*I))
3585 state = HandleSymbolDeath(state, *I, *T, Leaked);
Mike Stump11289f42009-09-09 15:08:12 +00003586 }
3587
Ted Kremenek884a8992009-05-08 23:09:42 +00003588 static unsigned LeakPPTag = 0;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003589 {
3590 GenericNodeBuilder Bd(Builder, S, &LeakPPTag);
3591 Pred = ProcessLeaks(state, Leaked, Bd, Eng, Pred);
3592 }
Mike Stump11289f42009-09-09 15:08:12 +00003593
Ted Kremenek884a8992009-05-08 23:09:42 +00003594 // Did we cache out?
3595 if (!Pred)
3596 return;
Mike Stump11289f42009-09-09 15:08:12 +00003597
Ted Kremenek68abaa92009-02-19 23:47:02 +00003598 // Now generate a new node that nukes the old bindings.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003599 RefBindings::Factory& F = state->get_context<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003600
Ted Kremenek68abaa92009-02-19 23:47:02 +00003601 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Ted Kremenek884a8992009-05-08 23:09:42 +00003602 E = SymReaper.dead_end(); I!=E; ++I) B = F.Remove(B, *I);
Mike Stump11289f42009-09-09 15:08:12 +00003603
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003604 state = state->set<RefBindings>(B);
Ted Kremenek68abaa92009-02-19 23:47:02 +00003605 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek400aae72009-02-05 06:50:21 +00003606}
3607
Zhongxing Xu20227f72009-08-06 01:32:16 +00003608void CFRefCount::ProcessNonLeakError(ExplodedNodeSet& Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003609 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003610 Expr* NodeExpr, Expr* ErrorExpr,
3611 ExplodedNode* Pred,
Ted Kremenek400aae72009-02-05 06:50:21 +00003612 const GRState* St,
3613 RefVal::Kind hasErr, SymbolRef Sym) {
3614 Builder.BuildSinks = true;
Zhongxing Xu107f7592009-08-06 12:48:26 +00003615 ExplodedNode *N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
Mike Stump11289f42009-09-09 15:08:12 +00003616
Ted Kremenek2d0ff622009-05-09 01:50:57 +00003617 if (!N)
3618 return;
Mike Stump11289f42009-09-09 15:08:12 +00003619
Ted Kremenek400aae72009-02-05 06:50:21 +00003620 CFRefBug *BT = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003621
Ted Kremenekea072e32009-03-17 19:42:23 +00003622 switch (hasErr) {
3623 default:
3624 assert(false && "Unhandled error.");
3625 return;
3626 case RefVal::ErrorUseAfterRelease:
3627 BT = static_cast<CFRefBug*>(useAfterRelease);
Mike Stump11289f42009-09-09 15:08:12 +00003628 break;
Ted Kremenekea072e32009-03-17 19:42:23 +00003629 case RefVal::ErrorReleaseNotOwned:
3630 BT = static_cast<CFRefBug*>(releaseNotOwned);
3631 break;
3632 case RefVal::ErrorDeallocGC:
3633 BT = static_cast<CFRefBug*>(deallocGC);
3634 break;
3635 case RefVal::ErrorDeallocNotOwned:
3636 BT = static_cast<CFRefBug*>(deallocNotOwned);
3637 break;
Ted Kremenek400aae72009-02-05 06:50:21 +00003638 }
Mike Stump11289f42009-09-09 15:08:12 +00003639
Ted Kremenek48d16452009-02-18 03:48:14 +00003640 CFRefReport *report = new CFRefReport(*BT, *this, N, Sym);
Ted Kremenek400aae72009-02-05 06:50:21 +00003641 report->addRange(ErrorExpr->getSourceRange());
3642 BR->EmitReport(report);
3643}
3644
3645//===----------------------------------------------------------------------===//
Ted Kremenek70a87882009-11-25 22:17:44 +00003646// Pieces of the retain/release checker implemented using a CheckerVisitor.
3647// More pieces of the retain/release checker will be migrated to this interface
3648// (ideally, all of it some day).
3649//===----------------------------------------------------------------------===//
3650
3651namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +00003652class RetainReleaseChecker
Ted Kremenek70a87882009-11-25 22:17:44 +00003653 : public CheckerVisitor<RetainReleaseChecker> {
3654 CFRefCount *TF;
3655public:
3656 RetainReleaseChecker(CFRefCount *tf) : TF(tf) {}
Ted Kremenekf89dcda2009-11-26 02:38:19 +00003657 static void* getTag() { static int x = 0; return &x; }
3658
3659 void PostVisitBlockExpr(CheckerContext &C, const BlockExpr *BE);
Ted Kremenek70a87882009-11-25 22:17:44 +00003660};
3661} // end anonymous namespace
3662
Ted Kremenekf89dcda2009-11-26 02:38:19 +00003663
3664void RetainReleaseChecker::PostVisitBlockExpr(CheckerContext &C,
3665 const BlockExpr *BE) {
3666
3667 // Scan the BlockDecRefExprs for any object the retain/release checker
3668 // may be tracking.
3669 if (!BE->hasBlockDeclRefExprs())
3670 return;
3671
3672 const GRState *state = C.getState();
3673 const BlockDataRegion *R =
3674 cast<BlockDataRegion>(state->getSVal(BE).getAsRegion());
3675
3676 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
3677 E = R->referenced_vars_end();
3678
3679 if (I == E)
3680 return;
3681
Ted Kremenek04af9f22009-12-07 22:05:27 +00003682 // FIXME: For now we invalidate the tracking of all symbols passed to blocks
3683 // via captured variables, even though captured variables result in a copy
3684 // and in implicit increment/decrement of a retain count.
3685 llvm::SmallVector<const MemRegion*, 10> Regions;
3686 const LocationContext *LC = C.getPredecessor()->getLocationContext();
3687 MemRegionManager &MemMgr = C.getValueManager().getRegionManager();
3688
3689 for ( ; I != E; ++I) {
3690 const VarRegion *VR = *I;
3691 if (VR->getSuperRegion() == R) {
3692 VR = MemMgr.getVarRegion(VR->getDecl(), LC);
3693 }
3694 Regions.push_back(VR);
3695 }
3696
3697 state =
3698 state->scanReachableSymbols<StopTrackingCallback>(Regions.data(),
3699 Regions.data() + Regions.size()).getState();
Ted Kremenekf89dcda2009-11-26 02:38:19 +00003700 C.addTransition(state);
3701}
3702
Ted Kremenek70a87882009-11-25 22:17:44 +00003703//===----------------------------------------------------------------------===//
Ted Kremenek4a78c3a2008-04-10 22:16:52 +00003704// Transfer function creation for external clients.
Ted Kremenek819e9b62008-03-11 06:39:11 +00003705//===----------------------------------------------------------------------===//
3706
Ted Kremenek9454227942009-11-25 22:08:49 +00003707void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
3708 BugReporter &BR = Eng.getBugReporter();
3709
3710 useAfterRelease = new UseAfterRelease(this);
3711 BR.Register(useAfterRelease);
3712
3713 releaseNotOwned = new BadRelease(this);
3714 BR.Register(releaseNotOwned);
3715
3716 deallocGC = new DeallocGC(this);
3717 BR.Register(deallocGC);
3718
3719 deallocNotOwned = new DeallocNotOwned(this);
3720 BR.Register(deallocNotOwned);
3721
3722 overAutorelease = new OverAutorelease(this);
3723 BR.Register(overAutorelease);
3724
3725 returnNotOwnedForOwned = new ReturnedNotOwnedForOwned(this);
3726 BR.Register(returnNotOwnedForOwned);
3727
3728 // First register "return" leaks.
3729 const char* name = 0;
3730
3731 if (isGCEnabled())
3732 name = "Leak of returned object when using garbage collection";
3733 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
3734 name = "Leak of returned object when not using garbage collection (GC) in "
3735 "dual GC/non-GC code";
3736 else {
3737 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
3738 name = "Leak of returned object";
3739 }
3740
3741 // Leaks should not be reported if they are post-dominated by a sink.
3742 leakAtReturn = new LeakAtReturn(this, name);
3743 leakAtReturn->setSuppressOnSink(true);
3744 BR.Register(leakAtReturn);
3745
3746 // Second, register leaks within a function/method.
3747 if (isGCEnabled())
3748 name = "Leak of object when using garbage collection";
3749 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
3750 name = "Leak of object when not using garbage collection (GC) in "
3751 "dual GC/non-GC code";
3752 else {
3753 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
3754 name = "Leak";
3755 }
3756
3757 // Leaks should not be reported if they are post-dominated by sinks.
3758 leakWithinFunction = new LeakWithinFunction(this, name);
3759 leakWithinFunction->setSuppressOnSink(true);
3760 BR.Register(leakWithinFunction);
3761
3762 // Save the reference to the BugReporter.
3763 this->BR = &BR;
Ted Kremenek70a87882009-11-25 22:17:44 +00003764
3765 // Register the RetainReleaseChecker with the GRExprEngine object.
3766 // Functionality in CFRefCount will be migrated to RetainReleaseChecker
3767 // over time.
3768 Eng.registerCheck(new RetainReleaseChecker(this));
Ted Kremenek9454227942009-11-25 22:08:49 +00003769}
3770
Ted Kremenekb0f87c42008-04-30 23:47:44 +00003771GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
3772 const LangOptions& lopts) {
Ted Kremenek1f352db2008-07-22 16:21:24 +00003773 return new CFRefCount(Ctx, GCEnabled, lopts);
Mike Stump11289f42009-09-09 15:08:12 +00003774}