blob: 5eac3fdc5d79a7a1604e8b95fe54d98d5491826e [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"
Mike Stump11289f42009-09-09 15:08:12 +000025#include "clang/AST/DeclObjC.h"
Ted Kremenek819e9b62008-03-11 06:39:11 +000026#include "llvm/ADT/DenseMap.h"
27#include "llvm/ADT/FoldingSet.h"
28#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek0747e7e2008-10-21 15:53:15 +000029#include "llvm/ADT/ImmutableList.h"
Ted Kremenekb6cbf282008-05-07 18:36:45 +000030#include "llvm/ADT/StringExtras.h"
Ted Kremenekce8e8812008-04-09 01:10:13 +000031#include "llvm/Support/Compiler.h"
Ted Kremenekc812b232008-05-16 18:33:44 +000032#include "llvm/ADT/STLExtras.h"
Ted Kremenek9551ab62008-08-12 20:41:56 +000033#include <stdarg.h>
Ted Kremenekea6507f2008-03-06 00:08:09 +000034
35using namespace clang;
Ted Kremenek01acb622008-10-24 21:18:08 +000036
37//===----------------------------------------------------------------------===//
38// Utility functions.
39//===----------------------------------------------------------------------===//
40
Ted Kremenek01acb622008-10-24 21:18:08 +000041// The "fundamental rule" for naming conventions of methods:
42// (url broken into two lines)
43// http://developer.apple.com/documentation/Cocoa/Conceptual/
44// MemoryMgmt/Tasks/MemoryManagementRules.html
45//
46// "You take ownership of an object if you create it using a method whose name
Mike Stump11289f42009-09-09 15:08:12 +000047// begins with "alloc" or "new" or contains "copy" (for example, alloc,
Ted Kremenek01acb622008-10-24 21:18:08 +000048// newObject, or mutableCopy), or if you send it a retain message. You are
49// responsible for relinquishing ownership of objects you own using release
50// or autorelease. Any other time you receive an object, you must
51// not release it."
52//
Ted Kremenek8a73c712009-02-21 05:13:43 +000053
54using llvm::CStrInCStrNoCase;
Ted Kremenek97ad7b62009-02-21 18:26:02 +000055using llvm::StringsEqualNoCase;
Ted Kremenek8a73c712009-02-21 05:13:43 +000056
57enum NamingConvention { NoConvention, CreateRule, InitRule };
58
59static inline bool isWordEnd(char ch, char prev, char next) {
60 return ch == '\0'
61 || (islower(prev) && isupper(ch)) // xxxC
62 || (isupper(prev) && isupper(ch) && islower(next)) // XXCreate
63 || !isalpha(ch);
64}
Mike Stump11289f42009-09-09 15:08:12 +000065
66static inline const char* parseWord(const char* s) {
Ted Kremenek8a73c712009-02-21 05:13:43 +000067 char ch = *s, prev = '\0';
68 assert(ch != '\0');
69 char next = *(s+1);
70 while (!isWordEnd(ch, prev, next)) {
71 prev = ch;
72 ch = next;
73 next = *((++s)+1);
74 }
75 return s;
76}
77
Ted Kremenek32819772009-05-15 15:49:00 +000078static NamingConvention deriveNamingConvention(Selector S) {
79 IdentifierInfo *II = S.getIdentifierInfoForSlot(0);
Mike Stump11289f42009-09-09 15:08:12 +000080
Ted Kremenek32819772009-05-15 15:49:00 +000081 if (!II)
82 return NoConvention;
Mike Stump11289f42009-09-09 15:08:12 +000083
Daniel Dunbar9d9aa162009-10-17 18:12:45 +000084 const char *s = II->getNameStart();
Mike Stump11289f42009-09-09 15:08:12 +000085
Ted Kremenek8a73c712009-02-21 05:13:43 +000086 // A method/function name may contain a prefix. We don't know it is there,
87 // however, until we encounter the first '_'.
88 bool InPossiblePrefix = true;
89 bool AtBeginning = true;
90 NamingConvention C = NoConvention;
Mike Stump11289f42009-09-09 15:08:12 +000091
Ted Kremenek8a73c712009-02-21 05:13:43 +000092 while (*s != '\0') {
93 // Skip '_'.
94 if (*s == '_') {
95 if (InPossiblePrefix) {
96 InPossiblePrefix = false;
97 AtBeginning = true;
98 // Discard whatever 'convention' we
99 // had already derived since it occurs
100 // in the prefix.
101 C = NoConvention;
102 }
103 ++s;
104 continue;
105 }
Mike Stump11289f42009-09-09 15:08:12 +0000106
Ted Kremenek8a73c712009-02-21 05:13:43 +0000107 // Skip numbers, ':', etc.
108 if (!isalpha(*s)) {
109 ++s;
110 continue;
111 }
Mike Stump11289f42009-09-09 15:08:12 +0000112
Ted Kremenek8a73c712009-02-21 05:13:43 +0000113 const char *wordEnd = parseWord(s);
114 assert(wordEnd > s);
115 unsigned len = wordEnd - s;
Mike Stump11289f42009-09-09 15:08:12 +0000116
Ted Kremenek8a73c712009-02-21 05:13:43 +0000117 switch (len) {
118 default:
119 break;
120 case 3:
121 // Methods starting with 'new' follow the create rule.
Ted Kremenek97ad7b62009-02-21 18:26:02 +0000122 if (AtBeginning && StringsEqualNoCase("new", s, len))
Mike Stump11289f42009-09-09 15:08:12 +0000123 C = CreateRule;
Ted Kremenek8a73c712009-02-21 05:13:43 +0000124 break;
125 case 4:
126 // Methods starting with 'alloc' or contain 'copy' follow the
127 // create rule
Ted Kremenek340fd2d2009-03-13 20:27:06 +0000128 if (C == NoConvention && StringsEqualNoCase("copy", s, len))
Ted Kremenek8a73c712009-02-21 05:13:43 +0000129 C = CreateRule;
130 else // Methods starting with 'init' follow the init rule.
Ted Kremenek97ad7b62009-02-21 18:26:02 +0000131 if (AtBeginning && StringsEqualNoCase("init", s, len))
Ted Kremenek340fd2d2009-03-13 20:27:06 +0000132 C = InitRule;
133 break;
134 case 5:
135 if (AtBeginning && StringsEqualNoCase("alloc", s, len))
136 C = CreateRule;
Ted Kremenek8a73c712009-02-21 05:13:43 +0000137 break;
138 }
Mike Stump11289f42009-09-09 15:08:12 +0000139
Ted Kremenek8a73c712009-02-21 05:13:43 +0000140 // If we aren't in the prefix and have a derived convention then just
141 // return it now.
142 if (!InPossiblePrefix && C != NoConvention)
143 return C;
144
145 AtBeginning = false;
146 s = wordEnd;
147 }
148
149 // We will get here if there wasn't more than one word
150 // after the prefix.
151 return C;
152}
153
Ted Kremenek32819772009-05-15 15:49:00 +0000154static bool followsFundamentalRule(Selector S) {
155 return deriveNamingConvention(S) == CreateRule;
Ted Kremenek2855a932008-11-05 16:54:44 +0000156}
157
Ted Kremenek223a7d52009-04-29 23:03:22 +0000158static const ObjCMethodDecl*
Mike Stump11289f42009-09-09 15:08:12 +0000159ResolveToInterfaceMethodDecl(const ObjCMethodDecl *MD) {
Ted Kremenek223a7d52009-04-29 23:03:22 +0000160 ObjCInterfaceDecl *ID =
161 const_cast<ObjCInterfaceDecl*>(MD->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +0000162
Ted Kremenek223a7d52009-04-29 23:03:22 +0000163 return MD->isInstanceMethod()
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000164 ? ID->lookupInstanceMethod(MD->getSelector())
165 : ID->lookupClassMethod(MD->getSelector());
Ted Kremenek2855a932008-11-05 16:54:44 +0000166}
Ted Kremenek01acb622008-10-24 21:18:08 +0000167
Ted Kremenek884a8992009-05-08 23:09:42 +0000168namespace {
169class VISIBILITY_HIDDEN GenericNodeBuilder {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000170 GRStmtNodeBuilder *SNB;
Ted Kremenek884a8992009-05-08 23:09:42 +0000171 Stmt *S;
172 const void *tag;
Zhongxing Xu107f7592009-08-06 12:48:26 +0000173 GREndPathNodeBuilder *ENB;
Ted Kremenek884a8992009-05-08 23:09:42 +0000174public:
Zhongxing Xu107f7592009-08-06 12:48:26 +0000175 GenericNodeBuilder(GRStmtNodeBuilder &snb, Stmt *s,
Ted Kremenek884a8992009-05-08 23:09:42 +0000176 const void *t)
177 : SNB(&snb), S(s), tag(t), ENB(0) {}
Zhongxing Xu107f7592009-08-06 12:48:26 +0000178
179 GenericNodeBuilder(GREndPathNodeBuilder &enb)
Ted Kremenek884a8992009-05-08 23:09:42 +0000180 : SNB(0), S(0), tag(0), ENB(&enb) {}
Mike Stump11289f42009-09-09 15:08:12 +0000181
Zhongxing Xu107f7592009-08-06 12:48:26 +0000182 ExplodedNode *MakeNode(const GRState *state, ExplodedNode *Pred) {
Ted Kremenek884a8992009-05-08 23:09:42 +0000183 if (SNB)
Mike Stump11289f42009-09-09 15:08:12 +0000184 return SNB->generateNode(PostStmt(S, Pred->getLocationContext(), tag),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000185 state, Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenek884a8992009-05-08 23:09:42 +0000187 assert(ENB);
Ted Kremenek9ec08aa2009-05-09 00:44:07 +0000188 return ENB->generateNode(state, Pred);
Ted Kremenek884a8992009-05-08 23:09:42 +0000189 }
190};
191} // end anonymous namespace
192
Ted Kremenekc8bef6a2008-04-09 23:49:11 +0000193//===----------------------------------------------------------------------===//
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000194// Selector creation functions.
Ted Kremeneka506fec2008-04-17 18:12:53 +0000195//===----------------------------------------------------------------------===//
196
Ted Kremenekf0b0f2e2008-05-01 18:31:44 +0000197static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
Ted Kremeneka506fec2008-04-17 18:12:53 +0000198 IdentifierInfo* II = &Ctx.Idents.get(name);
199 return Ctx.Selectors.getSelector(0, &II);
200}
201
Ted Kremenek0806f912008-05-06 00:30:21 +0000202static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
203 IdentifierInfo* II = &Ctx.Idents.get(name);
204 return Ctx.Selectors.getSelector(1, &II);
205}
206
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000207//===----------------------------------------------------------------------===//
208// Type querying functions.
209//===----------------------------------------------------------------------===//
210
Ted Kremenek7e904222009-01-12 21:45:02 +0000211static bool isRefType(QualType RetTy, const char* prefix,
212 ASTContext* Ctx = 0, const char* name = 0) {
Mike Stump11289f42009-09-09 15:08:12 +0000213
Ted Kremenek95d18192009-05-12 04:53:03 +0000214 // Recursively walk the typedef stack, allowing typedefs of reference types.
215 while (1) {
216 if (TypedefType* TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
Daniel Dunbar9d9aa162009-10-17 18:12:45 +0000217 llvm::StringRef TDName = TD->getDecl()->getIdentifier()->getNameStr();
218 if (TDName.startswith(prefix) && TDName.endswith("Ref"))
Ted Kremenek95d18192009-05-12 04:53:03 +0000219 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000220
Ted Kremenek95d18192009-05-12 04:53:03 +0000221 RetTy = TD->getDecl()->getUnderlyingType();
222 continue;
223 }
224 break;
Ted Kremenek7e904222009-01-12 21:45:02 +0000225 }
226
227 if (!Ctx || !name)
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000228 return false;
Ted Kremenek7e904222009-01-12 21:45:02 +0000229
230 // Is the type void*?
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000231 const PointerType* PT = RetTy->getAs<PointerType>();
Ted Kremenek7e904222009-01-12 21:45:02 +0000232 if (!(PT->getPointeeType().getUnqualifiedType() == Ctx->VoidTy))
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000233 return false;
Ted Kremenek7e904222009-01-12 21:45:02 +0000234
235 // Does the name start with the prefix?
Daniel Dunbar9d9aa162009-10-17 18:12:45 +0000236 return llvm::StringRef(name).startswith(prefix);
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000237}
238
Ted Kremeneka506fec2008-04-17 18:12:53 +0000239//===----------------------------------------------------------------------===//
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000240// Primitives used for constructing summaries for function/method calls.
Ted Kremenekc8bef6a2008-04-09 23:49:11 +0000241//===----------------------------------------------------------------------===//
242
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000243/// ArgEffect is used to summarize a function/method call's effect on a
244/// particular argument.
Ted Kremenekea072e32009-03-17 19:42:23 +0000245enum ArgEffect { Autorelease, Dealloc, DecRef, DecRefMsg, DoNothing,
246 DoNothingByRef, IncRefMsg, IncRef, MakeCollectable, MayEscape,
247 NewAutoreleasePool, SelfOwn, StopTracking };
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000248
Ted Kremenek819e9b62008-03-11 06:39:11 +0000249namespace llvm {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000250template <> struct FoldingSetTrait<ArgEffect> {
251static inline void Profile(const ArgEffect X, FoldingSetNodeID& ID) {
252 ID.AddInteger((unsigned) X);
253}
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000254};
Ted Kremenek819e9b62008-03-11 06:39:11 +0000255} // end llvm namespace
256
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000257/// ArgEffects summarizes the effects of a function/method call on all of
258/// its arguments.
259typedef llvm::ImmutableMap<unsigned,ArgEffect> ArgEffects;
260
Ted Kremenek819e9b62008-03-11 06:39:11 +0000261namespace {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000262
263/// RetEffect is used to summarize a function/method call's behavior with
Mike Stump11289f42009-09-09 15:08:12 +0000264/// respect to its return value.
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000265class VISIBILITY_HIDDEN RetEffect {
Ted Kremenek819e9b62008-03-11 06:39:11 +0000266public:
Ted Kremenekab4a8b52008-06-23 18:02:52 +0000267 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
Ted Kremenek1272f702009-05-12 20:06:54 +0000268 NotOwnedSymbol, GCNotOwnedSymbol, ReceiverAlias,
269 OwnedWhenTrackedReceiver };
Mike Stump11289f42009-09-09 15:08:12 +0000270
271 enum ObjKind { CF, ObjC, AnyObj };
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000272
Ted Kremenek819e9b62008-03-11 06:39:11 +0000273private:
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000274 Kind K;
275 ObjKind O;
276 unsigned index;
277
278 RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {}
279 RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {}
Mike Stump11289f42009-09-09 15:08:12 +0000280
Ted Kremenek819e9b62008-03-11 06:39:11 +0000281public:
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000282 Kind getKind() const { return K; }
283
284 ObjKind getObjKind() const { return O; }
Mike Stump11289f42009-09-09 15:08:12 +0000285
286 unsigned getIndex() const {
Ted Kremenek819e9b62008-03-11 06:39:11 +0000287 assert(getKind() == Alias);
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000288 return index;
Ted Kremenek819e9b62008-03-11 06:39:11 +0000289 }
Mike Stump11289f42009-09-09 15:08:12 +0000290
Ted Kremenek223a7d52009-04-29 23:03:22 +0000291 bool isOwned() const {
Ted Kremenek1272f702009-05-12 20:06:54 +0000292 return K == OwnedSymbol || K == OwnedAllocatedSymbol ||
293 K == OwnedWhenTrackedReceiver;
Ted Kremenek223a7d52009-04-29 23:03:22 +0000294 }
Mike Stump11289f42009-09-09 15:08:12 +0000295
Ted Kremenek1272f702009-05-12 20:06:54 +0000296 static RetEffect MakeOwnedWhenTrackedReceiver() {
297 return RetEffect(OwnedWhenTrackedReceiver, ObjC);
298 }
Mike Stump11289f42009-09-09 15:08:12 +0000299
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000300 static RetEffect MakeAlias(unsigned Idx) {
301 return RetEffect(Alias, Idx);
302 }
303 static RetEffect MakeReceiverAlias() {
304 return RetEffect(ReceiverAlias);
Mike Stump11289f42009-09-09 15:08:12 +0000305 }
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000306 static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) {
307 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o);
Mike Stump11289f42009-09-09 15:08:12 +0000308 }
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000309 static RetEffect MakeNotOwned(ObjKind o) {
310 return RetEffect(NotOwnedSymbol, o);
Ted Kremeneke6633562009-04-27 19:14:45 +0000311 }
312 static RetEffect MakeGCNotOwned() {
313 return RetEffect(GCNotOwnedSymbol, ObjC);
314 }
Mike Stump11289f42009-09-09 15:08:12 +0000315
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000316 static RetEffect MakeNoRet() {
317 return RetEffect(NoRet);
Ted Kremenekab4a8b52008-06-23 18:02:52 +0000318 }
Mike Stump11289f42009-09-09 15:08:12 +0000319
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000320 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000321 ID.AddInteger((unsigned)K);
322 ID.AddInteger((unsigned)O);
323 ID.AddInteger(index);
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000324 }
Ted Kremenek819e9b62008-03-11 06:39:11 +0000325};
Mike Stump11289f42009-09-09 15:08:12 +0000326
327
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000328class VISIBILITY_HIDDEN RetainSummary {
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000329 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
330 /// specifies the argument (starting from 0). This can be sparsely
331 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000332 ArgEffects Args;
Mike Stump11289f42009-09-09 15:08:12 +0000333
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000334 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
335 /// do not have an entry in Args.
336 ArgEffect DefaultArgEffect;
Mike Stump11289f42009-09-09 15:08:12 +0000337
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000338 /// Receiver - If this summary applies to an Objective-C message expression,
339 /// this is the effect applied to the state of the receiver.
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000340 ArgEffect Receiver;
Mike Stump11289f42009-09-09 15:08:12 +0000341
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000342 /// Ret - The effect on the return value. Used to indicate if the
343 /// function/method call returns a new tracked symbol, returns an
344 /// alias of one of the arguments in the call, and so on.
Ted Kremenek819e9b62008-03-11 06:39:11 +0000345 RetEffect Ret;
Mike Stump11289f42009-09-09 15:08:12 +0000346
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000347 /// EndPath - Indicates that execution of this method/function should
348 /// terminate the simulation of a path.
349 bool EndPath;
Mike Stump11289f42009-09-09 15:08:12 +0000350
Ted Kremenek819e9b62008-03-11 06:39:11 +0000351public:
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000352 RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff,
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000353 ArgEffect ReceiverEff, bool endpath = false)
354 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
Mike Stump11289f42009-09-09 15:08:12 +0000355 EndPath(endpath) {}
356
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000357 /// getArg - Return the argument effect on the argument specified by
358 /// idx (starting from 0).
Ted Kremenekbf9d8042008-03-11 17:48:22 +0000359 ArgEffect getArg(unsigned idx) const {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000360 if (const ArgEffect *AE = Args.lookup(idx))
361 return *AE;
Mike Stump11289f42009-09-09 15:08:12 +0000362
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000363 return DefaultArgEffect;
Ted Kremenekbf9d8042008-03-11 17:48:22 +0000364 }
Mike Stump11289f42009-09-09 15:08:12 +0000365
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000366 /// setDefaultArgEffect - Set the default argument effect.
367 void setDefaultArgEffect(ArgEffect E) {
368 DefaultArgEffect = E;
369 }
Mike Stump11289f42009-09-09 15:08:12 +0000370
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000371 /// setArg - Set the argument effect on the argument specified by idx.
372 void setArgEffect(ArgEffects::Factory& AF, unsigned idx, ArgEffect E) {
373 Args = AF.Add(Args, idx, E);
374 }
Mike Stump11289f42009-09-09 15:08:12 +0000375
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000376 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000377 RetEffect getRetEffect() const { return Ret; }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000379 /// setRetEffect - Set the effect of the return value of the call.
380 void setRetEffect(RetEffect E) { Ret = E; }
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000382 /// isEndPath - Returns true if executing the given method/function should
383 /// terminate the path.
384 bool isEndPath() const { return EndPath; }
Mike Stump11289f42009-09-09 15:08:12 +0000385
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000386 /// getReceiverEffect - Returns the effect on the receiver of the call.
387 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000388 ArgEffect getReceiverEffect() const { return Receiver; }
Mike Stump11289f42009-09-09 15:08:12 +0000389
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000390 /// setReceiverEffect - Set the effect on the receiver of the call.
391 void setReceiverEffect(ArgEffect E) { Receiver = E; }
Mike Stump11289f42009-09-09 15:08:12 +0000392
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000393 typedef ArgEffects::iterator ExprIterator;
Mike Stump11289f42009-09-09 15:08:12 +0000394
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000395 ExprIterator begin_args() const { return Args.begin(); }
396 ExprIterator end_args() const { return Args.end(); }
Mike Stump11289f42009-09-09 15:08:12 +0000397
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000398 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects A,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000399 RetEffect RetEff, ArgEffect DefaultEff,
Ted Kremenekf7faa422008-07-18 17:39:56 +0000400 ArgEffect ReceiverEff, bool EndPath) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000401 ID.Add(A);
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000402 ID.Add(RetEff);
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000403 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000404 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenekf7faa422008-07-18 17:39:56 +0000405 ID.AddInteger((unsigned) EndPath);
Ted Kremenek819e9b62008-03-11 06:39:11 +0000406 }
Mike Stump11289f42009-09-09 15:08:12 +0000407
Ted Kremenek819e9b62008-03-11 06:39:11 +0000408 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf7faa422008-07-18 17:39:56 +0000409 Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
Ted Kremenek819e9b62008-03-11 06:39:11 +0000410 }
411};
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000412} // end anonymous namespace
Ted Kremenek819e9b62008-03-11 06:39:11 +0000413
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000414//===----------------------------------------------------------------------===//
415// Data structures for constructing summaries.
416//===----------------------------------------------------------------------===//
Ted Kremenekb1d13292008-06-24 03:49:48 +0000417
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000418namespace {
419class VISIBILITY_HIDDEN ObjCSummaryKey {
420 IdentifierInfo* II;
421 Selector S;
Mike Stump11289f42009-09-09 15:08:12 +0000422public:
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000423 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
424 : II(ii), S(s) {}
425
Ted Kremenek223a7d52009-04-29 23:03:22 +0000426 ObjCSummaryKey(const ObjCInterfaceDecl* d, Selector s)
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000427 : II(d ? d->getIdentifier() : 0), S(s) {}
Ted Kremenek5801f652009-05-13 18:16:01 +0000428
429 ObjCSummaryKey(const ObjCInterfaceDecl* d, IdentifierInfo *ii, Selector s)
430 : II(d ? d->getIdentifier() : ii), S(s) {}
Mike Stump11289f42009-09-09 15:08:12 +0000431
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000432 ObjCSummaryKey(Selector s)
433 : II(0), S(s) {}
Mike Stump11289f42009-09-09 15:08:12 +0000434
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000435 IdentifierInfo* getIdentifier() const { return II; }
436 Selector getSelector() const { return S; }
437};
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000438}
439
440namespace llvm {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000441template <> struct DenseMapInfo<ObjCSummaryKey> {
442 static inline ObjCSummaryKey getEmptyKey() {
443 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
444 DenseMapInfo<Selector>::getEmptyKey());
445 }
Mike Stump11289f42009-09-09 15:08:12 +0000446
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000447 static inline ObjCSummaryKey getTombstoneKey() {
448 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
Mike Stump11289f42009-09-09 15:08:12 +0000449 DenseMapInfo<Selector>::getTombstoneKey());
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000452 static unsigned getHashValue(const ObjCSummaryKey &V) {
453 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
Mike Stump11289f42009-09-09 15:08:12 +0000454 & 0x88888888)
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000455 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
456 & 0x55555555);
457 }
Mike Stump11289f42009-09-09 15:08:12 +0000458
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000459 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
460 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
461 RHS.getIdentifier()) &&
462 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
463 RHS.getSelector());
464 }
Mike Stump11289f42009-09-09 15:08:12 +0000465
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000466 static bool isPod() {
467 return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
468 DenseMapInfo<Selector>::isPod();
469 }
470};
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000471} // end llvm namespace
Mike Stump11289f42009-09-09 15:08:12 +0000472
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000473namespace {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000474class VISIBILITY_HIDDEN ObjCSummaryCache {
475 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
476 MapTy M;
477public:
478 ObjCSummaryCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000479
Ted Kremenek8be51382009-07-21 23:27:57 +0000480 RetainSummary* find(const ObjCInterfaceDecl* D, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +0000481 Selector S) {
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000482 // Lookup the method using the decl for the class @interface. If we
483 // have no decl, lookup using the class name.
484 return D ? find(D, S) : find(ClsName, S);
485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
487 RetainSummary* find(const ObjCInterfaceDecl* D, Selector S) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000488 // Do a lookup with the (D,S) pair. If we find a match return
489 // the iterator.
490 ObjCSummaryKey K(D, S);
491 MapTy::iterator I = M.find(K);
Mike Stump11289f42009-09-09 15:08:12 +0000492
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000493 if (I != M.end() || !D)
Ted Kremenek8be51382009-07-21 23:27:57 +0000494 return I->second;
Mike Stump11289f42009-09-09 15:08:12 +0000495
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000496 // Walk the super chain. If we find a hit with a parent, we'll end
497 // up returning that summary. We actually allow that key (null,S), as
498 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
499 // generate initial summaries without having to worry about NSObject
500 // being declared.
501 // FIXME: We may change this at some point.
502 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
503 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
504 break;
Mike Stump11289f42009-09-09 15:08:12 +0000505
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000506 if (!C)
Ted Kremenek8be51382009-07-21 23:27:57 +0000507 return NULL;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000508 }
Mike Stump11289f42009-09-09 15:08:12 +0000509
510 // Cache the summary with original key to make the next lookup faster
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000511 // and return the iterator.
Ted Kremenek8be51382009-07-21 23:27:57 +0000512 RetainSummary *Summ = I->second;
513 M[K] = Summ;
514 return Summ;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000515 }
Mike Stump11289f42009-09-09 15:08:12 +0000516
Ted Kremenek9551ab62008-08-12 20:41:56 +0000517
Ted Kremenek8be51382009-07-21 23:27:57 +0000518 RetainSummary* find(Expr* Receiver, Selector S) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000519 return find(getReceiverDecl(Receiver), S);
520 }
Mike Stump11289f42009-09-09 15:08:12 +0000521
Ted Kremenek8be51382009-07-21 23:27:57 +0000522 RetainSummary* find(IdentifierInfo* II, Selector S) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000523 // FIXME: Class method lookup. Right now we dont' have a good way
524 // of going between IdentifierInfo* and the class hierarchy.
Ted Kremenek8be51382009-07-21 23:27:57 +0000525 MapTy::iterator I = M.find(ObjCSummaryKey(II, S));
Mike Stump11289f42009-09-09 15:08:12 +0000526
Ted Kremenek8be51382009-07-21 23:27:57 +0000527 if (I == M.end())
528 I = M.find(ObjCSummaryKey(S));
Mike Stump11289f42009-09-09 15:08:12 +0000529
Ted Kremenek8be51382009-07-21 23:27:57 +0000530 return I == M.end() ? NULL : I->second;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000531 }
Mike Stump11289f42009-09-09 15:08:12 +0000532
533 const ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
Steve Naroff7cae42b2009-07-10 23:34:53 +0000534 if (const ObjCObjectPointerType* PT =
John McCall9dd450b2009-09-21 23:43:11 +0000535 E->getType()->getAs<ObjCObjectPointerType>())
Steve Naroff7cae42b2009-07-10 23:34:53 +0000536 return PT->getInterfaceDecl();
537
538 return NULL;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000539 }
Mike Stump11289f42009-09-09 15:08:12 +0000540
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000541 RetainSummary*& operator[](ObjCMessageExpr* ME) {
Mike Stump11289f42009-09-09 15:08:12 +0000542
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000543 Selector S = ME->getSelector();
Mike Stump11289f42009-09-09 15:08:12 +0000544
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000545 if (Expr* Receiver = ME->getReceiver()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +0000546 const ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000547 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000550 return M[ObjCSummaryKey(ME->getClassName(), S)];
551 }
Mike Stump11289f42009-09-09 15:08:12 +0000552
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000553 RetainSummary*& operator[](ObjCSummaryKey K) {
554 return M[K];
555 }
Mike Stump11289f42009-09-09 15:08:12 +0000556
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000557 RetainSummary*& operator[](Selector S) {
558 return M[ ObjCSummaryKey(S) ];
559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560};
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000561} // end anonymous namespace
562
563//===----------------------------------------------------------------------===//
564// Data structures for managing collections of summaries.
565//===----------------------------------------------------------------------===//
566
567namespace {
568class VISIBILITY_HIDDEN RetainSummaryManager {
Ted Kremenek00daccd2008-05-05 22:11:16 +0000569
570 //==-----------------------------------------------------------------==//
571 // Typedefs.
572 //==-----------------------------------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +0000573
Ted Kremenek00daccd2008-05-05 22:11:16 +0000574 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
575 FuncSummariesTy;
Mike Stump11289f42009-09-09 15:08:12 +0000576
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000577 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Mike Stump11289f42009-09-09 15:08:12 +0000578
Ted Kremenek00daccd2008-05-05 22:11:16 +0000579 //==-----------------------------------------------------------------==//
580 // Data.
581 //==-----------------------------------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +0000582
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000583 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek4b7ca772008-04-29 05:33:51 +0000584 ASTContext& Ctx;
Ted Kremenekab54e512008-07-01 17:21:27 +0000585
Ted Kremenekae529272008-07-09 18:11:16 +0000586 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
587 /// "CFDictionaryCreate".
588 IdentifierInfo* CFDictionaryCreateII;
Mike Stump11289f42009-09-09 15:08:12 +0000589
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000590 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek4b7ca772008-04-29 05:33:51 +0000591 const bool GCEnabled;
Mike Stump11289f42009-09-09 15:08:12 +0000592
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000593 /// FuncSummaries - A map from FunctionDecls to summaries.
Mike Stump11289f42009-09-09 15:08:12 +0000594 FuncSummariesTy FuncSummaries;
595
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000596 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
597 /// to summaries.
Ted Kremenekea736c52008-06-23 22:21:20 +0000598 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenek00daccd2008-05-05 22:11:16 +0000599
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000600 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenekea736c52008-06-23 22:21:20 +0000601 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenek00daccd2008-05-05 22:11:16 +0000602
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000603 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
604 /// and all other data used by the checker.
Ted Kremenek00daccd2008-05-05 22:11:16 +0000605 llvm::BumpPtrAllocator BPAlloc;
Mike Stump11289f42009-09-09 15:08:12 +0000606
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000607 /// AF - A factory for ArgEffects objects.
Mike Stump11289f42009-09-09 15:08:12 +0000608 ArgEffects::Factory AF;
609
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000610 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenek00daccd2008-05-05 22:11:16 +0000611 ArgEffects ScratchArgs;
Mike Stump11289f42009-09-09 15:08:12 +0000612
Ted Kremenek9157fbb2009-05-07 23:40:42 +0000613 /// ObjCAllocRetE - Default return effect for methods returning Objective-C
614 /// objects.
615 RetEffect ObjCAllocRetE;
Ted Kremeneka03705c2009-06-05 23:18:01 +0000616
Mike Stump11289f42009-09-09 15:08:12 +0000617 /// ObjCInitRetE - Default return effect for init methods returning
Ted Kremenek815fbb62009-08-20 05:13:36 +0000618 /// Objective-C objects.
Ted Kremeneka03705c2009-06-05 23:18:01 +0000619 RetEffect ObjCInitRetE;
Mike Stump11289f42009-09-09 15:08:12 +0000620
Ted Kremenekff606a12009-05-04 04:57:00 +0000621 RetainSummary DefaultSummary;
Ted Kremenek10427bd2008-05-06 18:11:36 +0000622 RetainSummary* StopSummary;
Mike Stump11289f42009-09-09 15:08:12 +0000623
Ted Kremenek00daccd2008-05-05 22:11:16 +0000624 //==-----------------------------------------------------------------==//
625 // Methods.
626 //==-----------------------------------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +0000627
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000628 /// getArgEffects - Returns a persistent ArgEffects object based on the
629 /// data in ScratchArgs.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000630 ArgEffects getArgEffects();
Ted Kremenek819e9b62008-03-11 06:39:11 +0000631
Mike Stump11289f42009-09-09 15:08:12 +0000632 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
633
Ted Kremenekcc3d1882008-10-23 01:56:15 +0000634public:
Ted Kremenek1272f702009-05-12 20:06:54 +0000635 RetEffect getObjAllocRetEffect() const { return ObjCAllocRetE; }
636
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000637 RetainSummary *getDefaultSummary() {
638 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
639 return new (Summ) RetainSummary(DefaultSummary);
640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
Ted Kremenek82157a12009-02-23 16:51:39 +0000642 RetainSummary* getUnarySummary(const FunctionType* FT, UnaryFuncKind func);
Mike Stump11289f42009-09-09 15:08:12 +0000643
Ted Kremenek00daccd2008-05-05 22:11:16 +0000644 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
Mike Stump11289f42009-09-09 15:08:12 +0000645 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek7e904222009-01-12 21:45:02 +0000646 RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
Mike Stump11289f42009-09-09 15:08:12 +0000647
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000648 RetainSummary* getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000649 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000650 ArgEffect DefaultEff = MayEscape,
651 bool isEndPath = false);
Ted Kremenek3700b762008-10-29 04:07:07 +0000652
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000653 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000654 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek1df2f3a2008-05-22 17:31:13 +0000655 ArgEffect DefaultEff = MayEscape) {
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000656 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek0806f912008-05-06 00:30:21 +0000657 }
Mike Stump11289f42009-09-09 15:08:12 +0000658
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000659 RetainSummary *getPersistentStopSummary() {
Ted Kremenek10427bd2008-05-06 18:11:36 +0000660 if (StopSummary)
661 return StopSummary;
Mike Stump11289f42009-09-09 15:08:12 +0000662
Ted Kremenek10427bd2008-05-06 18:11:36 +0000663 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
664 StopTracking, StopTracking);
Ted Kremenek3700b762008-10-29 04:07:07 +0000665
Ted Kremenek10427bd2008-05-06 18:11:36 +0000666 return StopSummary;
Mike Stump11289f42009-09-09 15:08:12 +0000667 }
Ted Kremenek015c3562008-05-06 04:20:12 +0000668
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000669 RetainSummary *getInitMethodSummary(QualType RetTy);
Ted Kremenek3d1e9722008-05-05 23:55:01 +0000670
Ted Kremenekea736c52008-06-23 22:21:20 +0000671 void InitializeClassMethodSummaries();
672 void InitializeMethodSummaries();
Mike Stump11289f42009-09-09 15:08:12 +0000673
Ted Kremenekb4cf4a52009-05-03 04:42:10 +0000674 bool isTrackedObjCObjectType(QualType T);
Ted Kremenek4b59ccb2009-05-03 06:08:32 +0000675 bool isTrackedCFObjectType(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000676
Ted Kremenekcc3d1882008-10-23 01:56:15 +0000677private:
Mike Stump11289f42009-09-09 15:08:12 +0000678
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000679 void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
680 RetainSummary* Summ) {
681 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
682 }
Mike Stump11289f42009-09-09 15:08:12 +0000683
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000684 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
685 ObjCClassMethodSummaries[S] = Summ;
686 }
Mike Stump11289f42009-09-09 15:08:12 +0000687
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000688 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
689 ObjCMethodSummaries[S] = Summ;
690 }
Ted Kremenek00dfe302009-03-04 23:30:42 +0000691
692 void addClassMethSummary(const char* Cls, const char* nullaryName,
693 RetainSummary *Summ) {
694 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
695 Selector S = GetNullarySelector(nullaryName, Ctx);
696 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
697 }
Mike Stump11289f42009-09-09 15:08:12 +0000698
Ted Kremenekdce78462009-02-25 02:54:57 +0000699 void addInstMethSummary(const char* Cls, const char* nullaryName,
700 RetainSummary *Summ) {
701 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
702 Selector S = GetNullarySelector(nullaryName, Ctx);
703 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
704 }
Mike Stump11289f42009-09-09 15:08:12 +0000705
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000706 Selector generateSelector(va_list argp) {
Ted Kremenek050b91c2008-08-12 18:30:56 +0000707 llvm::SmallVector<IdentifierInfo*, 10> II;
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000708
Ted Kremenek050b91c2008-08-12 18:30:56 +0000709 while (const char* s = va_arg(argp, const char*))
710 II.push_back(&Ctx.Idents.get(s));
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000711
Mike Stump11289f42009-09-09 15:08:12 +0000712 return Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000713 }
Mike Stump11289f42009-09-09 15:08:12 +0000714
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000715 void addMethodSummary(IdentifierInfo *ClsII, ObjCMethodSummariesTy& Summaries,
716 RetainSummary* Summ, va_list argp) {
717 Selector S = generateSelector(argp);
718 Summaries[ObjCSummaryKey(ClsII, S)] = Summ;
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000719 }
Mike Stump11289f42009-09-09 15:08:12 +0000720
Ted Kremenek3f13f592008-08-12 18:48:50 +0000721 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
722 va_list argp;
723 va_start(argp, Summ);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000724 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
Mike Stump11289f42009-09-09 15:08:12 +0000725 va_end(argp);
Ted Kremenek3f13f592008-08-12 18:48:50 +0000726 }
Mike Stump11289f42009-09-09 15:08:12 +0000727
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000728 void addClsMethSummary(const char* Cls, RetainSummary* Summ, ...) {
729 va_list argp;
730 va_start(argp, Summ);
731 addMethodSummary(&Ctx.Idents.get(Cls),ObjCClassMethodSummaries, Summ, argp);
732 va_end(argp);
733 }
Mike Stump11289f42009-09-09 15:08:12 +0000734
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000735 void addClsMethSummary(IdentifierInfo *II, RetainSummary* Summ, ...) {
736 va_list argp;
737 va_start(argp, Summ);
738 addMethodSummary(II, ObjCClassMethodSummaries, Summ, argp);
739 va_end(argp);
740 }
741
Ted Kremenek050b91c2008-08-12 18:30:56 +0000742 void addPanicSummary(const char* Cls, ...) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000743 RetainSummary* Summ = getPersistentSummary(AF.GetEmptyMap(),
744 RetEffect::MakeNoRet(),
Ted Kremenek050b91c2008-08-12 18:30:56 +0000745 DoNothing, DoNothing, true);
746 va_list argp;
747 va_start (argp, Cls);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000748 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
Ted Kremenek050b91c2008-08-12 18:30:56 +0000749 va_end(argp);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000750 }
Mike Stump11289f42009-09-09 15:08:12 +0000751
Ted Kremenek819e9b62008-03-11 06:39:11 +0000752public:
Mike Stump11289f42009-09-09 15:08:12 +0000753
Ted Kremenek00daccd2008-05-05 22:11:16 +0000754 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenekab54e512008-07-01 17:21:27 +0000755 : Ctx(ctx),
Ted Kremenekae529272008-07-09 18:11:16 +0000756 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000757 GCEnabled(gcenabled), AF(BPAlloc), ScratchArgs(AF.GetEmptyMap()),
Ted Kremenek9157fbb2009-05-07 23:40:42 +0000758 ObjCAllocRetE(gcenabled ? RetEffect::MakeGCNotOwned()
759 : RetEffect::MakeOwned(RetEffect::ObjC, true)),
Ted Kremenekea675cf2009-06-11 18:17:24 +0000760 ObjCInitRetE(gcenabled ? RetEffect::MakeGCNotOwned()
761 : RetEffect::MakeOwnedWhenTrackedReceiver()),
Ted Kremenekff606a12009-05-04 04:57:00 +0000762 DefaultSummary(AF.GetEmptyMap() /* per-argument effects (none) */,
763 RetEffect::MakeNoRet() /* return effect */,
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000764 MayEscape, /* default argument effect */
765 DoNothing /* receiver effect */),
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000766 StopSummary(0) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000767
768 InitializeClassMethodSummaries();
769 InitializeMethodSummaries();
770 }
Mike Stump11289f42009-09-09 15:08:12 +0000771
Ted Kremenek00daccd2008-05-05 22:11:16 +0000772 ~RetainSummaryManager();
Mike Stump11289f42009-09-09 15:08:12 +0000773
774 RetainSummary* getSummary(FunctionDecl* FD);
775
Ted Kremenek223a7d52009-04-29 23:03:22 +0000776 RetainSummary* getInstanceMethodSummary(ObjCMessageExpr* ME,
777 const ObjCInterfaceDecl* ID) {
Ted Kremenek38724302009-04-29 17:09:14 +0000778 return getInstanceMethodSummary(ME->getSelector(), ME->getClassName(),
Mike Stump11289f42009-09-09 15:08:12 +0000779 ID, ME->getMethodDecl(), ME->getType());
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000780 }
Mike Stump11289f42009-09-09 15:08:12 +0000781
Ted Kremenek38724302009-04-29 17:09:14 +0000782 RetainSummary* getInstanceMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +0000783 const ObjCInterfaceDecl* ID,
784 const ObjCMethodDecl *MD,
785 QualType RetTy);
Ted Kremenek7686ffa2009-04-29 00:42:39 +0000786
787 RetainSummary *getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +0000788 const ObjCInterfaceDecl *ID,
789 const ObjCMethodDecl *MD,
790 QualType RetTy);
Mike Stump11289f42009-09-09 15:08:12 +0000791
Ted Kremenek7686ffa2009-04-29 00:42:39 +0000792 RetainSummary *getClassMethodSummary(ObjCMessageExpr *ME) {
793 return getClassMethodSummary(ME->getSelector(), ME->getClassName(),
794 ME->getClassInfo().first,
795 ME->getMethodDecl(), ME->getType());
796 }
Ted Kremenek99fe1692009-04-29 17:17:48 +0000797
798 /// getMethodSummary - This version of getMethodSummary is used to query
799 /// the summary for the current method being analyzed.
Ted Kremenek223a7d52009-04-29 23:03:22 +0000800 RetainSummary *getMethodSummary(const ObjCMethodDecl *MD) {
801 // FIXME: Eventually this should be unneeded.
Ted Kremenek223a7d52009-04-29 23:03:22 +0000802 const ObjCInterfaceDecl *ID = MD->getClassInterface();
Ted Kremenekb2a143f2009-04-30 05:41:14 +0000803 Selector S = MD->getSelector();
Ted Kremenek99fe1692009-04-29 17:17:48 +0000804 IdentifierInfo *ClsName = ID->getIdentifier();
805 QualType ResultTy = MD->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +0000806
807 // Resolve the method decl last.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000808 if (const ObjCMethodDecl *InterfaceMD = ResolveToInterfaceMethodDecl(MD))
Ted Kremenek497df912009-04-30 05:47:23 +0000809 MD = InterfaceMD;
Mike Stump11289f42009-09-09 15:08:12 +0000810
Ted Kremenek99fe1692009-04-29 17:17:48 +0000811 if (MD->isInstanceMethod())
812 return getInstanceMethodSummary(S, ClsName, ID, MD, ResultTy);
813 else
814 return getClassMethodSummary(S, ClsName, ID, MD, ResultTy);
815 }
Mike Stump11289f42009-09-09 15:08:12 +0000816
Ted Kremenek223a7d52009-04-29 23:03:22 +0000817 RetainSummary* getCommonMethodSummary(const ObjCMethodDecl* MD,
818 Selector S, QualType RetTy);
819
Ted Kremenekc2de7272009-05-09 02:58:13 +0000820 void updateSummaryFromAnnotations(RetainSummary &Summ,
821 const ObjCMethodDecl *MD);
822
823 void updateSummaryFromAnnotations(RetainSummary &Summ,
824 const FunctionDecl *FD);
825
Ted Kremenek00daccd2008-05-05 22:11:16 +0000826 bool isGCEnabled() const { return GCEnabled; }
Mike Stump11289f42009-09-09 15:08:12 +0000827
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000828 RetainSummary *copySummary(RetainSummary *OldSumm) {
829 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
830 new (Summ) RetainSummary(*OldSumm);
831 return Summ;
Mike Stump11289f42009-09-09 15:08:12 +0000832 }
Ted Kremenek819e9b62008-03-11 06:39:11 +0000833};
Mike Stump11289f42009-09-09 15:08:12 +0000834
Ted Kremenek819e9b62008-03-11 06:39:11 +0000835} // end anonymous namespace
836
837//===----------------------------------------------------------------------===//
838// Implementation of checker data structures.
839//===----------------------------------------------------------------------===//
840
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000841RetainSummaryManager::~RetainSummaryManager() {}
Ted Kremenek819e9b62008-03-11 06:39:11 +0000842
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000843ArgEffects RetainSummaryManager::getArgEffects() {
844 ArgEffects AE = ScratchArgs;
845 ScratchArgs = AF.GetEmptyMap();
846 return AE;
Ted Kremenek68d73d12008-03-12 01:21:45 +0000847}
848
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000849RetainSummary*
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000850RetainSummaryManager::getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000851 ArgEffect ReceiverEff,
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000852 ArgEffect DefaultEff,
Mike Stump11289f42009-09-09 15:08:12 +0000853 bool isEndPath) {
Ted Kremenekf7141592008-04-24 17:22:33 +0000854 // Create the summary and return it.
Ted Kremenek1bff64e2009-05-04 04:30:18 +0000855 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000856 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenek68d73d12008-03-12 01:21:45 +0000857 return Summ;
858}
859
Ted Kremenek00daccd2008-05-05 22:11:16 +0000860//===----------------------------------------------------------------------===//
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000861// Predicates.
862//===----------------------------------------------------------------------===//
863
Ted Kremenekb4cf4a52009-05-03 04:42:10 +0000864bool RetainSummaryManager::isTrackedObjCObjectType(QualType Ty) {
Steve Naroff79d12152009-07-16 15:41:00 +0000865 if (!Ty->isObjCObjectPointerType())
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000866 return false;
867
John McCall9dd450b2009-09-21 23:43:11 +0000868 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +0000869
Steve Naroff7cae42b2009-07-10 23:34:53 +0000870 // Can be true for objects with the 'NSObject' attribute.
871 if (!PT)
Ted Kremenek37467812009-04-23 22:11:07 +0000872 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000873
Steve Naroff7cae42b2009-07-10 23:34:53 +0000874 // We assume that id<..>, id, and "Class" all represent tracked objects.
875 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
876 PT->isObjCClassType())
877 return true;
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000878
Mike Stump11289f42009-09-09 15:08:12 +0000879 // Does the interface subclass NSObject?
880 // FIXME: We can memoize here if this gets too expensive.
881 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000882
Ted Kremeneke4302ee2009-05-16 01:38:01 +0000883 // Assume that anything declared with a forward declaration and no
884 // @interface subclasses NSObject.
885 if (ID->isForwardDecl())
886 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000887
Ted Kremeneke4302ee2009-05-16 01:38:01 +0000888 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
889
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000890 for ( ; ID ; ID = ID->getSuperClass())
891 if (ID->getIdentifier() == NSObjectII)
892 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000893
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000894 return false;
895}
896
Ted Kremenek4b59ccb2009-05-03 06:08:32 +0000897bool RetainSummaryManager::isTrackedCFObjectType(QualType T) {
898 return isRefType(T, "CF") || // Core Foundation.
899 isRefType(T, "CG") || // Core Graphics.
900 isRefType(T, "DADisk") || // Disk Arbitration API.
901 isRefType(T, "DADissenter") ||
902 isRefType(T, "DASessionRef");
903}
904
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000905//===----------------------------------------------------------------------===//
Ted Kremenek00daccd2008-05-05 22:11:16 +0000906// Summary creation for functions (largely uses of Core Foundation).
907//===----------------------------------------------------------------------===//
Ted Kremenek68d73d12008-03-12 01:21:45 +0000908
Ted Kremenek7e904222009-01-12 21:45:02 +0000909static bool isRetain(FunctionDecl* FD, const char* FName) {
910 const char* loc = strstr(FName, "Retain");
911 return loc && loc[sizeof("Retain")-1] == '\0';
912}
913
914static bool isRelease(FunctionDecl* FD, const char* FName) {
915 const char* loc = strstr(FName, "Release");
916 return loc && loc[sizeof("Release")-1] == '\0';
917}
918
Ted Kremenekf890bfe2008-06-24 03:56:45 +0000919RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenekf7141592008-04-24 17:22:33 +0000920 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenek00daccd2008-05-05 22:11:16 +0000921 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenek00daccd2008-05-05 22:11:16 +0000922 if (I != FuncSummaries.end())
Ted Kremenekf7141592008-04-24 17:22:33 +0000923 return I->second;
924
Ted Kremenekdf76e6d2009-05-04 15:34:07 +0000925 // No summary? Generate one.
Ted Kremenek7e904222009-01-12 21:45:02 +0000926 RetainSummary *S = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000927
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000928 do {
Ted Kremenek7e904222009-01-12 21:45:02 +0000929 // We generate "stop" summaries for implicitly defined functions.
930 if (FD->isImplicit()) {
931 S = getPersistentStopSummary();
932 break;
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000933 }
Mike Stump11289f42009-09-09 15:08:12 +0000934
John McCall9dd450b2009-09-21 23:43:11 +0000935 // [PR 3337] Use 'getAs<FunctionType>' to strip away any typedefs on the
Ted Kremenek86afde32009-01-16 18:40:33 +0000936 // function's type.
John McCall9dd450b2009-09-21 23:43:11 +0000937 const FunctionType* FT = FD->getType()->getAs<FunctionType>();
Ted Kremenek7e904222009-01-12 21:45:02 +0000938 const char* FName = FD->getIdentifier()->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000939
Ted Kremenek5f968932009-03-05 22:11:14 +0000940 // Strip away preceding '_'. Doing this here will effect all the checks
941 // down below.
942 while (*FName == '_') ++FName;
Mike Stump11289f42009-09-09 15:08:12 +0000943
Ted Kremenek7e904222009-01-12 21:45:02 +0000944 // Inspect the result type.
945 QualType RetTy = FT->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +0000946
Ted Kremenek7e904222009-01-12 21:45:02 +0000947 // FIXME: This should all be refactored into a chain of "summary lookup"
948 // filters.
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +0000949 assert(ScratchArgs.isEmpty());
950
Ted Kremenekea675cf2009-06-11 18:17:24 +0000951 switch (strlen(FName)) {
952 default: break;
Ted Kremenek80816ac2009-10-13 22:55:33 +0000953 case 14:
954 if (!memcmp(FName, "pthread_create", 14)) {
955 // Part of: <rdar://problem/7299394>. This will be addressed
956 // better with IPA.
957 S = getPersistentStopSummary();
958 }
959 break;
Ted Kremenek3092e9c2009-06-15 20:36:07 +0000960
Ted Kremenekea675cf2009-06-11 18:17:24 +0000961 case 17:
962 // Handle: id NSMakeCollectable(CFTypeRef)
963 if (!memcmp(FName, "NSMakeCollectable", 17)) {
Steve Naroff7cae42b2009-07-10 23:34:53 +0000964 S = (RetTy->isObjCIdType())
Ted Kremenekea675cf2009-06-11 18:17:24 +0000965 ? getUnarySummary(FT, cfmakecollectable)
966 : getPersistentStopSummary();
967 }
Ted Kremenek3092e9c2009-06-15 20:36:07 +0000968 else if (!memcmp(FName, "IOBSDNameMatching", 17) ||
969 !memcmp(FName, "IOServiceMatching", 17)) {
970 // Part of <rdar://problem/6961230>. (IOKit)
971 // This should be addressed using a API table.
972 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
973 DoNothing, DoNothing);
974 }
Ted Kremenekea675cf2009-06-11 18:17:24 +0000975 break;
Ted Kremenek3092e9c2009-06-15 20:36:07 +0000976
977 case 21:
978 if (!memcmp(FName, "IOServiceNameMatching", 21)) {
979 // Part of <rdar://problem/6961230>. (IOKit)
980 // This should be addressed using a API table.
981 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
982 DoNothing, DoNothing);
983 }
984 break;
985
986 case 24:
987 if (!memcmp(FName, "IOServiceAddNotification", 24)) {
988 // Part of <rdar://problem/6961230>. (IOKit)
989 // This should be addressed using a API table.
990 ScratchArgs = AF.Add(ScratchArgs, 2, DecRef);
Ted Kremenek55adb822009-10-15 22:25:12 +0000991 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,DoNothing);
Ted Kremenek3092e9c2009-06-15 20:36:07 +0000992 }
993 break;
Mike Stump11289f42009-09-09 15:08:12 +0000994
Ted Kremenek3092e9c2009-06-15 20:36:07 +0000995 case 25:
996 if (!memcmp(FName, "IORegistryEntryIDMatching", 25)) {
997 // Part of <rdar://problem/6961230>. (IOKit)
998 // This should be addressed using a API table.
999 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
1000 DoNothing, DoNothing);
1001 }
1002 break;
Mike Stump11289f42009-09-09 15:08:12 +00001003
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001004 case 26:
1005 if (!memcmp(FName, "IOOpenFirmwarePathMatching", 26)) {
1006 // Part of <rdar://problem/6961230>. (IOKit)
1007 // This should be addressed using a API table.
1008 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
Mike Stump11289f42009-09-09 15:08:12 +00001009 DoNothing, DoNothing);
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001010 }
1011 break;
1012
Ted Kremenekea675cf2009-06-11 18:17:24 +00001013 case 27:
1014 if (!memcmp(FName, "IOServiceGetMatchingService", 27)) {
1015 // Part of <rdar://problem/6961230>.
1016 // This should be addressed using a API table.
Ted Kremenekea675cf2009-06-11 18:17:24 +00001017 ScratchArgs = AF.Add(ScratchArgs, 1, DecRef);
Mike Stump11289f42009-09-09 15:08:12 +00001018 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenekea675cf2009-06-11 18:17:24 +00001019 }
1020 break;
1021
1022 case 28:
1023 if (!memcmp(FName, "IOServiceGetMatchingServices", 28)) {
1024 // FIXES: <rdar://problem/6326900>
1025 // This should be addressed using a API table. This strcmp is also
1026 // a little gross, but there is no need to super optimize here.
Ted Kremenekea675cf2009-06-11 18:17:24 +00001027 ScratchArgs = AF.Add(ScratchArgs, 1, DecRef);
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +00001028 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1029 DoNothing);
1030 }
1031 else if (!memcmp(FName, "CVPixelBufferCreateWithBytes", 28)) {
1032 // FIXES: <rdar://problem/7283567>
1033 // Eventually this can be improved by recognizing that the pixel
1034 // buffer passed to CVPixelBufferCreateWithBytes is released via
1035 // a callback and doing full IPA to make sure this is done correctly.
1036 ScratchArgs = AF.Add(ScratchArgs, 7, StopTracking);
1037 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1038 DoNothing);
Ted Kremenekea675cf2009-06-11 18:17:24 +00001039 }
1040 break;
Mike Stump11289f42009-09-09 15:08:12 +00001041
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001042 case 32:
1043 if (!memcmp(FName, "IOServiceAddMatchingNotification", 32)) {
1044 // Part of <rdar://problem/6961230>.
1045 // This should be addressed using a API table.
1046 ScratchArgs = AF.Add(ScratchArgs, 2, DecRef);
Mike Stump11289f42009-09-09 15:08:12 +00001047 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001048 }
1049 break;
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +00001050
1051 case 34:
1052 if (!memcmp(FName, "CVPixelBufferCreateWithPlanarBytes", 34)) {
1053 // FIXES: <rdar://problem/7283567>
1054 // Eventually this can be improved by recognizing that the pixel
1055 // buffer passed to CVPixelBufferCreateWithPlanarBytes is released
1056 // via a callback and doing full IPA to make sure this is done
1057 // correctly.
1058 ScratchArgs = AF.Add(ScratchArgs, 12, StopTracking);
1059 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1060 DoNothing);
1061 }
1062 break;
Ted Kremenekea675cf2009-06-11 18:17:24 +00001063 }
Mike Stump11289f42009-09-09 15:08:12 +00001064
Ted Kremenekea675cf2009-06-11 18:17:24 +00001065 // Did we get a summary?
1066 if (S)
1067 break;
Ted Kremenek211094d2009-03-17 22:43:44 +00001068
1069 // Enable this code once the semantics of NSDeallocateObject are resolved
1070 // for GC. <rdar://problem/6619988>
1071#if 0
1072 // Handle: NSDeallocateObject(id anObject);
1073 // This method does allow 'nil' (although we don't check it now).
Mike Stump11289f42009-09-09 15:08:12 +00001074 if (strcmp(FName, "NSDeallocateObject") == 0) {
Ted Kremenek211094d2009-03-17 22:43:44 +00001075 return RetTy == Ctx.VoidTy
1076 ? getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, Dealloc)
1077 : getPersistentStopSummary();
1078 }
1079#endif
Ted Kremenek7e904222009-01-12 21:45:02 +00001080
1081 if (RetTy->isPointerType()) {
1082 // For CoreFoundation ('CF') types.
1083 if (isRefType(RetTy, "CF", &Ctx, FName)) {
1084 if (isRetain(FD, FName))
1085 S = getUnarySummary(FT, cfretain);
1086 else if (strstr(FName, "MakeCollectable"))
1087 S = getUnarySummary(FT, cfmakecollectable);
Mike Stump11289f42009-09-09 15:08:12 +00001088 else
Ted Kremenek7e904222009-01-12 21:45:02 +00001089 S = getCFCreateGetRuleSummary(FD, FName);
1090
1091 break;
1092 }
1093
1094 // For CoreGraphics ('CG') types.
1095 if (isRefType(RetTy, "CG", &Ctx, FName)) {
1096 if (isRetain(FD, FName))
1097 S = getUnarySummary(FT, cfretain);
1098 else
1099 S = getCFCreateGetRuleSummary(FD, FName);
1100
1101 break;
1102 }
1103
1104 // For the Disk Arbitration API (DiskArbitration/DADisk.h)
1105 if (isRefType(RetTy, "DADisk") ||
1106 isRefType(RetTy, "DADissenter") ||
1107 isRefType(RetTy, "DASessionRef")) {
1108 S = getCFCreateGetRuleSummary(FD, FName);
1109 break;
1110 }
Mike Stump11289f42009-09-09 15:08:12 +00001111
Ted Kremenek7e904222009-01-12 21:45:02 +00001112 break;
1113 }
1114
1115 // Check for release functions, the only kind of functions that we care
1116 // about that don't return a pointer type.
1117 if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) {
Ted Kremenek5f968932009-03-05 22:11:14 +00001118 // Test for 'CGCF'.
1119 if (FName[1] == 'G' && FName[2] == 'C' && FName[3] == 'F')
1120 FName += 4;
1121 else
1122 FName += 2;
Mike Stump11289f42009-09-09 15:08:12 +00001123
Ted Kremenek5f968932009-03-05 22:11:14 +00001124 if (isRelease(FD, FName))
Ted Kremenek7e904222009-01-12 21:45:02 +00001125 S = getUnarySummary(FT, cfrelease);
1126 else {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001127 assert (ScratchArgs.isEmpty());
Ted Kremeneked90de42009-01-29 22:45:13 +00001128 // Remaining CoreFoundation and CoreGraphics functions.
1129 // We use to assume that they all strictly followed the ownership idiom
1130 // and that ownership cannot be transferred. While this is technically
1131 // correct, many methods allow a tracked object to escape. For example:
1132 //
Mike Stump11289f42009-09-09 15:08:12 +00001133 // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...);
Ted Kremeneked90de42009-01-29 22:45:13 +00001134 // CFDictionaryAddValue(y, key, x);
Mike Stump11289f42009-09-09 15:08:12 +00001135 // CFRelease(x);
Ted Kremeneked90de42009-01-29 22:45:13 +00001136 // ... it is okay to use 'x' since 'y' has a reference to it
1137 //
1138 // We handle this and similar cases with the follow heuristic. If the
Ted Kremenekd982f002009-08-20 00:57:22 +00001139 // function name contains "InsertValue", "SetValue", "AddValue",
1140 // "AppendValue", or "SetAttribute", then we assume that arguments may
1141 // "escape." This means that something else holds on to the object,
1142 // allowing it be used even after its local retain count drops to 0.
Ted Kremeneked90de42009-01-29 22:45:13 +00001143 ArgEffect E = (CStrInCStrNoCase(FName, "InsertValue") ||
1144 CStrInCStrNoCase(FName, "AddValue") ||
Ted Kremenek0ca23d32009-02-05 22:34:53 +00001145 CStrInCStrNoCase(FName, "SetValue") ||
Ted Kremenekd982f002009-08-20 00:57:22 +00001146 CStrInCStrNoCase(FName, "AppendValue") ||
1147 CStrInCStrNoCase(FName, "SetAttribute"))
Ted Kremeneked90de42009-01-29 22:45:13 +00001148 ? MayEscape : DoNothing;
Mike Stump11289f42009-09-09 15:08:12 +00001149
Ted Kremeneked90de42009-01-29 22:45:13 +00001150 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E);
Ted Kremenek7e904222009-01-12 21:45:02 +00001151 }
1152 }
Ted Kremenekfa89e2f2008-07-15 16:50:12 +00001153 }
1154 while (0);
Mike Stump11289f42009-09-09 15:08:12 +00001155
Ted Kremenek1d9a2672009-05-04 05:31:22 +00001156 if (!S)
1157 S = getDefaultSummary();
Ted Kremenekf7141592008-04-24 17:22:33 +00001158
Ted Kremenekc2de7272009-05-09 02:58:13 +00001159 // Annotations override defaults.
1160 assert(S);
1161 updateSummaryFromAnnotations(*S, FD);
Mike Stump11289f42009-09-09 15:08:12 +00001162
Ted Kremenek00daccd2008-05-05 22:11:16 +00001163 FuncSummaries[FD] = S;
Mike Stump11289f42009-09-09 15:08:12 +00001164 return S;
Ted Kremenekea6507f2008-03-06 00:08:09 +00001165}
1166
Ted Kremenekfa89e2f2008-07-15 16:50:12 +00001167RetainSummary*
1168RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
1169 const char* FName) {
Mike Stump11289f42009-09-09 15:08:12 +00001170
Ted Kremenek875db812008-05-05 16:51:50 +00001171 if (strstr(FName, "Create") || strstr(FName, "Copy"))
1172 return getCFSummaryCreateRule(FD);
Mike Stump11289f42009-09-09 15:08:12 +00001173
Ted Kremenek875db812008-05-05 16:51:50 +00001174 if (strstr(FName, "Get"))
1175 return getCFSummaryGetRule(FD);
Mike Stump11289f42009-09-09 15:08:12 +00001176
Ted Kremenekff606a12009-05-04 04:57:00 +00001177 return getDefaultSummary();
Ted Kremenek875db812008-05-05 16:51:50 +00001178}
1179
Ted Kremenek00daccd2008-05-05 22:11:16 +00001180RetainSummary*
Ted Kremenek82157a12009-02-23 16:51:39 +00001181RetainSummaryManager::getUnarySummary(const FunctionType* FT,
1182 UnaryFuncKind func) {
1183
Ted Kremenek7e904222009-01-12 21:45:02 +00001184 // Sanity check that this is *really* a unary function. This can
1185 // happen if people do weird things.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001186 const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT);
Ted Kremenek7e904222009-01-12 21:45:02 +00001187 if (!FTP || FTP->getNumArgs() != 1)
1188 return getPersistentStopSummary();
Mike Stump11289f42009-09-09 15:08:12 +00001189
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001190 assert (ScratchArgs.isEmpty());
Mike Stump11289f42009-09-09 15:08:12 +00001191
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001192 switch (func) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001193 case cfretain: {
1194 ScratchArgs = AF.Add(ScratchArgs, 0, IncRef);
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00001195 return getPersistentSummary(RetEffect::MakeAlias(0),
1196 DoNothing, DoNothing);
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001197 }
Mike Stump11289f42009-09-09 15:08:12 +00001198
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001199 case cfrelease: {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001200 ScratchArgs = AF.Add(ScratchArgs, 0, DecRef);
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00001201 return getPersistentSummary(RetEffect::MakeNoRet(),
1202 DoNothing, DoNothing);
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001203 }
Mike Stump11289f42009-09-09 15:08:12 +00001204
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001205 case cfmakecollectable: {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001206 ScratchArgs = AF.Add(ScratchArgs, 0, MakeCollectable);
Mike Stump11289f42009-09-09 15:08:12 +00001207 return getPersistentSummary(RetEffect::MakeAlias(0),DoNothing, DoNothing);
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001208 }
Mike Stump11289f42009-09-09 15:08:12 +00001209
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001210 default:
Ted Kremenek875db812008-05-05 16:51:50 +00001211 assert (false && "Not a supported unary function.");
Ted Kremenekff606a12009-05-04 04:57:00 +00001212 return getDefaultSummary();
Ted Kremenek4b772092008-04-10 23:44:06 +00001213 }
Ted Kremenek68d73d12008-03-12 01:21:45 +00001214}
1215
Ted Kremenek00daccd2008-05-05 22:11:16 +00001216RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001217 assert (ScratchArgs.isEmpty());
Mike Stump11289f42009-09-09 15:08:12 +00001218
Ted Kremenekae529272008-07-09 18:11:16 +00001219 if (FD->getIdentifier() == CFDictionaryCreateII) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001220 ScratchArgs = AF.Add(ScratchArgs, 1, DoNothingByRef);
1221 ScratchArgs = AF.Add(ScratchArgs, 2, DoNothingByRef);
Ted Kremenekae529272008-07-09 18:11:16 +00001222 }
Mike Stump11289f42009-09-09 15:08:12 +00001223
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001224 return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenek68d73d12008-03-12 01:21:45 +00001225}
1226
Ted Kremenek00daccd2008-05-05 22:11:16 +00001227RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Mike Stump11289f42009-09-09 15:08:12 +00001228 assert (ScratchArgs.isEmpty());
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001229 return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF),
1230 DoNothing, DoNothing);
Ted Kremenek68d73d12008-03-12 01:21:45 +00001231}
1232
Ted Kremenek819e9b62008-03-11 06:39:11 +00001233//===----------------------------------------------------------------------===//
Ted Kremenek00daccd2008-05-05 22:11:16 +00001234// Summary creation for Selectors.
1235//===----------------------------------------------------------------------===//
1236
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001237RetainSummary*
Ted Kremenek0b50fb12009-04-29 05:04:30 +00001238RetainSummaryManager::getInitMethodSummary(QualType RetTy) {
Mike Stump11289f42009-09-09 15:08:12 +00001239 assert(ScratchArgs.isEmpty());
Ted Kremenek1272f702009-05-12 20:06:54 +00001240 // 'init' methods conceptually return a newly allocated object and claim
Mike Stump11289f42009-09-09 15:08:12 +00001241 // the receiver.
Ted Kremenek1272f702009-05-12 20:06:54 +00001242 if (isTrackedObjCObjectType(RetTy) || isTrackedCFObjectType(RetTy))
Ted Kremeneka03705c2009-06-05 23:18:01 +00001243 return getPersistentSummary(ObjCInitRetE, DecRefMsg);
Mike Stump11289f42009-09-09 15:08:12 +00001244
Ted Kremenek1272f702009-05-12 20:06:54 +00001245 return getDefaultSummary();
Ted Kremenek3d1e9722008-05-05 23:55:01 +00001246}
Ted Kremenekc2de7272009-05-09 02:58:13 +00001247
1248void
1249RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1250 const FunctionDecl *FD) {
1251 if (!FD)
1252 return;
1253
Ted Kremenekea675cf2009-06-11 18:17:24 +00001254 QualType RetTy = FD->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +00001255
Ted Kremenekc2de7272009-05-09 02:58:13 +00001256 // Determine if there is a special return effect for this method.
Ted Kremenekea1c2212009-06-05 23:00:33 +00001257 if (isTrackedObjCObjectType(RetTy)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001258 if (FD->getAttr<NSReturnsRetainedAttr>()) {
Ted Kremenekc2de7272009-05-09 02:58:13 +00001259 Summ.setRetEffect(ObjCAllocRetE);
1260 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001261 else if (FD->getAttr<CFReturnsRetainedAttr>()) {
Ted Kremenekea1c2212009-06-05 23:00:33 +00001262 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenekea675cf2009-06-11 18:17:24 +00001263 }
1264 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001265 else if (RetTy->getAs<PointerType>()) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001266 if (FD->getAttr<CFReturnsRetainedAttr>()) {
Ted Kremenekc2de7272009-05-09 02:58:13 +00001267 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
1268 }
1269 }
1270}
1271
1272void
1273RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1274 const ObjCMethodDecl *MD) {
1275 if (!MD)
1276 return;
1277
Ted Kremenek0578e432009-07-06 18:30:43 +00001278 bool isTrackedLoc = false;
Mike Stump11289f42009-09-09 15:08:12 +00001279
Ted Kremenekc2de7272009-05-09 02:58:13 +00001280 // Determine if there is a special return effect for this method.
1281 if (isTrackedObjCObjectType(MD->getResultType())) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001282 if (MD->getAttr<NSReturnsRetainedAttr>()) {
Ted Kremenekc2de7272009-05-09 02:58:13 +00001283 Summ.setRetEffect(ObjCAllocRetE);
Ted Kremenek0578e432009-07-06 18:30:43 +00001284 return;
Ted Kremenekc2de7272009-05-09 02:58:13 +00001285 }
Mike Stump11289f42009-09-09 15:08:12 +00001286
Ted Kremenek0578e432009-07-06 18:30:43 +00001287 isTrackedLoc = true;
Ted Kremenekc2de7272009-05-09 02:58:13 +00001288 }
Mike Stump11289f42009-09-09 15:08:12 +00001289
Ted Kremenek0578e432009-07-06 18:30:43 +00001290 if (!isTrackedLoc)
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001291 isTrackedLoc = MD->getResultType()->getAs<PointerType>() != NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001292
Ted Kremenek0578e432009-07-06 18:30:43 +00001293 if (isTrackedLoc && MD->getAttr<CFReturnsRetainedAttr>())
1294 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenekc2de7272009-05-09 02:58:13 +00001295}
1296
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001297RetainSummary*
Ted Kremenek223a7d52009-04-29 23:03:22 +00001298RetainSummaryManager::getCommonMethodSummary(const ObjCMethodDecl* MD,
1299 Selector S, QualType RetTy) {
Ted Kremenek6a966b22009-04-24 21:56:17 +00001300
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001301 if (MD) {
Ted Kremenek6e86caf2009-04-24 18:00:17 +00001302 // Scan the method decl for 'void*' arguments. These should be treated
1303 // as 'StopTracking' because they are often used with delegates.
1304 // Delegates are a frequent form of false positives with the retain
1305 // count checker.
1306 unsigned i = 0;
1307 for (ObjCMethodDecl::param_iterator I = MD->param_begin(),
1308 E = MD->param_end(); I != E; ++I, ++i)
1309 if (ParmVarDecl *PD = *I) {
1310 QualType Ty = Ctx.getCanonicalType(PD->getType());
1311 if (Ty.getUnqualifiedType() == Ctx.VoidPtrTy)
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001312 ScratchArgs = AF.Add(ScratchArgs, i, StopTracking);
Ted Kremenek6e86caf2009-04-24 18:00:17 +00001313 }
1314 }
Mike Stump11289f42009-09-09 15:08:12 +00001315
Ted Kremenek6a966b22009-04-24 21:56:17 +00001316 // Any special effect for the receiver?
1317 ArgEffect ReceiverEff = DoNothing;
Mike Stump11289f42009-09-09 15:08:12 +00001318
Ted Kremenek6a966b22009-04-24 21:56:17 +00001319 // If one of the arguments in the selector has the keyword 'delegate' we
1320 // should stop tracking the reference count for the receiver. This is
1321 // because the reference count is quite possibly handled by a delegate
1322 // method.
1323 if (S.isKeywordSelector()) {
1324 const std::string &str = S.getAsString();
1325 assert(!str.empty());
1326 if (CStrInCStrNoCase(&str[0], "delegate:")) ReceiverEff = StopTracking;
1327 }
Mike Stump11289f42009-09-09 15:08:12 +00001328
Ted Kremenek60746a02009-04-23 23:08:22 +00001329 // Look for methods that return an owned object.
Mike Stump11289f42009-09-09 15:08:12 +00001330 if (isTrackedObjCObjectType(RetTy)) {
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001331 // EXPERIMENTAL: Assume the Cocoa conventions for all objects returned
1332 // by instance methods.
Ted Kremenek32819772009-05-15 15:49:00 +00001333 RetEffect E = followsFundamentalRule(S)
1334 ? ObjCAllocRetE : RetEffect::MakeNotOwned(RetEffect::ObjC);
Mike Stump11289f42009-09-09 15:08:12 +00001335
1336 return getPersistentSummary(E, ReceiverEff, MayEscape);
Ted Kremenek6e86caf2009-04-24 18:00:17 +00001337 }
Mike Stump11289f42009-09-09 15:08:12 +00001338
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001339 // Look for methods that return an owned core foundation object.
1340 if (isTrackedCFObjectType(RetTy)) {
Ted Kremenek32819772009-05-15 15:49:00 +00001341 RetEffect E = followsFundamentalRule(S)
1342 ? RetEffect::MakeOwned(RetEffect::CF, true)
1343 : RetEffect::MakeNotOwned(RetEffect::CF);
Mike Stump11289f42009-09-09 15:08:12 +00001344
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001345 return getPersistentSummary(E, ReceiverEff, MayEscape);
1346 }
Mike Stump11289f42009-09-09 15:08:12 +00001347
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001348 if (ScratchArgs.isEmpty() && ReceiverEff == DoNothing)
Ted Kremenekff606a12009-05-04 04:57:00 +00001349 return getDefaultSummary();
Mike Stump11289f42009-09-09 15:08:12 +00001350
Ted Kremenek1d9a2672009-05-04 05:31:22 +00001351 return getPersistentSummary(RetEffect::MakeNoRet(), ReceiverEff, MayEscape);
Ted Kremenek60746a02009-04-23 23:08:22 +00001352}
1353
1354RetainSummary*
Ted Kremenek38724302009-04-29 17:09:14 +00001355RetainSummaryManager::getInstanceMethodSummary(Selector S,
1356 IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +00001357 const ObjCInterfaceDecl* ID,
1358 const ObjCMethodDecl *MD,
Ted Kremenek38724302009-04-29 17:09:14 +00001359 QualType RetTy) {
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001360
Ted Kremenek0b50fb12009-04-29 05:04:30 +00001361 // Look up a summary in our summary cache.
Ted Kremenek8be51382009-07-21 23:27:57 +00001362 RetainSummary *Summ = ObjCMethodSummaries.find(ID, ClsName, S);
Mike Stump11289f42009-09-09 15:08:12 +00001363
Ted Kremenek8be51382009-07-21 23:27:57 +00001364 if (!Summ) {
1365 assert(ScratchArgs.isEmpty());
Mike Stump11289f42009-09-09 15:08:12 +00001366
Ted Kremenek8be51382009-07-21 23:27:57 +00001367 // "initXXX": pass-through for receiver.
1368 if (deriveNamingConvention(S) == InitRule)
1369 Summ = getInitMethodSummary(RetTy);
1370 else
1371 Summ = getCommonMethodSummary(MD, S, RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001372
Ted Kremenek8be51382009-07-21 23:27:57 +00001373 // Annotations override defaults.
1374 updateSummaryFromAnnotations(*Summ, MD);
Mike Stump11289f42009-09-09 15:08:12 +00001375
Ted Kremenek8be51382009-07-21 23:27:57 +00001376 // Memoize the summary.
1377 ObjCMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1378 }
Mike Stump11289f42009-09-09 15:08:12 +00001379
Ted Kremenekf27110f2009-04-23 19:11:35 +00001380 return Summ;
Ted Kremenek3d1e9722008-05-05 23:55:01 +00001381}
1382
Ted Kremenek767d0742008-05-06 21:26:51 +00001383RetainSummary*
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001384RetainSummaryManager::getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +00001385 const ObjCInterfaceDecl *ID,
1386 const ObjCMethodDecl *MD,
1387 QualType RetTy) {
Ted Kremenek8a5ad392009-04-24 17:50:11 +00001388
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001389 assert(ClsName && "Class name must be specified.");
Mike Stump11289f42009-09-09 15:08:12 +00001390 RetainSummary *Summ = ObjCClassMethodSummaries.find(ID, ClsName, S);
1391
Ted Kremenek8be51382009-07-21 23:27:57 +00001392 if (!Summ) {
1393 Summ = getCommonMethodSummary(MD, S, RetTy);
1394 // Annotations override defaults.
1395 updateSummaryFromAnnotations(*Summ, MD);
1396 // Memoize the summary.
1397 ObjCClassMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1398 }
Mike Stump11289f42009-09-09 15:08:12 +00001399
Ted Kremenekf27110f2009-04-23 19:11:35 +00001400 return Summ;
Ted Kremenek767d0742008-05-06 21:26:51 +00001401}
1402
Mike Stump11289f42009-09-09 15:08:12 +00001403void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9157fbb2009-05-07 23:40:42 +00001404 assert(ScratchArgs.isEmpty());
1405 RetainSummary* Summ = getPersistentSummary(ObjCAllocRetE);
Mike Stump11289f42009-09-09 15:08:12 +00001406
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001407 // Create the summaries for "alloc", "new", and "allocWithZone:" for
1408 // NSObject and its derivatives.
1409 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
1410 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
1411 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001412
1413 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek55adb822009-10-15 22:25:12 +00001414 addClassMethSummary("NSAssertionHandler", "currentHandler",
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001415 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC)));
Mike Stump11289f42009-09-09 15:08:12 +00001416
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001417 // Create the [NSAutoreleasePool addObject:] summary.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001418 ScratchArgs = AF.Add(ScratchArgs, 0, Autorelease);
Ted Kremenek55adb822009-10-15 22:25:12 +00001419 addClassMethSummary("NSAutoreleasePool", "addObject",
1420 getPersistentSummary(RetEffect::MakeNoRet(),
1421 DoNothing, Autorelease));
Mike Stump11289f42009-09-09 15:08:12 +00001422
Ted Kremenek4e45d802009-10-15 22:26:21 +00001423 // Create a summary for [NSCursor dragCopyCursor].
1424 addClassMethSummary("NSCursor", "dragCopyCursor",
1425 getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1426 DoNothing));
1427
Ted Kremenek8a5ad392009-04-24 17:50:11 +00001428 // Create the summaries for [NSObject performSelector...]. We treat
1429 // these as 'stop tracking' for the arguments because they are often
1430 // used for delegates that can release the object. When we have better
1431 // inter-procedural analysis we can potentially do something better. This
1432 // workaround is to remove false positives.
1433 Summ = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, StopTracking);
1434 IdentifierInfo *NSObjectII = &Ctx.Idents.get("NSObject");
1435 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1436 "afterDelay", NULL);
1437 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1438 "afterDelay", "inModes", NULL);
1439 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1440 "withObject", "waitUntilDone", NULL);
1441 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1442 "withObject", "waitUntilDone", "modes", NULL);
1443 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1444 "withObject", "waitUntilDone", NULL);
1445 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1446 "withObject", "waitUntilDone", "modes", NULL);
1447 addClsMethSummary(NSObjectII, Summ, "performSelectorInBackground",
1448 "withObject", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001449
Ted Kremenekf9fa3cb2009-05-14 21:29:16 +00001450 // Specially handle NSData.
1451 RetainSummary *dataWithBytesNoCopySumm =
1452 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC), DoNothing,
1453 DoNothing);
1454 addClsMethSummary("NSData", dataWithBytesNoCopySumm,
1455 "dataWithBytesNoCopy", "length", NULL);
1456 addClsMethSummary("NSData", dataWithBytesNoCopySumm,
1457 "dataWithBytesNoCopy", "length", "freeWhenDone", NULL);
Ted Kremenek0806f912008-05-06 00:30:21 +00001458}
1459
Ted Kremenekea736c52008-06-23 22:21:20 +00001460void RetainSummaryManager::InitializeMethodSummaries() {
Mike Stump11289f42009-09-09 15:08:12 +00001461
1462 assert (ScratchArgs.isEmpty());
1463
Ted Kremenek767d0742008-05-06 21:26:51 +00001464 // Create the "init" selector. It just acts as a pass-through for the
1465 // receiver.
Mike Stump11289f42009-09-09 15:08:12 +00001466 RetainSummary *InitSumm = getPersistentSummary(ObjCInitRetE, DecRefMsg);
Ted Kremenek815fbb62009-08-20 05:13:36 +00001467 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
1468
1469 // awakeAfterUsingCoder: behaves basically like an 'init' method. It
1470 // claims the receiver and returns a retained object.
1471 addNSObjectMethSummary(GetUnarySelector("awakeAfterUsingCoder", Ctx),
1472 InitSumm);
Mike Stump11289f42009-09-09 15:08:12 +00001473
Ted Kremenek767d0742008-05-06 21:26:51 +00001474 // The next methods are allocators.
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001475 RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE);
Mike Stump11289f42009-09-09 15:08:12 +00001476 RetainSummary *CFAllocSumm =
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001477 getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Mike Stump11289f42009-09-09 15:08:12 +00001478
1479 // Create the "copy" selector.
1480 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), AllocSumm);
Ted Kremenek9551ab62008-08-12 20:41:56 +00001481
Ted Kremenekbe7c56e2008-05-06 00:38:54 +00001482 // Create the "mutableCopy" selector.
Ted Kremenek10369122009-05-20 22:39:57 +00001483 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), AllocSumm);
Mike Stump11289f42009-09-09 15:08:12 +00001484
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001485 // Create the "retain" selector.
Ted Kremenek9157fbb2009-05-07 23:40:42 +00001486 RetEffect E = RetEffect::MakeReceiverAlias();
Ted Kremenek10369122009-05-20 22:39:57 +00001487 RetainSummary *Summ = getPersistentSummary(E, IncRefMsg);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001488 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001489
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001490 // Create the "release" selector.
Ted Kremenekf68490a2009-02-18 18:54:33 +00001491 Summ = getPersistentSummary(E, DecRefMsg);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001492 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001493
Ted Kremenekbcdb4682008-05-07 21:17:39 +00001494 // Create the "drain" selector.
1495 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001496 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001497
Ted Kremenekea072e32009-03-17 19:42:23 +00001498 // Create the -dealloc summary.
1499 Summ = getPersistentSummary(RetEffect::MakeNoRet(), Dealloc);
1500 addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ);
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001501
1502 // Create the "autorelease" selector.
Ted Kremenekc7832092009-01-28 21:44:40 +00001503 Summ = getPersistentSummary(E, Autorelease);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001504 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001505
Ted Kremenek50db3d02009-02-23 17:45:03 +00001506 // Specially handle NSAutoreleasePool.
Ted Kremenekdce78462009-02-25 02:54:57 +00001507 addInstMethSummary("NSAutoreleasePool", "init",
Ted Kremenek50db3d02009-02-23 17:45:03 +00001508 getPersistentSummary(RetEffect::MakeReceiverAlias(),
Ted Kremenekdce78462009-02-25 02:54:57 +00001509 NewAutoreleasePool));
Mike Stump11289f42009-09-09 15:08:12 +00001510
1511 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremeneke73f2822009-02-23 02:51:29 +00001512 // FIXME: For now we opt for false negatives with NSWindow, as these objects
1513 // self-own themselves. However, they only do this once they are displayed.
1514 // Thus, we need to track an NSWindow's display status.
1515 // This is tracked in <rdar://problem/6062711>.
Ted Kremenek00dfe302009-03-04 23:30:42 +00001516 // See also http://llvm.org/bugs/show_bug.cgi?id=3714.
Ted Kremenek1272f702009-05-12 20:06:54 +00001517 RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet(),
1518 StopTracking,
1519 StopTracking);
Mike Stump11289f42009-09-09 15:08:12 +00001520
Ted Kremenek751e7e32009-04-03 19:02:51 +00001521 addClassMethSummary("NSWindow", "alloc", NoTrackYet);
1522
Ted Kremenek00dfe302009-03-04 23:30:42 +00001523#if 0
Ted Kremenek1272f702009-05-12 20:06:54 +00001524 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001525 "styleMask", "backing", "defer", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001526
Ted Kremenek1272f702009-05-12 20:06:54 +00001527 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001528 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek00dfe302009-03-04 23:30:42 +00001529#endif
Mike Stump11289f42009-09-09 15:08:12 +00001530
Ted Kremenek3f13f592008-08-12 18:48:50 +00001531 // For NSPanel (which subclasses NSWindow), allocated objects are not
1532 // self-owned.
Ted Kremenek751e7e32009-04-03 19:02:51 +00001533 // FIXME: For now we don't track NSPanels. object for the same reason
1534 // as for NSWindow objects.
1535 addClassMethSummary("NSPanel", "alloc", NoTrackYet);
Mike Stump11289f42009-09-09 15:08:12 +00001536
Ted Kremenek1272f702009-05-12 20:06:54 +00001537#if 0
1538 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001539 "styleMask", "backing", "defer", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001540
Ted Kremenek1272f702009-05-12 20:06:54 +00001541 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001542 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek1272f702009-05-12 20:06:54 +00001543#endif
Mike Stump11289f42009-09-09 15:08:12 +00001544
Ted Kremenek501ba032009-05-18 23:14:34 +00001545 // Don't track allocated autorelease pools yet, as it is okay to prematurely
1546 // exit a method.
1547 addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001548
Ted Kremenek3b2294c2008-07-18 17:24:20 +00001549 // Create NSAssertionHandler summaries.
Ted Kremenek050b91c2008-08-12 18:30:56 +00001550 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
Mike Stump11289f42009-09-09 15:08:12 +00001551 "lineNumber", "description", NULL);
1552
Ted Kremenek050b91c2008-08-12 18:30:56 +00001553 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1554 "file", "lineNumber", "description", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001555
Ted Kremenek10369122009-05-20 22:39:57 +00001556 // Create summaries QCRenderer/QCView -createSnapShotImageOfType:
1557 addInstMethSummary("QCRenderer", AllocSumm,
1558 "createSnapshotImageOfType", NULL);
1559 addInstMethSummary("QCView", AllocSumm,
1560 "createSnapshotImageOfType", NULL);
1561
Ted Kremenek96aa1462009-06-15 20:58:58 +00001562 // Create summaries for CIContext, 'createCGImage' and
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001563 // 'createCGLayerWithSize'. These objects are CF objects, and are not
1564 // automatically garbage collected.
1565 addInstMethSummary("CIContext", CFAllocSumm,
Ted Kremenek10369122009-05-20 22:39:57 +00001566 "createCGImage", "fromRect", NULL);
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001567 addInstMethSummary("CIContext", CFAllocSumm,
Mike Stump11289f42009-09-09 15:08:12 +00001568 "createCGImage", "fromRect", "format", "colorSpace", NULL);
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001569 addInstMethSummary("CIContext", CFAllocSumm, "createCGLayerWithSize",
Ted Kremenek96aa1462009-06-15 20:58:58 +00001570 "info", NULL);
Ted Kremenekbe7c56e2008-05-06 00:38:54 +00001571}
1572
Ted Kremenek00daccd2008-05-05 22:11:16 +00001573//===----------------------------------------------------------------------===//
Ted Kremenek71454892008-04-16 20:40:59 +00001574// Reference-counting logic (typestate + counts).
Ted Kremenek819e9b62008-03-11 06:39:11 +00001575//===----------------------------------------------------------------------===//
1576
Ted Kremenek819e9b62008-03-11 06:39:11 +00001577namespace {
Mike Stump11289f42009-09-09 15:08:12 +00001578
Ted Kremenekc8bef6a2008-04-09 23:49:11 +00001579class VISIBILITY_HIDDEN RefVal {
Mike Stump11289f42009-09-09 15:08:12 +00001580public:
Ted Kremeneka506fec2008-04-17 18:12:53 +00001581 enum Kind {
Mike Stump11289f42009-09-09 15:08:12 +00001582 Owned = 0, // Owning reference.
1583 NotOwned, // Reference is not owned by still valid (not freed).
Ted Kremeneka506fec2008-04-17 18:12:53 +00001584 Released, // Object has been released.
1585 ReturnedOwned, // Returned object passes ownership to caller.
1586 ReturnedNotOwned, // Return object does not pass ownership to caller.
Ted Kremenekea072e32009-03-17 19:42:23 +00001587 ERROR_START,
1588 ErrorDeallocNotOwned, // -dealloc called on non-owned object.
1589 ErrorDeallocGC, // Calling -dealloc with GC enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001590 ErrorUseAfterRelease, // Object used after released.
Ted Kremeneka506fec2008-04-17 18:12:53 +00001591 ErrorReleaseNotOwned, // Release of an object that was not owned.
Ted Kremenekea072e32009-03-17 19:42:23 +00001592 ERROR_LEAK_START,
Ted Kremenek631ff232008-10-22 23:56:21 +00001593 ErrorLeak, // A memory leak due to excessive reference counts.
Ted Kremenekd35272f2009-05-09 00:10:05 +00001594 ErrorLeakReturned, // A memory leak due to the returning method not having
1595 // the correct naming conventions.
Ted Kremenekdee56e32009-05-10 06:25:57 +00001596 ErrorGCLeakReturned,
1597 ErrorOverAutorelease,
1598 ErrorReturnedNotOwned
Ted Kremeneka506fec2008-04-17 18:12:53 +00001599 };
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001600
Mike Stump11289f42009-09-09 15:08:12 +00001601private:
Ted Kremeneka506fec2008-04-17 18:12:53 +00001602 Kind kind;
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001603 RetEffect::ObjKind okind;
Ted Kremeneka506fec2008-04-17 18:12:53 +00001604 unsigned Cnt;
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001605 unsigned ACnt;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001606 QualType T;
1607
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001608 RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t)
1609 : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {}
Ted Kremenekbf9d8042008-03-11 17:48:22 +00001610
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001611 RefVal(Kind k, unsigned cnt = 0)
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001612 : kind(k), okind(RetEffect::AnyObj), Cnt(cnt), ACnt(0) {}
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001613
Mike Stump11289f42009-09-09 15:08:12 +00001614public:
Ted Kremeneka506fec2008-04-17 18:12:53 +00001615 Kind getKind() const { return kind; }
Mike Stump11289f42009-09-09 15:08:12 +00001616
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001617 RetEffect::ObjKind getObjKind() const { return okind; }
Ted Kremenekbf9d8042008-03-11 17:48:22 +00001618
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001619 unsigned getCount() const { return Cnt; }
1620 unsigned getAutoreleaseCount() const { return ACnt; }
1621 unsigned getCombinedCounts() const { return Cnt + ACnt; }
1622 void clearCounts() { Cnt = 0; ACnt = 0; }
Ted Kremenekd35272f2009-05-09 00:10:05 +00001623 void setCount(unsigned i) { Cnt = i; }
1624 void setAutoreleaseCount(unsigned i) { ACnt = i; }
Mike Stump11289f42009-09-09 15:08:12 +00001625
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001626 QualType getType() const { return T; }
Mike Stump11289f42009-09-09 15:08:12 +00001627
Ted Kremeneka506fec2008-04-17 18:12:53 +00001628 // Useful predicates.
Mike Stump11289f42009-09-09 15:08:12 +00001629
Ted Kremenekea072e32009-03-17 19:42:23 +00001630 static bool isError(Kind k) { return k >= ERROR_START; }
Mike Stump11289f42009-09-09 15:08:12 +00001631
Ted Kremenekea072e32009-03-17 19:42:23 +00001632 static bool isLeak(Kind k) { return k >= ERROR_LEAK_START; }
Mike Stump11289f42009-09-09 15:08:12 +00001633
Ted Kremenek8784a7c2008-04-11 22:25:11 +00001634 bool isOwned() const {
1635 return getKind() == Owned;
1636 }
Mike Stump11289f42009-09-09 15:08:12 +00001637
Ted Kremenekcbf4c612008-04-16 22:32:20 +00001638 bool isNotOwned() const {
1639 return getKind() == NotOwned;
1640 }
Mike Stump11289f42009-09-09 15:08:12 +00001641
Ted Kremeneka506fec2008-04-17 18:12:53 +00001642 bool isReturnedOwned() const {
1643 return getKind() == ReturnedOwned;
1644 }
Mike Stump11289f42009-09-09 15:08:12 +00001645
Ted Kremeneka506fec2008-04-17 18:12:53 +00001646 bool isReturnedNotOwned() const {
1647 return getKind() == ReturnedNotOwned;
1648 }
Mike Stump11289f42009-09-09 15:08:12 +00001649
Ted Kremeneka506fec2008-04-17 18:12:53 +00001650 bool isNonLeakError() const {
1651 Kind k = getKind();
1652 return isError(k) && !isLeak(k);
1653 }
Mike Stump11289f42009-09-09 15:08:12 +00001654
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001655 static RefVal makeOwned(RetEffect::ObjKind o, QualType t,
1656 unsigned Count = 1) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001657 return RefVal(Owned, o, Count, 0, t);
Ted Kremenek3c03d522008-04-10 23:09:18 +00001658 }
Mike Stump11289f42009-09-09 15:08:12 +00001659
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001660 static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t,
1661 unsigned Count = 0) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001662 return RefVal(NotOwned, o, Count, 0, t);
Ted Kremenek3c03d522008-04-10 23:09:18 +00001663 }
Mike Stump11289f42009-09-09 15:08:12 +00001664
Ted Kremeneka506fec2008-04-17 18:12:53 +00001665 // Comparison, profiling, and pretty-printing.
Mike Stump11289f42009-09-09 15:08:12 +00001666
Ted Kremeneka506fec2008-04-17 18:12:53 +00001667 bool operator==(const RefVal& X) const {
Ted Kremenek3978f792009-05-10 05:11:21 +00001668 return kind == X.kind && Cnt == X.Cnt && T == X.T && ACnt == X.ACnt;
Ted Kremeneka506fec2008-04-17 18:12:53 +00001669 }
Mike Stump11289f42009-09-09 15:08:12 +00001670
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001671 RefVal operator-(size_t i) const {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001672 return RefVal(getKind(), getObjKind(), getCount() - i,
1673 getAutoreleaseCount(), getType());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001674 }
Mike Stump11289f42009-09-09 15:08:12 +00001675
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001676 RefVal operator+(size_t i) const {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001677 return RefVal(getKind(), getObjKind(), getCount() + i,
1678 getAutoreleaseCount(), getType());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001679 }
Mike Stump11289f42009-09-09 15:08:12 +00001680
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001681 RefVal operator^(Kind k) const {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001682 return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(),
1683 getType());
1684 }
Mike Stump11289f42009-09-09 15:08:12 +00001685
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001686 RefVal autorelease() const {
1687 return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1,
1688 getType());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001689 }
Mike Stump11289f42009-09-09 15:08:12 +00001690
Ted Kremeneka506fec2008-04-17 18:12:53 +00001691 void Profile(llvm::FoldingSetNodeID& ID) const {
1692 ID.AddInteger((unsigned) kind);
1693 ID.AddInteger(Cnt);
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001694 ID.AddInteger(ACnt);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001695 ID.Add(T);
Ted Kremeneka506fec2008-04-17 18:12:53 +00001696 }
1697
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001698 void print(llvm::raw_ostream& Out) const;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00001699};
Mike Stump11289f42009-09-09 15:08:12 +00001700
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001701void RefVal::print(llvm::raw_ostream& Out) const {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001702 if (!T.isNull())
1703 Out << "Tracked Type:" << T.getAsString() << '\n';
Mike Stump11289f42009-09-09 15:08:12 +00001704
Ted Kremenek2a723e62008-03-11 19:44:10 +00001705 switch (getKind()) {
1706 default: assert(false);
Mike Stump11289f42009-09-09 15:08:12 +00001707 case Owned: {
Ted Kremenek3c03d522008-04-10 23:09:18 +00001708 Out << "Owned";
1709 unsigned cnt = getCount();
1710 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek2a723e62008-03-11 19:44:10 +00001711 break;
Ted Kremenek3c03d522008-04-10 23:09:18 +00001712 }
Mike Stump11289f42009-09-09 15:08:12 +00001713
Ted Kremenek3c03d522008-04-10 23:09:18 +00001714 case NotOwned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00001715 Out << "NotOwned";
Ted Kremenek3c03d522008-04-10 23:09:18 +00001716 unsigned cnt = getCount();
1717 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek2a723e62008-03-11 19:44:10 +00001718 break;
Ted Kremenek3c03d522008-04-10 23:09:18 +00001719 }
Mike Stump11289f42009-09-09 15:08:12 +00001720
1721 case ReturnedOwned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00001722 Out << "ReturnedOwned";
1723 unsigned cnt = getCount();
1724 if (cnt) Out << " (+ " << cnt << ")";
1725 break;
1726 }
Mike Stump11289f42009-09-09 15:08:12 +00001727
Ted Kremeneka506fec2008-04-17 18:12:53 +00001728 case ReturnedNotOwned: {
1729 Out << "ReturnedNotOwned";
1730 unsigned cnt = getCount();
1731 if (cnt) Out << " (+ " << cnt << ")";
1732 break;
1733 }
Mike Stump11289f42009-09-09 15:08:12 +00001734
Ted Kremenek2a723e62008-03-11 19:44:10 +00001735 case Released:
1736 Out << "Released";
1737 break;
Ted Kremenekea072e32009-03-17 19:42:23 +00001738
1739 case ErrorDeallocGC:
1740 Out << "-dealloc (GC)";
1741 break;
Mike Stump11289f42009-09-09 15:08:12 +00001742
Ted Kremenekea072e32009-03-17 19:42:23 +00001743 case ErrorDeallocNotOwned:
1744 Out << "-dealloc (not-owned)";
1745 break;
Mike Stump11289f42009-09-09 15:08:12 +00001746
Ted Kremenekcbf4c612008-04-16 22:32:20 +00001747 case ErrorLeak:
1748 Out << "Leaked";
Mike Stump11289f42009-09-09 15:08:12 +00001749 break;
1750
Ted Kremenek631ff232008-10-22 23:56:21 +00001751 case ErrorLeakReturned:
1752 Out << "Leaked (Bad naming)";
1753 break;
Mike Stump11289f42009-09-09 15:08:12 +00001754
Ted Kremenekdee56e32009-05-10 06:25:57 +00001755 case ErrorGCLeakReturned:
1756 Out << "Leaked (GC-ed at return)";
1757 break;
1758
Ted Kremenek2a723e62008-03-11 19:44:10 +00001759 case ErrorUseAfterRelease:
1760 Out << "Use-After-Release [ERROR]";
1761 break;
Mike Stump11289f42009-09-09 15:08:12 +00001762
Ted Kremenek2a723e62008-03-11 19:44:10 +00001763 case ErrorReleaseNotOwned:
1764 Out << "Release of Not-Owned [ERROR]";
1765 break;
Mike Stump11289f42009-09-09 15:08:12 +00001766
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00001767 case RefVal::ErrorOverAutorelease:
1768 Out << "Over autoreleased";
1769 break;
Mike Stump11289f42009-09-09 15:08:12 +00001770
Ted Kremenekdee56e32009-05-10 06:25:57 +00001771 case RefVal::ErrorReturnedNotOwned:
1772 Out << "Non-owned object returned instead of owned";
1773 break;
Ted Kremenek2a723e62008-03-11 19:44:10 +00001774 }
Mike Stump11289f42009-09-09 15:08:12 +00001775
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001776 if (ACnt) {
1777 Out << " [ARC +" << ACnt << ']';
1778 }
Ted Kremenek2a723e62008-03-11 19:44:10 +00001779}
Mike Stump11289f42009-09-09 15:08:12 +00001780
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001781} // end anonymous namespace
1782
1783//===----------------------------------------------------------------------===//
1784// RefBindings - State used to track object reference counts.
1785//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +00001786
Ted Kremenekd8242f12008-12-05 02:27:51 +00001787typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001788static int RefBIndex = 0;
1789
1790namespace clang {
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001791 template<>
1792 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
Mike Stump11289f42009-09-09 15:08:12 +00001793 static inline void* GDMIndex() { return &RefBIndex; }
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001794 };
1795}
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001796
1797//===----------------------------------------------------------------------===//
Ted Kremenekc52f9392009-02-24 19:15:11 +00001798// AutoreleaseBindings - State used to track objects in autorelease pools.
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001799//===----------------------------------------------------------------------===//
1800
Ted Kremenekc52f9392009-02-24 19:15:11 +00001801typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts;
1802typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents;
1803typedef llvm::ImmutableList<SymbolRef> ARStack;
Ted Kremenek50db3d02009-02-23 17:45:03 +00001804
Ted Kremenekc52f9392009-02-24 19:15:11 +00001805static int AutoRCIndex = 0;
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001806static int AutoRBIndex = 0;
1807
Ted Kremenekc52f9392009-02-24 19:15:11 +00001808namespace { class VISIBILITY_HIDDEN AutoreleasePoolContents {}; }
Ted Kremenekdce78462009-02-25 02:54:57 +00001809namespace { class VISIBILITY_HIDDEN AutoreleaseStack {}; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001810
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001811namespace clang {
Ted Kremenekdce78462009-02-25 02:54:57 +00001812template<> struct GRStateTrait<AutoreleaseStack>
Ted Kremenekc52f9392009-02-24 19:15:11 +00001813 : public GRStatePartialTrait<ARStack> {
Mike Stump11289f42009-09-09 15:08:12 +00001814 static inline void* GDMIndex() { return &AutoRBIndex; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001815};
1816
1817template<> struct GRStateTrait<AutoreleasePoolContents>
1818 : public GRStatePartialTrait<ARPoolContents> {
Mike Stump11289f42009-09-09 15:08:12 +00001819 static inline void* GDMIndex() { return &AutoRCIndex; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001820};
1821} // end clang namespace
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001822
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001823static SymbolRef GetCurrentAutoreleasePool(const GRState* state) {
1824 ARStack stack = state->get<AutoreleaseStack>();
1825 return stack.isEmpty() ? SymbolRef() : stack.getHead();
1826}
1827
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001828static const GRState * SendAutorelease(const GRState *state,
1829 ARCounts::Factory &F, SymbolRef sym) {
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001830
1831 SymbolRef pool = GetCurrentAutoreleasePool(state);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001832 const ARCounts *cnts = state->get<AutoreleasePoolContents>(pool);
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001833 ARCounts newCnts(0);
Mike Stump11289f42009-09-09 15:08:12 +00001834
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001835 if (cnts) {
1836 const unsigned *cnt = (*cnts).lookup(sym);
1837 newCnts = F.Add(*cnts, sym, cnt ? *cnt + 1 : 1);
1838 }
1839 else
1840 newCnts = F.Add(F.GetEmptyMap(), sym, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001841
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001842 return state->set<AutoreleasePoolContents>(pool, newCnts);
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001843}
1844
Ted Kremenek71454892008-04-16 20:40:59 +00001845//===----------------------------------------------------------------------===//
1846// Transfer functions.
1847//===----------------------------------------------------------------------===//
1848
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001849namespace {
Mike Stump11289f42009-09-09 15:08:12 +00001850
Ted Kremenek1642bda2009-06-26 00:05:51 +00001851class VISIBILITY_HIDDEN CFRefCount : public GRTransferFuncs {
Ted Kremenek396f4362008-04-18 03:39:05 +00001852public:
Ted Kremenek16306102008-08-13 21:24:49 +00001853 class BindingsPrinter : public GRState::Printer {
Ted Kremenek2a723e62008-03-11 19:44:10 +00001854 public:
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001855 virtual void Print(llvm::raw_ostream& Out, const GRState* state,
Ted Kremenek16306102008-08-13 21:24:49 +00001856 const char* nl, const char* sep);
Ted Kremenek2a723e62008-03-11 19:44:10 +00001857 };
Ted Kremenek396f4362008-04-18 03:39:05 +00001858
1859private:
Zhongxing Xu107f7592009-08-06 12:48:26 +00001860 typedef llvm::DenseMap<const ExplodedNode*, const RetainSummary*>
Mike Stump11289f42009-09-09 15:08:12 +00001861 SummaryLogTy;
Ted Kremenek48d16452009-02-18 03:48:14 +00001862
Mike Stump11289f42009-09-09 15:08:12 +00001863 RetainSummaryManager Summaries;
Ted Kremenek48d16452009-02-18 03:48:14 +00001864 SummaryLogTy SummaryLog;
Ted Kremenek00daccd2008-05-05 22:11:16 +00001865 const LangOptions& LOpts;
Ted Kremenekc52f9392009-02-24 19:15:11 +00001866 ARCounts::Factory ARCountFactory;
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001867
Ted Kremenek400aae72009-02-05 06:50:21 +00001868 BugType *useAfterRelease, *releaseNotOwned;
Ted Kremenekea072e32009-03-17 19:42:23 +00001869 BugType *deallocGC, *deallocNotOwned;
Ted Kremenek400aae72009-02-05 06:50:21 +00001870 BugType *leakWithinFunction, *leakAtReturn;
Ted Kremenekd35272f2009-05-09 00:10:05 +00001871 BugType *overAutorelease;
Ted Kremenekdee56e32009-05-10 06:25:57 +00001872 BugType *returnNotOwnedForOwned;
Ted Kremenek400aae72009-02-05 06:50:21 +00001873 BugReporter *BR;
Mike Stump11289f42009-09-09 15:08:12 +00001874
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001875 const GRState * Update(const GRState * state, SymbolRef sym, RefVal V, ArgEffect E,
Ted Kremenekc52f9392009-02-24 19:15:11 +00001876 RefVal::Kind& hasErr);
1877
Zhongxing Xu20227f72009-08-06 01:32:16 +00001878 void ProcessNonLeakError(ExplodedNodeSet& Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001879 GRStmtNodeBuilder& Builder,
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00001880 Expr* NodeExpr, Expr* ErrorExpr,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001881 ExplodedNode* Pred,
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +00001882 const GRState* St,
Ted Kremenekd8242f12008-12-05 02:27:51 +00001883 RefVal::Kind hasErr, SymbolRef Sym);
Mike Stump11289f42009-09-09 15:08:12 +00001884
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001885 const GRState * HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek884a8992009-05-08 23:09:42 +00001886 llvm::SmallVectorImpl<SymbolRef> &Leaked);
Mike Stump11289f42009-09-09 15:08:12 +00001887
Zhongxing Xu20227f72009-08-06 01:32:16 +00001888 ExplodedNode* ProcessLeaks(const GRState * state,
Ted Kremenek884a8992009-05-08 23:09:42 +00001889 llvm::SmallVectorImpl<SymbolRef> &Leaked,
1890 GenericNodeBuilder &Builder,
1891 GRExprEngine &Eng,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001892 ExplodedNode *Pred = 0);
Mike Stump11289f42009-09-09 15:08:12 +00001893
1894public:
Ted Kremenek1f352db2008-07-22 16:21:24 +00001895 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001896 : Summaries(Ctx, gcenabled),
Ted Kremenekea072e32009-03-17 19:42:23 +00001897 LOpts(lopts), useAfterRelease(0), releaseNotOwned(0),
1898 deallocGC(0), deallocNotOwned(0),
Ted Kremenekdee56e32009-05-10 06:25:57 +00001899 leakWithinFunction(0), leakAtReturn(0), overAutorelease(0),
1900 returnNotOwnedForOwned(0), BR(0) {}
Mike Stump11289f42009-09-09 15:08:12 +00001901
Ted Kremenek400aae72009-02-05 06:50:21 +00001902 virtual ~CFRefCount() {}
Mike Stump11289f42009-09-09 15:08:12 +00001903
Ted Kremenekfc5d0672009-02-04 23:49:09 +00001904 void RegisterChecks(BugReporter &BR);
Mike Stump11289f42009-09-09 15:08:12 +00001905
Ted Kremenekceba6ea2008-08-16 00:49:49 +00001906 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1907 Printers.push_back(new BindingsPrinter());
Ted Kremenek2a723e62008-03-11 19:44:10 +00001908 }
Mike Stump11289f42009-09-09 15:08:12 +00001909
Ted Kremenek00daccd2008-05-05 22:11:16 +00001910 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenekb0f87c42008-04-30 23:47:44 +00001911 const LangOptions& getLangOptions() const { return LOpts; }
Mike Stump11289f42009-09-09 15:08:12 +00001912
Zhongxing Xu20227f72009-08-06 01:32:16 +00001913 const RetainSummary *getSummaryOfNode(const ExplodedNode *N) const {
Ted Kremenek48d16452009-02-18 03:48:14 +00001914 SummaryLogTy::const_iterator I = SummaryLog.find(N);
1915 return I == SummaryLog.end() ? 0 : I->second;
1916 }
Mike Stump11289f42009-09-09 15:08:12 +00001917
Ted Kremenek819e9b62008-03-11 06:39:11 +00001918 // Calls.
Ted Kremenek00daccd2008-05-05 22:11:16 +00001919
Zhongxing Xu20227f72009-08-06 01:32:16 +00001920 void EvalSummary(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00001921 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001922 GRStmtNodeBuilder& Builder,
Ted Kremenek00daccd2008-05-05 22:11:16 +00001923 Expr* Ex,
1924 Expr* Receiver,
Ted Kremenekff606a12009-05-04 04:57:00 +00001925 const RetainSummary& Summ,
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00001926 ExprIterator arg_beg, ExprIterator arg_end,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001927 ExplodedNode* Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001928
Zhongxing Xu20227f72009-08-06 01:32:16 +00001929 virtual void EvalCall(ExplodedNodeSet& Dst,
Ted Kremenek626bd2d2008-03-12 21:06:49 +00001930 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001931 GRStmtNodeBuilder& Builder,
Zhongxing Xu27f17422008-10-17 05:57:07 +00001932 CallExpr* CE, SVal L,
Mike Stump11289f42009-09-09 15:08:12 +00001933 ExplodedNode* Pred);
1934
1935
Zhongxing Xu20227f72009-08-06 01:32:16 +00001936 virtual void EvalObjCMessageExpr(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001937 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001938 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001939 ObjCMessageExpr* ME,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001940 ExplodedNode* Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001941
Zhongxing Xu20227f72009-08-06 01:32:16 +00001942 bool EvalObjCMessageExprAux(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001943 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001944 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001945 ObjCMessageExpr* ME,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001946 ExplodedNode* Pred);
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001947
Mike Stump11289f42009-09-09 15:08:12 +00001948 // Stores.
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00001949 virtual void EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val);
1950
Ted Kremenek8784a7c2008-04-11 22:25:11 +00001951 // End-of-path.
Mike Stump11289f42009-09-09 15:08:12 +00001952
Ted Kremenek8784a7c2008-04-11 22:25:11 +00001953 virtual void EvalEndPath(GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001954 GREndPathNodeBuilder& Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001955
Zhongxing Xu20227f72009-08-06 01:32:16 +00001956 virtual void EvalDeadSymbols(ExplodedNodeSet& Dst,
Ted Kremenekb0daf2f2008-04-24 23:57:27 +00001957 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001958 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001959 ExplodedNode* Pred,
Ted Kremenek16fbfe62009-01-21 22:26:05 +00001960 Stmt* S, const GRState* state,
1961 SymbolReaper& SymReaper);
Mike Stump11289f42009-09-09 15:08:12 +00001962
Zhongxing Xu20227f72009-08-06 01:32:16 +00001963 std::pair<ExplodedNode*, const GRState *>
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001964 HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001965 ExplodedNode* Pred, GRExprEngine &Eng,
Ted Kremenekd35272f2009-05-09 00:10:05 +00001966 SymbolRef Sym, RefVal V, bool &stop);
Ted Kremeneka506fec2008-04-17 18:12:53 +00001967 // Return statements.
Mike Stump11289f42009-09-09 15:08:12 +00001968
Zhongxing Xu20227f72009-08-06 01:32:16 +00001969 virtual void EvalReturn(ExplodedNodeSet& Dst,
Ted Kremeneka506fec2008-04-17 18:12:53 +00001970 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001971 GRStmtNodeBuilder& Builder,
Ted Kremeneka506fec2008-04-17 18:12:53 +00001972 ReturnStmt* S,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001973 ExplodedNode* Pred);
Ted Kremenek4d837282008-04-18 19:23:43 +00001974
1975 // Assumptions.
1976
Ted Kremenekf9906842009-06-18 22:57:13 +00001977 virtual const GRState *EvalAssume(const GRState* state, SVal condition,
1978 bool assumption);
Ted Kremenek819e9b62008-03-11 06:39:11 +00001979};
1980
1981} // end anonymous namespace
1982
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001983static void PrintPool(llvm::raw_ostream &Out, SymbolRef Sym,
1984 const GRState *state) {
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001985 Out << ' ';
Ted Kremenek3e31c262009-03-26 03:35:11 +00001986 if (Sym)
1987 Out << Sym->getSymbolID();
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001988 else
1989 Out << "<pool>";
1990 Out << ":{";
Mike Stump11289f42009-09-09 15:08:12 +00001991
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001992 // Get the contents of the pool.
1993 if (const ARCounts *cnts = state->get<AutoreleasePoolContents>(Sym))
1994 for (ARCounts::iterator J=cnts->begin(), EJ=cnts->end(); J != EJ; ++J)
1995 Out << '(' << J.getKey() << ',' << J.getData() << ')';
1996
Mike Stump11289f42009-09-09 15:08:12 +00001997 Out << '}';
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001998}
Ted Kremenek396f4362008-04-18 03:39:05 +00001999
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002000void CFRefCount::BindingsPrinter::Print(llvm::raw_ostream& Out,
2001 const GRState* state,
Ted Kremenek16306102008-08-13 21:24:49 +00002002 const char* nl, const char* sep) {
Mike Stump11289f42009-09-09 15:08:12 +00002003
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00002004 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00002005
Ted Kremenek16306102008-08-13 21:24:49 +00002006 if (!B.isEmpty())
Ted Kremenek2a723e62008-03-11 19:44:10 +00002007 Out << sep << nl;
Mike Stump11289f42009-09-09 15:08:12 +00002008
Ted Kremenek2a723e62008-03-11 19:44:10 +00002009 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
2010 Out << (*I).first << " : ";
2011 (*I).second.print(Out);
2012 Out << nl;
2013 }
Mike Stump11289f42009-09-09 15:08:12 +00002014
Ted Kremenekdce78462009-02-25 02:54:57 +00002015 // Print the autorelease stack.
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002016 Out << sep << nl << "AR pool stack:";
Ted Kremenekdce78462009-02-25 02:54:57 +00002017 ARStack stack = state->get<AutoreleaseStack>();
Mike Stump11289f42009-09-09 15:08:12 +00002018
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002019 PrintPool(Out, SymbolRef(), state); // Print the caller's pool.
2020 for (ARStack::iterator I=stack.begin(), E=stack.end(); I!=E; ++I)
2021 PrintPool(Out, *I, state);
2022
2023 Out << nl;
Ted Kremenek2a723e62008-03-11 19:44:10 +00002024}
2025
Ted Kremenek6bd78702009-04-29 18:50:19 +00002026//===----------------------------------------------------------------------===//
2027// Error reporting.
2028//===----------------------------------------------------------------------===//
2029
2030namespace {
Mike Stump11289f42009-09-09 15:08:12 +00002031
Ted Kremenek6bd78702009-04-29 18:50:19 +00002032 //===-------------===//
2033 // Bug Descriptions. //
Mike Stump11289f42009-09-09 15:08:12 +00002034 //===-------------===//
2035
Ted Kremenek6bd78702009-04-29 18:50:19 +00002036 class VISIBILITY_HIDDEN CFRefBug : public BugType {
2037 protected:
2038 CFRefCount& TF;
Mike Stump11289f42009-09-09 15:08:12 +00002039
2040 CFRefBug(CFRefCount* tf, const char* name)
2041 : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {}
Ted Kremenek6bd78702009-04-29 18:50:19 +00002042 public:
Mike Stump11289f42009-09-09 15:08:12 +00002043
Ted Kremenek6bd78702009-04-29 18:50:19 +00002044 CFRefCount& getTF() { return TF; }
2045 const CFRefCount& getTF() const { return TF; }
Mike Stump11289f42009-09-09 15:08:12 +00002046
Ted Kremenek6bd78702009-04-29 18:50:19 +00002047 // FIXME: Eventually remove.
2048 virtual const char* getDescription() const = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002049
Ted Kremenek6bd78702009-04-29 18:50:19 +00002050 virtual bool isLeak() const { return false; }
2051 };
Mike Stump11289f42009-09-09 15:08:12 +00002052
Ted Kremenek6bd78702009-04-29 18:50:19 +00002053 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2054 public:
2055 UseAfterRelease(CFRefCount* tf)
2056 : CFRefBug(tf, "Use-after-release") {}
Mike Stump11289f42009-09-09 15:08:12 +00002057
Ted Kremenek6bd78702009-04-29 18:50:19 +00002058 const char* getDescription() const {
2059 return "Reference-counted object is used after it is released";
Mike Stump11289f42009-09-09 15:08:12 +00002060 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002061 };
Mike Stump11289f42009-09-09 15:08:12 +00002062
Ted Kremenek6bd78702009-04-29 18:50:19 +00002063 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2064 public:
2065 BadRelease(CFRefCount* tf) : CFRefBug(tf, "Bad release") {}
Mike Stump11289f42009-09-09 15:08:12 +00002066
Ted Kremenek6bd78702009-04-29 18:50:19 +00002067 const char* getDescription() const {
Ted Kremenek5c22e112009-10-01 17:31:50 +00002068 return "Incorrect decrement of the reference count of an object that is "
2069 "not owned at this point by the caller";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002070 }
2071 };
Mike Stump11289f42009-09-09 15:08:12 +00002072
Ted Kremenek6bd78702009-04-29 18:50:19 +00002073 class VISIBILITY_HIDDEN DeallocGC : public CFRefBug {
2074 public:
Ted Kremenekd35272f2009-05-09 00:10:05 +00002075 DeallocGC(CFRefCount *tf)
2076 : CFRefBug(tf, "-dealloc called while using garbage collection") {}
Mike Stump11289f42009-09-09 15:08:12 +00002077
Ted Kremenek6bd78702009-04-29 18:50:19 +00002078 const char *getDescription() const {
Ted Kremenekd35272f2009-05-09 00:10:05 +00002079 return "-dealloc called while using garbage collection";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002080 }
2081 };
Mike Stump11289f42009-09-09 15:08:12 +00002082
Ted Kremenek6bd78702009-04-29 18:50:19 +00002083 class VISIBILITY_HIDDEN DeallocNotOwned : public CFRefBug {
2084 public:
Ted Kremenekd35272f2009-05-09 00:10:05 +00002085 DeallocNotOwned(CFRefCount *tf)
2086 : CFRefBug(tf, "-dealloc sent to non-exclusively owned object") {}
Mike Stump11289f42009-09-09 15:08:12 +00002087
Ted Kremenek6bd78702009-04-29 18:50:19 +00002088 const char *getDescription() const {
2089 return "-dealloc sent to object that may be referenced elsewhere";
2090 }
Mike Stump11289f42009-09-09 15:08:12 +00002091 };
2092
Ted Kremenekd35272f2009-05-09 00:10:05 +00002093 class VISIBILITY_HIDDEN OverAutorelease : public CFRefBug {
2094 public:
Mike Stump11289f42009-09-09 15:08:12 +00002095 OverAutorelease(CFRefCount *tf) :
Ted Kremenekd35272f2009-05-09 00:10:05 +00002096 CFRefBug(tf, "Object sent -autorelease too many times") {}
Mike Stump11289f42009-09-09 15:08:12 +00002097
Ted Kremenekd35272f2009-05-09 00:10:05 +00002098 const char *getDescription() const {
Ted Kremenek3978f792009-05-10 05:11:21 +00002099 return "Object sent -autorelease too many times";
Ted Kremenekd35272f2009-05-09 00:10:05 +00002100 }
2101 };
Mike Stump11289f42009-09-09 15:08:12 +00002102
Ted Kremenekdee56e32009-05-10 06:25:57 +00002103 class VISIBILITY_HIDDEN ReturnedNotOwnedForOwned : public CFRefBug {
2104 public:
2105 ReturnedNotOwnedForOwned(CFRefCount *tf) :
2106 CFRefBug(tf, "Method should return an owned object") {}
Mike Stump11289f42009-09-09 15:08:12 +00002107
Ted Kremenekdee56e32009-05-10 06:25:57 +00002108 const char *getDescription() const {
2109 return "Object with +0 retain counts returned to caller where a +1 "
2110 "(owning) retain count is expected";
2111 }
2112 };
Mike Stump11289f42009-09-09 15:08:12 +00002113
Ted Kremenek6bd78702009-04-29 18:50:19 +00002114 class VISIBILITY_HIDDEN Leak : public CFRefBug {
2115 const bool isReturn;
2116 protected:
2117 Leak(CFRefCount* tf, const char* name, bool isRet)
2118 : CFRefBug(tf, name), isReturn(isRet) {}
2119 public:
Mike Stump11289f42009-09-09 15:08:12 +00002120
Ted Kremenek6bd78702009-04-29 18:50:19 +00002121 const char* getDescription() const { return ""; }
Mike Stump11289f42009-09-09 15:08:12 +00002122
Ted Kremenek6bd78702009-04-29 18:50:19 +00002123 bool isLeak() const { return true; }
2124 };
Mike Stump11289f42009-09-09 15:08:12 +00002125
Ted Kremenek6bd78702009-04-29 18:50:19 +00002126 class VISIBILITY_HIDDEN LeakAtReturn : public Leak {
2127 public:
2128 LeakAtReturn(CFRefCount* tf, const char* name)
2129 : Leak(tf, name, true) {}
2130 };
Mike Stump11289f42009-09-09 15:08:12 +00002131
Ted Kremenek6bd78702009-04-29 18:50:19 +00002132 class VISIBILITY_HIDDEN LeakWithinFunction : public Leak {
2133 public:
2134 LeakWithinFunction(CFRefCount* tf, const char* name)
2135 : Leak(tf, name, false) {}
Mike Stump11289f42009-09-09 15:08:12 +00002136 };
2137
Ted Kremenek6bd78702009-04-29 18:50:19 +00002138 //===---------===//
2139 // Bug Reports. //
2140 //===---------===//
Mike Stump11289f42009-09-09 15:08:12 +00002141
Ted Kremenek6bd78702009-04-29 18:50:19 +00002142 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
2143 protected:
2144 SymbolRef Sym;
2145 const CFRefCount &TF;
2146 public:
2147 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002148 ExplodedNode *n, SymbolRef sym)
Ted Kremenek3978f792009-05-10 05:11:21 +00002149 : RangedBugReport(D, D.getDescription(), n), Sym(sym), TF(tf) {}
2150
2151 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002152 ExplodedNode *n, SymbolRef sym, const char* endText)
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00002153 : RangedBugReport(D, D.getDescription(), endText, n), Sym(sym), TF(tf) {}
Mike Stump11289f42009-09-09 15:08:12 +00002154
Ted Kremenek6bd78702009-04-29 18:50:19 +00002155 virtual ~CFRefReport() {}
Mike Stump11289f42009-09-09 15:08:12 +00002156
Ted Kremenek6bd78702009-04-29 18:50:19 +00002157 CFRefBug& getBugType() {
2158 return (CFRefBug&) RangedBugReport::getBugType();
2159 }
2160 const CFRefBug& getBugType() const {
2161 return (const CFRefBug&) RangedBugReport::getBugType();
2162 }
Mike Stump11289f42009-09-09 15:08:12 +00002163
Zhongxing Xu7864b9ea2009-08-18 08:58:41 +00002164 virtual void getRanges(const SourceRange*& beg, const SourceRange*& end) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002165 if (!getBugType().isLeak())
Zhongxing Xu7864b9ea2009-08-18 08:58:41 +00002166 RangedBugReport::getRanges(beg, end);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002167 else
2168 beg = end = 0;
2169 }
Mike Stump11289f42009-09-09 15:08:12 +00002170
Ted Kremenek6bd78702009-04-29 18:50:19 +00002171 SymbolRef getSymbol() const { return Sym; }
Mike Stump11289f42009-09-09 15:08:12 +00002172
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002173 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002174 const ExplodedNode* N);
Mike Stump11289f42009-09-09 15:08:12 +00002175
Ted Kremenek6bd78702009-04-29 18:50:19 +00002176 std::pair<const char**,const char**> getExtraDescriptiveText();
Mike Stump11289f42009-09-09 15:08:12 +00002177
Zhongxing Xu20227f72009-08-06 01:32:16 +00002178 PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
2179 const ExplodedNode* PrevN,
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002180 BugReporterContext& BRC);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002181 };
Ted Kremenek3978f792009-05-10 05:11:21 +00002182
Ted Kremenek6bd78702009-04-29 18:50:19 +00002183 class VISIBILITY_HIDDEN CFRefLeakReport : public CFRefReport {
2184 SourceLocation AllocSite;
2185 const MemRegion* AllocBinding;
2186 public:
2187 CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002188 ExplodedNode *n, SymbolRef sym,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002189 GRExprEngine& Eng);
Mike Stump11289f42009-09-09 15:08:12 +00002190
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002191 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002192 const ExplodedNode* N);
Mike Stump11289f42009-09-09 15:08:12 +00002193
Ted Kremenek6bd78702009-04-29 18:50:19 +00002194 SourceLocation getLocation() const { return AllocSite; }
Mike Stump11289f42009-09-09 15:08:12 +00002195 };
Ted Kremenek6bd78702009-04-29 18:50:19 +00002196} // end anonymous namespace
2197
2198void CFRefCount::RegisterChecks(BugReporter& BR) {
2199 useAfterRelease = new UseAfterRelease(this);
2200 BR.Register(useAfterRelease);
Mike Stump11289f42009-09-09 15:08:12 +00002201
Ted Kremenek6bd78702009-04-29 18:50:19 +00002202 releaseNotOwned = new BadRelease(this);
2203 BR.Register(releaseNotOwned);
Mike Stump11289f42009-09-09 15:08:12 +00002204
Ted Kremenek6bd78702009-04-29 18:50:19 +00002205 deallocGC = new DeallocGC(this);
2206 BR.Register(deallocGC);
Mike Stump11289f42009-09-09 15:08:12 +00002207
Ted Kremenek6bd78702009-04-29 18:50:19 +00002208 deallocNotOwned = new DeallocNotOwned(this);
2209 BR.Register(deallocNotOwned);
Mike Stump11289f42009-09-09 15:08:12 +00002210
Ted Kremenekd35272f2009-05-09 00:10:05 +00002211 overAutorelease = new OverAutorelease(this);
2212 BR.Register(overAutorelease);
Mike Stump11289f42009-09-09 15:08:12 +00002213
Ted Kremenekdee56e32009-05-10 06:25:57 +00002214 returnNotOwnedForOwned = new ReturnedNotOwnedForOwned(this);
2215 BR.Register(returnNotOwnedForOwned);
Mike Stump11289f42009-09-09 15:08:12 +00002216
Ted Kremenek6bd78702009-04-29 18:50:19 +00002217 // First register "return" leaks.
2218 const char* name = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002219
Ted Kremenek6bd78702009-04-29 18:50:19 +00002220 if (isGCEnabled())
2221 name = "Leak of returned object when using garbage collection";
2222 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
2223 name = "Leak of returned object when not using garbage collection (GC) in "
2224 "dual GC/non-GC code";
2225 else {
2226 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
2227 name = "Leak of returned object";
2228 }
Mike Stump11289f42009-09-09 15:08:12 +00002229
Ted Kremenek41129692009-09-14 22:01:32 +00002230 // Leaks should not be reported if they are post-dominated by a sink.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002231 leakAtReturn = new LeakAtReturn(this, name);
Ted Kremenek41129692009-09-14 22:01:32 +00002232 leakAtReturn->setSuppressOnSink(true);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002233 BR.Register(leakAtReturn);
Mike Stump11289f42009-09-09 15:08:12 +00002234
Ted Kremenek6bd78702009-04-29 18:50:19 +00002235 // Second, register leaks within a function/method.
2236 if (isGCEnabled())
Mike Stump11289f42009-09-09 15:08:12 +00002237 name = "Leak of object when using garbage collection";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002238 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
2239 name = "Leak of object when not using garbage collection (GC) in "
2240 "dual GC/non-GC code";
2241 else {
2242 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
2243 name = "Leak";
2244 }
Mike Stump11289f42009-09-09 15:08:12 +00002245
Ted Kremenek41129692009-09-14 22:01:32 +00002246 // Leaks should not be reported if they are post-dominated by sinks.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002247 leakWithinFunction = new LeakWithinFunction(this, name);
Ted Kremenek41129692009-09-14 22:01:32 +00002248 leakWithinFunction->setSuppressOnSink(true);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002249 BR.Register(leakWithinFunction);
Mike Stump11289f42009-09-09 15:08:12 +00002250
Ted Kremenek6bd78702009-04-29 18:50:19 +00002251 // Save the reference to the BugReporter.
2252 this->BR = &BR;
2253}
2254
2255static const char* Msgs[] = {
2256 // GC only
Mike Stump11289f42009-09-09 15:08:12 +00002257 "Code is compiled to only use garbage collection",
Ted Kremenek6bd78702009-04-29 18:50:19 +00002258 // No GC.
2259 "Code is compiled to use reference counts",
2260 // Hybrid, with GC.
2261 "Code is compiled to use either garbage collection (GC) or reference counts"
Mike Stump11289f42009-09-09 15:08:12 +00002262 " (non-GC). The bug occurs with GC enabled",
Ted Kremenek6bd78702009-04-29 18:50:19 +00002263 // Hybrid, without GC
2264 "Code is compiled to use either garbage collection (GC) or reference counts"
2265 " (non-GC). The bug occurs in non-GC mode"
2266};
2267
2268std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2269 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
Mike Stump11289f42009-09-09 15:08:12 +00002270
Ted Kremenek6bd78702009-04-29 18:50:19 +00002271 switch (TF.getLangOptions().getGCMode()) {
2272 default:
2273 assert(false);
Mike Stump11289f42009-09-09 15:08:12 +00002274
Ted Kremenek6bd78702009-04-29 18:50:19 +00002275 case LangOptions::GCOnly:
2276 assert (TF.isGCEnabled());
Mike Stump11289f42009-09-09 15:08:12 +00002277 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2278
Ted Kremenek6bd78702009-04-29 18:50:19 +00002279 case LangOptions::NonGC:
2280 assert (!TF.isGCEnabled());
2281 return std::make_pair(&Msgs[1], &Msgs[1]+1);
Mike Stump11289f42009-09-09 15:08:12 +00002282
Ted Kremenek6bd78702009-04-29 18:50:19 +00002283 case LangOptions::HybridGC:
2284 if (TF.isGCEnabled())
2285 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2286 else
2287 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2288 }
2289}
2290
2291static inline bool contains(const llvm::SmallVectorImpl<ArgEffect>& V,
2292 ArgEffect X) {
2293 for (llvm::SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end();
2294 I!=E; ++I)
2295 if (*I == X) return true;
Mike Stump11289f42009-09-09 15:08:12 +00002296
Ted Kremenek6bd78702009-04-29 18:50:19 +00002297 return false;
2298}
2299
Zhongxing Xu20227f72009-08-06 01:32:16 +00002300PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode* N,
2301 const ExplodedNode* PrevN,
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002302 BugReporterContext& BRC) {
Mike Stump11289f42009-09-09 15:08:12 +00002303
Ted Kremenek051a03d2009-05-13 07:12:33 +00002304 if (!isa<PostStmt>(N->getLocation()))
2305 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002306
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002307 // Check if the type state has changed.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002308 const GRState *PrevSt = PrevN->getState();
2309 const GRState *CurrSt = N->getState();
Mike Stump11289f42009-09-09 15:08:12 +00002310
2311 const RefVal* CurrT = CurrSt->get<RefBindings>(Sym);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002312 if (!CurrT) return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002313
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002314 const RefVal &CurrV = *CurrT;
2315 const RefVal *PrevT = PrevSt->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002316
Ted Kremenek6bd78702009-04-29 18:50:19 +00002317 // Create a string buffer to constain all the useful things we want
2318 // to tell the user.
2319 std::string sbuf;
2320 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +00002321
Ted Kremenek6bd78702009-04-29 18:50:19 +00002322 // This is the allocation site since the previous node had no bindings
2323 // for this symbol.
2324 if (!PrevT) {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002325 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002326
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002327 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002328 // Get the name of the callee (if it is available).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002329 SVal X = CurrSt->getSValAsScalarOrLoc(CE->getCallee());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002330 if (const FunctionDecl* FD = X.getAsFunctionDecl())
2331 os << "Call to function '" << FD->getNameAsString() <<'\'';
2332 else
Mike Stump11289f42009-09-09 15:08:12 +00002333 os << "function call";
2334 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002335 else {
2336 assert (isa<ObjCMessageExpr>(S));
2337 os << "Method";
2338 }
Mike Stump11289f42009-09-09 15:08:12 +00002339
Ted Kremenek6bd78702009-04-29 18:50:19 +00002340 if (CurrV.getObjKind() == RetEffect::CF) {
2341 os << " returns a Core Foundation object with a ";
2342 }
2343 else {
2344 assert (CurrV.getObjKind() == RetEffect::ObjC);
2345 os << " returns an Objective-C object with a ";
2346 }
Mike Stump11289f42009-09-09 15:08:12 +00002347
Ted Kremenek6bd78702009-04-29 18:50:19 +00002348 if (CurrV.isOwned()) {
2349 os << "+1 retain count (owning reference).";
Mike Stump11289f42009-09-09 15:08:12 +00002350
Ted Kremenek6bd78702009-04-29 18:50:19 +00002351 if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) {
2352 assert(CurrV.getObjKind() == RetEffect::CF);
2353 os << " "
2354 "Core Foundation objects are not automatically garbage collected.";
2355 }
2356 }
2357 else {
2358 assert (CurrV.isNotOwned());
2359 os << "+0 retain count (non-owning reference).";
2360 }
Mike Stump11289f42009-09-09 15:08:12 +00002361
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002362 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002363 return new PathDiagnosticEventPiece(Pos, os.str());
2364 }
Mike Stump11289f42009-09-09 15:08:12 +00002365
Ted Kremenek6bd78702009-04-29 18:50:19 +00002366 // Gather up the effects that were performed on the object at this
2367 // program point
2368 llvm::SmallVector<ArgEffect, 2> AEffects;
Mike Stump11289f42009-09-09 15:08:12 +00002369
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002370 if (const RetainSummary *Summ =
2371 TF.getSummaryOfNode(BRC.getNodeResolver().getOriginalNode(N))) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002372 // We only have summaries attached to nodes after evaluating CallExpr and
2373 // ObjCMessageExprs.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002374 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002375
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002376 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002377 // Iterate through the parameter expressions and see if the symbol
2378 // was ever passed as an argument.
2379 unsigned i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002380
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002381 for (CallExpr::const_arg_iterator AI=CE->arg_begin(), AE=CE->arg_end();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002382 AI!=AE; ++AI, ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002383
Ted Kremenek6bd78702009-04-29 18:50:19 +00002384 // Retrieve the value of the argument. Is it the symbol
2385 // we are interested in?
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002386 if (CurrSt->getSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym)
Ted Kremenek6bd78702009-04-29 18:50:19 +00002387 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002388
Ted Kremenek6bd78702009-04-29 18:50:19 +00002389 // We have an argument. Get the effect!
2390 AEffects.push_back(Summ->getArg(i));
2391 }
2392 }
Mike Stump11289f42009-09-09 15:08:12 +00002393 else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002394 if (const Expr *receiver = ME->getReceiver())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002395 if (CurrSt->getSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002396 // The symbol we are tracking is the receiver.
2397 AEffects.push_back(Summ->getReceiverEffect());
2398 }
2399 }
2400 }
Mike Stump11289f42009-09-09 15:08:12 +00002401
Ted Kremenek6bd78702009-04-29 18:50:19 +00002402 do {
2403 // Get the previous type state.
2404 RefVal PrevV = *PrevT;
Mike Stump11289f42009-09-09 15:08:12 +00002405
Ted Kremenek6bd78702009-04-29 18:50:19 +00002406 // Specially handle -dealloc.
2407 if (!TF.isGCEnabled() && contains(AEffects, Dealloc)) {
2408 // Determine if the object's reference count was pushed to zero.
2409 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
2410 // We may not have transitioned to 'release' if we hit an error.
2411 // This case is handled elsewhere.
2412 if (CurrV.getKind() == RefVal::Released) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002413 assert(CurrV.getCombinedCounts() == 0);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002414 os << "Object released by directly sending the '-dealloc' message";
2415 break;
2416 }
2417 }
Mike Stump11289f42009-09-09 15:08:12 +00002418
Ted Kremenek6bd78702009-04-29 18:50:19 +00002419 // Specially handle CFMakeCollectable and friends.
2420 if (contains(AEffects, MakeCollectable)) {
2421 // Get the name of the function.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002422 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002423 SVal X = CurrSt->getSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002424 const FunctionDecl* FD = X.getAsFunctionDecl();
2425 const std::string& FName = FD->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +00002426
Ted Kremenek6bd78702009-04-29 18:50:19 +00002427 if (TF.isGCEnabled()) {
2428 // Determine if the object's reference count was pushed to zero.
2429 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
Mike Stump11289f42009-09-09 15:08:12 +00002430
Ted Kremenek6bd78702009-04-29 18:50:19 +00002431 os << "In GC mode a call to '" << FName
2432 << "' decrements an object's retain count and registers the "
2433 "object with the garbage collector. ";
Mike Stump11289f42009-09-09 15:08:12 +00002434
Ted Kremenek6bd78702009-04-29 18:50:19 +00002435 if (CurrV.getKind() == RefVal::Released) {
2436 assert(CurrV.getCount() == 0);
2437 os << "Since it now has a 0 retain count the object can be "
2438 "automatically collected by the garbage collector.";
2439 }
2440 else
2441 os << "An object must have a 0 retain count to be garbage collected. "
2442 "After this call its retain count is +" << CurrV.getCount()
2443 << '.';
2444 }
Mike Stump11289f42009-09-09 15:08:12 +00002445 else
Ted Kremenek6bd78702009-04-29 18:50:19 +00002446 os << "When GC is not enabled a call to '" << FName
2447 << "' has no effect on its argument.";
Mike Stump11289f42009-09-09 15:08:12 +00002448
Ted Kremenek6bd78702009-04-29 18:50:19 +00002449 // Nothing more to say.
2450 break;
2451 }
Mike Stump11289f42009-09-09 15:08:12 +00002452
2453 // Determine if the typestate has changed.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002454 if (!(PrevV == CurrV))
2455 switch (CurrV.getKind()) {
2456 case RefVal::Owned:
2457 case RefVal::NotOwned:
Mike Stump11289f42009-09-09 15:08:12 +00002458
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002459 if (PrevV.getCount() == CurrV.getCount()) {
2460 // Did an autorelease message get sent?
2461 if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount())
2462 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002463
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00002464 assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount());
Ted Kremenek3978f792009-05-10 05:11:21 +00002465 os << "Object sent -autorelease message";
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002466 break;
2467 }
Mike Stump11289f42009-09-09 15:08:12 +00002468
Ted Kremenek6bd78702009-04-29 18:50:19 +00002469 if (PrevV.getCount() > CurrV.getCount())
2470 os << "Reference count decremented.";
2471 else
2472 os << "Reference count incremented.";
Mike Stump11289f42009-09-09 15:08:12 +00002473
Ted Kremenek6bd78702009-04-29 18:50:19 +00002474 if (unsigned Count = CurrV.getCount())
2475 os << " The object now has a +" << Count << " retain count.";
Mike Stump11289f42009-09-09 15:08:12 +00002476
Ted Kremenek6bd78702009-04-29 18:50:19 +00002477 if (PrevV.getKind() == RefVal::Released) {
2478 assert(TF.isGCEnabled() && CurrV.getCount() > 0);
2479 os << " The object is not eligible for garbage collection until the "
2480 "retain count reaches 0 again.";
2481 }
Mike Stump11289f42009-09-09 15:08:12 +00002482
Ted Kremenek6bd78702009-04-29 18:50:19 +00002483 break;
Mike Stump11289f42009-09-09 15:08:12 +00002484
Ted Kremenek6bd78702009-04-29 18:50:19 +00002485 case RefVal::Released:
2486 os << "Object released.";
2487 break;
Mike Stump11289f42009-09-09 15:08:12 +00002488
Ted Kremenek6bd78702009-04-29 18:50:19 +00002489 case RefVal::ReturnedOwned:
2490 os << "Object returned to caller as an owning reference (single retain "
2491 "count transferred to caller).";
2492 break;
Mike Stump11289f42009-09-09 15:08:12 +00002493
Ted Kremenek6bd78702009-04-29 18:50:19 +00002494 case RefVal::ReturnedNotOwned:
2495 os << "Object returned to caller with a +0 (non-owning) retain count.";
2496 break;
Mike Stump11289f42009-09-09 15:08:12 +00002497
Ted Kremenek6bd78702009-04-29 18:50:19 +00002498 default:
2499 return NULL;
2500 }
Mike Stump11289f42009-09-09 15:08:12 +00002501
Ted Kremenek6bd78702009-04-29 18:50:19 +00002502 // Emit any remaining diagnostics for the argument effects (if any).
2503 for (llvm::SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(),
2504 E=AEffects.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002505
Ted Kremenek6bd78702009-04-29 18:50:19 +00002506 // A bunch of things have alternate behavior under GC.
2507 if (TF.isGCEnabled())
2508 switch (*I) {
2509 default: break;
2510 case Autorelease:
2511 os << "In GC mode an 'autorelease' has no effect.";
2512 continue;
2513 case IncRefMsg:
2514 os << "In GC mode the 'retain' message has no effect.";
2515 continue;
2516 case DecRefMsg:
2517 os << "In GC mode the 'release' message has no effect.";
2518 continue;
2519 }
2520 }
Mike Stump11289f42009-09-09 15:08:12 +00002521 } while (0);
2522
Ted Kremenek6bd78702009-04-29 18:50:19 +00002523 if (os.str().empty())
2524 return 0; // We have nothing to say!
Ted Kremenek051a03d2009-05-13 07:12:33 +00002525
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002526 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002527 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002528 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(Pos, os.str());
Mike Stump11289f42009-09-09 15:08:12 +00002529
Ted Kremenek6bd78702009-04-29 18:50:19 +00002530 // Add the range by scanning the children of the statement for any bindings
2531 // to Sym.
Mike Stump11289f42009-09-09 15:08:12 +00002532 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002533 I!=E; ++I)
2534 if (const Expr* Exp = dyn_cast_or_null<Expr>(*I))
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002535 if (CurrSt->getSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002536 P->addRange(Exp->getSourceRange());
2537 break;
2538 }
Mike Stump11289f42009-09-09 15:08:12 +00002539
Ted Kremenek6bd78702009-04-29 18:50:19 +00002540 return P;
2541}
2542
2543namespace {
2544 class VISIBILITY_HIDDEN FindUniqueBinding :
2545 public StoreManager::BindingsHandler {
2546 SymbolRef Sym;
2547 const MemRegion* Binding;
2548 bool First;
Mike Stump11289f42009-09-09 15:08:12 +00002549
Ted Kremenek6bd78702009-04-29 18:50:19 +00002550 public:
2551 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
Mike Stump11289f42009-09-09 15:08:12 +00002552
Ted Kremenek6bd78702009-04-29 18:50:19 +00002553 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
2554 SVal val) {
Mike Stump11289f42009-09-09 15:08:12 +00002555
2556 SymbolRef SymV = val.getAsSymbol();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002557 if (!SymV || SymV != Sym)
2558 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002559
Ted Kremenek6bd78702009-04-29 18:50:19 +00002560 if (Binding) {
2561 First = false;
2562 return false;
2563 }
2564 else
2565 Binding = R;
Mike Stump11289f42009-09-09 15:08:12 +00002566
2567 return true;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002568 }
Mike Stump11289f42009-09-09 15:08:12 +00002569
Ted Kremenek6bd78702009-04-29 18:50:19 +00002570 operator bool() { return First && Binding; }
2571 const MemRegion* getRegion() { return Binding; }
Mike Stump11289f42009-09-09 15:08:12 +00002572 };
Ted Kremenek6bd78702009-04-29 18:50:19 +00002573}
2574
Zhongxing Xu20227f72009-08-06 01:32:16 +00002575static std::pair<const ExplodedNode*,const MemRegion*>
2576GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode* N,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002577 SymbolRef Sym) {
Mike Stump11289f42009-09-09 15:08:12 +00002578
Ted Kremenek6bd78702009-04-29 18:50:19 +00002579 // Find both first node that referred to the tracked symbol and the
2580 // memory location that value was store to.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002581 const ExplodedNode* Last = N;
Mike Stump11289f42009-09-09 15:08:12 +00002582 const MemRegion* FirstBinding = 0;
2583
Ted Kremenek6bd78702009-04-29 18:50:19 +00002584 while (N) {
2585 const GRState* St = N->getState();
2586 RefBindings B = St->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00002587
Ted Kremenek6bd78702009-04-29 18:50:19 +00002588 if (!B.lookup(Sym))
2589 break;
Mike Stump11289f42009-09-09 15:08:12 +00002590
Ted Kremenek6bd78702009-04-29 18:50:19 +00002591 FindUniqueBinding FB(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002592 StateMgr.iterBindings(St, FB);
2593 if (FB) FirstBinding = FB.getRegion();
2594
Ted Kremenek6bd78702009-04-29 18:50:19 +00002595 Last = N;
Mike Stump11289f42009-09-09 15:08:12 +00002596 N = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002597 }
Mike Stump11289f42009-09-09 15:08:12 +00002598
Ted Kremenek6bd78702009-04-29 18:50:19 +00002599 return std::make_pair(Last, FirstBinding);
2600}
2601
2602PathDiagnosticPiece*
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002603CFRefReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002604 const ExplodedNode* EndN) {
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002605 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenek6bd78702009-04-29 18:50:19 +00002606 // assigned to different variables, etc.
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002607 BRC.addNotableSymbol(Sym);
2608 return RangedBugReport::getEndPath(BRC, EndN);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002609}
2610
2611PathDiagnosticPiece*
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002612CFRefLeakReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002613 const ExplodedNode* EndN){
Mike Stump11289f42009-09-09 15:08:12 +00002614
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002615 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenek6bd78702009-04-29 18:50:19 +00002616 // assigned to different variables, etc.
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002617 BRC.addNotableSymbol(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002618
Ted Kremenek6bd78702009-04-29 18:50:19 +00002619 // We are reporting a leak. Walk up the graph to get to the first node where
2620 // the symbol appeared, and also get the first VarDecl that tracked object
2621 // is stored to.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002622 const ExplodedNode* AllocNode = 0;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002623 const MemRegion* FirstBinding = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002624
Ted Kremenek6bd78702009-04-29 18:50:19 +00002625 llvm::tie(AllocNode, FirstBinding) =
Ted Kremenek8c8fb482009-05-08 23:32:51 +00002626 GetAllocationSite(BRC.getStateManager(), EndN, Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002627
2628 // Get the allocate site.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002629 assert(AllocNode);
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002630 const Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002631
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002632 SourceManager& SMgr = BRC.getSourceManager();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002633 unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart());
Mike Stump11289f42009-09-09 15:08:12 +00002634
Ted Kremenek6bd78702009-04-29 18:50:19 +00002635 // Compute an actual location for the leak. Sometimes a leak doesn't
2636 // occur at an actual statement (e.g., transition between blocks; end
2637 // of function) so we need to walk the graph and compute a real location.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002638 const ExplodedNode* LeakN = EndN;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002639 PathDiagnosticLocation L;
Mike Stump11289f42009-09-09 15:08:12 +00002640
Ted Kremenek6bd78702009-04-29 18:50:19 +00002641 while (LeakN) {
2642 ProgramPoint P = LeakN->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002643
Ted Kremenek6bd78702009-04-29 18:50:19 +00002644 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
2645 L = PathDiagnosticLocation(PS->getStmt()->getLocStart(), SMgr);
2646 break;
2647 }
2648 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
2649 if (const Stmt* Term = BE->getSrc()->getTerminator()) {
2650 L = PathDiagnosticLocation(Term->getLocStart(), SMgr);
2651 break;
2652 }
2653 }
Mike Stump11289f42009-09-09 15:08:12 +00002654
Ted Kremenek6bd78702009-04-29 18:50:19 +00002655 LeakN = LeakN->succ_empty() ? 0 : *(LeakN->succ_begin());
2656 }
Mike Stump11289f42009-09-09 15:08:12 +00002657
Ted Kremenek6bd78702009-04-29 18:50:19 +00002658 if (!L.isValid()) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002659 const Decl &D = EndN->getCodeDecl();
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002660 L = PathDiagnosticLocation(D.getBodyRBrace(), SMgr);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002661 }
Mike Stump11289f42009-09-09 15:08:12 +00002662
Ted Kremenek6bd78702009-04-29 18:50:19 +00002663 std::string sbuf;
2664 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +00002665
Ted Kremenek6bd78702009-04-29 18:50:19 +00002666 os << "Object allocated on line " << AllocLine;
Mike Stump11289f42009-09-09 15:08:12 +00002667
Ted Kremenek6bd78702009-04-29 18:50:19 +00002668 if (FirstBinding)
Mike Stump11289f42009-09-09 15:08:12 +00002669 os << " and stored into '" << FirstBinding->getString() << '\'';
2670
Ted Kremenek6bd78702009-04-29 18:50:19 +00002671 // Get the retain count.
2672 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002673
Ted Kremenek6bd78702009-04-29 18:50:19 +00002674 if (RV->getKind() == RefVal::ErrorLeakReturned) {
2675 // FIXME: Per comments in rdar://6320065, "create" only applies to CF
2676 // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership
2677 // to the caller for NS objects.
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002678 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002679 os << " is returned from a method whose name ('"
Ted Kremenek223a7d52009-04-29 23:03:22 +00002680 << MD.getSelector().getAsString()
Ted Kremenek6bd78702009-04-29 18:50:19 +00002681 << "') does not contain 'copy' or otherwise starts with"
2682 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenekd6bef2e2009-04-29 22:25:52 +00002683 " in the Memory Management Guide for Cocoa (object leaked)";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002684 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00002685 else if (RV->getKind() == RefVal::ErrorGCLeakReturned) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002686 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremenekdee56e32009-05-10 06:25:57 +00002687 os << " and returned from method '" << MD.getSelector().getAsString()
Ted Kremenek1f8e4342009-05-10 16:52:15 +00002688 << "' is potentially leaked when using garbage collection. Callers "
2689 "of this method do not expect a returned object with a +1 retain "
2690 "count since they expect the object to be managed by the garbage "
2691 "collector";
Ted Kremenekdee56e32009-05-10 06:25:57 +00002692 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002693 else
2694 os << " is no longer referenced after this point and has a retain count of"
Ted Kremenekd6bef2e2009-04-29 22:25:52 +00002695 " +" << RV->getCount() << " (object leaked)";
Mike Stump11289f42009-09-09 15:08:12 +00002696
Ted Kremenek6bd78702009-04-29 18:50:19 +00002697 return new PathDiagnosticEventPiece(L, os.str());
2698}
2699
Ted Kremenek6bd78702009-04-29 18:50:19 +00002700CFRefLeakReport::CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002701 ExplodedNode *n,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002702 SymbolRef sym, GRExprEngine& Eng)
Mike Stump11289f42009-09-09 15:08:12 +00002703: CFRefReport(D, tf, n, sym) {
2704
Ted Kremenek6bd78702009-04-29 18:50:19 +00002705 // Most bug reports are cached at the location where they occured.
2706 // With leaks, we want to unique them by the location where they were
2707 // allocated, and only report a single path. To do this, we need to find
2708 // the allocation site of a piece of tracked memory, which we do via a
2709 // call to GetAllocationSite. This will walk the ExplodedGraph backwards.
2710 // Note that this is *not* the trimmed graph; we are guaranteed, however,
2711 // that all ancestor nodes that represent the allocation site have the
2712 // same SourceLocation.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002713 const ExplodedNode* AllocNode = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002714
Ted Kremenek6bd78702009-04-29 18:50:19 +00002715 llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding.
Ted Kremenek8c8fb482009-05-08 23:32:51 +00002716 GetAllocationSite(Eng.getStateManager(), getEndNode(), getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00002717
Ted Kremenek6bd78702009-04-29 18:50:19 +00002718 // Get the SourceLocation for the allocation site.
2719 ProgramPoint P = AllocNode->getLocation();
2720 AllocSite = cast<PostStmt>(P).getStmt()->getLocStart();
Mike Stump11289f42009-09-09 15:08:12 +00002721
Ted Kremenek6bd78702009-04-29 18:50:19 +00002722 // Fill in the description of the bug.
2723 Description.clear();
2724 llvm::raw_string_ostream os(Description);
2725 SourceManager& SMgr = Eng.getContext().getSourceManager();
2726 unsigned AllocLine = SMgr.getInstantiationLineNumber(AllocSite);
Ted Kremenekf1e76672009-05-02 19:05:19 +00002727 os << "Potential leak ";
2728 if (tf.isGCEnabled()) {
2729 os << "(when using garbage collection) ";
Mike Stump11289f42009-09-09 15:08:12 +00002730 }
Ted Kremenekf1e76672009-05-02 19:05:19 +00002731 os << "of an object allocated on line " << AllocLine;
Mike Stump11289f42009-09-09 15:08:12 +00002732
Ted Kremenek6bd78702009-04-29 18:50:19 +00002733 // FIXME: AllocBinding doesn't get populated for RegionStore yet.
2734 if (AllocBinding)
2735 os << " and stored into '" << AllocBinding->getString() << '\'';
2736}
2737
2738//===----------------------------------------------------------------------===//
2739// Main checker logic.
2740//===----------------------------------------------------------------------===//
2741
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002742/// GetReturnType - Used to get the return type of a message expression or
2743/// function call with the intention of affixing that type to a tracked symbol.
2744/// While the the return type can be queried directly from RetEx, when
2745/// invoking class methods we augment to the return type to be that of
2746/// a pointer to the class (as opposed it just being id).
Steve Naroff7cae42b2009-07-10 23:34:53 +00002747static QualType GetReturnType(const Expr* RetE, ASTContext& Ctx) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002748 QualType RetTy = RetE->getType();
Steve Naroff7cae42b2009-07-10 23:34:53 +00002749 // If RetE is not a message expression just return its type.
2750 // If RetE is a message expression, return its types if it is something
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002751 /// more specific than id.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002752 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
John McCall9dd450b2009-09-21 23:43:11 +00002753 if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
Mike Stump11289f42009-09-09 15:08:12 +00002754 if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
Steve Naroff7cae42b2009-07-10 23:34:53 +00002755 PT->isObjCClassType()) {
2756 // At this point we know the return type of the message expression is
2757 // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
2758 // is a call to a class method whose type we can resolve. In such
2759 // cases, promote the return type to XXX* (where XXX is the class).
Mike Stump11289f42009-09-09 15:08:12 +00002760 const ObjCInterfaceDecl *D = ME->getClassInfo().first;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002761 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
2762 }
Mike Stump11289f42009-09-09 15:08:12 +00002763
Steve Naroff7cae42b2009-07-10 23:34:53 +00002764 return RetTy;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002765}
2766
Zhongxing Xu20227f72009-08-06 01:32:16 +00002767void CFRefCount::EvalSummary(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002768 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002769 GRStmtNodeBuilder& Builder,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002770 Expr* Ex,
2771 Expr* Receiver,
Ted Kremenekff606a12009-05-04 04:57:00 +00002772 const RetainSummary& Summ,
Zhongxing Xuac129432009-04-20 05:24:46 +00002773 ExprIterator arg_beg, ExprIterator arg_end,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002774 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00002775
Ted Kremenek819e9b62008-03-11 06:39:11 +00002776 // Get the state.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002777 const GRState *state = Builder.GetState(Pred);
Ted Kremenek821537e2008-05-06 02:41:27 +00002778
2779 // Evaluate the effect of the arguments.
Ted Kremenek8cb96e92008-04-16 04:28:53 +00002780 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek68d73d12008-03-12 01:21:45 +00002781 unsigned idx = 0;
Ted Kremenek988990f2008-04-11 18:40:51 +00002782 Expr* ErrorExpr = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002783 SymbolRef ErrorSym = 0;
2784
2785 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
2786 SVal V = state->getSValAsScalarOrLoc(*I);
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002787 SymbolRef Sym = V.getAsLocSymbol();
Ted Kremenek804fc232009-03-04 00:13:50 +00002788
Ted Kremenek3e31c262009-03-26 03:35:11 +00002789 if (Sym)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002790 if (RefBindings::data_type* T = state->get<RefBindings>(Sym)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002791 state = Update(state, Sym, *T, Summ.getArg(idx), hasErr);
Ted Kremenekc52f9392009-02-24 19:15:11 +00002792 if (hasErr) {
Ted Kremenek988990f2008-04-11 18:40:51 +00002793 ErrorExpr = *I;
Ted Kremenek4963d112008-07-07 16:21:19 +00002794 ErrorSym = Sym;
Ted Kremenek988990f2008-04-11 18:40:51 +00002795 break;
Mike Stump11289f42009-09-09 15:08:12 +00002796 }
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002797 continue;
Ted Kremenekc52f9392009-02-24 19:15:11 +00002798 }
Ted Kremenekae529272008-07-09 18:11:16 +00002799
Ted Kremenekf9539d02009-09-22 04:48:39 +00002800 tryAgain:
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002801 if (isa<Loc>(V)) {
2802 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002803 if (Summ.getArg(idx) == DoNothingByRef)
Ted Kremenekae529272008-07-09 18:11:16 +00002804 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002805
2806 // Invalidate the value of the variable passed by reference.
2807
Ted Kremenek4d851462008-07-03 23:26:32 +00002808 // FIXME: We can have collisions on the conjured symbol if the
2809 // expression *I also creates conjured symbols. We probably want
2810 // to identify conjured symbols by an expression pair: the enclosing
2811 // expression (the context) and the expression itself. This should
Mike Stump11289f42009-09-09 15:08:12 +00002812 // disambiguate conjured symbols.
Zhongxing Xu4744d562009-06-29 06:43:40 +00002813 unsigned Count = Builder.getCurrentBlockCount();
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002814 StoreManager& StoreMgr = Eng.getStateManager().getStoreManager();
Ted Kremenek97f75f82009-05-11 22:55:17 +00002815
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002816 const MemRegion *R = MR->getRegion();
2817 // Are we dealing with an ElementRegion? If the element type is
2818 // a basic integer type (e.g., char, int) and the underying region
2819 // is a variable region then strip off the ElementRegion.
2820 // FIXME: We really need to think about this for the general case
2821 // as sometimes we are reasoning about arrays and other times
2822 // about (char*), etc., is just a form of passing raw bytes.
2823 // e.g., void *p = alloca(); foo((char*)p);
2824 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
2825 // Checking for 'integral type' is probably too promiscuous, but
2826 // we'll leave it in for now until we have a systematic way of
2827 // handling all of these cases. Eventually we need to come up
2828 // with an interface to StoreManager so that this logic can be
2829 // approriately delegated to the respective StoreManagers while
2830 // still allowing us to do checker-specific logic (e.g.,
2831 // invalidating reference counts), probably via callbacks.
2832 if (ER->getElementType()->isIntegralType()) {
2833 const MemRegion *superReg = ER->getSuperRegion();
2834 if (isa<VarRegion>(superReg) || isa<FieldRegion>(superReg) ||
2835 isa<ObjCIvarRegion>(superReg))
2836 R = cast<TypedRegion>(superReg);
Ted Kremenek0626df42009-05-06 18:19:24 +00002837 }
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002838 // FIXME: What about layers of ElementRegions?
2839 }
Zhongxing Xu4744d562009-06-29 06:43:40 +00002840
Ted Kremenek1eb68092009-10-16 00:30:49 +00002841 StoreManager::InvalidatedSymbols IS;
2842 state = StoreMgr.InvalidateRegion(state, R, *I, Count, &IS);
2843 for (StoreManager::InvalidatedSymbols::iterator I = IS.begin(),
2844 E = IS.end(); I!=E; ++I) {
2845 // Remove any existing reference-count binding.
2846 state = state->remove<RefBindings>(*I);
2847 }
Ted Kremenek4d851462008-07-03 23:26:32 +00002848 }
2849 else {
2850 // Nuke all other arguments passed by reference.
Ted Kremenekf9539d02009-09-22 04:48:39 +00002851 // FIXME: is this necessary or correct? This handles the non-Region
2852 // cases. Is it ever valid to store to these?
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002853 state = state->unbindLoc(cast<Loc>(V));
Ted Kremenek4d851462008-07-03 23:26:32 +00002854 }
Ted Kremenek0a86fdb2008-04-11 20:51:02 +00002855 }
Ted Kremenekf9539d02009-09-22 04:48:39 +00002856 else if (isa<nonloc::LocAsInteger>(V)) {
2857 // If we are passing a location wrapped as an integer, unwrap it and
2858 // invalidate the values referred by the location.
2859 V = cast<nonloc::LocAsInteger>(V).getLoc();
2860 goto tryAgain;
2861 }
Mike Stump11289f42009-09-09 15:08:12 +00002862 }
2863
2864 // Evaluate the effect on the message receiver.
Ted Kremenek821537e2008-05-06 02:41:27 +00002865 if (!ErrorExpr && Receiver) {
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002866 SymbolRef Sym = state->getSValAsScalarOrLoc(Receiver).getAsLocSymbol();
Ted Kremenek3e31c262009-03-26 03:35:11 +00002867 if (Sym) {
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002868 if (const RefVal* T = state->get<RefBindings>(Sym)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002869 state = Update(state, Sym, *T, Summ.getReceiverEffect(), hasErr);
Ted Kremenekc52f9392009-02-24 19:15:11 +00002870 if (hasErr) {
Ted Kremenek821537e2008-05-06 02:41:27 +00002871 ErrorExpr = Receiver;
Ted Kremenek4963d112008-07-07 16:21:19 +00002872 ErrorSym = Sym;
Ted Kremenek821537e2008-05-06 02:41:27 +00002873 }
Ted Kremenekc52f9392009-02-24 19:15:11 +00002874 }
Ted Kremenek821537e2008-05-06 02:41:27 +00002875 }
2876 }
Mike Stump11289f42009-09-09 15:08:12 +00002877
2878 // Process any errors.
Ted Kremenek8cb96e92008-04-16 04:28:53 +00002879 if (hasErr) {
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00002880 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek396f4362008-04-18 03:39:05 +00002881 hasErr, ErrorSym);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002882 return;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00002883 }
Mike Stump11289f42009-09-09 15:08:12 +00002884
2885 // Consult the summary for the return value.
Ted Kremenekff606a12009-05-04 04:57:00 +00002886 RetEffect RE = Summ.getRetEffect();
Mike Stump11289f42009-09-09 15:08:12 +00002887
Ted Kremenek1272f702009-05-12 20:06:54 +00002888 if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
2889 assert(Receiver);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002890 SVal V = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenek1272f702009-05-12 20:06:54 +00002891 bool found = false;
2892 if (SymbolRef Sym = V.getAsLocSymbol())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002893 if (state->get<RefBindings>(Sym)) {
Ted Kremenek1272f702009-05-12 20:06:54 +00002894 found = true;
2895 RE = Summaries.getObjAllocRetEffect();
2896 }
2897
2898 if (!found)
2899 RE = RetEffect::MakeNoRet();
Mike Stump11289f42009-09-09 15:08:12 +00002900 }
2901
Ted Kremenek68d73d12008-03-12 01:21:45 +00002902 switch (RE.getKind()) {
2903 default:
2904 assert (false && "Unhandled RetEffect."); break;
Mike Stump11289f42009-09-09 15:08:12 +00002905
2906 case RetEffect::NoRet: {
Ted Kremenek831f3272008-04-11 20:23:24 +00002907 // Make up a symbol for the return value (not reference counted).
Ted Kremenek1642bda2009-06-26 00:05:51 +00002908 // FIXME: Most of this logic is not specific to the retain/release
2909 // checker.
Mike Stump11289f42009-09-09 15:08:12 +00002910
Ted Kremenek21387322008-10-17 22:23:12 +00002911 // FIXME: We eventually should handle structs and other compound types
2912 // that are returned by value.
Mike Stump11289f42009-09-09 15:08:12 +00002913
Ted Kremenek21387322008-10-17 22:23:12 +00002914 QualType T = Ex->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002915
Ted Kremenek16866d62008-11-13 06:10:40 +00002916 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenek831f3272008-04-11 20:23:24 +00002917 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekf2489ea2009-04-09 22:22:44 +00002918 ValueManager &ValMgr = Eng.getValueManager();
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002919 SVal X = ValMgr.getConjuredSymbolVal(NULL, Ex, T, Count);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002920 state = state->BindExpr(Ex, X, false);
Mike Stump11289f42009-09-09 15:08:12 +00002921 }
2922
Ted Kremenek4b772092008-04-10 23:44:06 +00002923 break;
Ted Kremenek21387322008-10-17 22:23:12 +00002924 }
Mike Stump11289f42009-09-09 15:08:12 +00002925
Ted Kremenek68d73d12008-03-12 01:21:45 +00002926 case RetEffect::Alias: {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002927 unsigned idx = RE.getIndex();
Ted Kremenek08e17112008-06-17 02:43:46 +00002928 assert (arg_end >= arg_beg);
Ted Kremenek00daccd2008-05-05 22:11:16 +00002929 assert (idx < (unsigned) (arg_end - arg_beg));
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002930 SVal V = state->getSValAsScalarOrLoc(*(arg_beg+idx));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002931 state = state->BindExpr(Ex, V, false);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002932 break;
2933 }
Mike Stump11289f42009-09-09 15:08:12 +00002934
Ted Kremenek821537e2008-05-06 02:41:27 +00002935 case RetEffect::ReceiverAlias: {
2936 assert (Receiver);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002937 SVal V = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002938 state = state->BindExpr(Ex, V, false);
Ted Kremenek821537e2008-05-06 02:41:27 +00002939 break;
2940 }
Mike Stump11289f42009-09-09 15:08:12 +00002941
Ted Kremenekab4a8b52008-06-23 18:02:52 +00002942 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek68d73d12008-03-12 01:21:45 +00002943 case RetEffect::OwnedSymbol: {
2944 unsigned Count = Builder.getCurrentBlockCount();
Mike Stump11289f42009-09-09 15:08:12 +00002945 ValueManager &ValMgr = Eng.getValueManager();
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002946 SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002947 QualType RetT = GetReturnType(Ex, ValMgr.getContext());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002948 state = state->set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(),
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002949 RetT));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002950 state = state->BindExpr(Ex, ValMgr.makeLoc(Sym), false);
Ted Kremenek0b891a32009-03-09 22:46:49 +00002951
2952 // FIXME: Add a flag to the checker where allocations are assumed to
2953 // *not fail.
2954#if 0
Ted Kremenek2e561dd2009-01-28 22:27:59 +00002955 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) {
2956 bool isFeasible;
2957 state = state.Assume(loc::SymbolVal(Sym), true, isFeasible);
Mike Stump11289f42009-09-09 15:08:12 +00002958 assert(isFeasible && "Cannot assume fresh symbol is non-null.");
Ted Kremenek2e561dd2009-01-28 22:27:59 +00002959 }
Ted Kremenek0b891a32009-03-09 22:46:49 +00002960#endif
Mike Stump11289f42009-09-09 15:08:12 +00002961
Ted Kremenek68d73d12008-03-12 01:21:45 +00002962 break;
2963 }
Mike Stump11289f42009-09-09 15:08:12 +00002964
Ted Kremeneke6633562009-04-27 19:14:45 +00002965 case RetEffect::GCNotOwnedSymbol:
Ted Kremenek68d73d12008-03-12 01:21:45 +00002966 case RetEffect::NotOwnedSymbol: {
2967 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002968 ValueManager &ValMgr = Eng.getValueManager();
2969 SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002970 QualType RetT = GetReturnType(Ex, ValMgr.getContext());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002971 state = state->set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002972 RetT));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002973 state = state->BindExpr(Ex, ValMgr.makeLoc(Sym), false);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002974 break;
2975 }
2976 }
Mike Stump11289f42009-09-09 15:08:12 +00002977
Ted Kremenekd84fff62009-02-18 02:00:25 +00002978 // Generate a sink node if we are at the end of a path.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002979 ExplodedNode *NewNode =
Ted Kremenekff606a12009-05-04 04:57:00 +00002980 Summ.isEndPath() ? Builder.MakeSinkNode(Dst, Ex, Pred, state)
2981 : Builder.MakeNode(Dst, Ex, Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00002982
Ted Kremenekd84fff62009-02-18 02:00:25 +00002983 // Annotate the edge with summary we used.
Ted Kremenekff606a12009-05-04 04:57:00 +00002984 if (NewNode) SummaryLog[NewNode] = &Summ;
Ted Kremenek00daccd2008-05-05 22:11:16 +00002985}
2986
2987
Zhongxing Xu20227f72009-08-06 01:32:16 +00002988void CFRefCount::EvalCall(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002989 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002990 GRStmtNodeBuilder& Builder,
Zhongxing Xu27f17422008-10-17 05:57:07 +00002991 CallExpr* CE, SVal L,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002992 ExplodedNode* Pred) {
Zhongxing Xuac129432009-04-20 05:24:46 +00002993 const FunctionDecl* FD = L.getAsFunctionDecl();
Mike Stump11289f42009-09-09 15:08:12 +00002994 RetainSummary* Summ = !FD ? Summaries.getDefaultSummary()
Zhongxing Xuac129432009-04-20 05:24:46 +00002995 : Summaries.getSummary(const_cast<FunctionDecl*>(FD));
Mike Stump11289f42009-09-09 15:08:12 +00002996
Ted Kremenekff606a12009-05-04 04:57:00 +00002997 assert(Summ);
2998 EvalSummary(Dst, Eng, Builder, CE, 0, *Summ,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002999 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenekea6507f2008-03-06 00:08:09 +00003000}
Ted Kremenek819e9b62008-03-11 06:39:11 +00003001
Zhongxing Xu20227f72009-08-06 01:32:16 +00003002void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003003 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003004 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003005 ObjCMessageExpr* ME,
Mike Stump11289f42009-09-09 15:08:12 +00003006 ExplodedNode* Pred) {
Ted Kremenekff606a12009-05-04 04:57:00 +00003007 RetainSummary* Summ = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003008
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003009 if (Expr* Receiver = ME->getReceiver()) {
3010 // We need the type-information of the tracked receiver object
3011 // Retrieve it from the state.
Ted Kremenek5801f652009-05-13 18:16:01 +00003012 const ObjCInterfaceDecl* ID = 0;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003013
3014 // FIXME: Wouldn't it be great if this code could be reduced? It's just
3015 // a chain of lookups.
Ted Kremenek0b50fb12009-04-29 05:04:30 +00003016 // FIXME: Is this really working as expected? There are cases where
3017 // we just use the 'ID' from the message expression.
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +00003018 const GRState* St = Builder.GetState(Pred);
Ted Kremenek095f1a92009-06-18 23:58:37 +00003019 SVal V = St->getSValAsScalarOrLoc(Receiver);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003020
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003021 SymbolRef Sym = V.getAsLocSymbol();
Mike Stump11289f42009-09-09 15:08:12 +00003022
Ted Kremenek3e31c262009-03-26 03:35:11 +00003023 if (Sym) {
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00003024 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003025 if (const ObjCObjectPointerType* PT =
John McCall9dd450b2009-09-21 23:43:11 +00003026 T->getType()->getAs<ObjCObjectPointerType>())
Steve Naroff7cae42b2009-07-10 23:34:53 +00003027 ID = PT->getInterfaceDecl();
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003028 }
3029 }
Ted Kremenek5801f652009-05-13 18:16:01 +00003030
3031 // FIXME: this is a hack. This may or may not be the actual method
3032 // that is called.
3033 if (!ID) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003034 if (const ObjCObjectPointerType *PT =
John McCall9dd450b2009-09-21 23:43:11 +00003035 Receiver->getType()->getAs<ObjCObjectPointerType>())
Steve Naroff7cae42b2009-07-10 23:34:53 +00003036 ID = PT->getInterfaceDecl();
Ted Kremenek5801f652009-05-13 18:16:01 +00003037 }
3038
Ted Kremenek38724302009-04-29 17:09:14 +00003039 // FIXME: The receiver could be a reference to a class, meaning that
3040 // we should use the class method.
3041 Summ = Summaries.getInstanceMethodSummary(ME, ID);
Ted Kremenek03466c22008-10-24 20:32:50 +00003042
Ted Kremenekcc3d1882008-10-23 01:56:15 +00003043 // Special-case: are we sending a mesage to "self"?
3044 // This is a hack. When we have full-IP this should be removed.
Mike Stump11289f42009-09-09 15:08:12 +00003045 if (isa<ObjCMethodDecl>(Pred->getLocationContext()->getDecl())) {
Ted Kremenek1d9a2672009-05-04 05:31:22 +00003046 if (Expr* Receiver = ME->getReceiver()) {
Ted Kremenek095f1a92009-06-18 23:58:37 +00003047 SVal X = St->getSValAsScalarOrLoc(Receiver);
Mike Stump11289f42009-09-09 15:08:12 +00003048 if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X)) {
Ted Kremenek608677a2009-08-21 23:25:54 +00003049 // Get the region associated with 'self'.
Mike Stump11289f42009-09-09 15:08:12 +00003050 const LocationContext *LC = Pred->getLocationContext();
Ted Kremenek608677a2009-08-21 23:25:54 +00003051 if (const ImplicitParamDecl *SelfDecl = LC->getSelfDecl()) {
Mike Stump11289f42009-09-09 15:08:12 +00003052 SVal SelfVal = St->getSVal(St->getRegion(SelfDecl, LC));
Ted Kremenek608677a2009-08-21 23:25:54 +00003053 if (L->getBaseRegion() == SelfVal.getAsRegion()) {
3054 // Update the summary to make the default argument effect
3055 // 'StopTracking'.
3056 Summ = Summaries.copySummary(Summ);
3057 Summ->setDefaultArgEffect(StopTracking);
3058 }
Mike Stump11289f42009-09-09 15:08:12 +00003059 }
Ted Kremenek608677a2009-08-21 23:25:54 +00003060 }
Ted Kremenekcc3d1882008-10-23 01:56:15 +00003061 }
3062 }
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003063 }
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003064 else
Ted Kremenek0a1f9c42009-04-23 21:25:57 +00003065 Summ = Summaries.getClassMethodSummary(ME);
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003066
Ted Kremenekff606a12009-05-04 04:57:00 +00003067 if (!Summ)
3068 Summ = Summaries.getDefaultSummary();
Ted Kremenek8a5ad392009-04-24 17:50:11 +00003069
Ted Kremenekff606a12009-05-04 04:57:00 +00003070 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), *Summ,
Ted Kremenek015c3562008-05-06 04:20:12 +00003071 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003072}
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003073
3074namespace {
3075class VISIBILITY_HIDDEN StopTrackingCallback : public SymbolVisitor {
Ted Kremenek89a303c2009-06-18 00:49:02 +00003076 const GRState *state;
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003077public:
Ted Kremenek89a303c2009-06-18 00:49:02 +00003078 StopTrackingCallback(const GRState *st) : state(st) {}
3079 const GRState *getState() const { return state; }
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003080
3081 bool VisitSymbol(SymbolRef sym) {
Ted Kremenek89a303c2009-06-18 00:49:02 +00003082 state = state->remove<RefBindings>(sym);
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003083 return true;
3084 }
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003085};
3086} // end anonymous namespace
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003087
Mike Stump11289f42009-09-09 15:08:12 +00003088
3089void CFRefCount::EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val) {
3090 // Are we storing to something that causes the value to "escape"?
Ted Kremenek71454892008-04-16 20:40:59 +00003091 bool escapes = false;
Mike Stump11289f42009-09-09 15:08:12 +00003092
Ted Kremeneke86755e2008-10-18 03:49:51 +00003093 // A value escapes in three possible cases (this may change):
3094 //
3095 // (1) we are binding to something that is not a memory region.
3096 // (2) we are binding to a memregion that does not have stack storage
3097 // (3) we are binding to a memregion with stack storage that the store
Mike Stump11289f42009-09-09 15:08:12 +00003098 // does not understand.
Ted Kremenek89a303c2009-06-18 00:49:02 +00003099 const GRState *state = B.getState();
Ted Kremeneke86755e2008-10-18 03:49:51 +00003100
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003101 if (!isa<loc::MemRegionVal>(location))
Ted Kremenek71454892008-04-16 20:40:59 +00003102 escapes = true;
Ted Kremenek5ca90a22008-10-04 05:50:14 +00003103 else {
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003104 const MemRegion* R = cast<loc::MemRegionVal>(location).getRegion();
Ted Kremenek404b1322009-06-23 18:05:21 +00003105 escapes = !R->hasStackStorage();
Mike Stump11289f42009-09-09 15:08:12 +00003106
Ted Kremeneke86755e2008-10-18 03:49:51 +00003107 if (!escapes) {
3108 // To test (3), generate a new state with the binding removed. If it is
3109 // the same state, then it escapes (since the store cannot represent
3110 // the binding).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003111 escapes = (state == (state->bindLoc(cast<Loc>(location), UnknownVal())));
Ted Kremeneke86755e2008-10-18 03:49:51 +00003112 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +00003113 }
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003114
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003115 // If our store can represent the binding and we aren't storing to something
3116 // that doesn't have local storage then just return and have the simulation
3117 // state continue as is.
3118 if (!escapes)
3119 return;
Ted Kremeneke86755e2008-10-18 03:49:51 +00003120
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003121 // Otherwise, find all symbols referenced by 'val' that we are tracking
3122 // and stop tracking them.
Ted Kremenek89a303c2009-06-18 00:49:02 +00003123 B.MakeNode(state->scanReachableSymbols<StopTrackingCallback>(val).getState());
Ted Kremenekcbf4c612008-04-16 22:32:20 +00003124}
3125
Ted Kremeneka506fec2008-04-17 18:12:53 +00003126 // Return statements.
3127
Zhongxing Xu20227f72009-08-06 01:32:16 +00003128void CFRefCount::EvalReturn(ExplodedNodeSet& Dst,
Ted Kremeneka506fec2008-04-17 18:12:53 +00003129 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003130 GRStmtNodeBuilder& Builder,
Ted Kremeneka506fec2008-04-17 18:12:53 +00003131 ReturnStmt* S,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003132 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00003133
Ted Kremeneka506fec2008-04-17 18:12:53 +00003134 Expr* RetE = S->getRetValue();
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003135 if (!RetE)
Ted Kremeneka506fec2008-04-17 18:12:53 +00003136 return;
Mike Stump11289f42009-09-09 15:08:12 +00003137
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003138 const GRState *state = Builder.GetState(Pred);
3139 SymbolRef Sym = state->getSValAsScalarOrLoc(RetE).getAsLocSymbol();
Mike Stump11289f42009-09-09 15:08:12 +00003140
Ted Kremenek3e31c262009-03-26 03:35:11 +00003141 if (!Sym)
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003142 return;
Mike Stump11289f42009-09-09 15:08:12 +00003143
Ted Kremeneka506fec2008-04-17 18:12:53 +00003144 // Get the reference count binding (if any).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003145 const RefVal* T = state->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00003146
Ted Kremeneka506fec2008-04-17 18:12:53 +00003147 if (!T)
3148 return;
Mike Stump11289f42009-09-09 15:08:12 +00003149
3150 // Change the reference count.
3151 RefVal X = *T;
3152
3153 switch (X.getKind()) {
3154 case RefVal::Owned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00003155 unsigned cnt = X.getCount();
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003156 assert (cnt > 0);
Ted Kremenek3978f792009-05-10 05:11:21 +00003157 X.setCount(cnt - 1);
3158 X = X ^ RefVal::ReturnedOwned;
Ted Kremeneka506fec2008-04-17 18:12:53 +00003159 break;
3160 }
Mike Stump11289f42009-09-09 15:08:12 +00003161
Ted Kremeneka506fec2008-04-17 18:12:53 +00003162 case RefVal::NotOwned: {
3163 unsigned cnt = X.getCount();
Ted Kremenek3978f792009-05-10 05:11:21 +00003164 if (cnt) {
3165 X.setCount(cnt - 1);
3166 X = X ^ RefVal::ReturnedOwned;
3167 }
3168 else {
3169 X = X ^ RefVal::ReturnedNotOwned;
3170 }
Ted Kremeneka506fec2008-04-17 18:12:53 +00003171 break;
3172 }
Mike Stump11289f42009-09-09 15:08:12 +00003173
3174 default:
Ted Kremeneka506fec2008-04-17 18:12:53 +00003175 return;
3176 }
Mike Stump11289f42009-09-09 15:08:12 +00003177
Ted Kremeneka506fec2008-04-17 18:12:53 +00003178 // Update the binding.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003179 state = state->set<RefBindings>(Sym, X);
Ted Kremenek6bd78702009-04-29 18:50:19 +00003180 Pred = Builder.MakeNode(Dst, S, Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003181
Ted Kremenekb4e27a12009-04-30 05:51:50 +00003182 // Did we cache out?
3183 if (!Pred)
3184 return;
Mike Stump11289f42009-09-09 15:08:12 +00003185
Ted Kremenek3978f792009-05-10 05:11:21 +00003186 // Update the autorelease counts.
3187 static unsigned autoreleasetag = 0;
3188 GenericNodeBuilder Bd(Builder, S, &autoreleasetag);
3189 bool stop = false;
3190 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state , Bd, Pred, Eng, Sym,
3191 X, stop);
Mike Stump11289f42009-09-09 15:08:12 +00003192
Ted Kremenek3978f792009-05-10 05:11:21 +00003193 // Did we cache out?
3194 if (!Pred || stop)
3195 return;
Mike Stump11289f42009-09-09 15:08:12 +00003196
Ted Kremenek3978f792009-05-10 05:11:21 +00003197 // Get the updated binding.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003198 T = state->get<RefBindings>(Sym);
Ted Kremenek3978f792009-05-10 05:11:21 +00003199 assert(T);
3200 X = *T;
Mike Stump11289f42009-09-09 15:08:12 +00003201
Ted Kremenek6bd78702009-04-29 18:50:19 +00003202 // Any leaks or other errors?
3203 if (X.isReturnedOwned() && X.getCount() == 0) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00003204 Decl const *CD = &Pred->getCodeDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003205 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00003206 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
Ted Kremenekdee56e32009-05-10 06:25:57 +00003207 RetEffect RE = Summ.getRetEffect();
3208 bool hasError = false;
3209
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003210 if (RE.getKind() != RetEffect::NoRet) {
3211 if (isGCEnabled() && RE.getObjKind() == RetEffect::ObjC) {
3212 // Things are more complicated with garbage collection. If the
3213 // returned object is suppose to be an Objective-C object, we have
3214 // a leak (as the caller expects a GC'ed object) because no
3215 // method should return ownership unless it returns a CF object.
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003216 hasError = true;
Ted Kremenek8070b822009-10-14 23:58:34 +00003217 X = X ^ RefVal::ErrorGCLeakReturned;
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003218 }
3219 else if (!RE.isOwned()) {
3220 // Either we are using GC and the returned object is a CF type
3221 // or we aren't using GC. In either case, we expect that the
Mike Stump11289f42009-09-09 15:08:12 +00003222 // enclosing method is expected to return ownership.
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003223 hasError = true;
3224 X = X ^ RefVal::ErrorLeakReturned;
3225 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00003226 }
Mike Stump11289f42009-09-09 15:08:12 +00003227
3228 if (hasError) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00003229 // Generate an error node.
Ted Kremenekdee56e32009-05-10 06:25:57 +00003230 static int ReturnOwnLeakTag = 0;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003231 state = state->set<RefBindings>(Sym, X);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003232 ExplodedNode *N =
Zhongxing Xue1190f72009-08-15 03:17:38 +00003233 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3234 &ReturnOwnLeakTag), state, Pred);
Ted Kremenekdee56e32009-05-10 06:25:57 +00003235 if (N) {
3236 CFRefReport *report =
Ted Kremenekb4e27a12009-04-30 05:51:50 +00003237 new CFRefLeakReport(*static_cast<CFRefBug*>(leakAtReturn), *this,
3238 N, Sym, Eng);
3239 BR->EmitReport(report);
3240 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00003241 }
Mike Stump11289f42009-09-09 15:08:12 +00003242 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00003243 }
3244 else if (X.isReturnedNotOwned()) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00003245 Decl const *CD = &Pred->getCodeDecl();
Ted Kremenekdee56e32009-05-10 06:25:57 +00003246 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
3247 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
3248 if (Summ.getRetEffect().isOwned()) {
3249 // Trying to return a not owned object to a caller expecting an
3250 // owned object.
Mike Stump11289f42009-09-09 15:08:12 +00003251
Ted Kremenekdee56e32009-05-10 06:25:57 +00003252 static int ReturnNotOwnedForOwnedTag = 0;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003253 state = state->set<RefBindings>(Sym, X ^ RefVal::ErrorReturnedNotOwned);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003254 if (ExplodedNode *N =
Zhongxing Xue1190f72009-08-15 03:17:38 +00003255 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3256 &ReturnNotOwnedForOwnedTag),
3257 state, Pred)) {
Ted Kremenekdee56e32009-05-10 06:25:57 +00003258 CFRefReport *report =
3259 new CFRefReport(*static_cast<CFRefBug*>(returnNotOwnedForOwned),
3260 *this, N, Sym);
3261 BR->EmitReport(report);
3262 }
3263 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00003264 }
3265 }
Ted Kremeneka506fec2008-04-17 18:12:53 +00003266}
3267
Ted Kremenek4d837282008-04-18 19:23:43 +00003268// Assumptions.
3269
Ted Kremenekf9906842009-06-18 22:57:13 +00003270const GRState* CFRefCount::EvalAssume(const GRState *state,
3271 SVal Cond, bool Assumption) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003272
3273 // FIXME: We may add to the interface of EvalAssume the list of symbols
3274 // whose assumptions have changed. For now we just iterate through the
3275 // bindings and check if any of the tracked symbols are NULL. This isn't
Mike Stump11289f42009-09-09 15:08:12 +00003276 // too bad since the number of symbols we will track in practice are
Ted Kremenek4d837282008-04-18 19:23:43 +00003277 // probably small and EvalAssume is only called at branches and a few
3278 // other places.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003279 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003280
Ted Kremenek4d837282008-04-18 19:23:43 +00003281 if (B.isEmpty())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003282 return state;
Mike Stump11289f42009-09-09 15:08:12 +00003283
3284 bool changed = false;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003285 RefBindings::Factory& RefBFactory = state->get_context<RefBindings>();
Ted Kremenek4d837282008-04-18 19:23:43 +00003286
Mike Stump11289f42009-09-09 15:08:12 +00003287 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003288 // Check if the symbol is null (or equal to any constant).
3289 // If this is the case, stop tracking the symbol.
Ted Kremenekf9906842009-06-18 22:57:13 +00003290 if (state->getSymVal(I.getKey())) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003291 changed = true;
3292 B = RefBFactory.Remove(B, I.getKey());
3293 }
3294 }
Mike Stump11289f42009-09-09 15:08:12 +00003295
Ted Kremenek87aab6c2008-08-17 03:20:02 +00003296 if (changed)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003297 state = state->set<RefBindings>(B);
Mike Stump11289f42009-09-09 15:08:12 +00003298
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00003299 return state;
Ted Kremenek4d837282008-04-18 19:23:43 +00003300}
Ted Kremenek819e9b62008-03-11 06:39:11 +00003301
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003302const GRState * CFRefCount::Update(const GRState * state, SymbolRef sym,
Ted Kremenekc52f9392009-02-24 19:15:11 +00003303 RefVal V, ArgEffect E,
3304 RefVal::Kind& hasErr) {
Ted Kremenekf68490a2009-02-18 18:54:33 +00003305
3306 // In GC mode [... release] and [... retain] do nothing.
3307 switch (E) {
3308 default: break;
3309 case IncRefMsg: E = isGCEnabled() ? DoNothing : IncRef; break;
3310 case DecRefMsg: E = isGCEnabled() ? DoNothing : DecRef; break;
Ted Kremenek10452892009-02-18 21:57:45 +00003311 case MakeCollectable: E = isGCEnabled() ? DecRef : DoNothing; break;
Mike Stump11289f42009-09-09 15:08:12 +00003312 case NewAutoreleasePool: E = isGCEnabled() ? DoNothing :
Ted Kremenek50db3d02009-02-23 17:45:03 +00003313 NewAutoreleasePool; break;
Ted Kremenekf68490a2009-02-18 18:54:33 +00003314 }
Mike Stump11289f42009-09-09 15:08:12 +00003315
Ted Kremenekea072e32009-03-17 19:42:23 +00003316 // Handle all use-after-releases.
3317 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
3318 V = V ^ RefVal::ErrorUseAfterRelease;
3319 hasErr = V.getKind();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003320 return state->set<RefBindings>(sym, V);
Mike Stump11289f42009-09-09 15:08:12 +00003321 }
3322
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003323 switch (E) {
3324 default:
3325 assert (false && "Unhandled CFRef transition.");
Mike Stump11289f42009-09-09 15:08:12 +00003326
Ted Kremenekea072e32009-03-17 19:42:23 +00003327 case Dealloc:
3328 // Any use of -dealloc in GC is *bad*.
3329 if (isGCEnabled()) {
3330 V = V ^ RefVal::ErrorDeallocGC;
3331 hasErr = V.getKind();
3332 break;
3333 }
Mike Stump11289f42009-09-09 15:08:12 +00003334
Ted Kremenekea072e32009-03-17 19:42:23 +00003335 switch (V.getKind()) {
3336 default:
3337 assert(false && "Invalid case.");
3338 case RefVal::Owned:
3339 // The object immediately transitions to the released state.
3340 V = V ^ RefVal::Released;
3341 V.clearCounts();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003342 return state->set<RefBindings>(sym, V);
Ted Kremenekea072e32009-03-17 19:42:23 +00003343 case RefVal::NotOwned:
3344 V = V ^ RefVal::ErrorDeallocNotOwned;
3345 hasErr = V.getKind();
3346 break;
Mike Stump11289f42009-09-09 15:08:12 +00003347 }
Ted Kremenekea072e32009-03-17 19:42:23 +00003348 break;
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003349
Ted Kremenek8ec8cf02009-02-25 23:11:49 +00003350 case NewAutoreleasePool:
3351 assert(!isGCEnabled());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003352 return state->add<AutoreleaseStack>(sym);
Mike Stump11289f42009-09-09 15:08:12 +00003353
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003354 case MayEscape:
3355 if (V.getKind() == RefVal::Owned) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003356 V = V ^ RefVal::NotOwned;
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003357 break;
3358 }
Ted Kremenekea072e32009-03-17 19:42:23 +00003359
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003360 // Fall-through.
Mike Stump11289f42009-09-09 15:08:12 +00003361
Ted Kremenekae529272008-07-09 18:11:16 +00003362 case DoNothingByRef:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003363 case DoNothing:
Ted Kremenekc52f9392009-02-24 19:15:11 +00003364 return state;
Ted Kremeneka0e071c2008-06-30 16:57:41 +00003365
Ted Kremenekc7832092009-01-28 21:44:40 +00003366 case Autorelease:
Ted Kremenekea072e32009-03-17 19:42:23 +00003367 if (isGCEnabled())
3368 return state;
Mike Stump11289f42009-09-09 15:08:12 +00003369
Ted Kremenek8c3f0042009-03-20 17:34:15 +00003370 // Update the autorelease counts.
3371 state = SendAutorelease(state, ARCountFactory, sym);
Ted Kremenek3a0516b2009-05-08 20:01:42 +00003372 V = V.autorelease();
Ted Kremenek2d0ff622009-05-09 01:50:57 +00003373 break;
Ted Kremenekd35272f2009-05-09 00:10:05 +00003374
Ted Kremenek821537e2008-05-06 02:41:27 +00003375 case StopTracking:
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003376 return state->remove<RefBindings>(sym);
Ted Kremenek050b91c2008-08-12 18:30:56 +00003377
Mike Stump11289f42009-09-09 15:08:12 +00003378 case IncRef:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003379 switch (V.getKind()) {
3380 default:
3381 assert(false);
3382
3383 case RefVal::Owned:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003384 case RefVal::NotOwned:
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003385 V = V + 1;
Mike Stump11289f42009-09-09 15:08:12 +00003386 break;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003387 case RefVal::Released:
Ted Kremenekea072e32009-03-17 19:42:23 +00003388 // Non-GC cases are handled above.
3389 assert(isGCEnabled());
3390 V = (V ^ RefVal::Owned) + 1;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003391 break;
Mike Stump11289f42009-09-09 15:08:12 +00003392 }
Ted Kremenek4b772092008-04-10 23:44:06 +00003393 break;
Mike Stump11289f42009-09-09 15:08:12 +00003394
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003395 case SelfOwn:
3396 V = V ^ RefVal::NotOwned;
Ted Kremenekf68490a2009-02-18 18:54:33 +00003397 // Fall-through.
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003398 case DecRef:
3399 switch (V.getKind()) {
3400 default:
Ted Kremenekea072e32009-03-17 19:42:23 +00003401 // case 'RefVal::Released' handled above.
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003402 assert (false);
Ted Kremenek050b91c2008-08-12 18:30:56 +00003403
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003404 case RefVal::Owned:
Ted Kremenek551747f2009-02-18 22:57:22 +00003405 assert(V.getCount() > 0);
3406 if (V.getCount() == 1) V = V ^ RefVal::Released;
3407 V = V - 1;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003408 break;
Mike Stump11289f42009-09-09 15:08:12 +00003409
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003410 case RefVal::NotOwned:
3411 if (V.getCount() > 0)
3412 V = V - 1;
Ted Kremenek3c03d522008-04-10 23:09:18 +00003413 else {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003414 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003415 hasErr = V.getKind();
Mike Stump11289f42009-09-09 15:08:12 +00003416 }
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003417 break;
Mike Stump11289f42009-09-09 15:08:12 +00003418
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003419 case RefVal::Released:
Ted Kremenekea072e32009-03-17 19:42:23 +00003420 // Non-GC cases are handled above.
3421 assert(isGCEnabled());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003422 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003423 hasErr = V.getKind();
Mike Stump11289f42009-09-09 15:08:12 +00003424 break;
3425 }
Ted Kremenek4b772092008-04-10 23:44:06 +00003426 break;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003427 }
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003428 return state->set<RefBindings>(sym, V);
Ted Kremenek819e9b62008-03-11 06:39:11 +00003429}
3430
Ted Kremenekce8e8812008-04-09 01:10:13 +00003431//===----------------------------------------------------------------------===//
Ted Kremenek400aae72009-02-05 06:50:21 +00003432// Handle dead symbols and end-of-path.
3433//===----------------------------------------------------------------------===//
3434
Zhongxing Xu20227f72009-08-06 01:32:16 +00003435std::pair<ExplodedNode*, const GRState *>
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003436CFRefCount::HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003437 ExplodedNode* Pred,
Ted Kremenekd35272f2009-05-09 00:10:05 +00003438 GRExprEngine &Eng,
3439 SymbolRef Sym, RefVal V, bool &stop) {
Mike Stump11289f42009-09-09 15:08:12 +00003440
Ted Kremenekd35272f2009-05-09 00:10:05 +00003441 unsigned ACnt = V.getAutoreleaseCount();
3442 stop = false;
3443
3444 // No autorelease counts? Nothing to be done.
3445 if (!ACnt)
3446 return std::make_pair(Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003447
3448 assert(!isGCEnabled() && "Autorelease counts in GC mode?");
Ted Kremenekd35272f2009-05-09 00:10:05 +00003449 unsigned Cnt = V.getCount();
Mike Stump11289f42009-09-09 15:08:12 +00003450
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003451 // FIXME: Handle sending 'autorelease' to already released object.
3452
3453 if (V.getKind() == RefVal::ReturnedOwned)
3454 ++Cnt;
Mike Stump11289f42009-09-09 15:08:12 +00003455
Ted Kremenekd35272f2009-05-09 00:10:05 +00003456 if (ACnt <= Cnt) {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003457 if (ACnt == Cnt) {
3458 V.clearCounts();
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003459 if (V.getKind() == RefVal::ReturnedOwned)
3460 V = V ^ RefVal::ReturnedNotOwned;
3461 else
3462 V = V ^ RefVal::NotOwned;
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003463 }
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003464 else {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003465 V.setCount(Cnt - ACnt);
3466 V.setAutoreleaseCount(0);
3467 }
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003468 state = state->set<RefBindings>(Sym, V);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003469 ExplodedNode *N = Bd.MakeNode(state, Pred);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003470 stop = (N == 0);
3471 return std::make_pair(N, state);
Mike Stump11289f42009-09-09 15:08:12 +00003472 }
Ted Kremenekd35272f2009-05-09 00:10:05 +00003473
3474 // Woah! More autorelease counts then retain counts left.
3475 // Emit hard error.
3476 stop = true;
3477 V = V ^ RefVal::ErrorOverAutorelease;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003478 state = state->set<RefBindings>(Sym, V);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003479
Zhongxing Xu20227f72009-08-06 01:32:16 +00003480 if (ExplodedNode *N = Bd.MakeNode(state, Pred)) {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003481 N->markAsSink();
Mike Stump11289f42009-09-09 15:08:12 +00003482
Ted Kremenek3978f792009-05-10 05:11:21 +00003483 std::string sbuf;
3484 llvm::raw_string_ostream os(sbuf);
Ted Kremenek4785e412009-05-15 06:02:08 +00003485 os << "Object over-autoreleased: object was sent -autorelease";
Ted Kremenek3978f792009-05-10 05:11:21 +00003486 if (V.getAutoreleaseCount() > 1)
3487 os << V.getAutoreleaseCount() << " times";
3488 os << " but the object has ";
3489 if (V.getCount() == 0)
3490 os << "zero (locally visible)";
3491 else
3492 os << "+" << V.getCount();
3493 os << " retain counts";
Mike Stump11289f42009-09-09 15:08:12 +00003494
Ted Kremenekd35272f2009-05-09 00:10:05 +00003495 CFRefReport *report =
3496 new CFRefReport(*static_cast<CFRefBug*>(overAutorelease),
Ted Kremenek3978f792009-05-10 05:11:21 +00003497 *this, N, Sym, os.str().c_str());
Ted Kremenekd35272f2009-05-09 00:10:05 +00003498 BR->EmitReport(report);
3499 }
Mike Stump11289f42009-09-09 15:08:12 +00003500
Zhongxing Xu20227f72009-08-06 01:32:16 +00003501 return std::make_pair((ExplodedNode*)0, state);
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003502}
Ted Kremenek884a8992009-05-08 23:09:42 +00003503
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003504const GRState *
3505CFRefCount::HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek884a8992009-05-08 23:09:42 +00003506 llvm::SmallVectorImpl<SymbolRef> &Leaked) {
Mike Stump11289f42009-09-09 15:08:12 +00003507
3508 bool hasLeak = V.isOwned() ||
Ted Kremenek884a8992009-05-08 23:09:42 +00003509 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Mike Stump11289f42009-09-09 15:08:12 +00003510
Ted Kremenek884a8992009-05-08 23:09:42 +00003511 if (!hasLeak)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003512 return state->remove<RefBindings>(sid);
Mike Stump11289f42009-09-09 15:08:12 +00003513
Ted Kremenek884a8992009-05-08 23:09:42 +00003514 Leaked.push_back(sid);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003515 return state->set<RefBindings>(sid, V ^ RefVal::ErrorLeak);
Ted Kremenek884a8992009-05-08 23:09:42 +00003516}
3517
Zhongxing Xu20227f72009-08-06 01:32:16 +00003518ExplodedNode*
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003519CFRefCount::ProcessLeaks(const GRState * state,
Ted Kremenek884a8992009-05-08 23:09:42 +00003520 llvm::SmallVectorImpl<SymbolRef> &Leaked,
3521 GenericNodeBuilder &Builder,
3522 GRExprEngine& Eng,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003523 ExplodedNode *Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00003524
Ted Kremenek884a8992009-05-08 23:09:42 +00003525 if (Leaked.empty())
3526 return Pred;
Mike Stump11289f42009-09-09 15:08:12 +00003527
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003528 // Generate an intermediate node representing the leak point.
Zhongxing Xu20227f72009-08-06 01:32:16 +00003529 ExplodedNode *N = Builder.MakeNode(state, Pred);
Mike Stump11289f42009-09-09 15:08:12 +00003530
Ted Kremenek884a8992009-05-08 23:09:42 +00003531 if (N) {
3532 for (llvm::SmallVectorImpl<SymbolRef>::iterator
3533 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00003534
3535 CFRefBug *BT = static_cast<CFRefBug*>(Pred ? leakWithinFunction
Ted Kremenek884a8992009-05-08 23:09:42 +00003536 : leakAtReturn);
3537 assert(BT && "BugType not initialized.");
3538 CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, *I, Eng);
3539 BR->EmitReport(report);
3540 }
3541 }
Mike Stump11289f42009-09-09 15:08:12 +00003542
Ted Kremenek884a8992009-05-08 23:09:42 +00003543 return N;
3544}
3545
Ted Kremenek400aae72009-02-05 06:50:21 +00003546void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003547 GREndPathNodeBuilder& Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00003548
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003549 const GRState *state = Builder.getState();
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003550 GenericNodeBuilder Bd(Builder);
Mike Stump11289f42009-09-09 15:08:12 +00003551 RefBindings B = state->get<RefBindings>();
Zhongxing Xu20227f72009-08-06 01:32:16 +00003552 ExplodedNode *Pred = 0;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003553
3554 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekd35272f2009-05-09 00:10:05 +00003555 bool stop = false;
3556 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3557 (*I).first,
Mike Stump11289f42009-09-09 15:08:12 +00003558 (*I).second, stop);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003559
3560 if (stop)
3561 return;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003562 }
Mike Stump11289f42009-09-09 15:08:12 +00003563
3564 B = state->get<RefBindings>();
3565 llvm::SmallVector<SymbolRef, 10> Leaked;
3566
Ted Kremenek884a8992009-05-08 23:09:42 +00003567 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
3568 state = HandleSymbolDeath(state, (*I).first, (*I).second, Leaked);
3569
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003570 ProcessLeaks(state, Leaked, Bd, Eng, Pred);
Ted Kremenek400aae72009-02-05 06:50:21 +00003571}
3572
Zhongxing Xu20227f72009-08-06 01:32:16 +00003573void CFRefCount::EvalDeadSymbols(ExplodedNodeSet& Dst,
Ted Kremenek400aae72009-02-05 06:50:21 +00003574 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003575 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003576 ExplodedNode* Pred,
Ted Kremenek400aae72009-02-05 06:50:21 +00003577 Stmt* S,
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003578 const GRState* state,
Ted Kremenek400aae72009-02-05 06:50:21 +00003579 SymbolReaper& SymReaper) {
Ted Kremenek884a8992009-05-08 23:09:42 +00003580
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003581 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003582
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003583 // Update counts from autorelease pools
3584 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3585 E = SymReaper.dead_end(); I != E; ++I) {
3586 SymbolRef Sym = *I;
3587 if (const RefVal* T = B.lookup(Sym)){
3588 // Use the symbol as the tag.
3589 // FIXME: This might not be as unique as we would like.
3590 GenericNodeBuilder Bd(Builder, S, Sym);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003591 bool stop = false;
3592 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3593 Sym, *T, stop);
3594 if (stop)
3595 return;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003596 }
3597 }
Mike Stump11289f42009-09-09 15:08:12 +00003598
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003599 B = state->get<RefBindings>();
Ted Kremenek884a8992009-05-08 23:09:42 +00003600 llvm::SmallVector<SymbolRef, 10> Leaked;
Mike Stump11289f42009-09-09 15:08:12 +00003601
Ted Kremenek400aae72009-02-05 06:50:21 +00003602 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00003603 E = SymReaper.dead_end(); I != E; ++I) {
Ted Kremenek884a8992009-05-08 23:09:42 +00003604 if (const RefVal* T = B.lookup(*I))
3605 state = HandleSymbolDeath(state, *I, *T, Leaked);
Mike Stump11289f42009-09-09 15:08:12 +00003606 }
3607
Ted Kremenek884a8992009-05-08 23:09:42 +00003608 static unsigned LeakPPTag = 0;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003609 {
3610 GenericNodeBuilder Bd(Builder, S, &LeakPPTag);
3611 Pred = ProcessLeaks(state, Leaked, Bd, Eng, Pred);
3612 }
Mike Stump11289f42009-09-09 15:08:12 +00003613
Ted Kremenek884a8992009-05-08 23:09:42 +00003614 // Did we cache out?
3615 if (!Pred)
3616 return;
Mike Stump11289f42009-09-09 15:08:12 +00003617
Ted Kremenek68abaa92009-02-19 23:47:02 +00003618 // Now generate a new node that nukes the old bindings.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003619 RefBindings::Factory& F = state->get_context<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003620
Ted Kremenek68abaa92009-02-19 23:47:02 +00003621 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Ted Kremenek884a8992009-05-08 23:09:42 +00003622 E = SymReaper.dead_end(); I!=E; ++I) B = F.Remove(B, *I);
Mike Stump11289f42009-09-09 15:08:12 +00003623
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003624 state = state->set<RefBindings>(B);
Ted Kremenek68abaa92009-02-19 23:47:02 +00003625 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek400aae72009-02-05 06:50:21 +00003626}
3627
Zhongxing Xu20227f72009-08-06 01:32:16 +00003628void CFRefCount::ProcessNonLeakError(ExplodedNodeSet& Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003629 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003630 Expr* NodeExpr, Expr* ErrorExpr,
3631 ExplodedNode* Pred,
Ted Kremenek400aae72009-02-05 06:50:21 +00003632 const GRState* St,
3633 RefVal::Kind hasErr, SymbolRef Sym) {
3634 Builder.BuildSinks = true;
Zhongxing Xu107f7592009-08-06 12:48:26 +00003635 ExplodedNode *N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
Mike Stump11289f42009-09-09 15:08:12 +00003636
Ted Kremenek2d0ff622009-05-09 01:50:57 +00003637 if (!N)
3638 return;
Mike Stump11289f42009-09-09 15:08:12 +00003639
Ted Kremenek400aae72009-02-05 06:50:21 +00003640 CFRefBug *BT = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003641
Ted Kremenekea072e32009-03-17 19:42:23 +00003642 switch (hasErr) {
3643 default:
3644 assert(false && "Unhandled error.");
3645 return;
3646 case RefVal::ErrorUseAfterRelease:
3647 BT = static_cast<CFRefBug*>(useAfterRelease);
Mike Stump11289f42009-09-09 15:08:12 +00003648 break;
Ted Kremenekea072e32009-03-17 19:42:23 +00003649 case RefVal::ErrorReleaseNotOwned:
3650 BT = static_cast<CFRefBug*>(releaseNotOwned);
3651 break;
3652 case RefVal::ErrorDeallocGC:
3653 BT = static_cast<CFRefBug*>(deallocGC);
3654 break;
3655 case RefVal::ErrorDeallocNotOwned:
3656 BT = static_cast<CFRefBug*>(deallocNotOwned);
3657 break;
Ted Kremenek400aae72009-02-05 06:50:21 +00003658 }
Mike Stump11289f42009-09-09 15:08:12 +00003659
Ted Kremenek48d16452009-02-18 03:48:14 +00003660 CFRefReport *report = new CFRefReport(*BT, *this, N, Sym);
Ted Kremenek400aae72009-02-05 06:50:21 +00003661 report->addRange(ErrorExpr->getSourceRange());
3662 BR->EmitReport(report);
3663}
3664
3665//===----------------------------------------------------------------------===//
Ted Kremenek4a78c3a2008-04-10 22:16:52 +00003666// Transfer function creation for external clients.
Ted Kremenek819e9b62008-03-11 06:39:11 +00003667//===----------------------------------------------------------------------===//
3668
Ted Kremenekb0f87c42008-04-30 23:47:44 +00003669GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
3670 const LangOptions& lopts) {
Ted Kremenek1f352db2008-07-22 16:21:24 +00003671 return new CFRefCount(Ctx, GCEnabled, lopts);
Mike Stump11289f42009-09-09 15:08:12 +00003672}