blob: 2da0297e41236abd81183b182cebcbc971659cfe [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
Ted Kremenek32819772009-05-15 15:49:00 +000084 const char *s = II->getName();
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 hasPrefix(const char* s, const char* prefix) {
212 if (!prefix)
213 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000214
Ted Kremenek7e904222009-01-12 21:45:02 +0000215 char c = *s;
216 char cP = *prefix;
Mike Stump11289f42009-09-09 15:08:12 +0000217
Ted Kremenek7e904222009-01-12 21:45:02 +0000218 while (c != '\0' && cP != '\0') {
219 if (c != cP) break;
220 c = *(++s);
221 cP = *(++prefix);
222 }
Mike Stump11289f42009-09-09 15:08:12 +0000223
Ted Kremenek7e904222009-01-12 21:45:02 +0000224 return cP == '\0';
Ted Kremenekf958ec52008-05-07 20:06:41 +0000225}
226
Ted Kremenek7e904222009-01-12 21:45:02 +0000227static bool hasSuffix(const char* s, const char* suffix) {
228 const char* loc = strstr(s, suffix);
229 return loc && strcmp(suffix, loc) == 0;
230}
231
232static bool isRefType(QualType RetTy, const char* prefix,
233 ASTContext* Ctx = 0, const char* name = 0) {
Mike Stump11289f42009-09-09 15:08:12 +0000234
Ted Kremenek95d18192009-05-12 04:53:03 +0000235 // Recursively walk the typedef stack, allowing typedefs of reference types.
236 while (1) {
237 if (TypedefType* TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
238 const char* TDName = TD->getDecl()->getIdentifier()->getName();
239 if (hasPrefix(TDName, prefix) && hasSuffix(TDName, "Ref"))
240 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000241
Ted Kremenek95d18192009-05-12 04:53:03 +0000242 RetTy = TD->getDecl()->getUnderlyingType();
243 continue;
244 }
245 break;
Ted Kremenek7e904222009-01-12 21:45:02 +0000246 }
247
248 if (!Ctx || !name)
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000249 return false;
Ted Kremenek7e904222009-01-12 21:45:02 +0000250
251 // Is the type void*?
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000252 const PointerType* PT = RetTy->getAs<PointerType>();
Ted Kremenek7e904222009-01-12 21:45:02 +0000253 if (!(PT->getPointeeType().getUnqualifiedType() == Ctx->VoidTy))
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000254 return false;
Ted Kremenek7e904222009-01-12 21:45:02 +0000255
256 // Does the name start with the prefix?
257 return hasPrefix(name, prefix);
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000258}
259
Ted Kremeneka506fec2008-04-17 18:12:53 +0000260//===----------------------------------------------------------------------===//
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000261// Primitives used for constructing summaries for function/method calls.
Ted Kremenekc8bef6a2008-04-09 23:49:11 +0000262//===----------------------------------------------------------------------===//
263
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000264/// ArgEffect is used to summarize a function/method call's effect on a
265/// particular argument.
Ted Kremenekea072e32009-03-17 19:42:23 +0000266enum ArgEffect { Autorelease, Dealloc, DecRef, DecRefMsg, DoNothing,
267 DoNothingByRef, IncRefMsg, IncRef, MakeCollectable, MayEscape,
268 NewAutoreleasePool, SelfOwn, StopTracking };
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000269
Ted Kremenek819e9b62008-03-11 06:39:11 +0000270namespace llvm {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000271template <> struct FoldingSetTrait<ArgEffect> {
272static inline void Profile(const ArgEffect X, FoldingSetNodeID& ID) {
273 ID.AddInteger((unsigned) X);
274}
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000275};
Ted Kremenek819e9b62008-03-11 06:39:11 +0000276} // end llvm namespace
277
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000278/// ArgEffects summarizes the effects of a function/method call on all of
279/// its arguments.
280typedef llvm::ImmutableMap<unsigned,ArgEffect> ArgEffects;
281
Ted Kremenek819e9b62008-03-11 06:39:11 +0000282namespace {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000283
284/// RetEffect is used to summarize a function/method call's behavior with
Mike Stump11289f42009-09-09 15:08:12 +0000285/// respect to its return value.
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000286class VISIBILITY_HIDDEN RetEffect {
Ted Kremenek819e9b62008-03-11 06:39:11 +0000287public:
Ted Kremenekab4a8b52008-06-23 18:02:52 +0000288 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
Ted Kremenek1272f702009-05-12 20:06:54 +0000289 NotOwnedSymbol, GCNotOwnedSymbol, ReceiverAlias,
290 OwnedWhenTrackedReceiver };
Mike Stump11289f42009-09-09 15:08:12 +0000291
292 enum ObjKind { CF, ObjC, AnyObj };
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000293
Ted Kremenek819e9b62008-03-11 06:39:11 +0000294private:
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000295 Kind K;
296 ObjKind O;
297 unsigned index;
298
299 RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {}
300 RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {}
Mike Stump11289f42009-09-09 15:08:12 +0000301
Ted Kremenek819e9b62008-03-11 06:39:11 +0000302public:
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000303 Kind getKind() const { return K; }
304
305 ObjKind getObjKind() const { return O; }
Mike Stump11289f42009-09-09 15:08:12 +0000306
307 unsigned getIndex() const {
Ted Kremenek819e9b62008-03-11 06:39:11 +0000308 assert(getKind() == Alias);
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000309 return index;
Ted Kremenek819e9b62008-03-11 06:39:11 +0000310 }
Mike Stump11289f42009-09-09 15:08:12 +0000311
Ted Kremenek223a7d52009-04-29 23:03:22 +0000312 bool isOwned() const {
Ted Kremenek1272f702009-05-12 20:06:54 +0000313 return K == OwnedSymbol || K == OwnedAllocatedSymbol ||
314 K == OwnedWhenTrackedReceiver;
Ted Kremenek223a7d52009-04-29 23:03:22 +0000315 }
Mike Stump11289f42009-09-09 15:08:12 +0000316
Ted Kremenek1272f702009-05-12 20:06:54 +0000317 static RetEffect MakeOwnedWhenTrackedReceiver() {
318 return RetEffect(OwnedWhenTrackedReceiver, ObjC);
319 }
Mike Stump11289f42009-09-09 15:08:12 +0000320
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000321 static RetEffect MakeAlias(unsigned Idx) {
322 return RetEffect(Alias, Idx);
323 }
324 static RetEffect MakeReceiverAlias() {
325 return RetEffect(ReceiverAlias);
Mike Stump11289f42009-09-09 15:08:12 +0000326 }
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000327 static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) {
328 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o);
Mike Stump11289f42009-09-09 15:08:12 +0000329 }
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000330 static RetEffect MakeNotOwned(ObjKind o) {
331 return RetEffect(NotOwnedSymbol, o);
Ted Kremeneke6633562009-04-27 19:14:45 +0000332 }
333 static RetEffect MakeGCNotOwned() {
334 return RetEffect(GCNotOwnedSymbol, ObjC);
335 }
Mike Stump11289f42009-09-09 15:08:12 +0000336
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000337 static RetEffect MakeNoRet() {
338 return RetEffect(NoRet);
Ted Kremenekab4a8b52008-06-23 18:02:52 +0000339 }
Mike Stump11289f42009-09-09 15:08:12 +0000340
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000341 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekaeb115f2009-01-28 05:56:51 +0000342 ID.AddInteger((unsigned)K);
343 ID.AddInteger((unsigned)O);
344 ID.AddInteger(index);
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000345 }
Ted Kremenek819e9b62008-03-11 06:39:11 +0000346};
Mike Stump11289f42009-09-09 15:08:12 +0000347
348
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000349class VISIBILITY_HIDDEN RetainSummary {
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000350 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
351 /// specifies the argument (starting from 0). This can be sparsely
352 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000353 ArgEffects Args;
Mike Stump11289f42009-09-09 15:08:12 +0000354
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000355 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
356 /// do not have an entry in Args.
357 ArgEffect DefaultArgEffect;
Mike Stump11289f42009-09-09 15:08:12 +0000358
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000359 /// Receiver - If this summary applies to an Objective-C message expression,
360 /// this is the effect applied to the state of the receiver.
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000361 ArgEffect Receiver;
Mike Stump11289f42009-09-09 15:08:12 +0000362
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000363 /// Ret - The effect on the return value. Used to indicate if the
364 /// function/method call returns a new tracked symbol, returns an
365 /// alias of one of the arguments in the call, and so on.
Ted Kremenek819e9b62008-03-11 06:39:11 +0000366 RetEffect Ret;
Mike Stump11289f42009-09-09 15:08:12 +0000367
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000368 /// EndPath - Indicates that execution of this method/function should
369 /// terminate the simulation of a path.
370 bool EndPath;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Ted Kremenek819e9b62008-03-11 06:39:11 +0000372public:
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000373 RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff,
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000374 ArgEffect ReceiverEff, bool endpath = false)
375 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
Mike Stump11289f42009-09-09 15:08:12 +0000376 EndPath(endpath) {}
377
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000378 /// getArg - Return the argument effect on the argument specified by
379 /// idx (starting from 0).
Ted Kremenekbf9d8042008-03-11 17:48:22 +0000380 ArgEffect getArg(unsigned idx) const {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000381 if (const ArgEffect *AE = Args.lookup(idx))
382 return *AE;
Mike Stump11289f42009-09-09 15:08:12 +0000383
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000384 return DefaultArgEffect;
Ted Kremenekbf9d8042008-03-11 17:48:22 +0000385 }
Mike Stump11289f42009-09-09 15:08:12 +0000386
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000387 /// setDefaultArgEffect - Set the default argument effect.
388 void setDefaultArgEffect(ArgEffect E) {
389 DefaultArgEffect = E;
390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000392 /// setArg - Set the argument effect on the argument specified by idx.
393 void setArgEffect(ArgEffects::Factory& AF, unsigned idx, ArgEffect E) {
394 Args = AF.Add(Args, idx, E);
395 }
Mike Stump11289f42009-09-09 15:08:12 +0000396
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000397 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000398 RetEffect getRetEffect() const { return Ret; }
Mike Stump11289f42009-09-09 15:08:12 +0000399
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000400 /// setRetEffect - Set the effect of the return value of the call.
401 void setRetEffect(RetEffect E) { Ret = E; }
Mike Stump11289f42009-09-09 15:08:12 +0000402
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000403 /// isEndPath - Returns true if executing the given method/function should
404 /// terminate the path.
405 bool isEndPath() const { return EndPath; }
Mike Stump11289f42009-09-09 15:08:12 +0000406
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000407 /// getReceiverEffect - Returns the effect on the receiver of the call.
408 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000409 ArgEffect getReceiverEffect() const { return Receiver; }
Mike Stump11289f42009-09-09 15:08:12 +0000410
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000411 /// setReceiverEffect - Set the effect on the receiver of the call.
412 void setReceiverEffect(ArgEffect E) { Receiver = E; }
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000414 typedef ArgEffects::iterator ExprIterator;
Mike Stump11289f42009-09-09 15:08:12 +0000415
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000416 ExprIterator begin_args() const { return Args.begin(); }
417 ExprIterator end_args() const { return Args.end(); }
Mike Stump11289f42009-09-09 15:08:12 +0000418
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000419 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects A,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000420 RetEffect RetEff, ArgEffect DefaultEff,
Ted Kremenekf7faa422008-07-18 17:39:56 +0000421 ArgEffect ReceiverEff, bool EndPath) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000422 ID.Add(A);
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000423 ID.Add(RetEff);
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000424 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000425 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenekf7faa422008-07-18 17:39:56 +0000426 ID.AddInteger((unsigned) EndPath);
Ted Kremenek819e9b62008-03-11 06:39:11 +0000427 }
Mike Stump11289f42009-09-09 15:08:12 +0000428
Ted Kremenek819e9b62008-03-11 06:39:11 +0000429 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenekf7faa422008-07-18 17:39:56 +0000430 Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
Ted Kremenek819e9b62008-03-11 06:39:11 +0000431 }
432};
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000433} // end anonymous namespace
Ted Kremenek819e9b62008-03-11 06:39:11 +0000434
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000435//===----------------------------------------------------------------------===//
436// Data structures for constructing summaries.
437//===----------------------------------------------------------------------===//
Ted Kremenekb1d13292008-06-24 03:49:48 +0000438
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000439namespace {
440class VISIBILITY_HIDDEN ObjCSummaryKey {
441 IdentifierInfo* II;
442 Selector S;
Mike Stump11289f42009-09-09 15:08:12 +0000443public:
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000444 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
445 : II(ii), S(s) {}
446
Ted Kremenek223a7d52009-04-29 23:03:22 +0000447 ObjCSummaryKey(const ObjCInterfaceDecl* d, Selector s)
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000448 : II(d ? d->getIdentifier() : 0), S(s) {}
Ted Kremenek5801f652009-05-13 18:16:01 +0000449
450 ObjCSummaryKey(const ObjCInterfaceDecl* d, IdentifierInfo *ii, Selector s)
451 : II(d ? d->getIdentifier() : ii), S(s) {}
Mike Stump11289f42009-09-09 15:08:12 +0000452
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000453 ObjCSummaryKey(Selector s)
454 : II(0), S(s) {}
Mike Stump11289f42009-09-09 15:08:12 +0000455
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000456 IdentifierInfo* getIdentifier() const { return II; }
457 Selector getSelector() const { return S; }
458};
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000459}
460
461namespace llvm {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000462template <> struct DenseMapInfo<ObjCSummaryKey> {
463 static inline ObjCSummaryKey getEmptyKey() {
464 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
465 DenseMapInfo<Selector>::getEmptyKey());
466 }
Mike Stump11289f42009-09-09 15:08:12 +0000467
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000468 static inline ObjCSummaryKey getTombstoneKey() {
469 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
Mike Stump11289f42009-09-09 15:08:12 +0000470 DenseMapInfo<Selector>::getTombstoneKey());
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000471 }
Mike Stump11289f42009-09-09 15:08:12 +0000472
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000473 static unsigned getHashValue(const ObjCSummaryKey &V) {
474 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
Mike Stump11289f42009-09-09 15:08:12 +0000475 & 0x88888888)
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000476 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
477 & 0x55555555);
478 }
Mike Stump11289f42009-09-09 15:08:12 +0000479
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000480 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
481 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
482 RHS.getIdentifier()) &&
483 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
484 RHS.getSelector());
485 }
Mike Stump11289f42009-09-09 15:08:12 +0000486
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000487 static bool isPod() {
488 return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
489 DenseMapInfo<Selector>::isPod();
490 }
491};
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000492} // end llvm namespace
Mike Stump11289f42009-09-09 15:08:12 +0000493
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000494namespace {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000495class VISIBILITY_HIDDEN ObjCSummaryCache {
496 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
497 MapTy M;
498public:
499 ObjCSummaryCache() {}
Mike Stump11289f42009-09-09 15:08:12 +0000500
Ted Kremenek8be51382009-07-21 23:27:57 +0000501 RetainSummary* find(const ObjCInterfaceDecl* D, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +0000502 Selector S) {
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000503 // Lookup the method using the decl for the class @interface. If we
504 // have no decl, lookup using the class name.
505 return D ? find(D, S) : find(ClsName, S);
506 }
Mike Stump11289f42009-09-09 15:08:12 +0000507
508 RetainSummary* find(const ObjCInterfaceDecl* D, Selector S) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000509 // Do a lookup with the (D,S) pair. If we find a match return
510 // the iterator.
511 ObjCSummaryKey K(D, S);
512 MapTy::iterator I = M.find(K);
Mike Stump11289f42009-09-09 15:08:12 +0000513
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000514 if (I != M.end() || !D)
Ted Kremenek8be51382009-07-21 23:27:57 +0000515 return I->second;
Mike Stump11289f42009-09-09 15:08:12 +0000516
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000517 // Walk the super chain. If we find a hit with a parent, we'll end
518 // up returning that summary. We actually allow that key (null,S), as
519 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
520 // generate initial summaries without having to worry about NSObject
521 // being declared.
522 // FIXME: We may change this at some point.
523 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
524 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
525 break;
Mike Stump11289f42009-09-09 15:08:12 +0000526
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000527 if (!C)
Ted Kremenek8be51382009-07-21 23:27:57 +0000528 return NULL;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000529 }
Mike Stump11289f42009-09-09 15:08:12 +0000530
531 // Cache the summary with original key to make the next lookup faster
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000532 // and return the iterator.
Ted Kremenek8be51382009-07-21 23:27:57 +0000533 RetainSummary *Summ = I->second;
534 M[K] = Summ;
535 return Summ;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000536 }
Mike Stump11289f42009-09-09 15:08:12 +0000537
Ted Kremenek9551ab62008-08-12 20:41:56 +0000538
Ted Kremenek8be51382009-07-21 23:27:57 +0000539 RetainSummary* find(Expr* Receiver, Selector S) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000540 return find(getReceiverDecl(Receiver), S);
541 }
Mike Stump11289f42009-09-09 15:08:12 +0000542
Ted Kremenek8be51382009-07-21 23:27:57 +0000543 RetainSummary* find(IdentifierInfo* II, Selector S) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000544 // FIXME: Class method lookup. Right now we dont' have a good way
545 // of going between IdentifierInfo* and the class hierarchy.
Ted Kremenek8be51382009-07-21 23:27:57 +0000546 MapTy::iterator I = M.find(ObjCSummaryKey(II, S));
Mike Stump11289f42009-09-09 15:08:12 +0000547
Ted Kremenek8be51382009-07-21 23:27:57 +0000548 if (I == M.end())
549 I = M.find(ObjCSummaryKey(S));
Mike Stump11289f42009-09-09 15:08:12 +0000550
Ted Kremenek8be51382009-07-21 23:27:57 +0000551 return I == M.end() ? NULL : I->second;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000552 }
Mike Stump11289f42009-09-09 15:08:12 +0000553
554 const ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
Steve Naroff7cae42b2009-07-10 23:34:53 +0000555 if (const ObjCObjectPointerType* PT =
John McCall9dd450b2009-09-21 23:43:11 +0000556 E->getType()->getAs<ObjCObjectPointerType>())
Steve Naroff7cae42b2009-07-10 23:34:53 +0000557 return PT->getInterfaceDecl();
558
559 return NULL;
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000560 }
Mike Stump11289f42009-09-09 15:08:12 +0000561
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000562 RetainSummary*& operator[](ObjCMessageExpr* ME) {
Mike Stump11289f42009-09-09 15:08:12 +0000563
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000564 Selector S = ME->getSelector();
Mike Stump11289f42009-09-09 15:08:12 +0000565
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000566 if (Expr* Receiver = ME->getReceiver()) {
Steve Naroff7cae42b2009-07-10 23:34:53 +0000567 const ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000568 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
569 }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000571 return M[ObjCSummaryKey(ME->getClassName(), S)];
572 }
Mike Stump11289f42009-09-09 15:08:12 +0000573
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000574 RetainSummary*& operator[](ObjCSummaryKey K) {
575 return M[K];
576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000578 RetainSummary*& operator[](Selector S) {
579 return M[ ObjCSummaryKey(S) ];
580 }
Mike Stump11289f42009-09-09 15:08:12 +0000581};
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000582} // end anonymous namespace
583
584//===----------------------------------------------------------------------===//
585// Data structures for managing collections of summaries.
586//===----------------------------------------------------------------------===//
587
588namespace {
589class VISIBILITY_HIDDEN RetainSummaryManager {
Ted Kremenek00daccd2008-05-05 22:11:16 +0000590
591 //==-----------------------------------------------------------------==//
592 // Typedefs.
593 //==-----------------------------------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +0000594
Ted Kremenek00daccd2008-05-05 22:11:16 +0000595 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
596 FuncSummariesTy;
Mike Stump11289f42009-09-09 15:08:12 +0000597
Ted Kremenek0cfc1612008-06-23 23:30:29 +0000598 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Mike Stump11289f42009-09-09 15:08:12 +0000599
Ted Kremenek00daccd2008-05-05 22:11:16 +0000600 //==-----------------------------------------------------------------==//
601 // Data.
602 //==-----------------------------------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +0000603
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000604 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek4b7ca772008-04-29 05:33:51 +0000605 ASTContext& Ctx;
Ted Kremenekab54e512008-07-01 17:21:27 +0000606
Ted Kremenekae529272008-07-09 18:11:16 +0000607 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
608 /// "CFDictionaryCreate".
609 IdentifierInfo* CFDictionaryCreateII;
Mike Stump11289f42009-09-09 15:08:12 +0000610
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000611 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek4b7ca772008-04-29 05:33:51 +0000612 const bool GCEnabled;
Mike Stump11289f42009-09-09 15:08:12 +0000613
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000614 /// FuncSummaries - A map from FunctionDecls to summaries.
Mike Stump11289f42009-09-09 15:08:12 +0000615 FuncSummariesTy FuncSummaries;
616
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000617 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
618 /// to summaries.
Ted Kremenekea736c52008-06-23 22:21:20 +0000619 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenek00daccd2008-05-05 22:11:16 +0000620
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000621 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenekea736c52008-06-23 22:21:20 +0000622 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenek00daccd2008-05-05 22:11:16 +0000623
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000624 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
625 /// and all other data used by the checker.
Ted Kremenek00daccd2008-05-05 22:11:16 +0000626 llvm::BumpPtrAllocator BPAlloc;
Mike Stump11289f42009-09-09 15:08:12 +0000627
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000628 /// AF - A factory for ArgEffects objects.
Mike Stump11289f42009-09-09 15:08:12 +0000629 ArgEffects::Factory AF;
630
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000631 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenek00daccd2008-05-05 22:11:16 +0000632 ArgEffects ScratchArgs;
Mike Stump11289f42009-09-09 15:08:12 +0000633
Ted Kremenek9157fbb2009-05-07 23:40:42 +0000634 /// ObjCAllocRetE - Default return effect for methods returning Objective-C
635 /// objects.
636 RetEffect ObjCAllocRetE;
Ted Kremeneka03705c2009-06-05 23:18:01 +0000637
Mike Stump11289f42009-09-09 15:08:12 +0000638 /// ObjCInitRetE - Default return effect for init methods returning
Ted Kremenek815fbb62009-08-20 05:13:36 +0000639 /// Objective-C objects.
Ted Kremeneka03705c2009-06-05 23:18:01 +0000640 RetEffect ObjCInitRetE;
Mike Stump11289f42009-09-09 15:08:12 +0000641
Ted Kremenekff606a12009-05-04 04:57:00 +0000642 RetainSummary DefaultSummary;
Ted Kremenek10427bd2008-05-06 18:11:36 +0000643 RetainSummary* StopSummary;
Mike Stump11289f42009-09-09 15:08:12 +0000644
Ted Kremenek00daccd2008-05-05 22:11:16 +0000645 //==-----------------------------------------------------------------==//
646 // Methods.
647 //==-----------------------------------------------------------------==//
Mike Stump11289f42009-09-09 15:08:12 +0000648
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000649 /// getArgEffects - Returns a persistent ArgEffects object based on the
650 /// data in ScratchArgs.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000651 ArgEffects getArgEffects();
Ted Kremenek819e9b62008-03-11 06:39:11 +0000652
Mike Stump11289f42009-09-09 15:08:12 +0000653 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
654
Ted Kremenekcc3d1882008-10-23 01:56:15 +0000655public:
Ted Kremenek1272f702009-05-12 20:06:54 +0000656 RetEffect getObjAllocRetEffect() const { return ObjCAllocRetE; }
657
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000658 RetainSummary *getDefaultSummary() {
659 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
660 return new (Summ) RetainSummary(DefaultSummary);
661 }
Mike Stump11289f42009-09-09 15:08:12 +0000662
Ted Kremenek82157a12009-02-23 16:51:39 +0000663 RetainSummary* getUnarySummary(const FunctionType* FT, UnaryFuncKind func);
Mike Stump11289f42009-09-09 15:08:12 +0000664
Ted Kremenek00daccd2008-05-05 22:11:16 +0000665 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
Mike Stump11289f42009-09-09 15:08:12 +0000666 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek7e904222009-01-12 21:45:02 +0000667 RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
Mike Stump11289f42009-09-09 15:08:12 +0000668
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000669 RetainSummary* getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000670 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000671 ArgEffect DefaultEff = MayEscape,
672 bool isEndPath = false);
Ted Kremenek3700b762008-10-29 04:07:07 +0000673
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000674 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000675 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek1df2f3a2008-05-22 17:31:13 +0000676 ArgEffect DefaultEff = MayEscape) {
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000677 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek0806f912008-05-06 00:30:21 +0000678 }
Mike Stump11289f42009-09-09 15:08:12 +0000679
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000680 RetainSummary *getPersistentStopSummary() {
Ted Kremenek10427bd2008-05-06 18:11:36 +0000681 if (StopSummary)
682 return StopSummary;
Mike Stump11289f42009-09-09 15:08:12 +0000683
Ted Kremenek10427bd2008-05-06 18:11:36 +0000684 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
685 StopTracking, StopTracking);
Ted Kremenek3700b762008-10-29 04:07:07 +0000686
Ted Kremenek10427bd2008-05-06 18:11:36 +0000687 return StopSummary;
Mike Stump11289f42009-09-09 15:08:12 +0000688 }
Ted Kremenek015c3562008-05-06 04:20:12 +0000689
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000690 RetainSummary *getInitMethodSummary(QualType RetTy);
Ted Kremenek3d1e9722008-05-05 23:55:01 +0000691
Ted Kremenekea736c52008-06-23 22:21:20 +0000692 void InitializeClassMethodSummaries();
693 void InitializeMethodSummaries();
Mike Stump11289f42009-09-09 15:08:12 +0000694
Ted Kremenekb4cf4a52009-05-03 04:42:10 +0000695 bool isTrackedObjCObjectType(QualType T);
Ted Kremenek4b59ccb2009-05-03 06:08:32 +0000696 bool isTrackedCFObjectType(QualType T);
Mike Stump11289f42009-09-09 15:08:12 +0000697
Ted Kremenekcc3d1882008-10-23 01:56:15 +0000698private:
Mike Stump11289f42009-09-09 15:08:12 +0000699
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000700 void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
701 RetainSummary* Summ) {
702 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
703 }
Mike Stump11289f42009-09-09 15:08:12 +0000704
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000705 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
706 ObjCClassMethodSummaries[S] = Summ;
707 }
Mike Stump11289f42009-09-09 15:08:12 +0000708
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000709 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
710 ObjCMethodSummaries[S] = Summ;
711 }
Ted Kremenek00dfe302009-03-04 23:30:42 +0000712
713 void addClassMethSummary(const char* Cls, const char* nullaryName,
714 RetainSummary *Summ) {
715 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
716 Selector S = GetNullarySelector(nullaryName, Ctx);
717 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
718 }
Mike Stump11289f42009-09-09 15:08:12 +0000719
Ted Kremenekdce78462009-02-25 02:54:57 +0000720 void addInstMethSummary(const char* Cls, const char* nullaryName,
721 RetainSummary *Summ) {
722 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
723 Selector S = GetNullarySelector(nullaryName, Ctx);
724 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
725 }
Mike Stump11289f42009-09-09 15:08:12 +0000726
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000727 Selector generateSelector(va_list argp) {
Ted Kremenek050b91c2008-08-12 18:30:56 +0000728 llvm::SmallVector<IdentifierInfo*, 10> II;
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000729
Ted Kremenek050b91c2008-08-12 18:30:56 +0000730 while (const char* s = va_arg(argp, const char*))
731 II.push_back(&Ctx.Idents.get(s));
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000732
Mike Stump11289f42009-09-09 15:08:12 +0000733 return Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000734 }
Mike Stump11289f42009-09-09 15:08:12 +0000735
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000736 void addMethodSummary(IdentifierInfo *ClsII, ObjCMethodSummariesTy& Summaries,
737 RetainSummary* Summ, va_list argp) {
738 Selector S = generateSelector(argp);
739 Summaries[ObjCSummaryKey(ClsII, S)] = Summ;
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000740 }
Mike Stump11289f42009-09-09 15:08:12 +0000741
Ted Kremenek3f13f592008-08-12 18:48:50 +0000742 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
743 va_list argp;
744 va_start(argp, Summ);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000745 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
Mike Stump11289f42009-09-09 15:08:12 +0000746 va_end(argp);
Ted Kremenek3f13f592008-08-12 18:48:50 +0000747 }
Mike Stump11289f42009-09-09 15:08:12 +0000748
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000749 void addClsMethSummary(const char* Cls, RetainSummary* Summ, ...) {
750 va_list argp;
751 va_start(argp, Summ);
752 addMethodSummary(&Ctx.Idents.get(Cls),ObjCClassMethodSummaries, Summ, argp);
753 va_end(argp);
754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000756 void addClsMethSummary(IdentifierInfo *II, RetainSummary* Summ, ...) {
757 va_list argp;
758 va_start(argp, Summ);
759 addMethodSummary(II, ObjCClassMethodSummaries, Summ, argp);
760 va_end(argp);
761 }
762
Ted Kremenek050b91c2008-08-12 18:30:56 +0000763 void addPanicSummary(const char* Cls, ...) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000764 RetainSummary* Summ = getPersistentSummary(AF.GetEmptyMap(),
765 RetEffect::MakeNoRet(),
Ted Kremenek050b91c2008-08-12 18:30:56 +0000766 DoNothing, DoNothing, true);
767 va_list argp;
768 va_start (argp, Cls);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000769 addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
Ted Kremenek050b91c2008-08-12 18:30:56 +0000770 va_end(argp);
Ted Kremenek8a5ad392009-04-24 17:50:11 +0000771 }
Mike Stump11289f42009-09-09 15:08:12 +0000772
Ted Kremenek819e9b62008-03-11 06:39:11 +0000773public:
Mike Stump11289f42009-09-09 15:08:12 +0000774
Ted Kremenek00daccd2008-05-05 22:11:16 +0000775 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenekab54e512008-07-01 17:21:27 +0000776 : Ctx(ctx),
Ted Kremenekae529272008-07-09 18:11:16 +0000777 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000778 GCEnabled(gcenabled), AF(BPAlloc), ScratchArgs(AF.GetEmptyMap()),
Ted Kremenek9157fbb2009-05-07 23:40:42 +0000779 ObjCAllocRetE(gcenabled ? RetEffect::MakeGCNotOwned()
780 : RetEffect::MakeOwned(RetEffect::ObjC, true)),
Ted Kremenekea675cf2009-06-11 18:17:24 +0000781 ObjCInitRetE(gcenabled ? RetEffect::MakeGCNotOwned()
782 : RetEffect::MakeOwnedWhenTrackedReceiver()),
Ted Kremenekff606a12009-05-04 04:57:00 +0000783 DefaultSummary(AF.GetEmptyMap() /* per-argument effects (none) */,
784 RetEffect::MakeNoRet() /* return effect */,
Ted Kremenekd0e3ab22009-05-11 18:30:24 +0000785 MayEscape, /* default argument effect */
786 DoNothing /* receiver effect */),
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000787 StopSummary(0) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +0000788
789 InitializeClassMethodSummaries();
790 InitializeMethodSummaries();
791 }
Mike Stump11289f42009-09-09 15:08:12 +0000792
Ted Kremenek00daccd2008-05-05 22:11:16 +0000793 ~RetainSummaryManager();
Mike Stump11289f42009-09-09 15:08:12 +0000794
795 RetainSummary* getSummary(FunctionDecl* FD);
796
Ted Kremenek223a7d52009-04-29 23:03:22 +0000797 RetainSummary* getInstanceMethodSummary(ObjCMessageExpr* ME,
798 const ObjCInterfaceDecl* ID) {
Ted Kremenek38724302009-04-29 17:09:14 +0000799 return getInstanceMethodSummary(ME->getSelector(), ME->getClassName(),
Mike Stump11289f42009-09-09 15:08:12 +0000800 ID, ME->getMethodDecl(), ME->getType());
Ted Kremenek0b50fb12009-04-29 05:04:30 +0000801 }
Mike Stump11289f42009-09-09 15:08:12 +0000802
Ted Kremenek38724302009-04-29 17:09:14 +0000803 RetainSummary* getInstanceMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +0000804 const ObjCInterfaceDecl* ID,
805 const ObjCMethodDecl *MD,
806 QualType RetTy);
Ted Kremenek7686ffa2009-04-29 00:42:39 +0000807
808 RetainSummary *getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +0000809 const ObjCInterfaceDecl *ID,
810 const ObjCMethodDecl *MD,
811 QualType RetTy);
Mike Stump11289f42009-09-09 15:08:12 +0000812
Ted Kremenek7686ffa2009-04-29 00:42:39 +0000813 RetainSummary *getClassMethodSummary(ObjCMessageExpr *ME) {
814 return getClassMethodSummary(ME->getSelector(), ME->getClassName(),
815 ME->getClassInfo().first,
816 ME->getMethodDecl(), ME->getType());
817 }
Ted Kremenek99fe1692009-04-29 17:17:48 +0000818
819 /// getMethodSummary - This version of getMethodSummary is used to query
820 /// the summary for the current method being analyzed.
Ted Kremenek223a7d52009-04-29 23:03:22 +0000821 RetainSummary *getMethodSummary(const ObjCMethodDecl *MD) {
822 // FIXME: Eventually this should be unneeded.
Ted Kremenek223a7d52009-04-29 23:03:22 +0000823 const ObjCInterfaceDecl *ID = MD->getClassInterface();
Ted Kremenekb2a143f2009-04-30 05:41:14 +0000824 Selector S = MD->getSelector();
Ted Kremenek99fe1692009-04-29 17:17:48 +0000825 IdentifierInfo *ClsName = ID->getIdentifier();
826 QualType ResultTy = MD->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +0000827
828 // Resolve the method decl last.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000829 if (const ObjCMethodDecl *InterfaceMD = ResolveToInterfaceMethodDecl(MD))
Ted Kremenek497df912009-04-30 05:47:23 +0000830 MD = InterfaceMD;
Mike Stump11289f42009-09-09 15:08:12 +0000831
Ted Kremenek99fe1692009-04-29 17:17:48 +0000832 if (MD->isInstanceMethod())
833 return getInstanceMethodSummary(S, ClsName, ID, MD, ResultTy);
834 else
835 return getClassMethodSummary(S, ClsName, ID, MD, ResultTy);
836 }
Mike Stump11289f42009-09-09 15:08:12 +0000837
Ted Kremenek223a7d52009-04-29 23:03:22 +0000838 RetainSummary* getCommonMethodSummary(const ObjCMethodDecl* MD,
839 Selector S, QualType RetTy);
840
Ted Kremenekc2de7272009-05-09 02:58:13 +0000841 void updateSummaryFromAnnotations(RetainSummary &Summ,
842 const ObjCMethodDecl *MD);
843
844 void updateSummaryFromAnnotations(RetainSummary &Summ,
845 const FunctionDecl *FD);
846
Ted Kremenek00daccd2008-05-05 22:11:16 +0000847 bool isGCEnabled() const { return GCEnabled; }
Mike Stump11289f42009-09-09 15:08:12 +0000848
Ted Kremenek1d9a2672009-05-04 05:31:22 +0000849 RetainSummary *copySummary(RetainSummary *OldSumm) {
850 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
851 new (Summ) RetainSummary(*OldSumm);
852 return Summ;
Mike Stump11289f42009-09-09 15:08:12 +0000853 }
Ted Kremenek819e9b62008-03-11 06:39:11 +0000854};
Mike Stump11289f42009-09-09 15:08:12 +0000855
Ted Kremenek819e9b62008-03-11 06:39:11 +0000856} // end anonymous namespace
857
858//===----------------------------------------------------------------------===//
859// Implementation of checker data structures.
860//===----------------------------------------------------------------------===//
861
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000862RetainSummaryManager::~RetainSummaryManager() {}
Ted Kremenek819e9b62008-03-11 06:39:11 +0000863
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000864ArgEffects RetainSummaryManager::getArgEffects() {
865 ArgEffects AE = ScratchArgs;
866 ScratchArgs = AF.GetEmptyMap();
867 return AE;
Ted Kremenek68d73d12008-03-12 01:21:45 +0000868}
869
Ted Kremenekb0862dc2008-05-06 02:26:56 +0000870RetainSummary*
Ted Kremenek7d79a5f2009-05-03 05:20:50 +0000871RetainSummaryManager::getPersistentSummary(ArgEffects AE, RetEffect RetEff,
Ted Kremenekcb2e6362008-05-06 15:44:25 +0000872 ArgEffect ReceiverEff,
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000873 ArgEffect DefaultEff,
Mike Stump11289f42009-09-09 15:08:12 +0000874 bool isEndPath) {
Ted Kremenekf7141592008-04-24 17:22:33 +0000875 // Create the summary and return it.
Ted Kremenek1bff64e2009-05-04 04:30:18 +0000876 RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek3b2294c2008-07-18 17:24:20 +0000877 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenek68d73d12008-03-12 01:21:45 +0000878 return Summ;
879}
880
Ted Kremenek00daccd2008-05-05 22:11:16 +0000881//===----------------------------------------------------------------------===//
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000882// Predicates.
883//===----------------------------------------------------------------------===//
884
Ted Kremenekb4cf4a52009-05-03 04:42:10 +0000885bool RetainSummaryManager::isTrackedObjCObjectType(QualType Ty) {
Steve Naroff79d12152009-07-16 15:41:00 +0000886 if (!Ty->isObjCObjectPointerType())
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000887 return false;
888
John McCall9dd450b2009-09-21 23:43:11 +0000889 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
Mike Stump11289f42009-09-09 15:08:12 +0000890
Steve Naroff7cae42b2009-07-10 23:34:53 +0000891 // Can be true for objects with the 'NSObject' attribute.
892 if (!PT)
Ted Kremenek37467812009-04-23 22:11:07 +0000893 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000894
Steve Naroff7cae42b2009-07-10 23:34:53 +0000895 // We assume that id<..>, id, and "Class" all represent tracked objects.
896 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
897 PT->isObjCClassType())
898 return true;
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000899
Mike Stump11289f42009-09-09 15:08:12 +0000900 // Does the interface subclass NSObject?
901 // FIXME: We can memoize here if this gets too expensive.
902 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000903
Ted Kremeneke4302ee2009-05-16 01:38:01 +0000904 // Assume that anything declared with a forward declaration and no
905 // @interface subclasses NSObject.
906 if (ID->isForwardDecl())
907 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000908
Ted Kremeneke4302ee2009-05-16 01:38:01 +0000909 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
910
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000911 for ( ; ID ; ID = ID->getSuperClass())
912 if (ID->getIdentifier() == NSObjectII)
913 return true;
Mike Stump11289f42009-09-09 15:08:12 +0000914
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000915 return false;
916}
917
Ted Kremenek4b59ccb2009-05-03 06:08:32 +0000918bool RetainSummaryManager::isTrackedCFObjectType(QualType T) {
919 return isRefType(T, "CF") || // Core Foundation.
920 isRefType(T, "CG") || // Core Graphics.
921 isRefType(T, "DADisk") || // Disk Arbitration API.
922 isRefType(T, "DADissenter") ||
923 isRefType(T, "DASessionRef");
924}
925
Ted Kremenek1d92d2c2009-01-07 00:39:56 +0000926//===----------------------------------------------------------------------===//
Ted Kremenek00daccd2008-05-05 22:11:16 +0000927// Summary creation for functions (largely uses of Core Foundation).
928//===----------------------------------------------------------------------===//
Ted Kremenek68d73d12008-03-12 01:21:45 +0000929
Ted Kremenek7e904222009-01-12 21:45:02 +0000930static bool isRetain(FunctionDecl* FD, const char* FName) {
931 const char* loc = strstr(FName, "Retain");
932 return loc && loc[sizeof("Retain")-1] == '\0';
933}
934
935static bool isRelease(FunctionDecl* FD, const char* FName) {
936 const char* loc = strstr(FName, "Release");
937 return loc && loc[sizeof("Release")-1] == '\0';
938}
939
Ted Kremenekf890bfe2008-06-24 03:56:45 +0000940RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenekf7141592008-04-24 17:22:33 +0000941 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenek00daccd2008-05-05 22:11:16 +0000942 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenek00daccd2008-05-05 22:11:16 +0000943 if (I != FuncSummaries.end())
Ted Kremenekf7141592008-04-24 17:22:33 +0000944 return I->second;
945
Ted Kremenekdf76e6d2009-05-04 15:34:07 +0000946 // No summary? Generate one.
Ted Kremenek7e904222009-01-12 21:45:02 +0000947 RetainSummary *S = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000948
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000949 do {
Ted Kremenek7e904222009-01-12 21:45:02 +0000950 // We generate "stop" summaries for implicitly defined functions.
951 if (FD->isImplicit()) {
952 S = getPersistentStopSummary();
953 break;
Ted Kremenekfa89e2f2008-07-15 16:50:12 +0000954 }
Mike Stump11289f42009-09-09 15:08:12 +0000955
John McCall9dd450b2009-09-21 23:43:11 +0000956 // [PR 3337] Use 'getAs<FunctionType>' to strip away any typedefs on the
Ted Kremenek86afde32009-01-16 18:40:33 +0000957 // function's type.
John McCall9dd450b2009-09-21 23:43:11 +0000958 const FunctionType* FT = FD->getType()->getAs<FunctionType>();
Ted Kremenek7e904222009-01-12 21:45:02 +0000959 const char* FName = FD->getIdentifier()->getName();
Mike Stump11289f42009-09-09 15:08:12 +0000960
Ted Kremenek5f968932009-03-05 22:11:14 +0000961 // Strip away preceding '_'. Doing this here will effect all the checks
962 // down below.
963 while (*FName == '_') ++FName;
Mike Stump11289f42009-09-09 15:08:12 +0000964
Ted Kremenek7e904222009-01-12 21:45:02 +0000965 // Inspect the result type.
966 QualType RetTy = FT->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +0000967
Ted Kremenek7e904222009-01-12 21:45:02 +0000968 // FIXME: This should all be refactored into a chain of "summary lookup"
969 // filters.
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +0000970 assert(ScratchArgs.isEmpty());
971
Ted Kremenekea675cf2009-06-11 18:17:24 +0000972 switch (strlen(FName)) {
973 default: break;
Ted Kremenek80816ac2009-10-13 22:55:33 +0000974 case 14:
975 if (!memcmp(FName, "pthread_create", 14)) {
976 // Part of: <rdar://problem/7299394>. This will be addressed
977 // better with IPA.
978 S = getPersistentStopSummary();
979 }
980 break;
Ted Kremenek3092e9c2009-06-15 20:36:07 +0000981
Ted Kremenekea675cf2009-06-11 18:17:24 +0000982 case 17:
983 // Handle: id NSMakeCollectable(CFTypeRef)
984 if (!memcmp(FName, "NSMakeCollectable", 17)) {
Steve Naroff7cae42b2009-07-10 23:34:53 +0000985 S = (RetTy->isObjCIdType())
Ted Kremenekea675cf2009-06-11 18:17:24 +0000986 ? getUnarySummary(FT, cfmakecollectable)
987 : getPersistentStopSummary();
988 }
Ted Kremenek3092e9c2009-06-15 20:36:07 +0000989 else if (!memcmp(FName, "IOBSDNameMatching", 17) ||
990 !memcmp(FName, "IOServiceMatching", 17)) {
991 // Part of <rdar://problem/6961230>. (IOKit)
992 // This should be addressed using a API table.
993 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
994 DoNothing, DoNothing);
995 }
Ted Kremenekea675cf2009-06-11 18:17:24 +0000996 break;
Ted Kremenek3092e9c2009-06-15 20:36:07 +0000997
998 case 21:
999 if (!memcmp(FName, "IOServiceNameMatching", 21)) {
1000 // Part of <rdar://problem/6961230>. (IOKit)
1001 // This should be addressed using a API table.
1002 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
1003 DoNothing, DoNothing);
1004 }
1005 break;
1006
1007 case 24:
1008 if (!memcmp(FName, "IOServiceAddNotification", 24)) {
1009 // Part of <rdar://problem/6961230>. (IOKit)
1010 // This should be addressed using a API table.
1011 ScratchArgs = AF.Add(ScratchArgs, 2, DecRef);
Ted Kremenek55adb822009-10-15 22:25:12 +00001012 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,DoNothing);
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001013 }
1014 break;
Mike Stump11289f42009-09-09 15:08:12 +00001015
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001016 case 25:
1017 if (!memcmp(FName, "IORegistryEntryIDMatching", 25)) {
1018 // Part of <rdar://problem/6961230>. (IOKit)
1019 // This should be addressed using a API table.
1020 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
1021 DoNothing, DoNothing);
1022 }
1023 break;
Mike Stump11289f42009-09-09 15:08:12 +00001024
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001025 case 26:
1026 if (!memcmp(FName, "IOOpenFirmwarePathMatching", 26)) {
1027 // Part of <rdar://problem/6961230>. (IOKit)
1028 // This should be addressed using a API table.
1029 S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
Mike Stump11289f42009-09-09 15:08:12 +00001030 DoNothing, DoNothing);
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001031 }
1032 break;
1033
Ted Kremenekea675cf2009-06-11 18:17:24 +00001034 case 27:
1035 if (!memcmp(FName, "IOServiceGetMatchingService", 27)) {
1036 // Part of <rdar://problem/6961230>.
1037 // This should be addressed using a API table.
Ted Kremenekea675cf2009-06-11 18:17:24 +00001038 ScratchArgs = AF.Add(ScratchArgs, 1, DecRef);
Mike Stump11289f42009-09-09 15:08:12 +00001039 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenekea675cf2009-06-11 18:17:24 +00001040 }
1041 break;
1042
1043 case 28:
1044 if (!memcmp(FName, "IOServiceGetMatchingServices", 28)) {
1045 // FIXES: <rdar://problem/6326900>
1046 // This should be addressed using a API table. This strcmp is also
1047 // a little gross, but there is no need to super optimize here.
Ted Kremenekea675cf2009-06-11 18:17:24 +00001048 ScratchArgs = AF.Add(ScratchArgs, 1, DecRef);
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +00001049 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1050 DoNothing);
1051 }
1052 else if (!memcmp(FName, "CVPixelBufferCreateWithBytes", 28)) {
1053 // FIXES: <rdar://problem/7283567>
1054 // Eventually this can be improved by recognizing that the pixel
1055 // buffer passed to CVPixelBufferCreateWithBytes is released via
1056 // a callback and doing full IPA to make sure this is done correctly.
1057 ScratchArgs = AF.Add(ScratchArgs, 7, StopTracking);
1058 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1059 DoNothing);
Ted Kremenekea675cf2009-06-11 18:17:24 +00001060 }
1061 break;
Mike Stump11289f42009-09-09 15:08:12 +00001062
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001063 case 32:
1064 if (!memcmp(FName, "IOServiceAddMatchingNotification", 32)) {
1065 // Part of <rdar://problem/6961230>.
1066 // This should be addressed using a API table.
1067 ScratchArgs = AF.Add(ScratchArgs, 2, DecRef);
Mike Stump11289f42009-09-09 15:08:12 +00001068 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenek3092e9c2009-06-15 20:36:07 +00001069 }
1070 break;
Ted Kremenekb4ec3fc2009-10-14 00:27:24 +00001071
1072 case 34:
1073 if (!memcmp(FName, "CVPixelBufferCreateWithPlanarBytes", 34)) {
1074 // FIXES: <rdar://problem/7283567>
1075 // Eventually this can be improved by recognizing that the pixel
1076 // buffer passed to CVPixelBufferCreateWithPlanarBytes is released
1077 // via a callback and doing full IPA to make sure this is done
1078 // correctly.
1079 ScratchArgs = AF.Add(ScratchArgs, 12, StopTracking);
1080 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
1081 DoNothing);
1082 }
1083 break;
Ted Kremenekea675cf2009-06-11 18:17:24 +00001084 }
Mike Stump11289f42009-09-09 15:08:12 +00001085
Ted Kremenekea675cf2009-06-11 18:17:24 +00001086 // Did we get a summary?
1087 if (S)
1088 break;
Ted Kremenek211094d2009-03-17 22:43:44 +00001089
1090 // Enable this code once the semantics of NSDeallocateObject are resolved
1091 // for GC. <rdar://problem/6619988>
1092#if 0
1093 // Handle: NSDeallocateObject(id anObject);
1094 // This method does allow 'nil' (although we don't check it now).
Mike Stump11289f42009-09-09 15:08:12 +00001095 if (strcmp(FName, "NSDeallocateObject") == 0) {
Ted Kremenek211094d2009-03-17 22:43:44 +00001096 return RetTy == Ctx.VoidTy
1097 ? getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, Dealloc)
1098 : getPersistentStopSummary();
1099 }
1100#endif
Ted Kremenek7e904222009-01-12 21:45:02 +00001101
1102 if (RetTy->isPointerType()) {
1103 // For CoreFoundation ('CF') types.
1104 if (isRefType(RetTy, "CF", &Ctx, FName)) {
1105 if (isRetain(FD, FName))
1106 S = getUnarySummary(FT, cfretain);
1107 else if (strstr(FName, "MakeCollectable"))
1108 S = getUnarySummary(FT, cfmakecollectable);
Mike Stump11289f42009-09-09 15:08:12 +00001109 else
Ted Kremenek7e904222009-01-12 21:45:02 +00001110 S = getCFCreateGetRuleSummary(FD, FName);
1111
1112 break;
1113 }
1114
1115 // For CoreGraphics ('CG') types.
1116 if (isRefType(RetTy, "CG", &Ctx, FName)) {
1117 if (isRetain(FD, FName))
1118 S = getUnarySummary(FT, cfretain);
1119 else
1120 S = getCFCreateGetRuleSummary(FD, FName);
1121
1122 break;
1123 }
1124
1125 // For the Disk Arbitration API (DiskArbitration/DADisk.h)
1126 if (isRefType(RetTy, "DADisk") ||
1127 isRefType(RetTy, "DADissenter") ||
1128 isRefType(RetTy, "DASessionRef")) {
1129 S = getCFCreateGetRuleSummary(FD, FName);
1130 break;
1131 }
Mike Stump11289f42009-09-09 15:08:12 +00001132
Ted Kremenek7e904222009-01-12 21:45:02 +00001133 break;
1134 }
1135
1136 // Check for release functions, the only kind of functions that we care
1137 // about that don't return a pointer type.
1138 if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) {
Ted Kremenek5f968932009-03-05 22:11:14 +00001139 // Test for 'CGCF'.
1140 if (FName[1] == 'G' && FName[2] == 'C' && FName[3] == 'F')
1141 FName += 4;
1142 else
1143 FName += 2;
Mike Stump11289f42009-09-09 15:08:12 +00001144
Ted Kremenek5f968932009-03-05 22:11:14 +00001145 if (isRelease(FD, FName))
Ted Kremenek7e904222009-01-12 21:45:02 +00001146 S = getUnarySummary(FT, cfrelease);
1147 else {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001148 assert (ScratchArgs.isEmpty());
Ted Kremeneked90de42009-01-29 22:45:13 +00001149 // Remaining CoreFoundation and CoreGraphics functions.
1150 // We use to assume that they all strictly followed the ownership idiom
1151 // and that ownership cannot be transferred. While this is technically
1152 // correct, many methods allow a tracked object to escape. For example:
1153 //
Mike Stump11289f42009-09-09 15:08:12 +00001154 // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...);
Ted Kremeneked90de42009-01-29 22:45:13 +00001155 // CFDictionaryAddValue(y, key, x);
Mike Stump11289f42009-09-09 15:08:12 +00001156 // CFRelease(x);
Ted Kremeneked90de42009-01-29 22:45:13 +00001157 // ... it is okay to use 'x' since 'y' has a reference to it
1158 //
1159 // We handle this and similar cases with the follow heuristic. If the
Ted Kremenekd982f002009-08-20 00:57:22 +00001160 // function name contains "InsertValue", "SetValue", "AddValue",
1161 // "AppendValue", or "SetAttribute", then we assume that arguments may
1162 // "escape." This means that something else holds on to the object,
1163 // allowing it be used even after its local retain count drops to 0.
Ted Kremeneked90de42009-01-29 22:45:13 +00001164 ArgEffect E = (CStrInCStrNoCase(FName, "InsertValue") ||
1165 CStrInCStrNoCase(FName, "AddValue") ||
Ted Kremenek0ca23d32009-02-05 22:34:53 +00001166 CStrInCStrNoCase(FName, "SetValue") ||
Ted Kremenekd982f002009-08-20 00:57:22 +00001167 CStrInCStrNoCase(FName, "AppendValue") ||
1168 CStrInCStrNoCase(FName, "SetAttribute"))
Ted Kremeneked90de42009-01-29 22:45:13 +00001169 ? MayEscape : DoNothing;
Mike Stump11289f42009-09-09 15:08:12 +00001170
Ted Kremeneked90de42009-01-29 22:45:13 +00001171 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E);
Ted Kremenek7e904222009-01-12 21:45:02 +00001172 }
1173 }
Ted Kremenekfa89e2f2008-07-15 16:50:12 +00001174 }
1175 while (0);
Mike Stump11289f42009-09-09 15:08:12 +00001176
Ted Kremenek1d9a2672009-05-04 05:31:22 +00001177 if (!S)
1178 S = getDefaultSummary();
Ted Kremenekf7141592008-04-24 17:22:33 +00001179
Ted Kremenekc2de7272009-05-09 02:58:13 +00001180 // Annotations override defaults.
1181 assert(S);
1182 updateSummaryFromAnnotations(*S, FD);
Mike Stump11289f42009-09-09 15:08:12 +00001183
Ted Kremenek00daccd2008-05-05 22:11:16 +00001184 FuncSummaries[FD] = S;
Mike Stump11289f42009-09-09 15:08:12 +00001185 return S;
Ted Kremenekea6507f2008-03-06 00:08:09 +00001186}
1187
Ted Kremenekfa89e2f2008-07-15 16:50:12 +00001188RetainSummary*
1189RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
1190 const char* FName) {
Mike Stump11289f42009-09-09 15:08:12 +00001191
Ted Kremenek875db812008-05-05 16:51:50 +00001192 if (strstr(FName, "Create") || strstr(FName, "Copy"))
1193 return getCFSummaryCreateRule(FD);
Mike Stump11289f42009-09-09 15:08:12 +00001194
Ted Kremenek875db812008-05-05 16:51:50 +00001195 if (strstr(FName, "Get"))
1196 return getCFSummaryGetRule(FD);
Mike Stump11289f42009-09-09 15:08:12 +00001197
Ted Kremenekff606a12009-05-04 04:57:00 +00001198 return getDefaultSummary();
Ted Kremenek875db812008-05-05 16:51:50 +00001199}
1200
Ted Kremenek00daccd2008-05-05 22:11:16 +00001201RetainSummary*
Ted Kremenek82157a12009-02-23 16:51:39 +00001202RetainSummaryManager::getUnarySummary(const FunctionType* FT,
1203 UnaryFuncKind func) {
1204
Ted Kremenek7e904222009-01-12 21:45:02 +00001205 // Sanity check that this is *really* a unary function. This can
1206 // happen if people do weird things.
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001207 const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT);
Ted Kremenek7e904222009-01-12 21:45:02 +00001208 if (!FTP || FTP->getNumArgs() != 1)
1209 return getPersistentStopSummary();
Mike Stump11289f42009-09-09 15:08:12 +00001210
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001211 assert (ScratchArgs.isEmpty());
Mike Stump11289f42009-09-09 15:08:12 +00001212
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001213 switch (func) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001214 case cfretain: {
1215 ScratchArgs = AF.Add(ScratchArgs, 0, IncRef);
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00001216 return getPersistentSummary(RetEffect::MakeAlias(0),
1217 DoNothing, DoNothing);
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001218 }
Mike Stump11289f42009-09-09 15:08:12 +00001219
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001220 case cfrelease: {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001221 ScratchArgs = AF.Add(ScratchArgs, 0, DecRef);
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00001222 return getPersistentSummary(RetEffect::MakeNoRet(),
1223 DoNothing, DoNothing);
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001224 }
Mike Stump11289f42009-09-09 15:08:12 +00001225
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001226 case cfmakecollectable: {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001227 ScratchArgs = AF.Add(ScratchArgs, 0, MakeCollectable);
Mike Stump11289f42009-09-09 15:08:12 +00001228 return getPersistentSummary(RetEffect::MakeAlias(0),DoNothing, DoNothing);
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001231 default:
Ted Kremenek875db812008-05-05 16:51:50 +00001232 assert (false && "Not a supported unary function.");
Ted Kremenekff606a12009-05-04 04:57:00 +00001233 return getDefaultSummary();
Ted Kremenek4b772092008-04-10 23:44:06 +00001234 }
Ted Kremenek68d73d12008-03-12 01:21:45 +00001235}
1236
Ted Kremenek00daccd2008-05-05 22:11:16 +00001237RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001238 assert (ScratchArgs.isEmpty());
Mike Stump11289f42009-09-09 15:08:12 +00001239
Ted Kremenekae529272008-07-09 18:11:16 +00001240 if (FD->getIdentifier() == CFDictionaryCreateII) {
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001241 ScratchArgs = AF.Add(ScratchArgs, 1, DoNothingByRef);
1242 ScratchArgs = AF.Add(ScratchArgs, 2, DoNothingByRef);
Ted Kremenekae529272008-07-09 18:11:16 +00001243 }
Mike Stump11289f42009-09-09 15:08:12 +00001244
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001245 return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenek68d73d12008-03-12 01:21:45 +00001246}
1247
Ted Kremenek00daccd2008-05-05 22:11:16 +00001248RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Mike Stump11289f42009-09-09 15:08:12 +00001249 assert (ScratchArgs.isEmpty());
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001250 return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF),
1251 DoNothing, DoNothing);
Ted Kremenek68d73d12008-03-12 01:21:45 +00001252}
1253
Ted Kremenek819e9b62008-03-11 06:39:11 +00001254//===----------------------------------------------------------------------===//
Ted Kremenek00daccd2008-05-05 22:11:16 +00001255// Summary creation for Selectors.
1256//===----------------------------------------------------------------------===//
1257
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001258RetainSummary*
Ted Kremenek0b50fb12009-04-29 05:04:30 +00001259RetainSummaryManager::getInitMethodSummary(QualType RetTy) {
Mike Stump11289f42009-09-09 15:08:12 +00001260 assert(ScratchArgs.isEmpty());
Ted Kremenek1272f702009-05-12 20:06:54 +00001261 // 'init' methods conceptually return a newly allocated object and claim
Mike Stump11289f42009-09-09 15:08:12 +00001262 // the receiver.
Ted Kremenek1272f702009-05-12 20:06:54 +00001263 if (isTrackedObjCObjectType(RetTy) || isTrackedCFObjectType(RetTy))
Ted Kremeneka03705c2009-06-05 23:18:01 +00001264 return getPersistentSummary(ObjCInitRetE, DecRefMsg);
Mike Stump11289f42009-09-09 15:08:12 +00001265
Ted Kremenek1272f702009-05-12 20:06:54 +00001266 return getDefaultSummary();
Ted Kremenek3d1e9722008-05-05 23:55:01 +00001267}
Ted Kremenekc2de7272009-05-09 02:58:13 +00001268
1269void
1270RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1271 const FunctionDecl *FD) {
1272 if (!FD)
1273 return;
1274
Ted Kremenekea675cf2009-06-11 18:17:24 +00001275 QualType RetTy = FD->getResultType();
Mike Stump11289f42009-09-09 15:08:12 +00001276
Ted Kremenekc2de7272009-05-09 02:58:13 +00001277 // Determine if there is a special return effect for this method.
Ted Kremenekea1c2212009-06-05 23:00:33 +00001278 if (isTrackedObjCObjectType(RetTy)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001279 if (FD->getAttr<NSReturnsRetainedAttr>()) {
Ted Kremenekc2de7272009-05-09 02:58:13 +00001280 Summ.setRetEffect(ObjCAllocRetE);
1281 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001282 else if (FD->getAttr<CFReturnsRetainedAttr>()) {
Ted Kremenekea1c2212009-06-05 23:00:33 +00001283 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenekea675cf2009-06-11 18:17:24 +00001284 }
1285 }
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001286 else if (RetTy->getAs<PointerType>()) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001287 if (FD->getAttr<CFReturnsRetainedAttr>()) {
Ted Kremenekc2de7272009-05-09 02:58:13 +00001288 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
1289 }
1290 }
1291}
1292
1293void
1294RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1295 const ObjCMethodDecl *MD) {
1296 if (!MD)
1297 return;
1298
Ted Kremenek0578e432009-07-06 18:30:43 +00001299 bool isTrackedLoc = false;
Mike Stump11289f42009-09-09 15:08:12 +00001300
Ted Kremenekc2de7272009-05-09 02:58:13 +00001301 // Determine if there is a special return effect for this method.
1302 if (isTrackedObjCObjectType(MD->getResultType())) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001303 if (MD->getAttr<NSReturnsRetainedAttr>()) {
Ted Kremenekc2de7272009-05-09 02:58:13 +00001304 Summ.setRetEffect(ObjCAllocRetE);
Ted Kremenek0578e432009-07-06 18:30:43 +00001305 return;
Ted Kremenekc2de7272009-05-09 02:58:13 +00001306 }
Mike Stump11289f42009-09-09 15:08:12 +00001307
Ted Kremenek0578e432009-07-06 18:30:43 +00001308 isTrackedLoc = true;
Ted Kremenekc2de7272009-05-09 02:58:13 +00001309 }
Mike Stump11289f42009-09-09 15:08:12 +00001310
Ted Kremenek0578e432009-07-06 18:30:43 +00001311 if (!isTrackedLoc)
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001312 isTrackedLoc = MD->getResultType()->getAs<PointerType>() != NULL;
Mike Stump11289f42009-09-09 15:08:12 +00001313
Ted Kremenek0578e432009-07-06 18:30:43 +00001314 if (isTrackedLoc && MD->getAttr<CFReturnsRetainedAttr>())
1315 Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenekc2de7272009-05-09 02:58:13 +00001316}
1317
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001318RetainSummary*
Ted Kremenek223a7d52009-04-29 23:03:22 +00001319RetainSummaryManager::getCommonMethodSummary(const ObjCMethodDecl* MD,
1320 Selector S, QualType RetTy) {
Ted Kremenek6a966b22009-04-24 21:56:17 +00001321
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001322 if (MD) {
Ted Kremenek6e86caf2009-04-24 18:00:17 +00001323 // Scan the method decl for 'void*' arguments. These should be treated
1324 // as 'StopTracking' because they are often used with delegates.
1325 // Delegates are a frequent form of false positives with the retain
1326 // count checker.
1327 unsigned i = 0;
1328 for (ObjCMethodDecl::param_iterator I = MD->param_begin(),
1329 E = MD->param_end(); I != E; ++I, ++i)
1330 if (ParmVarDecl *PD = *I) {
1331 QualType Ty = Ctx.getCanonicalType(PD->getType());
1332 if (Ty.getUnqualifiedType() == Ctx.VoidPtrTy)
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001333 ScratchArgs = AF.Add(ScratchArgs, i, StopTracking);
Ted Kremenek6e86caf2009-04-24 18:00:17 +00001334 }
1335 }
Mike Stump11289f42009-09-09 15:08:12 +00001336
Ted Kremenek6a966b22009-04-24 21:56:17 +00001337 // Any special effect for the receiver?
1338 ArgEffect ReceiverEff = DoNothing;
Mike Stump11289f42009-09-09 15:08:12 +00001339
Ted Kremenek6a966b22009-04-24 21:56:17 +00001340 // If one of the arguments in the selector has the keyword 'delegate' we
1341 // should stop tracking the reference count for the receiver. This is
1342 // because the reference count is quite possibly handled by a delegate
1343 // method.
1344 if (S.isKeywordSelector()) {
1345 const std::string &str = S.getAsString();
1346 assert(!str.empty());
1347 if (CStrInCStrNoCase(&str[0], "delegate:")) ReceiverEff = StopTracking;
1348 }
Mike Stump11289f42009-09-09 15:08:12 +00001349
Ted Kremenek60746a02009-04-23 23:08:22 +00001350 // Look for methods that return an owned object.
Mike Stump11289f42009-09-09 15:08:12 +00001351 if (isTrackedObjCObjectType(RetTy)) {
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001352 // EXPERIMENTAL: Assume the Cocoa conventions for all objects returned
1353 // by instance methods.
Ted Kremenek32819772009-05-15 15:49:00 +00001354 RetEffect E = followsFundamentalRule(S)
1355 ? ObjCAllocRetE : RetEffect::MakeNotOwned(RetEffect::ObjC);
Mike Stump11289f42009-09-09 15:08:12 +00001356
1357 return getPersistentSummary(E, ReceiverEff, MayEscape);
Ted Kremenek6e86caf2009-04-24 18:00:17 +00001358 }
Mike Stump11289f42009-09-09 15:08:12 +00001359
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001360 // Look for methods that return an owned core foundation object.
1361 if (isTrackedCFObjectType(RetTy)) {
Ted Kremenek32819772009-05-15 15:49:00 +00001362 RetEffect E = followsFundamentalRule(S)
1363 ? RetEffect::MakeOwned(RetEffect::CF, true)
1364 : RetEffect::MakeNotOwned(RetEffect::CF);
Mike Stump11289f42009-09-09 15:08:12 +00001365
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001366 return getPersistentSummary(E, ReceiverEff, MayEscape);
1367 }
Mike Stump11289f42009-09-09 15:08:12 +00001368
Ted Kremenek4b59ccb2009-05-03 06:08:32 +00001369 if (ScratchArgs.isEmpty() && ReceiverEff == DoNothing)
Ted Kremenekff606a12009-05-04 04:57:00 +00001370 return getDefaultSummary();
Mike Stump11289f42009-09-09 15:08:12 +00001371
Ted Kremenek1d9a2672009-05-04 05:31:22 +00001372 return getPersistentSummary(RetEffect::MakeNoRet(), ReceiverEff, MayEscape);
Ted Kremenek60746a02009-04-23 23:08:22 +00001373}
1374
1375RetainSummary*
Ted Kremenek38724302009-04-29 17:09:14 +00001376RetainSummaryManager::getInstanceMethodSummary(Selector S,
1377 IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +00001378 const ObjCInterfaceDecl* ID,
1379 const ObjCMethodDecl *MD,
Ted Kremenek38724302009-04-29 17:09:14 +00001380 QualType RetTy) {
Ted Kremenekcb2e6362008-05-06 15:44:25 +00001381
Ted Kremenek0b50fb12009-04-29 05:04:30 +00001382 // Look up a summary in our summary cache.
Ted Kremenek8be51382009-07-21 23:27:57 +00001383 RetainSummary *Summ = ObjCMethodSummaries.find(ID, ClsName, S);
Mike Stump11289f42009-09-09 15:08:12 +00001384
Ted Kremenek8be51382009-07-21 23:27:57 +00001385 if (!Summ) {
1386 assert(ScratchArgs.isEmpty());
Mike Stump11289f42009-09-09 15:08:12 +00001387
Ted Kremenek8be51382009-07-21 23:27:57 +00001388 // "initXXX": pass-through for receiver.
1389 if (deriveNamingConvention(S) == InitRule)
1390 Summ = getInitMethodSummary(RetTy);
1391 else
1392 Summ = getCommonMethodSummary(MD, S, RetTy);
Mike Stump11289f42009-09-09 15:08:12 +00001393
Ted Kremenek8be51382009-07-21 23:27:57 +00001394 // Annotations override defaults.
1395 updateSummaryFromAnnotations(*Summ, MD);
Mike Stump11289f42009-09-09 15:08:12 +00001396
Ted Kremenek8be51382009-07-21 23:27:57 +00001397 // Memoize the summary.
1398 ObjCMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1399 }
Mike Stump11289f42009-09-09 15:08:12 +00001400
Ted Kremenekf27110f2009-04-23 19:11:35 +00001401 return Summ;
Ted Kremenek3d1e9722008-05-05 23:55:01 +00001402}
1403
Ted Kremenek767d0742008-05-06 21:26:51 +00001404RetainSummary*
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001405RetainSummaryManager::getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
Ted Kremenek223a7d52009-04-29 23:03:22 +00001406 const ObjCInterfaceDecl *ID,
1407 const ObjCMethodDecl *MD,
1408 QualType RetTy) {
Ted Kremenek8a5ad392009-04-24 17:50:11 +00001409
Ted Kremenek7686ffa2009-04-29 00:42:39 +00001410 assert(ClsName && "Class name must be specified.");
Mike Stump11289f42009-09-09 15:08:12 +00001411 RetainSummary *Summ = ObjCClassMethodSummaries.find(ID, ClsName, S);
1412
Ted Kremenek8be51382009-07-21 23:27:57 +00001413 if (!Summ) {
1414 Summ = getCommonMethodSummary(MD, S, RetTy);
1415 // Annotations override defaults.
1416 updateSummaryFromAnnotations(*Summ, MD);
1417 // Memoize the summary.
1418 ObjCClassMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1419 }
Mike Stump11289f42009-09-09 15:08:12 +00001420
Ted Kremenekf27110f2009-04-23 19:11:35 +00001421 return Summ;
Ted Kremenek767d0742008-05-06 21:26:51 +00001422}
1423
Mike Stump11289f42009-09-09 15:08:12 +00001424void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9157fbb2009-05-07 23:40:42 +00001425 assert(ScratchArgs.isEmpty());
1426 RetainSummary* Summ = getPersistentSummary(ObjCAllocRetE);
Mike Stump11289f42009-09-09 15:08:12 +00001427
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001428 // Create the summaries for "alloc", "new", and "allocWithZone:" for
1429 // NSObject and its derivatives.
1430 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
1431 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
1432 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001433
1434 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek55adb822009-10-15 22:25:12 +00001435 addClassMethSummary("NSAssertionHandler", "currentHandler",
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001436 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC)));
Mike Stump11289f42009-09-09 15:08:12 +00001437
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001438 // Create the [NSAutoreleasePool addObject:] summary.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001439 ScratchArgs = AF.Add(ScratchArgs, 0, Autorelease);
Ted Kremenek55adb822009-10-15 22:25:12 +00001440 addClassMethSummary("NSAutoreleasePool", "addObject",
1441 getPersistentSummary(RetEffect::MakeNoRet(),
1442 DoNothing, Autorelease));
Mike Stump11289f42009-09-09 15:08:12 +00001443
Ted Kremenek8a5ad392009-04-24 17:50:11 +00001444 // Create the summaries for [NSObject performSelector...]. We treat
1445 // these as 'stop tracking' for the arguments because they are often
1446 // used for delegates that can release the object. When we have better
1447 // inter-procedural analysis we can potentially do something better. This
1448 // workaround is to remove false positives.
1449 Summ = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, StopTracking);
1450 IdentifierInfo *NSObjectII = &Ctx.Idents.get("NSObject");
1451 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1452 "afterDelay", NULL);
1453 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1454 "afterDelay", "inModes", NULL);
1455 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1456 "withObject", "waitUntilDone", NULL);
1457 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1458 "withObject", "waitUntilDone", "modes", NULL);
1459 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1460 "withObject", "waitUntilDone", NULL);
1461 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1462 "withObject", "waitUntilDone", "modes", NULL);
1463 addClsMethSummary(NSObjectII, Summ, "performSelectorInBackground",
1464 "withObject", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001465
Ted Kremenekf9fa3cb2009-05-14 21:29:16 +00001466 // Specially handle NSData.
1467 RetainSummary *dataWithBytesNoCopySumm =
1468 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC), DoNothing,
1469 DoNothing);
1470 addClsMethSummary("NSData", dataWithBytesNoCopySumm,
1471 "dataWithBytesNoCopy", "length", NULL);
1472 addClsMethSummary("NSData", dataWithBytesNoCopySumm,
1473 "dataWithBytesNoCopy", "length", "freeWhenDone", NULL);
Ted Kremenek0806f912008-05-06 00:30:21 +00001474}
1475
Ted Kremenekea736c52008-06-23 22:21:20 +00001476void RetainSummaryManager::InitializeMethodSummaries() {
Mike Stump11289f42009-09-09 15:08:12 +00001477
1478 assert (ScratchArgs.isEmpty());
1479
Ted Kremenek767d0742008-05-06 21:26:51 +00001480 // Create the "init" selector. It just acts as a pass-through for the
1481 // receiver.
Mike Stump11289f42009-09-09 15:08:12 +00001482 RetainSummary *InitSumm = getPersistentSummary(ObjCInitRetE, DecRefMsg);
Ted Kremenek815fbb62009-08-20 05:13:36 +00001483 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
1484
1485 // awakeAfterUsingCoder: behaves basically like an 'init' method. It
1486 // claims the receiver and returns a retained object.
1487 addNSObjectMethSummary(GetUnarySelector("awakeAfterUsingCoder", Ctx),
1488 InitSumm);
Mike Stump11289f42009-09-09 15:08:12 +00001489
Ted Kremenek767d0742008-05-06 21:26:51 +00001490 // The next methods are allocators.
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001491 RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE);
Mike Stump11289f42009-09-09 15:08:12 +00001492 RetainSummary *CFAllocSumm =
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001493 getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Mike Stump11289f42009-09-09 15:08:12 +00001494
1495 // Create the "copy" selector.
1496 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), AllocSumm);
Ted Kremenek9551ab62008-08-12 20:41:56 +00001497
Ted Kremenekbe7c56e2008-05-06 00:38:54 +00001498 // Create the "mutableCopy" selector.
Ted Kremenek10369122009-05-20 22:39:57 +00001499 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), AllocSumm);
Mike Stump11289f42009-09-09 15:08:12 +00001500
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001501 // Create the "retain" selector.
Ted Kremenek9157fbb2009-05-07 23:40:42 +00001502 RetEffect E = RetEffect::MakeReceiverAlias();
Ted Kremenek10369122009-05-20 22:39:57 +00001503 RetainSummary *Summ = getPersistentSummary(E, IncRefMsg);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001504 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001505
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001506 // Create the "release" selector.
Ted Kremenekf68490a2009-02-18 18:54:33 +00001507 Summ = getPersistentSummary(E, DecRefMsg);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001508 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001509
Ted Kremenekbcdb4682008-05-07 21:17:39 +00001510 // Create the "drain" selector.
1511 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001512 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001513
Ted Kremenekea072e32009-03-17 19:42:23 +00001514 // Create the -dealloc summary.
1515 Summ = getPersistentSummary(RetEffect::MakeNoRet(), Dealloc);
1516 addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ);
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001517
1518 // Create the "autorelease" selector.
Ted Kremenekc7832092009-01-28 21:44:40 +00001519 Summ = getPersistentSummary(E, Autorelease);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001520 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001521
Ted Kremenek50db3d02009-02-23 17:45:03 +00001522 // Specially handle NSAutoreleasePool.
Ted Kremenekdce78462009-02-25 02:54:57 +00001523 addInstMethSummary("NSAutoreleasePool", "init",
Ted Kremenek50db3d02009-02-23 17:45:03 +00001524 getPersistentSummary(RetEffect::MakeReceiverAlias(),
Ted Kremenekdce78462009-02-25 02:54:57 +00001525 NewAutoreleasePool));
Mike Stump11289f42009-09-09 15:08:12 +00001526
1527 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremeneke73f2822009-02-23 02:51:29 +00001528 // FIXME: For now we opt for false negatives with NSWindow, as these objects
1529 // self-own themselves. However, they only do this once they are displayed.
1530 // Thus, we need to track an NSWindow's display status.
1531 // This is tracked in <rdar://problem/6062711>.
Ted Kremenek00dfe302009-03-04 23:30:42 +00001532 // See also http://llvm.org/bugs/show_bug.cgi?id=3714.
Ted Kremenek1272f702009-05-12 20:06:54 +00001533 RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet(),
1534 StopTracking,
1535 StopTracking);
Mike Stump11289f42009-09-09 15:08:12 +00001536
Ted Kremenek751e7e32009-04-03 19:02:51 +00001537 addClassMethSummary("NSWindow", "alloc", NoTrackYet);
1538
Ted Kremenek00dfe302009-03-04 23:30:42 +00001539#if 0
Ted Kremenek1272f702009-05-12 20:06:54 +00001540 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001541 "styleMask", "backing", "defer", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001542
Ted Kremenek1272f702009-05-12 20:06:54 +00001543 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001544 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek00dfe302009-03-04 23:30:42 +00001545#endif
Mike Stump11289f42009-09-09 15:08:12 +00001546
Ted Kremenek3f13f592008-08-12 18:48:50 +00001547 // For NSPanel (which subclasses NSWindow), allocated objects are not
1548 // self-owned.
Ted Kremenek751e7e32009-04-03 19:02:51 +00001549 // FIXME: For now we don't track NSPanels. object for the same reason
1550 // as for NSWindow objects.
1551 addClassMethSummary("NSPanel", "alloc", NoTrackYet);
Mike Stump11289f42009-09-09 15:08:12 +00001552
Ted Kremenek1272f702009-05-12 20:06:54 +00001553#if 0
1554 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001555 "styleMask", "backing", "defer", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001556
Ted Kremenek1272f702009-05-12 20:06:54 +00001557 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001558 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek1272f702009-05-12 20:06:54 +00001559#endif
Mike Stump11289f42009-09-09 15:08:12 +00001560
Ted Kremenek501ba032009-05-18 23:14:34 +00001561 // Don't track allocated autorelease pools yet, as it is okay to prematurely
1562 // exit a method.
1563 addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001564
Ted Kremenek3b2294c2008-07-18 17:24:20 +00001565 // Create NSAssertionHandler summaries.
Ted Kremenek050b91c2008-08-12 18:30:56 +00001566 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
Mike Stump11289f42009-09-09 15:08:12 +00001567 "lineNumber", "description", NULL);
1568
Ted Kremenek050b91c2008-08-12 18:30:56 +00001569 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1570 "file", "lineNumber", "description", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001571
Ted Kremenek10369122009-05-20 22:39:57 +00001572 // Create summaries QCRenderer/QCView -createSnapShotImageOfType:
1573 addInstMethSummary("QCRenderer", AllocSumm,
1574 "createSnapshotImageOfType", NULL);
1575 addInstMethSummary("QCView", AllocSumm,
1576 "createSnapshotImageOfType", NULL);
1577
Ted Kremenek96aa1462009-06-15 20:58:58 +00001578 // Create summaries for CIContext, 'createCGImage' and
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001579 // 'createCGLayerWithSize'. These objects are CF objects, and are not
1580 // automatically garbage collected.
1581 addInstMethSummary("CIContext", CFAllocSumm,
Ted Kremenek10369122009-05-20 22:39:57 +00001582 "createCGImage", "fromRect", NULL);
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001583 addInstMethSummary("CIContext", CFAllocSumm,
Mike Stump11289f42009-09-09 15:08:12 +00001584 "createCGImage", "fromRect", "format", "colorSpace", NULL);
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001585 addInstMethSummary("CIContext", CFAllocSumm, "createCGLayerWithSize",
Ted Kremenek96aa1462009-06-15 20:58:58 +00001586 "info", NULL);
Ted Kremenekbe7c56e2008-05-06 00:38:54 +00001587}
1588
Ted Kremenek00daccd2008-05-05 22:11:16 +00001589//===----------------------------------------------------------------------===//
Ted Kremenek71454892008-04-16 20:40:59 +00001590// Reference-counting logic (typestate + counts).
Ted Kremenek819e9b62008-03-11 06:39:11 +00001591//===----------------------------------------------------------------------===//
1592
Ted Kremenek819e9b62008-03-11 06:39:11 +00001593namespace {
Mike Stump11289f42009-09-09 15:08:12 +00001594
Ted Kremenekc8bef6a2008-04-09 23:49:11 +00001595class VISIBILITY_HIDDEN RefVal {
Mike Stump11289f42009-09-09 15:08:12 +00001596public:
Ted Kremeneka506fec2008-04-17 18:12:53 +00001597 enum Kind {
Mike Stump11289f42009-09-09 15:08:12 +00001598 Owned = 0, // Owning reference.
1599 NotOwned, // Reference is not owned by still valid (not freed).
Ted Kremeneka506fec2008-04-17 18:12:53 +00001600 Released, // Object has been released.
1601 ReturnedOwned, // Returned object passes ownership to caller.
1602 ReturnedNotOwned, // Return object does not pass ownership to caller.
Ted Kremenekea072e32009-03-17 19:42:23 +00001603 ERROR_START,
1604 ErrorDeallocNotOwned, // -dealloc called on non-owned object.
1605 ErrorDeallocGC, // Calling -dealloc with GC enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001606 ErrorUseAfterRelease, // Object used after released.
Ted Kremeneka506fec2008-04-17 18:12:53 +00001607 ErrorReleaseNotOwned, // Release of an object that was not owned.
Ted Kremenekea072e32009-03-17 19:42:23 +00001608 ERROR_LEAK_START,
Ted Kremenek631ff232008-10-22 23:56:21 +00001609 ErrorLeak, // A memory leak due to excessive reference counts.
Ted Kremenekd35272f2009-05-09 00:10:05 +00001610 ErrorLeakReturned, // A memory leak due to the returning method not having
1611 // the correct naming conventions.
Ted Kremenekdee56e32009-05-10 06:25:57 +00001612 ErrorGCLeakReturned,
1613 ErrorOverAutorelease,
1614 ErrorReturnedNotOwned
Ted Kremeneka506fec2008-04-17 18:12:53 +00001615 };
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001616
Mike Stump11289f42009-09-09 15:08:12 +00001617private:
Ted Kremeneka506fec2008-04-17 18:12:53 +00001618 Kind kind;
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001619 RetEffect::ObjKind okind;
Ted Kremeneka506fec2008-04-17 18:12:53 +00001620 unsigned Cnt;
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001621 unsigned ACnt;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001622 QualType T;
1623
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001624 RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t)
1625 : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {}
Ted Kremenekbf9d8042008-03-11 17:48:22 +00001626
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001627 RefVal(Kind k, unsigned cnt = 0)
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001628 : kind(k), okind(RetEffect::AnyObj), Cnt(cnt), ACnt(0) {}
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001629
Mike Stump11289f42009-09-09 15:08:12 +00001630public:
Ted Kremeneka506fec2008-04-17 18:12:53 +00001631 Kind getKind() const { return kind; }
Mike Stump11289f42009-09-09 15:08:12 +00001632
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001633 RetEffect::ObjKind getObjKind() const { return okind; }
Ted Kremenekbf9d8042008-03-11 17:48:22 +00001634
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001635 unsigned getCount() const { return Cnt; }
1636 unsigned getAutoreleaseCount() const { return ACnt; }
1637 unsigned getCombinedCounts() const { return Cnt + ACnt; }
1638 void clearCounts() { Cnt = 0; ACnt = 0; }
Ted Kremenekd35272f2009-05-09 00:10:05 +00001639 void setCount(unsigned i) { Cnt = i; }
1640 void setAutoreleaseCount(unsigned i) { ACnt = i; }
Mike Stump11289f42009-09-09 15:08:12 +00001641
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001642 QualType getType() const { return T; }
Mike Stump11289f42009-09-09 15:08:12 +00001643
Ted Kremeneka506fec2008-04-17 18:12:53 +00001644 // Useful predicates.
Mike Stump11289f42009-09-09 15:08:12 +00001645
Ted Kremenekea072e32009-03-17 19:42:23 +00001646 static bool isError(Kind k) { return k >= ERROR_START; }
Mike Stump11289f42009-09-09 15:08:12 +00001647
Ted Kremenekea072e32009-03-17 19:42:23 +00001648 static bool isLeak(Kind k) { return k >= ERROR_LEAK_START; }
Mike Stump11289f42009-09-09 15:08:12 +00001649
Ted Kremenek8784a7c2008-04-11 22:25:11 +00001650 bool isOwned() const {
1651 return getKind() == Owned;
1652 }
Mike Stump11289f42009-09-09 15:08:12 +00001653
Ted Kremenekcbf4c612008-04-16 22:32:20 +00001654 bool isNotOwned() const {
1655 return getKind() == NotOwned;
1656 }
Mike Stump11289f42009-09-09 15:08:12 +00001657
Ted Kremeneka506fec2008-04-17 18:12:53 +00001658 bool isReturnedOwned() const {
1659 return getKind() == ReturnedOwned;
1660 }
Mike Stump11289f42009-09-09 15:08:12 +00001661
Ted Kremeneka506fec2008-04-17 18:12:53 +00001662 bool isReturnedNotOwned() const {
1663 return getKind() == ReturnedNotOwned;
1664 }
Mike Stump11289f42009-09-09 15:08:12 +00001665
Ted Kremeneka506fec2008-04-17 18:12:53 +00001666 bool isNonLeakError() const {
1667 Kind k = getKind();
1668 return isError(k) && !isLeak(k);
1669 }
Mike Stump11289f42009-09-09 15:08:12 +00001670
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001671 static RefVal makeOwned(RetEffect::ObjKind o, QualType t,
1672 unsigned Count = 1) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001673 return RefVal(Owned, o, Count, 0, t);
Ted Kremenek3c03d522008-04-10 23:09:18 +00001674 }
Mike Stump11289f42009-09-09 15:08:12 +00001675
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001676 static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t,
1677 unsigned Count = 0) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001678 return RefVal(NotOwned, o, Count, 0, t);
Ted Kremenek3c03d522008-04-10 23:09:18 +00001679 }
Mike Stump11289f42009-09-09 15:08:12 +00001680
Ted Kremeneka506fec2008-04-17 18:12:53 +00001681 // Comparison, profiling, and pretty-printing.
Mike Stump11289f42009-09-09 15:08:12 +00001682
Ted Kremeneka506fec2008-04-17 18:12:53 +00001683 bool operator==(const RefVal& X) const {
Ted Kremenek3978f792009-05-10 05:11:21 +00001684 return kind == X.kind && Cnt == X.Cnt && T == X.T && ACnt == X.ACnt;
Ted Kremeneka506fec2008-04-17 18:12:53 +00001685 }
Mike Stump11289f42009-09-09 15:08:12 +00001686
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001687 RefVal operator-(size_t i) const {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001688 return RefVal(getKind(), getObjKind(), getCount() - i,
1689 getAutoreleaseCount(), getType());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001690 }
Mike Stump11289f42009-09-09 15:08:12 +00001691
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001692 RefVal operator+(size_t i) const {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001693 return RefVal(getKind(), getObjKind(), getCount() + i,
1694 getAutoreleaseCount(), getType());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001695 }
Mike Stump11289f42009-09-09 15:08:12 +00001696
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001697 RefVal operator^(Kind k) const {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001698 return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(),
1699 getType());
1700 }
Mike Stump11289f42009-09-09 15:08:12 +00001701
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001702 RefVal autorelease() const {
1703 return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1,
1704 getType());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001705 }
Mike Stump11289f42009-09-09 15:08:12 +00001706
Ted Kremeneka506fec2008-04-17 18:12:53 +00001707 void Profile(llvm::FoldingSetNodeID& ID) const {
1708 ID.AddInteger((unsigned) kind);
1709 ID.AddInteger(Cnt);
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001710 ID.AddInteger(ACnt);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001711 ID.Add(T);
Ted Kremeneka506fec2008-04-17 18:12:53 +00001712 }
1713
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001714 void print(llvm::raw_ostream& Out) const;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00001715};
Mike Stump11289f42009-09-09 15:08:12 +00001716
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001717void RefVal::print(llvm::raw_ostream& Out) const {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001718 if (!T.isNull())
1719 Out << "Tracked Type:" << T.getAsString() << '\n';
Mike Stump11289f42009-09-09 15:08:12 +00001720
Ted Kremenek2a723e62008-03-11 19:44:10 +00001721 switch (getKind()) {
1722 default: assert(false);
Mike Stump11289f42009-09-09 15:08:12 +00001723 case Owned: {
Ted Kremenek3c03d522008-04-10 23:09:18 +00001724 Out << "Owned";
1725 unsigned cnt = getCount();
1726 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek2a723e62008-03-11 19:44:10 +00001727 break;
Ted Kremenek3c03d522008-04-10 23:09:18 +00001728 }
Mike Stump11289f42009-09-09 15:08:12 +00001729
Ted Kremenek3c03d522008-04-10 23:09:18 +00001730 case NotOwned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00001731 Out << "NotOwned";
Ted Kremenek3c03d522008-04-10 23:09:18 +00001732 unsigned cnt = getCount();
1733 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek2a723e62008-03-11 19:44:10 +00001734 break;
Ted Kremenek3c03d522008-04-10 23:09:18 +00001735 }
Mike Stump11289f42009-09-09 15:08:12 +00001736
1737 case ReturnedOwned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00001738 Out << "ReturnedOwned";
1739 unsigned cnt = getCount();
1740 if (cnt) Out << " (+ " << cnt << ")";
1741 break;
1742 }
Mike Stump11289f42009-09-09 15:08:12 +00001743
Ted Kremeneka506fec2008-04-17 18:12:53 +00001744 case ReturnedNotOwned: {
1745 Out << "ReturnedNotOwned";
1746 unsigned cnt = getCount();
1747 if (cnt) Out << " (+ " << cnt << ")";
1748 break;
1749 }
Mike Stump11289f42009-09-09 15:08:12 +00001750
Ted Kremenek2a723e62008-03-11 19:44:10 +00001751 case Released:
1752 Out << "Released";
1753 break;
Ted Kremenekea072e32009-03-17 19:42:23 +00001754
1755 case ErrorDeallocGC:
1756 Out << "-dealloc (GC)";
1757 break;
Mike Stump11289f42009-09-09 15:08:12 +00001758
Ted Kremenekea072e32009-03-17 19:42:23 +00001759 case ErrorDeallocNotOwned:
1760 Out << "-dealloc (not-owned)";
1761 break;
Mike Stump11289f42009-09-09 15:08:12 +00001762
Ted Kremenekcbf4c612008-04-16 22:32:20 +00001763 case ErrorLeak:
1764 Out << "Leaked";
Mike Stump11289f42009-09-09 15:08:12 +00001765 break;
1766
Ted Kremenek631ff232008-10-22 23:56:21 +00001767 case ErrorLeakReturned:
1768 Out << "Leaked (Bad naming)";
1769 break;
Mike Stump11289f42009-09-09 15:08:12 +00001770
Ted Kremenekdee56e32009-05-10 06:25:57 +00001771 case ErrorGCLeakReturned:
1772 Out << "Leaked (GC-ed at return)";
1773 break;
1774
Ted Kremenek2a723e62008-03-11 19:44:10 +00001775 case ErrorUseAfterRelease:
1776 Out << "Use-After-Release [ERROR]";
1777 break;
Mike Stump11289f42009-09-09 15:08:12 +00001778
Ted Kremenek2a723e62008-03-11 19:44:10 +00001779 case ErrorReleaseNotOwned:
1780 Out << "Release of Not-Owned [ERROR]";
1781 break;
Mike Stump11289f42009-09-09 15:08:12 +00001782
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00001783 case RefVal::ErrorOverAutorelease:
1784 Out << "Over autoreleased";
1785 break;
Mike Stump11289f42009-09-09 15:08:12 +00001786
Ted Kremenekdee56e32009-05-10 06:25:57 +00001787 case RefVal::ErrorReturnedNotOwned:
1788 Out << "Non-owned object returned instead of owned";
1789 break;
Ted Kremenek2a723e62008-03-11 19:44:10 +00001790 }
Mike Stump11289f42009-09-09 15:08:12 +00001791
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001792 if (ACnt) {
1793 Out << " [ARC +" << ACnt << ']';
1794 }
Ted Kremenek2a723e62008-03-11 19:44:10 +00001795}
Mike Stump11289f42009-09-09 15:08:12 +00001796
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001797} // end anonymous namespace
1798
1799//===----------------------------------------------------------------------===//
1800// RefBindings - State used to track object reference counts.
1801//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +00001802
Ted Kremenekd8242f12008-12-05 02:27:51 +00001803typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001804static int RefBIndex = 0;
1805
1806namespace clang {
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001807 template<>
1808 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
Mike Stump11289f42009-09-09 15:08:12 +00001809 static inline void* GDMIndex() { return &RefBIndex; }
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001810 };
1811}
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001812
1813//===----------------------------------------------------------------------===//
Ted Kremenekc52f9392009-02-24 19:15:11 +00001814// AutoreleaseBindings - State used to track objects in autorelease pools.
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001815//===----------------------------------------------------------------------===//
1816
Ted Kremenekc52f9392009-02-24 19:15:11 +00001817typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts;
1818typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents;
1819typedef llvm::ImmutableList<SymbolRef> ARStack;
Ted Kremenek50db3d02009-02-23 17:45:03 +00001820
Ted Kremenekc52f9392009-02-24 19:15:11 +00001821static int AutoRCIndex = 0;
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001822static int AutoRBIndex = 0;
1823
Ted Kremenekc52f9392009-02-24 19:15:11 +00001824namespace { class VISIBILITY_HIDDEN AutoreleasePoolContents {}; }
Ted Kremenekdce78462009-02-25 02:54:57 +00001825namespace { class VISIBILITY_HIDDEN AutoreleaseStack {}; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001826
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001827namespace clang {
Ted Kremenekdce78462009-02-25 02:54:57 +00001828template<> struct GRStateTrait<AutoreleaseStack>
Ted Kremenekc52f9392009-02-24 19:15:11 +00001829 : public GRStatePartialTrait<ARStack> {
Mike Stump11289f42009-09-09 15:08:12 +00001830 static inline void* GDMIndex() { return &AutoRBIndex; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001831};
1832
1833template<> struct GRStateTrait<AutoreleasePoolContents>
1834 : public GRStatePartialTrait<ARPoolContents> {
Mike Stump11289f42009-09-09 15:08:12 +00001835 static inline void* GDMIndex() { return &AutoRCIndex; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001836};
1837} // end clang namespace
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001838
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001839static SymbolRef GetCurrentAutoreleasePool(const GRState* state) {
1840 ARStack stack = state->get<AutoreleaseStack>();
1841 return stack.isEmpty() ? SymbolRef() : stack.getHead();
1842}
1843
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001844static const GRState * SendAutorelease(const GRState *state,
1845 ARCounts::Factory &F, SymbolRef sym) {
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001846
1847 SymbolRef pool = GetCurrentAutoreleasePool(state);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001848 const ARCounts *cnts = state->get<AutoreleasePoolContents>(pool);
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001849 ARCounts newCnts(0);
Mike Stump11289f42009-09-09 15:08:12 +00001850
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001851 if (cnts) {
1852 const unsigned *cnt = (*cnts).lookup(sym);
1853 newCnts = F.Add(*cnts, sym, cnt ? *cnt + 1 : 1);
1854 }
1855 else
1856 newCnts = F.Add(F.GetEmptyMap(), sym, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001857
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001858 return state->set<AutoreleasePoolContents>(pool, newCnts);
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001859}
1860
Ted Kremenek71454892008-04-16 20:40:59 +00001861//===----------------------------------------------------------------------===//
1862// Transfer functions.
1863//===----------------------------------------------------------------------===//
1864
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001865namespace {
Mike Stump11289f42009-09-09 15:08:12 +00001866
Ted Kremenek1642bda2009-06-26 00:05:51 +00001867class VISIBILITY_HIDDEN CFRefCount : public GRTransferFuncs {
Ted Kremenek396f4362008-04-18 03:39:05 +00001868public:
Ted Kremenek16306102008-08-13 21:24:49 +00001869 class BindingsPrinter : public GRState::Printer {
Ted Kremenek2a723e62008-03-11 19:44:10 +00001870 public:
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001871 virtual void Print(llvm::raw_ostream& Out, const GRState* state,
Ted Kremenek16306102008-08-13 21:24:49 +00001872 const char* nl, const char* sep);
Ted Kremenek2a723e62008-03-11 19:44:10 +00001873 };
Ted Kremenek396f4362008-04-18 03:39:05 +00001874
1875private:
Zhongxing Xu107f7592009-08-06 12:48:26 +00001876 typedef llvm::DenseMap<const ExplodedNode*, const RetainSummary*>
Mike Stump11289f42009-09-09 15:08:12 +00001877 SummaryLogTy;
Ted Kremenek48d16452009-02-18 03:48:14 +00001878
Mike Stump11289f42009-09-09 15:08:12 +00001879 RetainSummaryManager Summaries;
Ted Kremenek48d16452009-02-18 03:48:14 +00001880 SummaryLogTy SummaryLog;
Ted Kremenek00daccd2008-05-05 22:11:16 +00001881 const LangOptions& LOpts;
Ted Kremenekc52f9392009-02-24 19:15:11 +00001882 ARCounts::Factory ARCountFactory;
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001883
Ted Kremenek400aae72009-02-05 06:50:21 +00001884 BugType *useAfterRelease, *releaseNotOwned;
Ted Kremenekea072e32009-03-17 19:42:23 +00001885 BugType *deallocGC, *deallocNotOwned;
Ted Kremenek400aae72009-02-05 06:50:21 +00001886 BugType *leakWithinFunction, *leakAtReturn;
Ted Kremenekd35272f2009-05-09 00:10:05 +00001887 BugType *overAutorelease;
Ted Kremenekdee56e32009-05-10 06:25:57 +00001888 BugType *returnNotOwnedForOwned;
Ted Kremenek400aae72009-02-05 06:50:21 +00001889 BugReporter *BR;
Mike Stump11289f42009-09-09 15:08:12 +00001890
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001891 const GRState * Update(const GRState * state, SymbolRef sym, RefVal V, ArgEffect E,
Ted Kremenekc52f9392009-02-24 19:15:11 +00001892 RefVal::Kind& hasErr);
1893
Zhongxing Xu20227f72009-08-06 01:32:16 +00001894 void ProcessNonLeakError(ExplodedNodeSet& Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001895 GRStmtNodeBuilder& Builder,
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00001896 Expr* NodeExpr, Expr* ErrorExpr,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001897 ExplodedNode* Pred,
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +00001898 const GRState* St,
Ted Kremenekd8242f12008-12-05 02:27:51 +00001899 RefVal::Kind hasErr, SymbolRef Sym);
Mike Stump11289f42009-09-09 15:08:12 +00001900
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001901 const GRState * HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek884a8992009-05-08 23:09:42 +00001902 llvm::SmallVectorImpl<SymbolRef> &Leaked);
Mike Stump11289f42009-09-09 15:08:12 +00001903
Zhongxing Xu20227f72009-08-06 01:32:16 +00001904 ExplodedNode* ProcessLeaks(const GRState * state,
Ted Kremenek884a8992009-05-08 23:09:42 +00001905 llvm::SmallVectorImpl<SymbolRef> &Leaked,
1906 GenericNodeBuilder &Builder,
1907 GRExprEngine &Eng,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001908 ExplodedNode *Pred = 0);
Mike Stump11289f42009-09-09 15:08:12 +00001909
1910public:
Ted Kremenek1f352db2008-07-22 16:21:24 +00001911 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001912 : Summaries(Ctx, gcenabled),
Ted Kremenekea072e32009-03-17 19:42:23 +00001913 LOpts(lopts), useAfterRelease(0), releaseNotOwned(0),
1914 deallocGC(0), deallocNotOwned(0),
Ted Kremenekdee56e32009-05-10 06:25:57 +00001915 leakWithinFunction(0), leakAtReturn(0), overAutorelease(0),
1916 returnNotOwnedForOwned(0), BR(0) {}
Mike Stump11289f42009-09-09 15:08:12 +00001917
Ted Kremenek400aae72009-02-05 06:50:21 +00001918 virtual ~CFRefCount() {}
Mike Stump11289f42009-09-09 15:08:12 +00001919
Ted Kremenekfc5d0672009-02-04 23:49:09 +00001920 void RegisterChecks(BugReporter &BR);
Mike Stump11289f42009-09-09 15:08:12 +00001921
Ted Kremenekceba6ea2008-08-16 00:49:49 +00001922 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1923 Printers.push_back(new BindingsPrinter());
Ted Kremenek2a723e62008-03-11 19:44:10 +00001924 }
Mike Stump11289f42009-09-09 15:08:12 +00001925
Ted Kremenek00daccd2008-05-05 22:11:16 +00001926 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenekb0f87c42008-04-30 23:47:44 +00001927 const LangOptions& getLangOptions() const { return LOpts; }
Mike Stump11289f42009-09-09 15:08:12 +00001928
Zhongxing Xu20227f72009-08-06 01:32:16 +00001929 const RetainSummary *getSummaryOfNode(const ExplodedNode *N) const {
Ted Kremenek48d16452009-02-18 03:48:14 +00001930 SummaryLogTy::const_iterator I = SummaryLog.find(N);
1931 return I == SummaryLog.end() ? 0 : I->second;
1932 }
Mike Stump11289f42009-09-09 15:08:12 +00001933
Ted Kremenek819e9b62008-03-11 06:39:11 +00001934 // Calls.
Ted Kremenek00daccd2008-05-05 22:11:16 +00001935
Zhongxing Xu20227f72009-08-06 01:32:16 +00001936 void EvalSummary(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00001937 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001938 GRStmtNodeBuilder& Builder,
Ted Kremenek00daccd2008-05-05 22:11:16 +00001939 Expr* Ex,
1940 Expr* Receiver,
Ted Kremenekff606a12009-05-04 04:57:00 +00001941 const RetainSummary& Summ,
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00001942 ExprIterator arg_beg, ExprIterator arg_end,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001943 ExplodedNode* Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001944
Zhongxing Xu20227f72009-08-06 01:32:16 +00001945 virtual void EvalCall(ExplodedNodeSet& Dst,
Ted Kremenek626bd2d2008-03-12 21:06:49 +00001946 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001947 GRStmtNodeBuilder& Builder,
Zhongxing Xu27f17422008-10-17 05:57:07 +00001948 CallExpr* CE, SVal L,
Mike Stump11289f42009-09-09 15:08:12 +00001949 ExplodedNode* Pred);
1950
1951
Zhongxing Xu20227f72009-08-06 01:32:16 +00001952 virtual void EvalObjCMessageExpr(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001953 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001954 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001955 ObjCMessageExpr* ME,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001956 ExplodedNode* Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001957
Zhongxing Xu20227f72009-08-06 01:32:16 +00001958 bool EvalObjCMessageExprAux(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001959 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001960 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001961 ObjCMessageExpr* ME,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001962 ExplodedNode* Pred);
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001963
Mike Stump11289f42009-09-09 15:08:12 +00001964 // Stores.
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00001965 virtual void EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val);
1966
Ted Kremenek8784a7c2008-04-11 22:25:11 +00001967 // End-of-path.
Mike Stump11289f42009-09-09 15:08:12 +00001968
Ted Kremenek8784a7c2008-04-11 22:25:11 +00001969 virtual void EvalEndPath(GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001970 GREndPathNodeBuilder& Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001971
Zhongxing Xu20227f72009-08-06 01:32:16 +00001972 virtual void EvalDeadSymbols(ExplodedNodeSet& Dst,
Ted Kremenekb0daf2f2008-04-24 23:57:27 +00001973 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001974 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001975 ExplodedNode* Pred,
Ted Kremenek16fbfe62009-01-21 22:26:05 +00001976 Stmt* S, const GRState* state,
1977 SymbolReaper& SymReaper);
Mike Stump11289f42009-09-09 15:08:12 +00001978
Zhongxing Xu20227f72009-08-06 01:32:16 +00001979 std::pair<ExplodedNode*, const GRState *>
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001980 HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001981 ExplodedNode* Pred, GRExprEngine &Eng,
Ted Kremenekd35272f2009-05-09 00:10:05 +00001982 SymbolRef Sym, RefVal V, bool &stop);
Ted Kremeneka506fec2008-04-17 18:12:53 +00001983 // Return statements.
Mike Stump11289f42009-09-09 15:08:12 +00001984
Zhongxing Xu20227f72009-08-06 01:32:16 +00001985 virtual void EvalReturn(ExplodedNodeSet& Dst,
Ted Kremeneka506fec2008-04-17 18:12:53 +00001986 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001987 GRStmtNodeBuilder& Builder,
Ted Kremeneka506fec2008-04-17 18:12:53 +00001988 ReturnStmt* S,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001989 ExplodedNode* Pred);
Ted Kremenek4d837282008-04-18 19:23:43 +00001990
1991 // Assumptions.
1992
Ted Kremenekf9906842009-06-18 22:57:13 +00001993 virtual const GRState *EvalAssume(const GRState* state, SVal condition,
1994 bool assumption);
Ted Kremenek819e9b62008-03-11 06:39:11 +00001995};
1996
1997} // end anonymous namespace
1998
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001999static void PrintPool(llvm::raw_ostream &Out, SymbolRef Sym,
2000 const GRState *state) {
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002001 Out << ' ';
Ted Kremenek3e31c262009-03-26 03:35:11 +00002002 if (Sym)
2003 Out << Sym->getSymbolID();
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002004 else
2005 Out << "<pool>";
2006 Out << ":{";
Mike Stump11289f42009-09-09 15:08:12 +00002007
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002008 // Get the contents of the pool.
2009 if (const ARCounts *cnts = state->get<AutoreleasePoolContents>(Sym))
2010 for (ARCounts::iterator J=cnts->begin(), EJ=cnts->end(); J != EJ; ++J)
2011 Out << '(' << J.getKey() << ',' << J.getData() << ')';
2012
Mike Stump11289f42009-09-09 15:08:12 +00002013 Out << '}';
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002014}
Ted Kremenek396f4362008-04-18 03:39:05 +00002015
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002016void CFRefCount::BindingsPrinter::Print(llvm::raw_ostream& Out,
2017 const GRState* state,
Ted Kremenek16306102008-08-13 21:24:49 +00002018 const char* nl, const char* sep) {
Mike Stump11289f42009-09-09 15:08:12 +00002019
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00002020 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00002021
Ted Kremenek16306102008-08-13 21:24:49 +00002022 if (!B.isEmpty())
Ted Kremenek2a723e62008-03-11 19:44:10 +00002023 Out << sep << nl;
Mike Stump11289f42009-09-09 15:08:12 +00002024
Ted Kremenek2a723e62008-03-11 19:44:10 +00002025 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
2026 Out << (*I).first << " : ";
2027 (*I).second.print(Out);
2028 Out << nl;
2029 }
Mike Stump11289f42009-09-09 15:08:12 +00002030
Ted Kremenekdce78462009-02-25 02:54:57 +00002031 // Print the autorelease stack.
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002032 Out << sep << nl << "AR pool stack:";
Ted Kremenekdce78462009-02-25 02:54:57 +00002033 ARStack stack = state->get<AutoreleaseStack>();
Mike Stump11289f42009-09-09 15:08:12 +00002034
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002035 PrintPool(Out, SymbolRef(), state); // Print the caller's pool.
2036 for (ARStack::iterator I=stack.begin(), E=stack.end(); I!=E; ++I)
2037 PrintPool(Out, *I, state);
2038
2039 Out << nl;
Ted Kremenek2a723e62008-03-11 19:44:10 +00002040}
2041
Ted Kremenek6bd78702009-04-29 18:50:19 +00002042//===----------------------------------------------------------------------===//
2043// Error reporting.
2044//===----------------------------------------------------------------------===//
2045
2046namespace {
Mike Stump11289f42009-09-09 15:08:12 +00002047
Ted Kremenek6bd78702009-04-29 18:50:19 +00002048 //===-------------===//
2049 // Bug Descriptions. //
Mike Stump11289f42009-09-09 15:08:12 +00002050 //===-------------===//
2051
Ted Kremenek6bd78702009-04-29 18:50:19 +00002052 class VISIBILITY_HIDDEN CFRefBug : public BugType {
2053 protected:
2054 CFRefCount& TF;
Mike Stump11289f42009-09-09 15:08:12 +00002055
2056 CFRefBug(CFRefCount* tf, const char* name)
2057 : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {}
Ted Kremenek6bd78702009-04-29 18:50:19 +00002058 public:
Mike Stump11289f42009-09-09 15:08:12 +00002059
Ted Kremenek6bd78702009-04-29 18:50:19 +00002060 CFRefCount& getTF() { return TF; }
2061 const CFRefCount& getTF() const { return TF; }
Mike Stump11289f42009-09-09 15:08:12 +00002062
Ted Kremenek6bd78702009-04-29 18:50:19 +00002063 // FIXME: Eventually remove.
2064 virtual const char* getDescription() const = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002065
Ted Kremenek6bd78702009-04-29 18:50:19 +00002066 virtual bool isLeak() const { return false; }
2067 };
Mike Stump11289f42009-09-09 15:08:12 +00002068
Ted Kremenek6bd78702009-04-29 18:50:19 +00002069 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2070 public:
2071 UseAfterRelease(CFRefCount* tf)
2072 : CFRefBug(tf, "Use-after-release") {}
Mike Stump11289f42009-09-09 15:08:12 +00002073
Ted Kremenek6bd78702009-04-29 18:50:19 +00002074 const char* getDescription() const {
2075 return "Reference-counted object is used after it is released";
Mike Stump11289f42009-09-09 15:08:12 +00002076 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002077 };
Mike Stump11289f42009-09-09 15:08:12 +00002078
Ted Kremenek6bd78702009-04-29 18:50:19 +00002079 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2080 public:
2081 BadRelease(CFRefCount* tf) : CFRefBug(tf, "Bad release") {}
Mike Stump11289f42009-09-09 15:08:12 +00002082
Ted Kremenek6bd78702009-04-29 18:50:19 +00002083 const char* getDescription() const {
Ted Kremenek5c22e112009-10-01 17:31:50 +00002084 return "Incorrect decrement of the reference count of an object that is "
2085 "not owned at this point by the caller";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002086 }
2087 };
Mike Stump11289f42009-09-09 15:08:12 +00002088
Ted Kremenek6bd78702009-04-29 18:50:19 +00002089 class VISIBILITY_HIDDEN DeallocGC : public CFRefBug {
2090 public:
Ted Kremenekd35272f2009-05-09 00:10:05 +00002091 DeallocGC(CFRefCount *tf)
2092 : CFRefBug(tf, "-dealloc called while using garbage collection") {}
Mike Stump11289f42009-09-09 15:08:12 +00002093
Ted Kremenek6bd78702009-04-29 18:50:19 +00002094 const char *getDescription() const {
Ted Kremenekd35272f2009-05-09 00:10:05 +00002095 return "-dealloc called while using garbage collection";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002096 }
2097 };
Mike Stump11289f42009-09-09 15:08:12 +00002098
Ted Kremenek6bd78702009-04-29 18:50:19 +00002099 class VISIBILITY_HIDDEN DeallocNotOwned : public CFRefBug {
2100 public:
Ted Kremenekd35272f2009-05-09 00:10:05 +00002101 DeallocNotOwned(CFRefCount *tf)
2102 : CFRefBug(tf, "-dealloc sent to non-exclusively owned object") {}
Mike Stump11289f42009-09-09 15:08:12 +00002103
Ted Kremenek6bd78702009-04-29 18:50:19 +00002104 const char *getDescription() const {
2105 return "-dealloc sent to object that may be referenced elsewhere";
2106 }
Mike Stump11289f42009-09-09 15:08:12 +00002107 };
2108
Ted Kremenekd35272f2009-05-09 00:10:05 +00002109 class VISIBILITY_HIDDEN OverAutorelease : public CFRefBug {
2110 public:
Mike Stump11289f42009-09-09 15:08:12 +00002111 OverAutorelease(CFRefCount *tf) :
Ted Kremenekd35272f2009-05-09 00:10:05 +00002112 CFRefBug(tf, "Object sent -autorelease too many times") {}
Mike Stump11289f42009-09-09 15:08:12 +00002113
Ted Kremenekd35272f2009-05-09 00:10:05 +00002114 const char *getDescription() const {
Ted Kremenek3978f792009-05-10 05:11:21 +00002115 return "Object sent -autorelease too many times";
Ted Kremenekd35272f2009-05-09 00:10:05 +00002116 }
2117 };
Mike Stump11289f42009-09-09 15:08:12 +00002118
Ted Kremenekdee56e32009-05-10 06:25:57 +00002119 class VISIBILITY_HIDDEN ReturnedNotOwnedForOwned : public CFRefBug {
2120 public:
2121 ReturnedNotOwnedForOwned(CFRefCount *tf) :
2122 CFRefBug(tf, "Method should return an owned object") {}
Mike Stump11289f42009-09-09 15:08:12 +00002123
Ted Kremenekdee56e32009-05-10 06:25:57 +00002124 const char *getDescription() const {
2125 return "Object with +0 retain counts returned to caller where a +1 "
2126 "(owning) retain count is expected";
2127 }
2128 };
Mike Stump11289f42009-09-09 15:08:12 +00002129
Ted Kremenek6bd78702009-04-29 18:50:19 +00002130 class VISIBILITY_HIDDEN Leak : public CFRefBug {
2131 const bool isReturn;
2132 protected:
2133 Leak(CFRefCount* tf, const char* name, bool isRet)
2134 : CFRefBug(tf, name), isReturn(isRet) {}
2135 public:
Mike Stump11289f42009-09-09 15:08:12 +00002136
Ted Kremenek6bd78702009-04-29 18:50:19 +00002137 const char* getDescription() const { return ""; }
Mike Stump11289f42009-09-09 15:08:12 +00002138
Ted Kremenek6bd78702009-04-29 18:50:19 +00002139 bool isLeak() const { return true; }
2140 };
Mike Stump11289f42009-09-09 15:08:12 +00002141
Ted Kremenek6bd78702009-04-29 18:50:19 +00002142 class VISIBILITY_HIDDEN LeakAtReturn : public Leak {
2143 public:
2144 LeakAtReturn(CFRefCount* tf, const char* name)
2145 : Leak(tf, name, true) {}
2146 };
Mike Stump11289f42009-09-09 15:08:12 +00002147
Ted Kremenek6bd78702009-04-29 18:50:19 +00002148 class VISIBILITY_HIDDEN LeakWithinFunction : public Leak {
2149 public:
2150 LeakWithinFunction(CFRefCount* tf, const char* name)
2151 : Leak(tf, name, false) {}
Mike Stump11289f42009-09-09 15:08:12 +00002152 };
2153
Ted Kremenek6bd78702009-04-29 18:50:19 +00002154 //===---------===//
2155 // Bug Reports. //
2156 //===---------===//
Mike Stump11289f42009-09-09 15:08:12 +00002157
Ted Kremenek6bd78702009-04-29 18:50:19 +00002158 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
2159 protected:
2160 SymbolRef Sym;
2161 const CFRefCount &TF;
2162 public:
2163 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002164 ExplodedNode *n, SymbolRef sym)
Ted Kremenek3978f792009-05-10 05:11:21 +00002165 : RangedBugReport(D, D.getDescription(), n), Sym(sym), TF(tf) {}
2166
2167 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002168 ExplodedNode *n, SymbolRef sym, const char* endText)
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00002169 : RangedBugReport(D, D.getDescription(), endText, n), Sym(sym), TF(tf) {}
Mike Stump11289f42009-09-09 15:08:12 +00002170
Ted Kremenek6bd78702009-04-29 18:50:19 +00002171 virtual ~CFRefReport() {}
Mike Stump11289f42009-09-09 15:08:12 +00002172
Ted Kremenek6bd78702009-04-29 18:50:19 +00002173 CFRefBug& getBugType() {
2174 return (CFRefBug&) RangedBugReport::getBugType();
2175 }
2176 const CFRefBug& getBugType() const {
2177 return (const CFRefBug&) RangedBugReport::getBugType();
2178 }
Mike Stump11289f42009-09-09 15:08:12 +00002179
Zhongxing Xu7864b9ea2009-08-18 08:58:41 +00002180 virtual void getRanges(const SourceRange*& beg, const SourceRange*& end) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002181 if (!getBugType().isLeak())
Zhongxing Xu7864b9ea2009-08-18 08:58:41 +00002182 RangedBugReport::getRanges(beg, end);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002183 else
2184 beg = end = 0;
2185 }
Mike Stump11289f42009-09-09 15:08:12 +00002186
Ted Kremenek6bd78702009-04-29 18:50:19 +00002187 SymbolRef getSymbol() const { return Sym; }
Mike Stump11289f42009-09-09 15:08:12 +00002188
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002189 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002190 const ExplodedNode* N);
Mike Stump11289f42009-09-09 15:08:12 +00002191
Ted Kremenek6bd78702009-04-29 18:50:19 +00002192 std::pair<const char**,const char**> getExtraDescriptiveText();
Mike Stump11289f42009-09-09 15:08:12 +00002193
Zhongxing Xu20227f72009-08-06 01:32:16 +00002194 PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
2195 const ExplodedNode* PrevN,
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002196 BugReporterContext& BRC);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002197 };
Ted Kremenek3978f792009-05-10 05:11:21 +00002198
Ted Kremenek6bd78702009-04-29 18:50:19 +00002199 class VISIBILITY_HIDDEN CFRefLeakReport : public CFRefReport {
2200 SourceLocation AllocSite;
2201 const MemRegion* AllocBinding;
2202 public:
2203 CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002204 ExplodedNode *n, SymbolRef sym,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002205 GRExprEngine& Eng);
Mike Stump11289f42009-09-09 15:08:12 +00002206
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002207 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002208 const ExplodedNode* N);
Mike Stump11289f42009-09-09 15:08:12 +00002209
Ted Kremenek6bd78702009-04-29 18:50:19 +00002210 SourceLocation getLocation() const { return AllocSite; }
Mike Stump11289f42009-09-09 15:08:12 +00002211 };
Ted Kremenek6bd78702009-04-29 18:50:19 +00002212} // end anonymous namespace
2213
2214void CFRefCount::RegisterChecks(BugReporter& BR) {
2215 useAfterRelease = new UseAfterRelease(this);
2216 BR.Register(useAfterRelease);
Mike Stump11289f42009-09-09 15:08:12 +00002217
Ted Kremenek6bd78702009-04-29 18:50:19 +00002218 releaseNotOwned = new BadRelease(this);
2219 BR.Register(releaseNotOwned);
Mike Stump11289f42009-09-09 15:08:12 +00002220
Ted Kremenek6bd78702009-04-29 18:50:19 +00002221 deallocGC = new DeallocGC(this);
2222 BR.Register(deallocGC);
Mike Stump11289f42009-09-09 15:08:12 +00002223
Ted Kremenek6bd78702009-04-29 18:50:19 +00002224 deallocNotOwned = new DeallocNotOwned(this);
2225 BR.Register(deallocNotOwned);
Mike Stump11289f42009-09-09 15:08:12 +00002226
Ted Kremenekd35272f2009-05-09 00:10:05 +00002227 overAutorelease = new OverAutorelease(this);
2228 BR.Register(overAutorelease);
Mike Stump11289f42009-09-09 15:08:12 +00002229
Ted Kremenekdee56e32009-05-10 06:25:57 +00002230 returnNotOwnedForOwned = new ReturnedNotOwnedForOwned(this);
2231 BR.Register(returnNotOwnedForOwned);
Mike Stump11289f42009-09-09 15:08:12 +00002232
Ted Kremenek6bd78702009-04-29 18:50:19 +00002233 // First register "return" leaks.
2234 const char* name = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002235
Ted Kremenek6bd78702009-04-29 18:50:19 +00002236 if (isGCEnabled())
2237 name = "Leak of returned object when using garbage collection";
2238 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
2239 name = "Leak of returned 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 of returned object";
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 a sink.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002247 leakAtReturn = new LeakAtReturn(this, name);
Ted Kremenek41129692009-09-14 22:01:32 +00002248 leakAtReturn->setSuppressOnSink(true);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002249 BR.Register(leakAtReturn);
Mike Stump11289f42009-09-09 15:08:12 +00002250
Ted Kremenek6bd78702009-04-29 18:50:19 +00002251 // Second, register leaks within a function/method.
2252 if (isGCEnabled())
Mike Stump11289f42009-09-09 15:08:12 +00002253 name = "Leak of object when using garbage collection";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002254 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
2255 name = "Leak of object when not using garbage collection (GC) in "
2256 "dual GC/non-GC code";
2257 else {
2258 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
2259 name = "Leak";
2260 }
Mike Stump11289f42009-09-09 15:08:12 +00002261
Ted Kremenek41129692009-09-14 22:01:32 +00002262 // Leaks should not be reported if they are post-dominated by sinks.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002263 leakWithinFunction = new LeakWithinFunction(this, name);
Ted Kremenek41129692009-09-14 22:01:32 +00002264 leakWithinFunction->setSuppressOnSink(true);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002265 BR.Register(leakWithinFunction);
Mike Stump11289f42009-09-09 15:08:12 +00002266
Ted Kremenek6bd78702009-04-29 18:50:19 +00002267 // Save the reference to the BugReporter.
2268 this->BR = &BR;
2269}
2270
2271static const char* Msgs[] = {
2272 // GC only
Mike Stump11289f42009-09-09 15:08:12 +00002273 "Code is compiled to only use garbage collection",
Ted Kremenek6bd78702009-04-29 18:50:19 +00002274 // No GC.
2275 "Code is compiled to use reference counts",
2276 // Hybrid, with GC.
2277 "Code is compiled to use either garbage collection (GC) or reference counts"
Mike Stump11289f42009-09-09 15:08:12 +00002278 " (non-GC). The bug occurs with GC enabled",
Ted Kremenek6bd78702009-04-29 18:50:19 +00002279 // Hybrid, without GC
2280 "Code is compiled to use either garbage collection (GC) or reference counts"
2281 " (non-GC). The bug occurs in non-GC mode"
2282};
2283
2284std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2285 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
Mike Stump11289f42009-09-09 15:08:12 +00002286
Ted Kremenek6bd78702009-04-29 18:50:19 +00002287 switch (TF.getLangOptions().getGCMode()) {
2288 default:
2289 assert(false);
Mike Stump11289f42009-09-09 15:08:12 +00002290
Ted Kremenek6bd78702009-04-29 18:50:19 +00002291 case LangOptions::GCOnly:
2292 assert (TF.isGCEnabled());
Mike Stump11289f42009-09-09 15:08:12 +00002293 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2294
Ted Kremenek6bd78702009-04-29 18:50:19 +00002295 case LangOptions::NonGC:
2296 assert (!TF.isGCEnabled());
2297 return std::make_pair(&Msgs[1], &Msgs[1]+1);
Mike Stump11289f42009-09-09 15:08:12 +00002298
Ted Kremenek6bd78702009-04-29 18:50:19 +00002299 case LangOptions::HybridGC:
2300 if (TF.isGCEnabled())
2301 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2302 else
2303 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2304 }
2305}
2306
2307static inline bool contains(const llvm::SmallVectorImpl<ArgEffect>& V,
2308 ArgEffect X) {
2309 for (llvm::SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end();
2310 I!=E; ++I)
2311 if (*I == X) return true;
Mike Stump11289f42009-09-09 15:08:12 +00002312
Ted Kremenek6bd78702009-04-29 18:50:19 +00002313 return false;
2314}
2315
Zhongxing Xu20227f72009-08-06 01:32:16 +00002316PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode* N,
2317 const ExplodedNode* PrevN,
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002318 BugReporterContext& BRC) {
Mike Stump11289f42009-09-09 15:08:12 +00002319
Ted Kremenek051a03d2009-05-13 07:12:33 +00002320 if (!isa<PostStmt>(N->getLocation()))
2321 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002322
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002323 // Check if the type state has changed.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002324 const GRState *PrevSt = PrevN->getState();
2325 const GRState *CurrSt = N->getState();
Mike Stump11289f42009-09-09 15:08:12 +00002326
2327 const RefVal* CurrT = CurrSt->get<RefBindings>(Sym);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002328 if (!CurrT) return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002329
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002330 const RefVal &CurrV = *CurrT;
2331 const RefVal *PrevT = PrevSt->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002332
Ted Kremenek6bd78702009-04-29 18:50:19 +00002333 // Create a string buffer to constain all the useful things we want
2334 // to tell the user.
2335 std::string sbuf;
2336 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +00002337
Ted Kremenek6bd78702009-04-29 18:50:19 +00002338 // This is the allocation site since the previous node had no bindings
2339 // for this symbol.
2340 if (!PrevT) {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002341 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002342
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002343 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002344 // Get the name of the callee (if it is available).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002345 SVal X = CurrSt->getSValAsScalarOrLoc(CE->getCallee());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002346 if (const FunctionDecl* FD = X.getAsFunctionDecl())
2347 os << "Call to function '" << FD->getNameAsString() <<'\'';
2348 else
Mike Stump11289f42009-09-09 15:08:12 +00002349 os << "function call";
2350 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002351 else {
2352 assert (isa<ObjCMessageExpr>(S));
2353 os << "Method";
2354 }
Mike Stump11289f42009-09-09 15:08:12 +00002355
Ted Kremenek6bd78702009-04-29 18:50:19 +00002356 if (CurrV.getObjKind() == RetEffect::CF) {
2357 os << " returns a Core Foundation object with a ";
2358 }
2359 else {
2360 assert (CurrV.getObjKind() == RetEffect::ObjC);
2361 os << " returns an Objective-C object with a ";
2362 }
Mike Stump11289f42009-09-09 15:08:12 +00002363
Ted Kremenek6bd78702009-04-29 18:50:19 +00002364 if (CurrV.isOwned()) {
2365 os << "+1 retain count (owning reference).";
Mike Stump11289f42009-09-09 15:08:12 +00002366
Ted Kremenek6bd78702009-04-29 18:50:19 +00002367 if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) {
2368 assert(CurrV.getObjKind() == RetEffect::CF);
2369 os << " "
2370 "Core Foundation objects are not automatically garbage collected.";
2371 }
2372 }
2373 else {
2374 assert (CurrV.isNotOwned());
2375 os << "+0 retain count (non-owning reference).";
2376 }
Mike Stump11289f42009-09-09 15:08:12 +00002377
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002378 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002379 return new PathDiagnosticEventPiece(Pos, os.str());
2380 }
Mike Stump11289f42009-09-09 15:08:12 +00002381
Ted Kremenek6bd78702009-04-29 18:50:19 +00002382 // Gather up the effects that were performed on the object at this
2383 // program point
2384 llvm::SmallVector<ArgEffect, 2> AEffects;
Mike Stump11289f42009-09-09 15:08:12 +00002385
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002386 if (const RetainSummary *Summ =
2387 TF.getSummaryOfNode(BRC.getNodeResolver().getOriginalNode(N))) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002388 // We only have summaries attached to nodes after evaluating CallExpr and
2389 // ObjCMessageExprs.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002390 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002391
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002392 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002393 // Iterate through the parameter expressions and see if the symbol
2394 // was ever passed as an argument.
2395 unsigned i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002396
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002397 for (CallExpr::const_arg_iterator AI=CE->arg_begin(), AE=CE->arg_end();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002398 AI!=AE; ++AI, ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002399
Ted Kremenek6bd78702009-04-29 18:50:19 +00002400 // Retrieve the value of the argument. Is it the symbol
2401 // we are interested in?
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002402 if (CurrSt->getSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym)
Ted Kremenek6bd78702009-04-29 18:50:19 +00002403 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002404
Ted Kremenek6bd78702009-04-29 18:50:19 +00002405 // We have an argument. Get the effect!
2406 AEffects.push_back(Summ->getArg(i));
2407 }
2408 }
Mike Stump11289f42009-09-09 15:08:12 +00002409 else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002410 if (const Expr *receiver = ME->getReceiver())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002411 if (CurrSt->getSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002412 // The symbol we are tracking is the receiver.
2413 AEffects.push_back(Summ->getReceiverEffect());
2414 }
2415 }
2416 }
Mike Stump11289f42009-09-09 15:08:12 +00002417
Ted Kremenek6bd78702009-04-29 18:50:19 +00002418 do {
2419 // Get the previous type state.
2420 RefVal PrevV = *PrevT;
Mike Stump11289f42009-09-09 15:08:12 +00002421
Ted Kremenek6bd78702009-04-29 18:50:19 +00002422 // Specially handle -dealloc.
2423 if (!TF.isGCEnabled() && contains(AEffects, Dealloc)) {
2424 // Determine if the object's reference count was pushed to zero.
2425 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
2426 // We may not have transitioned to 'release' if we hit an error.
2427 // This case is handled elsewhere.
2428 if (CurrV.getKind() == RefVal::Released) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002429 assert(CurrV.getCombinedCounts() == 0);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002430 os << "Object released by directly sending the '-dealloc' message";
2431 break;
2432 }
2433 }
Mike Stump11289f42009-09-09 15:08:12 +00002434
Ted Kremenek6bd78702009-04-29 18:50:19 +00002435 // Specially handle CFMakeCollectable and friends.
2436 if (contains(AEffects, MakeCollectable)) {
2437 // Get the name of the function.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002438 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002439 SVal X = CurrSt->getSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002440 const FunctionDecl* FD = X.getAsFunctionDecl();
2441 const std::string& FName = FD->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +00002442
Ted Kremenek6bd78702009-04-29 18:50:19 +00002443 if (TF.isGCEnabled()) {
2444 // Determine if the object's reference count was pushed to zero.
2445 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
Mike Stump11289f42009-09-09 15:08:12 +00002446
Ted Kremenek6bd78702009-04-29 18:50:19 +00002447 os << "In GC mode a call to '" << FName
2448 << "' decrements an object's retain count and registers the "
2449 "object with the garbage collector. ";
Mike Stump11289f42009-09-09 15:08:12 +00002450
Ted Kremenek6bd78702009-04-29 18:50:19 +00002451 if (CurrV.getKind() == RefVal::Released) {
2452 assert(CurrV.getCount() == 0);
2453 os << "Since it now has a 0 retain count the object can be "
2454 "automatically collected by the garbage collector.";
2455 }
2456 else
2457 os << "An object must have a 0 retain count to be garbage collected. "
2458 "After this call its retain count is +" << CurrV.getCount()
2459 << '.';
2460 }
Mike Stump11289f42009-09-09 15:08:12 +00002461 else
Ted Kremenek6bd78702009-04-29 18:50:19 +00002462 os << "When GC is not enabled a call to '" << FName
2463 << "' has no effect on its argument.";
Mike Stump11289f42009-09-09 15:08:12 +00002464
Ted Kremenek6bd78702009-04-29 18:50:19 +00002465 // Nothing more to say.
2466 break;
2467 }
Mike Stump11289f42009-09-09 15:08:12 +00002468
2469 // Determine if the typestate has changed.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002470 if (!(PrevV == CurrV))
2471 switch (CurrV.getKind()) {
2472 case RefVal::Owned:
2473 case RefVal::NotOwned:
Mike Stump11289f42009-09-09 15:08:12 +00002474
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002475 if (PrevV.getCount() == CurrV.getCount()) {
2476 // Did an autorelease message get sent?
2477 if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount())
2478 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002479
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00002480 assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount());
Ted Kremenek3978f792009-05-10 05:11:21 +00002481 os << "Object sent -autorelease message";
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002482 break;
2483 }
Mike Stump11289f42009-09-09 15:08:12 +00002484
Ted Kremenek6bd78702009-04-29 18:50:19 +00002485 if (PrevV.getCount() > CurrV.getCount())
2486 os << "Reference count decremented.";
2487 else
2488 os << "Reference count incremented.";
Mike Stump11289f42009-09-09 15:08:12 +00002489
Ted Kremenek6bd78702009-04-29 18:50:19 +00002490 if (unsigned Count = CurrV.getCount())
2491 os << " The object now has a +" << Count << " retain count.";
Mike Stump11289f42009-09-09 15:08:12 +00002492
Ted Kremenek6bd78702009-04-29 18:50:19 +00002493 if (PrevV.getKind() == RefVal::Released) {
2494 assert(TF.isGCEnabled() && CurrV.getCount() > 0);
2495 os << " The object is not eligible for garbage collection until the "
2496 "retain count reaches 0 again.";
2497 }
Mike Stump11289f42009-09-09 15:08:12 +00002498
Ted Kremenek6bd78702009-04-29 18:50:19 +00002499 break;
Mike Stump11289f42009-09-09 15:08:12 +00002500
Ted Kremenek6bd78702009-04-29 18:50:19 +00002501 case RefVal::Released:
2502 os << "Object released.";
2503 break;
Mike Stump11289f42009-09-09 15:08:12 +00002504
Ted Kremenek6bd78702009-04-29 18:50:19 +00002505 case RefVal::ReturnedOwned:
2506 os << "Object returned to caller as an owning reference (single retain "
2507 "count transferred to caller).";
2508 break;
Mike Stump11289f42009-09-09 15:08:12 +00002509
Ted Kremenek6bd78702009-04-29 18:50:19 +00002510 case RefVal::ReturnedNotOwned:
2511 os << "Object returned to caller with a +0 (non-owning) retain count.";
2512 break;
Mike Stump11289f42009-09-09 15:08:12 +00002513
Ted Kremenek6bd78702009-04-29 18:50:19 +00002514 default:
2515 return NULL;
2516 }
Mike Stump11289f42009-09-09 15:08:12 +00002517
Ted Kremenek6bd78702009-04-29 18:50:19 +00002518 // Emit any remaining diagnostics for the argument effects (if any).
2519 for (llvm::SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(),
2520 E=AEffects.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002521
Ted Kremenek6bd78702009-04-29 18:50:19 +00002522 // A bunch of things have alternate behavior under GC.
2523 if (TF.isGCEnabled())
2524 switch (*I) {
2525 default: break;
2526 case Autorelease:
2527 os << "In GC mode an 'autorelease' has no effect.";
2528 continue;
2529 case IncRefMsg:
2530 os << "In GC mode the 'retain' message has no effect.";
2531 continue;
2532 case DecRefMsg:
2533 os << "In GC mode the 'release' message has no effect.";
2534 continue;
2535 }
2536 }
Mike Stump11289f42009-09-09 15:08:12 +00002537 } while (0);
2538
Ted Kremenek6bd78702009-04-29 18:50:19 +00002539 if (os.str().empty())
2540 return 0; // We have nothing to say!
Ted Kremenek051a03d2009-05-13 07:12:33 +00002541
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002542 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002543 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002544 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(Pos, os.str());
Mike Stump11289f42009-09-09 15:08:12 +00002545
Ted Kremenek6bd78702009-04-29 18:50:19 +00002546 // Add the range by scanning the children of the statement for any bindings
2547 // to Sym.
Mike Stump11289f42009-09-09 15:08:12 +00002548 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002549 I!=E; ++I)
2550 if (const Expr* Exp = dyn_cast_or_null<Expr>(*I))
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002551 if (CurrSt->getSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002552 P->addRange(Exp->getSourceRange());
2553 break;
2554 }
Mike Stump11289f42009-09-09 15:08:12 +00002555
Ted Kremenek6bd78702009-04-29 18:50:19 +00002556 return P;
2557}
2558
2559namespace {
2560 class VISIBILITY_HIDDEN FindUniqueBinding :
2561 public StoreManager::BindingsHandler {
2562 SymbolRef Sym;
2563 const MemRegion* Binding;
2564 bool First;
Mike Stump11289f42009-09-09 15:08:12 +00002565
Ted Kremenek6bd78702009-04-29 18:50:19 +00002566 public:
2567 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
Mike Stump11289f42009-09-09 15:08:12 +00002568
Ted Kremenek6bd78702009-04-29 18:50:19 +00002569 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
2570 SVal val) {
Mike Stump11289f42009-09-09 15:08:12 +00002571
2572 SymbolRef SymV = val.getAsSymbol();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002573 if (!SymV || SymV != Sym)
2574 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002575
Ted Kremenek6bd78702009-04-29 18:50:19 +00002576 if (Binding) {
2577 First = false;
2578 return false;
2579 }
2580 else
2581 Binding = R;
Mike Stump11289f42009-09-09 15:08:12 +00002582
2583 return true;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002584 }
Mike Stump11289f42009-09-09 15:08:12 +00002585
Ted Kremenek6bd78702009-04-29 18:50:19 +00002586 operator bool() { return First && Binding; }
2587 const MemRegion* getRegion() { return Binding; }
Mike Stump11289f42009-09-09 15:08:12 +00002588 };
Ted Kremenek6bd78702009-04-29 18:50:19 +00002589}
2590
Zhongxing Xu20227f72009-08-06 01:32:16 +00002591static std::pair<const ExplodedNode*,const MemRegion*>
2592GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode* N,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002593 SymbolRef Sym) {
Mike Stump11289f42009-09-09 15:08:12 +00002594
Ted Kremenek6bd78702009-04-29 18:50:19 +00002595 // Find both first node that referred to the tracked symbol and the
2596 // memory location that value was store to.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002597 const ExplodedNode* Last = N;
Mike Stump11289f42009-09-09 15:08:12 +00002598 const MemRegion* FirstBinding = 0;
2599
Ted Kremenek6bd78702009-04-29 18:50:19 +00002600 while (N) {
2601 const GRState* St = N->getState();
2602 RefBindings B = St->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00002603
Ted Kremenek6bd78702009-04-29 18:50:19 +00002604 if (!B.lookup(Sym))
2605 break;
Mike Stump11289f42009-09-09 15:08:12 +00002606
Ted Kremenek6bd78702009-04-29 18:50:19 +00002607 FindUniqueBinding FB(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002608 StateMgr.iterBindings(St, FB);
2609 if (FB) FirstBinding = FB.getRegion();
2610
Ted Kremenek6bd78702009-04-29 18:50:19 +00002611 Last = N;
Mike Stump11289f42009-09-09 15:08:12 +00002612 N = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002613 }
Mike Stump11289f42009-09-09 15:08:12 +00002614
Ted Kremenek6bd78702009-04-29 18:50:19 +00002615 return std::make_pair(Last, FirstBinding);
2616}
2617
2618PathDiagnosticPiece*
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002619CFRefReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002620 const ExplodedNode* EndN) {
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002621 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenek6bd78702009-04-29 18:50:19 +00002622 // assigned to different variables, etc.
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002623 BRC.addNotableSymbol(Sym);
2624 return RangedBugReport::getEndPath(BRC, EndN);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002625}
2626
2627PathDiagnosticPiece*
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002628CFRefLeakReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002629 const ExplodedNode* EndN){
Mike Stump11289f42009-09-09 15:08:12 +00002630
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002631 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenek6bd78702009-04-29 18:50:19 +00002632 // assigned to different variables, etc.
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002633 BRC.addNotableSymbol(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002634
Ted Kremenek6bd78702009-04-29 18:50:19 +00002635 // We are reporting a leak. Walk up the graph to get to the first node where
2636 // the symbol appeared, and also get the first VarDecl that tracked object
2637 // is stored to.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002638 const ExplodedNode* AllocNode = 0;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002639 const MemRegion* FirstBinding = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002640
Ted Kremenek6bd78702009-04-29 18:50:19 +00002641 llvm::tie(AllocNode, FirstBinding) =
Ted Kremenek8c8fb482009-05-08 23:32:51 +00002642 GetAllocationSite(BRC.getStateManager(), EndN, Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002643
2644 // Get the allocate site.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002645 assert(AllocNode);
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002646 const Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002647
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002648 SourceManager& SMgr = BRC.getSourceManager();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002649 unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart());
Mike Stump11289f42009-09-09 15:08:12 +00002650
Ted Kremenek6bd78702009-04-29 18:50:19 +00002651 // Compute an actual location for the leak. Sometimes a leak doesn't
2652 // occur at an actual statement (e.g., transition between blocks; end
2653 // of function) so we need to walk the graph and compute a real location.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002654 const ExplodedNode* LeakN = EndN;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002655 PathDiagnosticLocation L;
Mike Stump11289f42009-09-09 15:08:12 +00002656
Ted Kremenek6bd78702009-04-29 18:50:19 +00002657 while (LeakN) {
2658 ProgramPoint P = LeakN->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002659
Ted Kremenek6bd78702009-04-29 18:50:19 +00002660 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
2661 L = PathDiagnosticLocation(PS->getStmt()->getLocStart(), SMgr);
2662 break;
2663 }
2664 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
2665 if (const Stmt* Term = BE->getSrc()->getTerminator()) {
2666 L = PathDiagnosticLocation(Term->getLocStart(), SMgr);
2667 break;
2668 }
2669 }
Mike Stump11289f42009-09-09 15:08:12 +00002670
Ted Kremenek6bd78702009-04-29 18:50:19 +00002671 LeakN = LeakN->succ_empty() ? 0 : *(LeakN->succ_begin());
2672 }
Mike Stump11289f42009-09-09 15:08:12 +00002673
Ted Kremenek6bd78702009-04-29 18:50:19 +00002674 if (!L.isValid()) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002675 const Decl &D = EndN->getCodeDecl();
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002676 L = PathDiagnosticLocation(D.getBodyRBrace(), SMgr);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002677 }
Mike Stump11289f42009-09-09 15:08:12 +00002678
Ted Kremenek6bd78702009-04-29 18:50:19 +00002679 std::string sbuf;
2680 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +00002681
Ted Kremenek6bd78702009-04-29 18:50:19 +00002682 os << "Object allocated on line " << AllocLine;
Mike Stump11289f42009-09-09 15:08:12 +00002683
Ted Kremenek6bd78702009-04-29 18:50:19 +00002684 if (FirstBinding)
Mike Stump11289f42009-09-09 15:08:12 +00002685 os << " and stored into '" << FirstBinding->getString() << '\'';
2686
Ted Kremenek6bd78702009-04-29 18:50:19 +00002687 // Get the retain count.
2688 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002689
Ted Kremenek6bd78702009-04-29 18:50:19 +00002690 if (RV->getKind() == RefVal::ErrorLeakReturned) {
2691 // FIXME: Per comments in rdar://6320065, "create" only applies to CF
2692 // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership
2693 // to the caller for NS objects.
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002694 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002695 os << " is returned from a method whose name ('"
Ted Kremenek223a7d52009-04-29 23:03:22 +00002696 << MD.getSelector().getAsString()
Ted Kremenek6bd78702009-04-29 18:50:19 +00002697 << "') does not contain 'copy' or otherwise starts with"
2698 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenekd6bef2e2009-04-29 22:25:52 +00002699 " in the Memory Management Guide for Cocoa (object leaked)";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002700 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00002701 else if (RV->getKind() == RefVal::ErrorGCLeakReturned) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002702 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremenekdee56e32009-05-10 06:25:57 +00002703 os << " and returned from method '" << MD.getSelector().getAsString()
Ted Kremenek1f8e4342009-05-10 16:52:15 +00002704 << "' is potentially leaked when using garbage collection. Callers "
2705 "of this method do not expect a returned object with a +1 retain "
2706 "count since they expect the object to be managed by the garbage "
2707 "collector";
Ted Kremenekdee56e32009-05-10 06:25:57 +00002708 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002709 else
2710 os << " is no longer referenced after this point and has a retain count of"
Ted Kremenekd6bef2e2009-04-29 22:25:52 +00002711 " +" << RV->getCount() << " (object leaked)";
Mike Stump11289f42009-09-09 15:08:12 +00002712
Ted Kremenek6bd78702009-04-29 18:50:19 +00002713 return new PathDiagnosticEventPiece(L, os.str());
2714}
2715
Ted Kremenek6bd78702009-04-29 18:50:19 +00002716CFRefLeakReport::CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002717 ExplodedNode *n,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002718 SymbolRef sym, GRExprEngine& Eng)
Mike Stump11289f42009-09-09 15:08:12 +00002719: CFRefReport(D, tf, n, sym) {
2720
Ted Kremenek6bd78702009-04-29 18:50:19 +00002721 // Most bug reports are cached at the location where they occured.
2722 // With leaks, we want to unique them by the location where they were
2723 // allocated, and only report a single path. To do this, we need to find
2724 // the allocation site of a piece of tracked memory, which we do via a
2725 // call to GetAllocationSite. This will walk the ExplodedGraph backwards.
2726 // Note that this is *not* the trimmed graph; we are guaranteed, however,
2727 // that all ancestor nodes that represent the allocation site have the
2728 // same SourceLocation.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002729 const ExplodedNode* AllocNode = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002730
Ted Kremenek6bd78702009-04-29 18:50:19 +00002731 llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding.
Ted Kremenek8c8fb482009-05-08 23:32:51 +00002732 GetAllocationSite(Eng.getStateManager(), getEndNode(), getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00002733
Ted Kremenek6bd78702009-04-29 18:50:19 +00002734 // Get the SourceLocation for the allocation site.
2735 ProgramPoint P = AllocNode->getLocation();
2736 AllocSite = cast<PostStmt>(P).getStmt()->getLocStart();
Mike Stump11289f42009-09-09 15:08:12 +00002737
Ted Kremenek6bd78702009-04-29 18:50:19 +00002738 // Fill in the description of the bug.
2739 Description.clear();
2740 llvm::raw_string_ostream os(Description);
2741 SourceManager& SMgr = Eng.getContext().getSourceManager();
2742 unsigned AllocLine = SMgr.getInstantiationLineNumber(AllocSite);
Ted Kremenekf1e76672009-05-02 19:05:19 +00002743 os << "Potential leak ";
2744 if (tf.isGCEnabled()) {
2745 os << "(when using garbage collection) ";
Mike Stump11289f42009-09-09 15:08:12 +00002746 }
Ted Kremenekf1e76672009-05-02 19:05:19 +00002747 os << "of an object allocated on line " << AllocLine;
Mike Stump11289f42009-09-09 15:08:12 +00002748
Ted Kremenek6bd78702009-04-29 18:50:19 +00002749 // FIXME: AllocBinding doesn't get populated for RegionStore yet.
2750 if (AllocBinding)
2751 os << " and stored into '" << AllocBinding->getString() << '\'';
2752}
2753
2754//===----------------------------------------------------------------------===//
2755// Main checker logic.
2756//===----------------------------------------------------------------------===//
2757
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002758/// GetReturnType - Used to get the return type of a message expression or
2759/// function call with the intention of affixing that type to a tracked symbol.
2760/// While the the return type can be queried directly from RetEx, when
2761/// invoking class methods we augment to the return type to be that of
2762/// a pointer to the class (as opposed it just being id).
Steve Naroff7cae42b2009-07-10 23:34:53 +00002763static QualType GetReturnType(const Expr* RetE, ASTContext& Ctx) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002764 QualType RetTy = RetE->getType();
Steve Naroff7cae42b2009-07-10 23:34:53 +00002765 // If RetE is not a message expression just return its type.
2766 // If RetE is a message expression, return its types if it is something
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002767 /// more specific than id.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002768 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
John McCall9dd450b2009-09-21 23:43:11 +00002769 if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
Mike Stump11289f42009-09-09 15:08:12 +00002770 if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
Steve Naroff7cae42b2009-07-10 23:34:53 +00002771 PT->isObjCClassType()) {
2772 // At this point we know the return type of the message expression is
2773 // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
2774 // is a call to a class method whose type we can resolve. In such
2775 // cases, promote the return type to XXX* (where XXX is the class).
Mike Stump11289f42009-09-09 15:08:12 +00002776 const ObjCInterfaceDecl *D = ME->getClassInfo().first;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002777 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
2778 }
Mike Stump11289f42009-09-09 15:08:12 +00002779
Steve Naroff7cae42b2009-07-10 23:34:53 +00002780 return RetTy;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002781}
2782
Zhongxing Xu20227f72009-08-06 01:32:16 +00002783void CFRefCount::EvalSummary(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002784 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002785 GRStmtNodeBuilder& Builder,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002786 Expr* Ex,
2787 Expr* Receiver,
Ted Kremenekff606a12009-05-04 04:57:00 +00002788 const RetainSummary& Summ,
Zhongxing Xuac129432009-04-20 05:24:46 +00002789 ExprIterator arg_beg, ExprIterator arg_end,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002790 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00002791
Ted Kremenek819e9b62008-03-11 06:39:11 +00002792 // Get the state.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002793 const GRState *state = Builder.GetState(Pred);
Ted Kremenek821537e2008-05-06 02:41:27 +00002794
2795 // Evaluate the effect of the arguments.
Ted Kremenek8cb96e92008-04-16 04:28:53 +00002796 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek68d73d12008-03-12 01:21:45 +00002797 unsigned idx = 0;
Ted Kremenek988990f2008-04-11 18:40:51 +00002798 Expr* ErrorExpr = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002799 SymbolRef ErrorSym = 0;
2800
2801 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
2802 SVal V = state->getSValAsScalarOrLoc(*I);
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002803 SymbolRef Sym = V.getAsLocSymbol();
Ted Kremenek804fc232009-03-04 00:13:50 +00002804
Ted Kremenek3e31c262009-03-26 03:35:11 +00002805 if (Sym)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002806 if (RefBindings::data_type* T = state->get<RefBindings>(Sym)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002807 state = Update(state, Sym, *T, Summ.getArg(idx), hasErr);
Ted Kremenekc52f9392009-02-24 19:15:11 +00002808 if (hasErr) {
Ted Kremenek988990f2008-04-11 18:40:51 +00002809 ErrorExpr = *I;
Ted Kremenek4963d112008-07-07 16:21:19 +00002810 ErrorSym = Sym;
Ted Kremenek988990f2008-04-11 18:40:51 +00002811 break;
Mike Stump11289f42009-09-09 15:08:12 +00002812 }
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002813 continue;
Ted Kremenekc52f9392009-02-24 19:15:11 +00002814 }
Ted Kremenekae529272008-07-09 18:11:16 +00002815
Ted Kremenekf9539d02009-09-22 04:48:39 +00002816 tryAgain:
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002817 if (isa<Loc>(V)) {
2818 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002819 if (Summ.getArg(idx) == DoNothingByRef)
Ted Kremenekae529272008-07-09 18:11:16 +00002820 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002821
2822 // Invalidate the value of the variable passed by reference.
2823
Ted Kremenek4d851462008-07-03 23:26:32 +00002824 // FIXME: We can have collisions on the conjured symbol if the
2825 // expression *I also creates conjured symbols. We probably want
2826 // to identify conjured symbols by an expression pair: the enclosing
2827 // expression (the context) and the expression itself. This should
Mike Stump11289f42009-09-09 15:08:12 +00002828 // disambiguate conjured symbols.
Zhongxing Xu4744d562009-06-29 06:43:40 +00002829 unsigned Count = Builder.getCurrentBlockCount();
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002830 StoreManager& StoreMgr = Eng.getStateManager().getStoreManager();
Ted Kremenek97f75f82009-05-11 22:55:17 +00002831
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002832 const MemRegion *R = MR->getRegion();
2833 // Are we dealing with an ElementRegion? If the element type is
2834 // a basic integer type (e.g., char, int) and the underying region
2835 // is a variable region then strip off the ElementRegion.
2836 // FIXME: We really need to think about this for the general case
2837 // as sometimes we are reasoning about arrays and other times
2838 // about (char*), etc., is just a form of passing raw bytes.
2839 // e.g., void *p = alloca(); foo((char*)p);
2840 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
2841 // Checking for 'integral type' is probably too promiscuous, but
2842 // we'll leave it in for now until we have a systematic way of
2843 // handling all of these cases. Eventually we need to come up
2844 // with an interface to StoreManager so that this logic can be
2845 // approriately delegated to the respective StoreManagers while
2846 // still allowing us to do checker-specific logic (e.g.,
2847 // invalidating reference counts), probably via callbacks.
2848 if (ER->getElementType()->isIntegralType()) {
2849 const MemRegion *superReg = ER->getSuperRegion();
2850 if (isa<VarRegion>(superReg) || isa<FieldRegion>(superReg) ||
2851 isa<ObjCIvarRegion>(superReg))
2852 R = cast<TypedRegion>(superReg);
Ted Kremenek0626df42009-05-06 18:19:24 +00002853 }
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002854 // FIXME: What about layers of ElementRegions?
2855 }
Zhongxing Xu4744d562009-06-29 06:43:40 +00002856
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002857 // Is the invalidated variable something that we were tracking?
2858 SymbolRef Sym = state->getSValAsScalarOrLoc(R).getAsLocSymbol();
Mike Stump11289f42009-09-09 15:08:12 +00002859
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002860 // Remove any existing reference-count binding.
Mike Stump11289f42009-09-09 15:08:12 +00002861 if (Sym)
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002862 state = state->remove<RefBindings>(Sym);
2863
2864 state = StoreMgr.InvalidateRegion(state, R, *I, Count);
Ted Kremenek4d851462008-07-03 23:26:32 +00002865 }
2866 else {
2867 // Nuke all other arguments passed by reference.
Ted Kremenekf9539d02009-09-22 04:48:39 +00002868 // FIXME: is this necessary or correct? This handles the non-Region
2869 // cases. Is it ever valid to store to these?
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002870 state = state->unbindLoc(cast<Loc>(V));
Ted Kremenek4d851462008-07-03 23:26:32 +00002871 }
Ted Kremenek0a86fdb2008-04-11 20:51:02 +00002872 }
Ted Kremenekf9539d02009-09-22 04:48:39 +00002873 else if (isa<nonloc::LocAsInteger>(V)) {
2874 // If we are passing a location wrapped as an integer, unwrap it and
2875 // invalidate the values referred by the location.
2876 V = cast<nonloc::LocAsInteger>(V).getLoc();
2877 goto tryAgain;
2878 }
Mike Stump11289f42009-09-09 15:08:12 +00002879 }
2880
2881 // Evaluate the effect on the message receiver.
Ted Kremenek821537e2008-05-06 02:41:27 +00002882 if (!ErrorExpr && Receiver) {
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002883 SymbolRef Sym = state->getSValAsScalarOrLoc(Receiver).getAsLocSymbol();
Ted Kremenek3e31c262009-03-26 03:35:11 +00002884 if (Sym) {
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002885 if (const RefVal* T = state->get<RefBindings>(Sym)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002886 state = Update(state, Sym, *T, Summ.getReceiverEffect(), hasErr);
Ted Kremenekc52f9392009-02-24 19:15:11 +00002887 if (hasErr) {
Ted Kremenek821537e2008-05-06 02:41:27 +00002888 ErrorExpr = Receiver;
Ted Kremenek4963d112008-07-07 16:21:19 +00002889 ErrorSym = Sym;
Ted Kremenek821537e2008-05-06 02:41:27 +00002890 }
Ted Kremenekc52f9392009-02-24 19:15:11 +00002891 }
Ted Kremenek821537e2008-05-06 02:41:27 +00002892 }
2893 }
Mike Stump11289f42009-09-09 15:08:12 +00002894
2895 // Process any errors.
Ted Kremenek8cb96e92008-04-16 04:28:53 +00002896 if (hasErr) {
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00002897 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek396f4362008-04-18 03:39:05 +00002898 hasErr, ErrorSym);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002899 return;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00002900 }
Mike Stump11289f42009-09-09 15:08:12 +00002901
2902 // Consult the summary for the return value.
Ted Kremenekff606a12009-05-04 04:57:00 +00002903 RetEffect RE = Summ.getRetEffect();
Mike Stump11289f42009-09-09 15:08:12 +00002904
Ted Kremenek1272f702009-05-12 20:06:54 +00002905 if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
2906 assert(Receiver);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002907 SVal V = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenek1272f702009-05-12 20:06:54 +00002908 bool found = false;
2909 if (SymbolRef Sym = V.getAsLocSymbol())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002910 if (state->get<RefBindings>(Sym)) {
Ted Kremenek1272f702009-05-12 20:06:54 +00002911 found = true;
2912 RE = Summaries.getObjAllocRetEffect();
2913 }
2914
2915 if (!found)
2916 RE = RetEffect::MakeNoRet();
Mike Stump11289f42009-09-09 15:08:12 +00002917 }
2918
Ted Kremenek68d73d12008-03-12 01:21:45 +00002919 switch (RE.getKind()) {
2920 default:
2921 assert (false && "Unhandled RetEffect."); break;
Mike Stump11289f42009-09-09 15:08:12 +00002922
2923 case RetEffect::NoRet: {
Ted Kremenek831f3272008-04-11 20:23:24 +00002924 // Make up a symbol for the return value (not reference counted).
Ted Kremenek1642bda2009-06-26 00:05:51 +00002925 // FIXME: Most of this logic is not specific to the retain/release
2926 // checker.
Mike Stump11289f42009-09-09 15:08:12 +00002927
Ted Kremenek21387322008-10-17 22:23:12 +00002928 // FIXME: We eventually should handle structs and other compound types
2929 // that are returned by value.
Mike Stump11289f42009-09-09 15:08:12 +00002930
Ted Kremenek21387322008-10-17 22:23:12 +00002931 QualType T = Ex->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002932
Ted Kremenek16866d62008-11-13 06:10:40 +00002933 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenek831f3272008-04-11 20:23:24 +00002934 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekf2489ea2009-04-09 22:22:44 +00002935 ValueManager &ValMgr = Eng.getValueManager();
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002936 SVal X = ValMgr.getConjuredSymbolVal(NULL, Ex, T, Count);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002937 state = state->BindExpr(Ex, X, false);
Mike Stump11289f42009-09-09 15:08:12 +00002938 }
2939
Ted Kremenek4b772092008-04-10 23:44:06 +00002940 break;
Ted Kremenek21387322008-10-17 22:23:12 +00002941 }
Mike Stump11289f42009-09-09 15:08:12 +00002942
Ted Kremenek68d73d12008-03-12 01:21:45 +00002943 case RetEffect::Alias: {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002944 unsigned idx = RE.getIndex();
Ted Kremenek08e17112008-06-17 02:43:46 +00002945 assert (arg_end >= arg_beg);
Ted Kremenek00daccd2008-05-05 22:11:16 +00002946 assert (idx < (unsigned) (arg_end - arg_beg));
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002947 SVal V = state->getSValAsScalarOrLoc(*(arg_beg+idx));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002948 state = state->BindExpr(Ex, V, false);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002949 break;
2950 }
Mike Stump11289f42009-09-09 15:08:12 +00002951
Ted Kremenek821537e2008-05-06 02:41:27 +00002952 case RetEffect::ReceiverAlias: {
2953 assert (Receiver);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002954 SVal V = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002955 state = state->BindExpr(Ex, V, false);
Ted Kremenek821537e2008-05-06 02:41:27 +00002956 break;
2957 }
Mike Stump11289f42009-09-09 15:08:12 +00002958
Ted Kremenekab4a8b52008-06-23 18:02:52 +00002959 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek68d73d12008-03-12 01:21:45 +00002960 case RetEffect::OwnedSymbol: {
2961 unsigned Count = Builder.getCurrentBlockCount();
Mike Stump11289f42009-09-09 15:08:12 +00002962 ValueManager &ValMgr = Eng.getValueManager();
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002963 SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002964 QualType RetT = GetReturnType(Ex, ValMgr.getContext());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002965 state = state->set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(),
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002966 RetT));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002967 state = state->BindExpr(Ex, ValMgr.makeLoc(Sym), false);
Ted Kremenek0b891a32009-03-09 22:46:49 +00002968
2969 // FIXME: Add a flag to the checker where allocations are assumed to
2970 // *not fail.
2971#if 0
Ted Kremenek2e561dd2009-01-28 22:27:59 +00002972 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) {
2973 bool isFeasible;
2974 state = state.Assume(loc::SymbolVal(Sym), true, isFeasible);
Mike Stump11289f42009-09-09 15:08:12 +00002975 assert(isFeasible && "Cannot assume fresh symbol is non-null.");
Ted Kremenek2e561dd2009-01-28 22:27:59 +00002976 }
Ted Kremenek0b891a32009-03-09 22:46:49 +00002977#endif
Mike Stump11289f42009-09-09 15:08:12 +00002978
Ted Kremenek68d73d12008-03-12 01:21:45 +00002979 break;
2980 }
Mike Stump11289f42009-09-09 15:08:12 +00002981
Ted Kremeneke6633562009-04-27 19:14:45 +00002982 case RetEffect::GCNotOwnedSymbol:
Ted Kremenek68d73d12008-03-12 01:21:45 +00002983 case RetEffect::NotOwnedSymbol: {
2984 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002985 ValueManager &ValMgr = Eng.getValueManager();
2986 SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002987 QualType RetT = GetReturnType(Ex, ValMgr.getContext());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002988 state = state->set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002989 RetT));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002990 state = state->BindExpr(Ex, ValMgr.makeLoc(Sym), false);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002991 break;
2992 }
2993 }
Mike Stump11289f42009-09-09 15:08:12 +00002994
Ted Kremenekd84fff62009-02-18 02:00:25 +00002995 // Generate a sink node if we are at the end of a path.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002996 ExplodedNode *NewNode =
Ted Kremenekff606a12009-05-04 04:57:00 +00002997 Summ.isEndPath() ? Builder.MakeSinkNode(Dst, Ex, Pred, state)
2998 : Builder.MakeNode(Dst, Ex, Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00002999
Ted Kremenekd84fff62009-02-18 02:00:25 +00003000 // Annotate the edge with summary we used.
Ted Kremenekff606a12009-05-04 04:57:00 +00003001 if (NewNode) SummaryLog[NewNode] = &Summ;
Ted Kremenek00daccd2008-05-05 22:11:16 +00003002}
3003
3004
Zhongxing Xu20227f72009-08-06 01:32:16 +00003005void CFRefCount::EvalCall(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00003006 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003007 GRStmtNodeBuilder& Builder,
Zhongxing Xu27f17422008-10-17 05:57:07 +00003008 CallExpr* CE, SVal L,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003009 ExplodedNode* Pred) {
Zhongxing Xuac129432009-04-20 05:24:46 +00003010 const FunctionDecl* FD = L.getAsFunctionDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003011 RetainSummary* Summ = !FD ? Summaries.getDefaultSummary()
Zhongxing Xuac129432009-04-20 05:24:46 +00003012 : Summaries.getSummary(const_cast<FunctionDecl*>(FD));
Mike Stump11289f42009-09-09 15:08:12 +00003013
Ted Kremenekff606a12009-05-04 04:57:00 +00003014 assert(Summ);
3015 EvalSummary(Dst, Eng, Builder, CE, 0, *Summ,
Ted Kremenek00daccd2008-05-05 22:11:16 +00003016 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenekea6507f2008-03-06 00:08:09 +00003017}
Ted Kremenek819e9b62008-03-11 06:39:11 +00003018
Zhongxing Xu20227f72009-08-06 01:32:16 +00003019void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003020 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003021 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003022 ObjCMessageExpr* ME,
Mike Stump11289f42009-09-09 15:08:12 +00003023 ExplodedNode* Pred) {
Ted Kremenekff606a12009-05-04 04:57:00 +00003024 RetainSummary* Summ = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003025
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003026 if (Expr* Receiver = ME->getReceiver()) {
3027 // We need the type-information of the tracked receiver object
3028 // Retrieve it from the state.
Ted Kremenek5801f652009-05-13 18:16:01 +00003029 const ObjCInterfaceDecl* ID = 0;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003030
3031 // FIXME: Wouldn't it be great if this code could be reduced? It's just
3032 // a chain of lookups.
Ted Kremenek0b50fb12009-04-29 05:04:30 +00003033 // FIXME: Is this really working as expected? There are cases where
3034 // we just use the 'ID' from the message expression.
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +00003035 const GRState* St = Builder.GetState(Pred);
Ted Kremenek095f1a92009-06-18 23:58:37 +00003036 SVal V = St->getSValAsScalarOrLoc(Receiver);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003037
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003038 SymbolRef Sym = V.getAsLocSymbol();
Mike Stump11289f42009-09-09 15:08:12 +00003039
Ted Kremenek3e31c262009-03-26 03:35:11 +00003040 if (Sym) {
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00003041 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003042 if (const ObjCObjectPointerType* PT =
John McCall9dd450b2009-09-21 23:43:11 +00003043 T->getType()->getAs<ObjCObjectPointerType>())
Steve Naroff7cae42b2009-07-10 23:34:53 +00003044 ID = PT->getInterfaceDecl();
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003045 }
3046 }
Ted Kremenek5801f652009-05-13 18:16:01 +00003047
3048 // FIXME: this is a hack. This may or may not be the actual method
3049 // that is called.
3050 if (!ID) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003051 if (const ObjCObjectPointerType *PT =
John McCall9dd450b2009-09-21 23:43:11 +00003052 Receiver->getType()->getAs<ObjCObjectPointerType>())
Steve Naroff7cae42b2009-07-10 23:34:53 +00003053 ID = PT->getInterfaceDecl();
Ted Kremenek5801f652009-05-13 18:16:01 +00003054 }
3055
Ted Kremenek38724302009-04-29 17:09:14 +00003056 // FIXME: The receiver could be a reference to a class, meaning that
3057 // we should use the class method.
3058 Summ = Summaries.getInstanceMethodSummary(ME, ID);
Ted Kremenek03466c22008-10-24 20:32:50 +00003059
Ted Kremenekcc3d1882008-10-23 01:56:15 +00003060 // Special-case: are we sending a mesage to "self"?
3061 // This is a hack. When we have full-IP this should be removed.
Mike Stump11289f42009-09-09 15:08:12 +00003062 if (isa<ObjCMethodDecl>(Pred->getLocationContext()->getDecl())) {
Ted Kremenek1d9a2672009-05-04 05:31:22 +00003063 if (Expr* Receiver = ME->getReceiver()) {
Ted Kremenek095f1a92009-06-18 23:58:37 +00003064 SVal X = St->getSValAsScalarOrLoc(Receiver);
Mike Stump11289f42009-09-09 15:08:12 +00003065 if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X)) {
Ted Kremenek608677a2009-08-21 23:25:54 +00003066 // Get the region associated with 'self'.
Mike Stump11289f42009-09-09 15:08:12 +00003067 const LocationContext *LC = Pred->getLocationContext();
Ted Kremenek608677a2009-08-21 23:25:54 +00003068 if (const ImplicitParamDecl *SelfDecl = LC->getSelfDecl()) {
Mike Stump11289f42009-09-09 15:08:12 +00003069 SVal SelfVal = St->getSVal(St->getRegion(SelfDecl, LC));
Ted Kremenek608677a2009-08-21 23:25:54 +00003070 if (L->getBaseRegion() == SelfVal.getAsRegion()) {
3071 // Update the summary to make the default argument effect
3072 // 'StopTracking'.
3073 Summ = Summaries.copySummary(Summ);
3074 Summ->setDefaultArgEffect(StopTracking);
3075 }
Mike Stump11289f42009-09-09 15:08:12 +00003076 }
Ted Kremenek608677a2009-08-21 23:25:54 +00003077 }
Ted Kremenekcc3d1882008-10-23 01:56:15 +00003078 }
3079 }
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003080 }
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003081 else
Ted Kremenek0a1f9c42009-04-23 21:25:57 +00003082 Summ = Summaries.getClassMethodSummary(ME);
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003083
Ted Kremenekff606a12009-05-04 04:57:00 +00003084 if (!Summ)
3085 Summ = Summaries.getDefaultSummary();
Ted Kremenek8a5ad392009-04-24 17:50:11 +00003086
Ted Kremenekff606a12009-05-04 04:57:00 +00003087 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), *Summ,
Ted Kremenek015c3562008-05-06 04:20:12 +00003088 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003089}
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003090
3091namespace {
3092class VISIBILITY_HIDDEN StopTrackingCallback : public SymbolVisitor {
Ted Kremenek89a303c2009-06-18 00:49:02 +00003093 const GRState *state;
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003094public:
Ted Kremenek89a303c2009-06-18 00:49:02 +00003095 StopTrackingCallback(const GRState *st) : state(st) {}
3096 const GRState *getState() const { return state; }
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003097
3098 bool VisitSymbol(SymbolRef sym) {
Ted Kremenek89a303c2009-06-18 00:49:02 +00003099 state = state->remove<RefBindings>(sym);
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003100 return true;
3101 }
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003102};
3103} // end anonymous namespace
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003104
Mike Stump11289f42009-09-09 15:08:12 +00003105
3106void CFRefCount::EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val) {
3107 // Are we storing to something that causes the value to "escape"?
Ted Kremenek71454892008-04-16 20:40:59 +00003108 bool escapes = false;
Mike Stump11289f42009-09-09 15:08:12 +00003109
Ted Kremeneke86755e2008-10-18 03:49:51 +00003110 // A value escapes in three possible cases (this may change):
3111 //
3112 // (1) we are binding to something that is not a memory region.
3113 // (2) we are binding to a memregion that does not have stack storage
3114 // (3) we are binding to a memregion with stack storage that the store
Mike Stump11289f42009-09-09 15:08:12 +00003115 // does not understand.
Ted Kremenek89a303c2009-06-18 00:49:02 +00003116 const GRState *state = B.getState();
Ted Kremeneke86755e2008-10-18 03:49:51 +00003117
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003118 if (!isa<loc::MemRegionVal>(location))
Ted Kremenek71454892008-04-16 20:40:59 +00003119 escapes = true;
Ted Kremenek5ca90a22008-10-04 05:50:14 +00003120 else {
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003121 const MemRegion* R = cast<loc::MemRegionVal>(location).getRegion();
Ted Kremenek404b1322009-06-23 18:05:21 +00003122 escapes = !R->hasStackStorage();
Mike Stump11289f42009-09-09 15:08:12 +00003123
Ted Kremeneke86755e2008-10-18 03:49:51 +00003124 if (!escapes) {
3125 // To test (3), generate a new state with the binding removed. If it is
3126 // the same state, then it escapes (since the store cannot represent
3127 // the binding).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003128 escapes = (state == (state->bindLoc(cast<Loc>(location), UnknownVal())));
Ted Kremeneke86755e2008-10-18 03:49:51 +00003129 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +00003130 }
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003131
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003132 // If our store can represent the binding and we aren't storing to something
3133 // that doesn't have local storage then just return and have the simulation
3134 // state continue as is.
3135 if (!escapes)
3136 return;
Ted Kremeneke86755e2008-10-18 03:49:51 +00003137
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003138 // Otherwise, find all symbols referenced by 'val' that we are tracking
3139 // and stop tracking them.
Ted Kremenek89a303c2009-06-18 00:49:02 +00003140 B.MakeNode(state->scanReachableSymbols<StopTrackingCallback>(val).getState());
Ted Kremenekcbf4c612008-04-16 22:32:20 +00003141}
3142
Ted Kremeneka506fec2008-04-17 18:12:53 +00003143 // Return statements.
3144
Zhongxing Xu20227f72009-08-06 01:32:16 +00003145void CFRefCount::EvalReturn(ExplodedNodeSet& Dst,
Ted Kremeneka506fec2008-04-17 18:12:53 +00003146 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003147 GRStmtNodeBuilder& Builder,
Ted Kremeneka506fec2008-04-17 18:12:53 +00003148 ReturnStmt* S,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003149 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00003150
Ted Kremeneka506fec2008-04-17 18:12:53 +00003151 Expr* RetE = S->getRetValue();
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003152 if (!RetE)
Ted Kremeneka506fec2008-04-17 18:12:53 +00003153 return;
Mike Stump11289f42009-09-09 15:08:12 +00003154
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003155 const GRState *state = Builder.GetState(Pred);
3156 SymbolRef Sym = state->getSValAsScalarOrLoc(RetE).getAsLocSymbol();
Mike Stump11289f42009-09-09 15:08:12 +00003157
Ted Kremenek3e31c262009-03-26 03:35:11 +00003158 if (!Sym)
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003159 return;
Mike Stump11289f42009-09-09 15:08:12 +00003160
Ted Kremeneka506fec2008-04-17 18:12:53 +00003161 // Get the reference count binding (if any).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003162 const RefVal* T = state->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00003163
Ted Kremeneka506fec2008-04-17 18:12:53 +00003164 if (!T)
3165 return;
Mike Stump11289f42009-09-09 15:08:12 +00003166
3167 // Change the reference count.
3168 RefVal X = *T;
3169
3170 switch (X.getKind()) {
3171 case RefVal::Owned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00003172 unsigned cnt = X.getCount();
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003173 assert (cnt > 0);
Ted Kremenek3978f792009-05-10 05:11:21 +00003174 X.setCount(cnt - 1);
3175 X = X ^ RefVal::ReturnedOwned;
Ted Kremeneka506fec2008-04-17 18:12:53 +00003176 break;
3177 }
Mike Stump11289f42009-09-09 15:08:12 +00003178
Ted Kremeneka506fec2008-04-17 18:12:53 +00003179 case RefVal::NotOwned: {
3180 unsigned cnt = X.getCount();
Ted Kremenek3978f792009-05-10 05:11:21 +00003181 if (cnt) {
3182 X.setCount(cnt - 1);
3183 X = X ^ RefVal::ReturnedOwned;
3184 }
3185 else {
3186 X = X ^ RefVal::ReturnedNotOwned;
3187 }
Ted Kremeneka506fec2008-04-17 18:12:53 +00003188 break;
3189 }
Mike Stump11289f42009-09-09 15:08:12 +00003190
3191 default:
Ted Kremeneka506fec2008-04-17 18:12:53 +00003192 return;
3193 }
Mike Stump11289f42009-09-09 15:08:12 +00003194
Ted Kremeneka506fec2008-04-17 18:12:53 +00003195 // Update the binding.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003196 state = state->set<RefBindings>(Sym, X);
Ted Kremenek6bd78702009-04-29 18:50:19 +00003197 Pred = Builder.MakeNode(Dst, S, Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003198
Ted Kremenekb4e27a12009-04-30 05:51:50 +00003199 // Did we cache out?
3200 if (!Pred)
3201 return;
Mike Stump11289f42009-09-09 15:08:12 +00003202
Ted Kremenek3978f792009-05-10 05:11:21 +00003203 // Update the autorelease counts.
3204 static unsigned autoreleasetag = 0;
3205 GenericNodeBuilder Bd(Builder, S, &autoreleasetag);
3206 bool stop = false;
3207 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state , Bd, Pred, Eng, Sym,
3208 X, stop);
Mike Stump11289f42009-09-09 15:08:12 +00003209
Ted Kremenek3978f792009-05-10 05:11:21 +00003210 // Did we cache out?
3211 if (!Pred || stop)
3212 return;
Mike Stump11289f42009-09-09 15:08:12 +00003213
Ted Kremenek3978f792009-05-10 05:11:21 +00003214 // Get the updated binding.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003215 T = state->get<RefBindings>(Sym);
Ted Kremenek3978f792009-05-10 05:11:21 +00003216 assert(T);
3217 X = *T;
Mike Stump11289f42009-09-09 15:08:12 +00003218
Ted Kremenek6bd78702009-04-29 18:50:19 +00003219 // Any leaks or other errors?
3220 if (X.isReturnedOwned() && X.getCount() == 0) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00003221 Decl const *CD = &Pred->getCodeDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003222 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00003223 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
Ted Kremenekdee56e32009-05-10 06:25:57 +00003224 RetEffect RE = Summ.getRetEffect();
3225 bool hasError = false;
3226
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003227 if (RE.getKind() != RetEffect::NoRet) {
3228 if (isGCEnabled() && RE.getObjKind() == RetEffect::ObjC) {
3229 // Things are more complicated with garbage collection. If the
3230 // returned object is suppose to be an Objective-C object, we have
3231 // a leak (as the caller expects a GC'ed object) because no
3232 // method should return ownership unless it returns a CF object.
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003233 hasError = true;
Ted Kremenek8070b822009-10-14 23:58:34 +00003234 X = X ^ RefVal::ErrorGCLeakReturned;
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003235 }
3236 else if (!RE.isOwned()) {
3237 // Either we are using GC and the returned object is a CF type
3238 // or we aren't using GC. In either case, we expect that the
Mike Stump11289f42009-09-09 15:08:12 +00003239 // enclosing method is expected to return ownership.
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003240 hasError = true;
3241 X = X ^ RefVal::ErrorLeakReturned;
3242 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00003243 }
Mike Stump11289f42009-09-09 15:08:12 +00003244
3245 if (hasError) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00003246 // Generate an error node.
Ted Kremenekdee56e32009-05-10 06:25:57 +00003247 static int ReturnOwnLeakTag = 0;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003248 state = state->set<RefBindings>(Sym, X);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003249 ExplodedNode *N =
Zhongxing Xue1190f72009-08-15 03:17:38 +00003250 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3251 &ReturnOwnLeakTag), state, Pred);
Ted Kremenekdee56e32009-05-10 06:25:57 +00003252 if (N) {
3253 CFRefReport *report =
Ted Kremenekb4e27a12009-04-30 05:51:50 +00003254 new CFRefLeakReport(*static_cast<CFRefBug*>(leakAtReturn), *this,
3255 N, Sym, Eng);
3256 BR->EmitReport(report);
3257 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00003258 }
Mike Stump11289f42009-09-09 15:08:12 +00003259 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00003260 }
3261 else if (X.isReturnedNotOwned()) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00003262 Decl const *CD = &Pred->getCodeDecl();
Ted Kremenekdee56e32009-05-10 06:25:57 +00003263 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
3264 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
3265 if (Summ.getRetEffect().isOwned()) {
3266 // Trying to return a not owned object to a caller expecting an
3267 // owned object.
Mike Stump11289f42009-09-09 15:08:12 +00003268
Ted Kremenekdee56e32009-05-10 06:25:57 +00003269 static int ReturnNotOwnedForOwnedTag = 0;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003270 state = state->set<RefBindings>(Sym, X ^ RefVal::ErrorReturnedNotOwned);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003271 if (ExplodedNode *N =
Zhongxing Xue1190f72009-08-15 03:17:38 +00003272 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3273 &ReturnNotOwnedForOwnedTag),
3274 state, Pred)) {
Ted Kremenekdee56e32009-05-10 06:25:57 +00003275 CFRefReport *report =
3276 new CFRefReport(*static_cast<CFRefBug*>(returnNotOwnedForOwned),
3277 *this, N, Sym);
3278 BR->EmitReport(report);
3279 }
3280 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00003281 }
3282 }
Ted Kremeneka506fec2008-04-17 18:12:53 +00003283}
3284
Ted Kremenek4d837282008-04-18 19:23:43 +00003285// Assumptions.
3286
Ted Kremenekf9906842009-06-18 22:57:13 +00003287const GRState* CFRefCount::EvalAssume(const GRState *state,
3288 SVal Cond, bool Assumption) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003289
3290 // FIXME: We may add to the interface of EvalAssume the list of symbols
3291 // whose assumptions have changed. For now we just iterate through the
3292 // bindings and check if any of the tracked symbols are NULL. This isn't
Mike Stump11289f42009-09-09 15:08:12 +00003293 // too bad since the number of symbols we will track in practice are
Ted Kremenek4d837282008-04-18 19:23:43 +00003294 // probably small and EvalAssume is only called at branches and a few
3295 // other places.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003296 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003297
Ted Kremenek4d837282008-04-18 19:23:43 +00003298 if (B.isEmpty())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003299 return state;
Mike Stump11289f42009-09-09 15:08:12 +00003300
3301 bool changed = false;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003302 RefBindings::Factory& RefBFactory = state->get_context<RefBindings>();
Ted Kremenek4d837282008-04-18 19:23:43 +00003303
Mike Stump11289f42009-09-09 15:08:12 +00003304 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003305 // Check if the symbol is null (or equal to any constant).
3306 // If this is the case, stop tracking the symbol.
Ted Kremenekf9906842009-06-18 22:57:13 +00003307 if (state->getSymVal(I.getKey())) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003308 changed = true;
3309 B = RefBFactory.Remove(B, I.getKey());
3310 }
3311 }
Mike Stump11289f42009-09-09 15:08:12 +00003312
Ted Kremenek87aab6c2008-08-17 03:20:02 +00003313 if (changed)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003314 state = state->set<RefBindings>(B);
Mike Stump11289f42009-09-09 15:08:12 +00003315
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00003316 return state;
Ted Kremenek4d837282008-04-18 19:23:43 +00003317}
Ted Kremenek819e9b62008-03-11 06:39:11 +00003318
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003319const GRState * CFRefCount::Update(const GRState * state, SymbolRef sym,
Ted Kremenekc52f9392009-02-24 19:15:11 +00003320 RefVal V, ArgEffect E,
3321 RefVal::Kind& hasErr) {
Ted Kremenekf68490a2009-02-18 18:54:33 +00003322
3323 // In GC mode [... release] and [... retain] do nothing.
3324 switch (E) {
3325 default: break;
3326 case IncRefMsg: E = isGCEnabled() ? DoNothing : IncRef; break;
3327 case DecRefMsg: E = isGCEnabled() ? DoNothing : DecRef; break;
Ted Kremenek10452892009-02-18 21:57:45 +00003328 case MakeCollectable: E = isGCEnabled() ? DecRef : DoNothing; break;
Mike Stump11289f42009-09-09 15:08:12 +00003329 case NewAutoreleasePool: E = isGCEnabled() ? DoNothing :
Ted Kremenek50db3d02009-02-23 17:45:03 +00003330 NewAutoreleasePool; break;
Ted Kremenekf68490a2009-02-18 18:54:33 +00003331 }
Mike Stump11289f42009-09-09 15:08:12 +00003332
Ted Kremenekea072e32009-03-17 19:42:23 +00003333 // Handle all use-after-releases.
3334 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
3335 V = V ^ RefVal::ErrorUseAfterRelease;
3336 hasErr = V.getKind();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003337 return state->set<RefBindings>(sym, V);
Mike Stump11289f42009-09-09 15:08:12 +00003338 }
3339
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003340 switch (E) {
3341 default:
3342 assert (false && "Unhandled CFRef transition.");
Mike Stump11289f42009-09-09 15:08:12 +00003343
Ted Kremenekea072e32009-03-17 19:42:23 +00003344 case Dealloc:
3345 // Any use of -dealloc in GC is *bad*.
3346 if (isGCEnabled()) {
3347 V = V ^ RefVal::ErrorDeallocGC;
3348 hasErr = V.getKind();
3349 break;
3350 }
Mike Stump11289f42009-09-09 15:08:12 +00003351
Ted Kremenekea072e32009-03-17 19:42:23 +00003352 switch (V.getKind()) {
3353 default:
3354 assert(false && "Invalid case.");
3355 case RefVal::Owned:
3356 // The object immediately transitions to the released state.
3357 V = V ^ RefVal::Released;
3358 V.clearCounts();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003359 return state->set<RefBindings>(sym, V);
Ted Kremenekea072e32009-03-17 19:42:23 +00003360 case RefVal::NotOwned:
3361 V = V ^ RefVal::ErrorDeallocNotOwned;
3362 hasErr = V.getKind();
3363 break;
Mike Stump11289f42009-09-09 15:08:12 +00003364 }
Ted Kremenekea072e32009-03-17 19:42:23 +00003365 break;
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003366
Ted Kremenek8ec8cf02009-02-25 23:11:49 +00003367 case NewAutoreleasePool:
3368 assert(!isGCEnabled());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003369 return state->add<AutoreleaseStack>(sym);
Mike Stump11289f42009-09-09 15:08:12 +00003370
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003371 case MayEscape:
3372 if (V.getKind() == RefVal::Owned) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003373 V = V ^ RefVal::NotOwned;
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003374 break;
3375 }
Ted Kremenekea072e32009-03-17 19:42:23 +00003376
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003377 // Fall-through.
Mike Stump11289f42009-09-09 15:08:12 +00003378
Ted Kremenekae529272008-07-09 18:11:16 +00003379 case DoNothingByRef:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003380 case DoNothing:
Ted Kremenekc52f9392009-02-24 19:15:11 +00003381 return state;
Ted Kremeneka0e071c2008-06-30 16:57:41 +00003382
Ted Kremenekc7832092009-01-28 21:44:40 +00003383 case Autorelease:
Ted Kremenekea072e32009-03-17 19:42:23 +00003384 if (isGCEnabled())
3385 return state;
Mike Stump11289f42009-09-09 15:08:12 +00003386
Ted Kremenek8c3f0042009-03-20 17:34:15 +00003387 // Update the autorelease counts.
3388 state = SendAutorelease(state, ARCountFactory, sym);
Ted Kremenek3a0516b2009-05-08 20:01:42 +00003389 V = V.autorelease();
Ted Kremenek2d0ff622009-05-09 01:50:57 +00003390 break;
Ted Kremenekd35272f2009-05-09 00:10:05 +00003391
Ted Kremenek821537e2008-05-06 02:41:27 +00003392 case StopTracking:
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003393 return state->remove<RefBindings>(sym);
Ted Kremenek050b91c2008-08-12 18:30:56 +00003394
Mike Stump11289f42009-09-09 15:08:12 +00003395 case IncRef:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003396 switch (V.getKind()) {
3397 default:
3398 assert(false);
3399
3400 case RefVal::Owned:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003401 case RefVal::NotOwned:
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003402 V = V + 1;
Mike Stump11289f42009-09-09 15:08:12 +00003403 break;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003404 case RefVal::Released:
Ted Kremenekea072e32009-03-17 19:42:23 +00003405 // Non-GC cases are handled above.
3406 assert(isGCEnabled());
3407 V = (V ^ RefVal::Owned) + 1;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003408 break;
Mike Stump11289f42009-09-09 15:08:12 +00003409 }
Ted Kremenek4b772092008-04-10 23:44:06 +00003410 break;
Mike Stump11289f42009-09-09 15:08:12 +00003411
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003412 case SelfOwn:
3413 V = V ^ RefVal::NotOwned;
Ted Kremenekf68490a2009-02-18 18:54:33 +00003414 // Fall-through.
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003415 case DecRef:
3416 switch (V.getKind()) {
3417 default:
Ted Kremenekea072e32009-03-17 19:42:23 +00003418 // case 'RefVal::Released' handled above.
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003419 assert (false);
Ted Kremenek050b91c2008-08-12 18:30:56 +00003420
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003421 case RefVal::Owned:
Ted Kremenek551747f2009-02-18 22:57:22 +00003422 assert(V.getCount() > 0);
3423 if (V.getCount() == 1) V = V ^ RefVal::Released;
3424 V = V - 1;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003425 break;
Mike Stump11289f42009-09-09 15:08:12 +00003426
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003427 case RefVal::NotOwned:
3428 if (V.getCount() > 0)
3429 V = V - 1;
Ted Kremenek3c03d522008-04-10 23:09:18 +00003430 else {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003431 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003432 hasErr = V.getKind();
Mike Stump11289f42009-09-09 15:08:12 +00003433 }
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003434 break;
Mike Stump11289f42009-09-09 15:08:12 +00003435
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003436 case RefVal::Released:
Ted Kremenekea072e32009-03-17 19:42:23 +00003437 // Non-GC cases are handled above.
3438 assert(isGCEnabled());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003439 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003440 hasErr = V.getKind();
Mike Stump11289f42009-09-09 15:08:12 +00003441 break;
3442 }
Ted Kremenek4b772092008-04-10 23:44:06 +00003443 break;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003444 }
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003445 return state->set<RefBindings>(sym, V);
Ted Kremenek819e9b62008-03-11 06:39:11 +00003446}
3447
Ted Kremenekce8e8812008-04-09 01:10:13 +00003448//===----------------------------------------------------------------------===//
Ted Kremenek400aae72009-02-05 06:50:21 +00003449// Handle dead symbols and end-of-path.
3450//===----------------------------------------------------------------------===//
3451
Zhongxing Xu20227f72009-08-06 01:32:16 +00003452std::pair<ExplodedNode*, const GRState *>
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003453CFRefCount::HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003454 ExplodedNode* Pred,
Ted Kremenekd35272f2009-05-09 00:10:05 +00003455 GRExprEngine &Eng,
3456 SymbolRef Sym, RefVal V, bool &stop) {
Mike Stump11289f42009-09-09 15:08:12 +00003457
Ted Kremenekd35272f2009-05-09 00:10:05 +00003458 unsigned ACnt = V.getAutoreleaseCount();
3459 stop = false;
3460
3461 // No autorelease counts? Nothing to be done.
3462 if (!ACnt)
3463 return std::make_pair(Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003464
3465 assert(!isGCEnabled() && "Autorelease counts in GC mode?");
Ted Kremenekd35272f2009-05-09 00:10:05 +00003466 unsigned Cnt = V.getCount();
Mike Stump11289f42009-09-09 15:08:12 +00003467
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003468 // FIXME: Handle sending 'autorelease' to already released object.
3469
3470 if (V.getKind() == RefVal::ReturnedOwned)
3471 ++Cnt;
Mike Stump11289f42009-09-09 15:08:12 +00003472
Ted Kremenekd35272f2009-05-09 00:10:05 +00003473 if (ACnt <= Cnt) {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003474 if (ACnt == Cnt) {
3475 V.clearCounts();
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003476 if (V.getKind() == RefVal::ReturnedOwned)
3477 V = V ^ RefVal::ReturnedNotOwned;
3478 else
3479 V = V ^ RefVal::NotOwned;
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003480 }
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003481 else {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003482 V.setCount(Cnt - ACnt);
3483 V.setAutoreleaseCount(0);
3484 }
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003485 state = state->set<RefBindings>(Sym, V);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003486 ExplodedNode *N = Bd.MakeNode(state, Pred);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003487 stop = (N == 0);
3488 return std::make_pair(N, state);
Mike Stump11289f42009-09-09 15:08:12 +00003489 }
Ted Kremenekd35272f2009-05-09 00:10:05 +00003490
3491 // Woah! More autorelease counts then retain counts left.
3492 // Emit hard error.
3493 stop = true;
3494 V = V ^ RefVal::ErrorOverAutorelease;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003495 state = state->set<RefBindings>(Sym, V);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003496
Zhongxing Xu20227f72009-08-06 01:32:16 +00003497 if (ExplodedNode *N = Bd.MakeNode(state, Pred)) {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003498 N->markAsSink();
Mike Stump11289f42009-09-09 15:08:12 +00003499
Ted Kremenek3978f792009-05-10 05:11:21 +00003500 std::string sbuf;
3501 llvm::raw_string_ostream os(sbuf);
Ted Kremenek4785e412009-05-15 06:02:08 +00003502 os << "Object over-autoreleased: object was sent -autorelease";
Ted Kremenek3978f792009-05-10 05:11:21 +00003503 if (V.getAutoreleaseCount() > 1)
3504 os << V.getAutoreleaseCount() << " times";
3505 os << " but the object has ";
3506 if (V.getCount() == 0)
3507 os << "zero (locally visible)";
3508 else
3509 os << "+" << V.getCount();
3510 os << " retain counts";
Mike Stump11289f42009-09-09 15:08:12 +00003511
Ted Kremenekd35272f2009-05-09 00:10:05 +00003512 CFRefReport *report =
3513 new CFRefReport(*static_cast<CFRefBug*>(overAutorelease),
Ted Kremenek3978f792009-05-10 05:11:21 +00003514 *this, N, Sym, os.str().c_str());
Ted Kremenekd35272f2009-05-09 00:10:05 +00003515 BR->EmitReport(report);
3516 }
Mike Stump11289f42009-09-09 15:08:12 +00003517
Zhongxing Xu20227f72009-08-06 01:32:16 +00003518 return std::make_pair((ExplodedNode*)0, state);
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003519}
Ted Kremenek884a8992009-05-08 23:09:42 +00003520
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003521const GRState *
3522CFRefCount::HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek884a8992009-05-08 23:09:42 +00003523 llvm::SmallVectorImpl<SymbolRef> &Leaked) {
Mike Stump11289f42009-09-09 15:08:12 +00003524
3525 bool hasLeak = V.isOwned() ||
Ted Kremenek884a8992009-05-08 23:09:42 +00003526 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Mike Stump11289f42009-09-09 15:08:12 +00003527
Ted Kremenek884a8992009-05-08 23:09:42 +00003528 if (!hasLeak)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003529 return state->remove<RefBindings>(sid);
Mike Stump11289f42009-09-09 15:08:12 +00003530
Ted Kremenek884a8992009-05-08 23:09:42 +00003531 Leaked.push_back(sid);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003532 return state->set<RefBindings>(sid, V ^ RefVal::ErrorLeak);
Ted Kremenek884a8992009-05-08 23:09:42 +00003533}
3534
Zhongxing Xu20227f72009-08-06 01:32:16 +00003535ExplodedNode*
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003536CFRefCount::ProcessLeaks(const GRState * state,
Ted Kremenek884a8992009-05-08 23:09:42 +00003537 llvm::SmallVectorImpl<SymbolRef> &Leaked,
3538 GenericNodeBuilder &Builder,
3539 GRExprEngine& Eng,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003540 ExplodedNode *Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00003541
Ted Kremenek884a8992009-05-08 23:09:42 +00003542 if (Leaked.empty())
3543 return Pred;
Mike Stump11289f42009-09-09 15:08:12 +00003544
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003545 // Generate an intermediate node representing the leak point.
Zhongxing Xu20227f72009-08-06 01:32:16 +00003546 ExplodedNode *N = Builder.MakeNode(state, Pred);
Mike Stump11289f42009-09-09 15:08:12 +00003547
Ted Kremenek884a8992009-05-08 23:09:42 +00003548 if (N) {
3549 for (llvm::SmallVectorImpl<SymbolRef>::iterator
3550 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00003551
3552 CFRefBug *BT = static_cast<CFRefBug*>(Pred ? leakWithinFunction
Ted Kremenek884a8992009-05-08 23:09:42 +00003553 : leakAtReturn);
3554 assert(BT && "BugType not initialized.");
3555 CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, *I, Eng);
3556 BR->EmitReport(report);
3557 }
3558 }
Mike Stump11289f42009-09-09 15:08:12 +00003559
Ted Kremenek884a8992009-05-08 23:09:42 +00003560 return N;
3561}
3562
Ted Kremenek400aae72009-02-05 06:50:21 +00003563void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003564 GREndPathNodeBuilder& Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00003565
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003566 const GRState *state = Builder.getState();
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003567 GenericNodeBuilder Bd(Builder);
Mike Stump11289f42009-09-09 15:08:12 +00003568 RefBindings B = state->get<RefBindings>();
Zhongxing Xu20227f72009-08-06 01:32:16 +00003569 ExplodedNode *Pred = 0;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003570
3571 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekd35272f2009-05-09 00:10:05 +00003572 bool stop = false;
3573 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3574 (*I).first,
Mike Stump11289f42009-09-09 15:08:12 +00003575 (*I).second, stop);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003576
3577 if (stop)
3578 return;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003579 }
Mike Stump11289f42009-09-09 15:08:12 +00003580
3581 B = state->get<RefBindings>();
3582 llvm::SmallVector<SymbolRef, 10> Leaked;
3583
Ted Kremenek884a8992009-05-08 23:09:42 +00003584 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
3585 state = HandleSymbolDeath(state, (*I).first, (*I).second, Leaked);
3586
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003587 ProcessLeaks(state, Leaked, Bd, Eng, Pred);
Ted Kremenek400aae72009-02-05 06:50:21 +00003588}
3589
Zhongxing Xu20227f72009-08-06 01:32:16 +00003590void CFRefCount::EvalDeadSymbols(ExplodedNodeSet& Dst,
Ted Kremenek400aae72009-02-05 06:50:21 +00003591 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003592 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003593 ExplodedNode* Pred,
Ted Kremenek400aae72009-02-05 06:50:21 +00003594 Stmt* S,
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003595 const GRState* state,
Ted Kremenek400aae72009-02-05 06:50:21 +00003596 SymbolReaper& SymReaper) {
Ted Kremenek884a8992009-05-08 23:09:42 +00003597
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003598 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003599
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003600 // Update counts from autorelease pools
3601 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3602 E = SymReaper.dead_end(); I != E; ++I) {
3603 SymbolRef Sym = *I;
3604 if (const RefVal* T = B.lookup(Sym)){
3605 // Use the symbol as the tag.
3606 // FIXME: This might not be as unique as we would like.
3607 GenericNodeBuilder Bd(Builder, S, Sym);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003608 bool stop = false;
3609 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3610 Sym, *T, stop);
3611 if (stop)
3612 return;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003613 }
3614 }
Mike Stump11289f42009-09-09 15:08:12 +00003615
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003616 B = state->get<RefBindings>();
Ted Kremenek884a8992009-05-08 23:09:42 +00003617 llvm::SmallVector<SymbolRef, 10> Leaked;
Mike Stump11289f42009-09-09 15:08:12 +00003618
Ted Kremenek400aae72009-02-05 06:50:21 +00003619 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00003620 E = SymReaper.dead_end(); I != E; ++I) {
Ted Kremenek884a8992009-05-08 23:09:42 +00003621 if (const RefVal* T = B.lookup(*I))
3622 state = HandleSymbolDeath(state, *I, *T, Leaked);
Mike Stump11289f42009-09-09 15:08:12 +00003623 }
3624
Ted Kremenek884a8992009-05-08 23:09:42 +00003625 static unsigned LeakPPTag = 0;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003626 {
3627 GenericNodeBuilder Bd(Builder, S, &LeakPPTag);
3628 Pred = ProcessLeaks(state, Leaked, Bd, Eng, Pred);
3629 }
Mike Stump11289f42009-09-09 15:08:12 +00003630
Ted Kremenek884a8992009-05-08 23:09:42 +00003631 // Did we cache out?
3632 if (!Pred)
3633 return;
Mike Stump11289f42009-09-09 15:08:12 +00003634
Ted Kremenek68abaa92009-02-19 23:47:02 +00003635 // Now generate a new node that nukes the old bindings.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003636 RefBindings::Factory& F = state->get_context<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003637
Ted Kremenek68abaa92009-02-19 23:47:02 +00003638 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Ted Kremenek884a8992009-05-08 23:09:42 +00003639 E = SymReaper.dead_end(); I!=E; ++I) B = F.Remove(B, *I);
Mike Stump11289f42009-09-09 15:08:12 +00003640
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003641 state = state->set<RefBindings>(B);
Ted Kremenek68abaa92009-02-19 23:47:02 +00003642 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek400aae72009-02-05 06:50:21 +00003643}
3644
Zhongxing Xu20227f72009-08-06 01:32:16 +00003645void CFRefCount::ProcessNonLeakError(ExplodedNodeSet& Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003646 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003647 Expr* NodeExpr, Expr* ErrorExpr,
3648 ExplodedNode* Pred,
Ted Kremenek400aae72009-02-05 06:50:21 +00003649 const GRState* St,
3650 RefVal::Kind hasErr, SymbolRef Sym) {
3651 Builder.BuildSinks = true;
Zhongxing Xu107f7592009-08-06 12:48:26 +00003652 ExplodedNode *N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
Mike Stump11289f42009-09-09 15:08:12 +00003653
Ted Kremenek2d0ff622009-05-09 01:50:57 +00003654 if (!N)
3655 return;
Mike Stump11289f42009-09-09 15:08:12 +00003656
Ted Kremenek400aae72009-02-05 06:50:21 +00003657 CFRefBug *BT = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003658
Ted Kremenekea072e32009-03-17 19:42:23 +00003659 switch (hasErr) {
3660 default:
3661 assert(false && "Unhandled error.");
3662 return;
3663 case RefVal::ErrorUseAfterRelease:
3664 BT = static_cast<CFRefBug*>(useAfterRelease);
Mike Stump11289f42009-09-09 15:08:12 +00003665 break;
Ted Kremenekea072e32009-03-17 19:42:23 +00003666 case RefVal::ErrorReleaseNotOwned:
3667 BT = static_cast<CFRefBug*>(releaseNotOwned);
3668 break;
3669 case RefVal::ErrorDeallocGC:
3670 BT = static_cast<CFRefBug*>(deallocGC);
3671 break;
3672 case RefVal::ErrorDeallocNotOwned:
3673 BT = static_cast<CFRefBug*>(deallocNotOwned);
3674 break;
Ted Kremenek400aae72009-02-05 06:50:21 +00003675 }
Mike Stump11289f42009-09-09 15:08:12 +00003676
Ted Kremenek48d16452009-02-18 03:48:14 +00003677 CFRefReport *report = new CFRefReport(*BT, *this, N, Sym);
Ted Kremenek400aae72009-02-05 06:50:21 +00003678 report->addRange(ErrorExpr->getSourceRange());
3679 BR->EmitReport(report);
3680}
3681
3682//===----------------------------------------------------------------------===//
Ted Kremenek4a78c3a2008-04-10 22:16:52 +00003683// Transfer function creation for external clients.
Ted Kremenek819e9b62008-03-11 06:39:11 +00003684//===----------------------------------------------------------------------===//
3685
Ted Kremenekb0f87c42008-04-30 23:47:44 +00003686GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
3687 const LangOptions& lopts) {
Ted Kremenek1f352db2008-07-22 16:21:24 +00003688 return new CFRefCount(Ctx, GCEnabled, lopts);
Mike Stump11289f42009-09-09 15:08:12 +00003689}