blob: 2d5cb5f5b89ef46a77ad03336af9ba4859dae03e [file] [log] [blame]
Chris Lattnerbda0b622008-03-15 23:59:48 +00001// CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
Ted Kremenek2fff37e2008-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 Greif843e9342008-03-06 10:40:09 +000010// This file defines the methods for CFRefCount, which implements
Ted Kremenek2fff37e2008-03-06 00:08:09 +000011// a reference count checker for Core Foundation (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000015#include "GRSimpleVals.h"
Ted Kremenek072192b2008-04-30 23:47:44 +000016#include "clang/Basic/LangOptions.h"
Ted Kremenekc9fa2f72008-05-01 23:13:35 +000017#include "clang/Basic/SourceManager.h"
Ted Kremenek4adc81e2008-08-13 04:27:00 +000018#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenekb9d17f92008-08-17 03:20:02 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000020#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek2fff37e2008-03-06 00:08:09 +000021#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000022#include "clang/Analysis/PathDiagnostic.h"
23#include "clang/Analysis/PathSensitive/BugReporter.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000024#include "clang/AST/DeclObjC.h"
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000025#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/FoldingSet.h"
27#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek6d348932008-10-21 15:53:15 +000028#include "llvm/ADT/ImmutableList.h"
Ted Kremenek900a2d72008-05-07 18:36:45 +000029#include "llvm/ADT/StringExtras.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000030#include "llvm/Support/Compiler.h"
Ted Kremenek6ed9afc2008-05-16 18:33:44 +000031#include "llvm/ADT/STLExtras.h"
Ted Kremenekf3948042008-03-11 19:44:10 +000032#include <ostream>
Ted Kremenek2cf943a2008-04-18 04:55:01 +000033#include <sstream>
Ted Kremenek98530452008-08-12 20:41:56 +000034#include <stdarg.h>
Ted Kremenek2fff37e2008-03-06 00:08:09 +000035
36using namespace clang;
Ted Kremenek5c74d502008-10-24 21:18:08 +000037
38//===----------------------------------------------------------------------===//
39// Utility functions.
40//===----------------------------------------------------------------------===//
41
Ted Kremenek900a2d72008-05-07 18:36:45 +000042using llvm::CStrInCStrNoCase;
Ted Kremenek2fff37e2008-03-06 00:08:09 +000043
Ted Kremenek5c74d502008-10-24 21:18:08 +000044// The "fundamental rule" for naming conventions of methods:
45// (url broken into two lines)
46// http://developer.apple.com/documentation/Cocoa/Conceptual/
47// MemoryMgmt/Tasks/MemoryManagementRules.html
48//
49// "You take ownership of an object if you create it using a method whose name
50// begins with “alloc” or “new” or contains “copy” (for example, alloc,
51// newObject, or mutableCopy), or if you send it a retain message. You are
52// responsible for relinquishing ownership of objects you own using release
53// or autorelease. Any other time you receive an object, you must
54// not release it."
55//
56static bool followsFundamentalRule(const char* s) {
Ted Kremeneke1e91af2008-10-30 23:14:58 +000057 while (*s == '_') ++s;
Ted Kremenek234a4c22009-01-07 00:39:56 +000058 return CStrInCStrNoCase(s, "copy")
59 || CStrInCStrNoCase(s, "new") == s
60 || CStrInCStrNoCase(s, "alloc") == s;
Ted Kremenek4c79e552008-11-05 16:54:44 +000061}
62
63static bool followsReturnRule(const char* s) {
64 while (*s == '_') ++s;
65 return followsFundamentalRule(s) || CStrInCStrNoCase(s, "init") == s;
66}
Ted Kremenek5c74d502008-10-24 21:18:08 +000067
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000068//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +000069// Selector creation functions.
Ted Kremenek4fd88972008-04-17 18:12:53 +000070//===----------------------------------------------------------------------===//
71
Ted Kremenekb83e02e2008-05-01 18:31:44 +000072static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
Ted Kremenek4fd88972008-04-17 18:12:53 +000073 IdentifierInfo* II = &Ctx.Idents.get(name);
74 return Ctx.Selectors.getSelector(0, &II);
75}
76
Ted Kremenek9c32d082008-05-06 00:30:21 +000077static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
78 IdentifierInfo* II = &Ctx.Idents.get(name);
79 return Ctx.Selectors.getSelector(1, &II);
80}
81
Ted Kremenek553cf182008-06-25 21:21:56 +000082//===----------------------------------------------------------------------===//
83// Type querying functions.
84//===----------------------------------------------------------------------===//
85
Ted Kremenek12619382009-01-12 21:45:02 +000086static bool hasPrefix(const char* s, const char* prefix) {
87 if (!prefix)
88 return true;
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000089
Ted Kremenek12619382009-01-12 21:45:02 +000090 char c = *s;
91 char cP = *prefix;
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000092
Ted Kremenek12619382009-01-12 21:45:02 +000093 while (c != '\0' && cP != '\0') {
94 if (c != cP) break;
95 c = *(++s);
96 cP = *(++prefix);
97 }
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000098
Ted Kremenek12619382009-01-12 21:45:02 +000099 return cP == '\0';
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000100}
101
Ted Kremenek12619382009-01-12 21:45:02 +0000102static bool hasSuffix(const char* s, const char* suffix) {
103 const char* loc = strstr(s, suffix);
104 return loc && strcmp(suffix, loc) == 0;
105}
106
107static bool isRefType(QualType RetTy, const char* prefix,
108 ASTContext* Ctx = 0, const char* name = 0) {
Ted Kremenek37d785b2008-07-15 16:50:12 +0000109
Ted Kremenek12619382009-01-12 21:45:02 +0000110 if (TypedefType* TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
111 const char* TDName = TD->getDecl()->getIdentifier()->getName();
112 return hasPrefix(TDName, prefix) && hasSuffix(TDName, "Ref");
113 }
114
115 if (!Ctx || !name)
Ted Kremenek37d785b2008-07-15 16:50:12 +0000116 return false;
Ted Kremenek12619382009-01-12 21:45:02 +0000117
118 // Is the type void*?
119 const PointerType* PT = RetTy->getAsPointerType();
120 if (!(PT->getPointeeType().getUnqualifiedType() == Ctx->VoidTy))
Ted Kremenek37d785b2008-07-15 16:50:12 +0000121 return false;
Ted Kremenek12619382009-01-12 21:45:02 +0000122
123 // Does the name start with the prefix?
124 return hasPrefix(name, prefix);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000125}
126
Ted Kremenek4fd88972008-04-17 18:12:53 +0000127//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +0000128// Primitives used for constructing summaries for function/method calls.
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000129//===----------------------------------------------------------------------===//
130
Ted Kremenek553cf182008-06-25 21:21:56 +0000131namespace {
132/// ArgEffect is used to summarize a function/method call's effect on a
133/// particular argument.
Ted Kremenek070a8252008-07-09 18:11:16 +0000134enum ArgEffect { IncRef, DecRef, DoNothing, DoNothingByRef,
135 StopTracking, MayEscape, SelfOwn, Autorelease };
Ted Kremenek553cf182008-06-25 21:21:56 +0000136
137/// ArgEffects summarizes the effects of a function/method call on all of
138/// its arguments.
139typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000140}
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000141
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000142namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000143template <> struct FoldingSetTrait<ArgEffects> {
144 static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
145 for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) {
146 ID.AddInteger(I->first);
147 ID.AddInteger((unsigned) I->second);
148 }
149 }
150};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000151} // end llvm namespace
152
153namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000154
155/// RetEffect is used to summarize a function/method call's behavior with
156/// respect to its return value.
157class VISIBILITY_HIDDEN RetEffect {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000158public:
Ted Kremeneka7344702008-06-23 18:02:52 +0000159 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
160 NotOwnedSymbol, ReceiverAlias };
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000161
162 enum ObjKind { CF, ObjC, AnyObj };
163
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000164private:
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000165 Kind K;
166 ObjKind O;
167 unsigned index;
168
169 RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {}
170 RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {}
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000171
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000172public:
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000173 Kind getKind() const { return K; }
174
175 ObjKind getObjKind() const { return O; }
Ted Kremenek553cf182008-06-25 21:21:56 +0000176
177 unsigned getIndex() const {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000178 assert(getKind() == Alias);
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000179 return index;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000180 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000181
Ted Kremenek553cf182008-06-25 21:21:56 +0000182 static RetEffect MakeAlias(unsigned Idx) {
183 return RetEffect(Alias, Idx);
184 }
185 static RetEffect MakeReceiverAlias() {
186 return RetEffect(ReceiverAlias);
187 }
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000188 static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) {
189 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o);
Ted Kremenek553cf182008-06-25 21:21:56 +0000190 }
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000191 static RetEffect MakeNotOwned(ObjKind o) {
192 return RetEffect(NotOwnedSymbol, o);
Ted Kremenek553cf182008-06-25 21:21:56 +0000193 }
194 static RetEffect MakeNoRet() {
195 return RetEffect(NoRet);
Ted Kremeneka7344702008-06-23 18:02:52 +0000196 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000197
Ted Kremenek553cf182008-06-25 21:21:56 +0000198 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000199 ID.AddInteger((unsigned)K);
200 ID.AddInteger((unsigned)O);
201 ID.AddInteger(index);
Ted Kremenek553cf182008-06-25 21:21:56 +0000202 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000203};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000204
Ted Kremenek553cf182008-06-25 21:21:56 +0000205
206class VISIBILITY_HIDDEN RetainSummary : public llvm::FoldingSetNode {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000207 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
208 /// specifies the argument (starting from 0). This can be sparsely
209 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000210 ArgEffects* Args;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000211
212 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
213 /// do not have an entry in Args.
214 ArgEffect DefaultArgEffect;
215
Ted Kremenek553cf182008-06-25 21:21:56 +0000216 /// Receiver - If this summary applies to an Objective-C message expression,
217 /// this is the effect applied to the state of the receiver.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000218 ArgEffect Receiver;
Ted Kremenek553cf182008-06-25 21:21:56 +0000219
220 /// Ret - The effect on the return value. Used to indicate if the
221 /// function/method call returns a new tracked symbol, returns an
222 /// alias of one of the arguments in the call, and so on.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000223 RetEffect Ret;
Ted Kremenek553cf182008-06-25 21:21:56 +0000224
Ted Kremenek70a733e2008-07-18 17:24:20 +0000225 /// EndPath - Indicates that execution of this method/function should
226 /// terminate the simulation of a path.
227 bool EndPath;
228
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000229public:
230
Ted Kremenek1bffd742008-05-06 15:44:25 +0000231 RetainSummary(ArgEffects* A, RetEffect R, ArgEffect defaultEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000232 ArgEffect ReceiverEff, bool endpath = false)
233 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
234 EndPath(endpath) {}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000235
Ted Kremenek553cf182008-06-25 21:21:56 +0000236 /// getArg - Return the argument effect on the argument specified by
237 /// idx (starting from 0).
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000238 ArgEffect getArg(unsigned idx) const {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000239
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000240 if (!Args)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000241 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000242
243 // If Args is present, it is likely to contain only 1 element.
244 // Just do a linear search. Do it from the back because functions with
245 // large numbers of arguments will be tail heavy with respect to which
Ted Kremenek553cf182008-06-25 21:21:56 +0000246 // argument they actually modify with respect to the reference count.
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000247 for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend();
248 I!=E; ++I) {
249
250 if (idx > I->first)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000251 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000252
253 if (idx == I->first)
254 return I->second;
255 }
256
Ted Kremenek1bffd742008-05-06 15:44:25 +0000257 return DefaultArgEffect;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000258 }
259
Ted Kremenek553cf182008-06-25 21:21:56 +0000260 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000261 RetEffect getRetEffect() const {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000262 return Ret;
263 }
264
Ted Kremenek70a733e2008-07-18 17:24:20 +0000265 /// isEndPath - Returns true if executing the given method/function should
266 /// terminate the path.
267 bool isEndPath() const { return EndPath; }
268
Ted Kremenek553cf182008-06-25 21:21:56 +0000269 /// getReceiverEffect - Returns the effect on the receiver of the call.
270 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000271 ArgEffect getReceiverEffect() const {
272 return Receiver;
273 }
274
Ted Kremenek55499762008-06-17 02:43:46 +0000275 typedef ArgEffects::const_iterator ExprIterator;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000276
Ted Kremenek55499762008-06-17 02:43:46 +0000277 ExprIterator begin_args() const { return Args->begin(); }
278 ExprIterator end_args() const { return Args->end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000279
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000280 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000281 RetEffect RetEff, ArgEffect DefaultEff,
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000282 ArgEffect ReceiverEff, bool EndPath) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000283 ID.AddPointer(A);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000284 ID.Add(RetEff);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000285 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000286 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000287 ID.AddInteger((unsigned) EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000288 }
289
290 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000291 Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000292 }
293};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000294} // end anonymous namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000295
Ted Kremenek553cf182008-06-25 21:21:56 +0000296//===----------------------------------------------------------------------===//
297// Data structures for constructing summaries.
298//===----------------------------------------------------------------------===//
Ted Kremenek53301ba2008-06-24 03:49:48 +0000299
Ted Kremenek553cf182008-06-25 21:21:56 +0000300namespace {
301class VISIBILITY_HIDDEN ObjCSummaryKey {
302 IdentifierInfo* II;
303 Selector S;
304public:
305 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
306 : II(ii), S(s) {}
307
308 ObjCSummaryKey(ObjCInterfaceDecl* d, Selector s)
309 : II(d ? d->getIdentifier() : 0), S(s) {}
310
311 ObjCSummaryKey(Selector s)
312 : II(0), S(s) {}
313
314 IdentifierInfo* getIdentifier() const { return II; }
315 Selector getSelector() const { return S; }
316};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000317}
318
319namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000320template <> struct DenseMapInfo<ObjCSummaryKey> {
321 static inline ObjCSummaryKey getEmptyKey() {
322 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
323 DenseMapInfo<Selector>::getEmptyKey());
324 }
Ted Kremenek4f22a782008-06-23 23:30:29 +0000325
Ted Kremenek553cf182008-06-25 21:21:56 +0000326 static inline ObjCSummaryKey getTombstoneKey() {
327 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
328 DenseMapInfo<Selector>::getTombstoneKey());
329 }
330
331 static unsigned getHashValue(const ObjCSummaryKey &V) {
332 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
333 & 0x88888888)
334 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
335 & 0x55555555);
336 }
337
338 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
339 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
340 RHS.getIdentifier()) &&
341 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
342 RHS.getSelector());
343 }
344
345 static bool isPod() {
346 return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
347 DenseMapInfo<Selector>::isPod();
348 }
349};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000350} // end llvm namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000351
Ted Kremenek4f22a782008-06-23 23:30:29 +0000352namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000353class VISIBILITY_HIDDEN ObjCSummaryCache {
354 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
355 MapTy M;
356public:
357 ObjCSummaryCache() {}
358
359 typedef MapTy::iterator iterator;
360
361 iterator find(ObjCInterfaceDecl* D, Selector S) {
362
363 // Do a lookup with the (D,S) pair. If we find a match return
364 // the iterator.
365 ObjCSummaryKey K(D, S);
366 MapTy::iterator I = M.find(K);
367
368 if (I != M.end() || !D)
369 return I;
370
371 // Walk the super chain. If we find a hit with a parent, we'll end
372 // up returning that summary. We actually allow that key (null,S), as
373 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
374 // generate initial summaries without having to worry about NSObject
375 // being declared.
376 // FIXME: We may change this at some point.
377 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
378 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
379 break;
380
381 if (!C)
382 return I;
383 }
384
385 // Cache the summary with original key to make the next lookup faster
386 // and return the iterator.
387 M[K] = I->second;
388 return I;
389 }
390
Ted Kremenek98530452008-08-12 20:41:56 +0000391
Ted Kremenek553cf182008-06-25 21:21:56 +0000392 iterator find(Expr* Receiver, Selector S) {
393 return find(getReceiverDecl(Receiver), S);
394 }
395
396 iterator find(IdentifierInfo* II, Selector S) {
397 // FIXME: Class method lookup. Right now we dont' have a good way
398 // of going between IdentifierInfo* and the class hierarchy.
399 iterator I = M.find(ObjCSummaryKey(II, S));
400 return I == M.end() ? M.find(ObjCSummaryKey(S)) : I;
401 }
402
403 ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
404
405 const PointerType* PT = E->getType()->getAsPointerType();
406 if (!PT) return 0;
407
408 ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType());
409 if (!OI) return 0;
410
411 return OI ? OI->getDecl() : 0;
412 }
413
414 iterator end() { return M.end(); }
415
416 RetainSummary*& operator[](ObjCMessageExpr* ME) {
417
418 Selector S = ME->getSelector();
419
420 if (Expr* Receiver = ME->getReceiver()) {
421 ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
422 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
423 }
424
425 return M[ObjCSummaryKey(ME->getClassName(), S)];
426 }
427
428 RetainSummary*& operator[](ObjCSummaryKey K) {
429 return M[K];
430 }
431
432 RetainSummary*& operator[](Selector S) {
433 return M[ ObjCSummaryKey(S) ];
434 }
435};
436} // end anonymous namespace
437
438//===----------------------------------------------------------------------===//
439// Data structures for managing collections of summaries.
440//===----------------------------------------------------------------------===//
441
442namespace {
443class VISIBILITY_HIDDEN RetainSummaryManager {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000444
445 //==-----------------------------------------------------------------==//
446 // Typedefs.
447 //==-----------------------------------------------------------------==//
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000448
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000449 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> >
450 ArgEffectsSetTy;
451
452 typedef llvm::FoldingSet<RetainSummary>
453 SummarySetTy;
454
455 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
456 FuncSummariesTy;
457
Ted Kremenek4f22a782008-06-23 23:30:29 +0000458 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000459
460 //==-----------------------------------------------------------------==//
461 // Data.
462 //==-----------------------------------------------------------------==//
463
Ted Kremenek553cf182008-06-25 21:21:56 +0000464 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek377e2302008-04-29 05:33:51 +0000465 ASTContext& Ctx;
Ted Kremenek179064e2008-07-01 17:21:27 +0000466
Ted Kremenek070a8252008-07-09 18:11:16 +0000467 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
468 /// "CFDictionaryCreate".
469 IdentifierInfo* CFDictionaryCreateII;
470
Ted Kremenek553cf182008-06-25 21:21:56 +0000471 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek377e2302008-04-29 05:33:51 +0000472 const bool GCEnabled;
473
Ted Kremenek553cf182008-06-25 21:21:56 +0000474 /// SummarySet - A FoldingSet of uniqued summaries.
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000475 SummarySetTy SummarySet;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000476
Ted Kremenek553cf182008-06-25 21:21:56 +0000477 /// FuncSummaries - A map from FunctionDecls to summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000478 FuncSummariesTy FuncSummaries;
479
Ted Kremenek553cf182008-06-25 21:21:56 +0000480 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
481 /// to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000482 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000483
Ted Kremenek553cf182008-06-25 21:21:56 +0000484 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000485 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000486
Ted Kremenek553cf182008-06-25 21:21:56 +0000487 /// ArgEffectsSet - A FoldingSet of uniqued ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000488 ArgEffectsSetTy ArgEffectsSet;
489
Ted Kremenek553cf182008-06-25 21:21:56 +0000490 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
491 /// and all other data used by the checker.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000492 llvm::BumpPtrAllocator BPAlloc;
493
Ted Kremenek553cf182008-06-25 21:21:56 +0000494 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000495 ArgEffects ScratchArgs;
496
Ted Kremenek432af592008-05-06 18:11:36 +0000497 RetainSummary* StopSummary;
498
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000499 //==-----------------------------------------------------------------==//
500 // Methods.
501 //==-----------------------------------------------------------------==//
502
Ted Kremenek553cf182008-06-25 21:21:56 +0000503 /// getArgEffects - Returns a persistent ArgEffects object based on the
504 /// data in ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000505 ArgEffects* getArgEffects();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000506
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000507 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000508
509public:
Ted Kremenek12619382009-01-12 21:45:02 +0000510 RetainSummary* getUnarySummary(FunctionType* FT, UnaryFuncKind func);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000511
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000512 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
513 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek12619382009-01-12 21:45:02 +0000514 RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000515
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000516 RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000517 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000518 ArgEffect DefaultEff = MayEscape,
519 bool isEndPath = false);
Ted Kremenek706522f2008-10-29 04:07:07 +0000520
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000521 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000522 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000523 ArgEffect DefaultEff = MayEscape) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000524 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek9c32d082008-05-06 00:30:21 +0000525 }
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000526
Ted Kremenek1bffd742008-05-06 15:44:25 +0000527 RetainSummary* getPersistentStopSummary() {
Ted Kremenek432af592008-05-06 18:11:36 +0000528 if (StopSummary)
529 return StopSummary;
530
531 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
532 StopTracking, StopTracking);
Ted Kremenek706522f2008-10-29 04:07:07 +0000533
Ted Kremenek432af592008-05-06 18:11:36 +0000534 return StopSummary;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000535 }
Ted Kremenekb3095252008-05-06 04:20:12 +0000536
Ted Kremenek553cf182008-06-25 21:21:56 +0000537 RetainSummary* getInitMethodSummary(ObjCMessageExpr* ME);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000538
Ted Kremenek1f180c32008-06-23 22:21:20 +0000539 void InitializeClassMethodSummaries();
540 void InitializeMethodSummaries();
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000541
Ted Kremenek234a4c22009-01-07 00:39:56 +0000542 bool isTrackedObjectType(QualType T);
543
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000544private:
545
Ted Kremenek70a733e2008-07-18 17:24:20 +0000546 void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
547 RetainSummary* Summ) {
548 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
549 }
550
Ted Kremenek553cf182008-06-25 21:21:56 +0000551 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
552 ObjCClassMethodSummaries[S] = Summ;
553 }
554
555 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
556 ObjCMethodSummaries[S] = Summ;
557 }
558
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000559 void addInstMethSummary(const char* Cls, RetainSummary* Summ, va_list argp) {
Ted Kremenek70a733e2008-07-18 17:24:20 +0000560
Ted Kremenek9e476de2008-08-12 18:30:56 +0000561 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
562 llvm::SmallVector<IdentifierInfo*, 10> II;
563
564 while (const char* s = va_arg(argp, const char*))
565 II.push_back(&Ctx.Idents.get(s));
566
567 Selector S = Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek70a733e2008-07-18 17:24:20 +0000568 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
569 }
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000570
571 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
572 va_list argp;
573 va_start(argp, Summ);
574 addInstMethSummary(Cls, Summ, argp);
575 va_end(argp);
576 }
Ted Kremenek9e476de2008-08-12 18:30:56 +0000577
578 void addPanicSummary(const char* Cls, ...) {
579 RetainSummary* Summ = getPersistentSummary(0, RetEffect::MakeNoRet(),
580 DoNothing, DoNothing, true);
581 va_list argp;
582 va_start (argp, Cls);
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000583 addInstMethSummary(Cls, Summ, argp);
Ted Kremenek9e476de2008-08-12 18:30:56 +0000584 va_end(argp);
585 }
Ted Kremenek70a733e2008-07-18 17:24:20 +0000586
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000587public:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000588
589 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenek179064e2008-07-01 17:21:27 +0000590 : Ctx(ctx),
Ted Kremenek070a8252008-07-09 18:11:16 +0000591 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek553cf182008-06-25 21:21:56 +0000592 GCEnabled(gcenabled), StopSummary(0) {
593
594 InitializeClassMethodSummaries();
595 InitializeMethodSummaries();
596 }
Ted Kremenek377e2302008-04-29 05:33:51 +0000597
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000598 ~RetainSummaryManager();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000599
Ted Kremenekab592272008-06-24 03:56:45 +0000600 RetainSummary* getSummary(FunctionDecl* FD);
Ted Kremenek553cf182008-06-25 21:21:56 +0000601 RetainSummary* getMethodSummary(ObjCMessageExpr* ME, ObjCInterfaceDecl* ID);
Ted Kremenek1f180c32008-06-23 22:21:20 +0000602 RetainSummary* getClassMethodSummary(IdentifierInfo* ClsName, Selector S);
Ted Kremenekb3095252008-05-06 04:20:12 +0000603
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000604 bool isGCEnabled() const { return GCEnabled; }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000605};
606
607} // end anonymous namespace
608
609//===----------------------------------------------------------------------===//
610// Implementation of checker data structures.
611//===----------------------------------------------------------------------===//
612
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000613RetainSummaryManager::~RetainSummaryManager() {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000614
615 // FIXME: The ArgEffects could eventually be allocated from BPAlloc,
616 // mitigating the need to do explicit cleanup of the
617 // Argument-Effect summaries.
618
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000619 for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(),
620 E = ArgEffectsSet.end(); I!=E; ++I)
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000621 I->getValue().~ArgEffects();
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000622}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000623
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000624ArgEffects* RetainSummaryManager::getArgEffects() {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000625
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000626 if (ScratchArgs.empty())
627 return NULL;
628
629 // Compute a profile for a non-empty ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000630 llvm::FoldingSetNodeID profile;
631 profile.Add(ScratchArgs);
632 void* InsertPos;
633
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000634 // Look up the uniqued copy, or create a new one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000635 llvm::FoldingSetNodeWrapper<ArgEffects>* E =
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000636 ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000637
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000638 if (E) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000639 ScratchArgs.clear();
640 return &E->getValue();
641 }
642
643 E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
Ted Kremenek553cf182008-06-25 21:21:56 +0000644 BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000645
646 new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000647 ArgEffectsSet.InsertNode(E, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000648
649 ScratchArgs.clear();
650 return &E->getValue();
651}
652
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000653RetainSummary*
654RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000655 ArgEffect ReceiverEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000656 ArgEffect DefaultEff,
657 bool isEndPath) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000658
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000659 // Generate a profile for the summary.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000660 llvm::FoldingSetNodeID profile;
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000661 RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff,
662 isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000663
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000664 // Look up the uniqued summary, or create one if it doesn't exist.
665 void* InsertPos;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000666 RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000667
668 if (Summ)
669 return Summ;
670
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000671 // Create the summary and return it.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000672 Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek70a733e2008-07-18 17:24:20 +0000673 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000674 SummarySet.InsertNode(Summ, InsertPos);
675
676 return Summ;
677}
678
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000679//===----------------------------------------------------------------------===//
Ted Kremenek234a4c22009-01-07 00:39:56 +0000680// Predicates.
681//===----------------------------------------------------------------------===//
682
683bool RetainSummaryManager::isTrackedObjectType(QualType T) {
684 if (!Ctx.isObjCObjectPointerType(T))
685 return false;
686
687 // Does it subclass NSObject?
688 ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
689
690 // We assume that id<..>, id, and "Class" all represent tracked objects.
691 if (!OT)
692 return true;
693
694 // Does the object type subclass NSObject?
695 // FIXME: We can memoize here if this gets too expensive.
696 IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject");
697 ObjCInterfaceDecl* ID = OT->getDecl();
698
699 for ( ; ID ; ID = ID->getSuperClass())
700 if (ID->getIdentifier() == NSObjectII)
701 return true;
702
703 return false;
704}
705
706//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000707// Summary creation for functions (largely uses of Core Foundation).
708//===----------------------------------------------------------------------===//
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000709
Ted Kremenek12619382009-01-12 21:45:02 +0000710static bool isRetain(FunctionDecl* FD, const char* FName) {
711 const char* loc = strstr(FName, "Retain");
712 return loc && loc[sizeof("Retain")-1] == '\0';
713}
714
715static bool isRelease(FunctionDecl* FD, const char* FName) {
716 const char* loc = strstr(FName, "Release");
717 return loc && loc[sizeof("Release")-1] == '\0';
718}
719
Ted Kremenekab592272008-06-24 03:56:45 +0000720RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000721
722 SourceLocation Loc = FD->getLocation();
723
724 if (!Loc.isFileID())
725 return NULL;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000726
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000727 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000728 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000729
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000730 if (I != FuncSummaries.end())
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000731 return I->second;
732
733 // No summary. Generate one.
Ted Kremenek12619382009-01-12 21:45:02 +0000734 RetainSummary *S = 0;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000735
Ted Kremenek37d785b2008-07-15 16:50:12 +0000736 do {
Ted Kremenek12619382009-01-12 21:45:02 +0000737 // We generate "stop" summaries for implicitly defined functions.
738 if (FD->isImplicit()) {
739 S = getPersistentStopSummary();
740 break;
Ted Kremenek37d785b2008-07-15 16:50:12 +0000741 }
Ted Kremenek6ca31912008-11-04 00:36:12 +0000742
Ted Kremenek99890652009-01-16 18:40:33 +0000743 // [PR 3337] Use 'getDesugaredType' to strip away any typedefs on the
744 // function's type.
745 FunctionType* FT = cast<FunctionType>(FD->getType()->getDesugaredType());
Ted Kremenek12619382009-01-12 21:45:02 +0000746 const char* FName = FD->getIdentifier()->getName();
747
748 // Inspect the result type.
749 QualType RetTy = FT->getResultType();
750
751 // FIXME: This should all be refactored into a chain of "summary lookup"
752 // filters.
753 if (strcmp(FName, "IOServiceGetMatchingServices") == 0) {
754 // FIXES: <rdar://problem/6326900>
755 // This should be addressed using a API table. This strcmp is also
756 // a little gross, but there is no need to super optimize here.
757 assert (ScratchArgs.empty());
758 ScratchArgs.push_back(std::make_pair(1, DecRef));
759 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
760 break;
Ted Kremenek64e859a2008-10-22 20:54:52 +0000761 }
Ted Kremenek12619382009-01-12 21:45:02 +0000762
763 // Handle: id NSMakeCollectable(CFTypeRef)
764 if (strcmp(FName, "NSMakeCollectable") == 0) {
765 S = (RetTy == Ctx.getObjCIdType())
766 ? getUnarySummary(FT, cfmakecollectable)
767 : getPersistentStopSummary();
768
769 break;
770 }
771
772 if (RetTy->isPointerType()) {
773 // For CoreFoundation ('CF') types.
774 if (isRefType(RetTy, "CF", &Ctx, FName)) {
775 if (isRetain(FD, FName))
776 S = getUnarySummary(FT, cfretain);
777 else if (strstr(FName, "MakeCollectable"))
778 S = getUnarySummary(FT, cfmakecollectable);
779 else
780 S = getCFCreateGetRuleSummary(FD, FName);
781
782 break;
783 }
784
785 // For CoreGraphics ('CG') types.
786 if (isRefType(RetTy, "CG", &Ctx, FName)) {
787 if (isRetain(FD, FName))
788 S = getUnarySummary(FT, cfretain);
789 else
790 S = getCFCreateGetRuleSummary(FD, FName);
791
792 break;
793 }
794
795 // For the Disk Arbitration API (DiskArbitration/DADisk.h)
796 if (isRefType(RetTy, "DADisk") ||
797 isRefType(RetTy, "DADissenter") ||
798 isRefType(RetTy, "DASessionRef")) {
799 S = getCFCreateGetRuleSummary(FD, FName);
800 break;
801 }
802
803 break;
804 }
805
806 // Check for release functions, the only kind of functions that we care
807 // about that don't return a pointer type.
808 if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) {
809 if (isRelease(FD, FName+2))
810 S = getUnarySummary(FT, cfrelease);
811 else {
812 // For CoreFoundation and CoreGraphics functions we assume they
813 // follow the ownership idiom strictly and thus do not cause
814 // ownership to "escape".
815 assert (ScratchArgs.empty());
816 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing,
817 DoNothing);
818 }
819 }
Ted Kremenek37d785b2008-07-15 16:50:12 +0000820 }
821 while (0);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000822
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000823 FuncSummaries[FD] = S;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000824 return S;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000825}
826
Ted Kremenek37d785b2008-07-15 16:50:12 +0000827RetainSummary*
828RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
829 const char* FName) {
830
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000831 if (strstr(FName, "Create") || strstr(FName, "Copy"))
832 return getCFSummaryCreateRule(FD);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000833
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000834 if (strstr(FName, "Get"))
835 return getCFSummaryGetRule(FD);
836
837 return 0;
838}
839
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000840RetainSummary*
Ted Kremenek12619382009-01-12 21:45:02 +0000841RetainSummaryManager::getUnarySummary(FunctionType* FT, UnaryFuncKind func) {
842 // Sanity check that this is *really* a unary function. This can
843 // happen if people do weird things.
844 FunctionTypeProto* FTP = dyn_cast<FunctionTypeProto>(FT);
845 if (!FTP || FTP->getNumArgs() != 1)
846 return getPersistentStopSummary();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000847
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000848 assert (ScratchArgs.empty());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000849
Ted Kremenek377e2302008-04-29 05:33:51 +0000850 switch (func) {
Ted Kremenek12619382009-01-12 21:45:02 +0000851 case cfretain: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000852 ScratchArgs.push_back(std::make_pair(0, IncRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000853 return getPersistentSummary(RetEffect::MakeAlias(0),
854 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000855 }
856
857 case cfrelease: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000858 ScratchArgs.push_back(std::make_pair(0, DecRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000859 return getPersistentSummary(RetEffect::MakeNoRet(),
860 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000861 }
862
863 case cfmakecollectable: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000864 if (GCEnabled)
865 ScratchArgs.push_back(std::make_pair(0, DecRef));
866
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000867 return getPersistentSummary(RetEffect::MakeAlias(0),
868 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000869 }
870
871 default:
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000872 assert (false && "Not a supported unary function.");
Ted Kremenek98530452008-08-12 20:41:56 +0000873 return 0;
Ted Kremenek940b1d82008-04-10 23:44:06 +0000874 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000875}
876
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000877RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000878 assert (ScratchArgs.empty());
Ted Kremenek070a8252008-07-09 18:11:16 +0000879
880 if (FD->getIdentifier() == CFDictionaryCreateII) {
881 ScratchArgs.push_back(std::make_pair(1, DoNothingByRef));
882 ScratchArgs.push_back(std::make_pair(2, DoNothingByRef));
883 }
884
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000885 return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000886}
887
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000888RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000889 assert (ScratchArgs.empty());
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000890 return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF),
891 DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000892}
893
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000894//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000895// Summary creation for Selectors.
896//===----------------------------------------------------------------------===//
897
Ted Kremenek1bffd742008-05-06 15:44:25 +0000898RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000899RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) {
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000900 assert(ScratchArgs.empty());
901
902 RetainSummary* Summ =
Ted Kremenek9c32d082008-05-06 00:30:21 +0000903 getPersistentSummary(RetEffect::MakeReceiverAlias());
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000904
Ted Kremenek553cf182008-06-25 21:21:56 +0000905 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000906 return Summ;
907}
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000908
Ted Kremenek553cf182008-06-25 21:21:56 +0000909
Ted Kremenek1bffd742008-05-06 15:44:25 +0000910RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000911RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
912 ObjCInterfaceDecl* ID) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000913
914 Selector S = ME->getSelector();
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000915
Ted Kremenek553cf182008-06-25 21:21:56 +0000916 // Look up a summary in our summary cache.
917 ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000918
Ted Kremenek1f180c32008-06-23 22:21:20 +0000919 if (I != ObjCMethodSummaries.end())
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000920 return I->second;
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000921
Ted Kremenek234a4c22009-01-07 00:39:56 +0000922 // "initXXX": pass-through for receiver.
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000923 const char* s = S.getIdentifierInfoForSlot(0)->getName();
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000924 assert (ScratchArgs.empty());
Ted Kremenekaee9e572008-05-06 06:09:09 +0000925
Ted Kremenek0327f772008-06-02 17:14:13 +0000926 if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0)
Ted Kremenek234a4c22009-01-07 00:39:56 +0000927 return getInitMethodSummary(ME);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000928
Ted Kremenek234a4c22009-01-07 00:39:56 +0000929 // Look for methods that return an owned object.
930 if (!isTrackedObjectType(Ctx.getCanonicalType(ME->getType())))
Ted Kremenek84060db2008-05-07 04:25:59 +0000931 return 0;
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000932
Ted Kremenek234a4c22009-01-07 00:39:56 +0000933 if (followsFundamentalRule(s)) {
934 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000935 : RetEffect::MakeOwned(RetEffect::ObjC, true);
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000936 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenek553cf182008-06-25 21:21:56 +0000937 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000938 return Summ;
939 }
Ted Kremenek1bffd742008-05-06 15:44:25 +0000940
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000941 return 0;
942}
943
Ted Kremenekc8395602008-05-06 21:26:51 +0000944RetainSummary*
Ted Kremenek1f180c32008-06-23 22:21:20 +0000945RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
946 Selector S) {
Ted Kremenekc8395602008-05-06 21:26:51 +0000947
Ted Kremenek553cf182008-06-25 21:21:56 +0000948 // FIXME: Eventually we should properly do class method summaries, but
949 // it requires us being able to walk the type hierarchy. Unfortunately,
950 // we cannot do this with just an IdentifierInfo* for the class name.
951
Ted Kremenekc8395602008-05-06 21:26:51 +0000952 // Look up a summary in our cache of Selectors -> Summaries.
Ted Kremenek553cf182008-06-25 21:21:56 +0000953 ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S);
Ted Kremenekc8395602008-05-06 21:26:51 +0000954
Ted Kremenek1f180c32008-06-23 22:21:20 +0000955 if (I != ObjCClassMethodSummaries.end())
Ted Kremenekc8395602008-05-06 21:26:51 +0000956 return I->second;
957
Ted Kremeneka22cc2f2008-05-06 23:07:13 +0000958 return 0;
Ted Kremenekc8395602008-05-06 21:26:51 +0000959}
960
Ted Kremenek1f180c32008-06-23 22:21:20 +0000961void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9c32d082008-05-06 00:30:21 +0000962
963 assert (ScratchArgs.empty());
964
Ted Kremeneka7344702008-06-23 18:02:52 +0000965 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000966 : RetEffect::MakeOwned(RetEffect::ObjC, true);
Ted Kremeneka7344702008-06-23 18:02:52 +0000967
Ted Kremenek9c32d082008-05-06 00:30:21 +0000968 RetainSummary* Summ = getPersistentSummary(E);
969
Ted Kremenek553cf182008-06-25 21:21:56 +0000970 // Create the summaries for "alloc", "new", and "allocWithZone:" for
971 // NSObject and its derivatives.
972 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
973 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
974 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Ted Kremenek70a733e2008-07-18 17:24:20 +0000975
976 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek9e476de2008-08-12 18:30:56 +0000977 addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"),
Ted Kremenek2d1652e2009-01-28 05:56:51 +0000978 GetNullarySelector("currentHandler", Ctx),
979 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC)));
Ted Kremenek6d348932008-10-21 15:53:15 +0000980
981 // Create the [NSAutoreleasePool addObject:] summary.
Ted Kremenekabf43972009-01-28 21:44:40 +0000982 ScratchArgs.push_back(std::make_pair(0, Autorelease));
983 addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"),
984 GetUnarySelector("addObject", Ctx),
985 getPersistentSummary(RetEffect::MakeNoRet(),
986 DoNothing, DoNothing));
Ted Kremenek9c32d082008-05-06 00:30:21 +0000987}
988
Ted Kremenek1f180c32008-06-23 22:21:20 +0000989void RetainSummaryManager::InitializeMethodSummaries() {
Ted Kremenekb3c3c282008-05-06 00:38:54 +0000990
991 assert (ScratchArgs.empty());
992
Ted Kremenekc8395602008-05-06 21:26:51 +0000993 // Create the "init" selector. It just acts as a pass-through for the
994 // receiver.
Ted Kremenek179064e2008-07-01 17:21:27 +0000995 RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias());
996 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
Ted Kremenekc8395602008-05-06 21:26:51 +0000997
998 // The next methods are allocators.
Ted Kremeneka7344702008-06-23 18:02:52 +0000999 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001000 : RetEffect::MakeOwned(RetEffect::ObjC, true);
Ted Kremeneka7344702008-06-23 18:02:52 +00001001
Ted Kremenek179064e2008-07-01 17:21:27 +00001002 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenekc8395602008-05-06 21:26:51 +00001003
1004 // Create the "copy" selector.
Ted Kremenek98530452008-08-12 20:41:56 +00001005 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ);
1006
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001007 // Create the "mutableCopy" selector.
Ted Kremenek553cf182008-06-25 21:21:56 +00001008 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001009
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001010 // Create the "retain" selector.
1011 E = RetEffect::MakeReceiverAlias();
1012 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001013 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001014
1015 // Create the "release" selector.
1016 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001017 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Ted Kremenek299e8152008-05-07 21:17:39 +00001018
1019 // Create the "drain" selector.
1020 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001021 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001022
1023 // Create the "autorelease" selector.
Ted Kremenekabf43972009-01-28 21:44:40 +00001024 Summ = getPersistentSummary(E, Autorelease);
Ted Kremenek553cf182008-06-25 21:21:56 +00001025 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001026
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001027 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek179064e2008-07-01 17:21:27 +00001028 RetainSummary *NSWindowSumm =
1029 getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn);
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001030
1031 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1032 "styleMask", "backing", "defer", NULL);
1033
1034 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1035 "styleMask", "backing", "defer", "screen", NULL);
1036
1037 // For NSPanel (which subclasses NSWindow), allocated objects are not
1038 // self-owned.
1039 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1040 "styleMask", "backing", "defer", NULL);
1041
1042 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1043 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek553cf182008-06-25 21:21:56 +00001044
Ted Kremenek70a733e2008-07-18 17:24:20 +00001045 // Create NSAssertionHandler summaries.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001046 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
1047 "lineNumber", "description", NULL);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001048
Ted Kremenek9e476de2008-08-12 18:30:56 +00001049 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1050 "file", "lineNumber", "description", NULL);
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001051}
1052
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001053//===----------------------------------------------------------------------===//
Ted Kremenek13922612008-04-16 20:40:59 +00001054// Reference-counting logic (typestate + counts).
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001055//===----------------------------------------------------------------------===//
1056
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001057namespace {
1058
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001059class VISIBILITY_HIDDEN RefVal {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001060public:
Ted Kremenek4fd88972008-04-17 18:12:53 +00001061 enum Kind {
1062 Owned = 0, // Owning reference.
1063 NotOwned, // Reference is not owned by still valid (not freed).
1064 Released, // Object has been released.
1065 ReturnedOwned, // Returned object passes ownership to caller.
1066 ReturnedNotOwned, // Return object does not pass ownership to caller.
1067 ErrorUseAfterRelease, // Object used after released.
1068 ErrorReleaseNotOwned, // Release of an object that was not owned.
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001069 ErrorLeak, // A memory leak due to excessive reference counts.
1070 ErrorLeakReturned // A memory leak due to the returning method not having
1071 // the correct naming conventions.
Ted Kremenek4fd88972008-04-17 18:12:53 +00001072 };
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001073
1074private:
Ted Kremenek4fd88972008-04-17 18:12:53 +00001075 Kind kind;
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001076 RetEffect::ObjKind okind;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001077 unsigned Cnt;
Ted Kremenek553cf182008-06-25 21:21:56 +00001078 QualType T;
1079
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001080 RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, QualType t)
1081 : kind(k), okind(o), Cnt(cnt), T(t) {}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001082
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001083 RefVal(Kind k, unsigned cnt = 0)
1084 : kind(k), okind(RetEffect::AnyObj), Cnt(cnt) {}
1085
1086public:
Ted Kremenek4fd88972008-04-17 18:12:53 +00001087 Kind getKind() const { return kind; }
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001088
1089 RetEffect::ObjKind getObjKind() const { return okind; }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001090
Ted Kremenek553cf182008-06-25 21:21:56 +00001091 unsigned getCount() const { return Cnt; }
1092 QualType getType() const { return T; }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001093
1094 // Useful predicates.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001095
Ted Kremenek73c750b2008-03-11 18:14:09 +00001096 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
1097
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001098 static bool isLeak(Kind k) { return k >= ErrorLeak; }
Ted Kremenekdb863712008-04-16 22:32:20 +00001099
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001100 bool isOwned() const {
1101 return getKind() == Owned;
1102 }
1103
Ted Kremenekdb863712008-04-16 22:32:20 +00001104 bool isNotOwned() const {
1105 return getKind() == NotOwned;
1106 }
1107
Ted Kremenek4fd88972008-04-17 18:12:53 +00001108 bool isReturnedOwned() const {
1109 return getKind() == ReturnedOwned;
1110 }
1111
1112 bool isReturnedNotOwned() const {
1113 return getKind() == ReturnedNotOwned;
1114 }
1115
1116 bool isNonLeakError() const {
1117 Kind k = getKind();
1118 return isError(k) && !isLeak(k);
1119 }
1120
1121 // State creation: normal state.
1122
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001123 static RefVal makeOwned(RetEffect::ObjKind o, QualType t,
1124 unsigned Count = 1) {
1125 return RefVal(Owned, o, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001126 }
1127
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001128 static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t,
1129 unsigned Count = 0) {
1130 return RefVal(NotOwned, o, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001131 }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001132
1133 static RefVal makeReturnedOwned(unsigned Count) {
1134 return RefVal(ReturnedOwned, Count);
1135 }
1136
1137 static RefVal makeReturnedNotOwned() {
1138 return RefVal(ReturnedNotOwned);
1139 }
1140
Ted Kremenek4fd88972008-04-17 18:12:53 +00001141 // Comparison, profiling, and pretty-printing.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001142
Ted Kremenek4fd88972008-04-17 18:12:53 +00001143 bool operator==(const RefVal& X) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001144 return kind == X.kind && Cnt == X.Cnt && T == X.T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001145 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001146
Ted Kremenek553cf182008-06-25 21:21:56 +00001147 RefVal operator-(size_t i) const {
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001148 return RefVal(getKind(), getObjKind(), getCount() - i, getType());
Ted Kremenek553cf182008-06-25 21:21:56 +00001149 }
1150
1151 RefVal operator+(size_t i) const {
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001152 return RefVal(getKind(), getObjKind(), getCount() + i, getType());
Ted Kremenek553cf182008-06-25 21:21:56 +00001153 }
1154
1155 RefVal operator^(Kind k) const {
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001156 return RefVal(k, getObjKind(), getCount(), getType());
Ted Kremenek553cf182008-06-25 21:21:56 +00001157 }
Ted Kremenek553cf182008-06-25 21:21:56 +00001158
Ted Kremenek4fd88972008-04-17 18:12:53 +00001159 void Profile(llvm::FoldingSetNodeID& ID) const {
1160 ID.AddInteger((unsigned) kind);
1161 ID.AddInteger(Cnt);
Ted Kremenek553cf182008-06-25 21:21:56 +00001162 ID.Add(T);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001163 }
1164
Ted Kremenekf3948042008-03-11 19:44:10 +00001165 void print(std::ostream& Out) const;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001166};
Ted Kremenekf3948042008-03-11 19:44:10 +00001167
1168void RefVal::print(std::ostream& Out) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001169 if (!T.isNull())
1170 Out << "Tracked Type:" << T.getAsString() << '\n';
1171
Ted Kremenekf3948042008-03-11 19:44:10 +00001172 switch (getKind()) {
1173 default: assert(false);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001174 case Owned: {
1175 Out << "Owned";
1176 unsigned cnt = getCount();
1177 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001178 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001179 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001180
Ted Kremenek61b9f872008-04-10 23:09:18 +00001181 case NotOwned: {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001182 Out << "NotOwned";
Ted Kremenek61b9f872008-04-10 23:09:18 +00001183 unsigned cnt = getCount();
1184 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001185 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001186 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001187
Ted Kremenek4fd88972008-04-17 18:12:53 +00001188 case ReturnedOwned: {
1189 Out << "ReturnedOwned";
1190 unsigned cnt = getCount();
1191 if (cnt) Out << " (+ " << cnt << ")";
1192 break;
1193 }
1194
1195 case ReturnedNotOwned: {
1196 Out << "ReturnedNotOwned";
1197 unsigned cnt = getCount();
1198 if (cnt) Out << " (+ " << cnt << ")";
1199 break;
1200 }
1201
Ted Kremenekf3948042008-03-11 19:44:10 +00001202 case Released:
1203 Out << "Released";
1204 break;
1205
Ted Kremenekdb863712008-04-16 22:32:20 +00001206 case ErrorLeak:
1207 Out << "Leaked";
1208 break;
1209
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001210 case ErrorLeakReturned:
1211 Out << "Leaked (Bad naming)";
1212 break;
1213
Ted Kremenekf3948042008-03-11 19:44:10 +00001214 case ErrorUseAfterRelease:
1215 Out << "Use-After-Release [ERROR]";
1216 break;
1217
1218 case ErrorReleaseNotOwned:
1219 Out << "Release of Not-Owned [ERROR]";
1220 break;
1221 }
1222}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001223
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001224} // end anonymous namespace
1225
1226//===----------------------------------------------------------------------===//
1227// RefBindings - State used to track object reference counts.
1228//===----------------------------------------------------------------------===//
1229
Ted Kremenek2dabd432008-12-05 02:27:51 +00001230typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001231static int RefBIndex = 0;
1232
1233namespace clang {
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001234 template<>
1235 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
1236 static inline void* GDMIndex() { return &RefBIndex; }
1237 };
1238}
Ted Kremenek6d348932008-10-21 15:53:15 +00001239
1240//===----------------------------------------------------------------------===//
1241// ARBindings - State used to track objects in autorelease pools.
1242//===----------------------------------------------------------------------===//
1243
Ted Kremenek2dabd432008-12-05 02:27:51 +00001244typedef llvm::ImmutableSet<SymbolRef> ARPoolContents;
1245typedef llvm::ImmutableList< std::pair<SymbolRef, ARPoolContents*> > ARBindings;
Ted Kremenek6d348932008-10-21 15:53:15 +00001246static int AutoRBIndex = 0;
1247
1248namespace clang {
1249 template<>
1250 struct GRStateTrait<ARBindings> : public GRStatePartialTrait<ARBindings> {
1251 static inline void* GDMIndex() { return &AutoRBIndex; }
1252 };
1253}
1254
Ted Kremenek13922612008-04-16 20:40:59 +00001255//===----------------------------------------------------------------------===//
1256// Transfer functions.
1257//===----------------------------------------------------------------------===//
1258
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001259namespace {
1260
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001261class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenek8dd56462008-04-18 03:39:05 +00001262public:
Ted Kremenek553cf182008-06-25 21:21:56 +00001263 // Type definitions.
Ted Kremenek2dabd432008-12-05 02:27:51 +00001264 typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolRef> >
Ted Kremenek8dd56462008-04-18 03:39:05 +00001265 ReleasesNotOwnedTy;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001266
Ted Kremenek8dd56462008-04-18 03:39:05 +00001267 typedef ReleasesNotOwnedTy UseAfterReleasesTy;
1268
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001269 typedef llvm::DenseMap<GRExprEngine::NodeTy*,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001270 std::vector<std::pair<SymbolRef,bool> >*>
Ted Kremenekdb863712008-04-16 22:32:20 +00001271 LeaksTy;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001272
Ted Kremenekae6814e2008-08-13 21:24:49 +00001273 class BindingsPrinter : public GRState::Printer {
Ted Kremenekf3948042008-03-11 19:44:10 +00001274 public:
Ted Kremenekae6814e2008-08-13 21:24:49 +00001275 virtual void Print(std::ostream& Out, const GRState* state,
1276 const char* nl, const char* sep);
Ted Kremenekf3948042008-03-11 19:44:10 +00001277 };
Ted Kremenek8dd56462008-04-18 03:39:05 +00001278
1279private:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001280 RetainSummaryManager Summaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001281 const LangOptions& LOpts;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001282
Ted Kremenek9e476de2008-08-12 18:30:56 +00001283 UseAfterReleasesTy UseAfterReleases;
1284 ReleasesNotOwnedTy ReleasesNotOwned;
1285 LeaksTy Leaks;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001286
Ted Kremenek2dabd432008-12-05 02:27:51 +00001287 RefBindings Update(RefBindings B, SymbolRef sym, RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001288 RefVal::Kind& hasErr, RefBindings::Factory& RefBFactory);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001289
Ted Kremenek2dabd432008-12-05 02:27:51 +00001290 RefVal::Kind& Update(GRStateRef& state, SymbolRef sym, RefVal V,
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001291 ArgEffect E, RefVal::Kind& hasErr) {
1292
1293 state = state.set<RefBindings>(Update(state.get<RefBindings>(), sym, V,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001294 E, hasErr,
1295 state.get_context<RefBindings>()));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001296 return hasErr;
1297 }
1298
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001299 void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1300 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001301 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001302 ExplodedNode<GRState>* Pred,
1303 const GRState* St,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001304 RefVal::Kind hasErr, SymbolRef Sym);
Ted Kremenekdb863712008-04-16 22:32:20 +00001305
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001306 std::pair<GRStateRef, bool>
1307 HandleSymbolDeath(GRStateManager& VMgr, const GRState* St,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001308 const Decl* CD, SymbolRef sid, RefVal V, bool& hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001309
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001310public:
Ted Kremenek13922612008-04-16 20:40:59 +00001311
Ted Kremenek78d46242008-07-22 16:21:24 +00001312 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek377e2302008-04-29 05:33:51 +00001313 : Summaries(Ctx, gcenabled),
Ted Kremenek9e476de2008-08-12 18:30:56 +00001314 LOpts(lopts) {}
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001315
Ted Kremenek8dd56462008-04-18 03:39:05 +00001316 virtual ~CFRefCount() {
1317 for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I)
1318 delete I->second;
1319 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001320
1321 virtual void RegisterChecks(GRExprEngine& Eng);
Ted Kremenekf3948042008-03-11 19:44:10 +00001322
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001323 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1324 Printers.push_back(new BindingsPrinter());
Ted Kremenekf3948042008-03-11 19:44:10 +00001325 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001326
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001327 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenek072192b2008-04-30 23:47:44 +00001328 const LangOptions& getLangOptions() const { return LOpts; }
1329
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001330 // Calls.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001331
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001332 void EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001333 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001334 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001335 Expr* Ex,
1336 Expr* Receiver,
1337 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001338 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001339 ExplodedNode<GRState>* Pred);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001340
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001341 virtual void EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek199e1a02008-03-12 21:06:49 +00001342 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001343 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001344 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001345 ExplodedNode<GRState>* Pred);
Ted Kremenekfa34b332008-04-09 01:10:13 +00001346
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001347
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001348 virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001349 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001350 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001351 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001352 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001353
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001354 bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001355 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001356 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001357 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001358 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001359
Ted Kremenek13922612008-04-16 20:40:59 +00001360 // Stores.
1361
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001362 virtual void EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001363 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001364 GRStmtNodeBuilder<GRState>& Builder,
1365 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001366 const GRState* St, SVal TargetLV, SVal Val);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001367 // End-of-path.
1368
1369 virtual void EvalEndPath(GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001370 GREndPathNodeBuilder<GRState>& Builder);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001371
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001372 virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001373 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001374 GRStmtNodeBuilder<GRState>& Builder,
1375 ExplodedNode<GRState>* Pred,
Ted Kremenek241677a2009-01-21 22:26:05 +00001376 Stmt* S, const GRState* state,
1377 SymbolReaper& SymReaper);
1378
Ted Kremenek4fd88972008-04-17 18:12:53 +00001379 // Return statements.
1380
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001381 virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001382 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001383 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001384 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001385 ExplodedNode<GRState>* Pred);
Ted Kremenekcb612922008-04-18 19:23:43 +00001386
1387 // Assumptions.
1388
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001389 virtual const GRState* EvalAssume(GRStateManager& VMgr,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001390 const GRState* St, SVal Cond,
Ted Kremenek4323a572008-07-10 22:03:41 +00001391 bool Assumption, bool& isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00001392
Ted Kremenekfa34b332008-04-09 01:10:13 +00001393 // Error iterators.
1394
1395 typedef UseAfterReleasesTy::iterator use_after_iterator;
1396 typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
Ted Kremenek989d5192008-04-17 23:43:50 +00001397 typedef LeaksTy::iterator leaks_iterator;
Ted Kremenekfa34b332008-04-09 01:10:13 +00001398
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001399 use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
1400 use_after_iterator use_after_end() { return UseAfterReleases.end(); }
Ted Kremenekfa34b332008-04-09 01:10:13 +00001401
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001402 bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
1403 bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
Ted Kremenek989d5192008-04-17 23:43:50 +00001404
1405 leaks_iterator leaks_begin() { return Leaks.begin(); }
1406 leaks_iterator leaks_end() { return Leaks.end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001407};
1408
1409} // end anonymous namespace
1410
Ted Kremenek8dd56462008-04-18 03:39:05 +00001411
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001412
1413
Ted Kremenekae6814e2008-08-13 21:24:49 +00001414void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state,
1415 const char* nl, const char* sep) {
1416
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001417 RefBindings B = state->get<RefBindings>();
Ted Kremenekf3948042008-03-11 19:44:10 +00001418
Ted Kremenekae6814e2008-08-13 21:24:49 +00001419 if (!B.isEmpty())
Ted Kremenekf3948042008-03-11 19:44:10 +00001420 Out << sep << nl;
1421
1422 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1423 Out << (*I).first << " : ";
1424 (*I).second.print(Out);
1425 Out << nl;
1426 }
1427}
1428
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001429static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) {
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001430 return Summ ? Summ->getArg(idx) : MayEscape;
Ted Kremenekf9561e52008-04-11 20:23:24 +00001431}
1432
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001433static inline RetEffect GetRetEffect(RetainSummary* Summ) {
1434 return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
Ted Kremenekf9561e52008-04-11 20:23:24 +00001435}
1436
Ted Kremenek14993892008-05-06 02:41:27 +00001437static inline ArgEffect GetReceiverE(RetainSummary* Summ) {
1438 return Summ ? Summ->getReceiverEffect() : DoNothing;
1439}
1440
Ted Kremenek70a733e2008-07-18 17:24:20 +00001441static inline bool IsEndPath(RetainSummary* Summ) {
1442 return Summ ? Summ->isEndPath() : false;
1443}
1444
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001445void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1446 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001447 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001448 ExplodedNode<GRState>* Pred,
1449 const GRState* St,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001450 RefVal::Kind hasErr, SymbolRef Sym) {
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001451 Builder.BuildSinks = true;
1452 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
1453
1454 if (!N) return;
1455
1456 switch (hasErr) {
1457 default: assert(false);
1458 case RefVal::ErrorUseAfterRelease:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001459 UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001460 break;
1461
1462 case RefVal::ErrorReleaseNotOwned:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001463 ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001464 break;
1465 }
1466}
1467
Ted Kremenek553cf182008-06-25 21:21:56 +00001468/// GetReturnType - Used to get the return type of a message expression or
1469/// function call with the intention of affixing that type to a tracked symbol.
1470/// While the the return type can be queried directly from RetEx, when
1471/// invoking class methods we augment to the return type to be that of
1472/// a pointer to the class (as opposed it just being id).
1473static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) {
1474
1475 QualType RetTy = RetE->getType();
1476
1477 // FIXME: We aren't handling id<...>.
Chris Lattner8b51fd72008-07-26 22:36:27 +00001478 const PointerType* PT = RetTy->getAsPointerType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001479 if (!PT)
1480 return RetTy;
1481
1482 // If RetEx is not a message expression just return its type.
1483 // If RetEx is a message expression, return its types if it is something
1484 /// more specific than id.
1485
1486 ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE);
1487
1488 if (!ME || !Ctx.isObjCIdType(PT->getPointeeType()))
1489 return RetTy;
1490
1491 ObjCInterfaceDecl* D = ME->getClassInfo().first;
1492
1493 // At this point we know the return type of the message expression is id.
1494 // If we have an ObjCInterceDecl, we know this is a call to a class method
1495 // whose type we can resolve. In such cases, promote the return type to
1496 // Class*.
1497 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
1498}
1499
1500
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001501void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001502 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001503 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001504 Expr* Ex,
1505 Expr* Receiver,
1506 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001507 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001508 ExplodedNode<GRState>* Pred) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001509
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001510 // Get the state.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001511 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001512 ASTContext& Ctx = Eng.getStateManager().getContext();
Ted Kremenek14993892008-05-06 02:41:27 +00001513
1514 // Evaluate the effect of the arguments.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001515 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001516 unsigned idx = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001517 Expr* ErrorExpr = NULL;
Ted Kremenek2dabd432008-12-05 02:27:51 +00001518 SymbolRef ErrorSym = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001519
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001520 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001521 SVal V = state.GetSVal(*I);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001522
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001523 if (isa<loc::SymbolVal>(V)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001524 SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001525 if (RefBindings::data_type* T = state.get<RefBindings>(Sym))
1526 if (Update(state, Sym, *T, GetArgE(Summ, idx), hasErr)) {
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001527 ErrorExpr = *I;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001528 ErrorSym = Sym;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001529 break;
1530 }
Ted Kremenekb8873552008-04-11 20:51:02 +00001531 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001532 else if (isa<Loc>(V)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001533 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenek070a8252008-07-09 18:11:16 +00001534
1535 if (GetArgE(Summ, idx) == DoNothingByRef)
1536 continue;
1537
1538 // Invalidate the value of the variable passed by reference.
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001539
1540 // FIXME: Either this logic should also be replicated in GRSimpleVals
1541 // or should be pulled into a separate "constraint engine."
Ted Kremenek070a8252008-07-09 18:11:16 +00001542
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001543 // FIXME: We can have collisions on the conjured symbol if the
1544 // expression *I also creates conjured symbols. We probably want
1545 // to identify conjured symbols by an expression pair: the enclosing
1546 // expression (the context) and the expression itself. This should
Ted Kremenek070a8252008-07-09 18:11:16 +00001547 // disambiguate conjured symbols.
Ted Kremenek9e240492008-10-04 05:50:14 +00001548
Ted Kremenek993f1c72008-10-17 20:28:54 +00001549 const TypedRegion* R = dyn_cast<TypedRegion>(MR->getRegion());
Ted Kremenek90b32362008-12-17 19:42:34 +00001550
1551 // Blast through AnonTypedRegions to get the original region type.
1552 while (R) {
1553 const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R);
1554 if (!ATR) break;
1555 R = dyn_cast<TypedRegion>(ATR->getSuperRegion());
1556 }
1557
Ted Kremenek9e240492008-10-04 05:50:14 +00001558 if (R) {
Ted Kremenek40e86d92008-12-18 23:34:57 +00001559
1560 // Is the invalidated variable something that we were tracking?
1561 SVal X = state.GetSVal(Loc::MakeVal(R));
1562
1563 if (isa<loc::SymbolVal>(X)) {
1564 SymbolRef Sym = cast<loc::SymbolVal>(X).getSymbol();
1565 state = state.remove<RefBindings>(Sym);
1566 }
1567
Ted Kremenek9e240492008-10-04 05:50:14 +00001568 // Set the value of the variable to be a conjured symbol.
1569 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek6eddeb12008-12-13 21:49:13 +00001570 QualType T = R->getRValueType(Ctx);
Ted Kremenek9e240492008-10-04 05:50:14 +00001571
Ted Kremenekfd301942008-10-17 22:23:12 +00001572 // FIXME: handle structs.
Ted Kremenek062e2f92008-11-13 06:10:40 +00001573 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001574 SymbolRef NewSym =
Ted Kremenekfd301942008-10-17 22:23:12 +00001575 Eng.getSymbolManager().getConjuredSymbol(*I, T, Count);
1576
Ted Kremenek90b32362008-12-17 19:42:34 +00001577 state = state.BindLoc(Loc::MakeVal(R),
Ted Kremenekfd301942008-10-17 22:23:12 +00001578 Loc::IsLocType(T)
1579 ? cast<SVal>(loc::SymbolVal(NewSym))
1580 : cast<SVal>(nonloc::SymbolVal(NewSym)));
1581 }
1582 else {
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001583 state = state.BindLoc(*MR, UnknownVal());
Ted Kremenekfd301942008-10-17 22:23:12 +00001584 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001585 }
1586 else
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001587 state = state.BindLoc(*MR, UnknownVal());
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001588 }
1589 else {
1590 // Nuke all other arguments passed by reference.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001591 state = state.Unbind(cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001592 }
Ted Kremenekb8873552008-04-11 20:51:02 +00001593 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001594 else if (isa<nonloc::LocAsInteger>(V))
1595 state = state.Unbind(cast<nonloc::LocAsInteger>(V).getLoc());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001596 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001597
Ted Kremenek553cf182008-06-25 21:21:56 +00001598 // Evaluate the effect on the message receiver.
Ted Kremenek14993892008-05-06 02:41:27 +00001599 if (!ErrorExpr && Receiver) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001600 SVal V = state.GetSVal(Receiver);
1601 if (isa<loc::SymbolVal>(V)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001602 SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001603 if (const RefVal* T = state.get<RefBindings>(Sym))
1604 if (Update(state, Sym, *T, GetReceiverE(Summ), hasErr)) {
Ted Kremenek14993892008-05-06 02:41:27 +00001605 ErrorExpr = Receiver;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001606 ErrorSym = Sym;
Ted Kremenek14993892008-05-06 02:41:27 +00001607 }
Ted Kremenek14993892008-05-06 02:41:27 +00001608 }
1609 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001610
Ted Kremenek553cf182008-06-25 21:21:56 +00001611 // Process any errors.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001612 if (hasErr) {
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001613 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001614 hasErr, ErrorSym);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001615 return;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001616 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001617
Ted Kremenek70a733e2008-07-18 17:24:20 +00001618 // Consult the summary for the return value.
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001619 RetEffect RE = GetRetEffect(Summ);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001620
1621 switch (RE.getKind()) {
1622 default:
1623 assert (false && "Unhandled RetEffect."); break;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001624
Ted Kremenekfd301942008-10-17 22:23:12 +00001625 case RetEffect::NoRet: {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001626
Ted Kremenekf9561e52008-04-11 20:23:24 +00001627 // Make up a symbol for the return value (not reference counted).
Ted Kremenekb8873552008-04-11 20:51:02 +00001628 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
1629 // should compose behavior, not copy it.
Ted Kremenekf9561e52008-04-11 20:23:24 +00001630
Ted Kremenekfd301942008-10-17 22:23:12 +00001631 // FIXME: We eventually should handle structs and other compound types
1632 // that are returned by value.
1633
1634 QualType T = Ex->getType();
1635
Ted Kremenek062e2f92008-11-13 06:10:40 +00001636 if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) {
Ted Kremenekf9561e52008-04-11 20:23:24 +00001637 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00001638 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001639
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001640 SVal X = Loc::IsLocType(Ex->getType())
1641 ? cast<SVal>(loc::SymbolVal(Sym))
1642 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenekf9561e52008-04-11 20:23:24 +00001643
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001644 state = state.BindExpr(Ex, X, false);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001645 }
1646
Ted Kremenek940b1d82008-04-10 23:44:06 +00001647 break;
Ted Kremenekfd301942008-10-17 22:23:12 +00001648 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00001649
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001650 case RetEffect::Alias: {
Ted Kremenek553cf182008-06-25 21:21:56 +00001651 unsigned idx = RE.getIndex();
Ted Kremenek55499762008-06-17 02:43:46 +00001652 assert (arg_end >= arg_beg);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001653 assert (idx < (unsigned) (arg_end - arg_beg));
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001654 SVal V = state.GetSVal(*(arg_beg+idx));
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001655 state = state.BindExpr(Ex, V, false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001656 break;
1657 }
1658
Ted Kremenek14993892008-05-06 02:41:27 +00001659 case RetEffect::ReceiverAlias: {
1660 assert (Receiver);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001661 SVal V = state.GetSVal(Receiver);
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001662 state = state.BindExpr(Ex, V, false);
Ted Kremenek14993892008-05-06 02:41:27 +00001663 break;
1664 }
1665
Ted Kremeneka7344702008-06-23 18:02:52 +00001666 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001667 case RetEffect::OwnedSymbol: {
1668 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00001669 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001670 QualType RetT = GetReturnType(Ex, Eng.getContext());
1671 state =
1672 state.set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(), RetT));
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001673 state = state.BindExpr(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001674
1675#if 0
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001676 RefBindings B = GetRefBindings(StImpl);
Ted Kremenek553cf182008-06-25 21:21:56 +00001677 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(RetT)));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001678#endif
1679
Ted Kremeneka7344702008-06-23 18:02:52 +00001680 // FIXME: Add a flag to the checker where allocations are allowed to fail.
1681 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol)
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001682 state = state.AddNE(Sym, Eng.getBasicVals().getZeroWithPtrWidth());
Ted Kremeneka7344702008-06-23 18:02:52 +00001683
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001684 break;
1685 }
1686
1687 case RetEffect::NotOwnedSymbol: {
1688 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenek2dabd432008-12-05 02:27:51 +00001689 SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001690 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001691
Ted Kremenek2d1652e2009-01-28 05:56:51 +00001692 state =
1693 state.set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),RetT));
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001694 state = state.BindExpr(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001695 break;
1696 }
1697 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001698
Ted Kremenek70a733e2008-07-18 17:24:20 +00001699 // Is this a sink?
1700 if (IsEndPath(Summ))
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001701 Builder.MakeSinkNode(Dst, Ex, Pred, state);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001702 else
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001703 Builder.MakeNode(Dst, Ex, Pred, state);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001704}
1705
1706
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001707void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001708 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001709 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001710 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001711 ExplodedNode<GRState>* Pred) {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001712
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001713 RetainSummary* Summ = !isa<loc::FuncVal>(L) ? 0
1714 : Summaries.getSummary(cast<loc::FuncVal>(L).getDecl());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001715
1716 EvalSummary(Dst, Eng, Builder, CE, 0, Summ,
1717 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenek2fff37e2008-03-06 00:08:09 +00001718}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001719
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001720void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001721 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001722 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001723 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001724 ExplodedNode<GRState>* Pred) {
Ted Kremenekb3095252008-05-06 04:20:12 +00001725 RetainSummary* Summ;
Ted Kremenek9040c652008-05-01 21:31:50 +00001726
Ted Kremenek553cf182008-06-25 21:21:56 +00001727 if (Expr* Receiver = ME->getReceiver()) {
1728 // We need the type-information of the tracked receiver object
1729 // Retrieve it from the state.
1730 ObjCInterfaceDecl* ID = 0;
1731
1732 // FIXME: Wouldn't it be great if this code could be reduced? It's just
1733 // a chain of lookups.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001734 const GRState* St = Builder.GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001735 SVal V = Eng.getStateManager().GetSVal(St, Receiver );
Ted Kremenek553cf182008-06-25 21:21:56 +00001736
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001737 if (isa<loc::SymbolVal>(V)) {
Ted Kremenek2dabd432008-12-05 02:27:51 +00001738 SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek553cf182008-06-25 21:21:56 +00001739
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001740 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001741 QualType Ty = T->getType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001742
1743 if (const PointerType* PT = Ty->getAsPointerType()) {
1744 QualType PointeeTy = PT->getPointeeType();
1745
1746 if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy))
1747 ID = IT->getDecl();
1748 }
1749 }
1750 }
1751
1752 Summ = Summaries.getMethodSummary(ME, ID);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001753
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001754 // Special-case: are we sending a mesage to "self"?
1755 // This is a hack. When we have full-IP this should be removed.
1756 if (!Summ) {
1757 ObjCMethodDecl* MD =
1758 dyn_cast<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl());
1759
1760 if (MD) {
1761 if (Expr* Receiver = ME->getReceiver()) {
1762 SVal X = Eng.getStateManager().GetSVal(St, Receiver);
1763 if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X))
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001764 if (L->getRegion() == Eng.getStateManager().getSelfRegion(St)) {
1765 // Create a summmary where all of the arguments "StopTracking".
1766 Summ = Summaries.getPersistentSummary(RetEffect::MakeNoRet(),
1767 DoNothing,
1768 StopTracking);
1769 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001770 }
1771 }
1772 }
Ted Kremenek553cf182008-06-25 21:21:56 +00001773 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001774 else
Ted Kremenek1f180c32008-06-23 22:21:20 +00001775 Summ = Summaries.getClassMethodSummary(ME->getClassName(),
1776 ME->getSelector());
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001777
Ted Kremenekb3095252008-05-06 04:20:12 +00001778 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ,
1779 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001780}
Ted Kremenekb3095252008-05-06 04:20:12 +00001781
Ted Kremenek13922612008-04-16 20:40:59 +00001782// Stores.
1783
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001784void CFRefCount::EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001785 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001786 GRStmtNodeBuilder<GRState>& Builder,
1787 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001788 const GRState* St, SVal TargetLV, SVal Val) {
Ted Kremenek13922612008-04-16 20:40:59 +00001789
1790 // Check if we have a binding for "Val" and if we are storing it to something
1791 // we don't understand or otherwise the value "escapes" the function.
1792
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001793 if (!isa<loc::SymbolVal>(Val))
Ted Kremenek13922612008-04-16 20:40:59 +00001794 return;
1795
1796 // Are we storing to something that causes the value to "escape"?
1797
1798 bool escapes = false;
1799
Ted Kremeneka496d162008-10-18 03:49:51 +00001800 // A value escapes in three possible cases (this may change):
1801 //
1802 // (1) we are binding to something that is not a memory region.
1803 // (2) we are binding to a memregion that does not have stack storage
1804 // (3) we are binding to a memregion with stack storage that the store
1805 // does not understand.
1806
Ted Kremenek2dabd432008-12-05 02:27:51 +00001807 SymbolRef Sym = cast<loc::SymbolVal>(Val).getSymbol();
Ted Kremeneka496d162008-10-18 03:49:51 +00001808 GRStateRef state(St, Eng.getStateManager());
1809
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001810 if (!isa<loc::MemRegionVal>(TargetLV))
Ted Kremenek13922612008-04-16 20:40:59 +00001811 escapes = true;
Ted Kremenek9e240492008-10-04 05:50:14 +00001812 else {
Ted Kremenek993f1c72008-10-17 20:28:54 +00001813 const MemRegion* R = cast<loc::MemRegionVal>(TargetLV).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00001814 escapes = !Eng.getStateManager().hasStackStorage(R);
Ted Kremeneka496d162008-10-18 03:49:51 +00001815
1816 if (!escapes) {
1817 // To test (3), generate a new state with the binding removed. If it is
1818 // the same state, then it escapes (since the store cannot represent
1819 // the binding).
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001820 GRStateRef stateNew = state.BindLoc(cast<Loc>(TargetLV), Val);
Ted Kremeneka496d162008-10-18 03:49:51 +00001821 escapes = (stateNew == state);
1822 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001823 }
Ted Kremenek13922612008-04-16 20:40:59 +00001824
1825 if (!escapes)
1826 return;
Ted Kremeneka496d162008-10-18 03:49:51 +00001827
1828 // Do we have a reference count binding?
1829 // FIXME: Is this step even needed? We do blow away the binding anyway.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001830 if (!state.get<RefBindings>(Sym))
Ted Kremenek13922612008-04-16 20:40:59 +00001831 return;
1832
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001833 // Nuke the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001834 state = state.remove<RefBindings>(Sym);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001835
Ted Kremenek13922612008-04-16 20:40:59 +00001836 // Hand of the remaining logic to the parent implementation.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001837 GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, state, TargetLV, Val);
Ted Kremenekdb863712008-04-16 22:32:20 +00001838}
1839
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001840// End-of-path.
1841
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001842
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001843std::pair<GRStateRef,bool>
1844CFRefCount::HandleSymbolDeath(GRStateManager& VMgr,
1845 const GRState* St, const Decl* CD,
Ted Kremenek2dabd432008-12-05 02:27:51 +00001846 SymbolRef sid,
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001847 RefVal V, bool& hasLeak) {
Ted Kremenekdb863712008-04-16 22:32:20 +00001848
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001849 GRStateRef state(St, VMgr);
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001850 assert ((!V.isReturnedOwned() || CD) &&
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001851 "CodeDecl must be available for reporting ReturnOwned errors.");
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001852
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001853 if (V.isReturnedOwned() && V.getCount() == 0)
1854 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
Chris Lattner077bf5e2008-11-24 03:33:13 +00001855 std::string s = MD->getSelector().getAsString();
Ted Kremenek4c79e552008-11-05 16:54:44 +00001856 if (!followsReturnRule(s.c_str())) {
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001857 hasLeak = true;
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001858 state = state.set<RefBindings>(sid, V ^ RefVal::ErrorLeakReturned);
1859 return std::make_pair(state, true);
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001860 }
1861 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001862
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001863 // All other cases.
1864
1865 hasLeak = V.isOwned() ||
1866 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001867
Ted Kremenekdb863712008-04-16 22:32:20 +00001868 if (!hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001869 return std::make_pair(state.remove<RefBindings>(sid), false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001870
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001871 return std::make_pair(state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak),
1872 false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001873}
1874
1875void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001876 GREndPathNodeBuilder<GRState>& Builder) {
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001877
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001878 const GRState* St = Builder.getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001879 RefBindings B = St->get<RefBindings>();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001880
Ted Kremenek2dabd432008-12-05 02:27:51 +00001881 llvm::SmallVector<std::pair<SymbolRef, bool>, 10> Leaked;
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001882 const Decl* CodeDecl = &Eng.getGraph().getCodeDecl();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001883
Ted Kremenekdb863712008-04-16 22:32:20 +00001884 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1885 bool hasLeak = false;
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001886
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001887 std::pair<GRStateRef, bool> X =
1888 HandleSymbolDeath(Eng.getStateManager(), St, CodeDecl,
1889 (*I).first, (*I).second, hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001890
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001891 St = X.first;
1892 if (hasLeak) Leaked.push_back(std::make_pair((*I).first, X.second));
Ted Kremenekdb863712008-04-16 22:32:20 +00001893 }
Ted Kremenek652adc62008-04-24 23:57:27 +00001894
1895 if (Leaked.empty())
1896 return;
1897
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001898 ExplodedNode<GRState>* N = Builder.MakeNode(St);
Ted Kremenek4f285152008-04-18 16:30:14 +00001899
Ted Kremenek652adc62008-04-24 23:57:27 +00001900 if (!N)
Ted Kremenek4f285152008-04-18 16:30:14 +00001901 return;
Ted Kremenekcb612922008-04-18 19:23:43 +00001902
Ted Kremenek2dabd432008-12-05 02:27:51 +00001903 std::vector<std::pair<SymbolRef,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek8dd56462008-04-18 03:39:05 +00001904 assert (!LeaksAtNode);
Ted Kremenek2dabd432008-12-05 02:27:51 +00001905 LeaksAtNode = new std::vector<std::pair<SymbolRef,bool> >();
Ted Kremenekdb863712008-04-16 22:32:20 +00001906
Ted Kremenek2dabd432008-12-05 02:27:51 +00001907 for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001908 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek8dd56462008-04-18 03:39:05 +00001909 (*LeaksAtNode).push_back(*I);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001910}
1911
Ted Kremenek652adc62008-04-24 23:57:27 +00001912// Dead symbols.
1913
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001914void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001915 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001916 GRStmtNodeBuilder<GRState>& Builder,
1917 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001918 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001919 const GRState* St,
Ted Kremenek241677a2009-01-21 22:26:05 +00001920 SymbolReaper& SymReaper) {
Ted Kremenek910e9992008-04-25 01:25:15 +00001921
Ted Kremenek652adc62008-04-24 23:57:27 +00001922 // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
1923
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001924 RefBindings B = St->get<RefBindings>();
Ted Kremenek2dabd432008-12-05 02:27:51 +00001925 llvm::SmallVector<std::pair<SymbolRef,bool>, 10> Leaked;
Ted Kremenek652adc62008-04-24 23:57:27 +00001926
Ted Kremenek241677a2009-01-21 22:26:05 +00001927 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
1928 E = SymReaper.dead_end(); I != E; ++I) {
Ted Kremenek652adc62008-04-24 23:57:27 +00001929
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001930 const RefVal* T = B.lookup(*I);
Ted Kremenek241677a2009-01-21 22:26:05 +00001931 if (!T) continue;
Ted Kremenek652adc62008-04-24 23:57:27 +00001932
1933 bool hasLeak = false;
1934
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001935 std::pair<GRStateRef, bool> X
1936 = HandleSymbolDeath(Eng.getStateManager(), St, 0, *I, *T, hasLeak);
1937
1938 St = X.first;
Ted Kremenek652adc62008-04-24 23:57:27 +00001939
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001940 if (hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001941 Leaked.push_back(std::make_pair(*I,X.second));
Ted Kremenek652adc62008-04-24 23:57:27 +00001942 }
1943
1944 if (Leaked.empty())
1945 return;
1946
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001947 ExplodedNode<GRState>* N = Builder.MakeNode(Dst, S, Pred, St);
Ted Kremenek652adc62008-04-24 23:57:27 +00001948
1949 if (!N)
1950 return;
1951
Ted Kremenek2dabd432008-12-05 02:27:51 +00001952 std::vector<std::pair<SymbolRef,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek652adc62008-04-24 23:57:27 +00001953 assert (!LeaksAtNode);
Ted Kremenek2dabd432008-12-05 02:27:51 +00001954 LeaksAtNode = new std::vector<std::pair<SymbolRef,bool> >();
Ted Kremenek652adc62008-04-24 23:57:27 +00001955
Ted Kremenek2dabd432008-12-05 02:27:51 +00001956 for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001957 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek652adc62008-04-24 23:57:27 +00001958 (*LeaksAtNode).push_back(*I);
1959}
1960
Ted Kremenek4fd88972008-04-17 18:12:53 +00001961 // Return statements.
1962
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001963void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001964 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001965 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001966 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001967 ExplodedNode<GRState>* Pred) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001968
1969 Expr* RetE = S->getRetValue();
1970 if (!RetE) return;
1971
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001972 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001973 SVal V = state.GetSVal(RetE);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001974
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001975 if (!isa<loc::SymbolVal>(V))
Ted Kremenek4fd88972008-04-17 18:12:53 +00001976 return;
1977
1978 // Get the reference count binding (if any).
Ted Kremenek2dabd432008-12-05 02:27:51 +00001979 SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001980 const RefVal* T = state.get<RefBindings>(Sym);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001981
1982 if (!T)
1983 return;
1984
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001985 // Change the reference count.
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001986 RefVal X = *T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001987
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001988 switch (X.getKind()) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001989 case RefVal::Owned: {
1990 unsigned cnt = X.getCount();
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001991 assert (cnt > 0);
1992 X = RefVal::makeReturnedOwned(cnt - 1);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001993 break;
1994 }
1995
1996 case RefVal::NotOwned: {
1997 unsigned cnt = X.getCount();
1998 X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
1999 : RefVal::makeReturnedNotOwned();
2000 break;
2001 }
2002
2003 default:
Ted Kremenek4fd88972008-04-17 18:12:53 +00002004 return;
2005 }
2006
2007 // Update the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002008 state = state.set<RefBindings>(Sym, X);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002009 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002010}
2011
Ted Kremenekcb612922008-04-18 19:23:43 +00002012// Assumptions.
2013
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002014const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr,
2015 const GRState* St,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002016 SVal Cond, bool Assumption,
Ted Kremenek4323a572008-07-10 22:03:41 +00002017 bool& isFeasible) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002018
2019 // FIXME: We may add to the interface of EvalAssume the list of symbols
2020 // whose assumptions have changed. For now we just iterate through the
2021 // bindings and check if any of the tracked symbols are NULL. This isn't
2022 // too bad since the number of symbols we will track in practice are
2023 // probably small and EvalAssume is only called at branches and a few
2024 // other places.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002025 RefBindings B = St->get<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002026
2027 if (B.isEmpty())
2028 return St;
2029
2030 bool changed = false;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002031
2032 GRStateRef state(St, VMgr);
2033 RefBindings::Factory& RefBFactory = state.get_context<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002034
2035 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002036 // Check if the symbol is null (or equal to any constant).
2037 // If this is the case, stop tracking the symbol.
Zhongxing Xu39cfed32008-08-29 14:52:36 +00002038 if (VMgr.getSymVal(St, I.getKey())) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002039 changed = true;
2040 B = RefBFactory.Remove(B, I.getKey());
2041 }
2042 }
2043
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002044 if (changed)
2045 state = state.set<RefBindings>(B);
Ted Kremenekcb612922008-04-18 19:23:43 +00002046
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002047 return state;
Ted Kremenekcb612922008-04-18 19:23:43 +00002048}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002049
Ted Kremenek2dabd432008-12-05 02:27:51 +00002050RefBindings CFRefCount::Update(RefBindings B, SymbolRef sym,
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002051 RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002052 RefVal::Kind& hasErr,
2053 RefBindings::Factory& RefBFactory) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002054
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002055 // FIXME: This dispatch can potentially be sped up by unifiying it into
2056 // a single switch statement. Opt for simplicity for now.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002057
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002058 switch (E) {
2059 default:
2060 assert (false && "Unhandled CFRef transition.");
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002061
2062 case MayEscape:
2063 if (V.getKind() == RefVal::Owned) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002064 V = V ^ RefVal::NotOwned;
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002065 break;
2066 }
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002067 // Fall-through.
Ted Kremenek070a8252008-07-09 18:11:16 +00002068 case DoNothingByRef:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002069 case DoNothing:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002070 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002071 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002072 hasErr = V.getKind();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002073 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002074 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002075 return B;
Ted Kremeneke19f4492008-06-30 16:57:41 +00002076
Ted Kremenekabf43972009-01-28 21:44:40 +00002077 case Autorelease:
2078 if (isGCEnabled()) return B;
2079 // Fall-through.
Ted Kremenek14993892008-05-06 02:41:27 +00002080 case StopTracking:
2081 return RefBFactory.Remove(B, sym);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002082
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002083 case IncRef:
2084 switch (V.getKind()) {
2085 default:
2086 assert(false);
2087
2088 case RefVal::Owned:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002089 case RefVal::NotOwned:
Ted Kremenek553cf182008-06-25 21:21:56 +00002090 V = V + 1;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002091 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002092 case RefVal::Released:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002093 if (isGCEnabled())
Ted Kremenek553cf182008-06-25 21:21:56 +00002094 V = V ^ RefVal::Owned;
Ted Kremenek65c91652008-04-29 05:44:10 +00002095 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002096 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek65c91652008-04-29 05:44:10 +00002097 hasErr = V.getKind();
2098 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002099 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002100 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002101 break;
2102
Ted Kremenek553cf182008-06-25 21:21:56 +00002103 case SelfOwn:
2104 V = V ^ RefVal::NotOwned;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002105 // Fall-through.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002106 case DecRef:
2107 switch (V.getKind()) {
2108 default:
2109 assert (false);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002110
Ted Kremenek553cf182008-06-25 21:21:56 +00002111 case RefVal::Owned:
2112 V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002113 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002114
Ted Kremenek553cf182008-06-25 21:21:56 +00002115 case RefVal::NotOwned:
2116 if (V.getCount() > 0)
2117 V = V - 1;
Ted Kremenek61b9f872008-04-10 23:09:18 +00002118 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002119 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002120 hasErr = V.getKind();
Ted Kremenek9e476de2008-08-12 18:30:56 +00002121 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002122 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002123
2124 case RefVal::Released:
Ted Kremenek553cf182008-06-25 21:21:56 +00002125 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002126 hasErr = V.getKind();
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002127 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002128 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002129 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002130 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002131 return RefBFactory.Add(B, sym, V);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002132}
2133
Ted Kremenekfa34b332008-04-09 01:10:13 +00002134//===----------------------------------------------------------------------===//
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002135// Error reporting.
Ted Kremenekfa34b332008-04-09 01:10:13 +00002136//===----------------------------------------------------------------------===//
2137
Ted Kremenek8dd56462008-04-18 03:39:05 +00002138namespace {
2139
2140 //===-------------===//
2141 // Bug Descriptions. //
2142 //===-------------===//
2143
Ted Kremenek95cc1ba2008-04-18 20:54:29 +00002144 class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002145 protected:
2146 CFRefCount& TF;
2147
2148 public:
2149 CFRefBug(CFRefCount& tf) : TF(tf) {}
Ted Kremenek072192b2008-04-30 23:47:44 +00002150
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002151 CFRefCount& getTF() { return TF; }
Ted Kremenek789deac2008-05-05 23:16:31 +00002152 const CFRefCount& getTF() const { return TF; }
2153
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002154 virtual bool isLeak() const { return false; }
Ted Kremenek8c036c72008-09-20 04:23:38 +00002155
2156 const char* getCategory() const {
Ted Kremenek062bae02008-09-27 22:02:42 +00002157 return "Memory (Core Foundation/Objective-C)";
Ted Kremenek8c036c72008-09-20 04:23:38 +00002158 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002159 };
2160
2161 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2162 public:
2163 UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
2164
2165 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002166 return "use-after-release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002167 }
2168 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002169 return "Reference-counted object is used after it is released.";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002170 }
2171
2172 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002173 };
2174
2175 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2176 public:
2177 BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
2178
2179 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002180 return "bad release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002181 }
2182 virtual const char* getDescription() const {
2183 return "Incorrect decrement of the reference count of a "
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002184 "CoreFoundation object: "
Ted Kremenek8dd56462008-04-18 03:39:05 +00002185 "The object is not owned at this point by the caller.";
2186 }
2187
2188 virtual void EmitWarnings(BugReporter& BR);
2189 };
2190
2191 class VISIBILITY_HIDDEN Leak : public CFRefBug {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002192 bool isReturn;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002193 public:
2194 Leak(CFRefCount& tf) : CFRefBug(tf) {}
2195
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002196 void setIsReturn(bool x) { isReturn = x; }
2197
Ted Kremenek8dd56462008-04-18 03:39:05 +00002198 virtual const char* getName() const {
Ted Kremenek432af592008-05-06 18:11:36 +00002199
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002200 if (!isReturn) {
2201 if (getTF().isGCEnabled())
2202 return "leak (GC)";
2203
2204 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
2205 return "leak (hybrid MM, non-GC)";
2206
2207 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
2208 return "leak";
2209 }
2210 else {
2211 if (getTF().isGCEnabled())
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002212 return "[naming convention] leak of returned object (GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002213
2214 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002215 return "[naming convention] leak of returned object (hybrid MM, "
2216 "non-GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002217
2218 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002219 return "[naming convention] leak of returned object";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002220 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002221 }
Ted Kremenek3148eb42009-01-24 00:55:43 +00002222
Ted Kremenek8dd56462008-04-18 03:39:05 +00002223 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002224 virtual void GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002225 virtual bool isLeak() const { return true; }
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002226 virtual bool isCached(BugReport& R);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002227 };
2228
2229 //===---------===//
2230 // Bug Reports. //
2231 //===---------===//
2232
2233 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
Ted Kremenek2dabd432008-12-05 02:27:51 +00002234 SymbolRef Sym;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002235 public:
Ted Kremenek2dabd432008-12-05 02:27:51 +00002236 CFRefReport(CFRefBug& D, ExplodedNode<GRState> *n, SymbolRef sym)
Ted Kremenek8dd56462008-04-18 03:39:05 +00002237 : RangedBugReport(D, n), Sym(sym) {}
2238
2239 virtual ~CFRefReport() {}
2240
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002241 CFRefBug& getBugType() {
2242 return (CFRefBug&) RangedBugReport::getBugType();
2243 }
2244 const CFRefBug& getBugType() const {
2245 return (const CFRefBug&) RangedBugReport::getBugType();
2246 }
2247
2248 virtual void getRanges(BugReporter& BR, const SourceRange*& beg,
2249 const SourceRange*& end) {
2250
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002251 if (!getBugType().isLeak())
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002252 RangedBugReport::getRanges(BR, beg, end);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002253 else
2254 beg = end = 0;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002255 }
2256
Ted Kremenek2dabd432008-12-05 02:27:51 +00002257 SymbolRef getSymbol() const { return Sym; }
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002258
Ted Kremenek3148eb42009-01-24 00:55:43 +00002259 PathDiagnosticPiece* getEndPath(BugReporter& BR,
2260 const ExplodedNode<GRState>* N);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002261
Ted Kremenek3148eb42009-01-24 00:55:43 +00002262 std::pair<const char**,const char**> getExtraDescriptiveText();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002263
Ted Kremenek3148eb42009-01-24 00:55:43 +00002264 PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState>* N,
2265 const ExplodedNode<GRState>* PrevN,
2266 const ExplodedGraph<GRState>& G,
2267 BugReporter& BR);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002268 };
2269
2270
2271} // end anonymous namespace
2272
2273void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002274 Eng.Register(new UseAfterRelease(*this));
2275 Eng.Register(new BadRelease(*this));
2276 Eng.Register(new Leak(*this));
2277}
2278
Ted Kremenek072192b2008-04-30 23:47:44 +00002279
2280static const char* Msgs[] = {
2281 "Code is compiled in garbage collection only mode" // GC only
2282 " (the bug occurs with garbage collection enabled).",
2283
2284 "Code is compiled without garbage collection.", // No GC.
2285
2286 "Code is compiled for use with and without garbage collection (GC)."
2287 " The bug occurs with GC enabled.", // Hybrid, with GC.
2288
2289 "Code is compiled for use with and without garbage collection (GC)."
2290 " The bug occurs in non-GC mode." // Hyrbird, without GC/
2291};
2292
2293std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2294 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
2295
2296 switch (TF.getLangOptions().getGCMode()) {
2297 default:
2298 assert(false);
Ted Kremenek31593ac2008-05-01 04:02:04 +00002299
2300 case LangOptions::GCOnly:
2301 assert (TF.isGCEnabled());
Ted Kremenek9e476de2008-08-12 18:30:56 +00002302 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2303
Ted Kremenek072192b2008-04-30 23:47:44 +00002304 case LangOptions::NonGC:
2305 assert (!TF.isGCEnabled());
Ted Kremenek072192b2008-04-30 23:47:44 +00002306 return std::make_pair(&Msgs[1], &Msgs[1]+1);
2307
2308 case LangOptions::HybridGC:
2309 if (TF.isGCEnabled())
2310 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2311 else
2312 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2313 }
2314}
2315
Ted Kremenek3148eb42009-01-24 00:55:43 +00002316PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode<GRState>* N,
2317 const ExplodedNode<GRState>* PrevN,
2318 const ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002319 BugReporter& BR) {
2320
Ted Kremenek611a15a2009-01-28 05:29:13 +00002321 // Check if the type state has changed.
2322 GRStateManager &StMgr = cast<GRBugReporter>(BR).getStateManager();
2323 GRStateRef PrevSt(PrevN->getState(), StMgr);
2324 GRStateRef CurrSt(N->getState(), StMgr);
Ted Kremenek20982802009-01-28 05:06:46 +00002325
Ted Kremenek611a15a2009-01-28 05:29:13 +00002326 const RefVal* CurrT = CurrSt.get<RefBindings>(Sym);
2327 if (!CurrT) return NULL;
2328
2329 const RefVal& CurrV = *CurrT;
2330 const RefVal* PrevT = PrevSt.get<RefBindings>(Sym);
Ted Kremenekce48e002008-05-05 17:53:17 +00002331
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002332 if (!PrevT) {
Ted Kremeneka1f117e2009-01-28 04:47:13 +00002333 std::string sbuf;
2334 llvm::raw_string_ostream os(sbuf);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002335
Ted Kremenekce48e002008-05-05 17:53:17 +00002336 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2337
Ted Kremenek5c1cd522009-01-28 05:15:02 +00002338 if (CallExpr *CE = dyn_cast<CallExpr>(S)) {
2339 // Get the name of the callee (if it is available).
2340 SVal X = CurrSt.GetSVal(CE->getCallee());
2341 if (loc::FuncVal* FV = dyn_cast<loc::FuncVal>(&X))
2342 os << "Call to function '" << FV->getDecl()->getNameAsString() <<'\'';
2343 else
Ted Kremeneka102c0c2009-01-28 06:01:42 +00002344 os << "function call";
Ted Kremenek5c1cd522009-01-28 05:15:02 +00002345 }
2346 else {
2347 assert (isa<ObjCMessageExpr>(S));
Ted Kremeneka102c0c2009-01-28 06:01:42 +00002348 os << "Method";
Ted Kremenekce48e002008-05-05 17:53:17 +00002349 }
Ted Kremenek5c1cd522009-01-28 05:15:02 +00002350
Ted Kremenek961b61d2009-01-28 06:06:36 +00002351 if (CurrV.getObjKind() == RetEffect::CF) {
2352 os << " returns a Core Foundation object with a ";
2353 }
2354 else {
2355 assert (CurrV.getObjKind() == RetEffect::ObjC);
2356 os << " returns an Objective-C object with a ";
2357 }
Ted Kremeneka102c0c2009-01-28 06:01:42 +00002358
Ted Kremenek23b8eaa2009-01-28 06:25:48 +00002359 if (CurrV.isOwned()) {
2360 os << "+1 retain count (owning reference).";
2361
2362 if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) {
2363 assert(CurrV.getObjKind() == RetEffect::CF);
2364 os << " "
2365 "Core Foundation objects are not automatically garbage collected.";
2366 }
2367 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002368 else {
2369 assert (CurrV.isNotOwned());
Ted Kremenek5c1cd522009-01-28 05:15:02 +00002370 os << "+0 retain count (non-owning reference).";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002371 }
Ted Kremenekce48e002008-05-05 17:53:17 +00002372
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002373 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremeneka1f117e2009-01-28 04:47:13 +00002374 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str());
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002375
2376 if (Expr* Exp = dyn_cast<Expr>(S))
2377 P->addRange(Exp->getSourceRange());
2378
2379 return P;
2380 }
2381
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002382 // Determine if the typestate has changed.
Ted Kremenek611a15a2009-01-28 05:29:13 +00002383 RefVal PrevV = *PrevT;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002384
2385 if (PrevV == CurrV)
2386 return NULL;
2387
2388 // The typestate has changed.
Ted Kremeneka1f117e2009-01-28 04:47:13 +00002389 std::string sbuf;
2390 llvm::raw_string_ostream os(sbuf);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002391
2392 switch (CurrV.getKind()) {
2393 case RefVal::Owned:
2394 case RefVal::NotOwned:
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002395
2396 if (PrevV.getCount() == CurrV.getCount())
2397 return 0;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002398
2399 if (PrevV.getCount() > CurrV.getCount())
2400 os << "Reference count decremented.";
2401 else
2402 os << "Reference count incremented.";
2403
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002404 if (unsigned Count = CurrV.getCount()) {
Ted Kremenekce48e002008-05-05 17:53:17 +00002405 os << " Object has +" << Count;
Ted Kremenek79c140b2008-04-18 05:32:44 +00002406
Ted Kremenekce48e002008-05-05 17:53:17 +00002407 if (Count > 1)
2408 os << " retain counts.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002409 else
Ted Kremenekce48e002008-05-05 17:53:17 +00002410 os << " retain count.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002411 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002412
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002413 break;
2414
2415 case RefVal::Released:
Ted Kremeneka1f117e2009-01-28 04:47:13 +00002416 os << "Object released.";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002417 break;
2418
2419 case RefVal::ReturnedOwned:
Ted Kremeneka1f117e2009-01-28 04:47:13 +00002420 os << "Object returned to caller as an owning reference (single retain "
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002421 "count transferred to caller).";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002422 break;
2423
2424 case RefVal::ReturnedNotOwned:
Ted Kremeneka1f117e2009-01-28 04:47:13 +00002425 os << "Object returned to caller with a +0 (non-owning) retain count.";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002426 break;
2427
2428 default:
2429 return NULL;
2430 }
2431
2432 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2433 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
Ted Kremeneka1f117e2009-01-28 04:47:13 +00002434 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str());
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002435
2436 // Add the range by scanning the children of the statement for any bindings
2437 // to Sym.
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002438 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
2439 if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) {
Ted Kremenek20982802009-01-28 05:06:46 +00002440 SVal X = CurrSt.GetSVal(Exp);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002441 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&X))
Ted Kremenek20982802009-01-28 05:06:46 +00002442 if (SV->getSymbol() == Sym) P->addRange(Exp->getSourceRange()); break;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002443 }
2444
2445 return P;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002446}
2447
Ted Kremenek9e240492008-10-04 05:50:14 +00002448namespace {
2449class VISIBILITY_HIDDEN FindUniqueBinding :
2450 public StoreManager::BindingsHandler {
Ted Kremenek2dabd432008-12-05 02:27:51 +00002451 SymbolRef Sym;
Ted Kremenek9e240492008-10-04 05:50:14 +00002452 MemRegion* Binding;
2453 bool First;
2454
2455 public:
Ted Kremenek2dabd432008-12-05 02:27:51 +00002456 FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
Ted Kremenek9e240492008-10-04 05:50:14 +00002457
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002458 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal val) {
2459 if (const loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002460 if (SV->getSymbol() != Sym)
2461 return true;
2462 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002463 else if (const nonloc::SymbolVal* SV=dyn_cast<nonloc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002464 if (SV->getSymbol() != Sym)
2465 return true;
2466 }
2467 else
2468 return true;
2469
2470 if (Binding) {
2471 First = false;
2472 return false;
2473 }
2474 else
2475 Binding = R;
2476
2477 return true;
2478 }
2479
2480 operator bool() { return First && Binding; }
2481 MemRegion* getRegion() { return Binding; }
2482};
2483}
2484
Ted Kremenek3148eb42009-01-24 00:55:43 +00002485static std::pair<const ExplodedNode<GRState>*,const MemRegion*>
2486GetAllocationSite(GRStateManager* StateMgr, const ExplodedNode<GRState>* N,
Ted Kremenek2dabd432008-12-05 02:27:51 +00002487 SymbolRef Sym) {
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002488
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002489 // Find both first node that referred to the tracked symbol and the
2490 // memory location that value was store to.
Ted Kremenek3148eb42009-01-24 00:55:43 +00002491 const ExplodedNode<GRState>* Last = N;
2492 const MemRegion* FirstBinding = 0;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002493
2494 while (N) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002495 const GRState* St = N->getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002496 RefBindings B = St->get<RefBindings>();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002497
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002498 if (!B.lookup(Sym))
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002499 break;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002500
2501 if (StateMgr) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002502 FindUniqueBinding FB(Sym);
2503 StateMgr->iterBindings(St, FB);
2504 if (FB) FirstBinding = FB.getRegion();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002505 }
2506
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002507 Last = N;
2508 N = N->pred_empty() ? NULL : *(N->pred_begin());
2509 }
2510
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002511 return std::make_pair(Last, FirstBinding);
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002512}
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002513
Ted Kremenek3148eb42009-01-24 00:55:43 +00002514PathDiagnosticPiece*
2515CFRefReport::getEndPath(BugReporter& br, const ExplodedNode<GRState>* EndN) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002516
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002517 GRBugReporter& BR = cast<GRBugReporter>(br);
2518
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002519 // Tell the BugReporter to report cases when the tracked symbol is
2520 // assigned to different variables, etc.
Ted Kremenekc0959972008-07-02 21:24:01 +00002521 cast<GRBugReporter>(BR).addNotableSymbol(Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002522
2523 if (!getBugType().isLeak())
Ted Kremeneke28565b2008-05-05 18:50:19 +00002524 return RangedBugReport::getEndPath(BR, EndN);
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002525
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002526 // We are a leak. Walk up the graph to get to the first node where the
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002527 // symbol appeared, and also get the first VarDecl that tracked object
2528 // is stored to.
Ted Kremenek3148eb42009-01-24 00:55:43 +00002529 const ExplodedNode<GRState>* AllocNode = 0;
2530 const MemRegion* FirstBinding = 0;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002531
2532 llvm::tie(AllocNode, FirstBinding) =
2533 GetAllocationSite(&BR.getStateManager(), EndN, Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002534
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002535 // Get the allocate site.
2536 assert (AllocNode);
2537 Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002538
Ted Kremeneke28565b2008-05-05 18:50:19 +00002539 SourceManager& SMgr = BR.getContext().getSourceManager();
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00002540 unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart());
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002541
Ted Kremeneke28565b2008-05-05 18:50:19 +00002542 // Get the leak site. We may have multiple ExplodedNodes (one with the
2543 // leak) that occur on the same line number; if the node with the leak
2544 // has any immediate predecessor nodes with the same line number, find
2545 // any transitive-successors that have a different statement and use that
2546 // line number instead. This avoids emiting a diagnostic like:
2547 //
2548 // // 'y' is leaked.
2549 // int x = foo(y);
2550 //
2551 // instead we want:
2552 //
2553 // int x = foo(y);
2554 // // 'y' is leaked.
2555
2556 Stmt* S = getStmt(BR); // This is the statement where the leak occured.
2557 assert (S);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00002558 unsigned EndLine = SMgr.getInstantiationLineNumber(S->getLocStart());
Ted Kremeneke28565b2008-05-05 18:50:19 +00002559
2560 // Look in the *trimmed* graph at the immediate predecessor of EndN. Does
2561 // it occur on the same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002562 PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above;
Ted Kremeneke28565b2008-05-05 18:50:19 +00002563
2564 assert (!EndN->pred_empty()); // Not possible to have 0 predecessors.
Ted Kremenek3148eb42009-01-24 00:55:43 +00002565 const ExplodedNode<GRState> *Pred = *(EndN->pred_begin());
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002566 ProgramPoint PredPos = Pred->getLocation();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002567
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002568 if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) {
Ted Kremeneke28565b2008-05-05 18:50:19 +00002569
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002570 Stmt* SPred = PredPS->getStmt();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002571
2572 // Predecessor at same line?
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00002573 if (SMgr.getInstantiationLineNumber(SPred->getLocStart()) != EndLine) {
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002574 Hint = PathDiagnosticPiece::Below;
2575 S = SPred;
2576 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002577 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002578
2579 // Generate the diagnostic.
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002580 FullSourceLoc L( S->getLocStart(), SMgr);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002581 std::ostringstream os;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002582
Ted Kremeneke28565b2008-05-05 18:50:19 +00002583 os << "Object allocated on line " << AllocLine;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002584
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002585 if (FirstBinding)
Ted Kremenek9e240492008-10-04 05:50:14 +00002586 os << " and stored into '" << FirstBinding->getString() << '\'';
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002587
Ted Kremenek9e240492008-10-04 05:50:14 +00002588
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002589 // Get the retain count.
2590 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
2591
2592 if (RV->getKind() == RefVal::ErrorLeakReturned) {
Ted Kremenek04f9d462008-12-02 01:26:07 +00002593 // FIXME: Per comments in rdar://6320065, "create" only applies to CF
2594 // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership
2595 // to the caller for NS objects.
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002596 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BR.getGraph().getCodeDecl());
2597 os << " is returned from a method whose name ('"
Chris Lattner077bf5e2008-11-24 03:33:13 +00002598 << MD.getSelector().getAsString()
Ted Kremenek234a4c22009-01-07 00:39:56 +00002599 << "') does not contain 'copy' or otherwise starts with"
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002600 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002601 " in the Memory Management Guide for Cocoa (object leaked).";
2602 }
2603 else
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002604 os << " is no longer referenced after this point and has a retain count of"
2605 " +"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002606 << RV->getCount() << " (object leaked).";
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002607
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002608 return new PathDiagnosticPiece(L, os.str(), Hint);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002609}
2610
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002611void UseAfterRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002612
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002613 for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
2614 E = TF.use_after_end(); I != E; ++I) {
2615
Ted Kremenek8dd56462008-04-18 03:39:05 +00002616 CFRefReport report(*this, I->first, I->second.second);
2617 report.addRange(I->second.first->getSourceRange());
Ted Kremenek75840e12008-04-18 01:56:37 +00002618 BR.EmitWarning(report);
Ted Kremenekfa34b332008-04-09 01:10:13 +00002619 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002620}
2621
2622void BadRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002623
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002624 for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
2625 E = TF.bad_release_end(); I != E; ++I) {
2626
Ted Kremenek8dd56462008-04-18 03:39:05 +00002627 CFRefReport report(*this, I->first, I->second.second);
2628 report.addRange(I->second.first->getSourceRange());
2629 BR.EmitWarning(report);
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002630 }
2631}
Ted Kremenekfa34b332008-04-09 01:10:13 +00002632
Ted Kremenek989d5192008-04-17 23:43:50 +00002633void Leak::EmitWarnings(BugReporter& BR) {
2634
2635 for (CFRefCount::leaks_iterator I = TF.leaks_begin(),
2636 E = TF.leaks_end(); I != E; ++I) {
2637
Ted Kremenek2dabd432008-12-05 02:27:51 +00002638 std::vector<std::pair<SymbolRef, bool> >& SymV = *(I->second);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002639 unsigned n = SymV.size();
2640
2641 for (unsigned i = 0; i < n; ++i) {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002642 setIsReturn(SymV[i].second);
2643 CFRefReport report(*this, I->first, SymV[i].first);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002644 BR.EmitWarning(report);
2645 }
Ted Kremenek989d5192008-04-17 23:43:50 +00002646 }
2647}
2648
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002649void Leak::GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002650 for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end();
2651 I!=E; ++I)
2652 Nodes.push_back(I->first);
2653}
2654
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002655bool Leak::isCached(BugReport& R) {
2656
2657 // Most bug reports are cached at the location where they occured.
2658 // With leaks, we want to unique them by the location where they were
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002659 // allocated, and only report a single path.
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002660
Ted Kremenek2dabd432008-12-05 02:27:51 +00002661 SymbolRef Sym = static_cast<CFRefReport&>(R).getSymbol();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002662
Ted Kremenek3148eb42009-01-24 00:55:43 +00002663 const ExplodedNode<GRState>* AllocNode =
2664 GetAllocationSite(0, R.getEndNode(), Sym).first;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002665
2666 if (!AllocNode)
2667 return false;
2668
2669 return BugTypeCacheLocation::isCached(AllocNode->getLocation());
2670}
2671
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002672//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +00002673// Transfer function creation for external clients.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002674//===----------------------------------------------------------------------===//
2675
Ted Kremenek072192b2008-04-30 23:47:44 +00002676GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
2677 const LangOptions& lopts) {
Ted Kremenek78d46242008-07-22 16:21:24 +00002678 return new CFRefCount(Ctx, GCEnabled, lopts);
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +00002679}