blob: eb1265dda7ead915109d2c4e2d6ba97fdf28cda2 [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);
Mike Stump11289f42009-09-09 15:08: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 Kremenek050b91c2008-08-12 18:30:56 +00001435 addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"),
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001436 GetNullarySelector("currentHandler", Ctx),
1437 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC)));
Mike Stump11289f42009-09-09 15:08:12 +00001438
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001439 // Create the [NSAutoreleasePool addObject:] summary.
Ted Kremenek7d79a5f2009-05-03 05:20:50 +00001440 ScratchArgs = AF.Add(ScratchArgs, 0, Autorelease);
Ted Kremenekc7832092009-01-28 21:44:40 +00001441 addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"),
1442 GetUnarySelector("addObject", Ctx),
1443 getPersistentSummary(RetEffect::MakeNoRet(),
Ted Kremenek7d4fc5b2009-02-23 02:31:16 +00001444 DoNothing, Autorelease));
Mike Stump11289f42009-09-09 15:08:12 +00001445
Ted Kremenek8a5ad392009-04-24 17:50:11 +00001446 // Create the summaries for [NSObject performSelector...]. We treat
1447 // these as 'stop tracking' for the arguments because they are often
1448 // used for delegates that can release the object. When we have better
1449 // inter-procedural analysis we can potentially do something better. This
1450 // workaround is to remove false positives.
1451 Summ = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, StopTracking);
1452 IdentifierInfo *NSObjectII = &Ctx.Idents.get("NSObject");
1453 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1454 "afterDelay", NULL);
1455 addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1456 "afterDelay", "inModes", NULL);
1457 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1458 "withObject", "waitUntilDone", NULL);
1459 addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1460 "withObject", "waitUntilDone", "modes", NULL);
1461 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1462 "withObject", "waitUntilDone", NULL);
1463 addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1464 "withObject", "waitUntilDone", "modes", NULL);
1465 addClsMethSummary(NSObjectII, Summ, "performSelectorInBackground",
1466 "withObject", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001467
Ted Kremenekf9fa3cb2009-05-14 21:29:16 +00001468 // Specially handle NSData.
1469 RetainSummary *dataWithBytesNoCopySumm =
1470 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC), DoNothing,
1471 DoNothing);
1472 addClsMethSummary("NSData", dataWithBytesNoCopySumm,
1473 "dataWithBytesNoCopy", "length", NULL);
1474 addClsMethSummary("NSData", dataWithBytesNoCopySumm,
1475 "dataWithBytesNoCopy", "length", "freeWhenDone", NULL);
Ted Kremenek0806f912008-05-06 00:30:21 +00001476}
1477
Ted Kremenekea736c52008-06-23 22:21:20 +00001478void RetainSummaryManager::InitializeMethodSummaries() {
Mike Stump11289f42009-09-09 15:08:12 +00001479
1480 assert (ScratchArgs.isEmpty());
1481
Ted Kremenek767d0742008-05-06 21:26:51 +00001482 // Create the "init" selector. It just acts as a pass-through for the
1483 // receiver.
Mike Stump11289f42009-09-09 15:08:12 +00001484 RetainSummary *InitSumm = getPersistentSummary(ObjCInitRetE, DecRefMsg);
Ted Kremenek815fbb62009-08-20 05:13:36 +00001485 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
1486
1487 // awakeAfterUsingCoder: behaves basically like an 'init' method. It
1488 // claims the receiver and returns a retained object.
1489 addNSObjectMethSummary(GetUnarySelector("awakeAfterUsingCoder", Ctx),
1490 InitSumm);
Mike Stump11289f42009-09-09 15:08:12 +00001491
Ted Kremenek767d0742008-05-06 21:26:51 +00001492 // The next methods are allocators.
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001493 RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE);
Mike Stump11289f42009-09-09 15:08:12 +00001494 RetainSummary *CFAllocSumm =
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001495 getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Mike Stump11289f42009-09-09 15:08:12 +00001496
1497 // Create the "copy" selector.
1498 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), AllocSumm);
Ted Kremenek9551ab62008-08-12 20:41:56 +00001499
Ted Kremenekbe7c56e2008-05-06 00:38:54 +00001500 // Create the "mutableCopy" selector.
Ted Kremenek10369122009-05-20 22:39:57 +00001501 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), AllocSumm);
Mike Stump11289f42009-09-09 15:08:12 +00001502
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001503 // Create the "retain" selector.
Ted Kremenek9157fbb2009-05-07 23:40:42 +00001504 RetEffect E = RetEffect::MakeReceiverAlias();
Ted Kremenek10369122009-05-20 22:39:57 +00001505 RetainSummary *Summ = getPersistentSummary(E, IncRefMsg);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001506 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001507
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001508 // Create the "release" selector.
Ted Kremenekf68490a2009-02-18 18:54:33 +00001509 Summ = getPersistentSummary(E, DecRefMsg);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001510 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001511
Ted Kremenekbcdb4682008-05-07 21:17:39 +00001512 // Create the "drain" selector.
1513 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001514 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001515
Ted Kremenekea072e32009-03-17 19:42:23 +00001516 // Create the -dealloc summary.
1517 Summ = getPersistentSummary(RetEffect::MakeNoRet(), Dealloc);
1518 addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ);
Ted Kremenekb0862dc2008-05-06 02:26:56 +00001519
1520 // Create the "autorelease" selector.
Ted Kremenekc7832092009-01-28 21:44:40 +00001521 Summ = getPersistentSummary(E, Autorelease);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001522 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Mike Stump11289f42009-09-09 15:08:12 +00001523
Ted Kremenek50db3d02009-02-23 17:45:03 +00001524 // Specially handle NSAutoreleasePool.
Ted Kremenekdce78462009-02-25 02:54:57 +00001525 addInstMethSummary("NSAutoreleasePool", "init",
Ted Kremenek50db3d02009-02-23 17:45:03 +00001526 getPersistentSummary(RetEffect::MakeReceiverAlias(),
Ted Kremenekdce78462009-02-25 02:54:57 +00001527 NewAutoreleasePool));
Mike Stump11289f42009-09-09 15:08:12 +00001528
1529 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremeneke73f2822009-02-23 02:51:29 +00001530 // FIXME: For now we opt for false negatives with NSWindow, as these objects
1531 // self-own themselves. However, they only do this once they are displayed.
1532 // Thus, we need to track an NSWindow's display status.
1533 // This is tracked in <rdar://problem/6062711>.
Ted Kremenek00dfe302009-03-04 23:30:42 +00001534 // See also http://llvm.org/bugs/show_bug.cgi?id=3714.
Ted Kremenek1272f702009-05-12 20:06:54 +00001535 RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet(),
1536 StopTracking,
1537 StopTracking);
Mike Stump11289f42009-09-09 15:08:12 +00001538
Ted Kremenek751e7e32009-04-03 19:02:51 +00001539 addClassMethSummary("NSWindow", "alloc", NoTrackYet);
1540
Ted Kremenek00dfe302009-03-04 23:30:42 +00001541#if 0
Ted Kremenek1272f702009-05-12 20:06:54 +00001542 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001543 "styleMask", "backing", "defer", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001544
Ted Kremenek1272f702009-05-12 20:06:54 +00001545 addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001546 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek00dfe302009-03-04 23:30:42 +00001547#endif
Mike Stump11289f42009-09-09 15:08:12 +00001548
Ted Kremenek3f13f592008-08-12 18:48:50 +00001549 // For NSPanel (which subclasses NSWindow), allocated objects are not
1550 // self-owned.
Ted Kremenek751e7e32009-04-03 19:02:51 +00001551 // FIXME: For now we don't track NSPanels. object for the same reason
1552 // as for NSWindow objects.
1553 addClassMethSummary("NSPanel", "alloc", NoTrackYet);
Mike Stump11289f42009-09-09 15:08:12 +00001554
Ted Kremenek1272f702009-05-12 20:06:54 +00001555#if 0
1556 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001557 "styleMask", "backing", "defer", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001558
Ted Kremenek1272f702009-05-12 20:06:54 +00001559 addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
Ted Kremenek3f13f592008-08-12 18:48:50 +00001560 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek1272f702009-05-12 20:06:54 +00001561#endif
Mike Stump11289f42009-09-09 15:08:12 +00001562
Ted Kremenek501ba032009-05-18 23:14:34 +00001563 // Don't track allocated autorelease pools yet, as it is okay to prematurely
1564 // exit a method.
1565 addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001566
Ted Kremenek3b2294c2008-07-18 17:24:20 +00001567 // Create NSAssertionHandler summaries.
Ted Kremenek050b91c2008-08-12 18:30:56 +00001568 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
Mike Stump11289f42009-09-09 15:08:12 +00001569 "lineNumber", "description", NULL);
1570
Ted Kremenek050b91c2008-08-12 18:30:56 +00001571 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1572 "file", "lineNumber", "description", NULL);
Mike Stump11289f42009-09-09 15:08:12 +00001573
Ted Kremenek10369122009-05-20 22:39:57 +00001574 // Create summaries QCRenderer/QCView -createSnapShotImageOfType:
1575 addInstMethSummary("QCRenderer", AllocSumm,
1576 "createSnapshotImageOfType", NULL);
1577 addInstMethSummary("QCView", AllocSumm,
1578 "createSnapshotImageOfType", NULL);
1579
Ted Kremenek96aa1462009-06-15 20:58:58 +00001580 // Create summaries for CIContext, 'createCGImage' and
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001581 // 'createCGLayerWithSize'. These objects are CF objects, and are not
1582 // automatically garbage collected.
1583 addInstMethSummary("CIContext", CFAllocSumm,
Ted Kremenek10369122009-05-20 22:39:57 +00001584 "createCGImage", "fromRect", NULL);
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001585 addInstMethSummary("CIContext", CFAllocSumm,
Mike Stump11289f42009-09-09 15:08:12 +00001586 "createCGImage", "fromRect", "format", "colorSpace", NULL);
Ted Kremenek52ac2b52009-08-28 19:52:12 +00001587 addInstMethSummary("CIContext", CFAllocSumm, "createCGLayerWithSize",
Ted Kremenek96aa1462009-06-15 20:58:58 +00001588 "info", NULL);
Ted Kremenekbe7c56e2008-05-06 00:38:54 +00001589}
1590
Ted Kremenek00daccd2008-05-05 22:11:16 +00001591//===----------------------------------------------------------------------===//
Ted Kremenek71454892008-04-16 20:40:59 +00001592// Reference-counting logic (typestate + counts).
Ted Kremenek819e9b62008-03-11 06:39:11 +00001593//===----------------------------------------------------------------------===//
1594
Ted Kremenek819e9b62008-03-11 06:39:11 +00001595namespace {
Mike Stump11289f42009-09-09 15:08:12 +00001596
Ted Kremenekc8bef6a2008-04-09 23:49:11 +00001597class VISIBILITY_HIDDEN RefVal {
Mike Stump11289f42009-09-09 15:08:12 +00001598public:
Ted Kremeneka506fec2008-04-17 18:12:53 +00001599 enum Kind {
Mike Stump11289f42009-09-09 15:08:12 +00001600 Owned = 0, // Owning reference.
1601 NotOwned, // Reference is not owned by still valid (not freed).
Ted Kremeneka506fec2008-04-17 18:12:53 +00001602 Released, // Object has been released.
1603 ReturnedOwned, // Returned object passes ownership to caller.
1604 ReturnedNotOwned, // Return object does not pass ownership to caller.
Ted Kremenekea072e32009-03-17 19:42:23 +00001605 ERROR_START,
1606 ErrorDeallocNotOwned, // -dealloc called on non-owned object.
1607 ErrorDeallocGC, // Calling -dealloc with GC enabled.
Mike Stump11289f42009-09-09 15:08:12 +00001608 ErrorUseAfterRelease, // Object used after released.
Ted Kremeneka506fec2008-04-17 18:12:53 +00001609 ErrorReleaseNotOwned, // Release of an object that was not owned.
Ted Kremenekea072e32009-03-17 19:42:23 +00001610 ERROR_LEAK_START,
Ted Kremenek631ff232008-10-22 23:56:21 +00001611 ErrorLeak, // A memory leak due to excessive reference counts.
Ted Kremenekd35272f2009-05-09 00:10:05 +00001612 ErrorLeakReturned, // A memory leak due to the returning method not having
1613 // the correct naming conventions.
Ted Kremenekdee56e32009-05-10 06:25:57 +00001614 ErrorGCLeakReturned,
1615 ErrorOverAutorelease,
1616 ErrorReturnedNotOwned
Ted Kremeneka506fec2008-04-17 18:12:53 +00001617 };
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001618
Mike Stump11289f42009-09-09 15:08:12 +00001619private:
Ted Kremeneka506fec2008-04-17 18:12:53 +00001620 Kind kind;
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001621 RetEffect::ObjKind okind;
Ted Kremeneka506fec2008-04-17 18:12:53 +00001622 unsigned Cnt;
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001623 unsigned ACnt;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001624 QualType T;
1625
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001626 RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t)
1627 : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {}
Ted Kremenekbf9d8042008-03-11 17:48:22 +00001628
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001629 RefVal(Kind k, unsigned cnt = 0)
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001630 : kind(k), okind(RetEffect::AnyObj), Cnt(cnt), ACnt(0) {}
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001631
Mike Stump11289f42009-09-09 15:08:12 +00001632public:
Ted Kremeneka506fec2008-04-17 18:12:53 +00001633 Kind getKind() const { return kind; }
Mike Stump11289f42009-09-09 15:08:12 +00001634
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001635 RetEffect::ObjKind getObjKind() const { return okind; }
Ted Kremenekbf9d8042008-03-11 17:48:22 +00001636
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001637 unsigned getCount() const { return Cnt; }
1638 unsigned getAutoreleaseCount() const { return ACnt; }
1639 unsigned getCombinedCounts() const { return Cnt + ACnt; }
1640 void clearCounts() { Cnt = 0; ACnt = 0; }
Ted Kremenekd35272f2009-05-09 00:10:05 +00001641 void setCount(unsigned i) { Cnt = i; }
1642 void setAutoreleaseCount(unsigned i) { ACnt = i; }
Mike Stump11289f42009-09-09 15:08:12 +00001643
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001644 QualType getType() const { return T; }
Mike Stump11289f42009-09-09 15:08:12 +00001645
Ted Kremeneka506fec2008-04-17 18:12:53 +00001646 // Useful predicates.
Mike Stump11289f42009-09-09 15:08:12 +00001647
Ted Kremenekea072e32009-03-17 19:42:23 +00001648 static bool isError(Kind k) { return k >= ERROR_START; }
Mike Stump11289f42009-09-09 15:08:12 +00001649
Ted Kremenekea072e32009-03-17 19:42:23 +00001650 static bool isLeak(Kind k) { return k >= ERROR_LEAK_START; }
Mike Stump11289f42009-09-09 15:08:12 +00001651
Ted Kremenek8784a7c2008-04-11 22:25:11 +00001652 bool isOwned() const {
1653 return getKind() == Owned;
1654 }
Mike Stump11289f42009-09-09 15:08:12 +00001655
Ted Kremenekcbf4c612008-04-16 22:32:20 +00001656 bool isNotOwned() const {
1657 return getKind() == NotOwned;
1658 }
Mike Stump11289f42009-09-09 15:08:12 +00001659
Ted Kremeneka506fec2008-04-17 18:12:53 +00001660 bool isReturnedOwned() const {
1661 return getKind() == ReturnedOwned;
1662 }
Mike Stump11289f42009-09-09 15:08:12 +00001663
Ted Kremeneka506fec2008-04-17 18:12:53 +00001664 bool isReturnedNotOwned() const {
1665 return getKind() == ReturnedNotOwned;
1666 }
Mike Stump11289f42009-09-09 15:08:12 +00001667
Ted Kremeneka506fec2008-04-17 18:12:53 +00001668 bool isNonLeakError() const {
1669 Kind k = getKind();
1670 return isError(k) && !isLeak(k);
1671 }
Mike Stump11289f42009-09-09 15:08:12 +00001672
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001673 static RefVal makeOwned(RetEffect::ObjKind o, QualType t,
1674 unsigned Count = 1) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001675 return RefVal(Owned, o, Count, 0, t);
Ted Kremenek3c03d522008-04-10 23:09:18 +00001676 }
Mike Stump11289f42009-09-09 15:08:12 +00001677
Ted Kremenekaeb115f2009-01-28 05:56:51 +00001678 static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t,
1679 unsigned Count = 0) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001680 return RefVal(NotOwned, o, Count, 0, t);
Ted Kremenek3c03d522008-04-10 23:09:18 +00001681 }
Mike Stump11289f42009-09-09 15:08:12 +00001682
Ted Kremeneka506fec2008-04-17 18:12:53 +00001683 // Comparison, profiling, and pretty-printing.
Mike Stump11289f42009-09-09 15:08:12 +00001684
Ted Kremeneka506fec2008-04-17 18:12:53 +00001685 bool operator==(const RefVal& X) const {
Ted Kremenek3978f792009-05-10 05:11:21 +00001686 return kind == X.kind && Cnt == X.Cnt && T == X.T && ACnt == X.ACnt;
Ted Kremeneka506fec2008-04-17 18:12:53 +00001687 }
Mike Stump11289f42009-09-09 15:08:12 +00001688
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001689 RefVal operator-(size_t i) const {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001690 return RefVal(getKind(), getObjKind(), getCount() - i,
1691 getAutoreleaseCount(), getType());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001692 }
Mike Stump11289f42009-09-09 15:08:12 +00001693
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001694 RefVal operator+(size_t i) const {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001695 return RefVal(getKind(), getObjKind(), getCount() + i,
1696 getAutoreleaseCount(), getType());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001697 }
Mike Stump11289f42009-09-09 15:08:12 +00001698
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001699 RefVal operator^(Kind k) const {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001700 return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(),
1701 getType());
1702 }
Mike Stump11289f42009-09-09 15:08:12 +00001703
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001704 RefVal autorelease() const {
1705 return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1,
1706 getType());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001707 }
Mike Stump11289f42009-09-09 15:08:12 +00001708
Ted Kremeneka506fec2008-04-17 18:12:53 +00001709 void Profile(llvm::FoldingSetNodeID& ID) const {
1710 ID.AddInteger((unsigned) kind);
1711 ID.AddInteger(Cnt);
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001712 ID.AddInteger(ACnt);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001713 ID.Add(T);
Ted Kremeneka506fec2008-04-17 18:12:53 +00001714 }
1715
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001716 void print(llvm::raw_ostream& Out) const;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00001717};
Mike Stump11289f42009-09-09 15:08:12 +00001718
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001719void RefVal::print(llvm::raw_ostream& Out) const {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00001720 if (!T.isNull())
1721 Out << "Tracked Type:" << T.getAsString() << '\n';
Mike Stump11289f42009-09-09 15:08:12 +00001722
Ted Kremenek2a723e62008-03-11 19:44:10 +00001723 switch (getKind()) {
1724 default: assert(false);
Mike Stump11289f42009-09-09 15:08:12 +00001725 case Owned: {
Ted Kremenek3c03d522008-04-10 23:09:18 +00001726 Out << "Owned";
1727 unsigned cnt = getCount();
1728 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek2a723e62008-03-11 19:44:10 +00001729 break;
Ted Kremenek3c03d522008-04-10 23:09:18 +00001730 }
Mike Stump11289f42009-09-09 15:08:12 +00001731
Ted Kremenek3c03d522008-04-10 23:09:18 +00001732 case NotOwned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00001733 Out << "NotOwned";
Ted Kremenek3c03d522008-04-10 23:09:18 +00001734 unsigned cnt = getCount();
1735 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek2a723e62008-03-11 19:44:10 +00001736 break;
Ted Kremenek3c03d522008-04-10 23:09:18 +00001737 }
Mike Stump11289f42009-09-09 15:08:12 +00001738
1739 case ReturnedOwned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00001740 Out << "ReturnedOwned";
1741 unsigned cnt = getCount();
1742 if (cnt) Out << " (+ " << cnt << ")";
1743 break;
1744 }
Mike Stump11289f42009-09-09 15:08:12 +00001745
Ted Kremeneka506fec2008-04-17 18:12:53 +00001746 case ReturnedNotOwned: {
1747 Out << "ReturnedNotOwned";
1748 unsigned cnt = getCount();
1749 if (cnt) Out << " (+ " << cnt << ")";
1750 break;
1751 }
Mike Stump11289f42009-09-09 15:08:12 +00001752
Ted Kremenek2a723e62008-03-11 19:44:10 +00001753 case Released:
1754 Out << "Released";
1755 break;
Ted Kremenekea072e32009-03-17 19:42:23 +00001756
1757 case ErrorDeallocGC:
1758 Out << "-dealloc (GC)";
1759 break;
Mike Stump11289f42009-09-09 15:08:12 +00001760
Ted Kremenekea072e32009-03-17 19:42:23 +00001761 case ErrorDeallocNotOwned:
1762 Out << "-dealloc (not-owned)";
1763 break;
Mike Stump11289f42009-09-09 15:08:12 +00001764
Ted Kremenekcbf4c612008-04-16 22:32:20 +00001765 case ErrorLeak:
1766 Out << "Leaked";
Mike Stump11289f42009-09-09 15:08:12 +00001767 break;
1768
Ted Kremenek631ff232008-10-22 23:56:21 +00001769 case ErrorLeakReturned:
1770 Out << "Leaked (Bad naming)";
1771 break;
Mike Stump11289f42009-09-09 15:08:12 +00001772
Ted Kremenekdee56e32009-05-10 06:25:57 +00001773 case ErrorGCLeakReturned:
1774 Out << "Leaked (GC-ed at return)";
1775 break;
1776
Ted Kremenek2a723e62008-03-11 19:44:10 +00001777 case ErrorUseAfterRelease:
1778 Out << "Use-After-Release [ERROR]";
1779 break;
Mike Stump11289f42009-09-09 15:08:12 +00001780
Ted Kremenek2a723e62008-03-11 19:44:10 +00001781 case ErrorReleaseNotOwned:
1782 Out << "Release of Not-Owned [ERROR]";
1783 break;
Mike Stump11289f42009-09-09 15:08:12 +00001784
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00001785 case RefVal::ErrorOverAutorelease:
1786 Out << "Over autoreleased";
1787 break;
Mike Stump11289f42009-09-09 15:08:12 +00001788
Ted Kremenekdee56e32009-05-10 06:25:57 +00001789 case RefVal::ErrorReturnedNotOwned:
1790 Out << "Non-owned object returned instead of owned";
1791 break;
Ted Kremenek2a723e62008-03-11 19:44:10 +00001792 }
Mike Stump11289f42009-09-09 15:08:12 +00001793
Ted Kremenek3a0516b2009-05-08 20:01:42 +00001794 if (ACnt) {
1795 Out << " [ARC +" << ACnt << ']';
1796 }
Ted Kremenek2a723e62008-03-11 19:44:10 +00001797}
Mike Stump11289f42009-09-09 15:08:12 +00001798
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001799} // end anonymous namespace
1800
1801//===----------------------------------------------------------------------===//
1802// RefBindings - State used to track object reference counts.
1803//===----------------------------------------------------------------------===//
Mike Stump11289f42009-09-09 15:08:12 +00001804
Ted Kremenekd8242f12008-12-05 02:27:51 +00001805typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001806static int RefBIndex = 0;
1807
1808namespace clang {
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001809 template<>
1810 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
Mike Stump11289f42009-09-09 15:08:12 +00001811 static inline void* GDMIndex() { return &RefBIndex; }
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001812 };
1813}
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001814
1815//===----------------------------------------------------------------------===//
Ted Kremenekc52f9392009-02-24 19:15:11 +00001816// AutoreleaseBindings - State used to track objects in autorelease pools.
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001817//===----------------------------------------------------------------------===//
1818
Ted Kremenekc52f9392009-02-24 19:15:11 +00001819typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts;
1820typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents;
1821typedef llvm::ImmutableList<SymbolRef> ARStack;
Ted Kremenek50db3d02009-02-23 17:45:03 +00001822
Ted Kremenekc52f9392009-02-24 19:15:11 +00001823static int AutoRCIndex = 0;
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001824static int AutoRBIndex = 0;
1825
Ted Kremenekc52f9392009-02-24 19:15:11 +00001826namespace { class VISIBILITY_HIDDEN AutoreleasePoolContents {}; }
Ted Kremenekdce78462009-02-25 02:54:57 +00001827namespace { class VISIBILITY_HIDDEN AutoreleaseStack {}; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001828
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001829namespace clang {
Ted Kremenekdce78462009-02-25 02:54:57 +00001830template<> struct GRStateTrait<AutoreleaseStack>
Ted Kremenekc52f9392009-02-24 19:15:11 +00001831 : public GRStatePartialTrait<ARStack> {
Mike Stump11289f42009-09-09 15:08:12 +00001832 static inline void* GDMIndex() { return &AutoRBIndex; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001833};
1834
1835template<> struct GRStateTrait<AutoreleasePoolContents>
1836 : public GRStatePartialTrait<ARPoolContents> {
Mike Stump11289f42009-09-09 15:08:12 +00001837 static inline void* GDMIndex() { return &AutoRCIndex; }
Ted Kremenekc52f9392009-02-24 19:15:11 +00001838};
1839} // end clang namespace
Ted Kremenek0747e7e2008-10-21 15:53:15 +00001840
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001841static SymbolRef GetCurrentAutoreleasePool(const GRState* state) {
1842 ARStack stack = state->get<AutoreleaseStack>();
1843 return stack.isEmpty() ? SymbolRef() : stack.getHead();
1844}
1845
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001846static const GRState * SendAutorelease(const GRState *state,
1847 ARCounts::Factory &F, SymbolRef sym) {
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001848
1849 SymbolRef pool = GetCurrentAutoreleasePool(state);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001850 const ARCounts *cnts = state->get<AutoreleasePoolContents>(pool);
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001851 ARCounts newCnts(0);
Mike Stump11289f42009-09-09 15:08:12 +00001852
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001853 if (cnts) {
1854 const unsigned *cnt = (*cnts).lookup(sym);
1855 newCnts = F.Add(*cnts, sym, cnt ? *cnt + 1 : 1);
1856 }
1857 else
1858 newCnts = F.Add(F.GetEmptyMap(), sym, 1);
Mike Stump11289f42009-09-09 15:08:12 +00001859
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001860 return state->set<AutoreleasePoolContents>(pool, newCnts);
Ted Kremenek8c3f0042009-03-20 17:34:15 +00001861}
1862
Ted Kremenek71454892008-04-16 20:40:59 +00001863//===----------------------------------------------------------------------===//
1864// Transfer functions.
1865//===----------------------------------------------------------------------===//
1866
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00001867namespace {
Mike Stump11289f42009-09-09 15:08:12 +00001868
Ted Kremenek1642bda2009-06-26 00:05:51 +00001869class VISIBILITY_HIDDEN CFRefCount : public GRTransferFuncs {
Ted Kremenek396f4362008-04-18 03:39:05 +00001870public:
Ted Kremenek16306102008-08-13 21:24:49 +00001871 class BindingsPrinter : public GRState::Printer {
Ted Kremenek2a723e62008-03-11 19:44:10 +00001872 public:
Ted Kremenek799bb6e2009-06-24 23:06:47 +00001873 virtual void Print(llvm::raw_ostream& Out, const GRState* state,
Ted Kremenek16306102008-08-13 21:24:49 +00001874 const char* nl, const char* sep);
Ted Kremenek2a723e62008-03-11 19:44:10 +00001875 };
Ted Kremenek396f4362008-04-18 03:39:05 +00001876
1877private:
Zhongxing Xu107f7592009-08-06 12:48:26 +00001878 typedef llvm::DenseMap<const ExplodedNode*, const RetainSummary*>
Mike Stump11289f42009-09-09 15:08:12 +00001879 SummaryLogTy;
Ted Kremenek48d16452009-02-18 03:48:14 +00001880
Mike Stump11289f42009-09-09 15:08:12 +00001881 RetainSummaryManager Summaries;
Ted Kremenek48d16452009-02-18 03:48:14 +00001882 SummaryLogTy SummaryLog;
Ted Kremenek00daccd2008-05-05 22:11:16 +00001883 const LangOptions& LOpts;
Ted Kremenekc52f9392009-02-24 19:15:11 +00001884 ARCounts::Factory ARCountFactory;
Ted Kremenek87aab6c2008-08-17 03:20:02 +00001885
Ted Kremenek400aae72009-02-05 06:50:21 +00001886 BugType *useAfterRelease, *releaseNotOwned;
Ted Kremenekea072e32009-03-17 19:42:23 +00001887 BugType *deallocGC, *deallocNotOwned;
Ted Kremenek400aae72009-02-05 06:50:21 +00001888 BugType *leakWithinFunction, *leakAtReturn;
Ted Kremenekd35272f2009-05-09 00:10:05 +00001889 BugType *overAutorelease;
Ted Kremenekdee56e32009-05-10 06:25:57 +00001890 BugType *returnNotOwnedForOwned;
Ted Kremenek400aae72009-02-05 06:50:21 +00001891 BugReporter *BR;
Mike Stump11289f42009-09-09 15:08:12 +00001892
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001893 const GRState * Update(const GRState * state, SymbolRef sym, RefVal V, ArgEffect E,
Ted Kremenekc52f9392009-02-24 19:15:11 +00001894 RefVal::Kind& hasErr);
1895
Zhongxing Xu20227f72009-08-06 01:32:16 +00001896 void ProcessNonLeakError(ExplodedNodeSet& Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001897 GRStmtNodeBuilder& Builder,
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00001898 Expr* NodeExpr, Expr* ErrorExpr,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001899 ExplodedNode* Pred,
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +00001900 const GRState* St,
Ted Kremenekd8242f12008-12-05 02:27:51 +00001901 RefVal::Kind hasErr, SymbolRef Sym);
Mike Stump11289f42009-09-09 15:08:12 +00001902
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001903 const GRState * HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek884a8992009-05-08 23:09:42 +00001904 llvm::SmallVectorImpl<SymbolRef> &Leaked);
Mike Stump11289f42009-09-09 15:08:12 +00001905
Zhongxing Xu20227f72009-08-06 01:32:16 +00001906 ExplodedNode* ProcessLeaks(const GRState * state,
Ted Kremenek884a8992009-05-08 23:09:42 +00001907 llvm::SmallVectorImpl<SymbolRef> &Leaked,
1908 GenericNodeBuilder &Builder,
1909 GRExprEngine &Eng,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001910 ExplodedNode *Pred = 0);
Mike Stump11289f42009-09-09 15:08:12 +00001911
1912public:
Ted Kremenek1f352db2008-07-22 16:21:24 +00001913 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek4b7ca772008-04-29 05:33:51 +00001914 : Summaries(Ctx, gcenabled),
Ted Kremenekea072e32009-03-17 19:42:23 +00001915 LOpts(lopts), useAfterRelease(0), releaseNotOwned(0),
1916 deallocGC(0), deallocNotOwned(0),
Ted Kremenekdee56e32009-05-10 06:25:57 +00001917 leakWithinFunction(0), leakAtReturn(0), overAutorelease(0),
1918 returnNotOwnedForOwned(0), BR(0) {}
Mike Stump11289f42009-09-09 15:08:12 +00001919
Ted Kremenek400aae72009-02-05 06:50:21 +00001920 virtual ~CFRefCount() {}
Mike Stump11289f42009-09-09 15:08:12 +00001921
Ted Kremenekfc5d0672009-02-04 23:49:09 +00001922 void RegisterChecks(BugReporter &BR);
Mike Stump11289f42009-09-09 15:08:12 +00001923
Ted Kremenekceba6ea2008-08-16 00:49:49 +00001924 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1925 Printers.push_back(new BindingsPrinter());
Ted Kremenek2a723e62008-03-11 19:44:10 +00001926 }
Mike Stump11289f42009-09-09 15:08:12 +00001927
Ted Kremenek00daccd2008-05-05 22:11:16 +00001928 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenekb0f87c42008-04-30 23:47:44 +00001929 const LangOptions& getLangOptions() const { return LOpts; }
Mike Stump11289f42009-09-09 15:08:12 +00001930
Zhongxing Xu20227f72009-08-06 01:32:16 +00001931 const RetainSummary *getSummaryOfNode(const ExplodedNode *N) const {
Ted Kremenek48d16452009-02-18 03:48:14 +00001932 SummaryLogTy::const_iterator I = SummaryLog.find(N);
1933 return I == SummaryLog.end() ? 0 : I->second;
1934 }
Mike Stump11289f42009-09-09 15:08:12 +00001935
Ted Kremenek819e9b62008-03-11 06:39:11 +00001936 // Calls.
Ted Kremenek00daccd2008-05-05 22:11:16 +00001937
Zhongxing Xu20227f72009-08-06 01:32:16 +00001938 void EvalSummary(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00001939 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001940 GRStmtNodeBuilder& Builder,
Ted Kremenek00daccd2008-05-05 22:11:16 +00001941 Expr* Ex,
1942 Expr* Receiver,
Ted Kremenekff606a12009-05-04 04:57:00 +00001943 const RetainSummary& Summ,
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00001944 ExprIterator arg_beg, ExprIterator arg_end,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001945 ExplodedNode* Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001946
Zhongxing Xu20227f72009-08-06 01:32:16 +00001947 virtual void EvalCall(ExplodedNodeSet& Dst,
Ted Kremenek626bd2d2008-03-12 21:06:49 +00001948 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001949 GRStmtNodeBuilder& Builder,
Zhongxing Xu27f17422008-10-17 05:57:07 +00001950 CallExpr* CE, SVal L,
Mike Stump11289f42009-09-09 15:08:12 +00001951 ExplodedNode* Pred);
1952
1953
Zhongxing Xu20227f72009-08-06 01:32:16 +00001954 virtual void EvalObjCMessageExpr(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001955 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001956 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001957 ObjCMessageExpr* ME,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001958 ExplodedNode* Pred);
Mike Stump11289f42009-09-09 15:08:12 +00001959
Zhongxing Xu20227f72009-08-06 01:32:16 +00001960 bool EvalObjCMessageExprAux(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001961 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001962 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001963 ObjCMessageExpr* ME,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001964 ExplodedNode* Pred);
Ted Kremenek748c7ce2008-04-15 23:44:31 +00001965
Mike Stump11289f42009-09-09 15:08:12 +00001966 // Stores.
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00001967 virtual void EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val);
1968
Ted Kremenek8784a7c2008-04-11 22:25:11 +00001969 // End-of-path.
Mike Stump11289f42009-09-09 15:08:12 +00001970
Ted Kremenek8784a7c2008-04-11 22:25:11 +00001971 virtual void EvalEndPath(GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001972 GREndPathNodeBuilder& Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001973
Zhongxing Xu20227f72009-08-06 01:32:16 +00001974 virtual void EvalDeadSymbols(ExplodedNodeSet& Dst,
Ted Kremenekb0daf2f2008-04-24 23:57:27 +00001975 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001976 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001977 ExplodedNode* Pred,
Ted Kremenek16fbfe62009-01-21 22:26:05 +00001978 Stmt* S, const GRState* state,
1979 SymbolReaper& SymReaper);
Mike Stump11289f42009-09-09 15:08:12 +00001980
Zhongxing Xu20227f72009-08-06 01:32:16 +00001981 std::pair<ExplodedNode*, const GRState *>
Ted Kremenekd93c6e32009-06-18 01:23:53 +00001982 HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001983 ExplodedNode* Pred, GRExprEngine &Eng,
Ted Kremenekd35272f2009-05-09 00:10:05 +00001984 SymbolRef Sym, RefVal V, bool &stop);
Ted Kremeneka506fec2008-04-17 18:12:53 +00001985 // Return statements.
Mike Stump11289f42009-09-09 15:08:12 +00001986
Zhongxing Xu20227f72009-08-06 01:32:16 +00001987 virtual void EvalReturn(ExplodedNodeSet& Dst,
Ted Kremeneka506fec2008-04-17 18:12:53 +00001988 GRExprEngine& Engine,
Zhongxing Xu107f7592009-08-06 12:48:26 +00001989 GRStmtNodeBuilder& Builder,
Ted Kremeneka506fec2008-04-17 18:12:53 +00001990 ReturnStmt* S,
Zhongxing Xu20227f72009-08-06 01:32:16 +00001991 ExplodedNode* Pred);
Ted Kremenek4d837282008-04-18 19:23:43 +00001992
1993 // Assumptions.
1994
Ted Kremenekf9906842009-06-18 22:57:13 +00001995 virtual const GRState *EvalAssume(const GRState* state, SVal condition,
1996 bool assumption);
Ted Kremenek819e9b62008-03-11 06:39:11 +00001997};
1998
1999} // end anonymous namespace
2000
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002001static void PrintPool(llvm::raw_ostream &Out, SymbolRef Sym,
2002 const GRState *state) {
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002003 Out << ' ';
Ted Kremenek3e31c262009-03-26 03:35:11 +00002004 if (Sym)
2005 Out << Sym->getSymbolID();
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002006 else
2007 Out << "<pool>";
2008 Out << ":{";
Mike Stump11289f42009-09-09 15:08:12 +00002009
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002010 // Get the contents of the pool.
2011 if (const ARCounts *cnts = state->get<AutoreleasePoolContents>(Sym))
2012 for (ARCounts::iterator J=cnts->begin(), EJ=cnts->end(); J != EJ; ++J)
2013 Out << '(' << J.getKey() << ',' << J.getData() << ')';
2014
Mike Stump11289f42009-09-09 15:08:12 +00002015 Out << '}';
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002016}
Ted Kremenek396f4362008-04-18 03:39:05 +00002017
Ted Kremenek799bb6e2009-06-24 23:06:47 +00002018void CFRefCount::BindingsPrinter::Print(llvm::raw_ostream& Out,
2019 const GRState* state,
Ted Kremenek16306102008-08-13 21:24:49 +00002020 const char* nl, const char* sep) {
Mike Stump11289f42009-09-09 15:08:12 +00002021
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00002022 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00002023
Ted Kremenek16306102008-08-13 21:24:49 +00002024 if (!B.isEmpty())
Ted Kremenek2a723e62008-03-11 19:44:10 +00002025 Out << sep << nl;
Mike Stump11289f42009-09-09 15:08:12 +00002026
Ted Kremenek2a723e62008-03-11 19:44:10 +00002027 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
2028 Out << (*I).first << " : ";
2029 (*I).second.print(Out);
2030 Out << nl;
2031 }
Mike Stump11289f42009-09-09 15:08:12 +00002032
Ted Kremenekdce78462009-02-25 02:54:57 +00002033 // Print the autorelease stack.
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002034 Out << sep << nl << "AR pool stack:";
Ted Kremenekdce78462009-02-25 02:54:57 +00002035 ARStack stack = state->get<AutoreleaseStack>();
Mike Stump11289f42009-09-09 15:08:12 +00002036
Ted Kremenek8c3f0042009-03-20 17:34:15 +00002037 PrintPool(Out, SymbolRef(), state); // Print the caller's pool.
2038 for (ARStack::iterator I=stack.begin(), E=stack.end(); I!=E; ++I)
2039 PrintPool(Out, *I, state);
2040
2041 Out << nl;
Ted Kremenek2a723e62008-03-11 19:44:10 +00002042}
2043
Ted Kremenek6bd78702009-04-29 18:50:19 +00002044//===----------------------------------------------------------------------===//
2045// Error reporting.
2046//===----------------------------------------------------------------------===//
2047
2048namespace {
Mike Stump11289f42009-09-09 15:08:12 +00002049
Ted Kremenek6bd78702009-04-29 18:50:19 +00002050 //===-------------===//
2051 // Bug Descriptions. //
Mike Stump11289f42009-09-09 15:08:12 +00002052 //===-------------===//
2053
Ted Kremenek6bd78702009-04-29 18:50:19 +00002054 class VISIBILITY_HIDDEN CFRefBug : public BugType {
2055 protected:
2056 CFRefCount& TF;
Mike Stump11289f42009-09-09 15:08:12 +00002057
2058 CFRefBug(CFRefCount* tf, const char* name)
2059 : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {}
Ted Kremenek6bd78702009-04-29 18:50:19 +00002060 public:
Mike Stump11289f42009-09-09 15:08:12 +00002061
Ted Kremenek6bd78702009-04-29 18:50:19 +00002062 CFRefCount& getTF() { return TF; }
2063 const CFRefCount& getTF() const { return TF; }
Mike Stump11289f42009-09-09 15:08:12 +00002064
Ted Kremenek6bd78702009-04-29 18:50:19 +00002065 // FIXME: Eventually remove.
2066 virtual const char* getDescription() const = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002067
Ted Kremenek6bd78702009-04-29 18:50:19 +00002068 virtual bool isLeak() const { return false; }
2069 };
Mike Stump11289f42009-09-09 15:08:12 +00002070
Ted Kremenek6bd78702009-04-29 18:50:19 +00002071 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2072 public:
2073 UseAfterRelease(CFRefCount* tf)
2074 : CFRefBug(tf, "Use-after-release") {}
Mike Stump11289f42009-09-09 15:08:12 +00002075
Ted Kremenek6bd78702009-04-29 18:50:19 +00002076 const char* getDescription() const {
2077 return "Reference-counted object is used after it is released";
Mike Stump11289f42009-09-09 15:08:12 +00002078 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002079 };
Mike Stump11289f42009-09-09 15:08:12 +00002080
Ted Kremenek6bd78702009-04-29 18:50:19 +00002081 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2082 public:
2083 BadRelease(CFRefCount* tf) : CFRefBug(tf, "Bad release") {}
Mike Stump11289f42009-09-09 15:08:12 +00002084
Ted Kremenek6bd78702009-04-29 18:50:19 +00002085 const char* getDescription() const {
Ted Kremenek5c22e112009-10-01 17:31:50 +00002086 return "Incorrect decrement of the reference count of an object that is "
2087 "not owned at this point by the caller";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002088 }
2089 };
Mike Stump11289f42009-09-09 15:08:12 +00002090
Ted Kremenek6bd78702009-04-29 18:50:19 +00002091 class VISIBILITY_HIDDEN DeallocGC : public CFRefBug {
2092 public:
Ted Kremenekd35272f2009-05-09 00:10:05 +00002093 DeallocGC(CFRefCount *tf)
2094 : CFRefBug(tf, "-dealloc called while using garbage collection") {}
Mike Stump11289f42009-09-09 15:08:12 +00002095
Ted Kremenek6bd78702009-04-29 18:50:19 +00002096 const char *getDescription() const {
Ted Kremenekd35272f2009-05-09 00:10:05 +00002097 return "-dealloc called while using garbage collection";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002098 }
2099 };
Mike Stump11289f42009-09-09 15:08:12 +00002100
Ted Kremenek6bd78702009-04-29 18:50:19 +00002101 class VISIBILITY_HIDDEN DeallocNotOwned : public CFRefBug {
2102 public:
Ted Kremenekd35272f2009-05-09 00:10:05 +00002103 DeallocNotOwned(CFRefCount *tf)
2104 : CFRefBug(tf, "-dealloc sent to non-exclusively owned object") {}
Mike Stump11289f42009-09-09 15:08:12 +00002105
Ted Kremenek6bd78702009-04-29 18:50:19 +00002106 const char *getDescription() const {
2107 return "-dealloc sent to object that may be referenced elsewhere";
2108 }
Mike Stump11289f42009-09-09 15:08:12 +00002109 };
2110
Ted Kremenekd35272f2009-05-09 00:10:05 +00002111 class VISIBILITY_HIDDEN OverAutorelease : public CFRefBug {
2112 public:
Mike Stump11289f42009-09-09 15:08:12 +00002113 OverAutorelease(CFRefCount *tf) :
Ted Kremenekd35272f2009-05-09 00:10:05 +00002114 CFRefBug(tf, "Object sent -autorelease too many times") {}
Mike Stump11289f42009-09-09 15:08:12 +00002115
Ted Kremenekd35272f2009-05-09 00:10:05 +00002116 const char *getDescription() const {
Ted Kremenek3978f792009-05-10 05:11:21 +00002117 return "Object sent -autorelease too many times";
Ted Kremenekd35272f2009-05-09 00:10:05 +00002118 }
2119 };
Mike Stump11289f42009-09-09 15:08:12 +00002120
Ted Kremenekdee56e32009-05-10 06:25:57 +00002121 class VISIBILITY_HIDDEN ReturnedNotOwnedForOwned : public CFRefBug {
2122 public:
2123 ReturnedNotOwnedForOwned(CFRefCount *tf) :
2124 CFRefBug(tf, "Method should return an owned object") {}
Mike Stump11289f42009-09-09 15:08:12 +00002125
Ted Kremenekdee56e32009-05-10 06:25:57 +00002126 const char *getDescription() const {
2127 return "Object with +0 retain counts returned to caller where a +1 "
2128 "(owning) retain count is expected";
2129 }
2130 };
Mike Stump11289f42009-09-09 15:08:12 +00002131
Ted Kremenek6bd78702009-04-29 18:50:19 +00002132 class VISIBILITY_HIDDEN Leak : public CFRefBug {
2133 const bool isReturn;
2134 protected:
2135 Leak(CFRefCount* tf, const char* name, bool isRet)
2136 : CFRefBug(tf, name), isReturn(isRet) {}
2137 public:
Mike Stump11289f42009-09-09 15:08:12 +00002138
Ted Kremenek6bd78702009-04-29 18:50:19 +00002139 const char* getDescription() const { return ""; }
Mike Stump11289f42009-09-09 15:08:12 +00002140
Ted Kremenek6bd78702009-04-29 18:50:19 +00002141 bool isLeak() const { return true; }
2142 };
Mike Stump11289f42009-09-09 15:08:12 +00002143
Ted Kremenek6bd78702009-04-29 18:50:19 +00002144 class VISIBILITY_HIDDEN LeakAtReturn : public Leak {
2145 public:
2146 LeakAtReturn(CFRefCount* tf, const char* name)
2147 : Leak(tf, name, true) {}
2148 };
Mike Stump11289f42009-09-09 15:08:12 +00002149
Ted Kremenek6bd78702009-04-29 18:50:19 +00002150 class VISIBILITY_HIDDEN LeakWithinFunction : public Leak {
2151 public:
2152 LeakWithinFunction(CFRefCount* tf, const char* name)
2153 : Leak(tf, name, false) {}
Mike Stump11289f42009-09-09 15:08:12 +00002154 };
2155
Ted Kremenek6bd78702009-04-29 18:50:19 +00002156 //===---------===//
2157 // Bug Reports. //
2158 //===---------===//
Mike Stump11289f42009-09-09 15:08:12 +00002159
Ted Kremenek6bd78702009-04-29 18:50:19 +00002160 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
2161 protected:
2162 SymbolRef Sym;
2163 const CFRefCount &TF;
2164 public:
2165 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002166 ExplodedNode *n, SymbolRef sym)
Ted Kremenek3978f792009-05-10 05:11:21 +00002167 : RangedBugReport(D, D.getDescription(), n), Sym(sym), TF(tf) {}
2168
2169 CFRefReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002170 ExplodedNode *n, SymbolRef sym, const char* endText)
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00002171 : RangedBugReport(D, D.getDescription(), endText, n), Sym(sym), TF(tf) {}
Mike Stump11289f42009-09-09 15:08:12 +00002172
Ted Kremenek6bd78702009-04-29 18:50:19 +00002173 virtual ~CFRefReport() {}
Mike Stump11289f42009-09-09 15:08:12 +00002174
Ted Kremenek6bd78702009-04-29 18:50:19 +00002175 CFRefBug& getBugType() {
2176 return (CFRefBug&) RangedBugReport::getBugType();
2177 }
2178 const CFRefBug& getBugType() const {
2179 return (const CFRefBug&) RangedBugReport::getBugType();
2180 }
Mike Stump11289f42009-09-09 15:08:12 +00002181
Zhongxing Xu7864b9ea2009-08-18 08:58:41 +00002182 virtual void getRanges(const SourceRange*& beg, const SourceRange*& end) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002183 if (!getBugType().isLeak())
Zhongxing Xu7864b9ea2009-08-18 08:58:41 +00002184 RangedBugReport::getRanges(beg, end);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002185 else
2186 beg = end = 0;
2187 }
Mike Stump11289f42009-09-09 15:08:12 +00002188
Ted Kremenek6bd78702009-04-29 18:50:19 +00002189 SymbolRef getSymbol() const { return Sym; }
Mike Stump11289f42009-09-09 15:08:12 +00002190
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002191 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002192 const ExplodedNode* N);
Mike Stump11289f42009-09-09 15:08:12 +00002193
Ted Kremenek6bd78702009-04-29 18:50:19 +00002194 std::pair<const char**,const char**> getExtraDescriptiveText();
Mike Stump11289f42009-09-09 15:08:12 +00002195
Zhongxing Xu20227f72009-08-06 01:32:16 +00002196 PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
2197 const ExplodedNode* PrevN,
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002198 BugReporterContext& BRC);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002199 };
Ted Kremenek3978f792009-05-10 05:11:21 +00002200
Ted Kremenek6bd78702009-04-29 18:50:19 +00002201 class VISIBILITY_HIDDEN CFRefLeakReport : public CFRefReport {
2202 SourceLocation AllocSite;
2203 const MemRegion* AllocBinding;
2204 public:
2205 CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002206 ExplodedNode *n, SymbolRef sym,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002207 GRExprEngine& Eng);
Mike Stump11289f42009-09-09 15:08:12 +00002208
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002209 PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002210 const ExplodedNode* N);
Mike Stump11289f42009-09-09 15:08:12 +00002211
Ted Kremenek6bd78702009-04-29 18:50:19 +00002212 SourceLocation getLocation() const { return AllocSite; }
Mike Stump11289f42009-09-09 15:08:12 +00002213 };
Ted Kremenek6bd78702009-04-29 18:50:19 +00002214} // end anonymous namespace
2215
2216void CFRefCount::RegisterChecks(BugReporter& BR) {
2217 useAfterRelease = new UseAfterRelease(this);
2218 BR.Register(useAfterRelease);
Mike Stump11289f42009-09-09 15:08:12 +00002219
Ted Kremenek6bd78702009-04-29 18:50:19 +00002220 releaseNotOwned = new BadRelease(this);
2221 BR.Register(releaseNotOwned);
Mike Stump11289f42009-09-09 15:08:12 +00002222
Ted Kremenek6bd78702009-04-29 18:50:19 +00002223 deallocGC = new DeallocGC(this);
2224 BR.Register(deallocGC);
Mike Stump11289f42009-09-09 15:08:12 +00002225
Ted Kremenek6bd78702009-04-29 18:50:19 +00002226 deallocNotOwned = new DeallocNotOwned(this);
2227 BR.Register(deallocNotOwned);
Mike Stump11289f42009-09-09 15:08:12 +00002228
Ted Kremenekd35272f2009-05-09 00:10:05 +00002229 overAutorelease = new OverAutorelease(this);
2230 BR.Register(overAutorelease);
Mike Stump11289f42009-09-09 15:08:12 +00002231
Ted Kremenekdee56e32009-05-10 06:25:57 +00002232 returnNotOwnedForOwned = new ReturnedNotOwnedForOwned(this);
2233 BR.Register(returnNotOwnedForOwned);
Mike Stump11289f42009-09-09 15:08:12 +00002234
Ted Kremenek6bd78702009-04-29 18:50:19 +00002235 // First register "return" leaks.
2236 const char* name = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002237
Ted Kremenek6bd78702009-04-29 18:50:19 +00002238 if (isGCEnabled())
2239 name = "Leak of returned object when using garbage collection";
2240 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
2241 name = "Leak of returned object when not using garbage collection (GC) in "
2242 "dual GC/non-GC code";
2243 else {
2244 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
2245 name = "Leak of returned object";
2246 }
Mike Stump11289f42009-09-09 15:08:12 +00002247
Ted Kremenek41129692009-09-14 22:01:32 +00002248 // Leaks should not be reported if they are post-dominated by a sink.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002249 leakAtReturn = new LeakAtReturn(this, name);
Ted Kremenek41129692009-09-14 22:01:32 +00002250 leakAtReturn->setSuppressOnSink(true);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002251 BR.Register(leakAtReturn);
Mike Stump11289f42009-09-09 15:08:12 +00002252
Ted Kremenek6bd78702009-04-29 18:50:19 +00002253 // Second, register leaks within a function/method.
2254 if (isGCEnabled())
Mike Stump11289f42009-09-09 15:08:12 +00002255 name = "Leak of object when using garbage collection";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002256 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
2257 name = "Leak of object when not using garbage collection (GC) in "
2258 "dual GC/non-GC code";
2259 else {
2260 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
2261 name = "Leak";
2262 }
Mike Stump11289f42009-09-09 15:08:12 +00002263
Ted Kremenek41129692009-09-14 22:01:32 +00002264 // Leaks should not be reported if they are post-dominated by sinks.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002265 leakWithinFunction = new LeakWithinFunction(this, name);
Ted Kremenek41129692009-09-14 22:01:32 +00002266 leakWithinFunction->setSuppressOnSink(true);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002267 BR.Register(leakWithinFunction);
Mike Stump11289f42009-09-09 15:08:12 +00002268
Ted Kremenek6bd78702009-04-29 18:50:19 +00002269 // Save the reference to the BugReporter.
2270 this->BR = &BR;
2271}
2272
2273static const char* Msgs[] = {
2274 // GC only
Mike Stump11289f42009-09-09 15:08:12 +00002275 "Code is compiled to only use garbage collection",
Ted Kremenek6bd78702009-04-29 18:50:19 +00002276 // No GC.
2277 "Code is compiled to use reference counts",
2278 // Hybrid, with GC.
2279 "Code is compiled to use either garbage collection (GC) or reference counts"
Mike Stump11289f42009-09-09 15:08:12 +00002280 " (non-GC). The bug occurs with GC enabled",
Ted Kremenek6bd78702009-04-29 18:50:19 +00002281 // Hybrid, without GC
2282 "Code is compiled to use either garbage collection (GC) or reference counts"
2283 " (non-GC). The bug occurs in non-GC mode"
2284};
2285
2286std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2287 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
Mike Stump11289f42009-09-09 15:08:12 +00002288
Ted Kremenek6bd78702009-04-29 18:50:19 +00002289 switch (TF.getLangOptions().getGCMode()) {
2290 default:
2291 assert(false);
Mike Stump11289f42009-09-09 15:08:12 +00002292
Ted Kremenek6bd78702009-04-29 18:50:19 +00002293 case LangOptions::GCOnly:
2294 assert (TF.isGCEnabled());
Mike Stump11289f42009-09-09 15:08:12 +00002295 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2296
Ted Kremenek6bd78702009-04-29 18:50:19 +00002297 case LangOptions::NonGC:
2298 assert (!TF.isGCEnabled());
2299 return std::make_pair(&Msgs[1], &Msgs[1]+1);
Mike Stump11289f42009-09-09 15:08:12 +00002300
Ted Kremenek6bd78702009-04-29 18:50:19 +00002301 case LangOptions::HybridGC:
2302 if (TF.isGCEnabled())
2303 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2304 else
2305 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2306 }
2307}
2308
2309static inline bool contains(const llvm::SmallVectorImpl<ArgEffect>& V,
2310 ArgEffect X) {
2311 for (llvm::SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end();
2312 I!=E; ++I)
2313 if (*I == X) return true;
Mike Stump11289f42009-09-09 15:08:12 +00002314
Ted Kremenek6bd78702009-04-29 18:50:19 +00002315 return false;
2316}
2317
Zhongxing Xu20227f72009-08-06 01:32:16 +00002318PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode* N,
2319 const ExplodedNode* PrevN,
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002320 BugReporterContext& BRC) {
Mike Stump11289f42009-09-09 15:08:12 +00002321
Ted Kremenek051a03d2009-05-13 07:12:33 +00002322 if (!isa<PostStmt>(N->getLocation()))
2323 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002324
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002325 // Check if the type state has changed.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002326 const GRState *PrevSt = PrevN->getState();
2327 const GRState *CurrSt = N->getState();
Mike Stump11289f42009-09-09 15:08:12 +00002328
2329 const RefVal* CurrT = CurrSt->get<RefBindings>(Sym);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002330 if (!CurrT) return NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002331
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002332 const RefVal &CurrV = *CurrT;
2333 const RefVal *PrevT = PrevSt->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002334
Ted Kremenek6bd78702009-04-29 18:50:19 +00002335 // Create a string buffer to constain all the useful things we want
2336 // to tell the user.
2337 std::string sbuf;
2338 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +00002339
Ted Kremenek6bd78702009-04-29 18:50:19 +00002340 // This is the allocation site since the previous node had no bindings
2341 // for this symbol.
2342 if (!PrevT) {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002343 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002344
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002345 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002346 // Get the name of the callee (if it is available).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002347 SVal X = CurrSt->getSValAsScalarOrLoc(CE->getCallee());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002348 if (const FunctionDecl* FD = X.getAsFunctionDecl())
2349 os << "Call to function '" << FD->getNameAsString() <<'\'';
2350 else
Mike Stump11289f42009-09-09 15:08:12 +00002351 os << "function call";
2352 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002353 else {
2354 assert (isa<ObjCMessageExpr>(S));
2355 os << "Method";
2356 }
Mike Stump11289f42009-09-09 15:08:12 +00002357
Ted Kremenek6bd78702009-04-29 18:50:19 +00002358 if (CurrV.getObjKind() == RetEffect::CF) {
2359 os << " returns a Core Foundation object with a ";
2360 }
2361 else {
2362 assert (CurrV.getObjKind() == RetEffect::ObjC);
2363 os << " returns an Objective-C object with a ";
2364 }
Mike Stump11289f42009-09-09 15:08:12 +00002365
Ted Kremenek6bd78702009-04-29 18:50:19 +00002366 if (CurrV.isOwned()) {
2367 os << "+1 retain count (owning reference).";
Mike Stump11289f42009-09-09 15:08:12 +00002368
Ted Kremenek6bd78702009-04-29 18:50:19 +00002369 if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) {
2370 assert(CurrV.getObjKind() == RetEffect::CF);
2371 os << " "
2372 "Core Foundation objects are not automatically garbage collected.";
2373 }
2374 }
2375 else {
2376 assert (CurrV.isNotOwned());
2377 os << "+0 retain count (non-owning reference).";
2378 }
Mike Stump11289f42009-09-09 15:08:12 +00002379
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002380 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002381 return new PathDiagnosticEventPiece(Pos, os.str());
2382 }
Mike Stump11289f42009-09-09 15:08:12 +00002383
Ted Kremenek6bd78702009-04-29 18:50:19 +00002384 // Gather up the effects that were performed on the object at this
2385 // program point
2386 llvm::SmallVector<ArgEffect, 2> AEffects;
Mike Stump11289f42009-09-09 15:08:12 +00002387
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002388 if (const RetainSummary *Summ =
2389 TF.getSummaryOfNode(BRC.getNodeResolver().getOriginalNode(N))) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002390 // We only have summaries attached to nodes after evaluating CallExpr and
2391 // ObjCMessageExprs.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002392 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002393
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002394 if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002395 // Iterate through the parameter expressions and see if the symbol
2396 // was ever passed as an argument.
2397 unsigned i = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002398
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002399 for (CallExpr::const_arg_iterator AI=CE->arg_begin(), AE=CE->arg_end();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002400 AI!=AE; ++AI, ++i) {
Mike Stump11289f42009-09-09 15:08:12 +00002401
Ted Kremenek6bd78702009-04-29 18:50:19 +00002402 // Retrieve the value of the argument. Is it the symbol
2403 // we are interested in?
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002404 if (CurrSt->getSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym)
Ted Kremenek6bd78702009-04-29 18:50:19 +00002405 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002406
Ted Kremenek6bd78702009-04-29 18:50:19 +00002407 // We have an argument. Get the effect!
2408 AEffects.push_back(Summ->getArg(i));
2409 }
2410 }
Mike Stump11289f42009-09-09 15:08:12 +00002411 else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) {
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002412 if (const Expr *receiver = ME->getReceiver())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002413 if (CurrSt->getSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002414 // The symbol we are tracking is the receiver.
2415 AEffects.push_back(Summ->getReceiverEffect());
2416 }
2417 }
2418 }
Mike Stump11289f42009-09-09 15:08:12 +00002419
Ted Kremenek6bd78702009-04-29 18:50:19 +00002420 do {
2421 // Get the previous type state.
2422 RefVal PrevV = *PrevT;
Mike Stump11289f42009-09-09 15:08:12 +00002423
Ted Kremenek6bd78702009-04-29 18:50:19 +00002424 // Specially handle -dealloc.
2425 if (!TF.isGCEnabled() && contains(AEffects, Dealloc)) {
2426 // Determine if the object's reference count was pushed to zero.
2427 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
2428 // We may not have transitioned to 'release' if we hit an error.
2429 // This case is handled elsewhere.
2430 if (CurrV.getKind() == RefVal::Released) {
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002431 assert(CurrV.getCombinedCounts() == 0);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002432 os << "Object released by directly sending the '-dealloc' message";
2433 break;
2434 }
2435 }
Mike Stump11289f42009-09-09 15:08:12 +00002436
Ted Kremenek6bd78702009-04-29 18:50:19 +00002437 // Specially handle CFMakeCollectable and friends.
2438 if (contains(AEffects, MakeCollectable)) {
2439 // Get the name of the function.
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002440 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002441 SVal X = CurrSt->getSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002442 const FunctionDecl* FD = X.getAsFunctionDecl();
2443 const std::string& FName = FD->getNameAsString();
Mike Stump11289f42009-09-09 15:08:12 +00002444
Ted Kremenek6bd78702009-04-29 18:50:19 +00002445 if (TF.isGCEnabled()) {
2446 // Determine if the object's reference count was pushed to zero.
2447 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
Mike Stump11289f42009-09-09 15:08:12 +00002448
Ted Kremenek6bd78702009-04-29 18:50:19 +00002449 os << "In GC mode a call to '" << FName
2450 << "' decrements an object's retain count and registers the "
2451 "object with the garbage collector. ";
Mike Stump11289f42009-09-09 15:08:12 +00002452
Ted Kremenek6bd78702009-04-29 18:50:19 +00002453 if (CurrV.getKind() == RefVal::Released) {
2454 assert(CurrV.getCount() == 0);
2455 os << "Since it now has a 0 retain count the object can be "
2456 "automatically collected by the garbage collector.";
2457 }
2458 else
2459 os << "An object must have a 0 retain count to be garbage collected. "
2460 "After this call its retain count is +" << CurrV.getCount()
2461 << '.';
2462 }
Mike Stump11289f42009-09-09 15:08:12 +00002463 else
Ted Kremenek6bd78702009-04-29 18:50:19 +00002464 os << "When GC is not enabled a call to '" << FName
2465 << "' has no effect on its argument.";
Mike Stump11289f42009-09-09 15:08:12 +00002466
Ted Kremenek6bd78702009-04-29 18:50:19 +00002467 // Nothing more to say.
2468 break;
2469 }
Mike Stump11289f42009-09-09 15:08:12 +00002470
2471 // Determine if the typestate has changed.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002472 if (!(PrevV == CurrV))
2473 switch (CurrV.getKind()) {
2474 case RefVal::Owned:
2475 case RefVal::NotOwned:
Mike Stump11289f42009-09-09 15:08:12 +00002476
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002477 if (PrevV.getCount() == CurrV.getCount()) {
2478 // Did an autorelease message get sent?
2479 if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount())
2480 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00002481
Zhongxing Xu08a2ede2009-05-12 10:10:00 +00002482 assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount());
Ted Kremenek3978f792009-05-10 05:11:21 +00002483 os << "Object sent -autorelease message";
Ted Kremenek3a0516b2009-05-08 20:01:42 +00002484 break;
2485 }
Mike Stump11289f42009-09-09 15:08:12 +00002486
Ted Kremenek6bd78702009-04-29 18:50:19 +00002487 if (PrevV.getCount() > CurrV.getCount())
2488 os << "Reference count decremented.";
2489 else
2490 os << "Reference count incremented.";
Mike Stump11289f42009-09-09 15:08:12 +00002491
Ted Kremenek6bd78702009-04-29 18:50:19 +00002492 if (unsigned Count = CurrV.getCount())
2493 os << " The object now has a +" << Count << " retain count.";
Mike Stump11289f42009-09-09 15:08:12 +00002494
Ted Kremenek6bd78702009-04-29 18:50:19 +00002495 if (PrevV.getKind() == RefVal::Released) {
2496 assert(TF.isGCEnabled() && CurrV.getCount() > 0);
2497 os << " The object is not eligible for garbage collection until the "
2498 "retain count reaches 0 again.";
2499 }
Mike Stump11289f42009-09-09 15:08:12 +00002500
Ted Kremenek6bd78702009-04-29 18:50:19 +00002501 break;
Mike Stump11289f42009-09-09 15:08:12 +00002502
Ted Kremenek6bd78702009-04-29 18:50:19 +00002503 case RefVal::Released:
2504 os << "Object released.";
2505 break;
Mike Stump11289f42009-09-09 15:08:12 +00002506
Ted Kremenek6bd78702009-04-29 18:50:19 +00002507 case RefVal::ReturnedOwned:
2508 os << "Object returned to caller as an owning reference (single retain "
2509 "count transferred to caller).";
2510 break;
Mike Stump11289f42009-09-09 15:08:12 +00002511
Ted Kremenek6bd78702009-04-29 18:50:19 +00002512 case RefVal::ReturnedNotOwned:
2513 os << "Object returned to caller with a +0 (non-owning) retain count.";
2514 break;
Mike Stump11289f42009-09-09 15:08:12 +00002515
Ted Kremenek6bd78702009-04-29 18:50:19 +00002516 default:
2517 return NULL;
2518 }
Mike Stump11289f42009-09-09 15:08:12 +00002519
Ted Kremenek6bd78702009-04-29 18:50:19 +00002520 // Emit any remaining diagnostics for the argument effects (if any).
2521 for (llvm::SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(),
2522 E=AEffects.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00002523
Ted Kremenek6bd78702009-04-29 18:50:19 +00002524 // A bunch of things have alternate behavior under GC.
2525 if (TF.isGCEnabled())
2526 switch (*I) {
2527 default: break;
2528 case Autorelease:
2529 os << "In GC mode an 'autorelease' has no effect.";
2530 continue;
2531 case IncRefMsg:
2532 os << "In GC mode the 'retain' message has no effect.";
2533 continue;
2534 case DecRefMsg:
2535 os << "In GC mode the 'release' message has no effect.";
2536 continue;
2537 }
2538 }
Mike Stump11289f42009-09-09 15:08:12 +00002539 } while (0);
2540
Ted Kremenek6bd78702009-04-29 18:50:19 +00002541 if (os.str().empty())
2542 return 0; // We have nothing to say!
Ted Kremenek051a03d2009-05-13 07:12:33 +00002543
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002544 const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002545 PathDiagnosticLocation Pos(S, BRC.getSourceManager());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002546 PathDiagnosticPiece* P = new PathDiagnosticEventPiece(Pos, os.str());
Mike Stump11289f42009-09-09 15:08:12 +00002547
Ted Kremenek6bd78702009-04-29 18:50:19 +00002548 // Add the range by scanning the children of the statement for any bindings
2549 // to Sym.
Mike Stump11289f42009-09-09 15:08:12 +00002550 for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002551 I!=E; ++I)
2552 if (const Expr* Exp = dyn_cast_or_null<Expr>(*I))
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002553 if (CurrSt->getSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00002554 P->addRange(Exp->getSourceRange());
2555 break;
2556 }
Mike Stump11289f42009-09-09 15:08:12 +00002557
Ted Kremenek6bd78702009-04-29 18:50:19 +00002558 return P;
2559}
2560
2561namespace {
2562 class VISIBILITY_HIDDEN FindUniqueBinding :
2563 public StoreManager::BindingsHandler {
2564 SymbolRef Sym;
2565 const MemRegion* Binding;
2566 bool First;
Mike Stump11289f42009-09-09 15:08:12 +00002567
Ted Kremenek6bd78702009-04-29 18:50:19 +00002568 public:
2569 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
Mike Stump11289f42009-09-09 15:08:12 +00002570
Ted Kremenek6bd78702009-04-29 18:50:19 +00002571 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
2572 SVal val) {
Mike Stump11289f42009-09-09 15:08:12 +00002573
2574 SymbolRef SymV = val.getAsSymbol();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002575 if (!SymV || SymV != Sym)
2576 return true;
Mike Stump11289f42009-09-09 15:08:12 +00002577
Ted Kremenek6bd78702009-04-29 18:50:19 +00002578 if (Binding) {
2579 First = false;
2580 return false;
2581 }
2582 else
2583 Binding = R;
Mike Stump11289f42009-09-09 15:08:12 +00002584
2585 return true;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002586 }
Mike Stump11289f42009-09-09 15:08:12 +00002587
Ted Kremenek6bd78702009-04-29 18:50:19 +00002588 operator bool() { return First && Binding; }
2589 const MemRegion* getRegion() { return Binding; }
Mike Stump11289f42009-09-09 15:08:12 +00002590 };
Ted Kremenek6bd78702009-04-29 18:50:19 +00002591}
2592
Zhongxing Xu20227f72009-08-06 01:32:16 +00002593static std::pair<const ExplodedNode*,const MemRegion*>
2594GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode* N,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002595 SymbolRef Sym) {
Mike Stump11289f42009-09-09 15:08:12 +00002596
Ted Kremenek6bd78702009-04-29 18:50:19 +00002597 // Find both first node that referred to the tracked symbol and the
2598 // memory location that value was store to.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002599 const ExplodedNode* Last = N;
Mike Stump11289f42009-09-09 15:08:12 +00002600 const MemRegion* FirstBinding = 0;
2601
Ted Kremenek6bd78702009-04-29 18:50:19 +00002602 while (N) {
2603 const GRState* St = N->getState();
2604 RefBindings B = St->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00002605
Ted Kremenek6bd78702009-04-29 18:50:19 +00002606 if (!B.lookup(Sym))
2607 break;
Mike Stump11289f42009-09-09 15:08:12 +00002608
Ted Kremenek6bd78702009-04-29 18:50:19 +00002609 FindUniqueBinding FB(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002610 StateMgr.iterBindings(St, FB);
2611 if (FB) FirstBinding = FB.getRegion();
2612
Ted Kremenek6bd78702009-04-29 18:50:19 +00002613 Last = N;
Mike Stump11289f42009-09-09 15:08:12 +00002614 N = N->pred_empty() ? NULL : *(N->pred_begin());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002615 }
Mike Stump11289f42009-09-09 15:08:12 +00002616
Ted Kremenek6bd78702009-04-29 18:50:19 +00002617 return std::make_pair(Last, FirstBinding);
2618}
2619
2620PathDiagnosticPiece*
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002621CFRefReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002622 const ExplodedNode* EndN) {
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002623 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenek6bd78702009-04-29 18:50:19 +00002624 // assigned to different variables, etc.
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002625 BRC.addNotableSymbol(Sym);
2626 return RangedBugReport::getEndPath(BRC, EndN);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002627}
2628
2629PathDiagnosticPiece*
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002630CFRefLeakReport::getEndPath(BugReporterContext& BRC,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002631 const ExplodedNode* EndN){
Mike Stump11289f42009-09-09 15:08:12 +00002632
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002633 // Tell the BugReporterContext to report cases when the tracked symbol is
Ted Kremenek6bd78702009-04-29 18:50:19 +00002634 // assigned to different variables, etc.
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002635 BRC.addNotableSymbol(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002636
Ted Kremenek6bd78702009-04-29 18:50:19 +00002637 // We are reporting a leak. Walk up the graph to get to the first node where
2638 // the symbol appeared, and also get the first VarDecl that tracked object
2639 // is stored to.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002640 const ExplodedNode* AllocNode = 0;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002641 const MemRegion* FirstBinding = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002642
Ted Kremenek6bd78702009-04-29 18:50:19 +00002643 llvm::tie(AllocNode, FirstBinding) =
Ted Kremenek8c8fb482009-05-08 23:32:51 +00002644 GetAllocationSite(BRC.getStateManager(), EndN, Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002645
2646 // Get the allocate site.
Ted Kremenek6bd78702009-04-29 18:50:19 +00002647 assert(AllocNode);
Ted Kremenekbfd28fd2009-07-22 22:35:28 +00002648 const Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Mike Stump11289f42009-09-09 15:08:12 +00002649
Ted Kremenekbb8d5462009-05-06 21:39:49 +00002650 SourceManager& SMgr = BRC.getSourceManager();
Ted Kremenek6bd78702009-04-29 18:50:19 +00002651 unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart());
Mike Stump11289f42009-09-09 15:08:12 +00002652
Ted Kremenek6bd78702009-04-29 18:50:19 +00002653 // Compute an actual location for the leak. Sometimes a leak doesn't
2654 // occur at an actual statement (e.g., transition between blocks; end
2655 // of function) so we need to walk the graph and compute a real location.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002656 const ExplodedNode* LeakN = EndN;
Ted Kremenek6bd78702009-04-29 18:50:19 +00002657 PathDiagnosticLocation L;
Mike Stump11289f42009-09-09 15:08:12 +00002658
Ted Kremenek6bd78702009-04-29 18:50:19 +00002659 while (LeakN) {
2660 ProgramPoint P = LeakN->getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00002661
Ted Kremenek6bd78702009-04-29 18:50:19 +00002662 if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
2663 L = PathDiagnosticLocation(PS->getStmt()->getLocStart(), SMgr);
2664 break;
2665 }
2666 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
2667 if (const Stmt* Term = BE->getSrc()->getTerminator()) {
2668 L = PathDiagnosticLocation(Term->getLocStart(), SMgr);
2669 break;
2670 }
2671 }
Mike Stump11289f42009-09-09 15:08:12 +00002672
Ted Kremenek6bd78702009-04-29 18:50:19 +00002673 LeakN = LeakN->succ_empty() ? 0 : *(LeakN->succ_begin());
2674 }
Mike Stump11289f42009-09-09 15:08:12 +00002675
Ted Kremenek6bd78702009-04-29 18:50:19 +00002676 if (!L.isValid()) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002677 const Decl &D = EndN->getCodeDecl();
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00002678 L = PathDiagnosticLocation(D.getBodyRBrace(), SMgr);
Ted Kremenek6bd78702009-04-29 18:50:19 +00002679 }
Mike Stump11289f42009-09-09 15:08:12 +00002680
Ted Kremenek6bd78702009-04-29 18:50:19 +00002681 std::string sbuf;
2682 llvm::raw_string_ostream os(sbuf);
Mike Stump11289f42009-09-09 15:08:12 +00002683
Ted Kremenek6bd78702009-04-29 18:50:19 +00002684 os << "Object allocated on line " << AllocLine;
Mike Stump11289f42009-09-09 15:08:12 +00002685
Ted Kremenek6bd78702009-04-29 18:50:19 +00002686 if (FirstBinding)
Mike Stump11289f42009-09-09 15:08:12 +00002687 os << " and stored into '" << FirstBinding->getString() << '\'';
2688
Ted Kremenek6bd78702009-04-29 18:50:19 +00002689 // Get the retain count.
2690 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00002691
Ted Kremenek6bd78702009-04-29 18:50:19 +00002692 if (RV->getKind() == RefVal::ErrorLeakReturned) {
2693 // FIXME: Per comments in rdar://6320065, "create" only applies to CF
2694 // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership
2695 // to the caller for NS objects.
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002696 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremenek6bd78702009-04-29 18:50:19 +00002697 os << " is returned from a method whose name ('"
Ted Kremenek223a7d52009-04-29 23:03:22 +00002698 << MD.getSelector().getAsString()
Ted Kremenek6bd78702009-04-29 18:50:19 +00002699 << "') does not contain 'copy' or otherwise starts with"
2700 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenekd6bef2e2009-04-29 22:25:52 +00002701 " in the Memory Management Guide for Cocoa (object leaked)";
Ted Kremenek6bd78702009-04-29 18:50:19 +00002702 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00002703 else if (RV->getKind() == RefVal::ErrorGCLeakReturned) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00002704 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
Ted Kremenekdee56e32009-05-10 06:25:57 +00002705 os << " and returned from method '" << MD.getSelector().getAsString()
Ted Kremenek1f8e4342009-05-10 16:52:15 +00002706 << "' is potentially leaked when using garbage collection. Callers "
2707 "of this method do not expect a returned object with a +1 retain "
2708 "count since they expect the object to be managed by the garbage "
2709 "collector";
Ted Kremenekdee56e32009-05-10 06:25:57 +00002710 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00002711 else
2712 os << " is no longer referenced after this point and has a retain count of"
Ted Kremenekd6bef2e2009-04-29 22:25:52 +00002713 " +" << RV->getCount() << " (object leaked)";
Mike Stump11289f42009-09-09 15:08:12 +00002714
Ted Kremenek6bd78702009-04-29 18:50:19 +00002715 return new PathDiagnosticEventPiece(L, os.str());
2716}
2717
Ted Kremenek6bd78702009-04-29 18:50:19 +00002718CFRefLeakReport::CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002719 ExplodedNode *n,
Ted Kremenek6bd78702009-04-29 18:50:19 +00002720 SymbolRef sym, GRExprEngine& Eng)
Mike Stump11289f42009-09-09 15:08:12 +00002721: CFRefReport(D, tf, n, sym) {
2722
Ted Kremenek6bd78702009-04-29 18:50:19 +00002723 // Most bug reports are cached at the location where they occured.
2724 // With leaks, we want to unique them by the location where they were
2725 // allocated, and only report a single path. To do this, we need to find
2726 // the allocation site of a piece of tracked memory, which we do via a
2727 // call to GetAllocationSite. This will walk the ExplodedGraph backwards.
2728 // Note that this is *not* the trimmed graph; we are guaranteed, however,
2729 // that all ancestor nodes that represent the allocation site have the
2730 // same SourceLocation.
Zhongxing Xu20227f72009-08-06 01:32:16 +00002731 const ExplodedNode* AllocNode = 0;
Mike Stump11289f42009-09-09 15:08:12 +00002732
Ted Kremenek6bd78702009-04-29 18:50:19 +00002733 llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding.
Ted Kremenek8c8fb482009-05-08 23:32:51 +00002734 GetAllocationSite(Eng.getStateManager(), getEndNode(), getSymbol());
Mike Stump11289f42009-09-09 15:08:12 +00002735
Ted Kremenek6bd78702009-04-29 18:50:19 +00002736 // Get the SourceLocation for the allocation site.
2737 ProgramPoint P = AllocNode->getLocation();
2738 AllocSite = cast<PostStmt>(P).getStmt()->getLocStart();
Mike Stump11289f42009-09-09 15:08:12 +00002739
Ted Kremenek6bd78702009-04-29 18:50:19 +00002740 // Fill in the description of the bug.
2741 Description.clear();
2742 llvm::raw_string_ostream os(Description);
2743 SourceManager& SMgr = Eng.getContext().getSourceManager();
2744 unsigned AllocLine = SMgr.getInstantiationLineNumber(AllocSite);
Ted Kremenekf1e76672009-05-02 19:05:19 +00002745 os << "Potential leak ";
2746 if (tf.isGCEnabled()) {
2747 os << "(when using garbage collection) ";
Mike Stump11289f42009-09-09 15:08:12 +00002748 }
Ted Kremenekf1e76672009-05-02 19:05:19 +00002749 os << "of an object allocated on line " << AllocLine;
Mike Stump11289f42009-09-09 15:08:12 +00002750
Ted Kremenek6bd78702009-04-29 18:50:19 +00002751 // FIXME: AllocBinding doesn't get populated for RegionStore yet.
2752 if (AllocBinding)
2753 os << " and stored into '" << AllocBinding->getString() << '\'';
2754}
2755
2756//===----------------------------------------------------------------------===//
2757// Main checker logic.
2758//===----------------------------------------------------------------------===//
2759
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002760/// GetReturnType - Used to get the return type of a message expression or
2761/// function call with the intention of affixing that type to a tracked symbol.
2762/// While the the return type can be queried directly from RetEx, when
2763/// invoking class methods we augment to the return type to be that of
2764/// a pointer to the class (as opposed it just being id).
Steve Naroff7cae42b2009-07-10 23:34:53 +00002765static QualType GetReturnType(const Expr* RetE, ASTContext& Ctx) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002766 QualType RetTy = RetE->getType();
Steve Naroff7cae42b2009-07-10 23:34:53 +00002767 // If RetE is not a message expression just return its type.
2768 // If RetE is a message expression, return its types if it is something
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002769 /// more specific than id.
Steve Naroff7cae42b2009-07-10 23:34:53 +00002770 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
John McCall9dd450b2009-09-21 23:43:11 +00002771 if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
Mike Stump11289f42009-09-09 15:08:12 +00002772 if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
Steve Naroff7cae42b2009-07-10 23:34:53 +00002773 PT->isObjCClassType()) {
2774 // At this point we know the return type of the message expression is
2775 // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
2776 // is a call to a class method whose type we can resolve. In such
2777 // cases, promote the return type to XXX* (where XXX is the class).
Mike Stump11289f42009-09-09 15:08:12 +00002778 const ObjCInterfaceDecl *D = ME->getClassInfo().first;
Steve Naroff7cae42b2009-07-10 23:34:53 +00002779 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
2780 }
Mike Stump11289f42009-09-09 15:08:12 +00002781
Steve Naroff7cae42b2009-07-10 23:34:53 +00002782 return RetTy;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002783}
2784
Zhongxing Xu20227f72009-08-06 01:32:16 +00002785void CFRefCount::EvalSummary(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002786 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00002787 GRStmtNodeBuilder& Builder,
Ted Kremenek00daccd2008-05-05 22:11:16 +00002788 Expr* Ex,
2789 Expr* Receiver,
Ted Kremenekff606a12009-05-04 04:57:00 +00002790 const RetainSummary& Summ,
Zhongxing Xuac129432009-04-20 05:24:46 +00002791 ExprIterator arg_beg, ExprIterator arg_end,
Zhongxing Xu20227f72009-08-06 01:32:16 +00002792 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00002793
Ted Kremenek819e9b62008-03-11 06:39:11 +00002794 // Get the state.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002795 const GRState *state = Builder.GetState(Pred);
Ted Kremenek821537e2008-05-06 02:41:27 +00002796
2797 // Evaluate the effect of the arguments.
Ted Kremenek8cb96e92008-04-16 04:28:53 +00002798 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek68d73d12008-03-12 01:21:45 +00002799 unsigned idx = 0;
Ted Kremenek988990f2008-04-11 18:40:51 +00002800 Expr* ErrorExpr = NULL;
Mike Stump11289f42009-09-09 15:08:12 +00002801 SymbolRef ErrorSym = 0;
2802
2803 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
2804 SVal V = state->getSValAsScalarOrLoc(*I);
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002805 SymbolRef Sym = V.getAsLocSymbol();
Ted Kremenek804fc232009-03-04 00:13:50 +00002806
Ted Kremenek3e31c262009-03-26 03:35:11 +00002807 if (Sym)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002808 if (RefBindings::data_type* T = state->get<RefBindings>(Sym)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002809 state = Update(state, Sym, *T, Summ.getArg(idx), hasErr);
Ted Kremenekc52f9392009-02-24 19:15:11 +00002810 if (hasErr) {
Ted Kremenek988990f2008-04-11 18:40:51 +00002811 ErrorExpr = *I;
Ted Kremenek4963d112008-07-07 16:21:19 +00002812 ErrorSym = Sym;
Ted Kremenek988990f2008-04-11 18:40:51 +00002813 break;
Mike Stump11289f42009-09-09 15:08:12 +00002814 }
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002815 continue;
Ted Kremenekc52f9392009-02-24 19:15:11 +00002816 }
Ted Kremenekae529272008-07-09 18:11:16 +00002817
Ted Kremenekf9539d02009-09-22 04:48:39 +00002818 tryAgain:
Ted Kremenekc9747dd2009-03-03 22:06:47 +00002819 if (isa<Loc>(V)) {
2820 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002821 if (Summ.getArg(idx) == DoNothingByRef)
Ted Kremenekae529272008-07-09 18:11:16 +00002822 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002823
2824 // Invalidate the value of the variable passed by reference.
2825
Ted Kremenek4d851462008-07-03 23:26:32 +00002826 // FIXME: We can have collisions on the conjured symbol if the
2827 // expression *I also creates conjured symbols. We probably want
2828 // to identify conjured symbols by an expression pair: the enclosing
2829 // expression (the context) and the expression itself. This should
Mike Stump11289f42009-09-09 15:08:12 +00002830 // disambiguate conjured symbols.
Zhongxing Xu4744d562009-06-29 06:43:40 +00002831 unsigned Count = Builder.getCurrentBlockCount();
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002832 StoreManager& StoreMgr = Eng.getStateManager().getStoreManager();
Ted Kremenek97f75f82009-05-11 22:55:17 +00002833
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002834 const MemRegion *R = MR->getRegion();
2835 // Are we dealing with an ElementRegion? If the element type is
2836 // a basic integer type (e.g., char, int) and the underying region
2837 // is a variable region then strip off the ElementRegion.
2838 // FIXME: We really need to think about this for the general case
2839 // as sometimes we are reasoning about arrays and other times
2840 // about (char*), etc., is just a form of passing raw bytes.
2841 // e.g., void *p = alloca(); foo((char*)p);
2842 if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
2843 // Checking for 'integral type' is probably too promiscuous, but
2844 // we'll leave it in for now until we have a systematic way of
2845 // handling all of these cases. Eventually we need to come up
2846 // with an interface to StoreManager so that this logic can be
2847 // approriately delegated to the respective StoreManagers while
2848 // still allowing us to do checker-specific logic (e.g.,
2849 // invalidating reference counts), probably via callbacks.
2850 if (ER->getElementType()->isIntegralType()) {
2851 const MemRegion *superReg = ER->getSuperRegion();
2852 if (isa<VarRegion>(superReg) || isa<FieldRegion>(superReg) ||
2853 isa<ObjCIvarRegion>(superReg))
2854 R = cast<TypedRegion>(superReg);
Ted Kremenek0626df42009-05-06 18:19:24 +00002855 }
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002856 // FIXME: What about layers of ElementRegions?
2857 }
Zhongxing Xu4744d562009-06-29 06:43:40 +00002858
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002859 // Is the invalidated variable something that we were tracking?
2860 SymbolRef Sym = state->getSValAsScalarOrLoc(R).getAsLocSymbol();
Mike Stump11289f42009-09-09 15:08:12 +00002861
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002862 // Remove any existing reference-count binding.
Mike Stump11289f42009-09-09 15:08:12 +00002863 if (Sym)
Zhongxing Xue1a3ace2009-07-06 06:01:24 +00002864 state = state->remove<RefBindings>(Sym);
2865
2866 state = StoreMgr.InvalidateRegion(state, R, *I, Count);
Ted Kremenek4d851462008-07-03 23:26:32 +00002867 }
2868 else {
2869 // Nuke all other arguments passed by reference.
Ted Kremenekf9539d02009-09-22 04:48:39 +00002870 // FIXME: is this necessary or correct? This handles the non-Region
2871 // cases. Is it ever valid to store to these?
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002872 state = state->unbindLoc(cast<Loc>(V));
Ted Kremenek4d851462008-07-03 23:26:32 +00002873 }
Ted Kremenek0a86fdb2008-04-11 20:51:02 +00002874 }
Ted Kremenekf9539d02009-09-22 04:48:39 +00002875 else if (isa<nonloc::LocAsInteger>(V)) {
2876 // If we are passing a location wrapped as an integer, unwrap it and
2877 // invalidate the values referred by the location.
2878 V = cast<nonloc::LocAsInteger>(V).getLoc();
2879 goto tryAgain;
2880 }
Mike Stump11289f42009-09-09 15:08:12 +00002881 }
2882
2883 // Evaluate the effect on the message receiver.
Ted Kremenek821537e2008-05-06 02:41:27 +00002884 if (!ErrorExpr && Receiver) {
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002885 SymbolRef Sym = state->getSValAsScalarOrLoc(Receiver).getAsLocSymbol();
Ted Kremenek3e31c262009-03-26 03:35:11 +00002886 if (Sym) {
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002887 if (const RefVal* T = state->get<RefBindings>(Sym)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00002888 state = Update(state, Sym, *T, Summ.getReceiverEffect(), hasErr);
Ted Kremenekc52f9392009-02-24 19:15:11 +00002889 if (hasErr) {
Ted Kremenek821537e2008-05-06 02:41:27 +00002890 ErrorExpr = Receiver;
Ted Kremenek4963d112008-07-07 16:21:19 +00002891 ErrorSym = Sym;
Ted Kremenek821537e2008-05-06 02:41:27 +00002892 }
Ted Kremenekc52f9392009-02-24 19:15:11 +00002893 }
Ted Kremenek821537e2008-05-06 02:41:27 +00002894 }
2895 }
Mike Stump11289f42009-09-09 15:08:12 +00002896
2897 // Process any errors.
Ted Kremenek8cb96e92008-04-16 04:28:53 +00002898 if (hasErr) {
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00002899 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek396f4362008-04-18 03:39:05 +00002900 hasErr, ErrorSym);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002901 return;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00002902 }
Mike Stump11289f42009-09-09 15:08:12 +00002903
2904 // Consult the summary for the return value.
Ted Kremenekff606a12009-05-04 04:57:00 +00002905 RetEffect RE = Summ.getRetEffect();
Mike Stump11289f42009-09-09 15:08:12 +00002906
Ted Kremenek1272f702009-05-12 20:06:54 +00002907 if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
2908 assert(Receiver);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002909 SVal V = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenek1272f702009-05-12 20:06:54 +00002910 bool found = false;
2911 if (SymbolRef Sym = V.getAsLocSymbol())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002912 if (state->get<RefBindings>(Sym)) {
Ted Kremenek1272f702009-05-12 20:06:54 +00002913 found = true;
2914 RE = Summaries.getObjAllocRetEffect();
2915 }
2916
2917 if (!found)
2918 RE = RetEffect::MakeNoRet();
Mike Stump11289f42009-09-09 15:08:12 +00002919 }
2920
Ted Kremenek68d73d12008-03-12 01:21:45 +00002921 switch (RE.getKind()) {
2922 default:
2923 assert (false && "Unhandled RetEffect."); break;
Mike Stump11289f42009-09-09 15:08:12 +00002924
2925 case RetEffect::NoRet: {
Ted Kremenek831f3272008-04-11 20:23:24 +00002926 // Make up a symbol for the return value (not reference counted).
Ted Kremenek1642bda2009-06-26 00:05:51 +00002927 // FIXME: Most of this logic is not specific to the retain/release
2928 // checker.
Mike Stump11289f42009-09-09 15:08:12 +00002929
Ted Kremenek21387322008-10-17 22:23:12 +00002930 // FIXME: We eventually should handle structs and other compound types
2931 // that are returned by value.
Mike Stump11289f42009-09-09 15:08:12 +00002932
Ted Kremenek21387322008-10-17 22:23:12 +00002933 QualType T = Ex->getType();
Mike Stump11289f42009-09-09 15:08:12 +00002934
Ted Kremenek16866d62008-11-13 06:10:40 +00002935 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenek831f3272008-04-11 20:23:24 +00002936 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekf2489ea2009-04-09 22:22:44 +00002937 ValueManager &ValMgr = Eng.getValueManager();
Ted Kremeneke41b81e2009-09-27 20:45:21 +00002938 SVal X = ValMgr.getConjuredSymbolVal(NULL, Ex, T, Count);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002939 state = state->BindExpr(Ex, X, false);
Mike Stump11289f42009-09-09 15:08:12 +00002940 }
2941
Ted Kremenek4b772092008-04-10 23:44:06 +00002942 break;
Ted Kremenek21387322008-10-17 22:23:12 +00002943 }
Mike Stump11289f42009-09-09 15:08:12 +00002944
Ted Kremenek68d73d12008-03-12 01:21:45 +00002945 case RetEffect::Alias: {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00002946 unsigned idx = RE.getIndex();
Ted Kremenek08e17112008-06-17 02:43:46 +00002947 assert (arg_end >= arg_beg);
Ted Kremenek00daccd2008-05-05 22:11:16 +00002948 assert (idx < (unsigned) (arg_end - arg_beg));
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002949 SVal V = state->getSValAsScalarOrLoc(*(arg_beg+idx));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002950 state = state->BindExpr(Ex, V, false);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002951 break;
2952 }
Mike Stump11289f42009-09-09 15:08:12 +00002953
Ted Kremenek821537e2008-05-06 02:41:27 +00002954 case RetEffect::ReceiverAlias: {
2955 assert (Receiver);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002956 SVal V = state->getSValAsScalarOrLoc(Receiver);
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002957 state = state->BindExpr(Ex, V, false);
Ted Kremenek821537e2008-05-06 02:41:27 +00002958 break;
2959 }
Mike Stump11289f42009-09-09 15:08:12 +00002960
Ted Kremenekab4a8b52008-06-23 18:02:52 +00002961 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek68d73d12008-03-12 01:21:45 +00002962 case RetEffect::OwnedSymbol: {
2963 unsigned Count = Builder.getCurrentBlockCount();
Mike Stump11289f42009-09-09 15:08:12 +00002964 ValueManager &ValMgr = Eng.getValueManager();
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002965 SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002966 QualType RetT = GetReturnType(Ex, ValMgr.getContext());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002967 state = state->set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(),
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002968 RetT));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002969 state = state->BindExpr(Ex, ValMgr.makeLoc(Sym), false);
Ted Kremenek0b891a32009-03-09 22:46:49 +00002970
2971 // FIXME: Add a flag to the checker where allocations are assumed to
2972 // *not fail.
2973#if 0
Ted Kremenek2e561dd2009-01-28 22:27:59 +00002974 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) {
2975 bool isFeasible;
2976 state = state.Assume(loc::SymbolVal(Sym), true, isFeasible);
Mike Stump11289f42009-09-09 15:08:12 +00002977 assert(isFeasible && "Cannot assume fresh symbol is non-null.");
Ted Kremenek2e561dd2009-01-28 22:27:59 +00002978 }
Ted Kremenek0b891a32009-03-09 22:46:49 +00002979#endif
Mike Stump11289f42009-09-09 15:08:12 +00002980
Ted Kremenek68d73d12008-03-12 01:21:45 +00002981 break;
2982 }
Mike Stump11289f42009-09-09 15:08:12 +00002983
Ted Kremeneke6633562009-04-27 19:14:45 +00002984 case RetEffect::GCNotOwnedSymbol:
Ted Kremenek68d73d12008-03-12 01:21:45 +00002985 case RetEffect::NotOwnedSymbol: {
2986 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002987 ValueManager &ValMgr = Eng.getValueManager();
2988 SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count);
Mike Stump11289f42009-09-09 15:08:12 +00002989 QualType RetT = GetReturnType(Ex, ValMgr.getContext());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00002990 state = state->set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),
Ted Kremenekaa4cfc22009-04-09 16:13:17 +00002991 RetT));
Ted Kremenek1d5f2f32009-08-27 22:17:37 +00002992 state = state->BindExpr(Ex, ValMgr.makeLoc(Sym), false);
Ted Kremenek68d73d12008-03-12 01:21:45 +00002993 break;
2994 }
2995 }
Mike Stump11289f42009-09-09 15:08:12 +00002996
Ted Kremenekd84fff62009-02-18 02:00:25 +00002997 // Generate a sink node if we are at the end of a path.
Zhongxing Xu107f7592009-08-06 12:48:26 +00002998 ExplodedNode *NewNode =
Ted Kremenekff606a12009-05-04 04:57:00 +00002999 Summ.isEndPath() ? Builder.MakeSinkNode(Dst, Ex, Pred, state)
3000 : Builder.MakeNode(Dst, Ex, Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003001
Ted Kremenekd84fff62009-02-18 02:00:25 +00003002 // Annotate the edge with summary we used.
Ted Kremenekff606a12009-05-04 04:57:00 +00003003 if (NewNode) SummaryLog[NewNode] = &Summ;
Ted Kremenek00daccd2008-05-05 22:11:16 +00003004}
3005
3006
Zhongxing Xu20227f72009-08-06 01:32:16 +00003007void CFRefCount::EvalCall(ExplodedNodeSet& Dst,
Ted Kremenek00daccd2008-05-05 22:11:16 +00003008 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003009 GRStmtNodeBuilder& Builder,
Zhongxing Xu27f17422008-10-17 05:57:07 +00003010 CallExpr* CE, SVal L,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003011 ExplodedNode* Pred) {
Zhongxing Xuac129432009-04-20 05:24:46 +00003012 const FunctionDecl* FD = L.getAsFunctionDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003013 RetainSummary* Summ = !FD ? Summaries.getDefaultSummary()
Zhongxing Xuac129432009-04-20 05:24:46 +00003014 : Summaries.getSummary(const_cast<FunctionDecl*>(FD));
Mike Stump11289f42009-09-09 15:08:12 +00003015
Ted Kremenekff606a12009-05-04 04:57:00 +00003016 assert(Summ);
3017 EvalSummary(Dst, Eng, Builder, CE, 0, *Summ,
Ted Kremenek00daccd2008-05-05 22:11:16 +00003018 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenekea6507f2008-03-06 00:08:09 +00003019}
Ted Kremenek819e9b62008-03-11 06:39:11 +00003020
Zhongxing Xu20227f72009-08-06 01:32:16 +00003021void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet& Dst,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003022 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003023 GRStmtNodeBuilder& Builder,
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003024 ObjCMessageExpr* ME,
Mike Stump11289f42009-09-09 15:08:12 +00003025 ExplodedNode* Pred) {
Ted Kremenekff606a12009-05-04 04:57:00 +00003026 RetainSummary* Summ = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003027
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003028 if (Expr* Receiver = ME->getReceiver()) {
3029 // We need the type-information of the tracked receiver object
3030 // Retrieve it from the state.
Ted Kremenek5801f652009-05-13 18:16:01 +00003031 const ObjCInterfaceDecl* ID = 0;
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003032
3033 // FIXME: Wouldn't it be great if this code could be reduced? It's just
3034 // a chain of lookups.
Ted Kremenek0b50fb12009-04-29 05:04:30 +00003035 // FIXME: Is this really working as expected? There are cases where
3036 // we just use the 'ID' from the message expression.
Ted Kremenek5ab5a1b2008-08-13 04:27:00 +00003037 const GRState* St = Builder.GetState(Pred);
Ted Kremenek095f1a92009-06-18 23:58:37 +00003038 SVal V = St->getSValAsScalarOrLoc(Receiver);
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003039
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003040 SymbolRef Sym = V.getAsLocSymbol();
Mike Stump11289f42009-09-09 15:08:12 +00003041
Ted Kremenek3e31c262009-03-26 03:35:11 +00003042 if (Sym) {
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00003043 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003044 if (const ObjCObjectPointerType* PT =
John McCall9dd450b2009-09-21 23:43:11 +00003045 T->getType()->getAs<ObjCObjectPointerType>())
Steve Naroff7cae42b2009-07-10 23:34:53 +00003046 ID = PT->getInterfaceDecl();
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003047 }
3048 }
Ted Kremenek5801f652009-05-13 18:16:01 +00003049
3050 // FIXME: this is a hack. This may or may not be the actual method
3051 // that is called.
3052 if (!ID) {
Steve Naroff7cae42b2009-07-10 23:34:53 +00003053 if (const ObjCObjectPointerType *PT =
John McCall9dd450b2009-09-21 23:43:11 +00003054 Receiver->getType()->getAs<ObjCObjectPointerType>())
Steve Naroff7cae42b2009-07-10 23:34:53 +00003055 ID = PT->getInterfaceDecl();
Ted Kremenek5801f652009-05-13 18:16:01 +00003056 }
3057
Ted Kremenek38724302009-04-29 17:09:14 +00003058 // FIXME: The receiver could be a reference to a class, meaning that
3059 // we should use the class method.
3060 Summ = Summaries.getInstanceMethodSummary(ME, ID);
Ted Kremenek03466c22008-10-24 20:32:50 +00003061
Ted Kremenekcc3d1882008-10-23 01:56:15 +00003062 // Special-case: are we sending a mesage to "self"?
3063 // This is a hack. When we have full-IP this should be removed.
Mike Stump11289f42009-09-09 15:08:12 +00003064 if (isa<ObjCMethodDecl>(Pred->getLocationContext()->getDecl())) {
Ted Kremenek1d9a2672009-05-04 05:31:22 +00003065 if (Expr* Receiver = ME->getReceiver()) {
Ted Kremenek095f1a92009-06-18 23:58:37 +00003066 SVal X = St->getSValAsScalarOrLoc(Receiver);
Mike Stump11289f42009-09-09 15:08:12 +00003067 if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X)) {
Ted Kremenek608677a2009-08-21 23:25:54 +00003068 // Get the region associated with 'self'.
Mike Stump11289f42009-09-09 15:08:12 +00003069 const LocationContext *LC = Pred->getLocationContext();
Ted Kremenek608677a2009-08-21 23:25:54 +00003070 if (const ImplicitParamDecl *SelfDecl = LC->getSelfDecl()) {
Mike Stump11289f42009-09-09 15:08:12 +00003071 SVal SelfVal = St->getSVal(St->getRegion(SelfDecl, LC));
Ted Kremenek608677a2009-08-21 23:25:54 +00003072 if (L->getBaseRegion() == SelfVal.getAsRegion()) {
3073 // Update the summary to make the default argument effect
3074 // 'StopTracking'.
3075 Summ = Summaries.copySummary(Summ);
3076 Summ->setDefaultArgEffect(StopTracking);
3077 }
Mike Stump11289f42009-09-09 15:08:12 +00003078 }
Ted Kremenek608677a2009-08-21 23:25:54 +00003079 }
Ted Kremenekcc3d1882008-10-23 01:56:15 +00003080 }
3081 }
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003082 }
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003083 else
Ted Kremenek0a1f9c42009-04-23 21:25:57 +00003084 Summ = Summaries.getClassMethodSummary(ME);
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003085
Ted Kremenekff606a12009-05-04 04:57:00 +00003086 if (!Summ)
3087 Summ = Summaries.getDefaultSummary();
Ted Kremenek8a5ad392009-04-24 17:50:11 +00003088
Ted Kremenekff606a12009-05-04 04:57:00 +00003089 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), *Summ,
Ted Kremenek015c3562008-05-06 04:20:12 +00003090 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek748c7ce2008-04-15 23:44:31 +00003091}
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003092
3093namespace {
3094class VISIBILITY_HIDDEN StopTrackingCallback : public SymbolVisitor {
Ted Kremenek89a303c2009-06-18 00:49:02 +00003095 const GRState *state;
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003096public:
Ted Kremenek89a303c2009-06-18 00:49:02 +00003097 StopTrackingCallback(const GRState *st) : state(st) {}
3098 const GRState *getState() const { return state; }
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003099
3100 bool VisitSymbol(SymbolRef sym) {
Ted Kremenek89a303c2009-06-18 00:49:02 +00003101 state = state->remove<RefBindings>(sym);
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003102 return true;
3103 }
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003104};
3105} // end anonymous namespace
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003106
Mike Stump11289f42009-09-09 15:08:12 +00003107
3108void CFRefCount::EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val) {
3109 // Are we storing to something that causes the value to "escape"?
Ted Kremenek71454892008-04-16 20:40:59 +00003110 bool escapes = false;
Mike Stump11289f42009-09-09 15:08:12 +00003111
Ted Kremeneke86755e2008-10-18 03:49:51 +00003112 // A value escapes in three possible cases (this may change):
3113 //
3114 // (1) we are binding to something that is not a memory region.
3115 // (2) we are binding to a memregion that does not have stack storage
3116 // (3) we are binding to a memregion with stack storage that the store
Mike Stump11289f42009-09-09 15:08:12 +00003117 // does not understand.
Ted Kremenek89a303c2009-06-18 00:49:02 +00003118 const GRState *state = B.getState();
Ted Kremeneke86755e2008-10-18 03:49:51 +00003119
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003120 if (!isa<loc::MemRegionVal>(location))
Ted Kremenek71454892008-04-16 20:40:59 +00003121 escapes = true;
Ted Kremenek5ca90a22008-10-04 05:50:14 +00003122 else {
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003123 const MemRegion* R = cast<loc::MemRegionVal>(location).getRegion();
Ted Kremenek404b1322009-06-23 18:05:21 +00003124 escapes = !R->hasStackStorage();
Mike Stump11289f42009-09-09 15:08:12 +00003125
Ted Kremeneke86755e2008-10-18 03:49:51 +00003126 if (!escapes) {
3127 // To test (3), generate a new state with the binding removed. If it is
3128 // the same state, then it escapes (since the store cannot represent
3129 // the binding).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003130 escapes = (state == (state->bindLoc(cast<Loc>(location), UnknownVal())));
Ted Kremeneke86755e2008-10-18 03:49:51 +00003131 }
Ted Kremenek5ca90a22008-10-04 05:50:14 +00003132 }
Ted Kremeneke68c0fc2009-02-14 01:43:44 +00003133
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003134 // If our store can represent the binding and we aren't storing to something
3135 // that doesn't have local storage then just return and have the simulation
3136 // state continue as is.
3137 if (!escapes)
3138 return;
Ted Kremeneke86755e2008-10-18 03:49:51 +00003139
Ted Kremenek4e9d4b52009-02-14 03:16:10 +00003140 // Otherwise, find all symbols referenced by 'val' that we are tracking
3141 // and stop tracking them.
Ted Kremenek89a303c2009-06-18 00:49:02 +00003142 B.MakeNode(state->scanReachableSymbols<StopTrackingCallback>(val).getState());
Ted Kremenekcbf4c612008-04-16 22:32:20 +00003143}
3144
Ted Kremeneka506fec2008-04-17 18:12:53 +00003145 // Return statements.
3146
Zhongxing Xu20227f72009-08-06 01:32:16 +00003147void CFRefCount::EvalReturn(ExplodedNodeSet& Dst,
Ted Kremeneka506fec2008-04-17 18:12:53 +00003148 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003149 GRStmtNodeBuilder& Builder,
Ted Kremeneka506fec2008-04-17 18:12:53 +00003150 ReturnStmt* S,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003151 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00003152
Ted Kremeneka506fec2008-04-17 18:12:53 +00003153 Expr* RetE = S->getRetValue();
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003154 if (!RetE)
Ted Kremeneka506fec2008-04-17 18:12:53 +00003155 return;
Mike Stump11289f42009-09-09 15:08:12 +00003156
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003157 const GRState *state = Builder.GetState(Pred);
3158 SymbolRef Sym = state->getSValAsScalarOrLoc(RetE).getAsLocSymbol();
Mike Stump11289f42009-09-09 15:08:12 +00003159
Ted Kremenek3e31c262009-03-26 03:35:11 +00003160 if (!Sym)
Ted Kremenekc9747dd2009-03-03 22:06:47 +00003161 return;
Mike Stump11289f42009-09-09 15:08:12 +00003162
Ted Kremeneka506fec2008-04-17 18:12:53 +00003163 // Get the reference count binding (if any).
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003164 const RefVal* T = state->get<RefBindings>(Sym);
Mike Stump11289f42009-09-09 15:08:12 +00003165
Ted Kremeneka506fec2008-04-17 18:12:53 +00003166 if (!T)
3167 return;
Mike Stump11289f42009-09-09 15:08:12 +00003168
3169 // Change the reference count.
3170 RefVal X = *T;
3171
3172 switch (X.getKind()) {
3173 case RefVal::Owned: {
Ted Kremeneka506fec2008-04-17 18:12:53 +00003174 unsigned cnt = X.getCount();
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003175 assert (cnt > 0);
Ted Kremenek3978f792009-05-10 05:11:21 +00003176 X.setCount(cnt - 1);
3177 X = X ^ RefVal::ReturnedOwned;
Ted Kremeneka506fec2008-04-17 18:12:53 +00003178 break;
3179 }
Mike Stump11289f42009-09-09 15:08:12 +00003180
Ted Kremeneka506fec2008-04-17 18:12:53 +00003181 case RefVal::NotOwned: {
3182 unsigned cnt = X.getCount();
Ted Kremenek3978f792009-05-10 05:11:21 +00003183 if (cnt) {
3184 X.setCount(cnt - 1);
3185 X = X ^ RefVal::ReturnedOwned;
3186 }
3187 else {
3188 X = X ^ RefVal::ReturnedNotOwned;
3189 }
Ted Kremeneka506fec2008-04-17 18:12:53 +00003190 break;
3191 }
Mike Stump11289f42009-09-09 15:08:12 +00003192
3193 default:
Ted Kremeneka506fec2008-04-17 18:12:53 +00003194 return;
3195 }
Mike Stump11289f42009-09-09 15:08:12 +00003196
Ted Kremeneka506fec2008-04-17 18:12:53 +00003197 // Update the binding.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003198 state = state->set<RefBindings>(Sym, X);
Ted Kremenek6bd78702009-04-29 18:50:19 +00003199 Pred = Builder.MakeNode(Dst, S, Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003200
Ted Kremenekb4e27a12009-04-30 05:51:50 +00003201 // Did we cache out?
3202 if (!Pred)
3203 return;
Mike Stump11289f42009-09-09 15:08:12 +00003204
Ted Kremenek3978f792009-05-10 05:11:21 +00003205 // Update the autorelease counts.
3206 static unsigned autoreleasetag = 0;
3207 GenericNodeBuilder Bd(Builder, S, &autoreleasetag);
3208 bool stop = false;
3209 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state , Bd, Pred, Eng, Sym,
3210 X, stop);
Mike Stump11289f42009-09-09 15:08:12 +00003211
Ted Kremenek3978f792009-05-10 05:11:21 +00003212 // Did we cache out?
3213 if (!Pred || stop)
3214 return;
Mike Stump11289f42009-09-09 15:08:12 +00003215
Ted Kremenek3978f792009-05-10 05:11:21 +00003216 // Get the updated binding.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003217 T = state->get<RefBindings>(Sym);
Ted Kremenek3978f792009-05-10 05:11:21 +00003218 assert(T);
3219 X = *T;
Mike Stump11289f42009-09-09 15:08:12 +00003220
Ted Kremenek6bd78702009-04-29 18:50:19 +00003221 // Any leaks or other errors?
3222 if (X.isReturnedOwned() && X.getCount() == 0) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00003223 Decl const *CD = &Pred->getCodeDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003224 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
Ted Kremenekff606a12009-05-04 04:57:00 +00003225 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
Ted Kremenekdee56e32009-05-10 06:25:57 +00003226 RetEffect RE = Summ.getRetEffect();
3227 bool hasError = false;
3228
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003229 if (RE.getKind() != RetEffect::NoRet) {
3230 if (isGCEnabled() && RE.getObjKind() == RetEffect::ObjC) {
3231 // Things are more complicated with garbage collection. If the
3232 // returned object is suppose to be an Objective-C object, we have
3233 // a leak (as the caller expects a GC'ed object) because no
3234 // method should return ownership unless it returns a CF object.
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003235 hasError = true;
Ted Kremenek8070b822009-10-14 23:58:34 +00003236 X = X ^ RefVal::ErrorGCLeakReturned;
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003237 }
3238 else if (!RE.isOwned()) {
3239 // Either we are using GC and the returned object is a CF type
3240 // or we aren't using GC. In either case, we expect that the
Mike Stump11289f42009-09-09 15:08:12 +00003241 // enclosing method is expected to return ownership.
Ted Kremeneke4302ee2009-05-16 01:38:01 +00003242 hasError = true;
3243 X = X ^ RefVal::ErrorLeakReturned;
3244 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00003245 }
Mike Stump11289f42009-09-09 15:08:12 +00003246
3247 if (hasError) {
Ted Kremenek6bd78702009-04-29 18:50:19 +00003248 // Generate an error node.
Ted Kremenekdee56e32009-05-10 06:25:57 +00003249 static int ReturnOwnLeakTag = 0;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003250 state = state->set<RefBindings>(Sym, X);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003251 ExplodedNode *N =
Zhongxing Xue1190f72009-08-15 03:17:38 +00003252 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3253 &ReturnOwnLeakTag), state, Pred);
Ted Kremenekdee56e32009-05-10 06:25:57 +00003254 if (N) {
3255 CFRefReport *report =
Ted Kremenekb4e27a12009-04-30 05:51:50 +00003256 new CFRefLeakReport(*static_cast<CFRefBug*>(leakAtReturn), *this,
3257 N, Sym, Eng);
3258 BR->EmitReport(report);
3259 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00003260 }
Mike Stump11289f42009-09-09 15:08:12 +00003261 }
Ted Kremenekdee56e32009-05-10 06:25:57 +00003262 }
3263 else if (X.isReturnedNotOwned()) {
Zhongxing Xu7e3431b2009-09-10 05:44:00 +00003264 Decl const *CD = &Pred->getCodeDecl();
Ted Kremenekdee56e32009-05-10 06:25:57 +00003265 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
3266 const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
3267 if (Summ.getRetEffect().isOwned()) {
3268 // Trying to return a not owned object to a caller expecting an
3269 // owned object.
Mike Stump11289f42009-09-09 15:08:12 +00003270
Ted Kremenekdee56e32009-05-10 06:25:57 +00003271 static int ReturnNotOwnedForOwnedTag = 0;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003272 state = state->set<RefBindings>(Sym, X ^ RefVal::ErrorReturnedNotOwned);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003273 if (ExplodedNode *N =
Zhongxing Xue1190f72009-08-15 03:17:38 +00003274 Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3275 &ReturnNotOwnedForOwnedTag),
3276 state, Pred)) {
Ted Kremenekdee56e32009-05-10 06:25:57 +00003277 CFRefReport *report =
3278 new CFRefReport(*static_cast<CFRefBug*>(returnNotOwnedForOwned),
3279 *this, N, Sym);
3280 BR->EmitReport(report);
3281 }
3282 }
Ted Kremenek6bd78702009-04-29 18:50:19 +00003283 }
3284 }
Ted Kremeneka506fec2008-04-17 18:12:53 +00003285}
3286
Ted Kremenek4d837282008-04-18 19:23:43 +00003287// Assumptions.
3288
Ted Kremenekf9906842009-06-18 22:57:13 +00003289const GRState* CFRefCount::EvalAssume(const GRState *state,
3290 SVal Cond, bool Assumption) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003291
3292 // FIXME: We may add to the interface of EvalAssume the list of symbols
3293 // whose assumptions have changed. For now we just iterate through the
3294 // bindings and check if any of the tracked symbols are NULL. This isn't
Mike Stump11289f42009-09-09 15:08:12 +00003295 // too bad since the number of symbols we will track in practice are
Ted Kremenek4d837282008-04-18 19:23:43 +00003296 // probably small and EvalAssume is only called at branches and a few
3297 // other places.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003298 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003299
Ted Kremenek4d837282008-04-18 19:23:43 +00003300 if (B.isEmpty())
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003301 return state;
Mike Stump11289f42009-09-09 15:08:12 +00003302
3303 bool changed = false;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003304 RefBindings::Factory& RefBFactory = state->get_context<RefBindings>();
Ted Kremenek4d837282008-04-18 19:23:43 +00003305
Mike Stump11289f42009-09-09 15:08:12 +00003306 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003307 // Check if the symbol is null (or equal to any constant).
3308 // If this is the case, stop tracking the symbol.
Ted Kremenekf9906842009-06-18 22:57:13 +00003309 if (state->getSymVal(I.getKey())) {
Ted Kremenek4d837282008-04-18 19:23:43 +00003310 changed = true;
3311 B = RefBFactory.Remove(B, I.getKey());
3312 }
3313 }
Mike Stump11289f42009-09-09 15:08:12 +00003314
Ted Kremenek87aab6c2008-08-17 03:20:02 +00003315 if (changed)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003316 state = state->set<RefBindings>(B);
Mike Stump11289f42009-09-09 15:08:12 +00003317
Ted Kremenekdb7dd9c2008-08-14 21:16:54 +00003318 return state;
Ted Kremenek4d837282008-04-18 19:23:43 +00003319}
Ted Kremenek819e9b62008-03-11 06:39:11 +00003320
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003321const GRState * CFRefCount::Update(const GRState * state, SymbolRef sym,
Ted Kremenekc52f9392009-02-24 19:15:11 +00003322 RefVal V, ArgEffect E,
3323 RefVal::Kind& hasErr) {
Ted Kremenekf68490a2009-02-18 18:54:33 +00003324
3325 // In GC mode [... release] and [... retain] do nothing.
3326 switch (E) {
3327 default: break;
3328 case IncRefMsg: E = isGCEnabled() ? DoNothing : IncRef; break;
3329 case DecRefMsg: E = isGCEnabled() ? DoNothing : DecRef; break;
Ted Kremenek10452892009-02-18 21:57:45 +00003330 case MakeCollectable: E = isGCEnabled() ? DecRef : DoNothing; break;
Mike Stump11289f42009-09-09 15:08:12 +00003331 case NewAutoreleasePool: E = isGCEnabled() ? DoNothing :
Ted Kremenek50db3d02009-02-23 17:45:03 +00003332 NewAutoreleasePool; break;
Ted Kremenekf68490a2009-02-18 18:54:33 +00003333 }
Mike Stump11289f42009-09-09 15:08:12 +00003334
Ted Kremenekea072e32009-03-17 19:42:23 +00003335 // Handle all use-after-releases.
3336 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
3337 V = V ^ RefVal::ErrorUseAfterRelease;
3338 hasErr = V.getKind();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003339 return state->set<RefBindings>(sym, V);
Mike Stump11289f42009-09-09 15:08:12 +00003340 }
3341
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003342 switch (E) {
3343 default:
3344 assert (false && "Unhandled CFRef transition.");
Mike Stump11289f42009-09-09 15:08:12 +00003345
Ted Kremenekea072e32009-03-17 19:42:23 +00003346 case Dealloc:
3347 // Any use of -dealloc in GC is *bad*.
3348 if (isGCEnabled()) {
3349 V = V ^ RefVal::ErrorDeallocGC;
3350 hasErr = V.getKind();
3351 break;
3352 }
Mike Stump11289f42009-09-09 15:08:12 +00003353
Ted Kremenekea072e32009-03-17 19:42:23 +00003354 switch (V.getKind()) {
3355 default:
3356 assert(false && "Invalid case.");
3357 case RefVal::Owned:
3358 // The object immediately transitions to the released state.
3359 V = V ^ RefVal::Released;
3360 V.clearCounts();
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003361 return state->set<RefBindings>(sym, V);
Ted Kremenekea072e32009-03-17 19:42:23 +00003362 case RefVal::NotOwned:
3363 V = V ^ RefVal::ErrorDeallocNotOwned;
3364 hasErr = V.getKind();
3365 break;
Mike Stump11289f42009-09-09 15:08:12 +00003366 }
Ted Kremenekea072e32009-03-17 19:42:23 +00003367 break;
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003368
Ted Kremenek8ec8cf02009-02-25 23:11:49 +00003369 case NewAutoreleasePool:
3370 assert(!isGCEnabled());
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003371 return state->add<AutoreleaseStack>(sym);
Mike Stump11289f42009-09-09 15:08:12 +00003372
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003373 case MayEscape:
3374 if (V.getKind() == RefVal::Owned) {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003375 V = V ^ RefVal::NotOwned;
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003376 break;
3377 }
Ted Kremenekea072e32009-03-17 19:42:23 +00003378
Ted Kremenek1df2f3a2008-05-22 17:31:13 +00003379 // Fall-through.
Mike Stump11289f42009-09-09 15:08:12 +00003380
Ted Kremenekae529272008-07-09 18:11:16 +00003381 case DoNothingByRef:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003382 case DoNothing:
Ted Kremenekc52f9392009-02-24 19:15:11 +00003383 return state;
Ted Kremeneka0e071c2008-06-30 16:57:41 +00003384
Ted Kremenekc7832092009-01-28 21:44:40 +00003385 case Autorelease:
Ted Kremenekea072e32009-03-17 19:42:23 +00003386 if (isGCEnabled())
3387 return state;
Mike Stump11289f42009-09-09 15:08:12 +00003388
Ted Kremenek8c3f0042009-03-20 17:34:15 +00003389 // Update the autorelease counts.
3390 state = SendAutorelease(state, ARCountFactory, sym);
Ted Kremenek3a0516b2009-05-08 20:01:42 +00003391 V = V.autorelease();
Ted Kremenek2d0ff622009-05-09 01:50:57 +00003392 break;
Ted Kremenekd35272f2009-05-09 00:10:05 +00003393
Ted Kremenek821537e2008-05-06 02:41:27 +00003394 case StopTracking:
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003395 return state->remove<RefBindings>(sym);
Ted Kremenek050b91c2008-08-12 18:30:56 +00003396
Mike Stump11289f42009-09-09 15:08:12 +00003397 case IncRef:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003398 switch (V.getKind()) {
3399 default:
3400 assert(false);
3401
3402 case RefVal::Owned:
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003403 case RefVal::NotOwned:
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003404 V = V + 1;
Mike Stump11289f42009-09-09 15:08:12 +00003405 break;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003406 case RefVal::Released:
Ted Kremenekea072e32009-03-17 19:42:23 +00003407 // Non-GC cases are handled above.
3408 assert(isGCEnabled());
3409 V = (V ^ RefVal::Owned) + 1;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003410 break;
Mike Stump11289f42009-09-09 15:08:12 +00003411 }
Ted Kremenek4b772092008-04-10 23:44:06 +00003412 break;
Mike Stump11289f42009-09-09 15:08:12 +00003413
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003414 case SelfOwn:
3415 V = V ^ RefVal::NotOwned;
Ted Kremenekf68490a2009-02-18 18:54:33 +00003416 // Fall-through.
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003417 case DecRef:
3418 switch (V.getKind()) {
3419 default:
Ted Kremenekea072e32009-03-17 19:42:23 +00003420 // case 'RefVal::Released' handled above.
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003421 assert (false);
Ted Kremenek050b91c2008-08-12 18:30:56 +00003422
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003423 case RefVal::Owned:
Ted Kremenek551747f2009-02-18 22:57:22 +00003424 assert(V.getCount() > 0);
3425 if (V.getCount() == 1) V = V ^ RefVal::Released;
3426 V = V - 1;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003427 break;
Mike Stump11289f42009-09-09 15:08:12 +00003428
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003429 case RefVal::NotOwned:
3430 if (V.getCount() > 0)
3431 V = V - 1;
Ted Kremenek3c03d522008-04-10 23:09:18 +00003432 else {
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003433 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003434 hasErr = V.getKind();
Mike Stump11289f42009-09-09 15:08:12 +00003435 }
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003436 break;
Mike Stump11289f42009-09-09 15:08:12 +00003437
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003438 case RefVal::Released:
Ted Kremenekea072e32009-03-17 19:42:23 +00003439 // Non-GC cases are handled above.
3440 assert(isGCEnabled());
Ted Kremenek3185c9c2008-06-25 21:21:56 +00003441 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek8cb96e92008-04-16 04:28:53 +00003442 hasErr = V.getKind();
Mike Stump11289f42009-09-09 15:08:12 +00003443 break;
3444 }
Ted Kremenek4b772092008-04-10 23:44:06 +00003445 break;
Ted Kremenekbf9d8042008-03-11 17:48:22 +00003446 }
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003447 return state->set<RefBindings>(sym, V);
Ted Kremenek819e9b62008-03-11 06:39:11 +00003448}
3449
Ted Kremenekce8e8812008-04-09 01:10:13 +00003450//===----------------------------------------------------------------------===//
Ted Kremenek400aae72009-02-05 06:50:21 +00003451// Handle dead symbols and end-of-path.
3452//===----------------------------------------------------------------------===//
3453
Zhongxing Xu20227f72009-08-06 01:32:16 +00003454std::pair<ExplodedNode*, const GRState *>
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003455CFRefCount::HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003456 ExplodedNode* Pred,
Ted Kremenekd35272f2009-05-09 00:10:05 +00003457 GRExprEngine &Eng,
3458 SymbolRef Sym, RefVal V, bool &stop) {
Mike Stump11289f42009-09-09 15:08:12 +00003459
Ted Kremenekd35272f2009-05-09 00:10:05 +00003460 unsigned ACnt = V.getAutoreleaseCount();
3461 stop = false;
3462
3463 // No autorelease counts? Nothing to be done.
3464 if (!ACnt)
3465 return std::make_pair(Pred, state);
Mike Stump11289f42009-09-09 15:08:12 +00003466
3467 assert(!isGCEnabled() && "Autorelease counts in GC mode?");
Ted Kremenekd35272f2009-05-09 00:10:05 +00003468 unsigned Cnt = V.getCount();
Mike Stump11289f42009-09-09 15:08:12 +00003469
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003470 // FIXME: Handle sending 'autorelease' to already released object.
3471
3472 if (V.getKind() == RefVal::ReturnedOwned)
3473 ++Cnt;
Mike Stump11289f42009-09-09 15:08:12 +00003474
Ted Kremenekd35272f2009-05-09 00:10:05 +00003475 if (ACnt <= Cnt) {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003476 if (ACnt == Cnt) {
3477 V.clearCounts();
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003478 if (V.getKind() == RefVal::ReturnedOwned)
3479 V = V ^ RefVal::ReturnedNotOwned;
3480 else
3481 V = V ^ RefVal::NotOwned;
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003482 }
Ted Kremenekdc7853c2009-05-11 15:26:06 +00003483 else {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003484 V.setCount(Cnt - ACnt);
3485 V.setAutoreleaseCount(0);
3486 }
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003487 state = state->set<RefBindings>(Sym, V);
Zhongxing Xu20227f72009-08-06 01:32:16 +00003488 ExplodedNode *N = Bd.MakeNode(state, Pred);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003489 stop = (N == 0);
3490 return std::make_pair(N, state);
Mike Stump11289f42009-09-09 15:08:12 +00003491 }
Ted Kremenekd35272f2009-05-09 00:10:05 +00003492
3493 // Woah! More autorelease counts then retain counts left.
3494 // Emit hard error.
3495 stop = true;
3496 V = V ^ RefVal::ErrorOverAutorelease;
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003497 state = state->set<RefBindings>(Sym, V);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003498
Zhongxing Xu20227f72009-08-06 01:32:16 +00003499 if (ExplodedNode *N = Bd.MakeNode(state, Pred)) {
Ted Kremenek9ec08aa2009-05-09 00:44:07 +00003500 N->markAsSink();
Mike Stump11289f42009-09-09 15:08:12 +00003501
Ted Kremenek3978f792009-05-10 05:11:21 +00003502 std::string sbuf;
3503 llvm::raw_string_ostream os(sbuf);
Ted Kremenek4785e412009-05-15 06:02:08 +00003504 os << "Object over-autoreleased: object was sent -autorelease";
Ted Kremenek3978f792009-05-10 05:11:21 +00003505 if (V.getAutoreleaseCount() > 1)
3506 os << V.getAutoreleaseCount() << " times";
3507 os << " but the object has ";
3508 if (V.getCount() == 0)
3509 os << "zero (locally visible)";
3510 else
3511 os << "+" << V.getCount();
3512 os << " retain counts";
Mike Stump11289f42009-09-09 15:08:12 +00003513
Ted Kremenekd35272f2009-05-09 00:10:05 +00003514 CFRefReport *report =
3515 new CFRefReport(*static_cast<CFRefBug*>(overAutorelease),
Ted Kremenek3978f792009-05-10 05:11:21 +00003516 *this, N, Sym, os.str().c_str());
Ted Kremenekd35272f2009-05-09 00:10:05 +00003517 BR->EmitReport(report);
3518 }
Mike Stump11289f42009-09-09 15:08:12 +00003519
Zhongxing Xu20227f72009-08-06 01:32:16 +00003520 return std::make_pair((ExplodedNode*)0, state);
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003521}
Ted Kremenek884a8992009-05-08 23:09:42 +00003522
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003523const GRState *
3524CFRefCount::HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
Ted Kremenek884a8992009-05-08 23:09:42 +00003525 llvm::SmallVectorImpl<SymbolRef> &Leaked) {
Mike Stump11289f42009-09-09 15:08:12 +00003526
3527 bool hasLeak = V.isOwned() ||
Ted Kremenek884a8992009-05-08 23:09:42 +00003528 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Mike Stump11289f42009-09-09 15:08:12 +00003529
Ted Kremenek884a8992009-05-08 23:09:42 +00003530 if (!hasLeak)
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003531 return state->remove<RefBindings>(sid);
Mike Stump11289f42009-09-09 15:08:12 +00003532
Ted Kremenek884a8992009-05-08 23:09:42 +00003533 Leaked.push_back(sid);
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003534 return state->set<RefBindings>(sid, V ^ RefVal::ErrorLeak);
Ted Kremenek884a8992009-05-08 23:09:42 +00003535}
3536
Zhongxing Xu20227f72009-08-06 01:32:16 +00003537ExplodedNode*
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003538CFRefCount::ProcessLeaks(const GRState * state,
Ted Kremenek884a8992009-05-08 23:09:42 +00003539 llvm::SmallVectorImpl<SymbolRef> &Leaked,
3540 GenericNodeBuilder &Builder,
3541 GRExprEngine& Eng,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003542 ExplodedNode *Pred) {
Mike Stump11289f42009-09-09 15:08:12 +00003543
Ted Kremenek884a8992009-05-08 23:09:42 +00003544 if (Leaked.empty())
3545 return Pred;
Mike Stump11289f42009-09-09 15:08:12 +00003546
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003547 // Generate an intermediate node representing the leak point.
Zhongxing Xu20227f72009-08-06 01:32:16 +00003548 ExplodedNode *N = Builder.MakeNode(state, Pred);
Mike Stump11289f42009-09-09 15:08:12 +00003549
Ted Kremenek884a8992009-05-08 23:09:42 +00003550 if (N) {
3551 for (llvm::SmallVectorImpl<SymbolRef>::iterator
3552 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +00003553
3554 CFRefBug *BT = static_cast<CFRefBug*>(Pred ? leakWithinFunction
Ted Kremenek884a8992009-05-08 23:09:42 +00003555 : leakAtReturn);
3556 assert(BT && "BugType not initialized.");
3557 CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, *I, Eng);
3558 BR->EmitReport(report);
3559 }
3560 }
Mike Stump11289f42009-09-09 15:08:12 +00003561
Ted Kremenek884a8992009-05-08 23:09:42 +00003562 return N;
3563}
3564
Ted Kremenek400aae72009-02-05 06:50:21 +00003565void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003566 GREndPathNodeBuilder& Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00003567
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003568 const GRState *state = Builder.getState();
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003569 GenericNodeBuilder Bd(Builder);
Mike Stump11289f42009-09-09 15:08:12 +00003570 RefBindings B = state->get<RefBindings>();
Zhongxing Xu20227f72009-08-06 01:32:16 +00003571 ExplodedNode *Pred = 0;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003572
3573 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
Ted Kremenekd35272f2009-05-09 00:10:05 +00003574 bool stop = false;
3575 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3576 (*I).first,
Mike Stump11289f42009-09-09 15:08:12 +00003577 (*I).second, stop);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003578
3579 if (stop)
3580 return;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003581 }
Mike Stump11289f42009-09-09 15:08:12 +00003582
3583 B = state->get<RefBindings>();
3584 llvm::SmallVector<SymbolRef, 10> Leaked;
3585
Ted Kremenek884a8992009-05-08 23:09:42 +00003586 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
3587 state = HandleSymbolDeath(state, (*I).first, (*I).second, Leaked);
3588
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003589 ProcessLeaks(state, Leaked, Bd, Eng, Pred);
Ted Kremenek400aae72009-02-05 06:50:21 +00003590}
3591
Zhongxing Xu20227f72009-08-06 01:32:16 +00003592void CFRefCount::EvalDeadSymbols(ExplodedNodeSet& Dst,
Ted Kremenek400aae72009-02-05 06:50:21 +00003593 GRExprEngine& Eng,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003594 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003595 ExplodedNode* Pred,
Ted Kremenek400aae72009-02-05 06:50:21 +00003596 Stmt* S,
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003597 const GRState* state,
Ted Kremenek400aae72009-02-05 06:50:21 +00003598 SymbolReaper& SymReaper) {
Ted Kremenek884a8992009-05-08 23:09:42 +00003599
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003600 RefBindings B = state->get<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003601
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003602 // Update counts from autorelease pools
3603 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3604 E = SymReaper.dead_end(); I != E; ++I) {
3605 SymbolRef Sym = *I;
3606 if (const RefVal* T = B.lookup(Sym)){
3607 // Use the symbol as the tag.
3608 // FIXME: This might not be as unique as we would like.
3609 GenericNodeBuilder Bd(Builder, S, Sym);
Ted Kremenekd35272f2009-05-09 00:10:05 +00003610 bool stop = false;
3611 llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3612 Sym, *T, stop);
3613 if (stop)
3614 return;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003615 }
3616 }
Mike Stump11289f42009-09-09 15:08:12 +00003617
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003618 B = state->get<RefBindings>();
Ted Kremenek884a8992009-05-08 23:09:42 +00003619 llvm::SmallVector<SymbolRef, 10> Leaked;
Mike Stump11289f42009-09-09 15:08:12 +00003620
Ted Kremenek400aae72009-02-05 06:50:21 +00003621 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Mike Stump11289f42009-09-09 15:08:12 +00003622 E = SymReaper.dead_end(); I != E; ++I) {
Ted Kremenek884a8992009-05-08 23:09:42 +00003623 if (const RefVal* T = B.lookup(*I))
3624 state = HandleSymbolDeath(state, *I, *T, Leaked);
Mike Stump11289f42009-09-09 15:08:12 +00003625 }
3626
Ted Kremenek884a8992009-05-08 23:09:42 +00003627 static unsigned LeakPPTag = 0;
Ted Kremenek8c8fb482009-05-08 23:32:51 +00003628 {
3629 GenericNodeBuilder Bd(Builder, S, &LeakPPTag);
3630 Pred = ProcessLeaks(state, Leaked, Bd, Eng, Pred);
3631 }
Mike Stump11289f42009-09-09 15:08:12 +00003632
Ted Kremenek884a8992009-05-08 23:09:42 +00003633 // Did we cache out?
3634 if (!Pred)
3635 return;
Mike Stump11289f42009-09-09 15:08:12 +00003636
Ted Kremenek68abaa92009-02-19 23:47:02 +00003637 // Now generate a new node that nukes the old bindings.
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003638 RefBindings::Factory& F = state->get_context<RefBindings>();
Mike Stump11289f42009-09-09 15:08:12 +00003639
Ted Kremenek68abaa92009-02-19 23:47:02 +00003640 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
Ted Kremenek884a8992009-05-08 23:09:42 +00003641 E = SymReaper.dead_end(); I!=E; ++I) B = F.Remove(B, *I);
Mike Stump11289f42009-09-09 15:08:12 +00003642
Ted Kremenekd93c6e32009-06-18 01:23:53 +00003643 state = state->set<RefBindings>(B);
Ted Kremenek68abaa92009-02-19 23:47:02 +00003644 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek400aae72009-02-05 06:50:21 +00003645}
3646
Zhongxing Xu20227f72009-08-06 01:32:16 +00003647void CFRefCount::ProcessNonLeakError(ExplodedNodeSet& Dst,
Zhongxing Xu107f7592009-08-06 12:48:26 +00003648 GRStmtNodeBuilder& Builder,
Zhongxing Xu20227f72009-08-06 01:32:16 +00003649 Expr* NodeExpr, Expr* ErrorExpr,
3650 ExplodedNode* Pred,
Ted Kremenek400aae72009-02-05 06:50:21 +00003651 const GRState* St,
3652 RefVal::Kind hasErr, SymbolRef Sym) {
3653 Builder.BuildSinks = true;
Zhongxing Xu107f7592009-08-06 12:48:26 +00003654 ExplodedNode *N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
Mike Stump11289f42009-09-09 15:08:12 +00003655
Ted Kremenek2d0ff622009-05-09 01:50:57 +00003656 if (!N)
3657 return;
Mike Stump11289f42009-09-09 15:08:12 +00003658
Ted Kremenek400aae72009-02-05 06:50:21 +00003659 CFRefBug *BT = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003660
Ted Kremenekea072e32009-03-17 19:42:23 +00003661 switch (hasErr) {
3662 default:
3663 assert(false && "Unhandled error.");
3664 return;
3665 case RefVal::ErrorUseAfterRelease:
3666 BT = static_cast<CFRefBug*>(useAfterRelease);
Mike Stump11289f42009-09-09 15:08:12 +00003667 break;
Ted Kremenekea072e32009-03-17 19:42:23 +00003668 case RefVal::ErrorReleaseNotOwned:
3669 BT = static_cast<CFRefBug*>(releaseNotOwned);
3670 break;
3671 case RefVal::ErrorDeallocGC:
3672 BT = static_cast<CFRefBug*>(deallocGC);
3673 break;
3674 case RefVal::ErrorDeallocNotOwned:
3675 BT = static_cast<CFRefBug*>(deallocNotOwned);
3676 break;
Ted Kremenek400aae72009-02-05 06:50:21 +00003677 }
Mike Stump11289f42009-09-09 15:08:12 +00003678
Ted Kremenek48d16452009-02-18 03:48:14 +00003679 CFRefReport *report = new CFRefReport(*BT, *this, N, Sym);
Ted Kremenek400aae72009-02-05 06:50:21 +00003680 report->addRange(ErrorExpr->getSourceRange());
3681 BR->EmitReport(report);
3682}
3683
3684//===----------------------------------------------------------------------===//
Ted Kremenek4a78c3a2008-04-10 22:16:52 +00003685// Transfer function creation for external clients.
Ted Kremenek819e9b62008-03-11 06:39:11 +00003686//===----------------------------------------------------------------------===//
3687
Ted Kremenekb0f87c42008-04-30 23:47:44 +00003688GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
3689 const LangOptions& lopts) {
Ted Kremenek1f352db2008-07-22 16:21:24 +00003690 return new CFRefCount(Ctx, GCEnabled, lopts);
Mike Stump11289f42009-09-09 15:08:12 +00003691}