blob: 7e6507cbe53411ca4a5e321605c580ae702f5f38 [file] [log] [blame]
Chris Lattnerbe1a7a02008-03-15 23:59:48 +00001// CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
Ted Kremenek827f93b2008-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 Greif2224fcb2008-03-06 10:40:09 +000010// This file defines the methods for CFRefCount, which implements
Ted Kremenek827f93b2008-03-06 00:08:09 +000011// a reference count checker for Core Foundation (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremeneka7338b42008-03-11 06:39:11 +000015#include "GRSimpleVals.h"
Ted Kremenekfe30beb2008-04-30 23:47:44 +000016#include "clang/Basic/LangOptions.h"
Ted Kremenekfe4d2312008-05-01 23:13:35 +000017#include "clang/Basic/SourceManager.h"
Ted Kremeneka42be302009-02-14 01:43:44 +000018#include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h"
Ted Kremenek91781202008-08-17 03:20:02 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000020#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek827f93b2008-03-06 00:08:09 +000021#include "clang/Analysis/LocalCheckers.h"
Ted Kremenek10fe66d2008-04-09 01:10:13 +000022#include "clang/Analysis/PathDiagnostic.h"
23#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek2ddb4b22009-02-14 03:16:10 +000024#include "clang/Analysis/PathSensitive/SymbolManager.h"
Daniel Dunbar64789f82008-08-11 05:35:13 +000025#include "clang/AST/DeclObjC.h"
Ted Kremeneka7338b42008-03-11 06:39:11 +000026#include "llvm/ADT/DenseMap.h"
27#include "llvm/ADT/FoldingSet.h"
28#include "llvm/ADT/ImmutableMap.h"
Ted Kremenekc8c8d2c2008-10-21 15:53:15 +000029#include "llvm/ADT/ImmutableList.h"
Ted Kremenek2ac4ba62008-05-07 18:36:45 +000030#include "llvm/ADT/StringExtras.h"
Ted Kremenek10fe66d2008-04-09 01:10:13 +000031#include "llvm/Support/Compiler.h"
Ted Kremenekd7e26782008-05-16 18:33:44 +000032#include "llvm/ADT/STLExtras.h"
Ted Kremenek3b11f7a2008-03-11 19:44:10 +000033#include <ostream>
Ted Kremenek9449ca92008-08-12 20:41:56 +000034#include <stdarg.h>
Ted Kremenek827f93b2008-03-06 00:08:09 +000035
36using namespace clang;
Ted Kremenekb6f09542008-10-24 21:18:08 +000037
38//===----------------------------------------------------------------------===//
39// Utility functions.
40//===----------------------------------------------------------------------===//
41
Ted Kremenekb6f09542008-10-24 21:18:08 +000042// The "fundamental rule" for naming conventions of methods:
43// (url broken into two lines)
44// http://developer.apple.com/documentation/Cocoa/Conceptual/
45// MemoryMgmt/Tasks/MemoryManagementRules.html
46//
47// "You take ownership of an object if you create it using a method whose name
48// begins with “alloc” or “new” or contains “copy” (for example, alloc,
49// newObject, or mutableCopy), or if you send it a retain message. You are
50// responsible for relinquishing ownership of objects you own using release
51// or autorelease. Any other time you receive an object, you must
52// not release it."
53//
Ted Kremenek4395b452009-02-21 05:13:43 +000054
55using llvm::CStrInCStrNoCase;
Ted Kremenekfd42ffc2009-02-21 18:26:02 +000056using llvm::StringsEqualNoCase;
Ted Kremenek4395b452009-02-21 05:13:43 +000057
58enum NamingConvention { NoConvention, CreateRule, InitRule };
59
60static inline bool isWordEnd(char ch, char prev, char next) {
61 return ch == '\0'
62 || (islower(prev) && isupper(ch)) // xxxC
63 || (isupper(prev) && isupper(ch) && islower(next)) // XXCreate
64 || !isalpha(ch);
65}
66
67static inline const char* parseWord(const char* s) {
68 char ch = *s, prev = '\0';
69 assert(ch != '\0');
70 char next = *(s+1);
71 while (!isWordEnd(ch, prev, next)) {
72 prev = ch;
73 ch = next;
74 next = *((++s)+1);
75 }
76 return s;
77}
78
79static NamingConvention deriveNamingConvention(const char* s) {
80 // A method/function name may contain a prefix. We don't know it is there,
81 // however, until we encounter the first '_'.
82 bool InPossiblePrefix = true;
83 bool AtBeginning = true;
84 NamingConvention C = NoConvention;
85
86 while (*s != '\0') {
87 // Skip '_'.
88 if (*s == '_') {
89 if (InPossiblePrefix) {
90 InPossiblePrefix = false;
91 AtBeginning = true;
92 // Discard whatever 'convention' we
93 // had already derived since it occurs
94 // in the prefix.
95 C = NoConvention;
96 }
97 ++s;
98 continue;
99 }
100
101 // Skip numbers, ':', etc.
102 if (!isalpha(*s)) {
103 ++s;
104 continue;
105 }
106
107 const char *wordEnd = parseWord(s);
108 assert(wordEnd > s);
109 unsigned len = wordEnd - s;
110
111 switch (len) {
112 default:
113 break;
114 case 3:
115 // Methods starting with 'new' follow the create rule.
Ted Kremenekfd42ffc2009-02-21 18:26:02 +0000116 if (AtBeginning && StringsEqualNoCase("new", s, len))
Ted Kremenek4395b452009-02-21 05:13:43 +0000117 C = CreateRule;
118 break;
119 case 4:
120 // Methods starting with 'alloc' or contain 'copy' follow the
121 // create rule
Ted Kremenekfd42ffc2009-02-21 18:26:02 +0000122 if ((AtBeginning && StringsEqualNoCase("alloc", s, len)) ||
Ted Kremenekea5a6b02009-02-22 07:32:24 +0000123 (C == NoConvention && StringsEqualNoCase("copy", s, len)))
Ted Kremenek4395b452009-02-21 05:13:43 +0000124 C = CreateRule;
125 else // Methods starting with 'init' follow the init rule.
Ted Kremenekfd42ffc2009-02-21 18:26:02 +0000126 if (AtBeginning && StringsEqualNoCase("init", s, len))
Ted Kremenek4395b452009-02-21 05:13:43 +0000127 C = InitRule;
128 break;
129 }
130
131 // If we aren't in the prefix and have a derived convention then just
132 // return it now.
133 if (!InPossiblePrefix && C != NoConvention)
134 return C;
135
136 AtBeginning = false;
137 s = wordEnd;
138 }
139
140 // We will get here if there wasn't more than one word
141 // after the prefix.
142 return C;
143}
144
Ted Kremenekb6f09542008-10-24 21:18:08 +0000145static bool followsFundamentalRule(const char* s) {
Ted Kremenek4395b452009-02-21 05:13:43 +0000146 return deriveNamingConvention(s) == CreateRule;
Ted Kremenekcdd3bb22008-11-05 16:54:44 +0000147}
148
149static bool followsReturnRule(const char* s) {
Ted Kremenek4395b452009-02-21 05:13:43 +0000150 NamingConvention C = deriveNamingConvention(s);
151 return C == CreateRule || C == InitRule;
Ted Kremenekcdd3bb22008-11-05 16:54:44 +0000152}
Ted Kremenekb6f09542008-10-24 21:18:08 +0000153
Ted Kremenek7d421f32008-04-09 23:49:11 +0000154//===----------------------------------------------------------------------===//
Ted Kremenek272aa852008-06-25 21:21:56 +0000155// Selector creation functions.
Ted Kremenekd9ccf682008-04-17 18:12:53 +0000156//===----------------------------------------------------------------------===//
157
Ted Kremenek1bd6ddb2008-05-01 18:31:44 +0000158static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
Ted Kremenekd9ccf682008-04-17 18:12:53 +0000159 IdentifierInfo* II = &Ctx.Idents.get(name);
160 return Ctx.Selectors.getSelector(0, &II);
161}
162
Ted Kremenek0e344d42008-05-06 00:30:21 +0000163static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
164 IdentifierInfo* II = &Ctx.Idents.get(name);
165 return Ctx.Selectors.getSelector(1, &II);
166}
167
Ted Kremenek272aa852008-06-25 21:21:56 +0000168//===----------------------------------------------------------------------===//
169// Type querying functions.
170//===----------------------------------------------------------------------===//
171
Ted Kremenek17144e82009-01-12 21:45:02 +0000172static bool hasPrefix(const char* s, const char* prefix) {
173 if (!prefix)
174 return true;
Ted Kremenek62820d82008-05-07 20:06:41 +0000175
Ted Kremenek17144e82009-01-12 21:45:02 +0000176 char c = *s;
177 char cP = *prefix;
Ted Kremenek62820d82008-05-07 20:06:41 +0000178
Ted Kremenek17144e82009-01-12 21:45:02 +0000179 while (c != '\0' && cP != '\0') {
180 if (c != cP) break;
181 c = *(++s);
182 cP = *(++prefix);
183 }
Ted Kremenek62820d82008-05-07 20:06:41 +0000184
Ted Kremenek17144e82009-01-12 21:45:02 +0000185 return cP == '\0';
Ted Kremenek62820d82008-05-07 20:06:41 +0000186}
187
Ted Kremenek17144e82009-01-12 21:45:02 +0000188static bool hasSuffix(const char* s, const char* suffix) {
189 const char* loc = strstr(s, suffix);
190 return loc && strcmp(suffix, loc) == 0;
191}
192
193static bool isRefType(QualType RetTy, const char* prefix,
194 ASTContext* Ctx = 0, const char* name = 0) {
Ted Kremenek4c5378c2008-07-15 16:50:12 +0000195
Ted Kremenek17144e82009-01-12 21:45:02 +0000196 if (TypedefType* TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
197 const char* TDName = TD->getDecl()->getIdentifier()->getName();
198 return hasPrefix(TDName, prefix) && hasSuffix(TDName, "Ref");
199 }
200
201 if (!Ctx || !name)
Ted Kremenek4c5378c2008-07-15 16:50:12 +0000202 return false;
Ted Kremenek17144e82009-01-12 21:45:02 +0000203
204 // Is the type void*?
205 const PointerType* PT = RetTy->getAsPointerType();
206 if (!(PT->getPointeeType().getUnqualifiedType() == Ctx->VoidTy))
Ted Kremenek4c5378c2008-07-15 16:50:12 +0000207 return false;
Ted Kremenek17144e82009-01-12 21:45:02 +0000208
209 // Does the name start with the prefix?
210 return hasPrefix(name, prefix);
Ted Kremenek4c5378c2008-07-15 16:50:12 +0000211}
212
Ted Kremenekd9ccf682008-04-17 18:12:53 +0000213//===----------------------------------------------------------------------===//
Ted Kremenek272aa852008-06-25 21:21:56 +0000214// Primitives used for constructing summaries for function/method calls.
Ted Kremenek7d421f32008-04-09 23:49:11 +0000215//===----------------------------------------------------------------------===//
216
Ted Kremenek272aa852008-06-25 21:21:56 +0000217namespace {
218/// ArgEffect is used to summarize a function/method call's effect on a
219/// particular argument.
Ted Kremenek58dd95b2009-02-18 18:54:33 +0000220enum ArgEffect { IncRefMsg, IncRef,
221 DecRefMsg, DecRef,
Ted Kremenek2126bef2009-02-18 21:57:45 +0000222 MakeCollectable,
Ted Kremenek58dd95b2009-02-18 18:54:33 +0000223 DoNothing, DoNothingByRef,
Ted Kremenekaac82832009-02-23 17:45:03 +0000224 StopTracking, MayEscape, SelfOwn, Autorelease,
225 NewAutoreleasePool };
Ted Kremenek272aa852008-06-25 21:21:56 +0000226
227/// ArgEffects summarizes the effects of a function/method call on all of
228/// its arguments.
229typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects;
Ted Kremeneka7338b42008-03-11 06:39:11 +0000230}
Ted Kremenek827f93b2008-03-06 00:08:09 +0000231
Ted Kremeneka7338b42008-03-11 06:39:11 +0000232namespace llvm {
Ted Kremenek272aa852008-06-25 21:21:56 +0000233template <> struct FoldingSetTrait<ArgEffects> {
234 static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
235 for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) {
236 ID.AddInteger(I->first);
237 ID.AddInteger((unsigned) I->second);
238 }
239 }
240};
Ted Kremeneka7338b42008-03-11 06:39:11 +0000241} // end llvm namespace
242
243namespace {
Ted Kremenek272aa852008-06-25 21:21:56 +0000244
245/// RetEffect is used to summarize a function/method call's behavior with
246/// respect to its return value.
247class VISIBILITY_HIDDEN RetEffect {
Ted Kremeneka7338b42008-03-11 06:39:11 +0000248public:
Ted Kremenek6a1cc252008-06-23 18:02:52 +0000249 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
250 NotOwnedSymbol, ReceiverAlias };
Ted Kremenek68621b92009-01-28 05:56:51 +0000251
252 enum ObjKind { CF, ObjC, AnyObj };
253
Ted Kremeneka7338b42008-03-11 06:39:11 +0000254private:
Ted Kremenek68621b92009-01-28 05:56:51 +0000255 Kind K;
256 ObjKind O;
257 unsigned index;
258
259 RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {}
260 RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {}
Ted Kremenek827f93b2008-03-06 00:08:09 +0000261
Ted Kremeneka7338b42008-03-11 06:39:11 +0000262public:
Ted Kremenek68621b92009-01-28 05:56:51 +0000263 Kind getKind() const { return K; }
264
265 ObjKind getObjKind() const { return O; }
Ted Kremenek272aa852008-06-25 21:21:56 +0000266
267 unsigned getIndex() const {
Ted Kremeneka7338b42008-03-11 06:39:11 +0000268 assert(getKind() == Alias);
Ted Kremenek68621b92009-01-28 05:56:51 +0000269 return index;
Ted Kremeneka7338b42008-03-11 06:39:11 +0000270 }
Ted Kremenek827f93b2008-03-06 00:08:09 +0000271
Ted Kremenek272aa852008-06-25 21:21:56 +0000272 static RetEffect MakeAlias(unsigned Idx) {
273 return RetEffect(Alias, Idx);
274 }
275 static RetEffect MakeReceiverAlias() {
276 return RetEffect(ReceiverAlias);
277 }
Ted Kremenek68621b92009-01-28 05:56:51 +0000278 static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) {
279 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o);
Ted Kremenek272aa852008-06-25 21:21:56 +0000280 }
Ted Kremenek68621b92009-01-28 05:56:51 +0000281 static RetEffect MakeNotOwned(ObjKind o) {
282 return RetEffect(NotOwnedSymbol, o);
Ted Kremenek272aa852008-06-25 21:21:56 +0000283 }
284 static RetEffect MakeNoRet() {
285 return RetEffect(NoRet);
Ted Kremenek6a1cc252008-06-23 18:02:52 +0000286 }
Ted Kremenek827f93b2008-03-06 00:08:09 +0000287
Ted Kremenek272aa852008-06-25 21:21:56 +0000288 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek68621b92009-01-28 05:56:51 +0000289 ID.AddInteger((unsigned)K);
290 ID.AddInteger((unsigned)O);
291 ID.AddInteger(index);
Ted Kremenek272aa852008-06-25 21:21:56 +0000292 }
Ted Kremeneka7338b42008-03-11 06:39:11 +0000293};
Ted Kremeneka7338b42008-03-11 06:39:11 +0000294
Ted Kremenek272aa852008-06-25 21:21:56 +0000295
296class VISIBILITY_HIDDEN RetainSummary : public llvm::FoldingSetNode {
Ted Kremenekbcaff792008-05-06 15:44:25 +0000297 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
298 /// specifies the argument (starting from 0). This can be sparsely
299 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremeneka7338b42008-03-11 06:39:11 +0000300 ArgEffects* Args;
Ted Kremenekbcaff792008-05-06 15:44:25 +0000301
302 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
303 /// do not have an entry in Args.
304 ArgEffect DefaultArgEffect;
305
Ted Kremenek272aa852008-06-25 21:21:56 +0000306 /// Receiver - If this summary applies to an Objective-C message expression,
307 /// this is the effect applied to the state of the receiver.
Ted Kremenek266d8b62008-05-06 02:26:56 +0000308 ArgEffect Receiver;
Ted Kremenek272aa852008-06-25 21:21:56 +0000309
310 /// Ret - The effect on the return value. Used to indicate if the
311 /// function/method call returns a new tracked symbol, returns an
312 /// alias of one of the arguments in the call, and so on.
Ted Kremeneka7338b42008-03-11 06:39:11 +0000313 RetEffect Ret;
Ted Kremenek272aa852008-06-25 21:21:56 +0000314
Ted Kremenekf2717b02008-07-18 17:24:20 +0000315 /// EndPath - Indicates that execution of this method/function should
316 /// terminate the simulation of a path.
317 bool EndPath;
318
Ted Kremeneka7338b42008-03-11 06:39:11 +0000319public:
320
Ted Kremenekbcaff792008-05-06 15:44:25 +0000321 RetainSummary(ArgEffects* A, RetEffect R, ArgEffect defaultEff,
Ted Kremenekf2717b02008-07-18 17:24:20 +0000322 ArgEffect ReceiverEff, bool endpath = false)
323 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
324 EndPath(endpath) {}
Ted Kremeneka7338b42008-03-11 06:39:11 +0000325
Ted Kremenek272aa852008-06-25 21:21:56 +0000326 /// getArg - Return the argument effect on the argument specified by
327 /// idx (starting from 0).
Ted Kremenek0d721572008-03-11 17:48:22 +0000328 ArgEffect getArg(unsigned idx) const {
Ted Kremenekbcaff792008-05-06 15:44:25 +0000329
Ted Kremenekae855d42008-04-24 17:22:33 +0000330 if (!Args)
Ted Kremenekbcaff792008-05-06 15:44:25 +0000331 return DefaultArgEffect;
Ted Kremenekae855d42008-04-24 17:22:33 +0000332
333 // If Args is present, it is likely to contain only 1 element.
334 // Just do a linear search. Do it from the back because functions with
335 // large numbers of arguments will be tail heavy with respect to which
Ted Kremenek272aa852008-06-25 21:21:56 +0000336 // argument they actually modify with respect to the reference count.
Ted Kremenekae855d42008-04-24 17:22:33 +0000337 for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend();
338 I!=E; ++I) {
339
340 if (idx > I->first)
Ted Kremenekbcaff792008-05-06 15:44:25 +0000341 return DefaultArgEffect;
Ted Kremenekae855d42008-04-24 17:22:33 +0000342
343 if (idx == I->first)
344 return I->second;
345 }
346
Ted Kremenekbcaff792008-05-06 15:44:25 +0000347 return DefaultArgEffect;
Ted Kremenek0d721572008-03-11 17:48:22 +0000348 }
349
Ted Kremenek272aa852008-06-25 21:21:56 +0000350 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek266d8b62008-05-06 02:26:56 +0000351 RetEffect getRetEffect() const {
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000352 return Ret;
353 }
354
Ted Kremenekf2717b02008-07-18 17:24:20 +0000355 /// isEndPath - Returns true if executing the given method/function should
356 /// terminate the path.
357 bool isEndPath() const { return EndPath; }
358
Ted Kremenek272aa852008-06-25 21:21:56 +0000359 /// getReceiverEffect - Returns the effect on the receiver of the call.
360 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek266d8b62008-05-06 02:26:56 +0000361 ArgEffect getReceiverEffect() const {
362 return Receiver;
363 }
364
Ted Kremenek2719e982008-06-17 02:43:46 +0000365 typedef ArgEffects::const_iterator ExprIterator;
Ted Kremeneka7338b42008-03-11 06:39:11 +0000366
Ted Kremenek2719e982008-06-17 02:43:46 +0000367 ExprIterator begin_args() const { return Args->begin(); }
368 ExprIterator end_args() const { return Args->end(); }
Ted Kremeneka7338b42008-03-11 06:39:11 +0000369
Ted Kremenek266d8b62008-05-06 02:26:56 +0000370 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A,
Ted Kremenekbcaff792008-05-06 15:44:25 +0000371 RetEffect RetEff, ArgEffect DefaultEff,
Ted Kremenek6fbecac2008-07-18 17:39:56 +0000372 ArgEffect ReceiverEff, bool EndPath) {
Ted Kremeneka7338b42008-03-11 06:39:11 +0000373 ID.AddPointer(A);
Ted Kremenek266d8b62008-05-06 02:26:56 +0000374 ID.Add(RetEff);
Ted Kremenekbcaff792008-05-06 15:44:25 +0000375 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenek266d8b62008-05-06 02:26:56 +0000376 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenek6fbecac2008-07-18 17:39:56 +0000377 ID.AddInteger((unsigned) EndPath);
Ted Kremeneka7338b42008-03-11 06:39:11 +0000378 }
379
380 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek6fbecac2008-07-18 17:39:56 +0000381 Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
Ted Kremeneka7338b42008-03-11 06:39:11 +0000382 }
383};
Ted Kremenek84f010c2008-06-23 23:30:29 +0000384} // end anonymous namespace
Ted Kremeneka7338b42008-03-11 06:39:11 +0000385
Ted Kremenek272aa852008-06-25 21:21:56 +0000386//===----------------------------------------------------------------------===//
387// Data structures for constructing summaries.
388//===----------------------------------------------------------------------===//
Ted Kremenek9f0fc792008-06-24 03:49:48 +0000389
Ted Kremenek272aa852008-06-25 21:21:56 +0000390namespace {
391class VISIBILITY_HIDDEN ObjCSummaryKey {
392 IdentifierInfo* II;
393 Selector S;
394public:
395 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
396 : II(ii), S(s) {}
397
398 ObjCSummaryKey(ObjCInterfaceDecl* d, Selector s)
399 : II(d ? d->getIdentifier() : 0), S(s) {}
400
401 ObjCSummaryKey(Selector s)
402 : II(0), S(s) {}
403
404 IdentifierInfo* getIdentifier() const { return II; }
405 Selector getSelector() const { return S; }
406};
Ted Kremenek84f010c2008-06-23 23:30:29 +0000407}
408
409namespace llvm {
Ted Kremenek272aa852008-06-25 21:21:56 +0000410template <> struct DenseMapInfo<ObjCSummaryKey> {
411 static inline ObjCSummaryKey getEmptyKey() {
412 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
413 DenseMapInfo<Selector>::getEmptyKey());
414 }
Ted Kremenek84f010c2008-06-23 23:30:29 +0000415
Ted Kremenek272aa852008-06-25 21:21:56 +0000416 static inline ObjCSummaryKey getTombstoneKey() {
417 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
418 DenseMapInfo<Selector>::getTombstoneKey());
419 }
420
421 static unsigned getHashValue(const ObjCSummaryKey &V) {
422 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
423 & 0x88888888)
424 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
425 & 0x55555555);
426 }
427
428 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
429 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
430 RHS.getIdentifier()) &&
431 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
432 RHS.getSelector());
433 }
434
435 static bool isPod() {
436 return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
437 DenseMapInfo<Selector>::isPod();
438 }
439};
Ted Kremenek84f010c2008-06-23 23:30:29 +0000440} // end llvm namespace
Ted Kremeneka7338b42008-03-11 06:39:11 +0000441
Ted Kremenek84f010c2008-06-23 23:30:29 +0000442namespace {
Ted Kremenek272aa852008-06-25 21:21:56 +0000443class VISIBILITY_HIDDEN ObjCSummaryCache {
444 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
445 MapTy M;
446public:
447 ObjCSummaryCache() {}
448
449 typedef MapTy::iterator iterator;
450
451 iterator find(ObjCInterfaceDecl* D, Selector S) {
452
453 // Do a lookup with the (D,S) pair. If we find a match return
454 // the iterator.
455 ObjCSummaryKey K(D, S);
456 MapTy::iterator I = M.find(K);
457
458 if (I != M.end() || !D)
459 return I;
460
461 // Walk the super chain. If we find a hit with a parent, we'll end
462 // up returning that summary. We actually allow that key (null,S), as
463 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
464 // generate initial summaries without having to worry about NSObject
465 // being declared.
466 // FIXME: We may change this at some point.
467 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
468 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
469 break;
470
471 if (!C)
472 return I;
473 }
474
475 // Cache the summary with original key to make the next lookup faster
476 // and return the iterator.
477 M[K] = I->second;
478 return I;
479 }
480
Ted Kremenek9449ca92008-08-12 20:41:56 +0000481
Ted Kremenek272aa852008-06-25 21:21:56 +0000482 iterator find(Expr* Receiver, Selector S) {
483 return find(getReceiverDecl(Receiver), S);
484 }
485
486 iterator find(IdentifierInfo* II, Selector S) {
487 // FIXME: Class method lookup. Right now we dont' have a good way
488 // of going between IdentifierInfo* and the class hierarchy.
489 iterator I = M.find(ObjCSummaryKey(II, S));
490 return I == M.end() ? M.find(ObjCSummaryKey(S)) : I;
491 }
492
493 ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
494
495 const PointerType* PT = E->getType()->getAsPointerType();
496 if (!PT) return 0;
497
498 ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType());
499 if (!OI) return 0;
500
501 return OI ? OI->getDecl() : 0;
502 }
503
504 iterator end() { return M.end(); }
505
506 RetainSummary*& operator[](ObjCMessageExpr* ME) {
507
508 Selector S = ME->getSelector();
509
510 if (Expr* Receiver = ME->getReceiver()) {
511 ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
512 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
513 }
514
515 return M[ObjCSummaryKey(ME->getClassName(), S)];
516 }
517
518 RetainSummary*& operator[](ObjCSummaryKey K) {
519 return M[K];
520 }
521
522 RetainSummary*& operator[](Selector S) {
523 return M[ ObjCSummaryKey(S) ];
524 }
525};
526} // end anonymous namespace
527
528//===----------------------------------------------------------------------===//
529// Data structures for managing collections of summaries.
530//===----------------------------------------------------------------------===//
531
532namespace {
533class VISIBILITY_HIDDEN RetainSummaryManager {
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000534
535 //==-----------------------------------------------------------------==//
536 // Typedefs.
537 //==-----------------------------------------------------------------==//
Ted Kremeneka7338b42008-03-11 06:39:11 +0000538
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000539 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> >
540 ArgEffectsSetTy;
541
542 typedef llvm::FoldingSet<RetainSummary>
543 SummarySetTy;
544
545 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
546 FuncSummariesTy;
547
Ted Kremenek84f010c2008-06-23 23:30:29 +0000548 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000549
550 //==-----------------------------------------------------------------==//
551 // Data.
552 //==-----------------------------------------------------------------==//
553
Ted Kremenek272aa852008-06-25 21:21:56 +0000554 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek9b0c09c2008-04-29 05:33:51 +0000555 ASTContext& Ctx;
Ted Kremeneke44927e2008-07-01 17:21:27 +0000556
Ted Kremenekede40b72008-07-09 18:11:16 +0000557 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
558 /// "CFDictionaryCreate".
559 IdentifierInfo* CFDictionaryCreateII;
560
Ted Kremenek272aa852008-06-25 21:21:56 +0000561 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek9b0c09c2008-04-29 05:33:51 +0000562 const bool GCEnabled;
563
Ted Kremenek272aa852008-06-25 21:21:56 +0000564 /// SummarySet - A FoldingSet of uniqued summaries.
Ted Kremeneka4c74292008-04-10 22:58:08 +0000565 SummarySetTy SummarySet;
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000566
Ted Kremenek272aa852008-06-25 21:21:56 +0000567 /// FuncSummaries - A map from FunctionDecls to summaries.
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000568 FuncSummariesTy FuncSummaries;
569
Ted Kremenek272aa852008-06-25 21:21:56 +0000570 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
571 /// to summaries.
Ted Kremenek97c1e0c2008-06-23 22:21:20 +0000572 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000573
Ted Kremenek272aa852008-06-25 21:21:56 +0000574 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenek97c1e0c2008-06-23 22:21:20 +0000575 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000576
Ted Kremenek272aa852008-06-25 21:21:56 +0000577 /// ArgEffectsSet - A FoldingSet of uniqued ArgEffects.
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000578 ArgEffectsSetTy ArgEffectsSet;
579
Ted Kremenek272aa852008-06-25 21:21:56 +0000580 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
581 /// and all other data used by the checker.
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000582 llvm::BumpPtrAllocator BPAlloc;
583
Ted Kremenek272aa852008-06-25 21:21:56 +0000584 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000585 ArgEffects ScratchArgs;
586
Ted Kremenekb3a44e72008-05-06 18:11:36 +0000587 RetainSummary* StopSummary;
588
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000589 //==-----------------------------------------------------------------==//
590 // Methods.
591 //==-----------------------------------------------------------------==//
592
Ted Kremenek272aa852008-06-25 21:21:56 +0000593 /// getArgEffects - Returns a persistent ArgEffects object based on the
594 /// data in ScratchArgs.
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000595 ArgEffects* getArgEffects();
Ted Kremeneka7338b42008-03-11 06:39:11 +0000596
Ted Kremenek562c1302008-05-05 16:51:50 +0000597 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
Ted Kremenek63d09ae2008-10-23 01:56:15 +0000598
599public:
Ted Kremenek064ef322009-02-23 16:51:39 +0000600 RetainSummary* getUnarySummary(const FunctionType* FT, UnaryFuncKind func);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000601
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000602 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
603 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek17144e82009-01-12 21:45:02 +0000604 RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000605
Ted Kremenek266d8b62008-05-06 02:26:56 +0000606 RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenekbcaff792008-05-06 15:44:25 +0000607 ArgEffect ReceiverEff = DoNothing,
Ted Kremenekf2717b02008-07-18 17:24:20 +0000608 ArgEffect DefaultEff = MayEscape,
609 bool isEndPath = false);
Ted Kremenek45d0b502008-10-29 04:07:07 +0000610
Ted Kremenek266d8b62008-05-06 02:26:56 +0000611 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenekbcaff792008-05-06 15:44:25 +0000612 ArgEffect ReceiverEff = DoNothing,
Ted Kremeneka3f30dd2008-05-22 17:31:13 +0000613 ArgEffect DefaultEff = MayEscape) {
Ted Kremenekbcaff792008-05-06 15:44:25 +0000614 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek0e344d42008-05-06 00:30:21 +0000615 }
Ted Kremenek42ea0322008-05-05 23:55:01 +0000616
Ted Kremenekbcaff792008-05-06 15:44:25 +0000617 RetainSummary* getPersistentStopSummary() {
Ted Kremenekb3a44e72008-05-06 18:11:36 +0000618 if (StopSummary)
619 return StopSummary;
620
621 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
622 StopTracking, StopTracking);
Ted Kremenek45d0b502008-10-29 04:07:07 +0000623
Ted Kremenekb3a44e72008-05-06 18:11:36 +0000624 return StopSummary;
Ted Kremenekbcaff792008-05-06 15:44:25 +0000625 }
Ted Kremenek926abf22008-05-06 04:20:12 +0000626
Ted Kremenek272aa852008-06-25 21:21:56 +0000627 RetainSummary* getInitMethodSummary(ObjCMessageExpr* ME);
Ted Kremenek42ea0322008-05-05 23:55:01 +0000628
Ted Kremenek97c1e0c2008-06-23 22:21:20 +0000629 void InitializeClassMethodSummaries();
630 void InitializeMethodSummaries();
Ted Kremenek63d09ae2008-10-23 01:56:15 +0000631
Ted Kremenek35920ed2009-01-07 00:39:56 +0000632 bool isTrackedObjectType(QualType T);
633
Ted Kremenek63d09ae2008-10-23 01:56:15 +0000634private:
635
Ted Kremenekf2717b02008-07-18 17:24:20 +0000636 void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
637 RetainSummary* Summ) {
638 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
639 }
640
Ted Kremenek272aa852008-06-25 21:21:56 +0000641 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
642 ObjCClassMethodSummaries[S] = Summ;
643 }
644
645 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
646 ObjCMethodSummaries[S] = Summ;
647 }
648
Ted Kremenek1b4b6562009-02-25 02:54:57 +0000649 void addInstMethSummary(const char* Cls, const char* nullaryName,
650 RetainSummary *Summ) {
651 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
652 Selector S = GetNullarySelector(nullaryName, Ctx);
653 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
654 }
655
Ted Kremenek45642a42008-08-12 18:48:50 +0000656 void addInstMethSummary(const char* Cls, RetainSummary* Summ, va_list argp) {
Ted Kremenekf2717b02008-07-18 17:24:20 +0000657
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +0000658 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
659 llvm::SmallVector<IdentifierInfo*, 10> II;
660
661 while (const char* s = va_arg(argp, const char*))
662 II.push_back(&Ctx.Idents.get(s));
663
664 Selector S = Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenekf2717b02008-07-18 17:24:20 +0000665 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
666 }
Ted Kremenek45642a42008-08-12 18:48:50 +0000667
668 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
669 va_list argp;
670 va_start(argp, Summ);
671 addInstMethSummary(Cls, Summ, argp);
672 va_end(argp);
673 }
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +0000674
675 void addPanicSummary(const char* Cls, ...) {
676 RetainSummary* Summ = getPersistentSummary(0, RetEffect::MakeNoRet(),
677 DoNothing, DoNothing, true);
678 va_list argp;
679 va_start (argp, Cls);
Ted Kremenek45642a42008-08-12 18:48:50 +0000680 addInstMethSummary(Cls, Summ, argp);
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +0000681 va_end(argp);
682 }
Ted Kremenekf2717b02008-07-18 17:24:20 +0000683
Ted Kremeneka7338b42008-03-11 06:39:11 +0000684public:
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000685
686 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremeneke44927e2008-07-01 17:21:27 +0000687 : Ctx(ctx),
Ted Kremenekede40b72008-07-09 18:11:16 +0000688 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek272aa852008-06-25 21:21:56 +0000689 GCEnabled(gcenabled), StopSummary(0) {
690
691 InitializeClassMethodSummaries();
692 InitializeMethodSummaries();
693 }
Ted Kremenek9b0c09c2008-04-29 05:33:51 +0000694
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000695 ~RetainSummaryManager();
Ted Kremeneka7338b42008-03-11 06:39:11 +0000696
Ted Kremenekd13c1872008-06-24 03:56:45 +0000697 RetainSummary* getSummary(FunctionDecl* FD);
Ted Kremenek272aa852008-06-25 21:21:56 +0000698 RetainSummary* getMethodSummary(ObjCMessageExpr* ME, ObjCInterfaceDecl* ID);
Ted Kremenek97c1e0c2008-06-23 22:21:20 +0000699 RetainSummary* getClassMethodSummary(IdentifierInfo* ClsName, Selector S);
Ted Kremenek926abf22008-05-06 04:20:12 +0000700
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000701 bool isGCEnabled() const { return GCEnabled; }
Ted Kremeneka7338b42008-03-11 06:39:11 +0000702};
703
704} // end anonymous namespace
705
706//===----------------------------------------------------------------------===//
707// Implementation of checker data structures.
708//===----------------------------------------------------------------------===//
709
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000710RetainSummaryManager::~RetainSummaryManager() {
Ted Kremeneka7338b42008-03-11 06:39:11 +0000711
712 // FIXME: The ArgEffects could eventually be allocated from BPAlloc,
713 // mitigating the need to do explicit cleanup of the
714 // Argument-Effect summaries.
715
Ted Kremenek42ea0322008-05-05 23:55:01 +0000716 for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(),
717 E = ArgEffectsSet.end(); I!=E; ++I)
Ted Kremeneka7338b42008-03-11 06:39:11 +0000718 I->getValue().~ArgEffects();
Ted Kremenek827f93b2008-03-06 00:08:09 +0000719}
Ted Kremeneka7338b42008-03-11 06:39:11 +0000720
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000721ArgEffects* RetainSummaryManager::getArgEffects() {
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000722
Ted Kremenekae855d42008-04-24 17:22:33 +0000723 if (ScratchArgs.empty())
724 return NULL;
725
726 // Compute a profile for a non-empty ScratchArgs.
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000727 llvm::FoldingSetNodeID profile;
728 profile.Add(ScratchArgs);
729 void* InsertPos;
730
Ted Kremenekae855d42008-04-24 17:22:33 +0000731 // Look up the uniqued copy, or create a new one.
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000732 llvm::FoldingSetNodeWrapper<ArgEffects>* E =
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000733 ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000734
Ted Kremenekae855d42008-04-24 17:22:33 +0000735 if (E) {
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000736 ScratchArgs.clear();
737 return &E->getValue();
738 }
739
740 E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
Ted Kremenek272aa852008-06-25 21:21:56 +0000741 BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000742
743 new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000744 ArgEffectsSet.InsertNode(E, InsertPos);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000745
746 ScratchArgs.clear();
747 return &E->getValue();
748}
749
Ted Kremenek266d8b62008-05-06 02:26:56 +0000750RetainSummary*
751RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenekbcaff792008-05-06 15:44:25 +0000752 ArgEffect ReceiverEff,
Ted Kremenekf2717b02008-07-18 17:24:20 +0000753 ArgEffect DefaultEff,
754 bool isEndPath) {
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000755
Ted Kremenekae855d42008-04-24 17:22:33 +0000756 // Generate a profile for the summary.
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000757 llvm::FoldingSetNodeID profile;
Ted Kremenek6fbecac2008-07-18 17:39:56 +0000758 RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff,
759 isEndPath);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000760
Ted Kremenekae855d42008-04-24 17:22:33 +0000761 // Look up the uniqued summary, or create one if it doesn't exist.
762 void* InsertPos;
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000763 RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000764
765 if (Summ)
766 return Summ;
767
Ted Kremenekae855d42008-04-24 17:22:33 +0000768 // Create the summary and return it.
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000769 Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenekf2717b02008-07-18 17:24:20 +0000770 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000771 SummarySet.InsertNode(Summ, InsertPos);
772
773 return Summ;
774}
775
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000776//===----------------------------------------------------------------------===//
Ted Kremenek35920ed2009-01-07 00:39:56 +0000777// Predicates.
778//===----------------------------------------------------------------------===//
779
780bool RetainSummaryManager::isTrackedObjectType(QualType T) {
781 if (!Ctx.isObjCObjectPointerType(T))
782 return false;
783
784 // Does it subclass NSObject?
785 ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
786
787 // We assume that id<..>, id, and "Class" all represent tracked objects.
788 if (!OT)
789 return true;
790
791 // Does the object type subclass NSObject?
792 // FIXME: We can memoize here if this gets too expensive.
793 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
794 ObjCInterfaceDecl* ID = OT->getDecl();
795
796 for ( ; ID ; ID = ID->getSuperClass())
797 if (ID->getIdentifier() == NSObjectII)
798 return true;
799
800 return false;
801}
802
803//===----------------------------------------------------------------------===//
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000804// Summary creation for functions (largely uses of Core Foundation).
805//===----------------------------------------------------------------------===//
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000806
Ted Kremenek17144e82009-01-12 21:45:02 +0000807static bool isRetain(FunctionDecl* FD, const char* FName) {
808 const char* loc = strstr(FName, "Retain");
809 return loc && loc[sizeof("Retain")-1] == '\0';
810}
811
812static bool isRelease(FunctionDecl* FD, const char* FName) {
813 const char* loc = strstr(FName, "Release");
814 return loc && loc[sizeof("Release")-1] == '\0';
815}
816
Ted Kremenekd13c1872008-06-24 03:56:45 +0000817RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000818
819 SourceLocation Loc = FD->getLocation();
820
821 if (!Loc.isFileID())
822 return NULL;
Ted Kremenek827f93b2008-03-06 00:08:09 +0000823
Ted Kremenekae855d42008-04-24 17:22:33 +0000824 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000825 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenekae855d42008-04-24 17:22:33 +0000826
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000827 if (I != FuncSummaries.end())
Ted Kremenekae855d42008-04-24 17:22:33 +0000828 return I->second;
829
830 // No summary. Generate one.
Ted Kremenek17144e82009-01-12 21:45:02 +0000831 RetainSummary *S = 0;
Ted Kremenek562c1302008-05-05 16:51:50 +0000832
Ted Kremenek4c5378c2008-07-15 16:50:12 +0000833 do {
Ted Kremenek17144e82009-01-12 21:45:02 +0000834 // We generate "stop" summaries for implicitly defined functions.
835 if (FD->isImplicit()) {
836 S = getPersistentStopSummary();
837 break;
Ted Kremenek4c5378c2008-07-15 16:50:12 +0000838 }
Ted Kremenekeafcc2f2008-11-04 00:36:12 +0000839
Ted Kremenek064ef322009-02-23 16:51:39 +0000840 // [PR 3337] Use 'getAsFunctionType' to strip away any typedefs on the
Ted Kremenekc239b9c2009-01-16 18:40:33 +0000841 // function's type.
Ted Kremenek064ef322009-02-23 16:51:39 +0000842 const FunctionType* FT = FD->getType()->getAsFunctionType();
Ted Kremenek17144e82009-01-12 21:45:02 +0000843 const char* FName = FD->getIdentifier()->getName();
844
845 // Inspect the result type.
846 QualType RetTy = FT->getResultType();
847
848 // FIXME: This should all be refactored into a chain of "summary lookup"
849 // filters.
850 if (strcmp(FName, "IOServiceGetMatchingServices") == 0) {
851 // FIXES: <rdar://problem/6326900>
852 // This should be addressed using a API table. This strcmp is also
853 // a little gross, but there is no need to super optimize here.
854 assert (ScratchArgs.empty());
855 ScratchArgs.push_back(std::make_pair(1, DecRef));
856 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
857 break;
Ted Kremenekcfc50c72008-10-22 20:54:52 +0000858 }
Ted Kremenek17144e82009-01-12 21:45:02 +0000859
860 // Handle: id NSMakeCollectable(CFTypeRef)
861 if (strcmp(FName, "NSMakeCollectable") == 0) {
862 S = (RetTy == Ctx.getObjCIdType())
863 ? getUnarySummary(FT, cfmakecollectable)
864 : getPersistentStopSummary();
865
866 break;
867 }
868
869 if (RetTy->isPointerType()) {
870 // For CoreFoundation ('CF') types.
871 if (isRefType(RetTy, "CF", &Ctx, FName)) {
872 if (isRetain(FD, FName))
873 S = getUnarySummary(FT, cfretain);
874 else if (strstr(FName, "MakeCollectable"))
875 S = getUnarySummary(FT, cfmakecollectable);
876 else
877 S = getCFCreateGetRuleSummary(FD, FName);
878
879 break;
880 }
881
882 // For CoreGraphics ('CG') types.
883 if (isRefType(RetTy, "CG", &Ctx, FName)) {
884 if (isRetain(FD, FName))
885 S = getUnarySummary(FT, cfretain);
886 else
887 S = getCFCreateGetRuleSummary(FD, FName);
888
889 break;
890 }
891
892 // For the Disk Arbitration API (DiskArbitration/DADisk.h)
893 if (isRefType(RetTy, "DADisk") ||
894 isRefType(RetTy, "DADissenter") ||
895 isRefType(RetTy, "DASessionRef")) {
896 S = getCFCreateGetRuleSummary(FD, FName);
897 break;
898 }
899
900 break;
901 }
902
903 // Check for release functions, the only kind of functions that we care
904 // about that don't return a pointer type.
905 if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) {
906 if (isRelease(FD, FName+2))
907 S = getUnarySummary(FT, cfrelease);
908 else {
Ted Kremenek7b293682009-01-29 22:45:13 +0000909 assert (ScratchArgs.empty());
910 // Remaining CoreFoundation and CoreGraphics functions.
911 // We use to assume that they all strictly followed the ownership idiom
912 // and that ownership cannot be transferred. While this is technically
913 // correct, many methods allow a tracked object to escape. For example:
914 //
915 // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...);
916 // CFDictionaryAddValue(y, key, x);
917 // CFRelease(x);
918 // ... it is okay to use 'x' since 'y' has a reference to it
919 //
920 // We handle this and similar cases with the follow heuristic. If the
921 // function name contains "InsertValue", "SetValue" or "AddValue" then
922 // we assume that arguments may "escape."
923 //
924 ArgEffect E = (CStrInCStrNoCase(FName, "InsertValue") ||
925 CStrInCStrNoCase(FName, "AddValue") ||
Ted Kremenekcf071252009-02-05 22:34:53 +0000926 CStrInCStrNoCase(FName, "SetValue") ||
927 CStrInCStrNoCase(FName, "AppendValue"))
Ted Kremenek7b293682009-01-29 22:45:13 +0000928 ? MayEscape : DoNothing;
929
930 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E);
Ted Kremenek17144e82009-01-12 21:45:02 +0000931 }
932 }
Ted Kremenek4c5378c2008-07-15 16:50:12 +0000933 }
934 while (0);
Ted Kremenekae855d42008-04-24 17:22:33 +0000935
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000936 FuncSummaries[FD] = S;
Ted Kremenek562c1302008-05-05 16:51:50 +0000937 return S;
Ted Kremenek827f93b2008-03-06 00:08:09 +0000938}
939
Ted Kremenek4c5378c2008-07-15 16:50:12 +0000940RetainSummary*
941RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
942 const char* FName) {
943
Ted Kremenek562c1302008-05-05 16:51:50 +0000944 if (strstr(FName, "Create") || strstr(FName, "Copy"))
945 return getCFSummaryCreateRule(FD);
Ted Kremenek4c5378c2008-07-15 16:50:12 +0000946
Ted Kremenek562c1302008-05-05 16:51:50 +0000947 if (strstr(FName, "Get"))
948 return getCFSummaryGetRule(FD);
949
950 return 0;
951}
952
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000953RetainSummary*
Ted Kremenek064ef322009-02-23 16:51:39 +0000954RetainSummaryManager::getUnarySummary(const FunctionType* FT,
955 UnaryFuncKind func) {
956
Ted Kremenek17144e82009-01-12 21:45:02 +0000957 // Sanity check that this is *really* a unary function. This can
958 // happen if people do weird things.
Douglas Gregor4fa58902009-02-26 23:50:07 +0000959 const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT);
Ted Kremenek17144e82009-01-12 21:45:02 +0000960 if (!FTP || FTP->getNumArgs() != 1)
961 return getPersistentStopSummary();
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000962
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000963 assert (ScratchArgs.empty());
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000964
Ted Kremenek9b0c09c2008-04-29 05:33:51 +0000965 switch (func) {
Ted Kremenek17144e82009-01-12 21:45:02 +0000966 case cfretain: {
Ted Kremenek9b0c09c2008-04-29 05:33:51 +0000967 ScratchArgs.push_back(std::make_pair(0, IncRef));
Ted Kremeneka3f30dd2008-05-22 17:31:13 +0000968 return getPersistentSummary(RetEffect::MakeAlias(0),
969 DoNothing, DoNothing);
Ted Kremenek9b0c09c2008-04-29 05:33:51 +0000970 }
971
972 case cfrelease: {
Ted Kremenek9b0c09c2008-04-29 05:33:51 +0000973 ScratchArgs.push_back(std::make_pair(0, DecRef));
Ted Kremeneka3f30dd2008-05-22 17:31:13 +0000974 return getPersistentSummary(RetEffect::MakeNoRet(),
975 DoNothing, DoNothing);
Ted Kremenek9b0c09c2008-04-29 05:33:51 +0000976 }
977
978 case cfmakecollectable: {
Ted Kremenek2126bef2009-02-18 21:57:45 +0000979 ScratchArgs.push_back(std::make_pair(0, MakeCollectable));
980 return getPersistentSummary(RetEffect::MakeAlias(0),DoNothing, DoNothing);
Ted Kremenek9b0c09c2008-04-29 05:33:51 +0000981 }
982
983 default:
Ted Kremenek562c1302008-05-05 16:51:50 +0000984 assert (false && "Not a supported unary function.");
Ted Kremenek9449ca92008-08-12 20:41:56 +0000985 return 0;
Ted Kremenekab2fa2a2008-04-10 23:44:06 +0000986 }
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000987}
988
Ted Kremeneka8c3c432008-05-05 22:11:16 +0000989RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenekae855d42008-04-24 17:22:33 +0000990 assert (ScratchArgs.empty());
Ted Kremenekede40b72008-07-09 18:11:16 +0000991
992 if (FD->getIdentifier() == CFDictionaryCreateII) {
993 ScratchArgs.push_back(std::make_pair(1, DoNothingByRef));
994 ScratchArgs.push_back(std::make_pair(2, DoNothingByRef));
995 }
996
Ted Kremenek68621b92009-01-28 05:56:51 +0000997 return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000998}
999
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001000RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Ted Kremenekae855d42008-04-24 17:22:33 +00001001 assert (ScratchArgs.empty());
Ted Kremenek68621b92009-01-28 05:56:51 +00001002 return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF),
1003 DoNothing, DoNothing);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001004}
1005
Ted Kremeneka7338b42008-03-11 06:39:11 +00001006//===----------------------------------------------------------------------===//
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001007// Summary creation for Selectors.
1008//===----------------------------------------------------------------------===//
1009
Ted Kremenekbcaff792008-05-06 15:44:25 +00001010RetainSummary*
Ted Kremenek272aa852008-06-25 21:21:56 +00001011RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) {
Ted Kremenek42ea0322008-05-05 23:55:01 +00001012 assert(ScratchArgs.empty());
1013
Ted Kremenek802cfc72009-02-20 00:05:35 +00001014 // 'init' methods only return an alias if the return type is a location type.
1015 QualType T = ME->getType();
Ted Kremenek42ea0322008-05-05 23:55:01 +00001016 RetainSummary* Summ =
Ted Kremenek802cfc72009-02-20 00:05:35 +00001017 getPersistentSummary(Loc::IsLocType(T) ? RetEffect::MakeReceiverAlias()
1018 : RetEffect::MakeNoRet());
Ted Kremenek42ea0322008-05-05 23:55:01 +00001019
Ted Kremenek272aa852008-06-25 21:21:56 +00001020 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek42ea0322008-05-05 23:55:01 +00001021 return Summ;
1022}
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001023
Ted Kremenek272aa852008-06-25 21:21:56 +00001024
Ted Kremenekbcaff792008-05-06 15:44:25 +00001025RetainSummary*
Ted Kremenek272aa852008-06-25 21:21:56 +00001026RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
1027 ObjCInterfaceDecl* ID) {
Ted Kremenekbcaff792008-05-06 15:44:25 +00001028
1029 Selector S = ME->getSelector();
Ted Kremenek42ea0322008-05-05 23:55:01 +00001030
Ted Kremenek272aa852008-06-25 21:21:56 +00001031 // Look up a summary in our summary cache.
1032 ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S);
Ted Kremenek42ea0322008-05-05 23:55:01 +00001033
Ted Kremenek97c1e0c2008-06-23 22:21:20 +00001034 if (I != ObjCMethodSummaries.end())
Ted Kremenek42ea0322008-05-05 23:55:01 +00001035 return I->second;
Ted Kremenek42ea0322008-05-05 23:55:01 +00001036
Ted Kremenek35920ed2009-01-07 00:39:56 +00001037 // "initXXX": pass-through for receiver.
Ted Kremenek42ea0322008-05-05 23:55:01 +00001038 const char* s = S.getIdentifierInfoForSlot(0)->getName();
Ted Kremenek48b6d9e2008-05-07 03:45:05 +00001039 assert (ScratchArgs.empty());
Ted Kremenek1d3d9562008-05-06 06:09:09 +00001040
Ted Kremenek4395b452009-02-21 05:13:43 +00001041 if (deriveNamingConvention(s) == InitRule)
Ted Kremenek35920ed2009-01-07 00:39:56 +00001042 return getInitMethodSummary(ME);
Ted Kremenekbcaff792008-05-06 15:44:25 +00001043
Ted Kremenek35920ed2009-01-07 00:39:56 +00001044 // Look for methods that return an owned object.
1045 if (!isTrackedObjectType(Ctx.getCanonicalType(ME->getType())))
Ted Kremenek5496f6d2008-05-07 04:25:59 +00001046 return 0;
Ted Kremenek48b6d9e2008-05-07 03:45:05 +00001047
Ted Kremenek35920ed2009-01-07 00:39:56 +00001048 if (followsFundamentalRule(s)) {
1049 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremenek68621b92009-01-28 05:56:51 +00001050 : RetEffect::MakeOwned(RetEffect::ObjC, true);
Ted Kremenek48b6d9e2008-05-07 03:45:05 +00001051 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenek272aa852008-06-25 21:21:56 +00001052 ObjCMethodSummaries[ME] = Summ;
Ted Kremenekbcaff792008-05-06 15:44:25 +00001053 return Summ;
1054 }
Ted Kremenekbcaff792008-05-06 15:44:25 +00001055
Ted Kremenek42ea0322008-05-05 23:55:01 +00001056 return 0;
1057}
1058
Ted Kremeneka7722b72008-05-06 21:26:51 +00001059RetainSummary*
Ted Kremenek97c1e0c2008-06-23 22:21:20 +00001060RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
1061 Selector S) {
Ted Kremeneka7722b72008-05-06 21:26:51 +00001062
Ted Kremenek272aa852008-06-25 21:21:56 +00001063 // FIXME: Eventually we should properly do class method summaries, but
1064 // it requires us being able to walk the type hierarchy. Unfortunately,
1065 // we cannot do this with just an IdentifierInfo* for the class name.
1066
Ted Kremeneka7722b72008-05-06 21:26:51 +00001067 // Look up a summary in our cache of Selectors -> Summaries.
Ted Kremenek272aa852008-06-25 21:21:56 +00001068 ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S);
Ted Kremeneka7722b72008-05-06 21:26:51 +00001069
Ted Kremenek97c1e0c2008-06-23 22:21:20 +00001070 if (I != ObjCClassMethodSummaries.end())
Ted Kremeneka7722b72008-05-06 21:26:51 +00001071 return I->second;
1072
Ted Kremenek4c479322008-05-06 23:07:13 +00001073 return 0;
Ted Kremeneka7722b72008-05-06 21:26:51 +00001074}
1075
Ted Kremenek97c1e0c2008-06-23 22:21:20 +00001076void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek0e344d42008-05-06 00:30:21 +00001077
1078 assert (ScratchArgs.empty());
1079
Ted Kremenek6a1cc252008-06-23 18:02:52 +00001080 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremenek68621b92009-01-28 05:56:51 +00001081 : RetEffect::MakeOwned(RetEffect::ObjC, true);
Ted Kremenek6a1cc252008-06-23 18:02:52 +00001082
Ted Kremenek0e344d42008-05-06 00:30:21 +00001083 RetainSummary* Summ = getPersistentSummary(E);
1084
Ted Kremenek272aa852008-06-25 21:21:56 +00001085 // Create the summaries for "alloc", "new", and "allocWithZone:" for
1086 // NSObject and its derivatives.
1087 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
1088 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
1089 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Ted Kremenekf2717b02008-07-18 17:24:20 +00001090
1091 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00001092 addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"),
Ted Kremenek68621b92009-01-28 05:56:51 +00001093 GetNullarySelector("currentHandler", Ctx),
1094 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC)));
Ted Kremenekc8c8d2c2008-10-21 15:53:15 +00001095
1096 // Create the [NSAutoreleasePool addObject:] summary.
Ted Kremenek9b112d22009-01-28 21:44:40 +00001097 ScratchArgs.push_back(std::make_pair(0, Autorelease));
1098 addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"),
1099 GetUnarySelector("addObject", Ctx),
1100 getPersistentSummary(RetEffect::MakeNoRet(),
Ted Kremenekf21cb242009-02-23 02:31:16 +00001101 DoNothing, Autorelease));
Ted Kremenek0e344d42008-05-06 00:30:21 +00001102}
1103
Ted Kremenek97c1e0c2008-06-23 22:21:20 +00001104void RetainSummaryManager::InitializeMethodSummaries() {
Ted Kremenek83b2cde2008-05-06 00:38:54 +00001105
1106 assert (ScratchArgs.empty());
1107
Ted Kremeneka7722b72008-05-06 21:26:51 +00001108 // Create the "init" selector. It just acts as a pass-through for the
1109 // receiver.
Ted Kremenek56c70aa2009-02-23 16:54:00 +00001110 RetainSummary* InitSumm =
1111 getPersistentSummary(RetEffect::MakeReceiverAlias());
Ted Kremeneke44927e2008-07-01 17:21:27 +00001112 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
Ted Kremeneka7722b72008-05-06 21:26:51 +00001113
1114 // The next methods are allocators.
Ted Kremenek6a1cc252008-06-23 18:02:52 +00001115 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremenek68621b92009-01-28 05:56:51 +00001116 : RetEffect::MakeOwned(RetEffect::ObjC, true);
Ted Kremenek6a1cc252008-06-23 18:02:52 +00001117
Ted Kremeneke44927e2008-07-01 17:21:27 +00001118 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremeneka7722b72008-05-06 21:26:51 +00001119
1120 // Create the "copy" selector.
Ted Kremenek9449ca92008-08-12 20:41:56 +00001121 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ);
1122
Ted Kremenek83b2cde2008-05-06 00:38:54 +00001123 // Create the "mutableCopy" selector.
Ted Kremenek272aa852008-06-25 21:21:56 +00001124 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ);
Ted Kremenek9449ca92008-08-12 20:41:56 +00001125
Ted Kremenek266d8b62008-05-06 02:26:56 +00001126 // Create the "retain" selector.
1127 E = RetEffect::MakeReceiverAlias();
Ted Kremenek58dd95b2009-02-18 18:54:33 +00001128 Summ = getPersistentSummary(E, IncRefMsg);
Ted Kremenek272aa852008-06-25 21:21:56 +00001129 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Ted Kremenek266d8b62008-05-06 02:26:56 +00001130
1131 // Create the "release" selector.
Ted Kremenek58dd95b2009-02-18 18:54:33 +00001132 Summ = getPersistentSummary(E, DecRefMsg);
Ted Kremenek272aa852008-06-25 21:21:56 +00001133 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Ted Kremenekc00b32b2008-05-07 21:17:39 +00001134
1135 // Create the "drain" selector.
1136 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek272aa852008-06-25 21:21:56 +00001137 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Ted Kremenek266d8b62008-05-06 02:26:56 +00001138
1139 // Create the "autorelease" selector.
Ted Kremenek9b112d22009-01-28 21:44:40 +00001140 Summ = getPersistentSummary(E, Autorelease);
Ted Kremenek272aa852008-06-25 21:21:56 +00001141 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Ted Kremenek9449ca92008-08-12 20:41:56 +00001142
Ted Kremenekaac82832009-02-23 17:45:03 +00001143 // Specially handle NSAutoreleasePool.
Ted Kremenek1b4b6562009-02-25 02:54:57 +00001144 addInstMethSummary("NSAutoreleasePool", "init",
Ted Kremenekaac82832009-02-23 17:45:03 +00001145 getPersistentSummary(RetEffect::MakeReceiverAlias(),
Ted Kremenek1b4b6562009-02-25 02:54:57 +00001146 NewAutoreleasePool));
Ted Kremenekaac82832009-02-23 17:45:03 +00001147
Ted Kremenek45642a42008-08-12 18:48:50 +00001148 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek7e3a3272009-02-23 02:51:29 +00001149 // FIXME: For now we opt for false negatives with NSWindow, as these objects
1150 // self-own themselves. However, they only do this once they are displayed.
1151 // Thus, we need to track an NSWindow's display status.
1152 // This is tracked in <rdar://problem/6062711>.
Ted Kremeneke44927e2008-07-01 17:21:27 +00001153 RetainSummary *NSWindowSumm =
Ted Kremenek7e3a3272009-02-23 02:51:29 +00001154 getPersistentSummary(RetEffect::MakeReceiverAlias(), StopTracking);
Ted Kremenek45642a42008-08-12 18:48:50 +00001155
1156 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1157 "styleMask", "backing", "defer", NULL);
1158
1159 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1160 "styleMask", "backing", "defer", "screen", NULL);
1161
1162 // For NSPanel (which subclasses NSWindow), allocated objects are not
1163 // self-owned.
1164 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1165 "styleMask", "backing", "defer", NULL);
1166
1167 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1168 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek272aa852008-06-25 21:21:56 +00001169
Ted Kremenekf2717b02008-07-18 17:24:20 +00001170 // Create NSAssertionHandler summaries.
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00001171 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
1172 "lineNumber", "description", NULL);
Ted Kremenekf2717b02008-07-18 17:24:20 +00001173
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00001174 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1175 "file", "lineNumber", "description", NULL);
Ted Kremenek83b2cde2008-05-06 00:38:54 +00001176}
1177
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001178//===----------------------------------------------------------------------===//
Ted Kremenek7aef4842008-04-16 20:40:59 +00001179// Reference-counting logic (typestate + counts).
Ted Kremeneka7338b42008-03-11 06:39:11 +00001180//===----------------------------------------------------------------------===//
1181
Ted Kremeneka7338b42008-03-11 06:39:11 +00001182namespace {
1183
Ted Kremenek7d421f32008-04-09 23:49:11 +00001184class VISIBILITY_HIDDEN RefVal {
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001185public:
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001186 enum Kind {
1187 Owned = 0, // Owning reference.
1188 NotOwned, // Reference is not owned by still valid (not freed).
1189 Released, // Object has been released.
1190 ReturnedOwned, // Returned object passes ownership to caller.
1191 ReturnedNotOwned, // Return object does not pass ownership to caller.
1192 ErrorUseAfterRelease, // Object used after released.
1193 ErrorReleaseNotOwned, // Release of an object that was not owned.
Ted Kremenek311f3d42008-10-22 23:56:21 +00001194 ErrorLeak, // A memory leak due to excessive reference counts.
1195 ErrorLeakReturned // A memory leak due to the returning method not having
1196 // the correct naming conventions.
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001197 };
Ted Kremenek68621b92009-01-28 05:56:51 +00001198
1199private:
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001200 Kind kind;
Ted Kremenek68621b92009-01-28 05:56:51 +00001201 RetEffect::ObjKind okind;
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001202 unsigned Cnt;
Ted Kremenek272aa852008-06-25 21:21:56 +00001203 QualType T;
1204
Ted Kremenek68621b92009-01-28 05:56:51 +00001205 RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, QualType t)
1206 : kind(k), okind(o), Cnt(cnt), T(t) {}
Ted Kremenek0d721572008-03-11 17:48:22 +00001207
Ted Kremenek68621b92009-01-28 05:56:51 +00001208 RefVal(Kind k, unsigned cnt = 0)
1209 : kind(k), okind(RetEffect::AnyObj), Cnt(cnt) {}
1210
1211public:
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001212 Kind getKind() const { return kind; }
Ted Kremenek68621b92009-01-28 05:56:51 +00001213
1214 RetEffect::ObjKind getObjKind() const { return okind; }
Ted Kremenek0d721572008-03-11 17:48:22 +00001215
Ted Kremenek272aa852008-06-25 21:21:56 +00001216 unsigned getCount() const { return Cnt; }
1217 QualType getType() const { return T; }
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001218
1219 // Useful predicates.
Ted Kremenek0d721572008-03-11 17:48:22 +00001220
Ted Kremenek1daa16c2008-03-11 18:14:09 +00001221 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
1222
Ted Kremenek0106e202008-10-24 20:32:50 +00001223 static bool isLeak(Kind k) { return k >= ErrorLeak; }
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00001224
Ted Kremenekffefc352008-04-11 22:25:11 +00001225 bool isOwned() const {
1226 return getKind() == Owned;
1227 }
1228
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00001229 bool isNotOwned() const {
1230 return getKind() == NotOwned;
1231 }
1232
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001233 bool isReturnedOwned() const {
1234 return getKind() == ReturnedOwned;
1235 }
1236
1237 bool isReturnedNotOwned() const {
1238 return getKind() == ReturnedNotOwned;
1239 }
1240
1241 bool isNonLeakError() const {
1242 Kind k = getKind();
1243 return isError(k) && !isLeak(k);
1244 }
1245
1246 // State creation: normal state.
1247
Ted Kremenek68621b92009-01-28 05:56:51 +00001248 static RefVal makeOwned(RetEffect::ObjKind o, QualType t,
1249 unsigned Count = 1) {
1250 return RefVal(Owned, o, Count, t);
Ted Kremenekc4f81022008-04-10 23:09:18 +00001251 }
1252
Ted Kremenek68621b92009-01-28 05:56:51 +00001253 static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t,
1254 unsigned Count = 0) {
1255 return RefVal(NotOwned, o, Count, t);
Ted Kremenekc4f81022008-04-10 23:09:18 +00001256 }
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001257
1258 static RefVal makeReturnedOwned(unsigned Count) {
1259 return RefVal(ReturnedOwned, Count);
1260 }
1261
1262 static RefVal makeReturnedNotOwned() {
1263 return RefVal(ReturnedNotOwned);
1264 }
1265
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001266 // Comparison, profiling, and pretty-printing.
Ted Kremenek0d721572008-03-11 17:48:22 +00001267
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001268 bool operator==(const RefVal& X) const {
Ted Kremenek272aa852008-06-25 21:21:56 +00001269 return kind == X.kind && Cnt == X.Cnt && T == X.T;
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001270 }
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001271
Ted Kremenek272aa852008-06-25 21:21:56 +00001272 RefVal operator-(size_t i) const {
Ted Kremenek68621b92009-01-28 05:56:51 +00001273 return RefVal(getKind(), getObjKind(), getCount() - i, getType());
Ted Kremenek272aa852008-06-25 21:21:56 +00001274 }
1275
1276 RefVal operator+(size_t i) const {
Ted Kremenek68621b92009-01-28 05:56:51 +00001277 return RefVal(getKind(), getObjKind(), getCount() + i, getType());
Ted Kremenek272aa852008-06-25 21:21:56 +00001278 }
1279
1280 RefVal operator^(Kind k) const {
Ted Kremenek68621b92009-01-28 05:56:51 +00001281 return RefVal(k, getObjKind(), getCount(), getType());
Ted Kremenek272aa852008-06-25 21:21:56 +00001282 }
Ted Kremenek272aa852008-06-25 21:21:56 +00001283
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001284 void Profile(llvm::FoldingSetNodeID& ID) const {
1285 ID.AddInteger((unsigned) kind);
1286 ID.AddInteger(Cnt);
Ted Kremenek272aa852008-06-25 21:21:56 +00001287 ID.Add(T);
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001288 }
1289
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001290 void print(std::ostream& Out) const;
Ted Kremenek0d721572008-03-11 17:48:22 +00001291};
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001292
1293void RefVal::print(std::ostream& Out) const {
Ted Kremenek272aa852008-06-25 21:21:56 +00001294 if (!T.isNull())
1295 Out << "Tracked Type:" << T.getAsString() << '\n';
1296
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001297 switch (getKind()) {
1298 default: assert(false);
Ted Kremenekc4f81022008-04-10 23:09:18 +00001299 case Owned: {
1300 Out << "Owned";
1301 unsigned cnt = getCount();
1302 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001303 break;
Ted Kremenekc4f81022008-04-10 23:09:18 +00001304 }
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001305
Ted Kremenekc4f81022008-04-10 23:09:18 +00001306 case NotOwned: {
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001307 Out << "NotOwned";
Ted Kremenekc4f81022008-04-10 23:09:18 +00001308 unsigned cnt = getCount();
1309 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001310 break;
Ted Kremenekc4f81022008-04-10 23:09:18 +00001311 }
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001312
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001313 case ReturnedOwned: {
1314 Out << "ReturnedOwned";
1315 unsigned cnt = getCount();
1316 if (cnt) Out << " (+ " << cnt << ")";
1317 break;
1318 }
1319
1320 case ReturnedNotOwned: {
1321 Out << "ReturnedNotOwned";
1322 unsigned cnt = getCount();
1323 if (cnt) Out << " (+ " << cnt << ")";
1324 break;
1325 }
1326
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001327 case Released:
1328 Out << "Released";
1329 break;
1330
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00001331 case ErrorLeak:
1332 Out << "Leaked";
1333 break;
1334
Ted Kremenek311f3d42008-10-22 23:56:21 +00001335 case ErrorLeakReturned:
1336 Out << "Leaked (Bad naming)";
1337 break;
1338
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001339 case ErrorUseAfterRelease:
1340 Out << "Use-After-Release [ERROR]";
1341 break;
1342
1343 case ErrorReleaseNotOwned:
1344 Out << "Release of Not-Owned [ERROR]";
1345 break;
1346 }
1347}
Ted Kremenek0d721572008-03-11 17:48:22 +00001348
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001349} // end anonymous namespace
1350
1351//===----------------------------------------------------------------------===//
1352// RefBindings - State used to track object reference counts.
1353//===----------------------------------------------------------------------===//
1354
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00001355typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001356static int RefBIndex = 0;
Ted Kremenek876d8df2009-02-19 23:47:02 +00001357static std::pair<const void*, const void*> LeakProgramPointTag(&RefBIndex, 0);
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001358
1359namespace clang {
Ted Kremenek91781202008-08-17 03:20:02 +00001360 template<>
1361 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
1362 static inline void* GDMIndex() { return &RefBIndex; }
1363 };
1364}
Ted Kremenekc8c8d2c2008-10-21 15:53:15 +00001365
1366//===----------------------------------------------------------------------===//
Ted Kremenekb6578942009-02-24 19:15:11 +00001367// AutoreleaseBindings - State used to track objects in autorelease pools.
Ted Kremenekc8c8d2c2008-10-21 15:53:15 +00001368//===----------------------------------------------------------------------===//
1369
Ted Kremenekb6578942009-02-24 19:15:11 +00001370typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts;
1371typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents;
1372typedef llvm::ImmutableList<SymbolRef> ARStack;
Ted Kremenekaac82832009-02-23 17:45:03 +00001373
Ted Kremenekb6578942009-02-24 19:15:11 +00001374static int AutoRCIndex = 0;
Ted Kremenekc8c8d2c2008-10-21 15:53:15 +00001375static int AutoRBIndex = 0;
1376
Ted Kremenekb6578942009-02-24 19:15:11 +00001377namespace { class VISIBILITY_HIDDEN AutoreleasePoolContents {}; }
Ted Kremenek1b4b6562009-02-25 02:54:57 +00001378namespace { class VISIBILITY_HIDDEN AutoreleaseStack {}; }
Ted Kremenekb6578942009-02-24 19:15:11 +00001379
Ted Kremenekc8c8d2c2008-10-21 15:53:15 +00001380namespace clang {
Ted Kremenek1b4b6562009-02-25 02:54:57 +00001381template<> struct GRStateTrait<AutoreleaseStack>
Ted Kremenekb6578942009-02-24 19:15:11 +00001382 : public GRStatePartialTrait<ARStack> {
1383 static inline void* GDMIndex() { return &AutoRBIndex; }
1384};
1385
1386template<> struct GRStateTrait<AutoreleasePoolContents>
1387 : public GRStatePartialTrait<ARPoolContents> {
1388 static inline void* GDMIndex() { return &AutoRCIndex; }
1389};
1390} // end clang namespace
Ted Kremenekc8c8d2c2008-10-21 15:53:15 +00001391
Ted Kremenek7aef4842008-04-16 20:40:59 +00001392//===----------------------------------------------------------------------===//
1393// Transfer functions.
1394//===----------------------------------------------------------------------===//
1395
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001396namespace {
1397
Ted Kremenek7d421f32008-04-09 23:49:11 +00001398class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00001399public:
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00001400 class BindingsPrinter : public GRState::Printer {
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001401 public:
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00001402 virtual void Print(std::ostream& Out, const GRState* state,
1403 const char* nl, const char* sep);
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001404 };
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00001405
1406private:
Ted Kremenekc26c4692009-02-18 03:48:14 +00001407 typedef llvm::DenseMap<const GRExprEngine::NodeTy*, const RetainSummary*>
1408 SummaryLogTy;
1409
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001410 RetainSummaryManager Summaries;
Ted Kremenekc26c4692009-02-18 03:48:14 +00001411 SummaryLogTy SummaryLog;
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001412 const LangOptions& LOpts;
Ted Kremenekb6578942009-02-24 19:15:11 +00001413 ARCounts::Factory ARCountFactory;
Ted Kremenek91781202008-08-17 03:20:02 +00001414
Ted Kremenek708af042009-02-05 06:50:21 +00001415 BugType *useAfterRelease, *releaseNotOwned;
1416 BugType *leakWithinFunction, *leakAtReturn;
1417 BugReporter *BR;
Ted Kremeneka7338b42008-03-11 06:39:11 +00001418
Ted Kremenekb6578942009-02-24 19:15:11 +00001419 GRStateRef Update(GRStateRef state, SymbolRef sym, RefVal V, ArgEffect E,
1420 RefVal::Kind& hasErr);
1421
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001422 void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1423 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00001424 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001425 ExplodedNode<GRState>* Pred,
1426 const GRState* St,
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00001427 RefVal::Kind hasErr, SymbolRef Sym);
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00001428
Ted Kremenek0106e202008-10-24 20:32:50 +00001429 std::pair<GRStateRef, bool>
1430 HandleSymbolDeath(GRStateManager& VMgr, const GRState* St,
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00001431 const Decl* CD, SymbolRef sid, RefVal V, bool& hasLeak);
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00001432
Ted Kremenekb6578942009-02-24 19:15:11 +00001433public:
Ted Kremenek9f20c7c2008-07-22 16:21:24 +00001434 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek9b0c09c2008-04-29 05:33:51 +00001435 : Summaries(Ctx, gcenabled),
Ted Kremenek708af042009-02-05 06:50:21 +00001436 LOpts(lopts), useAfterRelease(0), releaseNotOwned(0),
1437 leakWithinFunction(0), leakAtReturn(0), BR(0) {}
Ted Kremenek1feab292008-04-16 04:28:53 +00001438
Ted Kremenek708af042009-02-05 06:50:21 +00001439 virtual ~CFRefCount() {}
Ted Kremenek7d421f32008-04-09 23:49:11 +00001440
Ted Kremenekbf6babf2009-02-04 23:49:09 +00001441 void RegisterChecks(BugReporter &BR);
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001442
Ted Kremenekb0f2b9e2008-08-16 00:49:49 +00001443 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1444 Printers.push_back(new BindingsPrinter());
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001445 }
Ted Kremeneka7338b42008-03-11 06:39:11 +00001446
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001447 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenekfe30beb2008-04-30 23:47:44 +00001448 const LangOptions& getLangOptions() const { return LOpts; }
1449
Ted Kremenekc26c4692009-02-18 03:48:14 +00001450 const RetainSummary *getSummaryOfNode(const ExplodedNode<GRState> *N) const {
1451 SummaryLogTy::const_iterator I = SummaryLog.find(N);
1452 return I == SummaryLog.end() ? 0 : I->second;
1453 }
1454
Ted Kremeneka7338b42008-03-11 06:39:11 +00001455 // Calls.
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001456
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001457 void EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001458 GRExprEngine& Eng,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001459 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001460 Expr* Ex,
1461 Expr* Receiver,
1462 RetainSummary* Summ,
Ted Kremenek2719e982008-06-17 02:43:46 +00001463 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001464 ExplodedNode<GRState>* Pred);
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001465
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001466 virtual void EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekce0767f2008-03-12 21:06:49 +00001467 GRExprEngine& Eng,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001468 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu097fc982008-10-17 05:57:07 +00001469 CallExpr* CE, SVal L,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001470 ExplodedNode<GRState>* Pred);
Ted Kremenek10fe66d2008-04-09 01:10:13 +00001471
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001472
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001473 virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4b4738b2008-04-15 23:44:31 +00001474 GRExprEngine& Engine,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001475 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4b4738b2008-04-15 23:44:31 +00001476 ObjCMessageExpr* ME,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001477 ExplodedNode<GRState>* Pred);
Ted Kremenek4b4738b2008-04-15 23:44:31 +00001478
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001479 bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4b4738b2008-04-15 23:44:31 +00001480 GRExprEngine& Engine,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001481 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4b4738b2008-04-15 23:44:31 +00001482 ObjCMessageExpr* ME,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001483 ExplodedNode<GRState>* Pred);
Ted Kremenek4b4738b2008-04-15 23:44:31 +00001484
Ted Kremeneka42be302009-02-14 01:43:44 +00001485 // Stores.
1486 virtual void EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val);
1487
Ted Kremenekffefc352008-04-11 22:25:11 +00001488 // End-of-path.
1489
1490 virtual void EvalEndPath(GRExprEngine& Engine,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001491 GREndPathNodeBuilder<GRState>& Builder);
Ted Kremenekffefc352008-04-11 22:25:11 +00001492
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001493 virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek541db372008-04-24 23:57:27 +00001494 GRExprEngine& Engine,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001495 GRStmtNodeBuilder<GRState>& Builder,
1496 ExplodedNode<GRState>* Pred,
Ted Kremenek5c0729b2009-01-21 22:26:05 +00001497 Stmt* S, const GRState* state,
1498 SymbolReaper& SymReaper);
1499
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001500 // Return statements.
1501
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001502 virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001503 GRExprEngine& Engine,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001504 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd9ccf682008-04-17 18:12:53 +00001505 ReturnStmt* S,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001506 ExplodedNode<GRState>* Pred);
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00001507
1508 // Assumptions.
1509
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001510 virtual const GRState* EvalAssume(GRStateManager& VMgr,
Zhongxing Xu097fc982008-10-17 05:57:07 +00001511 const GRState* St, SVal Cond,
Ted Kremenekf22f8682008-07-10 22:03:41 +00001512 bool Assumption, bool& isFeasible);
Ted Kremeneka7338b42008-03-11 06:39:11 +00001513};
1514
1515} // end anonymous namespace
1516
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00001517
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00001518void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state,
1519 const char* nl, const char* sep) {
1520
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001521 RefBindings B = state->get<RefBindings>();
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001522
Ted Kremenekbccfbcc2008-08-13 21:24:49 +00001523 if (!B.isEmpty())
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001524 Out << sep << nl;
1525
1526 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1527 Out << (*I).first << " : ";
1528 (*I).second.print(Out);
1529 Out << nl;
1530 }
Ted Kremenek1b4b6562009-02-25 02:54:57 +00001531
1532 // Print the autorelease stack.
1533 ARStack stack = state->get<AutoreleaseStack>();
1534 if (!stack.isEmpty()) {
1535 Out << sep << nl << "AR pool stack:";
1536
1537 for (ARStack::iterator I=stack.begin(), E=stack.end(); I!=E; ++I)
1538 Out << ' ' << (*I);
1539
1540 Out << nl;
1541 }
Ted Kremenek3b11f7a2008-03-11 19:44:10 +00001542}
1543
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001544static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) {
Ted Kremeneka3f30dd2008-05-22 17:31:13 +00001545 return Summ ? Summ->getArg(idx) : MayEscape;
Ted Kremenek455dd862008-04-11 20:23:24 +00001546}
1547
Ted Kremenek266d8b62008-05-06 02:26:56 +00001548static inline RetEffect GetRetEffect(RetainSummary* Summ) {
1549 return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
Ted Kremenek455dd862008-04-11 20:23:24 +00001550}
1551
Ted Kremenek227c5372008-05-06 02:41:27 +00001552static inline ArgEffect GetReceiverE(RetainSummary* Summ) {
1553 return Summ ? Summ->getReceiverEffect() : DoNothing;
1554}
1555
Ted Kremenekf2717b02008-07-18 17:24:20 +00001556static inline bool IsEndPath(RetainSummary* Summ) {
1557 return Summ ? Summ->isEndPath() : false;
1558}
1559
Ted Kremenek1feab292008-04-16 04:28:53 +00001560
Ted Kremenek272aa852008-06-25 21:21:56 +00001561/// GetReturnType - Used to get the return type of a message expression or
1562/// function call with the intention of affixing that type to a tracked symbol.
1563/// While the the return type can be queried directly from RetEx, when
1564/// invoking class methods we augment to the return type to be that of
1565/// a pointer to the class (as opposed it just being id).
1566static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) {
1567
1568 QualType RetTy = RetE->getType();
1569
1570 // FIXME: We aren't handling id<...>.
Chris Lattnerb724ab22008-07-26 22:36:27 +00001571 const PointerType* PT = RetTy->getAsPointerType();
Ted Kremenek272aa852008-06-25 21:21:56 +00001572 if (!PT)
1573 return RetTy;
1574
1575 // If RetEx is not a message expression just return its type.
1576 // If RetEx is a message expression, return its types if it is something
1577 /// more specific than id.
1578
1579 ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE);
1580
Steve Naroff17c03822009-02-12 17:52:19 +00001581 if (!ME || !Ctx.isObjCIdStructType(PT->getPointeeType()))
Ted Kremenek272aa852008-06-25 21:21:56 +00001582 return RetTy;
1583
1584 ObjCInterfaceDecl* D = ME->getClassInfo().first;
1585
1586 // At this point we know the return type of the message expression is id.
1587 // If we have an ObjCInterceDecl, we know this is a call to a class method
1588 // whose type we can resolve. In such cases, promote the return type to
1589 // Class*.
1590 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
1591}
1592
1593
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001594void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001595 GRExprEngine& Eng,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001596 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001597 Expr* Ex,
1598 Expr* Receiver,
1599 RetainSummary* Summ,
Ted Kremenek2719e982008-06-17 02:43:46 +00001600 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001601 ExplodedNode<GRState>* Pred) {
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001602
Ted Kremeneka7338b42008-03-11 06:39:11 +00001603 // Get the state.
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001604 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Ted Kremenek0106e202008-10-24 20:32:50 +00001605 ASTContext& Ctx = Eng.getStateManager().getContext();
Ted Kremenek227c5372008-05-06 02:41:27 +00001606
1607 // Evaluate the effect of the arguments.
Ted Kremenek1feab292008-04-16 04:28:53 +00001608 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001609 unsigned idx = 0;
Ted Kremenek99b0ecb2008-04-11 18:40:51 +00001610 Expr* ErrorExpr = NULL;
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00001611 SymbolRef ErrorSym = 0;
Ted Kremenek99b0ecb2008-04-11 18:40:51 +00001612
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001613 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00001614 SVal V = state.GetSValAsScalarOrLoc(*I);
Ted Kremenek9577c1e2009-03-03 22:06:47 +00001615 SymbolRef Sym = V.getAsLocSymbol();
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00001616
Ted Kremenek9577c1e2009-03-03 22:06:47 +00001617 if (Sym.isValid())
Ted Kremenekb6578942009-02-24 19:15:11 +00001618 if (RefBindings::data_type* T = state.get<RefBindings>(Sym)) {
1619 state = Update(state, Sym, *T, GetArgE(Summ, idx), hasErr);
1620 if (hasErr) {
Ted Kremenek99b0ecb2008-04-11 18:40:51 +00001621 ErrorExpr = *I;
Ted Kremenek6064a362008-07-07 16:21:19 +00001622 ErrorSym = Sym;
Ted Kremenek99b0ecb2008-04-11 18:40:51 +00001623 break;
Ted Kremenek9577c1e2009-03-03 22:06:47 +00001624 }
1625 continue;
Ted Kremenekb6578942009-02-24 19:15:11 +00001626 }
Ted Kremenekede40b72008-07-09 18:11:16 +00001627
Ted Kremenek9577c1e2009-03-03 22:06:47 +00001628 if (isa<Loc>(V)) {
1629 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenekede40b72008-07-09 18:11:16 +00001630 if (GetArgE(Summ, idx) == DoNothingByRef)
1631 continue;
1632
1633 // Invalidate the value of the variable passed by reference.
Ted Kremenek852e3ca2008-07-03 23:26:32 +00001634
1635 // FIXME: Either this logic should also be replicated in GRSimpleVals
1636 // or should be pulled into a separate "constraint engine."
Ted Kremenekede40b72008-07-09 18:11:16 +00001637
Ted Kremenek852e3ca2008-07-03 23:26:32 +00001638 // FIXME: We can have collisions on the conjured symbol if the
1639 // expression *I also creates conjured symbols. We probably want
1640 // to identify conjured symbols by an expression pair: the enclosing
1641 // expression (the context) and the expression itself. This should
Ted Kremenekede40b72008-07-09 18:11:16 +00001642 // disambiguate conjured symbols.
Ted Kremenekb15eba42008-10-04 05:50:14 +00001643
Ted Kremenek38a4b4b2008-10-17 20:28:54 +00001644 const TypedRegion* R = dyn_cast<TypedRegion>(MR->getRegion());
Ted Kremenek58a26bf2008-12-17 19:42:34 +00001645
Ted Kremenek6a9b5352009-03-01 05:44:08 +00001646 // Blast through TypedViewRegions to get the original region type.
Ted Kremenek58a26bf2008-12-17 19:42:34 +00001647 while (R) {
Ted Kremenek6a9b5352009-03-01 05:44:08 +00001648 const TypedViewRegion* ATR = dyn_cast<TypedViewRegion>(R);
Ted Kremenek58a26bf2008-12-17 19:42:34 +00001649 if (!ATR) break;
1650 R = dyn_cast<TypedRegion>(ATR->getSuperRegion());
1651 }
1652
Ted Kremenek53b24182009-03-04 22:56:43 +00001653 if (R) {
Ted Kremenek618c6cd2008-12-18 23:34:57 +00001654 // Is the invalidated variable something that we were tracking?
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00001655 SymbolRef Sym = state.GetSValAsScalarOrLoc(R).getAsLocSymbol();
Ted Kremenek618c6cd2008-12-18 23:34:57 +00001656
Ted Kremenek53b24182009-03-04 22:56:43 +00001657 // Remove any existing reference-count binding.
1658 if (Sym.isValid()) state = state.remove<RefBindings>(Sym);
Ted Kremenekb15eba42008-10-04 05:50:14 +00001659
Ted Kremenek53b24182009-03-04 22:56:43 +00001660 if (R->isBoundable(Ctx)) {
1661 // Set the value of the variable to be a conjured symbol.
1662 unsigned Count = Builder.getCurrentBlockCount();
1663 QualType T = R->getRValueType(Ctx);
1664
1665 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
1666 SymbolRef NewSym =
1667 Eng.getSymbolManager().getConjuredSymbol(*I, T, Count);
1668
1669 state = state.BindLoc(Loc::MakeVal(R),
1670 Loc::IsLocType(T)
1671 ? cast<SVal>(loc::SymbolVal(NewSym))
1672 : cast<SVal>(nonloc::SymbolVal(NewSym)));
1673 }
1674 else if (const RecordType *RT = T->getAsStructureType()) {
1675 // Handle structs in a not so awesome way. Here we just
1676 // eagerly bind new symbols to the fields. In reality we
1677 // should have the store manager handle this. The idea is just
1678 // to prototype some basic functionality here. All of this logic
1679 // should one day soon just go away.
1680 const RecordDecl *RD = RT->getDecl()->getDefinition(Ctx);
1681
1682 // No record definition. There is nothing we can do.
1683 if (!RD)
1684 continue;
1685
1686 MemRegionManager &MRMgr = state.getManager().getRegionManager();
1687
1688 // Iterate through the fields and construct new symbols.
1689 for (RecordDecl::field_iterator FI=RD->field_begin(),
1690 FE=RD->field_end(); FI!=FE; ++FI) {
1691
1692 // For now just handle scalar fields.
1693 FieldDecl *FD = *FI;
1694 QualType FT = FD->getType();
1695
1696 if (Loc::IsLocType(FT) ||
1697 (FT->isIntegerType() && FT->isScalarType())) {
1698
1699 // Tag the symbol with the field decl so that we generate
1700 // a unique symbol.
1701 SymbolRef NewSym =
1702 Eng.getSymbolManager().getConjuredSymbol(*I, FT, Count, FD);
1703
1704 // Create a region.
1705 // FIXME: How do we handle 'typedefs' in TypeViewRegions?
1706 // e.g.:
1707 // typedef struct *s foo;
1708 //
1709 // ((foo) x)->f vs. x->f
1710 //
1711 // The cast will add a ViewTypeRegion. Probably RegionStore
1712 // needs to reason about typedefs explicitly when binding
1713 // fields and elements.
1714 //
1715 const FieldRegion* FR = MRMgr.getFieldRegion(FD, R);
1716
1717 state = state.BindLoc(Loc::MakeVal(FR),
1718 Loc::IsLocType(FT)
1719 ? cast<SVal>(loc::SymbolVal(NewSym))
1720 : cast<SVal>(nonloc::SymbolVal(NewSym)));
1721 }
1722 }
1723 }
1724 else {
1725 // Just blast away other values.
1726 state = state.BindLoc(*MR, UnknownVal());
1727 }
Ted Kremenek8f90e712008-10-17 22:23:12 +00001728 }
Ted Kremenekb15eba42008-10-04 05:50:14 +00001729 }
1730 else
Ted Kremenek09102db2008-11-12 19:22:09 +00001731 state = state.BindLoc(*MR, UnknownVal());
Ted Kremenek852e3ca2008-07-03 23:26:32 +00001732 }
1733 else {
1734 // Nuke all other arguments passed by reference.
Zhongxing Xu097fc982008-10-17 05:57:07 +00001735 state = state.Unbind(cast<Loc>(V));
Ted Kremenek852e3ca2008-07-03 23:26:32 +00001736 }
Ted Kremeneke4924202008-04-11 20:51:02 +00001737 }
Zhongxing Xu097fc982008-10-17 05:57:07 +00001738 else if (isa<nonloc::LocAsInteger>(V))
1739 state = state.Unbind(cast<nonloc::LocAsInteger>(V).getLoc());
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001740 }
Ted Kremenek1feab292008-04-16 04:28:53 +00001741
Ted Kremenek272aa852008-06-25 21:21:56 +00001742 // Evaluate the effect on the message receiver.
Ted Kremenek227c5372008-05-06 02:41:27 +00001743 if (!ErrorExpr && Receiver) {
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00001744 SymbolRef Sym = state.GetSValAsScalarOrLoc(Receiver).getAsLocSymbol();
Ted Kremenek9577c1e2009-03-03 22:06:47 +00001745 if (Sym.isValid()) {
Ted Kremenekb6578942009-02-24 19:15:11 +00001746 if (const RefVal* T = state.get<RefBindings>(Sym)) {
1747 state = Update(state, Sym, *T, GetReceiverE(Summ), hasErr);
1748 if (hasErr) {
Ted Kremenek227c5372008-05-06 02:41:27 +00001749 ErrorExpr = Receiver;
Ted Kremenek6064a362008-07-07 16:21:19 +00001750 ErrorSym = Sym;
Ted Kremenek227c5372008-05-06 02:41:27 +00001751 }
Ted Kremenekb6578942009-02-24 19:15:11 +00001752 }
Ted Kremenek227c5372008-05-06 02:41:27 +00001753 }
1754 }
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001755
Ted Kremenek272aa852008-06-25 21:21:56 +00001756 // Process any errors.
Ted Kremenek1feab292008-04-16 04:28:53 +00001757 if (hasErr) {
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001758 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00001759 hasErr, ErrorSym);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001760 return;
Ted Kremenek0d721572008-03-11 17:48:22 +00001761 }
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001762
Ted Kremenekf2717b02008-07-18 17:24:20 +00001763 // Consult the summary for the return value.
Ted Kremenek266d8b62008-05-06 02:26:56 +00001764 RetEffect RE = GetRetEffect(Summ);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001765
1766 switch (RE.getKind()) {
1767 default:
1768 assert (false && "Unhandled RetEffect."); break;
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001769
Ted Kremenek8f90e712008-10-17 22:23:12 +00001770 case RetEffect::NoRet: {
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001771
Ted Kremenek455dd862008-04-11 20:23:24 +00001772 // Make up a symbol for the return value (not reference counted).
Ted Kremeneke4924202008-04-11 20:51:02 +00001773 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
1774 // should compose behavior, not copy it.
Ted Kremenek455dd862008-04-11 20:23:24 +00001775
Ted Kremenek8f90e712008-10-17 22:23:12 +00001776 // FIXME: We eventually should handle structs and other compound types
1777 // that are returned by value.
1778
1779 QualType T = Ex->getType();
1780
Ted Kremenek79413a52008-11-13 06:10:40 +00001781 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenek455dd862008-04-11 20:23:24 +00001782 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00001783 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek455dd862008-04-11 20:23:24 +00001784
Ted Kremenek802cfc72009-02-20 00:05:35 +00001785 SVal X = Loc::IsLocType(T)
Zhongxing Xu097fc982008-10-17 05:57:07 +00001786 ? cast<SVal>(loc::SymbolVal(Sym))
1787 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenek455dd862008-04-11 20:23:24 +00001788
Ted Kremenek09102db2008-11-12 19:22:09 +00001789 state = state.BindExpr(Ex, X, false);
Ted Kremenek455dd862008-04-11 20:23:24 +00001790 }
1791
Ted Kremenekab2fa2a2008-04-10 23:44:06 +00001792 break;
Ted Kremenek8f90e712008-10-17 22:23:12 +00001793 }
Ted Kremenekab2fa2a2008-04-10 23:44:06 +00001794
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001795 case RetEffect::Alias: {
Ted Kremenek272aa852008-06-25 21:21:56 +00001796 unsigned idx = RE.getIndex();
Ted Kremenek2719e982008-06-17 02:43:46 +00001797 assert (arg_end >= arg_beg);
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001798 assert (idx < (unsigned) (arg_end - arg_beg));
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00001799 SVal V = state.GetSValAsScalarOrLoc(*(arg_beg+idx));
Ted Kremenek09102db2008-11-12 19:22:09 +00001800 state = state.BindExpr(Ex, V, false);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001801 break;
1802 }
1803
Ted Kremenek227c5372008-05-06 02:41:27 +00001804 case RetEffect::ReceiverAlias: {
1805 assert (Receiver);
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00001806 SVal V = state.GetSValAsScalarOrLoc(Receiver);
Ted Kremenek09102db2008-11-12 19:22:09 +00001807 state = state.BindExpr(Ex, V, false);
Ted Kremenek227c5372008-05-06 02:41:27 +00001808 break;
1809 }
1810
Ted Kremenek6a1cc252008-06-23 18:02:52 +00001811 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001812 case RetEffect::OwnedSymbol: {
1813 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00001814 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek68621b92009-01-28 05:56:51 +00001815 QualType RetT = GetReturnType(Ex, Eng.getContext());
1816 state =
1817 state.set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(), RetT));
Ted Kremenek09102db2008-11-12 19:22:09 +00001818 state = state.BindExpr(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001819
Ted Kremenek6a1cc252008-06-23 18:02:52 +00001820 // FIXME: Add a flag to the checker where allocations are allowed to fail.
Ted Kremeneke62fd052009-01-28 22:27:59 +00001821 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) {
1822 bool isFeasible;
1823 state = state.Assume(loc::SymbolVal(Sym), true, isFeasible);
1824 assert(isFeasible && "Cannot assume fresh symbol is non-null.");
1825 }
Ted Kremenek6a1cc252008-06-23 18:02:52 +00001826
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001827 break;
1828 }
1829
1830 case RetEffect::NotOwnedSymbol: {
1831 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00001832 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek272aa852008-06-25 21:21:56 +00001833 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001834
Ted Kremenek68621b92009-01-28 05:56:51 +00001835 state =
1836 state.set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),RetT));
Ted Kremenek09102db2008-11-12 19:22:09 +00001837 state = state.BindExpr(Ex, loc::SymbolVal(Sym), false);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001838 break;
1839 }
1840 }
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001841
Ted Kremenek0dd65012009-02-18 02:00:25 +00001842 // Generate a sink node if we are at the end of a path.
1843 GRExprEngine::NodeTy *NewNode =
1844 IsEndPath(Summ) ? Builder.MakeSinkNode(Dst, Ex, Pred, state)
1845 : Builder.MakeNode(Dst, Ex, Pred, state);
1846
1847 // Annotate the edge with summary we used.
1848 // FIXME: This assumes that we always use the same summary when generating
1849 // this node.
1850 if (NewNode) SummaryLog[NewNode] = Summ;
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001851}
1852
1853
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001854void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001855 GRExprEngine& Eng,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001856 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu097fc982008-10-17 05:57:07 +00001857 CallExpr* CE, SVal L,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001858 ExplodedNode<GRState>* Pred) {
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001859
Zhongxing Xu097fc982008-10-17 05:57:07 +00001860 RetainSummary* Summ = !isa<loc::FuncVal>(L) ? 0
1861 : Summaries.getSummary(cast<loc::FuncVal>(L).getDecl());
Ted Kremeneka8c3c432008-05-05 22:11:16 +00001862
1863 EvalSummary(Dst, Eng, Builder, CE, 0, Summ,
1864 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenek827f93b2008-03-06 00:08:09 +00001865}
Ted Kremeneka7338b42008-03-11 06:39:11 +00001866
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001867void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4b4738b2008-04-15 23:44:31 +00001868 GRExprEngine& Eng,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001869 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4b4738b2008-04-15 23:44:31 +00001870 ObjCMessageExpr* ME,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001871 ExplodedNode<GRState>* Pred) {
Ted Kremenek926abf22008-05-06 04:20:12 +00001872 RetainSummary* Summ;
Ted Kremenek33661802008-05-01 21:31:50 +00001873
Ted Kremenek272aa852008-06-25 21:21:56 +00001874 if (Expr* Receiver = ME->getReceiver()) {
1875 // We need the type-information of the tracked receiver object
1876 // Retrieve it from the state.
1877 ObjCInterfaceDecl* ID = 0;
1878
1879 // FIXME: Wouldn't it be great if this code could be reduced? It's just
1880 // a chain of lookups.
Ted Kremenekabd89ac2008-08-13 04:27:00 +00001881 const GRState* St = Builder.GetState(Pred);
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00001882 SVal V = Eng.getStateManager().GetSValAsScalarOrLoc(St, Receiver);
Ted Kremenek272aa852008-06-25 21:21:56 +00001883
Ted Kremenek9577c1e2009-03-03 22:06:47 +00001884 SymbolRef Sym = V.getAsLocSymbol();
1885 if (Sym.isValid()) {
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001886 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Ted Kremenek6064a362008-07-07 16:21:19 +00001887 QualType Ty = T->getType();
Ted Kremenek272aa852008-06-25 21:21:56 +00001888
1889 if (const PointerType* PT = Ty->getAsPointerType()) {
1890 QualType PointeeTy = PT->getPointeeType();
1891
1892 if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy))
1893 ID = IT->getDecl();
1894 }
1895 }
1896 }
1897
1898 Summ = Summaries.getMethodSummary(ME, ID);
Ted Kremenek0106e202008-10-24 20:32:50 +00001899
Ted Kremenek63d09ae2008-10-23 01:56:15 +00001900 // Special-case: are we sending a mesage to "self"?
1901 // This is a hack. When we have full-IP this should be removed.
1902 if (!Summ) {
1903 ObjCMethodDecl* MD =
1904 dyn_cast<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl());
1905
1906 if (MD) {
1907 if (Expr* Receiver = ME->getReceiver()) {
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00001908 SVal X = Eng.getStateManager().GetSValAsScalarOrLoc(St, Receiver);
Ted Kremenek63d09ae2008-10-23 01:56:15 +00001909 if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X))
Ted Kremenek0106e202008-10-24 20:32:50 +00001910 if (L->getRegion() == Eng.getStateManager().getSelfRegion(St)) {
1911 // Create a summmary where all of the arguments "StopTracking".
1912 Summ = Summaries.getPersistentSummary(RetEffect::MakeNoRet(),
1913 DoNothing,
1914 StopTracking);
1915 }
Ted Kremenek63d09ae2008-10-23 01:56:15 +00001916 }
1917 }
1918 }
Ted Kremenek272aa852008-06-25 21:21:56 +00001919 }
Ted Kremenek1feab292008-04-16 04:28:53 +00001920 else
Ted Kremenek97c1e0c2008-06-23 22:21:20 +00001921 Summ = Summaries.getClassMethodSummary(ME->getClassName(),
1922 ME->getSelector());
Ted Kremenek1feab292008-04-16 04:28:53 +00001923
Ted Kremenek926abf22008-05-06 04:20:12 +00001924 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ,
1925 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek4b4738b2008-04-15 23:44:31 +00001926}
Ted Kremenek2ddb4b22009-02-14 03:16:10 +00001927
1928namespace {
1929class VISIBILITY_HIDDEN StopTrackingCallback : public SymbolVisitor {
1930 GRStateRef state;
1931public:
1932 StopTrackingCallback(GRStateRef st) : state(st) {}
1933 GRStateRef getState() { return state; }
1934
1935 bool VisitSymbol(SymbolRef sym) {
1936 state = state.remove<RefBindings>(sym);
1937 return true;
1938 }
Ted Kremenek926abf22008-05-06 04:20:12 +00001939
Ted Kremenek2ddb4b22009-02-14 03:16:10 +00001940 const GRState* getState() const { return state.getState(); }
1941};
1942} // end anonymous namespace
1943
1944
Ted Kremeneka42be302009-02-14 01:43:44 +00001945void CFRefCount::EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val) {
Ted Kremeneka42be302009-02-14 01:43:44 +00001946 // Are we storing to something that causes the value to "escape"?
Ted Kremenek7aef4842008-04-16 20:40:59 +00001947 bool escapes = false;
1948
Ted Kremenek28d7eef2008-10-18 03:49:51 +00001949 // A value escapes in three possible cases (this may change):
1950 //
1951 // (1) we are binding to something that is not a memory region.
1952 // (2) we are binding to a memregion that does not have stack storage
1953 // (3) we are binding to a memregion with stack storage that the store
Ted Kremeneka42be302009-02-14 01:43:44 +00001954 // does not understand.
Ted Kremeneka42be302009-02-14 01:43:44 +00001955 GRStateRef state = B.getState();
Ted Kremenek28d7eef2008-10-18 03:49:51 +00001956
Ted Kremeneka42be302009-02-14 01:43:44 +00001957 if (!isa<loc::MemRegionVal>(location))
Ted Kremenek7aef4842008-04-16 20:40:59 +00001958 escapes = true;
Ted Kremenekb15eba42008-10-04 05:50:14 +00001959 else {
Ted Kremeneka42be302009-02-14 01:43:44 +00001960 const MemRegion* R = cast<loc::MemRegionVal>(location).getRegion();
1961 escapes = !B.getStateManager().hasStackStorage(R);
Ted Kremenek28d7eef2008-10-18 03:49:51 +00001962
1963 if (!escapes) {
1964 // To test (3), generate a new state with the binding removed. If it is
1965 // the same state, then it escapes (since the store cannot represent
1966 // the binding).
Ted Kremeneka42be302009-02-14 01:43:44 +00001967 escapes = (state == (state.BindLoc(cast<Loc>(location), UnknownVal())));
Ted Kremenek28d7eef2008-10-18 03:49:51 +00001968 }
Ted Kremenekb15eba42008-10-04 05:50:14 +00001969 }
Ted Kremeneka42be302009-02-14 01:43:44 +00001970
Ted Kremenek2ddb4b22009-02-14 03:16:10 +00001971 // If our store can represent the binding and we aren't storing to something
1972 // that doesn't have local storage then just return and have the simulation
1973 // state continue as is.
1974 if (!escapes)
1975 return;
Ted Kremenek28d7eef2008-10-18 03:49:51 +00001976
Ted Kremenek2ddb4b22009-02-14 03:16:10 +00001977 // Otherwise, find all symbols referenced by 'val' that we are tracking
1978 // and stop tracking them.
1979 B.MakeNode(state.scanReachableSymbols<StopTrackingCallback>(val).getState());
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00001980}
1981
Ted Kremenek0106e202008-10-24 20:32:50 +00001982std::pair<GRStateRef,bool>
1983CFRefCount::HandleSymbolDeath(GRStateManager& VMgr,
1984 const GRState* St, const Decl* CD,
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00001985 SymbolRef sid,
Ted Kremenek0106e202008-10-24 20:32:50 +00001986 RefVal V, bool& hasLeak) {
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00001987
Ted Kremenek4ae925c2008-08-14 21:16:54 +00001988 GRStateRef state(St, VMgr);
Sanjiv Guptafa451432008-10-31 09:52:39 +00001989 assert ((!V.isReturnedOwned() || CD) &&
Ted Kremenek311f3d42008-10-22 23:56:21 +00001990 "CodeDecl must be available for reporting ReturnOwned errors.");
Ted Kremenek63d09ae2008-10-23 01:56:15 +00001991
Ted Kremenek311f3d42008-10-22 23:56:21 +00001992 if (V.isReturnedOwned() && V.getCount() == 0)
1993 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
Chris Lattner3a8f2942008-11-24 03:33:13 +00001994 std::string s = MD->getSelector().getAsString();
Ted Kremenekcdd3bb22008-11-05 16:54:44 +00001995 if (!followsReturnRule(s.c_str())) {
Ted Kremenek311f3d42008-10-22 23:56:21 +00001996 hasLeak = true;
Ted Kremenek0106e202008-10-24 20:32:50 +00001997 state = state.set<RefBindings>(sid, V ^ RefVal::ErrorLeakReturned);
1998 return std::make_pair(state, true);
Ted Kremenek311f3d42008-10-22 23:56:21 +00001999 }
2000 }
Ted Kremenek63d09ae2008-10-23 01:56:15 +00002001
Ted Kremenek311f3d42008-10-22 23:56:21 +00002002 // All other cases.
2003
2004 hasLeak = V.isOwned() ||
2005 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Ted Kremenek4ae925c2008-08-14 21:16:54 +00002006
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00002007 if (!hasLeak)
Ted Kremenek0106e202008-10-24 20:32:50 +00002008 return std::make_pair(state.remove<RefBindings>(sid), false);
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00002009
Ted Kremenek0106e202008-10-24 20:32:50 +00002010 return std::make_pair(state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak),
2011 false);
Ted Kremenek3f3c9c82008-04-16 22:32:20 +00002012}
2013
Ted Kremenek541db372008-04-24 23:57:27 +00002014
Ted Kremenekffefc352008-04-11 22:25:11 +00002015
Ted Kremenek541db372008-04-24 23:57:27 +00002016// Dead symbols.
2017
Ted Kremenek708af042009-02-05 06:50:21 +00002018
Ted Kremenek541db372008-04-24 23:57:27 +00002019
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002020 // Return statements.
2021
Ted Kremenekabd89ac2008-08-13 04:27:00 +00002022void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002023 GRExprEngine& Eng,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00002024 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002025 ReturnStmt* S,
Ted Kremenekabd89ac2008-08-13 04:27:00 +00002026 ExplodedNode<GRState>* Pred) {
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002027
2028 Expr* RetE = S->getRetValue();
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002029 if (!RetE)
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002030 return;
2031
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002032 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00002033 SymbolRef Sym = state.GetSValAsScalarOrLoc(RetE).getAsLocSymbol();
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002034
2035 if (!Sym.isValid())
2036 return;
2037
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002038 // Get the reference count binding (if any).
Ted Kremenek4ae925c2008-08-14 21:16:54 +00002039 const RefVal* T = state.get<RefBindings>(Sym);
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002040
2041 if (!T)
2042 return;
2043
Ted Kremenek4ae925c2008-08-14 21:16:54 +00002044 // Change the reference count.
Ted Kremenek6064a362008-07-07 16:21:19 +00002045 RefVal X = *T;
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002046
Ted Kremenek4ae925c2008-08-14 21:16:54 +00002047 switch (X.getKind()) {
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002048 case RefVal::Owned: {
2049 unsigned cnt = X.getCount();
Ted Kremeneka3f30dd2008-05-22 17:31:13 +00002050 assert (cnt > 0);
2051 X = RefVal::makeReturnedOwned(cnt - 1);
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002052 break;
2053 }
2054
2055 case RefVal::NotOwned: {
2056 unsigned cnt = X.getCount();
2057 X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
2058 : RefVal::makeReturnedNotOwned();
2059 break;
2060 }
2061
2062 default:
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002063 return;
2064 }
2065
2066 // Update the binding.
Ted Kremenek91781202008-08-17 03:20:02 +00002067 state = state.set<RefBindings>(Sym, X);
Ted Kremenek4ae925c2008-08-14 21:16:54 +00002068 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenekd9ccf682008-04-17 18:12:53 +00002069}
2070
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00002071// Assumptions.
2072
Ted Kremenekabd89ac2008-08-13 04:27:00 +00002073const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr,
2074 const GRState* St,
Zhongxing Xu097fc982008-10-17 05:57:07 +00002075 SVal Cond, bool Assumption,
Ted Kremenekf22f8682008-07-10 22:03:41 +00002076 bool& isFeasible) {
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00002077
2078 // FIXME: We may add to the interface of EvalAssume the list of symbols
2079 // whose assumptions have changed. For now we just iterate through the
2080 // bindings and check if any of the tracked symbols are NULL. This isn't
2081 // too bad since the number of symbols we will track in practice are
2082 // probably small and EvalAssume is only called at branches and a few
2083 // other places.
Ted Kremenek4ae925c2008-08-14 21:16:54 +00002084 RefBindings B = St->get<RefBindings>();
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00002085
2086 if (B.isEmpty())
2087 return St;
2088
2089 bool changed = false;
Ted Kremenek91781202008-08-17 03:20:02 +00002090
2091 GRStateRef state(St, VMgr);
2092 RefBindings::Factory& RefBFactory = state.get_context<RefBindings>();
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00002093
2094 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00002095 // Check if the symbol is null (or equal to any constant).
2096 // If this is the case, stop tracking the symbol.
Zhongxing Xuc6b27d02008-08-29 14:52:36 +00002097 if (VMgr.getSymVal(St, I.getKey())) {
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00002098 changed = true;
2099 B = RefBFactory.Remove(B, I.getKey());
2100 }
2101 }
2102
Ted Kremenek91781202008-08-17 03:20:02 +00002103 if (changed)
2104 state = state.set<RefBindings>(B);
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00002105
Ted Kremenek4ae925c2008-08-14 21:16:54 +00002106 return state;
Ted Kremenekeef8f1e2008-04-18 19:23:43 +00002107}
Ted Kremeneka7338b42008-03-11 06:39:11 +00002108
Ted Kremenekb6578942009-02-24 19:15:11 +00002109GRStateRef CFRefCount::Update(GRStateRef state, SymbolRef sym,
2110 RefVal V, ArgEffect E,
2111 RefVal::Kind& hasErr) {
Ted Kremenek58dd95b2009-02-18 18:54:33 +00002112
2113 // In GC mode [... release] and [... retain] do nothing.
2114 switch (E) {
2115 default: break;
2116 case IncRefMsg: E = isGCEnabled() ? DoNothing : IncRef; break;
2117 case DecRefMsg: E = isGCEnabled() ? DoNothing : DecRef; break;
Ted Kremenek2126bef2009-02-18 21:57:45 +00002118 case MakeCollectable: E = isGCEnabled() ? DecRef : DoNothing; break;
Ted Kremenekaac82832009-02-23 17:45:03 +00002119 case NewAutoreleasePool: E = isGCEnabled() ? DoNothing :
2120 NewAutoreleasePool; break;
Ted Kremenek58dd95b2009-02-18 18:54:33 +00002121 }
Ted Kremeneka7338b42008-03-11 06:39:11 +00002122
Ted Kremenek0d721572008-03-11 17:48:22 +00002123 switch (E) {
2124 default:
2125 assert (false && "Unhandled CFRef transition.");
Ted Kremeneka3f30dd2008-05-22 17:31:13 +00002126
Ted Kremenekb7826ab2009-02-25 23:11:49 +00002127 case NewAutoreleasePool:
2128 assert(!isGCEnabled());
2129 return state.add<AutoreleaseStack>(sym);
2130
Ted Kremeneka3f30dd2008-05-22 17:31:13 +00002131 case MayEscape:
2132 if (V.getKind() == RefVal::Owned) {
Ted Kremenek272aa852008-06-25 21:21:56 +00002133 V = V ^ RefVal::NotOwned;
Ted Kremeneka3f30dd2008-05-22 17:31:13 +00002134 break;
2135 }
Ted Kremeneka3f30dd2008-05-22 17:31:13 +00002136 // Fall-through.
Ted Kremenek1b4b6562009-02-25 02:54:57 +00002137
Ted Kremenekede40b72008-07-09 18:11:16 +00002138 case DoNothingByRef:
Ted Kremenek0d721572008-03-11 17:48:22 +00002139 case DoNothing:
Ted Kremeneka8c3c432008-05-05 22:11:16 +00002140 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
Ted Kremenek272aa852008-06-25 21:21:56 +00002141 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek1feab292008-04-16 04:28:53 +00002142 hasErr = V.getKind();
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00002143 break;
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00002144 }
Ted Kremenekb6578942009-02-24 19:15:11 +00002145 return state;
Ted Kremeneke5a4bb02008-06-30 16:57:41 +00002146
Ted Kremenek9b112d22009-01-28 21:44:40 +00002147 case Autorelease:
Ted Kremenekb6578942009-02-24 19:15:11 +00002148 if (isGCEnabled()) return state;
Ted Kremenek9b112d22009-01-28 21:44:40 +00002149 // Fall-through.
Ted Kremenek227c5372008-05-06 02:41:27 +00002150 case StopTracking:
Ted Kremenekb6578942009-02-24 19:15:11 +00002151 return state.remove<RefBindings>(sym);
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00002152
Ted Kremenek0d721572008-03-11 17:48:22 +00002153 case IncRef:
2154 switch (V.getKind()) {
2155 default:
2156 assert(false);
2157
2158 case RefVal::Owned:
Ted Kremenek0d721572008-03-11 17:48:22 +00002159 case RefVal::NotOwned:
Ted Kremenek272aa852008-06-25 21:21:56 +00002160 V = V + 1;
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00002161 break;
Ted Kremenek0d721572008-03-11 17:48:22 +00002162 case RefVal::Released:
Ted Kremeneka8c3c432008-05-05 22:11:16 +00002163 if (isGCEnabled())
Ted Kremenekb7d9c9e2009-02-18 22:57:22 +00002164 V = (V ^ RefVal::Owned) + 1;
Ted Kremeneke2dd9572008-04-29 05:44:10 +00002165 else {
Ted Kremenek272aa852008-06-25 21:21:56 +00002166 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremeneke2dd9572008-04-29 05:44:10 +00002167 hasErr = V.getKind();
2168 }
Ted Kremenek0d721572008-03-11 17:48:22 +00002169 break;
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00002170 }
Ted Kremenekab2fa2a2008-04-10 23:44:06 +00002171 break;
2172
Ted Kremenek272aa852008-06-25 21:21:56 +00002173 case SelfOwn:
2174 V = V ^ RefVal::NotOwned;
Ted Kremenek58dd95b2009-02-18 18:54:33 +00002175 // Fall-through.
Ted Kremenek0d721572008-03-11 17:48:22 +00002176 case DecRef:
2177 switch (V.getKind()) {
2178 default:
2179 assert (false);
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00002180
Ted Kremenek272aa852008-06-25 21:21:56 +00002181 case RefVal::Owned:
Ted Kremenekb7d9c9e2009-02-18 22:57:22 +00002182 assert(V.getCount() > 0);
2183 if (V.getCount() == 1) V = V ^ RefVal::Released;
2184 V = V - 1;
Ted Kremenek0d721572008-03-11 17:48:22 +00002185 break;
Ted Kremenek0d721572008-03-11 17:48:22 +00002186
Ted Kremenek272aa852008-06-25 21:21:56 +00002187 case RefVal::NotOwned:
2188 if (V.getCount() > 0)
2189 V = V - 1;
Ted Kremenekc4f81022008-04-10 23:09:18 +00002190 else {
Ted Kremenek272aa852008-06-25 21:21:56 +00002191 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek1feab292008-04-16 04:28:53 +00002192 hasErr = V.getKind();
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00002193 }
Ted Kremenek0d721572008-03-11 17:48:22 +00002194 break;
Ted Kremenek0d721572008-03-11 17:48:22 +00002195
2196 case RefVal::Released:
Ted Kremenek272aa852008-06-25 21:21:56 +00002197 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek1feab292008-04-16 04:28:53 +00002198 hasErr = V.getKind();
Ted Kremenek0d721572008-03-11 17:48:22 +00002199 break;
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00002200 }
Ted Kremenekab2fa2a2008-04-10 23:44:06 +00002201 break;
Ted Kremenek0d721572008-03-11 17:48:22 +00002202 }
Ted Kremenekb6578942009-02-24 19:15:11 +00002203 return state.set<RefBindings>(sym, V);
Ted Kremeneka7338b42008-03-11 06:39:11 +00002204}
2205
Ted Kremenek10fe66d2008-04-09 01:10:13 +00002206//===----------------------------------------------------------------------===//
Ted Kremenek7d421f32008-04-09 23:49:11 +00002207// Error reporting.
Ted Kremenek10fe66d2008-04-09 01:10:13 +00002208//===----------------------------------------------------------------------===//
2209
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002210namespace {
2211
2212 //===-------------===//
2213 // Bug Descriptions. //
2214 //===-------------===//
2215
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002216 class VISIBILITY_HIDDEN CFRefBug : public BugType {
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002217 protected:
2218 CFRefCount& TF;
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002219
2220 CFRefBug(CFRefCount* tf, const char* name)
2221 : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {}
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002222 public:
Ted Kremenekfe30beb2008-04-30 23:47:44 +00002223
Ted Kremenek5c3407a2008-05-01 22:50:36 +00002224 CFRefCount& getTF() { return TF; }
Ted Kremenek0ff3f202008-05-05 23:16:31 +00002225 const CFRefCount& getTF() const { return TF; }
2226
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002227 // FIXME: Eventually remove.
2228 virtual const char* getDescription() const = 0;
2229
Ted Kremenekfe4d2312008-05-01 23:13:35 +00002230 virtual bool isLeak() const { return false; }
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002231 };
2232
2233 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2234 public:
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002235 UseAfterRelease(CFRefCount* tf)
2236 : CFRefBug(tf, "use-after-release") {}
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002237
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002238 const char* getDescription() const {
Ted Kremenekef9d0a82009-02-26 21:04:07 +00002239 return "Reference-counted object is used after it is released";
Ted Kremenek708af042009-02-05 06:50:21 +00002240 }
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002241 };
2242
2243 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2244 public:
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002245 BadRelease(CFRefCount* tf) : CFRefBug(tf, "bad release") {}
2246
2247 const char* getDescription() const {
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002248 return "Incorrect decrement of the reference count of a "
Ted Kremenekef9d0a82009-02-26 21:04:07 +00002249 "Core Foundation object ("
2250 "the object is not owned at this point by the caller)";
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002251 }
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002252 };
2253
2254 class VISIBILITY_HIDDEN Leak : public CFRefBug {
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002255 const bool isReturn;
2256 protected:
2257 Leak(CFRefCount* tf, const char* name, bool isRet)
2258 : CFRefBug(tf, name), isReturn(isRet) {}
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002259 public:
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002260
Ted Kremenek44274e62009-02-07 22:38:00 +00002261 const char* getDescription() const { return ""; }
Ted Kremenek3f6c6802009-01-24 00:55:43 +00002262
Ted Kremenek538a3ba2009-02-05 00:38:00 +00002263 bool isLeak() const { return true; }
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002264 };
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002265
2266 class VISIBILITY_HIDDEN LeakAtReturn : public Leak {
2267 public:
2268 LeakAtReturn(CFRefCount* tf, const char* name)
2269 : Leak(tf, name, true) {}
2270 };
2271
2272 class VISIBILITY_HIDDEN LeakWithinFunction : public Leak {
2273 public:
2274 LeakWithinFunction(CFRefCount* tf, const char* name)
2275 : Leak(tf, name, false) {}
2276 };
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002277
2278 //===---------===//
2279 // Bug Reports. //
2280 //===---------===//
2281
2282 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
Ted Kremenek8ff05042009-02-07 22:04:05 +00002283 protected:
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00002284 SymbolRef Sym;
Ted Kremenekc26c4692009-02-18 03:48:14 +00002285 const CFRefCount &TF;
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002286 public:
Ted Kremenekc26c4692009-02-18 03:48:14 +00002287 CFRefReport(CFRefBug& D, const CFRefCount &tf,
2288 ExplodedNode<GRState> *n, SymbolRef sym)
2289 : RangedBugReport(D, D.getDescription(), n), Sym(sym), TF(tf) {}
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002290
2291 virtual ~CFRefReport() {}
2292
Ted Kremenek5c3407a2008-05-01 22:50:36 +00002293 CFRefBug& getBugType() {
2294 return (CFRefBug&) RangedBugReport::getBugType();
2295 }
2296 const CFRefBug& getBugType() const {
2297 return (const CFRefBug&) RangedBugReport::getBugType();
2298 }
2299
2300 virtual void getRanges(BugReporter& BR, const SourceRange*& beg,
2301 const SourceRange*& end) {
2302
Ted Kremenek198cae02008-05-02 20:53:50 +00002303 if (!getBugType().isLeak())
Ted Kremenek5c3407a2008-05-01 22:50:36 +00002304 RangedBugReport::getRanges(BR, beg, end);
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00002305 else
2306 beg = end = 0;
Ted Kremenek5c3407a2008-05-01 22:50:36 +00002307 }
2308
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00002309 SymbolRef getSymbol() const { return Sym; }
Ted Kremenekd7e26782008-05-16 18:33:44 +00002310
Ted Kremenek3f6c6802009-01-24 00:55:43 +00002311 PathDiagnosticPiece* getEndPath(BugReporter& BR,
2312 const ExplodedNode<GRState>* N);
Ted Kremenekfe4d2312008-05-01 23:13:35 +00002313
Ted Kremenek3f6c6802009-01-24 00:55:43 +00002314 std::pair<const char**,const char**> getExtraDescriptiveText();
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002315
Ted Kremenek3f6c6802009-01-24 00:55:43 +00002316 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState>* N,
2317 const ExplodedNode<GRState>* PrevN,
2318 const ExplodedGraph<GRState>& G,
Ted Kremenekc26c4692009-02-18 03:48:14 +00002319 BugReporter& BR,
2320 NodeResolver& NR);
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002321 };
2322
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002323 class VISIBILITY_HIDDEN CFRefLeakReport : public CFRefReport {
Ted Kremenek86617f42009-02-07 22:19:59 +00002324 SourceLocation AllocSite;
2325 const MemRegion* AllocBinding;
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002326 public:
Ted Kremenekc26c4692009-02-18 03:48:14 +00002327 CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
2328 ExplodedNode<GRState> *n, SymbolRef sym,
Ted Kremenek44274e62009-02-07 22:38:00 +00002329 GRExprEngine& Eng);
Ted Kremenek8ff05042009-02-07 22:04:05 +00002330
2331 PathDiagnosticPiece* getEndPath(BugReporter& BR,
2332 const ExplodedNode<GRState>* N);
2333
Ted Kremenek86617f42009-02-07 22:19:59 +00002334 SourceLocation getLocation() const { return AllocSite; }
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002335 };
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002336} // end anonymous namespace
2337
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002338void CFRefCount::RegisterChecks(BugReporter& BR) {
Ted Kremenek708af042009-02-05 06:50:21 +00002339 useAfterRelease = new UseAfterRelease(this);
2340 BR.Register(useAfterRelease);
2341
2342 releaseNotOwned = new BadRelease(this);
2343 BR.Register(releaseNotOwned);
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002344
2345 // First register "return" leaks.
2346 const char* name = 0;
2347
2348 if (isGCEnabled())
2349 name = "[naming convention] leak of returned object (GC)";
2350 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
2351 name = "[naming convention] leak of returned object (hybrid MM, "
2352 "non-GC)";
2353 else {
2354 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
2355 name = "[naming convention] leak of returned object";
2356 }
2357
Ted Kremenek708af042009-02-05 06:50:21 +00002358 leakAtReturn = new LeakAtReturn(this, name);
2359 BR.Register(leakAtReturn);
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002360
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002361 // Second, register leaks within a function/method.
2362 if (isGCEnabled())
2363 name = "leak (GC)";
2364 else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
2365 name = "leak (hybrid MM, non-GC)";
2366 else {
2367 assert(getLangOptions().getGCMode() == LangOptions::NonGC);
2368 name = "leak";
2369 }
2370
Ted Kremenek708af042009-02-05 06:50:21 +00002371 leakWithinFunction = new LeakWithinFunction(this, name);
2372 BR.Register(leakWithinFunction);
2373
2374 // Save the reference to the BugReporter.
2375 this->BR = &BR;
Ted Kremenekbf6babf2009-02-04 23:49:09 +00002376}
Ted Kremenekfe30beb2008-04-30 23:47:44 +00002377
2378static const char* Msgs[] = {
Ted Kremenekef9d0a82009-02-26 21:04:07 +00002379 // GC only
2380 "Code is compiled to only use garbage collection",
2381 // No GC.
2382 "Code is compiled to not use reference counts and not garbage collection",
2383 // Hybrid, with GC.
2384 "Code is compiled to use either garbage collection (GC) or reference counts"
2385 " (non-GC). The bug occurs with GC enabled",
2386 // Hybrid, without GC
2387 "Code is compiled to use either garbage collection (GC) or reference counts"
2388 " (non-GC). The bug occurs in non-GC mode"
Ted Kremenekfe30beb2008-04-30 23:47:44 +00002389};
2390
2391std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2392 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
2393
2394 switch (TF.getLangOptions().getGCMode()) {
2395 default:
2396 assert(false);
Ted Kremenekcb4709402008-05-01 04:02:04 +00002397
2398 case LangOptions::GCOnly:
2399 assert (TF.isGCEnabled());
Ted Kremenek3d6ddbb2008-08-12 18:30:56 +00002400 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2401
Ted Kremenekfe30beb2008-04-30 23:47:44 +00002402 case LangOptions::NonGC:
2403 assert (!TF.isGCEnabled());
Ted Kremenekfe30beb2008-04-30 23:47:44 +00002404 return std::make_pair(&Msgs[1], &Msgs[1]+1);
2405
2406 case LangOptions::HybridGC:
2407 if (TF.isGCEnabled())
2408 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2409 else
2410 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2411 }
2412}
2413
Ted Kremenek2126bef2009-02-18 21:57:45 +00002414static inline bool contains(const llvm::SmallVectorImpl<ArgEffect>& V,
2415 ArgEffect X) {
2416 for (llvm::SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end();
2417 I!=E; ++I)
2418 if (*I == X) return true;
2419
2420 return false;
2421}
2422
Ted Kremenek3f6c6802009-01-24 00:55:43 +00002423PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode<GRState>* N,
2424 const ExplodedNode<GRState>* PrevN,
2425 const ExplodedGraph<GRState>& G,
Ted Kremenekc26c4692009-02-18 03:48:14 +00002426 BugReporter& BR,
2427 NodeResolver& NR) {
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002428
Ted Kremenek71745d92009-01-28 05:29:13 +00002429 // Check if the type state has changed.
2430 GRStateManager &StMgr = cast<GRBugReporter>(BR).getStateManager();
2431 GRStateRef PrevSt(PrevN->getState(), StMgr);
2432 GRStateRef CurrSt(N->getState(), StMgr);
Ted Kremenek335a3022009-01-28 05:06:46 +00002433
Ted Kremenek71745d92009-01-28 05:29:13 +00002434 const RefVal* CurrT = CurrSt.get<RefBindings>(Sym);
2435 if (!CurrT) return NULL;
2436
2437 const RefVal& CurrV = *CurrT;
2438 const RefVal* PrevT = PrevSt.get<RefBindings>(Sym);
Ted Kremenek9363fd92008-05-05 17:53:17 +00002439
Ted Kremenek2126bef2009-02-18 21:57:45 +00002440 // Create a string buffer to constain all the useful things we want
2441 // to tell the user.
2442 std::string sbuf;
2443 llvm::raw_string_ostream os(sbuf);
2444
Ted Kremenekc26c4692009-02-18 03:48:14 +00002445 // This is the allocation site since the previous node had no bindings
2446 // for this symbol.
Ted Kremeneka8503952008-04-18 04:55:01 +00002447 if (!PrevT) {
Ted Kremenek9363fd92008-05-05 17:53:17 +00002448 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2449
Ted Kremenek2e2b1332009-01-28 05:15:02 +00002450 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
2451 // Get the name of the callee (if it is available).
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00002452 SVal X = CurrSt.GetSValAsScalarOrLoc(CE->getCallee());
Ted Kremenek2e2b1332009-01-28 05:15:02 +00002453 if (loc::FuncVal* FV = dyn_cast<loc::FuncVal>(&X))
2454 os << "Call to function '" << FV->getDecl()->getNameAsString() <<'\'';
2455 else
Ted Kremenekb4bf8cf2009-01-28 06:01:42 +00002456 os << "function call";
Ted Kremenek2e2b1332009-01-28 05:15:02 +00002457 }
2458 else {
2459 assert (isa<ObjCMessageExpr>(S));
Ted Kremenekb4bf8cf2009-01-28 06:01:42 +00002460 os << "Method";
Ted Kremenek9363fd92008-05-05 17:53:17 +00002461 }
Ted Kremenek2e2b1332009-01-28 05:15:02 +00002462
Ted Kremenek18878b12009-01-28 06:06:36 +00002463 if (CurrV.getObjKind() == RetEffect::CF) {
2464 os << " returns a Core Foundation object with a ";
2465 }
2466 else {
2467 assert (CurrV.getObjKind() == RetEffect::ObjC);
2468 os << " returns an Objective-C object with a ";
2469 }
Ted Kremenekb4bf8cf2009-01-28 06:01:42 +00002470
Ted Kremenekabe30922009-01-28 06:25:48 +00002471 if (CurrV.isOwned()) {
2472 os << "+1 retain count (owning reference).";
2473
2474 if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) {
2475 assert(CurrV.getObjKind() == RetEffect::CF);
2476 os << " "
2477 "Core Foundation objects are not automatically garbage collected.";
2478 }
2479 }
Ted Kremeneka8503952008-04-18 04:55:01 +00002480 else {
2481 assert (CurrV.isNotOwned());
Ted Kremenek2e2b1332009-01-28 05:15:02 +00002482 os << "+0 retain count (non-owning reference).";
Ted Kremeneka8503952008-04-18 04:55:01 +00002483 }
Ted Kremenek9363fd92008-05-05 17:53:17 +00002484
Ted Kremeneka8503952008-04-18 04:55:01 +00002485 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenekbc543722009-01-28 04:47:13 +00002486 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str());
Ted Kremeneka8503952008-04-18 04:55:01 +00002487
2488 if (Expr* Exp = dyn_cast<Expr>(S))
2489 P->addRange(Exp->getSourceRange());
2490
2491 return P;
2492 }
Ted Kremeneka8503952008-04-18 04:55:01 +00002493
Ted Kremenek2126bef2009-02-18 21:57:45 +00002494 // Gather up the effects that were performed on the object at this
2495 // program point
2496 llvm::SmallVector<ArgEffect, 2> AEffects;
2497
Ted Kremenekc26c4692009-02-18 03:48:14 +00002498 if (const RetainSummary *Summ = TF.getSummaryOfNode(NR.getOriginalNode(N))) {
2499 // We only have summaries attached to nodes after evaluating CallExpr and
2500 // ObjCMessageExprs.
2501 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2502
Ted Kremenekc26c4692009-02-18 03:48:14 +00002503 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
2504 // Iterate through the parameter expressions and see if the symbol
2505 // was ever passed as an argument.
2506 unsigned i = 0;
2507
2508 for (CallExpr::arg_iterator AI=CE->arg_begin(), AE=CE->arg_end();
2509 AI!=AE; ++AI, ++i) {
Ted Kremenek2126bef2009-02-18 21:57:45 +00002510
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002511 // Retrieve the value of the argument. Is it the symbol
2512 // we are interested in?
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00002513 if (CurrSt.GetSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym)
Ted Kremenekc26c4692009-02-18 03:48:14 +00002514 continue;
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002515
Ted Kremenekc26c4692009-02-18 03:48:14 +00002516 // We have an argument. Get the effect!
2517 AEffects.push_back(Summ->getArg(i));
Ted Kremenek752b5842008-04-18 05:32:44 +00002518 }
Ted Kremenekc26c4692009-02-18 03:48:14 +00002519 }
2520 else if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) {
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002521 if (Expr *receiver = ME->getReceiver())
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00002522 if (CurrSt.GetSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) {
Ted Kremenek2126bef2009-02-18 21:57:45 +00002523 // The symbol we are tracking is the receiver.
2524 AEffects.push_back(Summ->getReceiverEffect());
2525 }
Ted Kremenekc26c4692009-02-18 03:48:14 +00002526 }
Ted Kremeneka8503952008-04-18 04:55:01 +00002527 }
Ted Kremenekc26c4692009-02-18 03:48:14 +00002528
Ted Kremenek2126bef2009-02-18 21:57:45 +00002529 do {
2530 // Get the previous type state.
2531 RefVal PrevV = *PrevT;
2532
2533 // Specially handle CFMakeCollectable and friends.
2534 if (contains(AEffects, MakeCollectable)) {
2535 // Get the name of the function.
2536 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2537 loc::FuncVal FV =
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00002538 cast<loc::FuncVal>(CurrSt.GetSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee()));
Ted Kremenek2126bef2009-02-18 21:57:45 +00002539 const std::string& FName = FV.getDecl()->getNameAsString();
2540
2541 if (TF.isGCEnabled()) {
2542 // Determine if the object's reference count was pushed to zero.
2543 assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
2544
2545 os << "In GC mode a call to '" << FName
2546 << "' decrements an object's retain count and registers the "
2547 "object with the garbage collector. ";
2548
Ted Kremenekb7d9c9e2009-02-18 22:57:22 +00002549 if (CurrV.getKind() == RefVal::Released) {
2550 assert(CurrV.getCount() == 0);
2551 os << "Since it now has a 0 retain count the object can be "
Ted Kremenek2126bef2009-02-18 21:57:45 +00002552 "automatically collected by the garbage collector.";
Ted Kremenekb7d9c9e2009-02-18 22:57:22 +00002553 }
Ted Kremenek2126bef2009-02-18 21:57:45 +00002554 else
2555 os << "An object must have a 0 retain count to be garbage collected. "
2556 "After this call its retain count is +" << CurrV.getCount()
2557 << '.';
2558 }
2559 else
2560 os << "When GC is not enabled a call to '" << FName
2561 << "' has no effect on its argument.";
2562
2563 // Nothing more to say.
2564 break;
2565 }
2566
2567 // Determine if the typestate has changed.
2568 if (!(PrevV == CurrV))
2569 switch (CurrV.getKind()) {
Ted Kremenekc26c4692009-02-18 03:48:14 +00002570 case RefVal::Owned:
2571 case RefVal::NotOwned:
2572
2573 if (PrevV.getCount() == CurrV.getCount())
2574 return 0;
2575
2576 if (PrevV.getCount() > CurrV.getCount())
2577 os << "Reference count decremented.";
2578 else
2579 os << "Reference count incremented.";
Ted Kremenekb7d9c9e2009-02-18 22:57:22 +00002580
Ted Kremenekef9d0a82009-02-26 21:04:07 +00002581 if (unsigned Count = CurrV.getCount())
2582 os << " The object now has a +" << Count << " retain count.";
Ted Kremenekb7d9c9e2009-02-18 22:57:22 +00002583
2584 if (PrevV.getKind() == RefVal::Released) {
2585 assert(TF.isGCEnabled() && CurrV.getCount() > 0);
2586 os << " The object is not eligible for garbage collection until the "
2587 "retain count reaches 0 again.";
2588 }
2589
Ted Kremenekc26c4692009-02-18 03:48:14 +00002590 break;
2591
2592 case RefVal::Released:
2593 os << "Object released.";
2594 break;
2595
2596 case RefVal::ReturnedOwned:
2597 os << "Object returned to caller as an owning reference (single retain "
2598 "count transferred to caller).";
2599 break;
2600
2601 case RefVal::ReturnedNotOwned:
2602 os << "Object returned to caller with a +0 (non-owning) retain count.";
2603 break;
2604
2605 default:
2606 return NULL;
Ted Kremenek2126bef2009-02-18 21:57:45 +00002607 }
2608
2609 // Emit any remaining diagnostics for the argument effects (if any).
2610 for (llvm::SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(),
2611 E=AEffects.end(); I != E; ++I) {
2612
2613 // A bunch of things have alternate behavior under GC.
2614 if (TF.isGCEnabled())
2615 switch (*I) {
2616 default: break;
2617 case Autorelease:
2618 os << "In GC mode an 'autorelease' has no effect.";
2619 continue;
2620 case IncRefMsg:
2621 os << "In GC mode the 'retain' message has no effect.";
2622 continue;
2623 case DecRefMsg:
2624 os << "In GC mode the 'release' message has no effect.";
2625 continue;
2626 }
Ted Kremenekc26c4692009-02-18 03:48:14 +00002627 }
Ted Kremenek2126bef2009-02-18 21:57:45 +00002628 } while(0);
Ted Kremenekc26c4692009-02-18 03:48:14 +00002629
2630 if (os.str().empty())
2631 return 0; // We have nothing to say!
Ted Kremeneka8503952008-04-18 04:55:01 +00002632
2633 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2634 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremenekbc543722009-01-28 04:47:13 +00002635 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str());
Ted Kremeneka8503952008-04-18 04:55:01 +00002636
2637 // Add the range by scanning the children of the statement for any bindings
2638 // to Sym.
Ted Kremeneka8503952008-04-18 04:55:01 +00002639 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002640 if (Expr* Exp = dyn_cast_or_null<Expr>(*I))
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00002641 if (CurrSt.GetSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) {
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002642 P->addRange(Exp->getSourceRange());
2643 break;
2644 }
Ted Kremeneka8503952008-04-18 04:55:01 +00002645
2646 return P;
Ted Kremenek2be7ddb2008-04-18 03:39:05 +00002647}
2648
Ted Kremenekb15eba42008-10-04 05:50:14 +00002649namespace {
2650class VISIBILITY_HIDDEN FindUniqueBinding :
2651 public StoreManager::BindingsHandler {
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00002652 SymbolRef Sym;
Ted Kremenekb15eba42008-10-04 05:50:14 +00002653 MemRegion* Binding;
2654 bool First;
2655
2656 public:
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00002657 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
Ted Kremenekb15eba42008-10-04 05:50:14 +00002658
Zhongxing Xu097fc982008-10-17 05:57:07 +00002659 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal val) {
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002660 SymbolRef SymV = val.getAsSymbol();
2661
2662 if (!SymV.isValid() || SymV != Sym)
Ted Kremenekb15eba42008-10-04 05:50:14 +00002663 return true;
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002664
Ted Kremenekb15eba42008-10-04 05:50:14 +00002665 if (Binding) {
2666 First = false;
2667 return false;
2668 }
2669 else
2670 Binding = R;
2671
2672 return true;
2673 }
2674
2675 operator bool() { return First && Binding; }
2676 MemRegion* getRegion() { return Binding; }
2677};
2678}
2679
Ted Kremenek3f6c6802009-01-24 00:55:43 +00002680static std::pair<const ExplodedNode<GRState>*,const MemRegion*>
Ted Kremenek86617f42009-02-07 22:19:59 +00002681GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode<GRState>* N,
Ted Kremenekb9cd9a72008-12-05 02:27:51 +00002682 SymbolRef Sym) {
Ted Kremenekd7e26782008-05-16 18:33:44 +00002683
Ted Kremenekbe9b6f72008-08-29 00:47:32 +00002684 // Find both first node that referred to the tracked symbol and the
2685 // memory location that value was store to.
Ted Kremenek3f6c6802009-01-24 00:55:43 +00002686 const ExplodedNode<GRState>* Last = N;
2687 const MemRegion* FirstBinding = 0;
Ted Kremenekd7e26782008-05-16 18:33:44 +00002688
2689 while (N) {
Ted Kremenekabd89ac2008-08-13 04:27:00 +00002690 const GRState* St = N->getState();
Ted Kremenek4ae925c2008-08-14 21:16:54 +00002691 RefBindings B = St->get<RefBindings>();
Ted Kremenekd7e26782008-05-16 18:33:44 +00002692
Ted Kremenek6064a362008-07-07 16:21:19 +00002693 if (!B.lookup(Sym))
Ted Kremenekd7e26782008-05-16 18:33:44 +00002694 break;
Ted Kremenekbe9b6f72008-08-29 00:47:32 +00002695
Ted Kremenek86617f42009-02-07 22:19:59 +00002696 FindUniqueBinding FB(Sym);
2697 StateMgr.iterBindings(St, FB);
2698 if (FB) FirstBinding = FB.getRegion();
Ted Kremenekd7e26782008-05-16 18:33:44 +00002699
Ted Kremenekd7e26782008-05-16 18:33:44 +00002700 Last = N;
2701 N = N->pred_empty() ? NULL : *(N->pred_begin());
2702 }
2703
Ted Kremenekbe9b6f72008-08-29 00:47:32 +00002704 return std::make_pair(Last, FirstBinding);
Ted Kremenekd7e26782008-05-16 18:33:44 +00002705}
Ted Kremenek4c479322008-05-06 23:07:13 +00002706
Ted Kremenek3f6c6802009-01-24 00:55:43 +00002707PathDiagnosticPiece*
2708CFRefReport::getEndPath(BugReporter& br, const ExplodedNode<GRState>* EndN) {
Ted Kremenek86953652008-05-22 23:45:19 +00002709
Ted Kremenekbe9b6f72008-08-29 00:47:32 +00002710 GRBugReporter& BR = cast<GRBugReporter>(br);
Ted Kremenek86953652008-05-22 23:45:19 +00002711 // Tell the BugReporter to report cases when the tracked symbol is
2712 // assigned to different variables, etc.
Ted Kremenekba1c7ed2008-07-02 21:24:01 +00002713 cast<GRBugReporter>(BR).addNotableSymbol(Sym);
Ted Kremenek8ff05042009-02-07 22:04:05 +00002714 return RangedBugReport::getEndPath(BR, EndN);
2715}
2716
2717PathDiagnosticPiece*
2718CFRefLeakReport::getEndPath(BugReporter& br, const ExplodedNode<GRState>* EndN){
2719
2720 GRBugReporter& BR = cast<GRBugReporter>(br);
2721 // Tell the BugReporter to report cases when the tracked symbol is
2722 // assigned to different variables, etc.
2723 cast<GRBugReporter>(BR).addNotableSymbol(Sym);
2724
2725 // We are reporting a leak. Walk up the graph to get to the first node where
2726 // the symbol appeared, and also get the first VarDecl that tracked object
Ted Kremenekd7e26782008-05-16 18:33:44 +00002727 // is stored to.
Ted Kremenek3f6c6802009-01-24 00:55:43 +00002728 const ExplodedNode<GRState>* AllocNode = 0;
2729 const MemRegion* FirstBinding = 0;
Ted Kremenekbe9b6f72008-08-29 00:47:32 +00002730
2731 llvm::tie(AllocNode, FirstBinding) =
Ted Kremenek86617f42009-02-07 22:19:59 +00002732 GetAllocationSite(BR.getStateManager(), EndN, Sym);
Ted Kremenekfe4d2312008-05-01 23:13:35 +00002733
Ted Kremenekd7e26782008-05-16 18:33:44 +00002734 // Get the allocate site.
2735 assert (AllocNode);
2736 Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Ted Kremenekfe4d2312008-05-01 23:13:35 +00002737
Ted Kremenekea794e92008-05-05 18:50:19 +00002738 SourceManager& SMgr = BR.getContext().getSourceManager();
Chris Lattner18c8dc02009-01-16 07:36:28 +00002739 unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart());
Ted Kremenekfe4d2312008-05-01 23:13:35 +00002740
Ted Kremeneke0336742009-02-18 23:28:26 +00002741 // Get the leak site. We want to find the last place where the symbol
2742 // was used in an expression.
2743 const ExplodedNode<GRState>* LeakN = EndN;
2744 Stmt *S = 0;
Ted Kremenekea794e92008-05-05 18:50:19 +00002745
Ted Kremeneke0336742009-02-18 23:28:26 +00002746 while (LeakN) {
Ted Kremenek914fc7c2009-02-24 23:30:57 +00002747 bool atBranch = false;
Ted Kremeneke0336742009-02-18 23:28:26 +00002748 ProgramPoint P = LeakN->getLocation();
Ted Kremeneke0336742009-02-18 23:28:26 +00002749
2750 if (const PostStmt *PS = dyn_cast<PostStmt>(&P))
2751 S = PS->getStmt();
Ted Kremenek914fc7c2009-02-24 23:30:57 +00002752 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
2753 // FIXME: What we really want is to set LeakN to be the node
2754 // for the BlockEntrance for the branch we took and have BugReporter
2755 // do the right thing.
Ted Kremeneke0336742009-02-18 23:28:26 +00002756 S = BE->getSrc()->getTerminator();
Ted Kremeneka1e39992009-02-24 23:34:17 +00002757 atBranch = (S != 0);
Ted Kremenek914fc7c2009-02-24 23:30:57 +00002758 }
Ted Kremeneke0336742009-02-18 23:28:26 +00002759
2760 if (S) {
2761 // Scan 'S' for uses of Sym.
2762 GRStateRef state(LeakN->getState(), BR.getStateManager());
2763 bool foundSymbol = false;
Ted Kremenek83ec2f92009-02-19 18:18:48 +00002764
2765 // First check if 'S' itself binds to the symbol.
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002766 if (Expr *Ex = dyn_cast<Expr>(S))
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00002767 if (state.GetSValAsScalarOrLoc(Ex).getAsLocSymbol() == Sym)
Ted Kremenek83ec2f92009-02-19 18:18:48 +00002768 foundSymbol = true;
Ted Kremenek83ec2f92009-02-19 18:18:48 +00002769
2770 if (!foundSymbol)
2771 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end();
2772 I!=E; ++I)
2773 if (Expr *Ex = dyn_cast_or_null<Expr>(*I)) {
Ted Kremenekb6ac0e52009-03-04 00:13:50 +00002774 SVal X = state.GetSValAsScalarOrLoc(Ex);
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002775 if (X.getAsLocSymbol() == Sym) {
Ted Kremenek83ec2f92009-02-19 18:18:48 +00002776 foundSymbol = true;
2777 break;
2778 }
Ted Kremeneke0336742009-02-18 23:28:26 +00002779 }
Ted Kremenek83ec2f92009-02-19 18:18:48 +00002780
Ted Kremeneke0336742009-02-18 23:28:26 +00002781 if (foundSymbol)
2782 break;
2783 }
2784
Ted Kremenek914fc7c2009-02-24 23:30:57 +00002785 // Don't traverse any higher than the branch.
2786 if (atBranch)
2787 break;
2788
Ted Kremeneke0336742009-02-18 23:28:26 +00002789 LeakN = LeakN->pred_empty() ? 0 : *(LeakN->pred_begin());
2790 }
2791
2792 assert(LeakN && S && "No leak site found.");
Ted Kremenekea794e92008-05-05 18:50:19 +00002793
Ted Kremenekea794e92008-05-05 18:50:19 +00002794 // Generate the diagnostic.
Ted Kremenek323207b2009-02-18 22:59:04 +00002795 FullSourceLoc L(S->getLocStart(), SMgr);
Ted Kremenek59f9fe12009-02-07 21:59:45 +00002796 std::string sbuf;
2797 llvm::raw_string_ostream os(sbuf);
Ted Kremenek198cae02008-05-02 20:53:50 +00002798
Ted Kremenekea794e92008-05-05 18:50:19 +00002799 os << "Object allocated on line " << AllocLine;
Ted Kremenek198cae02008-05-02 20:53:50 +00002800
Ted Kremenekbe9b6f72008-08-29 00:47:32 +00002801 if (FirstBinding)
Ted Kremenekb15eba42008-10-04 05:50:14 +00002802 os << " and stored into '" << FirstBinding->getString() << '\'';
2803
Ted Kremenek311f3d42008-10-22 23:56:21 +00002804 // Get the retain count.
2805 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
2806
2807 if (RV->getKind() == RefVal::ErrorLeakReturned) {
Ted Kremenekf9544fe2008-12-02 01:26:07 +00002808 // FIXME: Per comments in rdar://6320065, "create" only applies to CF
2809 // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership
2810 // to the caller for NS objects.
Ted Kremenek311f3d42008-10-22 23:56:21 +00002811 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BR.getGraph().getCodeDecl());
2812 os << " is returned from a method whose name ('"
Chris Lattner3a8f2942008-11-24 03:33:13 +00002813 << MD.getSelector().getAsString()
Ted Kremenek35920ed2009-01-07 00:39:56 +00002814 << "') does not contain 'copy' or otherwise starts with"
Ted Kremeneka05446c2008-10-24 21:22:44 +00002815 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenek311f3d42008-10-22 23:56:21 +00002816 " in the Memory Management Guide for Cocoa (object leaked).";
2817 }
2818 else
Ted Kremeneka05446c2008-10-24 21:22:44 +00002819 os << " is no longer referenced after this point and has a retain count of"
2820 " +"
Ted Kremenek311f3d42008-10-22 23:56:21 +00002821 << RV->getCount() << " (object leaked).";
Ted Kremenekfe4d2312008-05-01 23:13:35 +00002822
Ted Kremenek323207b2009-02-18 22:59:04 +00002823 return new PathDiagnosticPiece(L, os.str());
Ted Kremenekfe4d2312008-05-01 23:13:35 +00002824}
2825
Ted Kremenek7f3f41a2008-04-17 23:43:50 +00002826
Ted Kremenekc26c4692009-02-18 03:48:14 +00002827CFRefLeakReport::CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
2828 ExplodedNode<GRState> *n,
Ted Kremenek44274e62009-02-07 22:38:00 +00002829 SymbolRef sym, GRExprEngine& Eng)
Ted Kremenekc26c4692009-02-18 03:48:14 +00002830 : CFRefReport(D, tf, n, sym)
Ted Kremenek86617f42009-02-07 22:19:59 +00002831{
2832
Ted Kremenekd7e26782008-05-16 18:33:44 +00002833 // Most bug reports are cached at the location where they occured.
2834 // With leaks, we want to unique them by the location where they were
Ted Kremenek86617f42009-02-07 22:19:59 +00002835 // allocated, and only report a single path. To do this, we need to find
2836 // the allocation site of a piece of tracked memory, which we do via a
2837 // call to GetAllocationSite. This will walk the ExplodedGraph backwards.
2838 // Note that this is *not* the trimmed graph; we are guaranteed, however,
2839 // that all ancestor nodes that represent the allocation site have the
2840 // same SourceLocation.
2841 const ExplodedNode<GRState>* AllocNode = 0;
2842
2843 llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding.
Ted Kremenek44274e62009-02-07 22:38:00 +00002844 GetAllocationSite(Eng.getStateManager(), getEndNode(), getSymbol());
Ted Kremenek86617f42009-02-07 22:19:59 +00002845
Ted Kremenek86617f42009-02-07 22:19:59 +00002846 // Get the SourceLocation for the allocation site.
Ted Kremenek44274e62009-02-07 22:38:00 +00002847 ProgramPoint P = AllocNode->getLocation();
Ted Kremenek86617f42009-02-07 22:19:59 +00002848 AllocSite = cast<PostStmt>(P).getStmt()->getLocStart();
Ted Kremenek44274e62009-02-07 22:38:00 +00002849
2850 // Fill in the description of the bug.
2851 Description.clear();
2852 llvm::raw_string_ostream os(Description);
2853 SourceManager& SMgr = Eng.getContext().getSourceManager();
2854 unsigned AllocLine = SMgr.getInstantiationLineNumber(AllocSite);
Ted Kremenek91f51ce2009-02-07 22:54:59 +00002855 os << "Potential leak of object allocated on line " << AllocLine;
2856
2857 // FIXME: AllocBinding doesn't get populated for RegionStore yet.
2858 if (AllocBinding)
2859 os << " and store into '" << AllocBinding->getString() << '\'';
Ted Kremenekd7e26782008-05-16 18:33:44 +00002860}
2861
Ted Kremeneka7338b42008-03-11 06:39:11 +00002862//===----------------------------------------------------------------------===//
Ted Kremenek708af042009-02-05 06:50:21 +00002863// Handle dead symbols and end-of-path.
2864//===----------------------------------------------------------------------===//
2865
2866void CFRefCount::EvalEndPath(GRExprEngine& Eng,
2867 GREndPathNodeBuilder<GRState>& Builder) {
2868
2869 const GRState* St = Builder.getState();
2870 RefBindings B = St->get<RefBindings>();
2871
2872 llvm::SmallVector<std::pair<SymbolRef, bool>, 10> Leaked;
2873 const Decl* CodeDecl = &Eng.getGraph().getCodeDecl();
2874
2875 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
2876 bool hasLeak = false;
2877
2878 std::pair<GRStateRef, bool> X =
Ted Kremenek9577c1e2009-03-03 22:06:47 +00002879 HandleSymbolDeath(Eng.getStateManager(), St, CodeDecl,
2880 (*I).first, (*I).second, hasLeak);
Ted Kremenek708af042009-02-05 06:50:21 +00002881
2882 St = X.first;
2883 if (hasLeak) Leaked.push_back(std::make_pair((*I).first, X.second));
2884 }
2885
2886 if (Leaked.empty())
2887 return;
2888
2889 ExplodedNode<GRState>* N = Builder.MakeNode(St);
2890
2891 if (!N)
2892 return;
2893
2894 for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator
2895 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
2896
2897 CFRefBug *BT = static_cast<CFRefBug*>(I->second ? leakAtReturn
2898 : leakWithinFunction);
2899 assert(BT && "BugType not initialized.");
Ted Kremenekc26c4692009-02-18 03:48:14 +00002900 CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, I->first, Eng);
Ted Kremenek708af042009-02-05 06:50:21 +00002901 BR->EmitReport(report);
2902 }
2903}
2904
2905void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
2906 GRExprEngine& Eng,
2907 GRStmtNodeBuilder<GRState>& Builder,
2908 ExplodedNode<GRState>* Pred,
2909 Stmt* S,
2910 const GRState* St,
2911 SymbolReaper& SymReaper) {
2912
Ted Kremenek876d8df2009-02-19 23:47:02 +00002913 // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
Ted Kremenek708af042009-02-05 06:50:21 +00002914 RefBindings B = St->get<RefBindings>();
2915 llvm::SmallVector<std::pair<SymbolRef,bool>, 10> Leaked;
2916
2917 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
2918 E = SymReaper.dead_end(); I != E; ++I) {
2919
2920 const RefVal* T = B.lookup(*I);
2921 if (!T) continue;
2922
2923 bool hasLeak = false;
2924
2925 std::pair<GRStateRef, bool> X
Ted Kremenek876d8df2009-02-19 23:47:02 +00002926 = HandleSymbolDeath(Eng.getStateManager(), St, 0, *I, *T, hasLeak);
Ted Kremenek708af042009-02-05 06:50:21 +00002927
2928 St = X.first;
2929
2930 if (hasLeak)
2931 Leaked.push_back(std::make_pair(*I,X.second));
2932 }
2933
Ted Kremenek876d8df2009-02-19 23:47:02 +00002934 if (!Leaked.empty()) {
2935 // Create a new intermediate node representing the leak point. We
2936 // use a special program point that represents this checker-specific
2937 // transition. We use the address of RefBIndex as a unique tag for this
2938 // checker. We will create another node (if we don't cache out) that
2939 // removes the retain-count bindings from the state.
2940 // NOTE: We use 'generateNode' so that it does interplay with the
2941 // auto-transition logic.
2942 ExplodedNode<GRState>* N =
2943 Builder.generateNode(PostStmtCustom(S, &LeakProgramPointTag), St, Pred);
Ted Kremenek708af042009-02-05 06:50:21 +00002944
Ted Kremenek876d8df2009-02-19 23:47:02 +00002945 if (!N)
2946 return;
2947
2948 // Generate the bug reports.
2949 for (llvm::SmallVectorImpl<std::pair<SymbolRef,bool> >::iterator
2950 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
2951
2952 CFRefBug *BT = static_cast<CFRefBug*>(I->second ? leakAtReturn
2953 : leakWithinFunction);
2954 assert(BT && "BugType not initialized.");
Ted Kremenek56c70aa2009-02-23 16:54:00 +00002955 CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N,
2956 I->first, Eng);
Ted Kremenek876d8df2009-02-19 23:47:02 +00002957 BR->EmitReport(report);
2958 }
Ted Kremenek708af042009-02-05 06:50:21 +00002959
Ted Kremenek876d8df2009-02-19 23:47:02 +00002960 Pred = N;
Ted Kremenek708af042009-02-05 06:50:21 +00002961 }
Ted Kremenek876d8df2009-02-19 23:47:02 +00002962
2963 // Now generate a new node that nukes the old bindings.
2964 GRStateRef state(St, Eng.getStateManager());
2965 RefBindings::Factory& F = state.get_context<RefBindings>();
2966
2967 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
2968 E = SymReaper.dead_end(); I!=E; ++I)
2969 B = F.Remove(B, *I);
2970
2971 state = state.set<RefBindings>(B);
2972 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek708af042009-02-05 06:50:21 +00002973}
2974
2975void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
2976 GRStmtNodeBuilder<GRState>& Builder,
2977 Expr* NodeExpr, Expr* ErrorExpr,
2978 ExplodedNode<GRState>* Pred,
2979 const GRState* St,
2980 RefVal::Kind hasErr, SymbolRef Sym) {
2981 Builder.BuildSinks = true;
2982 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
2983
2984 if (!N) return;
2985
2986 CFRefBug *BT = 0;
2987
2988 if (hasErr == RefVal::ErrorUseAfterRelease)
2989 BT = static_cast<CFRefBug*>(useAfterRelease);
2990 else {
2991 assert(hasErr == RefVal::ErrorReleaseNotOwned);
2992 BT = static_cast<CFRefBug*>(releaseNotOwned);
2993 }
2994
Ted Kremenekc26c4692009-02-18 03:48:14 +00002995 CFRefReport *report = new CFRefReport(*BT, *this, N, Sym);
Ted Kremenek708af042009-02-05 06:50:21 +00002996 report->addRange(ErrorExpr->getSourceRange());
2997 BR->EmitReport(report);
2998}
2999
3000//===----------------------------------------------------------------------===//
Ted Kremenekb1983ba2008-04-10 22:16:52 +00003001// Transfer function creation for external clients.
Ted Kremeneka7338b42008-03-11 06:39:11 +00003002//===----------------------------------------------------------------------===//
3003
Ted Kremenekfe30beb2008-04-30 23:47:44 +00003004GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
3005 const LangOptions& lopts) {
Ted Kremenek9f20c7c2008-07-22 16:21:24 +00003006 return new CFRefCount(Ctx, GCEnabled, lopts);
Ted Kremeneka4c74292008-04-10 22:58:08 +00003007}