blob: b33a7ca5ab9b9e59ea9668659b06f6aeba22f739 [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 Kremenek5c74d502008-10-24 21:18:08 +000058 return CStrInCStrNoCase(s, "create") || CStrInCStrNoCase(s, "copy") ||
59 CStrInCStrNoCase(s, "new") == s || CStrInCStrNoCase(s, "alloc") == s;
Ted Kremenek4c79e552008-11-05 16:54:44 +000060}
61
62static bool followsReturnRule(const char* s) {
63 while (*s == '_') ++s;
64 return followsFundamentalRule(s) || CStrInCStrNoCase(s, "init") == s;
65}
Ted Kremenek5c74d502008-10-24 21:18:08 +000066
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000067//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +000068// Selector creation functions.
Ted Kremenek4fd88972008-04-17 18:12:53 +000069//===----------------------------------------------------------------------===//
70
Ted Kremenekb83e02e2008-05-01 18:31:44 +000071static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
Ted Kremenek4fd88972008-04-17 18:12:53 +000072 IdentifierInfo* II = &Ctx.Idents.get(name);
73 return Ctx.Selectors.getSelector(0, &II);
74}
75
Ted Kremenek9c32d082008-05-06 00:30:21 +000076static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
77 IdentifierInfo* II = &Ctx.Idents.get(name);
78 return Ctx.Selectors.getSelector(1, &II);
79}
80
Ted Kremenek553cf182008-06-25 21:21:56 +000081//===----------------------------------------------------------------------===//
82// Type querying functions.
83//===----------------------------------------------------------------------===//
84
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000085static bool isCFRefType(QualType T) {
86
87 if (!T->isPointerType())
88 return false;
89
Ted Kremenek553cf182008-06-25 21:21:56 +000090 // Check the typedef for the name "CF" and the substring "Ref".
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000091 TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
92
93 if (!TD)
94 return false;
95
96 const char* TDName = TD->getDecl()->getIdentifier()->getName();
97 assert (TDName);
98
99 if (TDName[0] != 'C' || TDName[1] != 'F')
100 return false;
101
102 if (strstr(TDName, "Ref") == 0)
103 return false;
104
105 return true;
106}
107
Ted Kremenek37d785b2008-07-15 16:50:12 +0000108static bool isCGRefType(QualType T) {
109
110 if (!T->isPointerType())
111 return false;
112
113 // Check the typedef for the name "CG" and the substring "Ref".
114 TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
115
116 if (!TD)
117 return false;
118
119 const char* TDName = TD->getDecl()->getIdentifier()->getName();
120 assert (TDName);
121
122 if (TDName[0] != 'C' || TDName[1] != 'G')
123 return false;
124
125 if (strstr(TDName, "Ref") == 0)
126 return false;
127
128 return true;
129}
130
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000131static bool isNSType(QualType T) {
132
133 if (!T->isPointerType())
134 return false;
135
136 ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
137
138 if (!OT)
139 return false;
140
141 const char* ClsName = OT->getDecl()->getIdentifier()->getName();
142 assert (ClsName);
143
144 if (ClsName[0] != 'N' || ClsName[1] != 'S')
145 return false;
146
147 return true;
148}
149
Ted Kremenek4fd88972008-04-17 18:12:53 +0000150//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +0000151// Primitives used for constructing summaries for function/method calls.
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000152//===----------------------------------------------------------------------===//
153
Ted Kremenek553cf182008-06-25 21:21:56 +0000154namespace {
155/// ArgEffect is used to summarize a function/method call's effect on a
156/// particular argument.
Ted Kremenek070a8252008-07-09 18:11:16 +0000157enum ArgEffect { IncRef, DecRef, DoNothing, DoNothingByRef,
158 StopTracking, MayEscape, SelfOwn, Autorelease };
Ted Kremenek553cf182008-06-25 21:21:56 +0000159
160/// ArgEffects summarizes the effects of a function/method call on all of
161/// its arguments.
162typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000163}
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000164
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000165namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000166template <> struct FoldingSetTrait<ArgEffects> {
167 static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
168 for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) {
169 ID.AddInteger(I->first);
170 ID.AddInteger((unsigned) I->second);
171 }
172 }
173};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000174} // end llvm namespace
175
176namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000177
178/// RetEffect is used to summarize a function/method call's behavior with
179/// respect to its return value.
180class VISIBILITY_HIDDEN RetEffect {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000181public:
Ted Kremeneka7344702008-06-23 18:02:52 +0000182 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
183 NotOwnedSymbol, ReceiverAlias };
Ted Kremenek553cf182008-06-25 21:21:56 +0000184
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000185private:
186 unsigned Data;
Ted Kremenek553cf182008-06-25 21:21:56 +0000187 RetEffect(Kind k, unsigned D = 0) { Data = (D << 3) | (unsigned) k; }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000188
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000189public:
Ted Kremenek553cf182008-06-25 21:21:56 +0000190
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000191 Kind getKind() const { return (Kind) (Data & 0x7); }
Ted Kremenek553cf182008-06-25 21:21:56 +0000192
193 unsigned getIndex() const {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000194 assert(getKind() == Alias);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000195 return Data >> 3;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000196 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000197
Ted Kremenek553cf182008-06-25 21:21:56 +0000198 static RetEffect MakeAlias(unsigned Idx) {
199 return RetEffect(Alias, Idx);
200 }
201 static RetEffect MakeReceiverAlias() {
202 return RetEffect(ReceiverAlias);
203 }
Ted Kremeneka7344702008-06-23 18:02:52 +0000204 static RetEffect MakeOwned(bool isAllocated = false) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000205 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol);
206 }
207 static RetEffect MakeNotOwned() {
208 return RetEffect(NotOwnedSymbol);
209 }
210 static RetEffect MakeNoRet() {
211 return RetEffect(NoRet);
Ted Kremeneka7344702008-06-23 18:02:52 +0000212 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000213
Ted Kremenek553cf182008-06-25 21:21:56 +0000214 operator Kind() const {
215 return getKind();
216 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000217
Ted Kremenek553cf182008-06-25 21:21:56 +0000218 void Profile(llvm::FoldingSetNodeID& ID) const {
219 ID.AddInteger(Data);
220 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000221};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000222
Ted Kremenek553cf182008-06-25 21:21:56 +0000223
224class VISIBILITY_HIDDEN RetainSummary : public llvm::FoldingSetNode {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000225 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
226 /// specifies the argument (starting from 0). This can be sparsely
227 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000228 ArgEffects* Args;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000229
230 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
231 /// do not have an entry in Args.
232 ArgEffect DefaultArgEffect;
233
Ted Kremenek553cf182008-06-25 21:21:56 +0000234 /// Receiver - If this summary applies to an Objective-C message expression,
235 /// this is the effect applied to the state of the receiver.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000236 ArgEffect Receiver;
Ted Kremenek553cf182008-06-25 21:21:56 +0000237
238 /// Ret - The effect on the return value. Used to indicate if the
239 /// function/method call returns a new tracked symbol, returns an
240 /// alias of one of the arguments in the call, and so on.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000241 RetEffect Ret;
Ted Kremenek553cf182008-06-25 21:21:56 +0000242
Ted Kremenek70a733e2008-07-18 17:24:20 +0000243 /// EndPath - Indicates that execution of this method/function should
244 /// terminate the simulation of a path.
245 bool EndPath;
246
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000247public:
248
Ted Kremenek1bffd742008-05-06 15:44:25 +0000249 RetainSummary(ArgEffects* A, RetEffect R, ArgEffect defaultEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000250 ArgEffect ReceiverEff, bool endpath = false)
251 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
252 EndPath(endpath) {}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000253
Ted Kremenek553cf182008-06-25 21:21:56 +0000254 /// getArg - Return the argument effect on the argument specified by
255 /// idx (starting from 0).
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000256 ArgEffect getArg(unsigned idx) const {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000257
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000258 if (!Args)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000259 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000260
261 // If Args is present, it is likely to contain only 1 element.
262 // Just do a linear search. Do it from the back because functions with
263 // large numbers of arguments will be tail heavy with respect to which
Ted Kremenek553cf182008-06-25 21:21:56 +0000264 // argument they actually modify with respect to the reference count.
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000265 for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend();
266 I!=E; ++I) {
267
268 if (idx > I->first)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000269 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000270
271 if (idx == I->first)
272 return I->second;
273 }
274
Ted Kremenek1bffd742008-05-06 15:44:25 +0000275 return DefaultArgEffect;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000276 }
277
Ted Kremenek553cf182008-06-25 21:21:56 +0000278 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000279 RetEffect getRetEffect() const {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000280 return Ret;
281 }
282
Ted Kremenek70a733e2008-07-18 17:24:20 +0000283 /// isEndPath - Returns true if executing the given method/function should
284 /// terminate the path.
285 bool isEndPath() const { return EndPath; }
286
Ted Kremenek553cf182008-06-25 21:21:56 +0000287 /// getReceiverEffect - Returns the effect on the receiver of the call.
288 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000289 ArgEffect getReceiverEffect() const {
290 return Receiver;
291 }
292
Ted Kremenek55499762008-06-17 02:43:46 +0000293 typedef ArgEffects::const_iterator ExprIterator;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000294
Ted Kremenek55499762008-06-17 02:43:46 +0000295 ExprIterator begin_args() const { return Args->begin(); }
296 ExprIterator end_args() const { return Args->end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000297
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000298 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000299 RetEffect RetEff, ArgEffect DefaultEff,
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000300 ArgEffect ReceiverEff, bool EndPath) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000301 ID.AddPointer(A);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000302 ID.Add(RetEff);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000303 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000304 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000305 ID.AddInteger((unsigned) EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000306 }
307
308 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000309 Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000310 }
311};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000312} // end anonymous namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000313
Ted Kremenek553cf182008-06-25 21:21:56 +0000314//===----------------------------------------------------------------------===//
315// Data structures for constructing summaries.
316//===----------------------------------------------------------------------===//
Ted Kremenek53301ba2008-06-24 03:49:48 +0000317
Ted Kremenek553cf182008-06-25 21:21:56 +0000318namespace {
319class VISIBILITY_HIDDEN ObjCSummaryKey {
320 IdentifierInfo* II;
321 Selector S;
322public:
323 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
324 : II(ii), S(s) {}
325
326 ObjCSummaryKey(ObjCInterfaceDecl* d, Selector s)
327 : II(d ? d->getIdentifier() : 0), S(s) {}
328
329 ObjCSummaryKey(Selector s)
330 : II(0), S(s) {}
331
332 IdentifierInfo* getIdentifier() const { return II; }
333 Selector getSelector() const { return S; }
334};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000335}
336
337namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000338template <> struct DenseMapInfo<ObjCSummaryKey> {
339 static inline ObjCSummaryKey getEmptyKey() {
340 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
341 DenseMapInfo<Selector>::getEmptyKey());
342 }
Ted Kremenek4f22a782008-06-23 23:30:29 +0000343
Ted Kremenek553cf182008-06-25 21:21:56 +0000344 static inline ObjCSummaryKey getTombstoneKey() {
345 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
346 DenseMapInfo<Selector>::getTombstoneKey());
347 }
348
349 static unsigned getHashValue(const ObjCSummaryKey &V) {
350 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
351 & 0x88888888)
352 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
353 & 0x55555555);
354 }
355
356 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
357 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
358 RHS.getIdentifier()) &&
359 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
360 RHS.getSelector());
361 }
362
363 static bool isPod() {
364 return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
365 DenseMapInfo<Selector>::isPod();
366 }
367};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000368} // end llvm namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000369
Ted Kremenek4f22a782008-06-23 23:30:29 +0000370namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000371class VISIBILITY_HIDDEN ObjCSummaryCache {
372 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
373 MapTy M;
374public:
375 ObjCSummaryCache() {}
376
377 typedef MapTy::iterator iterator;
378
379 iterator find(ObjCInterfaceDecl* D, Selector S) {
380
381 // Do a lookup with the (D,S) pair. If we find a match return
382 // the iterator.
383 ObjCSummaryKey K(D, S);
384 MapTy::iterator I = M.find(K);
385
386 if (I != M.end() || !D)
387 return I;
388
389 // Walk the super chain. If we find a hit with a parent, we'll end
390 // up returning that summary. We actually allow that key (null,S), as
391 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
392 // generate initial summaries without having to worry about NSObject
393 // being declared.
394 // FIXME: We may change this at some point.
395 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
396 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
397 break;
398
399 if (!C)
400 return I;
401 }
402
403 // Cache the summary with original key to make the next lookup faster
404 // and return the iterator.
405 M[K] = I->second;
406 return I;
407 }
408
Ted Kremenek98530452008-08-12 20:41:56 +0000409
Ted Kremenek553cf182008-06-25 21:21:56 +0000410 iterator find(Expr* Receiver, Selector S) {
411 return find(getReceiverDecl(Receiver), S);
412 }
413
414 iterator find(IdentifierInfo* II, Selector S) {
415 // FIXME: Class method lookup. Right now we dont' have a good way
416 // of going between IdentifierInfo* and the class hierarchy.
417 iterator I = M.find(ObjCSummaryKey(II, S));
418 return I == M.end() ? M.find(ObjCSummaryKey(S)) : I;
419 }
420
421 ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
422
423 const PointerType* PT = E->getType()->getAsPointerType();
424 if (!PT) return 0;
425
426 ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType());
427 if (!OI) return 0;
428
429 return OI ? OI->getDecl() : 0;
430 }
431
432 iterator end() { return M.end(); }
433
434 RetainSummary*& operator[](ObjCMessageExpr* ME) {
435
436 Selector S = ME->getSelector();
437
438 if (Expr* Receiver = ME->getReceiver()) {
439 ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
440 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
441 }
442
443 return M[ObjCSummaryKey(ME->getClassName(), S)];
444 }
445
446 RetainSummary*& operator[](ObjCSummaryKey K) {
447 return M[K];
448 }
449
450 RetainSummary*& operator[](Selector S) {
451 return M[ ObjCSummaryKey(S) ];
452 }
453};
454} // end anonymous namespace
455
456//===----------------------------------------------------------------------===//
457// Data structures for managing collections of summaries.
458//===----------------------------------------------------------------------===//
459
460namespace {
461class VISIBILITY_HIDDEN RetainSummaryManager {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000462
463 //==-----------------------------------------------------------------==//
464 // Typedefs.
465 //==-----------------------------------------------------------------==//
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000466
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000467 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> >
468 ArgEffectsSetTy;
469
470 typedef llvm::FoldingSet<RetainSummary>
471 SummarySetTy;
472
473 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
474 FuncSummariesTy;
475
Ted Kremenek4f22a782008-06-23 23:30:29 +0000476 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000477
478 //==-----------------------------------------------------------------==//
479 // Data.
480 //==-----------------------------------------------------------------==//
481
Ted Kremenek553cf182008-06-25 21:21:56 +0000482 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek377e2302008-04-29 05:33:51 +0000483 ASTContext& Ctx;
Ted Kremenek179064e2008-07-01 17:21:27 +0000484
Ted Kremenek070a8252008-07-09 18:11:16 +0000485 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
486 /// "CFDictionaryCreate".
487 IdentifierInfo* CFDictionaryCreateII;
488
Ted Kremenek553cf182008-06-25 21:21:56 +0000489 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek377e2302008-04-29 05:33:51 +0000490 const bool GCEnabled;
491
Ted Kremenek553cf182008-06-25 21:21:56 +0000492 /// SummarySet - A FoldingSet of uniqued summaries.
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000493 SummarySetTy SummarySet;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000494
Ted Kremenek553cf182008-06-25 21:21:56 +0000495 /// FuncSummaries - A map from FunctionDecls to summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000496 FuncSummariesTy FuncSummaries;
497
Ted Kremenek553cf182008-06-25 21:21:56 +0000498 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
499 /// to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000500 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000501
Ted Kremenek553cf182008-06-25 21:21:56 +0000502 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000503 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000504
Ted Kremenek553cf182008-06-25 21:21:56 +0000505 /// ArgEffectsSet - A FoldingSet of uniqued ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000506 ArgEffectsSetTy ArgEffectsSet;
507
Ted Kremenek553cf182008-06-25 21:21:56 +0000508 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
509 /// and all other data used by the checker.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000510 llvm::BumpPtrAllocator BPAlloc;
511
Ted Kremenek553cf182008-06-25 21:21:56 +0000512 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000513 ArgEffects ScratchArgs;
514
Ted Kremenek432af592008-05-06 18:11:36 +0000515 RetainSummary* StopSummary;
516
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000517 //==-----------------------------------------------------------------==//
518 // Methods.
519 //==-----------------------------------------------------------------==//
520
Ted Kremenek553cf182008-06-25 21:21:56 +0000521 /// getArgEffects - Returns a persistent ArgEffects object based on the
522 /// data in ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000523 ArgEffects* getArgEffects();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000524
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000525 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000526
527public:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000528 RetainSummary* getUnarySummary(FunctionDecl* FD, UnaryFuncKind func);
Ted Kremenek377e2302008-04-29 05:33:51 +0000529
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000530 RetainSummary* getNSSummary(FunctionDecl* FD, const char* FName);
531 RetainSummary* getCFSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000532 RetainSummary* getCGSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000533
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000534 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
535 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000536 RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000537
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000538 RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000539 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000540 ArgEffect DefaultEff = MayEscape,
541 bool isEndPath = false);
Ted Kremenek706522f2008-10-29 04:07:07 +0000542
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000543 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000544 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000545 ArgEffect DefaultEff = MayEscape) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000546 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek9c32d082008-05-06 00:30:21 +0000547 }
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000548
Ted Kremenek1bffd742008-05-06 15:44:25 +0000549 RetainSummary* getPersistentStopSummary() {
Ted Kremenek432af592008-05-06 18:11:36 +0000550 if (StopSummary)
551 return StopSummary;
552
553 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
554 StopTracking, StopTracking);
Ted Kremenek706522f2008-10-29 04:07:07 +0000555
Ted Kremenek432af592008-05-06 18:11:36 +0000556 return StopSummary;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000557 }
Ted Kremenekb3095252008-05-06 04:20:12 +0000558
Ted Kremenek553cf182008-06-25 21:21:56 +0000559 RetainSummary* getInitMethodSummary(ObjCMessageExpr* ME);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000560
Ted Kremenek1f180c32008-06-23 22:21:20 +0000561 void InitializeClassMethodSummaries();
562 void InitializeMethodSummaries();
Ted Kremenek896cd9d2008-10-23 01:56:15 +0000563
564private:
565
Ted Kremenek70a733e2008-07-18 17:24:20 +0000566 void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
567 RetainSummary* Summ) {
568 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
569 }
570
Ted Kremenek553cf182008-06-25 21:21:56 +0000571 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
572 ObjCClassMethodSummaries[S] = Summ;
573 }
574
575 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
576 ObjCMethodSummaries[S] = Summ;
577 }
578
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000579 void addInstMethSummary(const char* Cls, RetainSummary* Summ, va_list argp) {
Ted Kremenek70a733e2008-07-18 17:24:20 +0000580
Ted Kremenek9e476de2008-08-12 18:30:56 +0000581 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
582 llvm::SmallVector<IdentifierInfo*, 10> II;
583
584 while (const char* s = va_arg(argp, const char*))
585 II.push_back(&Ctx.Idents.get(s));
586
587 Selector S = Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek70a733e2008-07-18 17:24:20 +0000588 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
589 }
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000590
591 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
592 va_list argp;
593 va_start(argp, Summ);
594 addInstMethSummary(Cls, Summ, argp);
595 va_end(argp);
596 }
Ted Kremenek9e476de2008-08-12 18:30:56 +0000597
598 void addPanicSummary(const char* Cls, ...) {
599 RetainSummary* Summ = getPersistentSummary(0, RetEffect::MakeNoRet(),
600 DoNothing, DoNothing, true);
601 va_list argp;
602 va_start (argp, Cls);
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000603 addInstMethSummary(Cls, Summ, argp);
Ted Kremenek9e476de2008-08-12 18:30:56 +0000604 va_end(argp);
605 }
Ted Kremenek70a733e2008-07-18 17:24:20 +0000606
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000607public:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000608
609 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenek179064e2008-07-01 17:21:27 +0000610 : Ctx(ctx),
Ted Kremenek070a8252008-07-09 18:11:16 +0000611 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek553cf182008-06-25 21:21:56 +0000612 GCEnabled(gcenabled), StopSummary(0) {
613
614 InitializeClassMethodSummaries();
615 InitializeMethodSummaries();
616 }
Ted Kremenek377e2302008-04-29 05:33:51 +0000617
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000618 ~RetainSummaryManager();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000619
Ted Kremenekab592272008-06-24 03:56:45 +0000620 RetainSummary* getSummary(FunctionDecl* FD);
Ted Kremenek553cf182008-06-25 21:21:56 +0000621 RetainSummary* getMethodSummary(ObjCMessageExpr* ME, ObjCInterfaceDecl* ID);
Ted Kremenek1f180c32008-06-23 22:21:20 +0000622 RetainSummary* getClassMethodSummary(IdentifierInfo* ClsName, Selector S);
Ted Kremenekb3095252008-05-06 04:20:12 +0000623
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000624 bool isGCEnabled() const { return GCEnabled; }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000625};
626
627} // end anonymous namespace
628
629//===----------------------------------------------------------------------===//
630// Implementation of checker data structures.
631//===----------------------------------------------------------------------===//
632
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000633RetainSummaryManager::~RetainSummaryManager() {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000634
635 // FIXME: The ArgEffects could eventually be allocated from BPAlloc,
636 // mitigating the need to do explicit cleanup of the
637 // Argument-Effect summaries.
638
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000639 for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(),
640 E = ArgEffectsSet.end(); I!=E; ++I)
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000641 I->getValue().~ArgEffects();
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000642}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000643
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000644ArgEffects* RetainSummaryManager::getArgEffects() {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000645
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000646 if (ScratchArgs.empty())
647 return NULL;
648
649 // Compute a profile for a non-empty ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000650 llvm::FoldingSetNodeID profile;
651 profile.Add(ScratchArgs);
652 void* InsertPos;
653
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000654 // Look up the uniqued copy, or create a new one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000655 llvm::FoldingSetNodeWrapper<ArgEffects>* E =
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000656 ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000657
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000658 if (E) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000659 ScratchArgs.clear();
660 return &E->getValue();
661 }
662
663 E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
Ted Kremenek553cf182008-06-25 21:21:56 +0000664 BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000665
666 new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000667 ArgEffectsSet.InsertNode(E, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000668
669 ScratchArgs.clear();
670 return &E->getValue();
671}
672
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000673RetainSummary*
674RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000675 ArgEffect ReceiverEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000676 ArgEffect DefaultEff,
677 bool isEndPath) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000678
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000679 // Generate a profile for the summary.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000680 llvm::FoldingSetNodeID profile;
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000681 RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff,
682 isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000683
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000684 // Look up the uniqued summary, or create one if it doesn't exist.
685 void* InsertPos;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000686 RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000687
688 if (Summ)
689 return Summ;
690
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000691 // Create the summary and return it.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000692 Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek70a733e2008-07-18 17:24:20 +0000693 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000694 SummarySet.InsertNode(Summ, InsertPos);
695
696 return Summ;
697}
698
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000699//===----------------------------------------------------------------------===//
700// Summary creation for functions (largely uses of Core Foundation).
701//===----------------------------------------------------------------------===//
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000702
Ted Kremenekab592272008-06-24 03:56:45 +0000703RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000704
705 SourceLocation Loc = FD->getLocation();
706
707 if (!Loc.isFileID())
708 return NULL;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000709
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000710 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000711 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000712
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000713 if (I != FuncSummaries.end())
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000714 return I->second;
715
716 // No summary. Generate one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000717 const char* FName = FD->getIdentifier()->getName();
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000718
Ted Kremeneke1e91af2008-10-30 23:14:58 +0000719 RetainSummary *S = 0;
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000720 FunctionType* FT = dyn_cast<FunctionType>(FD->getType());
Ted Kremenek37d785b2008-07-15 16:50:12 +0000721
722 do {
723 if (FT) {
724
725 QualType T = FT->getResultType();
726
727 if (isCFRefType(T)) {
728 S = getCFSummary(FD, FName);
729 break;
730 }
731
732 if (isCGRefType(T)) {
733 S = getCGSummary(FD, FName );
734 break;
735 }
Ted Kremenek706522f2008-10-29 04:07:07 +0000736
737 // FIXME: This should all be refactored into a chain of "summary lookup"
738 // filters.
739 if (strcmp(FName, "IOServiceGetMatchingServices") == 0) {
740 // FIXES: <rdar://problem/6326900>
741 // This should be addressed using a API table. This strcmp is also
742 // a little gross, but there is no need to super optimize here.
743 assert (ScratchArgs.empty());
744 ScratchArgs.push_back(std::make_pair(1, DecRef));
745 S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
746 break;
747 }
Ted Kremenek37d785b2008-07-15 16:50:12 +0000748 }
Ted Kremenek6ca31912008-11-04 00:36:12 +0000749
750 // Ignore the prefix '_'
751 while (*FName == '_') ++FName;
Ted Kremenek37d785b2008-07-15 16:50:12 +0000752
Ted Kremenek64e859a2008-10-22 20:54:52 +0000753 if (FName[0] == 'C') {
754 if (FName[1] == 'F')
755 S = getCFSummary(FD, FName);
756 else if (FName[1] == 'G')
757 S = getCGSummary(FD, FName);
758 }
Ted Kremenek37d785b2008-07-15 16:50:12 +0000759 else if (FName[0] == 'N' && FName[1] == 'S')
760 S = getNSSummary(FD, FName);
761 }
762 while (0);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000763
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000764 FuncSummaries[FD] = S;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000765 return S;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000766}
767
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000768RetainSummary* RetainSummaryManager::getNSSummary(FunctionDecl* FD,
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000769 const char* FName) {
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000770 FName += 2;
771
772 if (strcmp(FName, "MakeCollectable") == 0)
773 return getUnarySummary(FD, cfmakecollectable);
774
775 return 0;
776}
Ted Kremenek37d785b2008-07-15 16:50:12 +0000777
778static bool isRetain(FunctionDecl* FD, const char* FName) {
Ted Kremenek10161bf2008-07-15 17:43:41 +0000779 const char* loc = strstr(FName, "Retain");
780 return loc && loc[sizeof("Retain")-1] == '\0';
Ted Kremenek37d785b2008-07-15 16:50:12 +0000781}
782
783static bool isRelease(FunctionDecl* FD, const char* FName) {
Ted Kremenek10161bf2008-07-15 17:43:41 +0000784 const char* loc = strstr(FName, "Release");
785 return loc && loc[sizeof("Release")-1] == '\0';
Ted Kremenek37d785b2008-07-15 16:50:12 +0000786}
787
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000788RetainSummary* RetainSummaryManager::getCFSummary(FunctionDecl* FD,
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000789 const char* FName) {
Ted Kremenekd07a7b12008-11-05 22:17:20 +0000790 if (FName[0] == 'C' && FName[1] == 'F')
791 FName += 2;
792
Ted Kremenek37d785b2008-07-15 16:50:12 +0000793 if (isRetain(FD, FName))
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000794 return getUnarySummary(FD, cfretain);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000795
Ted Kremenek37d785b2008-07-15 16:50:12 +0000796 if (isRelease(FD, FName))
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000797 return getUnarySummary(FD, cfrelease);
Ted Kremenek070a8252008-07-09 18:11:16 +0000798
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000799 if (strcmp(FName, "MakeCollectable") == 0)
800 return getUnarySummary(FD, cfmakecollectable);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000801
802 return getCFCreateGetRuleSummary(FD, FName);
803}
804
805RetainSummary* RetainSummaryManager::getCGSummary(FunctionDecl* FD,
806 const char* FName) {
807
Ted Kremenekd07a7b12008-11-05 22:17:20 +0000808 if (FName[0] == 'C' && FName[1] == 'G')
809 FName += 2;
810
Ted Kremenek37d785b2008-07-15 16:50:12 +0000811 if (isRelease(FD, FName))
812 return getUnarySummary(FD, cfrelease);
813
814 if (isRetain(FD, FName))
815 return getUnarySummary(FD, cfretain);
816
817 return getCFCreateGetRuleSummary(FD, FName);
818}
819
820RetainSummary*
821RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
822 const char* FName) {
823
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000824 if (strstr(FName, "Create") || strstr(FName, "Copy"))
825 return getCFSummaryCreateRule(FD);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000826
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000827 if (strstr(FName, "Get"))
828 return getCFSummaryGetRule(FD);
829
830 return 0;
831}
832
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000833RetainSummary*
834RetainSummaryManager::getUnarySummary(FunctionDecl* FD, UnaryFuncKind func) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000835
836 FunctionTypeProto* FT =
837 dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
838
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000839 if (FT) {
840
841 if (FT->getNumArgs() != 1)
842 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000843
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000844 TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr());
845
846 if (!ArgT)
847 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000848
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000849 if (!ArgT->isPointerType())
850 return NULL;
851 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000852
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000853 assert (ScratchArgs.empty());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000854
Ted Kremenek377e2302008-04-29 05:33:51 +0000855 switch (func) {
856 case cfretain: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000857 ScratchArgs.push_back(std::make_pair(0, IncRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000858 return getPersistentSummary(RetEffect::MakeAlias(0),
859 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000860 }
861
862 case cfrelease: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000863 ScratchArgs.push_back(std::make_pair(0, DecRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000864 return getPersistentSummary(RetEffect::MakeNoRet(),
865 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000866 }
867
868 case cfmakecollectable: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000869 if (GCEnabled)
870 ScratchArgs.push_back(std::make_pair(0, DecRef));
871
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000872 return getPersistentSummary(RetEffect::MakeAlias(0),
873 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000874 }
875
876 default:
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000877 assert (false && "Not a supported unary function.");
Ted Kremenek98530452008-08-12 20:41:56 +0000878 return 0;
Ted Kremenek940b1d82008-04-10 23:44:06 +0000879 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000880}
881
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000882RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000883
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000884 FunctionType* FT =
885 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000886
Ted Kremenek64e859a2008-10-22 20:54:52 +0000887 if (FT) {
888 QualType ResTy = FT->getResultType();
889
890 if (!isCFRefType(ResTy) && !isCGRefType(ResTy))
891 return getPersistentSummary(RetEffect::MakeNoRet());
892 }
893
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000894 assert (ScratchArgs.empty());
Ted Kremenek070a8252008-07-09 18:11:16 +0000895
896 if (FD->getIdentifier() == CFDictionaryCreateII) {
897 ScratchArgs.push_back(std::make_pair(1, DoNothingByRef));
898 ScratchArgs.push_back(std::make_pair(2, DoNothingByRef));
899 }
900
Ted Kremeneka7344702008-06-23 18:02:52 +0000901 return getPersistentSummary(RetEffect::MakeOwned(true));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000902}
903
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000904RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000905
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000906 FunctionType* FT =
907 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000908
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000909 if (FT) {
910 QualType RetTy = FT->getResultType();
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000911
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000912 // FIXME: For now we assume that all pointer types returned are referenced
913 // counted. Since this is the "Get" rule, we assume non-ownership, which
914 // works fine for things that are not reference counted. We do this because
915 // some generic data structures return "void*". We need something better
916 // in the future.
917
918 if (!isCFRefType(RetTy) && !RetTy->isPointerType())
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000919 return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000920 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000921
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000922 // FIXME: Add special-cases for functions that retain/release. For now
923 // just handle the default case.
924
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000925 assert (ScratchArgs.empty());
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000926 return getPersistentSummary(RetEffect::MakeNotOwned(), DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000927}
928
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000929//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000930// Summary creation for Selectors.
931//===----------------------------------------------------------------------===//
932
Ted Kremenek1bffd742008-05-06 15:44:25 +0000933RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000934RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) {
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000935 assert(ScratchArgs.empty());
936
937 RetainSummary* Summ =
Ted Kremenek9c32d082008-05-06 00:30:21 +0000938 getPersistentSummary(RetEffect::MakeReceiverAlias());
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000939
Ted Kremenek553cf182008-06-25 21:21:56 +0000940 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000941 return Summ;
942}
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000943
Ted Kremenek553cf182008-06-25 21:21:56 +0000944
Ted Kremenek1bffd742008-05-06 15:44:25 +0000945RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000946RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
947 ObjCInterfaceDecl* ID) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000948
949 Selector S = ME->getSelector();
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000950
Ted Kremenek553cf182008-06-25 21:21:56 +0000951 // Look up a summary in our summary cache.
952 ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000953
Ted Kremenek1f180c32008-06-23 22:21:20 +0000954 if (I != ObjCMethodSummaries.end())
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000955 return I->second;
Ted Kremenek553cf182008-06-25 21:21:56 +0000956
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000957 if (!ME->getType()->isPointerType())
958 return 0;
959
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000960 // "initXXX": pass-through for receiver.
961
962 const char* s = S.getIdentifierInfoForSlot(0)->getName();
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000963 assert (ScratchArgs.empty());
Ted Kremenekaee9e572008-05-06 06:09:09 +0000964
Ted Kremenek0327f772008-06-02 17:14:13 +0000965 if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0)
Ted Kremenek553cf182008-06-25 21:21:56 +0000966 return getInitMethodSummary(ME);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000967
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000968 // "copyXXX", "createXXX", "newXXX": allocators.
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000969
Ted Kremenek84060db2008-05-07 04:25:59 +0000970 if (!isNSType(ME->getReceiver()->getType()))
971 return 0;
972
Ted Kremenek9d1d5702008-10-24 21:22:44 +0000973 if (followsFundamentalRule(s)) {
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000974
975 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremeneka7344702008-06-23 18:02:52 +0000976 : RetEffect::MakeOwned(true);
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000977
978 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenek553cf182008-06-25 21:21:56 +0000979 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000980 return Summ;
981 }
Ted Kremenek1bffd742008-05-06 15:44:25 +0000982
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000983 return 0;
984}
985
Ted Kremenekc8395602008-05-06 21:26:51 +0000986RetainSummary*
Ted Kremenek1f180c32008-06-23 22:21:20 +0000987RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
988 Selector S) {
Ted Kremenekc8395602008-05-06 21:26:51 +0000989
Ted Kremenek553cf182008-06-25 21:21:56 +0000990 // FIXME: Eventually we should properly do class method summaries, but
991 // it requires us being able to walk the type hierarchy. Unfortunately,
992 // we cannot do this with just an IdentifierInfo* for the class name.
993
Ted Kremenekc8395602008-05-06 21:26:51 +0000994 // Look up a summary in our cache of Selectors -> Summaries.
Ted Kremenek553cf182008-06-25 21:21:56 +0000995 ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S);
Ted Kremenekc8395602008-05-06 21:26:51 +0000996
Ted Kremenek1f180c32008-06-23 22:21:20 +0000997 if (I != ObjCClassMethodSummaries.end())
Ted Kremenekc8395602008-05-06 21:26:51 +0000998 return I->second;
999
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00001000 return 0;
Ted Kremenekc8395602008-05-06 21:26:51 +00001001}
1002
Ted Kremenek1f180c32008-06-23 22:21:20 +00001003void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9c32d082008-05-06 00:30:21 +00001004
1005 assert (ScratchArgs.empty());
1006
Ted Kremeneka7344702008-06-23 18:02:52 +00001007 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
1008 : RetEffect::MakeOwned(true);
1009
Ted Kremenek9c32d082008-05-06 00:30:21 +00001010 RetainSummary* Summ = getPersistentSummary(E);
1011
Ted Kremenek553cf182008-06-25 21:21:56 +00001012 // Create the summaries for "alloc", "new", and "allocWithZone:" for
1013 // NSObject and its derivatives.
1014 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
1015 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
1016 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001017
1018 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001019 addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"),
Ted Kremenek1a804482008-07-18 18:14:26 +00001020 GetNullarySelector("currentHandler", Ctx),
Ted Kremenek6d348932008-10-21 15:53:15 +00001021 getPersistentSummary(RetEffect::MakeNotOwned()));
1022
1023 // Create the [NSAutoreleasePool addObject:] summary.
1024 if (!isGCEnabled()) {
1025 ScratchArgs.push_back(std::make_pair(0, Autorelease));
1026 addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"),
1027 GetUnarySelector("addObject", Ctx),
1028 getPersistentSummary(RetEffect::MakeNoRet(),
1029 DoNothing, DoNothing));
1030 }
Ted Kremenek9c32d082008-05-06 00:30:21 +00001031}
1032
Ted Kremenek1f180c32008-06-23 22:21:20 +00001033void RetainSummaryManager::InitializeMethodSummaries() {
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001034
1035 assert (ScratchArgs.empty());
1036
Ted Kremenekc8395602008-05-06 21:26:51 +00001037 // Create the "init" selector. It just acts as a pass-through for the
1038 // receiver.
Ted Kremenek179064e2008-07-01 17:21:27 +00001039 RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias());
1040 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
Ted Kremenekc8395602008-05-06 21:26:51 +00001041
1042 // The next methods are allocators.
Ted Kremeneka7344702008-06-23 18:02:52 +00001043 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
1044 : RetEffect::MakeOwned(true);
1045
Ted Kremenek179064e2008-07-01 17:21:27 +00001046 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenekc8395602008-05-06 21:26:51 +00001047
1048 // Create the "copy" selector.
Ted Kremenek98530452008-08-12 20:41:56 +00001049 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ);
1050
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001051 // Create the "mutableCopy" selector.
Ted Kremenek553cf182008-06-25 21:21:56 +00001052 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001053
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001054 // Create the "retain" selector.
1055 E = RetEffect::MakeReceiverAlias();
1056 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001057 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001058
1059 // Create the "release" selector.
1060 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001061 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Ted Kremenek299e8152008-05-07 21:17:39 +00001062
1063 // Create the "drain" selector.
1064 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001065 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001066
1067 // Create the "autorelease" selector.
Ted Kremeneke19f4492008-06-30 16:57:41 +00001068 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : Autorelease);
Ted Kremenek553cf182008-06-25 21:21:56 +00001069 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001070
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001071 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek179064e2008-07-01 17:21:27 +00001072 RetainSummary *NSWindowSumm =
1073 getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn);
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001074
1075 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1076 "styleMask", "backing", "defer", NULL);
1077
1078 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1079 "styleMask", "backing", "defer", "screen", NULL);
1080
1081 // For NSPanel (which subclasses NSWindow), allocated objects are not
1082 // self-owned.
1083 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1084 "styleMask", "backing", "defer", NULL);
1085
1086 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1087 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek553cf182008-06-25 21:21:56 +00001088
Ted Kremenek70a733e2008-07-18 17:24:20 +00001089 // Create NSAssertionHandler summaries.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001090 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
1091 "lineNumber", "description", NULL);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001092
Ted Kremenek9e476de2008-08-12 18:30:56 +00001093 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1094 "file", "lineNumber", "description", NULL);
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001095}
1096
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001097//===----------------------------------------------------------------------===//
Ted Kremenek13922612008-04-16 20:40:59 +00001098// Reference-counting logic (typestate + counts).
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001099//===----------------------------------------------------------------------===//
1100
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001101namespace {
1102
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001103class VISIBILITY_HIDDEN RefVal {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001104public:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001105
Ted Kremenek4fd88972008-04-17 18:12:53 +00001106 enum Kind {
1107 Owned = 0, // Owning reference.
1108 NotOwned, // Reference is not owned by still valid (not freed).
1109 Released, // Object has been released.
1110 ReturnedOwned, // Returned object passes ownership to caller.
1111 ReturnedNotOwned, // Return object does not pass ownership to caller.
1112 ErrorUseAfterRelease, // Object used after released.
1113 ErrorReleaseNotOwned, // Release of an object that was not owned.
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001114 ErrorLeak, // A memory leak due to excessive reference counts.
1115 ErrorLeakReturned // A memory leak due to the returning method not having
1116 // the correct naming conventions.
Ted Kremenek4fd88972008-04-17 18:12:53 +00001117 };
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001118
Ted Kremenek4fd88972008-04-17 18:12:53 +00001119private:
1120
1121 Kind kind;
1122 unsigned Cnt;
Ted Kremenek553cf182008-06-25 21:21:56 +00001123 QualType T;
1124
1125 RefVal(Kind k, unsigned cnt, QualType t) : kind(k), Cnt(cnt), T(t) {}
1126 RefVal(Kind k, unsigned cnt = 0) : kind(k), Cnt(cnt) {}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001127
1128public:
Ted Kremenekdb863712008-04-16 22:32:20 +00001129
Ted Kremenek4fd88972008-04-17 18:12:53 +00001130 Kind getKind() const { return kind; }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001131
Ted Kremenek553cf182008-06-25 21:21:56 +00001132 unsigned getCount() const { return Cnt; }
1133 QualType getType() const { return T; }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001134
1135 // Useful predicates.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001136
Ted Kremenek73c750b2008-03-11 18:14:09 +00001137 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
1138
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001139 static bool isLeak(Kind k) { return k >= ErrorLeak; }
Ted Kremenekdb863712008-04-16 22:32:20 +00001140
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001141 bool isOwned() const {
1142 return getKind() == Owned;
1143 }
1144
Ted Kremenekdb863712008-04-16 22:32:20 +00001145 bool isNotOwned() const {
1146 return getKind() == NotOwned;
1147 }
1148
Ted Kremenek4fd88972008-04-17 18:12:53 +00001149 bool isReturnedOwned() const {
1150 return getKind() == ReturnedOwned;
1151 }
1152
1153 bool isReturnedNotOwned() const {
1154 return getKind() == ReturnedNotOwned;
1155 }
1156
1157 bool isNonLeakError() const {
1158 Kind k = getKind();
1159 return isError(k) && !isLeak(k);
1160 }
1161
1162 // State creation: normal state.
1163
Ted Kremenek553cf182008-06-25 21:21:56 +00001164 static RefVal makeOwned(QualType t, unsigned Count = 1) {
1165 return RefVal(Owned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001166 }
1167
Ted Kremenek553cf182008-06-25 21:21:56 +00001168 static RefVal makeNotOwned(QualType t, unsigned Count = 0) {
1169 return RefVal(NotOwned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001170 }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001171
1172 static RefVal makeReturnedOwned(unsigned Count) {
1173 return RefVal(ReturnedOwned, Count);
1174 }
1175
1176 static RefVal makeReturnedNotOwned() {
1177 return RefVal(ReturnedNotOwned);
1178 }
1179
Ted Kremenek4fd88972008-04-17 18:12:53 +00001180 // Comparison, profiling, and pretty-printing.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001181
Ted Kremenek4fd88972008-04-17 18:12:53 +00001182 bool operator==(const RefVal& X) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001183 return kind == X.kind && Cnt == X.Cnt && T == X.T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001184 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001185
Ted Kremenek553cf182008-06-25 21:21:56 +00001186 RefVal operator-(size_t i) const {
1187 return RefVal(getKind(), getCount() - i, getType());
1188 }
1189
1190 RefVal operator+(size_t i) const {
1191 return RefVal(getKind(), getCount() + i, getType());
1192 }
1193
1194 RefVal operator^(Kind k) const {
1195 return RefVal(k, getCount(), getType());
1196 }
1197
1198
Ted Kremenek4fd88972008-04-17 18:12:53 +00001199 void Profile(llvm::FoldingSetNodeID& ID) const {
1200 ID.AddInteger((unsigned) kind);
1201 ID.AddInteger(Cnt);
Ted Kremenek553cf182008-06-25 21:21:56 +00001202 ID.Add(T);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001203 }
1204
Ted Kremenekf3948042008-03-11 19:44:10 +00001205 void print(std::ostream& Out) const;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001206};
Ted Kremenekf3948042008-03-11 19:44:10 +00001207
1208void RefVal::print(std::ostream& Out) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001209 if (!T.isNull())
1210 Out << "Tracked Type:" << T.getAsString() << '\n';
1211
Ted Kremenekf3948042008-03-11 19:44:10 +00001212 switch (getKind()) {
1213 default: assert(false);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001214 case Owned: {
1215 Out << "Owned";
1216 unsigned cnt = getCount();
1217 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001218 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001219 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001220
Ted Kremenek61b9f872008-04-10 23:09:18 +00001221 case NotOwned: {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001222 Out << "NotOwned";
Ted Kremenek61b9f872008-04-10 23:09:18 +00001223 unsigned cnt = getCount();
1224 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001225 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001226 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001227
Ted Kremenek4fd88972008-04-17 18:12:53 +00001228 case ReturnedOwned: {
1229 Out << "ReturnedOwned";
1230 unsigned cnt = getCount();
1231 if (cnt) Out << " (+ " << cnt << ")";
1232 break;
1233 }
1234
1235 case ReturnedNotOwned: {
1236 Out << "ReturnedNotOwned";
1237 unsigned cnt = getCount();
1238 if (cnt) Out << " (+ " << cnt << ")";
1239 break;
1240 }
1241
Ted Kremenekf3948042008-03-11 19:44:10 +00001242 case Released:
1243 Out << "Released";
1244 break;
1245
Ted Kremenekdb863712008-04-16 22:32:20 +00001246 case ErrorLeak:
1247 Out << "Leaked";
1248 break;
1249
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001250 case ErrorLeakReturned:
1251 Out << "Leaked (Bad naming)";
1252 break;
1253
Ted Kremenekf3948042008-03-11 19:44:10 +00001254 case ErrorUseAfterRelease:
1255 Out << "Use-After-Release [ERROR]";
1256 break;
1257
1258 case ErrorReleaseNotOwned:
1259 Out << "Release of Not-Owned [ERROR]";
1260 break;
1261 }
1262}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001263
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001264} // end anonymous namespace
1265
1266//===----------------------------------------------------------------------===//
1267// RefBindings - State used to track object reference counts.
1268//===----------------------------------------------------------------------===//
1269
1270typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001271static int RefBIndex = 0;
1272
1273namespace clang {
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001274 template<>
1275 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
1276 static inline void* GDMIndex() { return &RefBIndex; }
1277 };
1278}
Ted Kremenek6d348932008-10-21 15:53:15 +00001279
1280//===----------------------------------------------------------------------===//
1281// ARBindings - State used to track objects in autorelease pools.
1282//===----------------------------------------------------------------------===//
1283
1284typedef llvm::ImmutableSet<SymbolID> ARPoolContents;
1285typedef llvm::ImmutableList< std::pair<SymbolID, ARPoolContents*> > ARBindings;
1286static int AutoRBIndex = 0;
1287
1288namespace clang {
1289 template<>
1290 struct GRStateTrait<ARBindings> : public GRStatePartialTrait<ARBindings> {
1291 static inline void* GDMIndex() { return &AutoRBIndex; }
1292 };
1293}
1294
Ted Kremenek13922612008-04-16 20:40:59 +00001295//===----------------------------------------------------------------------===//
1296// Transfer functions.
1297//===----------------------------------------------------------------------===//
1298
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001299namespace {
1300
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001301class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenek8dd56462008-04-18 03:39:05 +00001302public:
Ted Kremenek553cf182008-06-25 21:21:56 +00001303 // Type definitions.
Ted Kremenek8dd56462008-04-18 03:39:05 +00001304 typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolID> >
1305 ReleasesNotOwnedTy;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001306
Ted Kremenek8dd56462008-04-18 03:39:05 +00001307 typedef ReleasesNotOwnedTy UseAfterReleasesTy;
1308
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001309 typedef llvm::DenseMap<GRExprEngine::NodeTy*,
1310 std::vector<std::pair<SymbolID,bool> >*>
Ted Kremenekdb863712008-04-16 22:32:20 +00001311 LeaksTy;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001312
Ted Kremenekae6814e2008-08-13 21:24:49 +00001313 class BindingsPrinter : public GRState::Printer {
Ted Kremenekf3948042008-03-11 19:44:10 +00001314 public:
Ted Kremenekae6814e2008-08-13 21:24:49 +00001315 virtual void Print(std::ostream& Out, const GRState* state,
1316 const char* nl, const char* sep);
Ted Kremenekf3948042008-03-11 19:44:10 +00001317 };
Ted Kremenek8dd56462008-04-18 03:39:05 +00001318
1319private:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001320 RetainSummaryManager Summaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001321 const LangOptions& LOpts;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001322
Ted Kremenek9e476de2008-08-12 18:30:56 +00001323 UseAfterReleasesTy UseAfterReleases;
1324 ReleasesNotOwnedTy ReleasesNotOwned;
1325 LeaksTy Leaks;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001326
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001327 RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001328 RefVal::Kind& hasErr, RefBindings::Factory& RefBFactory);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001329
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001330 RefVal::Kind& Update(GRStateRef& state, SymbolID sym, RefVal V,
1331 ArgEffect E, RefVal::Kind& hasErr) {
1332
1333 state = state.set<RefBindings>(Update(state.get<RefBindings>(), sym, V,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001334 E, hasErr,
1335 state.get_context<RefBindings>()));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001336 return hasErr;
1337 }
1338
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001339 void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1340 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001341 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001342 ExplodedNode<GRState>* Pred,
1343 const GRState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001344 RefVal::Kind hasErr, SymbolID Sym);
Ted Kremenekdb863712008-04-16 22:32:20 +00001345
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001346 std::pair<GRStateRef, bool>
1347 HandleSymbolDeath(GRStateManager& VMgr, const GRState* St,
1348 const Decl* CD, SymbolID sid, RefVal V, bool& hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001349
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001350public:
Ted Kremenek13922612008-04-16 20:40:59 +00001351
Ted Kremenek78d46242008-07-22 16:21:24 +00001352 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek377e2302008-04-29 05:33:51 +00001353 : Summaries(Ctx, gcenabled),
Ted Kremenek9e476de2008-08-12 18:30:56 +00001354 LOpts(lopts) {}
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001355
Ted Kremenek8dd56462008-04-18 03:39:05 +00001356 virtual ~CFRefCount() {
1357 for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I)
1358 delete I->second;
1359 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001360
1361 virtual void RegisterChecks(GRExprEngine& Eng);
Ted Kremenekf3948042008-03-11 19:44:10 +00001362
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001363 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1364 Printers.push_back(new BindingsPrinter());
Ted Kremenekf3948042008-03-11 19:44:10 +00001365 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001366
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001367 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenek072192b2008-04-30 23:47:44 +00001368 const LangOptions& getLangOptions() const { return LOpts; }
1369
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001370 // Calls.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001371
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001372 void EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001373 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001374 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001375 Expr* Ex,
1376 Expr* Receiver,
1377 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001378 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001379 ExplodedNode<GRState>* Pred);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001380
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001381 virtual void EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek199e1a02008-03-12 21:06:49 +00001382 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001383 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001384 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001385 ExplodedNode<GRState>* Pred);
Ted Kremenekfa34b332008-04-09 01:10:13 +00001386
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001387
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001388 virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001389 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001390 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001391 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001392 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001393
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001394 bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001395 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001396 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001397 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001398 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001399
Ted Kremenek13922612008-04-16 20:40:59 +00001400 // Stores.
1401
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001402 virtual void EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001403 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001404 GRStmtNodeBuilder<GRState>& Builder,
1405 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001406 const GRState* St, SVal TargetLV, SVal Val);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001407 // End-of-path.
1408
1409 virtual void EvalEndPath(GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001410 GREndPathNodeBuilder<GRState>& Builder);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001411
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001412 virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001413 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001414 GRStmtNodeBuilder<GRState>& Builder,
1415 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001416 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001417 const GRState* St,
1418 const GRStateManager::DeadSymbolsTy& Dead);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001419 // Return statements.
1420
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001421 virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001422 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001423 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001424 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001425 ExplodedNode<GRState>* Pred);
Ted Kremenekcb612922008-04-18 19:23:43 +00001426
1427 // Assumptions.
1428
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001429 virtual const GRState* EvalAssume(GRStateManager& VMgr,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001430 const GRState* St, SVal Cond,
Ted Kremenek4323a572008-07-10 22:03:41 +00001431 bool Assumption, bool& isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00001432
Ted Kremenekfa34b332008-04-09 01:10:13 +00001433 // Error iterators.
1434
1435 typedef UseAfterReleasesTy::iterator use_after_iterator;
1436 typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
Ted Kremenek989d5192008-04-17 23:43:50 +00001437 typedef LeaksTy::iterator leaks_iterator;
Ted Kremenekfa34b332008-04-09 01:10:13 +00001438
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001439 use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
1440 use_after_iterator use_after_end() { return UseAfterReleases.end(); }
Ted Kremenekfa34b332008-04-09 01:10:13 +00001441
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001442 bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
1443 bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
Ted Kremenek989d5192008-04-17 23:43:50 +00001444
1445 leaks_iterator leaks_begin() { return Leaks.begin(); }
1446 leaks_iterator leaks_end() { return Leaks.end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001447};
1448
1449} // end anonymous namespace
1450
Ted Kremenek8dd56462008-04-18 03:39:05 +00001451
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001452
1453
Ted Kremenekae6814e2008-08-13 21:24:49 +00001454void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state,
1455 const char* nl, const char* sep) {
1456
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001457 RefBindings B = state->get<RefBindings>();
Ted Kremenekf3948042008-03-11 19:44:10 +00001458
Ted Kremenekae6814e2008-08-13 21:24:49 +00001459 if (!B.isEmpty())
Ted Kremenekf3948042008-03-11 19:44:10 +00001460 Out << sep << nl;
1461
1462 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1463 Out << (*I).first << " : ";
1464 (*I).second.print(Out);
1465 Out << nl;
1466 }
1467}
1468
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001469static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) {
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001470 return Summ ? Summ->getArg(idx) : MayEscape;
Ted Kremenekf9561e52008-04-11 20:23:24 +00001471}
1472
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001473static inline RetEffect GetRetEffect(RetainSummary* Summ) {
1474 return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
Ted Kremenekf9561e52008-04-11 20:23:24 +00001475}
1476
Ted Kremenek14993892008-05-06 02:41:27 +00001477static inline ArgEffect GetReceiverE(RetainSummary* Summ) {
1478 return Summ ? Summ->getReceiverEffect() : DoNothing;
1479}
1480
Ted Kremenek70a733e2008-07-18 17:24:20 +00001481static inline bool IsEndPath(RetainSummary* Summ) {
1482 return Summ ? Summ->isEndPath() : false;
1483}
1484
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001485void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1486 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001487 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001488 ExplodedNode<GRState>* Pred,
1489 const GRState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001490 RefVal::Kind hasErr, SymbolID Sym) {
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001491 Builder.BuildSinks = true;
1492 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
1493
1494 if (!N) return;
1495
1496 switch (hasErr) {
1497 default: assert(false);
1498 case RefVal::ErrorUseAfterRelease:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001499 UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001500 break;
1501
1502 case RefVal::ErrorReleaseNotOwned:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001503 ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001504 break;
1505 }
1506}
1507
Ted Kremenek553cf182008-06-25 21:21:56 +00001508/// GetReturnType - Used to get the return type of a message expression or
1509/// function call with the intention of affixing that type to a tracked symbol.
1510/// While the the return type can be queried directly from RetEx, when
1511/// invoking class methods we augment to the return type to be that of
1512/// a pointer to the class (as opposed it just being id).
1513static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) {
1514
1515 QualType RetTy = RetE->getType();
1516
1517 // FIXME: We aren't handling id<...>.
Chris Lattner8b51fd72008-07-26 22:36:27 +00001518 const PointerType* PT = RetTy->getAsPointerType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001519 if (!PT)
1520 return RetTy;
1521
1522 // If RetEx is not a message expression just return its type.
1523 // If RetEx is a message expression, return its types if it is something
1524 /// more specific than id.
1525
1526 ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE);
1527
1528 if (!ME || !Ctx.isObjCIdType(PT->getPointeeType()))
1529 return RetTy;
1530
1531 ObjCInterfaceDecl* D = ME->getClassInfo().first;
1532
1533 // At this point we know the return type of the message expression is id.
1534 // If we have an ObjCInterceDecl, we know this is a call to a class method
1535 // whose type we can resolve. In such cases, promote the return type to
1536 // Class*.
1537 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
1538}
1539
1540
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001541void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001542 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001543 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001544 Expr* Ex,
1545 Expr* Receiver,
1546 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001547 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001548 ExplodedNode<GRState>* Pred) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001549
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001550 // Get the state.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001551 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001552 ASTContext& Ctx = Eng.getStateManager().getContext();
Ted Kremenek14993892008-05-06 02:41:27 +00001553
1554 // Evaluate the effect of the arguments.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001555 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001556 unsigned idx = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001557 Expr* ErrorExpr = NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001558 SymbolID ErrorSym = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001559
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001560 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001561 SVal V = state.GetSVal(*I);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001562
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001563 if (isa<loc::SymbolVal>(V)) {
1564 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001565 if (RefBindings::data_type* T = state.get<RefBindings>(Sym))
1566 if (Update(state, Sym, *T, GetArgE(Summ, idx), hasErr)) {
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001567 ErrorExpr = *I;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001568 ErrorSym = Sym;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001569 break;
1570 }
Ted Kremenekb8873552008-04-11 20:51:02 +00001571 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001572 else if (isa<Loc>(V)) {
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001573#if 0
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001574 // Nuke all arguments passed by reference.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001575 StateMgr.Unbind(StVals, cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001576#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001577 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenek070a8252008-07-09 18:11:16 +00001578
1579 if (GetArgE(Summ, idx) == DoNothingByRef)
1580 continue;
1581
1582 // Invalidate the value of the variable passed by reference.
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001583
1584 // FIXME: Either this logic should also be replicated in GRSimpleVals
1585 // or should be pulled into a separate "constraint engine."
Ted Kremenek070a8252008-07-09 18:11:16 +00001586
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001587 // FIXME: We can have collisions on the conjured symbol if the
1588 // expression *I also creates conjured symbols. We probably want
1589 // to identify conjured symbols by an expression pair: the enclosing
1590 // expression (the context) and the expression itself. This should
Ted Kremenek070a8252008-07-09 18:11:16 +00001591 // disambiguate conjured symbols.
1592
1593 // Is the invalidated variable something that we were tracking?
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001594 SVal X = state.GetSVal(*MR);
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001595
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001596 if (isa<loc::SymbolVal>(X)) {
1597 SymbolID Sym = cast<loc::SymbolVal>(X).getSymbol();
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001598 state = state.remove<RefBindings>(Sym);
Ted Kremenek070a8252008-07-09 18:11:16 +00001599 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001600
Ted Kremenek993f1c72008-10-17 20:28:54 +00001601 const TypedRegion* R = dyn_cast<TypedRegion>(MR->getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +00001602 if (R) {
1603 // Set the value of the variable to be a conjured symbol.
1604 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001605 QualType T = R->getType(Ctx);
Ted Kremenek9e240492008-10-04 05:50:14 +00001606
Ted Kremenekfd301942008-10-17 22:23:12 +00001607 // FIXME: handle structs.
1608 if (T->isIntegerType() || Loc::IsLocType(T)) {
1609 SymbolID NewSym =
1610 Eng.getSymbolManager().getConjuredSymbol(*I, T, Count);
1611
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001612 state = state.BindLoc(*MR,
Ted Kremenekfd301942008-10-17 22:23:12 +00001613 Loc::IsLocType(T)
1614 ? cast<SVal>(loc::SymbolVal(NewSym))
1615 : cast<SVal>(nonloc::SymbolVal(NewSym)));
1616 }
1617 else {
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001618 state = state.BindLoc(*MR, UnknownVal());
Ted Kremenekfd301942008-10-17 22:23:12 +00001619 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001620 }
1621 else
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001622 state = state.BindLoc(*MR, UnknownVal());
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001623 }
1624 else {
1625 // Nuke all other arguments passed by reference.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001626 state = state.Unbind(cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001627 }
1628#endif
Ted Kremenekb8873552008-04-11 20:51:02 +00001629 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001630 else if (isa<nonloc::LocAsInteger>(V))
1631 state = state.Unbind(cast<nonloc::LocAsInteger>(V).getLoc());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001632 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001633
Ted Kremenek553cf182008-06-25 21:21:56 +00001634 // Evaluate the effect on the message receiver.
Ted Kremenek14993892008-05-06 02:41:27 +00001635 if (!ErrorExpr && Receiver) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001636 SVal V = state.GetSVal(Receiver);
1637 if (isa<loc::SymbolVal>(V)) {
1638 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001639 if (const RefVal* T = state.get<RefBindings>(Sym))
1640 if (Update(state, Sym, *T, GetReceiverE(Summ), hasErr)) {
Ted Kremenek14993892008-05-06 02:41:27 +00001641 ErrorExpr = Receiver;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001642 ErrorSym = Sym;
Ted Kremenek14993892008-05-06 02:41:27 +00001643 }
Ted Kremenek14993892008-05-06 02:41:27 +00001644 }
1645 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001646
Ted Kremenek553cf182008-06-25 21:21:56 +00001647 // Process any errors.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001648 if (hasErr) {
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001649 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001650 hasErr, ErrorSym);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001651 return;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001652 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001653
Ted Kremenek70a733e2008-07-18 17:24:20 +00001654 // Consult the summary for the return value.
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001655 RetEffect RE = GetRetEffect(Summ);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001656
1657 switch (RE.getKind()) {
1658 default:
1659 assert (false && "Unhandled RetEffect."); break;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001660
Ted Kremenekfd301942008-10-17 22:23:12 +00001661 case RetEffect::NoRet: {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001662
Ted Kremenekf9561e52008-04-11 20:23:24 +00001663 // Make up a symbol for the return value (not reference counted).
Ted Kremenekb8873552008-04-11 20:51:02 +00001664 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
1665 // should compose behavior, not copy it.
Ted Kremenekf9561e52008-04-11 20:23:24 +00001666
Ted Kremenekfd301942008-10-17 22:23:12 +00001667 // FIXME: We eventually should handle structs and other compound types
1668 // that are returned by value.
1669
1670 QualType T = Ex->getType();
1671
1672 if (T->isIntegerType() || Loc::IsLocType(T)) {
Ted Kremenekf9561e52008-04-11 20:23:24 +00001673 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001674 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001675
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001676 SVal X = Loc::IsLocType(Ex->getType())
1677 ? cast<SVal>(loc::SymbolVal(Sym))
1678 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenekf9561e52008-04-11 20:23:24 +00001679
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001680 state = state.BindExpr(Ex, X, false);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001681 }
1682
Ted Kremenek940b1d82008-04-10 23:44:06 +00001683 break;
Ted Kremenekfd301942008-10-17 22:23:12 +00001684 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00001685
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001686 case RetEffect::Alias: {
Ted Kremenek553cf182008-06-25 21:21:56 +00001687 unsigned idx = RE.getIndex();
Ted Kremenek55499762008-06-17 02:43:46 +00001688 assert (arg_end >= arg_beg);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001689 assert (idx < (unsigned) (arg_end - arg_beg));
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001690 SVal V = state.GetSVal(*(arg_beg+idx));
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001691 state = state.BindExpr(Ex, V, false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001692 break;
1693 }
1694
Ted Kremenek14993892008-05-06 02:41:27 +00001695 case RetEffect::ReceiverAlias: {
1696 assert (Receiver);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001697 SVal V = state.GetSVal(Receiver);
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001698 state = state.BindExpr(Ex, V, false);
Ted Kremenek14993892008-05-06 02:41:27 +00001699 break;
1700 }
1701
Ted Kremeneka7344702008-06-23 18:02:52 +00001702 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001703 case RetEffect::OwnedSymbol: {
1704 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001705 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001706 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001707
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001708 state = state.set<RefBindings>(Sym, RefVal::makeOwned(RetT));
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001709 state = state.BindExpr(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001710
1711#if 0
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001712 RefBindings B = GetRefBindings(StImpl);
Ted Kremenek553cf182008-06-25 21:21:56 +00001713 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(RetT)));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001714#endif
1715
Ted Kremeneka7344702008-06-23 18:02:52 +00001716 // FIXME: Add a flag to the checker where allocations are allowed to fail.
1717 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol)
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001718 state = state.AddNE(Sym, Eng.getBasicVals().getZeroWithPtrWidth());
Ted Kremeneka7344702008-06-23 18:02:52 +00001719
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001720 break;
1721 }
1722
1723 case RetEffect::NotOwnedSymbol: {
1724 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001725 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001726 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001727
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001728 state = state.set<RefBindings>(Sym, RefVal::makeNotOwned(RetT));
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001729 state = state.BindExpr(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001730 break;
1731 }
1732 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001733
Ted Kremenek70a733e2008-07-18 17:24:20 +00001734 // Is this a sink?
1735 if (IsEndPath(Summ))
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001736 Builder.MakeSinkNode(Dst, Ex, Pred, state);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001737 else
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001738 Builder.MakeNode(Dst, Ex, Pred, state);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001739}
1740
1741
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001742void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001743 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001744 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001745 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001746 ExplodedNode<GRState>* Pred) {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001747
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001748 RetainSummary* Summ = !isa<loc::FuncVal>(L) ? 0
1749 : Summaries.getSummary(cast<loc::FuncVal>(L).getDecl());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001750
1751 EvalSummary(Dst, Eng, Builder, CE, 0, Summ,
1752 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenek2fff37e2008-03-06 00:08:09 +00001753}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001754
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001755void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001756 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001757 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001758 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001759 ExplodedNode<GRState>* Pred) {
Ted Kremenekb3095252008-05-06 04:20:12 +00001760 RetainSummary* Summ;
Ted Kremenek9040c652008-05-01 21:31:50 +00001761
Ted Kremenek553cf182008-06-25 21:21:56 +00001762 if (Expr* Receiver = ME->getReceiver()) {
1763 // We need the type-information of the tracked receiver object
1764 // Retrieve it from the state.
1765 ObjCInterfaceDecl* ID = 0;
1766
1767 // FIXME: Wouldn't it be great if this code could be reduced? It's just
1768 // a chain of lookups.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001769 const GRState* St = Builder.GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001770 SVal V = Eng.getStateManager().GetSVal(St, Receiver );
Ted Kremenek553cf182008-06-25 21:21:56 +00001771
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001772 if (isa<loc::SymbolVal>(V)) {
1773 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek553cf182008-06-25 21:21:56 +00001774
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001775 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001776 QualType Ty = T->getType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001777
1778 if (const PointerType* PT = Ty->getAsPointerType()) {
1779 QualType PointeeTy = PT->getPointeeType();
1780
1781 if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy))
1782 ID = IT->getDecl();
1783 }
1784 }
1785 }
1786
1787 Summ = Summaries.getMethodSummary(ME, ID);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001788
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001789 // Special-case: are we sending a mesage to "self"?
1790 // This is a hack. When we have full-IP this should be removed.
1791 if (!Summ) {
1792 ObjCMethodDecl* MD =
1793 dyn_cast<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl());
1794
1795 if (MD) {
1796 if (Expr* Receiver = ME->getReceiver()) {
1797 SVal X = Eng.getStateManager().GetSVal(St, Receiver);
1798 if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X))
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001799 if (L->getRegion() == Eng.getStateManager().getSelfRegion(St)) {
1800 // Create a summmary where all of the arguments "StopTracking".
1801 Summ = Summaries.getPersistentSummary(RetEffect::MakeNoRet(),
1802 DoNothing,
1803 StopTracking);
1804 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001805 }
1806 }
1807 }
Ted Kremenek553cf182008-06-25 21:21:56 +00001808 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001809 else
Ted Kremenek1f180c32008-06-23 22:21:20 +00001810 Summ = Summaries.getClassMethodSummary(ME->getClassName(),
1811 ME->getSelector());
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001812
Ted Kremenekb3095252008-05-06 04:20:12 +00001813 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ,
1814 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001815}
Ted Kremenekb3095252008-05-06 04:20:12 +00001816
Ted Kremenek13922612008-04-16 20:40:59 +00001817// Stores.
1818
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001819void CFRefCount::EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001820 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001821 GRStmtNodeBuilder<GRState>& Builder,
1822 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001823 const GRState* St, SVal TargetLV, SVal Val) {
Ted Kremenek13922612008-04-16 20:40:59 +00001824
1825 // Check if we have a binding for "Val" and if we are storing it to something
1826 // we don't understand or otherwise the value "escapes" the function.
1827
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001828 if (!isa<loc::SymbolVal>(Val))
Ted Kremenek13922612008-04-16 20:40:59 +00001829 return;
1830
1831 // Are we storing to something that causes the value to "escape"?
1832
1833 bool escapes = false;
1834
Ted Kremeneka496d162008-10-18 03:49:51 +00001835 // A value escapes in three possible cases (this may change):
1836 //
1837 // (1) we are binding to something that is not a memory region.
1838 // (2) we are binding to a memregion that does not have stack storage
1839 // (3) we are binding to a memregion with stack storage that the store
1840 // does not understand.
1841
1842 SymbolID Sym = cast<loc::SymbolVal>(Val).getSymbol();
1843 GRStateRef state(St, Eng.getStateManager());
1844
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001845 if (!isa<loc::MemRegionVal>(TargetLV))
Ted Kremenek13922612008-04-16 20:40:59 +00001846 escapes = true;
Ted Kremenek9e240492008-10-04 05:50:14 +00001847 else {
Ted Kremenek993f1c72008-10-17 20:28:54 +00001848 const MemRegion* R = cast<loc::MemRegionVal>(TargetLV).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00001849 escapes = !Eng.getStateManager().hasStackStorage(R);
Ted Kremeneka496d162008-10-18 03:49:51 +00001850
1851 if (!escapes) {
1852 // To test (3), generate a new state with the binding removed. If it is
1853 // the same state, then it escapes (since the store cannot represent
1854 // the binding).
Ted Kremeneka441b7e2008-11-12 19:22:09 +00001855 GRStateRef stateNew = state.BindLoc(cast<Loc>(TargetLV), Val);
Ted Kremeneka496d162008-10-18 03:49:51 +00001856 escapes = (stateNew == state);
1857 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001858 }
Ted Kremenek13922612008-04-16 20:40:59 +00001859
1860 if (!escapes)
1861 return;
Ted Kremeneka496d162008-10-18 03:49:51 +00001862
1863 // Do we have a reference count binding?
1864 // FIXME: Is this step even needed? We do blow away the binding anyway.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001865 if (!state.get<RefBindings>(Sym))
Ted Kremenek13922612008-04-16 20:40:59 +00001866 return;
1867
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001868 // Nuke the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001869 state = state.remove<RefBindings>(Sym);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001870
Ted Kremenek13922612008-04-16 20:40:59 +00001871 // Hand of the remaining logic to the parent implementation.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001872 GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, state, TargetLV, Val);
Ted Kremenekdb863712008-04-16 22:32:20 +00001873}
1874
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001875// End-of-path.
1876
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001877
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001878std::pair<GRStateRef,bool>
1879CFRefCount::HandleSymbolDeath(GRStateManager& VMgr,
1880 const GRState* St, const Decl* CD,
1881 SymbolID sid,
1882 RefVal V, bool& hasLeak) {
Ted Kremenekdb863712008-04-16 22:32:20 +00001883
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001884 GRStateRef state(St, VMgr);
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001885 assert ((!V.isReturnedOwned() || CD) &&
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001886 "CodeDecl must be available for reporting ReturnOwned errors.");
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001887
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001888 if (V.isReturnedOwned() && V.getCount() == 0)
1889 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
1890 std::string s = MD->getSelector().getName();
Ted Kremenek4c79e552008-11-05 16:54:44 +00001891 if (!followsReturnRule(s.c_str())) {
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001892 hasLeak = true;
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001893 state = state.set<RefBindings>(sid, V ^ RefVal::ErrorLeakReturned);
1894 return std::make_pair(state, true);
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001895 }
1896 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001897
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001898 // All other cases.
1899
1900 hasLeak = V.isOwned() ||
1901 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001902
Ted Kremenekdb863712008-04-16 22:32:20 +00001903 if (!hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001904 return std::make_pair(state.remove<RefBindings>(sid), false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001905
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001906 return std::make_pair(state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak),
1907 false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001908}
1909
1910void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001911 GREndPathNodeBuilder<GRState>& Builder) {
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001912
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001913 const GRState* St = Builder.getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001914 RefBindings B = St->get<RefBindings>();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001915
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001916 llvm::SmallVector<std::pair<SymbolID, bool>, 10> Leaked;
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001917 const Decl* CodeDecl = &Eng.getGraph().getCodeDecl();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001918
Ted Kremenekdb863712008-04-16 22:32:20 +00001919 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1920 bool hasLeak = false;
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001921
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001922 std::pair<GRStateRef, bool> X =
1923 HandleSymbolDeath(Eng.getStateManager(), St, CodeDecl,
1924 (*I).first, (*I).second, hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001925
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001926 St = X.first;
1927 if (hasLeak) Leaked.push_back(std::make_pair((*I).first, X.second));
Ted Kremenekdb863712008-04-16 22:32:20 +00001928 }
Ted Kremenek652adc62008-04-24 23:57:27 +00001929
1930 if (Leaked.empty())
1931 return;
1932
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001933 ExplodedNode<GRState>* N = Builder.MakeNode(St);
Ted Kremenek4f285152008-04-18 16:30:14 +00001934
Ted Kremenek652adc62008-04-24 23:57:27 +00001935 if (!N)
Ted Kremenek4f285152008-04-18 16:30:14 +00001936 return;
Ted Kremenekcb612922008-04-18 19:23:43 +00001937
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001938 std::vector<std::pair<SymbolID,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek8dd56462008-04-18 03:39:05 +00001939 assert (!LeaksAtNode);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001940 LeaksAtNode = new std::vector<std::pair<SymbolID,bool> >();
Ted Kremenekdb863712008-04-16 22:32:20 +00001941
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001942 for (llvm::SmallVector<std::pair<SymbolID,bool>, 10>::iterator
1943 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek8dd56462008-04-18 03:39:05 +00001944 (*LeaksAtNode).push_back(*I);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001945}
1946
Ted Kremenek652adc62008-04-24 23:57:27 +00001947// Dead symbols.
1948
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001949void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001950 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001951 GRStmtNodeBuilder<GRState>& Builder,
1952 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001953 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001954 const GRState* St,
1955 const GRStateManager::DeadSymbolsTy& Dead) {
Ted Kremenek910e9992008-04-25 01:25:15 +00001956
Ted Kremenek652adc62008-04-24 23:57:27 +00001957 // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
1958
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001959 RefBindings B = St->get<RefBindings>();
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001960 llvm::SmallVector<std::pair<SymbolID,bool>, 10> Leaked;
Ted Kremenek652adc62008-04-24 23:57:27 +00001961
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001962 for (GRStateManager::DeadSymbolsTy::const_iterator
Ted Kremenek652adc62008-04-24 23:57:27 +00001963 I=Dead.begin(), E=Dead.end(); I!=E; ++I) {
1964
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001965 const RefVal* T = B.lookup(*I);
Ted Kremenek652adc62008-04-24 23:57:27 +00001966
1967 if (!T)
1968 continue;
1969
1970 bool hasLeak = false;
1971
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001972 std::pair<GRStateRef, bool> X
1973 = HandleSymbolDeath(Eng.getStateManager(), St, 0, *I, *T, hasLeak);
1974
1975 St = X.first;
Ted Kremenek652adc62008-04-24 23:57:27 +00001976
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001977 if (hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001978 Leaked.push_back(std::make_pair(*I,X.second));
Ted Kremenek652adc62008-04-24 23:57:27 +00001979 }
1980
1981 if (Leaked.empty())
1982 return;
1983
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001984 ExplodedNode<GRState>* N = Builder.MakeNode(Dst, S, Pred, St);
Ted Kremenek652adc62008-04-24 23:57:27 +00001985
1986 if (!N)
1987 return;
1988
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001989 std::vector<std::pair<SymbolID,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek652adc62008-04-24 23:57:27 +00001990 assert (!LeaksAtNode);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001991 LeaksAtNode = new std::vector<std::pair<SymbolID,bool> >();
Ted Kremenek652adc62008-04-24 23:57:27 +00001992
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001993 for (llvm::SmallVector<std::pair<SymbolID,bool>, 10>::iterator
1994 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek652adc62008-04-24 23:57:27 +00001995 (*LeaksAtNode).push_back(*I);
1996}
1997
Ted Kremenek4fd88972008-04-17 18:12:53 +00001998 // Return statements.
1999
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002000void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00002001 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002002 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00002003 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002004 ExplodedNode<GRState>* Pred) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00002005
2006 Expr* RetE = S->getRetValue();
2007 if (!RetE) return;
2008
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002009 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002010 SVal V = state.GetSVal(RetE);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002011
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002012 if (!isa<loc::SymbolVal>(V))
Ted Kremenek4fd88972008-04-17 18:12:53 +00002013 return;
2014
2015 // Get the reference count binding (if any).
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002016 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002017 const RefVal* T = state.get<RefBindings>(Sym);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002018
2019 if (!T)
2020 return;
2021
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002022 // Change the reference count.
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002023 RefVal X = *T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00002024
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002025 switch (X.getKind()) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00002026 case RefVal::Owned: {
2027 unsigned cnt = X.getCount();
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002028 assert (cnt > 0);
2029 X = RefVal::makeReturnedOwned(cnt - 1);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002030 break;
2031 }
2032
2033 case RefVal::NotOwned: {
2034 unsigned cnt = X.getCount();
2035 X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
2036 : RefVal::makeReturnedNotOwned();
2037 break;
2038 }
2039
2040 default:
Ted Kremenek4fd88972008-04-17 18:12:53 +00002041 return;
2042 }
2043
2044 // Update the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002045 state = state.set<RefBindings>(Sym, X);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002046 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002047}
2048
Ted Kremenekcb612922008-04-18 19:23:43 +00002049// Assumptions.
2050
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002051const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr,
2052 const GRState* St,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002053 SVal Cond, bool Assumption,
Ted Kremenek4323a572008-07-10 22:03:41 +00002054 bool& isFeasible) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002055
2056 // FIXME: We may add to the interface of EvalAssume the list of symbols
2057 // whose assumptions have changed. For now we just iterate through the
2058 // bindings and check if any of the tracked symbols are NULL. This isn't
2059 // too bad since the number of symbols we will track in practice are
2060 // probably small and EvalAssume is only called at branches and a few
2061 // other places.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002062 RefBindings B = St->get<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002063
2064 if (B.isEmpty())
2065 return St;
2066
2067 bool changed = false;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002068
2069 GRStateRef state(St, VMgr);
2070 RefBindings::Factory& RefBFactory = state.get_context<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002071
2072 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002073 // Check if the symbol is null (or equal to any constant).
2074 // If this is the case, stop tracking the symbol.
Zhongxing Xu39cfed32008-08-29 14:52:36 +00002075 if (VMgr.getSymVal(St, I.getKey())) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002076 changed = true;
2077 B = RefBFactory.Remove(B, I.getKey());
2078 }
2079 }
2080
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002081 if (changed)
2082 state = state.set<RefBindings>(B);
Ted Kremenekcb612922008-04-18 19:23:43 +00002083
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002084 return state;
Ted Kremenekcb612922008-04-18 19:23:43 +00002085}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002086
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002087RefBindings CFRefCount::Update(RefBindings B, SymbolID sym,
2088 RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002089 RefVal::Kind& hasErr,
2090 RefBindings::Factory& RefBFactory) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002091
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002092 // FIXME: This dispatch can potentially be sped up by unifiying it into
2093 // a single switch statement. Opt for simplicity for now.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002094
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002095 switch (E) {
2096 default:
2097 assert (false && "Unhandled CFRef transition.");
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002098
2099 case MayEscape:
2100 if (V.getKind() == RefVal::Owned) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002101 V = V ^ RefVal::NotOwned;
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002102 break;
2103 }
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002104 // Fall-through.
Ted Kremenek070a8252008-07-09 18:11:16 +00002105 case DoNothingByRef:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002106 case DoNothing:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002107 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002108 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002109 hasErr = V.getKind();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002110 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002111 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002112 return B;
Ted Kremeneke19f4492008-06-30 16:57:41 +00002113
Ted Kremenek80d753f2008-07-01 00:01:02 +00002114 case Autorelease:
Ted Kremenek14993892008-05-06 02:41:27 +00002115 case StopTracking:
2116 return RefBFactory.Remove(B, sym);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002117
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002118 case IncRef:
2119 switch (V.getKind()) {
2120 default:
2121 assert(false);
2122
2123 case RefVal::Owned:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002124 case RefVal::NotOwned:
Ted Kremenek553cf182008-06-25 21:21:56 +00002125 V = V + 1;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002126 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002127 case RefVal::Released:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002128 if (isGCEnabled())
Ted Kremenek553cf182008-06-25 21:21:56 +00002129 V = V ^ RefVal::Owned;
Ted Kremenek65c91652008-04-29 05:44:10 +00002130 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002131 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek65c91652008-04-29 05:44:10 +00002132 hasErr = V.getKind();
2133 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002134 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002135 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002136 break;
2137
Ted Kremenek553cf182008-06-25 21:21:56 +00002138 case SelfOwn:
2139 V = V ^ RefVal::NotOwned;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002140 // Fall-through.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002141 case DecRef:
2142 switch (V.getKind()) {
2143 default:
2144 assert (false);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002145
Ted Kremenek553cf182008-06-25 21:21:56 +00002146 case RefVal::Owned:
2147 V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002148 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002149
Ted Kremenek553cf182008-06-25 21:21:56 +00002150 case RefVal::NotOwned:
2151 if (V.getCount() > 0)
2152 V = V - 1;
Ted Kremenek61b9f872008-04-10 23:09:18 +00002153 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002154 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002155 hasErr = V.getKind();
Ted Kremenek9e476de2008-08-12 18:30:56 +00002156 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002157 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002158
2159 case RefVal::Released:
Ted Kremenek553cf182008-06-25 21:21:56 +00002160 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002161 hasErr = V.getKind();
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002162 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002163 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002164 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002165 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002166 return RefBFactory.Add(B, sym, V);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002167}
2168
Ted Kremenekfa34b332008-04-09 01:10:13 +00002169//===----------------------------------------------------------------------===//
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002170// Error reporting.
Ted Kremenekfa34b332008-04-09 01:10:13 +00002171//===----------------------------------------------------------------------===//
2172
Ted Kremenek8dd56462008-04-18 03:39:05 +00002173namespace {
2174
2175 //===-------------===//
2176 // Bug Descriptions. //
2177 //===-------------===//
2178
Ted Kremenek95cc1ba2008-04-18 20:54:29 +00002179 class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002180 protected:
2181 CFRefCount& TF;
2182
2183 public:
2184 CFRefBug(CFRefCount& tf) : TF(tf) {}
Ted Kremenek072192b2008-04-30 23:47:44 +00002185
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002186 CFRefCount& getTF() { return TF; }
Ted Kremenek789deac2008-05-05 23:16:31 +00002187 const CFRefCount& getTF() const { return TF; }
2188
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002189 virtual bool isLeak() const { return false; }
Ted Kremenek8c036c72008-09-20 04:23:38 +00002190
2191 const char* getCategory() const {
Ted Kremenek062bae02008-09-27 22:02:42 +00002192 return "Memory (Core Foundation/Objective-C)";
Ted Kremenek8c036c72008-09-20 04:23:38 +00002193 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002194 };
2195
2196 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2197 public:
2198 UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
2199
2200 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002201 return "use-after-release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002202 }
2203 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002204 return "Reference-counted object is used after it is released.";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002205 }
2206
2207 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002208 };
2209
2210 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2211 public:
2212 BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
2213
2214 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002215 return "bad release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002216 }
2217 virtual const char* getDescription() const {
2218 return "Incorrect decrement of the reference count of a "
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002219 "CoreFoundation object: "
Ted Kremenek8dd56462008-04-18 03:39:05 +00002220 "The object is not owned at this point by the caller.";
2221 }
2222
2223 virtual void EmitWarnings(BugReporter& BR);
2224 };
2225
2226 class VISIBILITY_HIDDEN Leak : public CFRefBug {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002227 bool isReturn;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002228 public:
2229 Leak(CFRefCount& tf) : CFRefBug(tf) {}
2230
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002231 void setIsReturn(bool x) { isReturn = x; }
2232
Ted Kremenek8dd56462008-04-18 03:39:05 +00002233 virtual const char* getName() const {
Ted Kremenek432af592008-05-06 18:11:36 +00002234
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002235 if (!isReturn) {
2236 if (getTF().isGCEnabled())
2237 return "leak (GC)";
2238
2239 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
2240 return "leak (hybrid MM, non-GC)";
2241
2242 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
2243 return "leak";
2244 }
2245 else {
2246 if (getTF().isGCEnabled())
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002247 return "[naming convention] leak of returned object (GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002248
2249 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002250 return "[naming convention] leak of returned object (hybrid MM, "
2251 "non-GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002252
2253 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002254 return "[naming convention] leak of returned object";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002255 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002256 }
2257
2258 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002259 return "Object leaked";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002260 }
2261
2262 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002263 virtual void GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002264 virtual bool isLeak() const { return true; }
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002265 virtual bool isCached(BugReport& R);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002266 };
2267
2268 //===---------===//
2269 // Bug Reports. //
2270 //===---------===//
2271
2272 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
2273 SymbolID Sym;
2274 public:
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002275 CFRefReport(CFRefBug& D, ExplodedNode<GRState> *n, SymbolID sym)
Ted Kremenek8dd56462008-04-18 03:39:05 +00002276 : RangedBugReport(D, n), Sym(sym) {}
2277
2278 virtual ~CFRefReport() {}
2279
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002280 CFRefBug& getBugType() {
2281 return (CFRefBug&) RangedBugReport::getBugType();
2282 }
2283 const CFRefBug& getBugType() const {
2284 return (const CFRefBug&) RangedBugReport::getBugType();
2285 }
2286
2287 virtual void getRanges(BugReporter& BR, const SourceRange*& beg,
2288 const SourceRange*& end) {
2289
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002290 if (!getBugType().isLeak())
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002291 RangedBugReport::getRanges(BR, beg, end);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002292 else
2293 beg = end = 0;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002294 }
2295
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002296 SymbolID getSymbol() const { return Sym; }
2297
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002298 virtual PathDiagnosticPiece* getEndPath(BugReporter& BR,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002299 ExplodedNode<GRState>* N);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002300
Ted Kremenek072192b2008-04-30 23:47:44 +00002301 virtual std::pair<const char**,const char**> getExtraDescriptiveText();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002302
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002303 virtual PathDiagnosticPiece* VisitNode(ExplodedNode<GRState>* N,
2304 ExplodedNode<GRState>* PrevN,
2305 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002306 BugReporter& BR);
2307 };
2308
2309
2310} // end anonymous namespace
2311
2312void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002313 Eng.Register(new UseAfterRelease(*this));
2314 Eng.Register(new BadRelease(*this));
2315 Eng.Register(new Leak(*this));
2316}
2317
Ted Kremenek072192b2008-04-30 23:47:44 +00002318
2319static const char* Msgs[] = {
2320 "Code is compiled in garbage collection only mode" // GC only
2321 " (the bug occurs with garbage collection enabled).",
2322
2323 "Code is compiled without garbage collection.", // No GC.
2324
2325 "Code is compiled for use with and without garbage collection (GC)."
2326 " The bug occurs with GC enabled.", // Hybrid, with GC.
2327
2328 "Code is compiled for use with and without garbage collection (GC)."
2329 " The bug occurs in non-GC mode." // Hyrbird, without GC/
2330};
2331
2332std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2333 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
2334
2335 switch (TF.getLangOptions().getGCMode()) {
2336 default:
2337 assert(false);
Ted Kremenek31593ac2008-05-01 04:02:04 +00002338
2339 case LangOptions::GCOnly:
2340 assert (TF.isGCEnabled());
Ted Kremenek9e476de2008-08-12 18:30:56 +00002341 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2342
Ted Kremenek072192b2008-04-30 23:47:44 +00002343 case LangOptions::NonGC:
2344 assert (!TF.isGCEnabled());
Ted Kremenek072192b2008-04-30 23:47:44 +00002345 return std::make_pair(&Msgs[1], &Msgs[1]+1);
2346
2347 case LangOptions::HybridGC:
2348 if (TF.isGCEnabled())
2349 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2350 else
2351 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2352 }
2353}
2354
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002355PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<GRState>* N,
2356 ExplodedNode<GRState>* PrevN,
2357 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002358 BugReporter& BR) {
2359
2360 // Check if the type state has changed.
2361
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002362 const GRState* PrevSt = PrevN->getState();
2363 const GRState* CurrSt = N->getState();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002364
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002365 RefBindings PrevB = PrevSt->get<RefBindings>();
2366 RefBindings CurrB = CurrSt->get<RefBindings>();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002367
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002368 const RefVal* PrevT = PrevB.lookup(Sym);
2369 const RefVal* CurrT = CurrB.lookup(Sym);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002370
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002371 if (!CurrT)
2372 return NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002373
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002374 const char* Msg = NULL;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002375 const RefVal& CurrV = *CurrB.lookup(Sym);
Ted Kremenekce48e002008-05-05 17:53:17 +00002376
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002377 if (!PrevT) {
2378
Ted Kremenekce48e002008-05-05 17:53:17 +00002379 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2380
2381 if (CurrV.isOwned()) {
2382
2383 if (isa<CallExpr>(S))
2384 Msg = "Function call returns an object with a +1 retain count"
2385 " (owning reference).";
2386 else {
2387 assert (isa<ObjCMessageExpr>(S));
2388 Msg = "Method returns an object with a +1 retain count"
2389 " (owning reference).";
2390 }
2391 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002392 else {
2393 assert (CurrV.isNotOwned());
Ted Kremenekce48e002008-05-05 17:53:17 +00002394
2395 if (isa<CallExpr>(S))
2396 Msg = "Function call returns an object with a +0 retain count"
2397 " (non-owning reference).";
2398 else {
2399 assert (isa<ObjCMessageExpr>(S));
2400 Msg = "Method returns an object with a +0 retain count"
2401 " (non-owning reference).";
2402 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002403 }
Ted Kremenekce48e002008-05-05 17:53:17 +00002404
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002405 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2406 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2407
2408 if (Expr* Exp = dyn_cast<Expr>(S))
2409 P->addRange(Exp->getSourceRange());
2410
2411 return P;
2412 }
2413
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002414 // Determine if the typestate has changed.
2415 RefVal PrevV = *PrevB.lookup(Sym);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002416
2417 if (PrevV == CurrV)
2418 return NULL;
2419
2420 // The typestate has changed.
2421
2422 std::ostringstream os;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002423 std::string s;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002424
2425 switch (CurrV.getKind()) {
2426 case RefVal::Owned:
2427 case RefVal::NotOwned:
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002428
2429 if (PrevV.getCount() == CurrV.getCount())
2430 return 0;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002431
2432 if (PrevV.getCount() > CurrV.getCount())
2433 os << "Reference count decremented.";
2434 else
2435 os << "Reference count incremented.";
2436
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002437 if (unsigned Count = CurrV.getCount()) {
Ted Kremenekce48e002008-05-05 17:53:17 +00002438
2439 os << " Object has +" << Count;
Ted Kremenek79c140b2008-04-18 05:32:44 +00002440
Ted Kremenekce48e002008-05-05 17:53:17 +00002441 if (Count > 1)
2442 os << " retain counts.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002443 else
Ted Kremenekce48e002008-05-05 17:53:17 +00002444 os << " retain count.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002445 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002446
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002447 s = os.str();
2448 Msg = s.c_str();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002449
2450 break;
2451
2452 case RefVal::Released:
2453 Msg = "Object released.";
2454 break;
2455
2456 case RefVal::ReturnedOwned:
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002457 Msg = "Object returned to caller as an owning reference (single retain "
2458 "count transferred to caller).";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002459 break;
2460
2461 case RefVal::ReturnedNotOwned:
Ted Kremenekce48e002008-05-05 17:53:17 +00002462 Msg = "Object returned to caller with a +0 (non-owning) retain count.";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002463 break;
2464
2465 default:
2466 return NULL;
2467 }
2468
2469 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2470 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2471 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2472
2473 // Add the range by scanning the children of the statement for any bindings
2474 // to Sym.
2475
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002476 GRStateManager& VSM = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002477
2478 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
2479 if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002480 SVal X = VSM.GetSVal(CurrSt, Exp);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002481
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002482 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&X))
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002483 if (SV->getSymbol() == Sym) {
2484 P->addRange(Exp->getSourceRange()); break;
2485 }
2486 }
2487
2488 return P;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002489}
2490
Ted Kremenek9e240492008-10-04 05:50:14 +00002491namespace {
2492class VISIBILITY_HIDDEN FindUniqueBinding :
2493 public StoreManager::BindingsHandler {
2494 SymbolID Sym;
2495 MemRegion* Binding;
2496 bool First;
2497
2498 public:
2499 FindUniqueBinding(SymbolID sym) : Sym(sym), Binding(0), First(true) {}
2500
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002501 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal val) {
2502 if (const loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002503 if (SV->getSymbol() != Sym)
2504 return true;
2505 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002506 else if (const nonloc::SymbolVal* SV=dyn_cast<nonloc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002507 if (SV->getSymbol() != Sym)
2508 return true;
2509 }
2510 else
2511 return true;
2512
2513 if (Binding) {
2514 First = false;
2515 return false;
2516 }
2517 else
2518 Binding = R;
2519
2520 return true;
2521 }
2522
2523 operator bool() { return First && Binding; }
2524 MemRegion* getRegion() { return Binding; }
2525};
2526}
2527
2528static std::pair<ExplodedNode<GRState>*,MemRegion*>
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002529GetAllocationSite(GRStateManager* StateMgr, ExplodedNode<GRState>* N,
2530 SymbolID Sym) {
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002531
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002532 // Find both first node that referred to the tracked symbol and the
2533 // memory location that value was store to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002534 ExplodedNode<GRState>* Last = N;
Ted Kremenek9e240492008-10-04 05:50:14 +00002535 MemRegion* FirstBinding = 0;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002536
2537 while (N) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002538 const GRState* St = N->getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002539 RefBindings B = St->get<RefBindings>();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002540
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002541 if (!B.lookup(Sym))
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002542 break;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002543
2544 if (StateMgr) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002545 FindUniqueBinding FB(Sym);
2546 StateMgr->iterBindings(St, FB);
2547 if (FB) FirstBinding = FB.getRegion();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002548 }
2549
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002550 Last = N;
2551 N = N->pred_empty() ? NULL : *(N->pred_begin());
2552 }
2553
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002554 return std::make_pair(Last, FirstBinding);
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002555}
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002556
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002557PathDiagnosticPiece* CFRefReport::getEndPath(BugReporter& br,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002558 ExplodedNode<GRState>* EndN) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002559
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002560 GRBugReporter& BR = cast<GRBugReporter>(br);
2561
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002562 // Tell the BugReporter to report cases when the tracked symbol is
2563 // assigned to different variables, etc.
Ted Kremenekc0959972008-07-02 21:24:01 +00002564 cast<GRBugReporter>(BR).addNotableSymbol(Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002565
2566 if (!getBugType().isLeak())
Ted Kremeneke28565b2008-05-05 18:50:19 +00002567 return RangedBugReport::getEndPath(BR, EndN);
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002568
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002569 // We are a leak. Walk up the graph to get to the first node where the
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002570 // symbol appeared, and also get the first VarDecl that tracked object
2571 // is stored to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002572 ExplodedNode<GRState>* AllocNode = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +00002573 MemRegion* FirstBinding = 0;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002574
2575 llvm::tie(AllocNode, FirstBinding) =
2576 GetAllocationSite(&BR.getStateManager(), EndN, Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002577
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002578 // Get the allocate site.
2579 assert (AllocNode);
2580 Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002581
Ted Kremeneke28565b2008-05-05 18:50:19 +00002582 SourceManager& SMgr = BR.getContext().getSourceManager();
2583 unsigned AllocLine = SMgr.getLogicalLineNumber(FirstStmt->getLocStart());
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002584
Ted Kremeneke28565b2008-05-05 18:50:19 +00002585 // Get the leak site. We may have multiple ExplodedNodes (one with the
2586 // leak) that occur on the same line number; if the node with the leak
2587 // has any immediate predecessor nodes with the same line number, find
2588 // any transitive-successors that have a different statement and use that
2589 // line number instead. This avoids emiting a diagnostic like:
2590 //
2591 // // 'y' is leaked.
2592 // int x = foo(y);
2593 //
2594 // instead we want:
2595 //
2596 // int x = foo(y);
2597 // // 'y' is leaked.
2598
2599 Stmt* S = getStmt(BR); // This is the statement where the leak occured.
2600 assert (S);
2601 unsigned EndLine = SMgr.getLogicalLineNumber(S->getLocStart());
2602
2603 // Look in the *trimmed* graph at the immediate predecessor of EndN. Does
2604 // it occur on the same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002605 PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above;
Ted Kremeneke28565b2008-05-05 18:50:19 +00002606
2607 assert (!EndN->pred_empty()); // Not possible to have 0 predecessors.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002608 ExplodedNode<GRState> *Pred = *(EndN->pred_begin());
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002609 ProgramPoint PredPos = Pred->getLocation();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002610
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002611 if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) {
Ted Kremeneke28565b2008-05-05 18:50:19 +00002612
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002613 Stmt* SPred = PredPS->getStmt();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002614
2615 // Predecessor at same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002616 if (SMgr.getLogicalLineNumber(SPred->getLocStart()) != EndLine) {
2617 Hint = PathDiagnosticPiece::Below;
2618 S = SPred;
2619 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002620 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002621
2622 // Generate the diagnostic.
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002623 FullSourceLoc L( S->getLocStart(), SMgr);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002624 std::ostringstream os;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002625
Ted Kremeneke28565b2008-05-05 18:50:19 +00002626 os << "Object allocated on line " << AllocLine;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002627
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002628 if (FirstBinding)
Ted Kremenek9e240492008-10-04 05:50:14 +00002629 os << " and stored into '" << FirstBinding->getString() << '\'';
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002630
Ted Kremenek9e240492008-10-04 05:50:14 +00002631
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002632 // Get the retain count.
2633 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
2634
2635 if (RV->getKind() == RefVal::ErrorLeakReturned) {
2636 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BR.getGraph().getCodeDecl());
2637 os << " is returned from a method whose name ('"
2638 << MD.getSelector().getName()
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002639 << "') does not contain 'create' or 'copy' or otherwise starts with"
2640 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002641 " in the Memory Management Guide for Cocoa (object leaked).";
2642 }
2643 else
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002644 os << " is no longer referenced after this point and has a retain count of"
2645 " +"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002646 << RV->getCount() << " (object leaked).";
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002647
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002648 return new PathDiagnosticPiece(L, os.str(), Hint);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002649}
2650
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002651void UseAfterRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002652
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002653 for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
2654 E = TF.use_after_end(); I != E; ++I) {
2655
Ted Kremenek8dd56462008-04-18 03:39:05 +00002656 CFRefReport report(*this, I->first, I->second.second);
2657 report.addRange(I->second.first->getSourceRange());
Ted Kremenek75840e12008-04-18 01:56:37 +00002658 BR.EmitWarning(report);
Ted Kremenekfa34b332008-04-09 01:10:13 +00002659 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002660}
2661
2662void BadRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002663
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002664 for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
2665 E = TF.bad_release_end(); I != E; ++I) {
2666
Ted Kremenek8dd56462008-04-18 03:39:05 +00002667 CFRefReport report(*this, I->first, I->second.second);
2668 report.addRange(I->second.first->getSourceRange());
2669 BR.EmitWarning(report);
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002670 }
2671}
Ted Kremenekfa34b332008-04-09 01:10:13 +00002672
Ted Kremenek989d5192008-04-17 23:43:50 +00002673void Leak::EmitWarnings(BugReporter& BR) {
2674
2675 for (CFRefCount::leaks_iterator I = TF.leaks_begin(),
2676 E = TF.leaks_end(); I != E; ++I) {
2677
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002678 std::vector<std::pair<SymbolID, bool> >& SymV = *(I->second);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002679 unsigned n = SymV.size();
2680
2681 for (unsigned i = 0; i < n; ++i) {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002682 setIsReturn(SymV[i].second);
2683 CFRefReport report(*this, I->first, SymV[i].first);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002684 BR.EmitWarning(report);
2685 }
Ted Kremenek989d5192008-04-17 23:43:50 +00002686 }
2687}
2688
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002689void Leak::GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002690 for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end();
2691 I!=E; ++I)
2692 Nodes.push_back(I->first);
2693}
2694
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002695bool Leak::isCached(BugReport& R) {
2696
2697 // Most bug reports are cached at the location where they occured.
2698 // With leaks, we want to unique them by the location where they were
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002699 // allocated, and only report a single path.
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002700
2701 SymbolID Sym = static_cast<CFRefReport&>(R).getSymbol();
2702
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002703 ExplodedNode<GRState>* AllocNode =
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002704 GetAllocationSite(0, R.getEndNode(), Sym).first;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002705
2706 if (!AllocNode)
2707 return false;
2708
2709 return BugTypeCacheLocation::isCached(AllocNode->getLocation());
2710}
2711
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002712//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +00002713// Transfer function creation for external clients.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002714//===----------------------------------------------------------------------===//
2715
Ted Kremenek072192b2008-04-30 23:47:44 +00002716GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
2717 const LangOptions& lopts) {
Ted Kremenek78d46242008-07-22 16:21:24 +00002718 return new CFRefCount(Ctx, GCEnabled, lopts);
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +00002719}