blob: 1a38d957eb1124fc007b224d2956c6000a832b24 [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 Kremenek86ad3bc2008-05-05 16:51:50 +0000790
Ted Kremenek37d785b2008-07-15 16:50:12 +0000791 if (isRetain(FD, FName))
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000792 return getUnarySummary(FD, cfretain);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000793
Ted Kremenek37d785b2008-07-15 16:50:12 +0000794 if (isRelease(FD, FName))
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000795 return getUnarySummary(FD, cfrelease);
Ted Kremenek070a8252008-07-09 18:11:16 +0000796
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000797 if (strcmp(FName, "MakeCollectable") == 0)
798 return getUnarySummary(FD, cfmakecollectable);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000799
800 return getCFCreateGetRuleSummary(FD, FName);
801}
802
803RetainSummary* RetainSummaryManager::getCGSummary(FunctionDecl* FD,
804 const char* FName) {
805
Ted Kremenek37d785b2008-07-15 16:50:12 +0000806 if (isRelease(FD, FName))
807 return getUnarySummary(FD, cfrelease);
808
809 if (isRetain(FD, FName))
810 return getUnarySummary(FD, cfretain);
811
812 return getCFCreateGetRuleSummary(FD, FName);
813}
814
815RetainSummary*
816RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
817 const char* FName) {
818
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000819 if (strstr(FName, "Create") || strstr(FName, "Copy"))
820 return getCFSummaryCreateRule(FD);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000821
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000822 if (strstr(FName, "Get"))
823 return getCFSummaryGetRule(FD);
824
825 return 0;
826}
827
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000828RetainSummary*
829RetainSummaryManager::getUnarySummary(FunctionDecl* FD, UnaryFuncKind func) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000830
831 FunctionTypeProto* FT =
832 dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
833
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000834 if (FT) {
835
836 if (FT->getNumArgs() != 1)
837 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000838
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000839 TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr());
840
841 if (!ArgT)
842 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000843
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000844 if (!ArgT->isPointerType())
845 return NULL;
846 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000847
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000848 assert (ScratchArgs.empty());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000849
Ted Kremenek377e2302008-04-29 05:33:51 +0000850 switch (func) {
851 case cfretain: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000852 ScratchArgs.push_back(std::make_pair(0, IncRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000853 return getPersistentSummary(RetEffect::MakeAlias(0),
854 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000855 }
856
857 case cfrelease: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000858 ScratchArgs.push_back(std::make_pair(0, DecRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000859 return getPersistentSummary(RetEffect::MakeNoRet(),
860 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000861 }
862
863 case cfmakecollectable: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000864 if (GCEnabled)
865 ScratchArgs.push_back(std::make_pair(0, DecRef));
866
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000867 return getPersistentSummary(RetEffect::MakeAlias(0),
868 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000869 }
870
871 default:
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000872 assert (false && "Not a supported unary function.");
Ted Kremenek98530452008-08-12 20:41:56 +0000873 return 0;
Ted Kremenek940b1d82008-04-10 23:44:06 +0000874 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000875}
876
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000877RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000878
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000879 FunctionType* FT =
880 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000881
Ted Kremenek64e859a2008-10-22 20:54:52 +0000882 if (FT) {
883 QualType ResTy = FT->getResultType();
884
885 if (!isCFRefType(ResTy) && !isCGRefType(ResTy))
886 return getPersistentSummary(RetEffect::MakeNoRet());
887 }
888
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000889 assert (ScratchArgs.empty());
Ted Kremenek070a8252008-07-09 18:11:16 +0000890
891 if (FD->getIdentifier() == CFDictionaryCreateII) {
892 ScratchArgs.push_back(std::make_pair(1, DoNothingByRef));
893 ScratchArgs.push_back(std::make_pair(2, DoNothingByRef));
894 }
895
Ted Kremeneka7344702008-06-23 18:02:52 +0000896 return getPersistentSummary(RetEffect::MakeOwned(true));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000897}
898
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000899RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000900
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000901 FunctionType* FT =
902 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000903
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000904 if (FT) {
905 QualType RetTy = FT->getResultType();
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000906
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000907 // FIXME: For now we assume that all pointer types returned are referenced
908 // counted. Since this is the "Get" rule, we assume non-ownership, which
909 // works fine for things that are not reference counted. We do this because
910 // some generic data structures return "void*". We need something better
911 // in the future.
912
913 if (!isCFRefType(RetTy) && !RetTy->isPointerType())
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000914 return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000915 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000916
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000917 // FIXME: Add special-cases for functions that retain/release. For now
918 // just handle the default case.
919
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000920 assert (ScratchArgs.empty());
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000921 return getPersistentSummary(RetEffect::MakeNotOwned(), DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000922}
923
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000924//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000925// Summary creation for Selectors.
926//===----------------------------------------------------------------------===//
927
Ted Kremenek1bffd742008-05-06 15:44:25 +0000928RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000929RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) {
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000930 assert(ScratchArgs.empty());
931
932 RetainSummary* Summ =
Ted Kremenek9c32d082008-05-06 00:30:21 +0000933 getPersistentSummary(RetEffect::MakeReceiverAlias());
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000934
Ted Kremenek553cf182008-06-25 21:21:56 +0000935 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000936 return Summ;
937}
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000938
Ted Kremenek553cf182008-06-25 21:21:56 +0000939
Ted Kremenek1bffd742008-05-06 15:44:25 +0000940RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000941RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
942 ObjCInterfaceDecl* ID) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000943
944 Selector S = ME->getSelector();
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000945
Ted Kremenek553cf182008-06-25 21:21:56 +0000946 // Look up a summary in our summary cache.
947 ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000948
Ted Kremenek1f180c32008-06-23 22:21:20 +0000949 if (I != ObjCMethodSummaries.end())
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000950 return I->second;
Ted Kremenek553cf182008-06-25 21:21:56 +0000951
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000952 if (!ME->getType()->isPointerType())
953 return 0;
954
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000955 // "initXXX": pass-through for receiver.
956
957 const char* s = S.getIdentifierInfoForSlot(0)->getName();
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000958 assert (ScratchArgs.empty());
Ted Kremenekaee9e572008-05-06 06:09:09 +0000959
Ted Kremenek0327f772008-06-02 17:14:13 +0000960 if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0)
Ted Kremenek553cf182008-06-25 21:21:56 +0000961 return getInitMethodSummary(ME);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000962
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000963 // "copyXXX", "createXXX", "newXXX": allocators.
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000964
Ted Kremenek84060db2008-05-07 04:25:59 +0000965 if (!isNSType(ME->getReceiver()->getType()))
966 return 0;
967
Ted Kremenek9d1d5702008-10-24 21:22:44 +0000968 if (followsFundamentalRule(s)) {
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000969
970 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremeneka7344702008-06-23 18:02:52 +0000971 : RetEffect::MakeOwned(true);
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000972
973 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenek553cf182008-06-25 21:21:56 +0000974 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000975 return Summ;
976 }
Ted Kremenek1bffd742008-05-06 15:44:25 +0000977
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000978 return 0;
979}
980
Ted Kremenekc8395602008-05-06 21:26:51 +0000981RetainSummary*
Ted Kremenek1f180c32008-06-23 22:21:20 +0000982RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
983 Selector S) {
Ted Kremenekc8395602008-05-06 21:26:51 +0000984
Ted Kremenek553cf182008-06-25 21:21:56 +0000985 // FIXME: Eventually we should properly do class method summaries, but
986 // it requires us being able to walk the type hierarchy. Unfortunately,
987 // we cannot do this with just an IdentifierInfo* for the class name.
988
Ted Kremenekc8395602008-05-06 21:26:51 +0000989 // Look up a summary in our cache of Selectors -> Summaries.
Ted Kremenek553cf182008-06-25 21:21:56 +0000990 ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S);
Ted Kremenekc8395602008-05-06 21:26:51 +0000991
Ted Kremenek1f180c32008-06-23 22:21:20 +0000992 if (I != ObjCClassMethodSummaries.end())
Ted Kremenekc8395602008-05-06 21:26:51 +0000993 return I->second;
994
Ted Kremeneka22cc2f2008-05-06 23:07:13 +0000995 return 0;
Ted Kremenekc8395602008-05-06 21:26:51 +0000996}
997
Ted Kremenek1f180c32008-06-23 22:21:20 +0000998void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9c32d082008-05-06 00:30:21 +0000999
1000 assert (ScratchArgs.empty());
1001
Ted Kremeneka7344702008-06-23 18:02:52 +00001002 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
1003 : RetEffect::MakeOwned(true);
1004
Ted Kremenek9c32d082008-05-06 00:30:21 +00001005 RetainSummary* Summ = getPersistentSummary(E);
1006
Ted Kremenek553cf182008-06-25 21:21:56 +00001007 // Create the summaries for "alloc", "new", and "allocWithZone:" for
1008 // NSObject and its derivatives.
1009 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
1010 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
1011 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001012
1013 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001014 addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"),
Ted Kremenek1a804482008-07-18 18:14:26 +00001015 GetNullarySelector("currentHandler", Ctx),
Ted Kremenek6d348932008-10-21 15:53:15 +00001016 getPersistentSummary(RetEffect::MakeNotOwned()));
1017
1018 // Create the [NSAutoreleasePool addObject:] summary.
1019 if (!isGCEnabled()) {
1020 ScratchArgs.push_back(std::make_pair(0, Autorelease));
1021 addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"),
1022 GetUnarySelector("addObject", Ctx),
1023 getPersistentSummary(RetEffect::MakeNoRet(),
1024 DoNothing, DoNothing));
1025 }
Ted Kremenek9c32d082008-05-06 00:30:21 +00001026}
1027
Ted Kremenek1f180c32008-06-23 22:21:20 +00001028void RetainSummaryManager::InitializeMethodSummaries() {
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001029
1030 assert (ScratchArgs.empty());
1031
Ted Kremenekc8395602008-05-06 21:26:51 +00001032 // Create the "init" selector. It just acts as a pass-through for the
1033 // receiver.
Ted Kremenek179064e2008-07-01 17:21:27 +00001034 RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias());
1035 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
Ted Kremenekc8395602008-05-06 21:26:51 +00001036
1037 // The next methods are allocators.
Ted Kremeneka7344702008-06-23 18:02:52 +00001038 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
1039 : RetEffect::MakeOwned(true);
1040
Ted Kremenek179064e2008-07-01 17:21:27 +00001041 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenekc8395602008-05-06 21:26:51 +00001042
1043 // Create the "copy" selector.
Ted Kremenek98530452008-08-12 20:41:56 +00001044 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ);
1045
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001046 // Create the "mutableCopy" selector.
Ted Kremenek553cf182008-06-25 21:21:56 +00001047 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001048
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001049 // Create the "retain" selector.
1050 E = RetEffect::MakeReceiverAlias();
1051 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001052 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001053
1054 // Create the "release" selector.
1055 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001056 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Ted Kremenek299e8152008-05-07 21:17:39 +00001057
1058 // Create the "drain" selector.
1059 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001060 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001061
1062 // Create the "autorelease" selector.
Ted Kremeneke19f4492008-06-30 16:57:41 +00001063 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : Autorelease);
Ted Kremenek553cf182008-06-25 21:21:56 +00001064 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001065
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001066 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek179064e2008-07-01 17:21:27 +00001067 RetainSummary *NSWindowSumm =
1068 getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn);
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001069
1070 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1071 "styleMask", "backing", "defer", NULL);
1072
1073 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1074 "styleMask", "backing", "defer", "screen", NULL);
1075
1076 // For NSPanel (which subclasses NSWindow), allocated objects are not
1077 // self-owned.
1078 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1079 "styleMask", "backing", "defer", NULL);
1080
1081 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1082 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek553cf182008-06-25 21:21:56 +00001083
Ted Kremenek70a733e2008-07-18 17:24:20 +00001084 // Create NSAssertionHandler summaries.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001085 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
1086 "lineNumber", "description", NULL);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001087
Ted Kremenek9e476de2008-08-12 18:30:56 +00001088 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1089 "file", "lineNumber", "description", NULL);
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001090}
1091
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001092//===----------------------------------------------------------------------===//
Ted Kremenek13922612008-04-16 20:40:59 +00001093// Reference-counting logic (typestate + counts).
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001094//===----------------------------------------------------------------------===//
1095
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001096namespace {
1097
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001098class VISIBILITY_HIDDEN RefVal {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001099public:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001100
Ted Kremenek4fd88972008-04-17 18:12:53 +00001101 enum Kind {
1102 Owned = 0, // Owning reference.
1103 NotOwned, // Reference is not owned by still valid (not freed).
1104 Released, // Object has been released.
1105 ReturnedOwned, // Returned object passes ownership to caller.
1106 ReturnedNotOwned, // Return object does not pass ownership to caller.
1107 ErrorUseAfterRelease, // Object used after released.
1108 ErrorReleaseNotOwned, // Release of an object that was not owned.
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001109 ErrorLeak, // A memory leak due to excessive reference counts.
1110 ErrorLeakReturned // A memory leak due to the returning method not having
1111 // the correct naming conventions.
Ted Kremenek4fd88972008-04-17 18:12:53 +00001112 };
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001113
Ted Kremenek4fd88972008-04-17 18:12:53 +00001114private:
1115
1116 Kind kind;
1117 unsigned Cnt;
Ted Kremenek553cf182008-06-25 21:21:56 +00001118 QualType T;
1119
1120 RefVal(Kind k, unsigned cnt, QualType t) : kind(k), Cnt(cnt), T(t) {}
1121 RefVal(Kind k, unsigned cnt = 0) : kind(k), Cnt(cnt) {}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001122
1123public:
Ted Kremenekdb863712008-04-16 22:32:20 +00001124
Ted Kremenek4fd88972008-04-17 18:12:53 +00001125 Kind getKind() const { return kind; }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001126
Ted Kremenek553cf182008-06-25 21:21:56 +00001127 unsigned getCount() const { return Cnt; }
1128 QualType getType() const { return T; }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001129
1130 // Useful predicates.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001131
Ted Kremenek73c750b2008-03-11 18:14:09 +00001132 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
1133
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001134 static bool isLeak(Kind k) { return k >= ErrorLeak; }
Ted Kremenekdb863712008-04-16 22:32:20 +00001135
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001136 bool isOwned() const {
1137 return getKind() == Owned;
1138 }
1139
Ted Kremenekdb863712008-04-16 22:32:20 +00001140 bool isNotOwned() const {
1141 return getKind() == NotOwned;
1142 }
1143
Ted Kremenek4fd88972008-04-17 18:12:53 +00001144 bool isReturnedOwned() const {
1145 return getKind() == ReturnedOwned;
1146 }
1147
1148 bool isReturnedNotOwned() const {
1149 return getKind() == ReturnedNotOwned;
1150 }
1151
1152 bool isNonLeakError() const {
1153 Kind k = getKind();
1154 return isError(k) && !isLeak(k);
1155 }
1156
1157 // State creation: normal state.
1158
Ted Kremenek553cf182008-06-25 21:21:56 +00001159 static RefVal makeOwned(QualType t, unsigned Count = 1) {
1160 return RefVal(Owned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001161 }
1162
Ted Kremenek553cf182008-06-25 21:21:56 +00001163 static RefVal makeNotOwned(QualType t, unsigned Count = 0) {
1164 return RefVal(NotOwned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001165 }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001166
1167 static RefVal makeReturnedOwned(unsigned Count) {
1168 return RefVal(ReturnedOwned, Count);
1169 }
1170
1171 static RefVal makeReturnedNotOwned() {
1172 return RefVal(ReturnedNotOwned);
1173 }
1174
Ted Kremenek4fd88972008-04-17 18:12:53 +00001175 // Comparison, profiling, and pretty-printing.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001176
Ted Kremenek4fd88972008-04-17 18:12:53 +00001177 bool operator==(const RefVal& X) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001178 return kind == X.kind && Cnt == X.Cnt && T == X.T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001179 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001180
Ted Kremenek553cf182008-06-25 21:21:56 +00001181 RefVal operator-(size_t i) const {
1182 return RefVal(getKind(), getCount() - i, getType());
1183 }
1184
1185 RefVal operator+(size_t i) const {
1186 return RefVal(getKind(), getCount() + i, getType());
1187 }
1188
1189 RefVal operator^(Kind k) const {
1190 return RefVal(k, getCount(), getType());
1191 }
1192
1193
Ted Kremenek4fd88972008-04-17 18:12:53 +00001194 void Profile(llvm::FoldingSetNodeID& ID) const {
1195 ID.AddInteger((unsigned) kind);
1196 ID.AddInteger(Cnt);
Ted Kremenek553cf182008-06-25 21:21:56 +00001197 ID.Add(T);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001198 }
1199
Ted Kremenekf3948042008-03-11 19:44:10 +00001200 void print(std::ostream& Out) const;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001201};
Ted Kremenekf3948042008-03-11 19:44:10 +00001202
1203void RefVal::print(std::ostream& Out) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001204 if (!T.isNull())
1205 Out << "Tracked Type:" << T.getAsString() << '\n';
1206
Ted Kremenekf3948042008-03-11 19:44:10 +00001207 switch (getKind()) {
1208 default: assert(false);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001209 case Owned: {
1210 Out << "Owned";
1211 unsigned cnt = getCount();
1212 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001213 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001214 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001215
Ted Kremenek61b9f872008-04-10 23:09:18 +00001216 case NotOwned: {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001217 Out << "NotOwned";
Ted Kremenek61b9f872008-04-10 23:09:18 +00001218 unsigned cnt = getCount();
1219 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001220 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001221 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001222
Ted Kremenek4fd88972008-04-17 18:12:53 +00001223 case ReturnedOwned: {
1224 Out << "ReturnedOwned";
1225 unsigned cnt = getCount();
1226 if (cnt) Out << " (+ " << cnt << ")";
1227 break;
1228 }
1229
1230 case ReturnedNotOwned: {
1231 Out << "ReturnedNotOwned";
1232 unsigned cnt = getCount();
1233 if (cnt) Out << " (+ " << cnt << ")";
1234 break;
1235 }
1236
Ted Kremenekf3948042008-03-11 19:44:10 +00001237 case Released:
1238 Out << "Released";
1239 break;
1240
Ted Kremenekdb863712008-04-16 22:32:20 +00001241 case ErrorLeak:
1242 Out << "Leaked";
1243 break;
1244
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001245 case ErrorLeakReturned:
1246 Out << "Leaked (Bad naming)";
1247 break;
1248
Ted Kremenekf3948042008-03-11 19:44:10 +00001249 case ErrorUseAfterRelease:
1250 Out << "Use-After-Release [ERROR]";
1251 break;
1252
1253 case ErrorReleaseNotOwned:
1254 Out << "Release of Not-Owned [ERROR]";
1255 break;
1256 }
1257}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001258
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001259} // end anonymous namespace
1260
1261//===----------------------------------------------------------------------===//
1262// RefBindings - State used to track object reference counts.
1263//===----------------------------------------------------------------------===//
1264
1265typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001266static int RefBIndex = 0;
1267
1268namespace clang {
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001269 template<>
1270 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
1271 static inline void* GDMIndex() { return &RefBIndex; }
1272 };
1273}
Ted Kremenek6d348932008-10-21 15:53:15 +00001274
1275//===----------------------------------------------------------------------===//
1276// ARBindings - State used to track objects in autorelease pools.
1277//===----------------------------------------------------------------------===//
1278
1279typedef llvm::ImmutableSet<SymbolID> ARPoolContents;
1280typedef llvm::ImmutableList< std::pair<SymbolID, ARPoolContents*> > ARBindings;
1281static int AutoRBIndex = 0;
1282
1283namespace clang {
1284 template<>
1285 struct GRStateTrait<ARBindings> : public GRStatePartialTrait<ARBindings> {
1286 static inline void* GDMIndex() { return &AutoRBIndex; }
1287 };
1288}
1289
Ted Kremenek13922612008-04-16 20:40:59 +00001290//===----------------------------------------------------------------------===//
1291// Transfer functions.
1292//===----------------------------------------------------------------------===//
1293
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001294namespace {
1295
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001296class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenek8dd56462008-04-18 03:39:05 +00001297public:
Ted Kremenek553cf182008-06-25 21:21:56 +00001298 // Type definitions.
Ted Kremenek8dd56462008-04-18 03:39:05 +00001299 typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolID> >
1300 ReleasesNotOwnedTy;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001301
Ted Kremenek8dd56462008-04-18 03:39:05 +00001302 typedef ReleasesNotOwnedTy UseAfterReleasesTy;
1303
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001304 typedef llvm::DenseMap<GRExprEngine::NodeTy*,
1305 std::vector<std::pair<SymbolID,bool> >*>
Ted Kremenekdb863712008-04-16 22:32:20 +00001306 LeaksTy;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001307
Ted Kremenekae6814e2008-08-13 21:24:49 +00001308 class BindingsPrinter : public GRState::Printer {
Ted Kremenekf3948042008-03-11 19:44:10 +00001309 public:
Ted Kremenekae6814e2008-08-13 21:24:49 +00001310 virtual void Print(std::ostream& Out, const GRState* state,
1311 const char* nl, const char* sep);
Ted Kremenekf3948042008-03-11 19:44:10 +00001312 };
Ted Kremenek8dd56462008-04-18 03:39:05 +00001313
1314private:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001315 RetainSummaryManager Summaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001316 const LangOptions& LOpts;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001317
Ted Kremenek9e476de2008-08-12 18:30:56 +00001318 UseAfterReleasesTy UseAfterReleases;
1319 ReleasesNotOwnedTy ReleasesNotOwned;
1320 LeaksTy Leaks;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001321
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001322 RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001323 RefVal::Kind& hasErr, RefBindings::Factory& RefBFactory);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001324
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001325 RefVal::Kind& Update(GRStateRef& state, SymbolID sym, RefVal V,
1326 ArgEffect E, RefVal::Kind& hasErr) {
1327
1328 state = state.set<RefBindings>(Update(state.get<RefBindings>(), sym, V,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001329 E, hasErr,
1330 state.get_context<RefBindings>()));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001331 return hasErr;
1332 }
1333
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001334 void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1335 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001336 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001337 ExplodedNode<GRState>* Pred,
1338 const GRState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001339 RefVal::Kind hasErr, SymbolID Sym);
Ted Kremenekdb863712008-04-16 22:32:20 +00001340
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001341 std::pair<GRStateRef, bool>
1342 HandleSymbolDeath(GRStateManager& VMgr, const GRState* St,
1343 const Decl* CD, SymbolID sid, RefVal V, bool& hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001344
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001345public:
Ted Kremenek13922612008-04-16 20:40:59 +00001346
Ted Kremenek78d46242008-07-22 16:21:24 +00001347 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek377e2302008-04-29 05:33:51 +00001348 : Summaries(Ctx, gcenabled),
Ted Kremenek9e476de2008-08-12 18:30:56 +00001349 LOpts(lopts) {}
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001350
Ted Kremenek8dd56462008-04-18 03:39:05 +00001351 virtual ~CFRefCount() {
1352 for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I)
1353 delete I->second;
1354 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001355
1356 virtual void RegisterChecks(GRExprEngine& Eng);
Ted Kremenekf3948042008-03-11 19:44:10 +00001357
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001358 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1359 Printers.push_back(new BindingsPrinter());
Ted Kremenekf3948042008-03-11 19:44:10 +00001360 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001361
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001362 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenek072192b2008-04-30 23:47:44 +00001363 const LangOptions& getLangOptions() const { return LOpts; }
1364
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001365 // Calls.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001366
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001367 void EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001368 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001369 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001370 Expr* Ex,
1371 Expr* Receiver,
1372 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001373 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001374 ExplodedNode<GRState>* Pred);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001375
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001376 virtual void EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek199e1a02008-03-12 21:06:49 +00001377 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001378 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001379 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001380 ExplodedNode<GRState>* Pred);
Ted Kremenekfa34b332008-04-09 01:10:13 +00001381
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001382
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001383 virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001384 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001385 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001386 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001387 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001388
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001389 bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001390 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001391 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001392 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001393 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001394
Ted Kremenek13922612008-04-16 20:40:59 +00001395 // Stores.
1396
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001397 virtual void EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001398 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001399 GRStmtNodeBuilder<GRState>& Builder,
1400 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001401 const GRState* St, SVal TargetLV, SVal Val);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001402 // End-of-path.
1403
1404 virtual void EvalEndPath(GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001405 GREndPathNodeBuilder<GRState>& Builder);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001406
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001407 virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001408 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001409 GRStmtNodeBuilder<GRState>& Builder,
1410 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001411 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001412 const GRState* St,
1413 const GRStateManager::DeadSymbolsTy& Dead);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001414 // Return statements.
1415
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001416 virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001417 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001418 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001419 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001420 ExplodedNode<GRState>* Pred);
Ted Kremenekcb612922008-04-18 19:23:43 +00001421
1422 // Assumptions.
1423
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001424 virtual const GRState* EvalAssume(GRStateManager& VMgr,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001425 const GRState* St, SVal Cond,
Ted Kremenek4323a572008-07-10 22:03:41 +00001426 bool Assumption, bool& isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00001427
Ted Kremenekfa34b332008-04-09 01:10:13 +00001428 // Error iterators.
1429
1430 typedef UseAfterReleasesTy::iterator use_after_iterator;
1431 typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
Ted Kremenek989d5192008-04-17 23:43:50 +00001432 typedef LeaksTy::iterator leaks_iterator;
Ted Kremenekfa34b332008-04-09 01:10:13 +00001433
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001434 use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
1435 use_after_iterator use_after_end() { return UseAfterReleases.end(); }
Ted Kremenekfa34b332008-04-09 01:10:13 +00001436
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001437 bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
1438 bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
Ted Kremenek989d5192008-04-17 23:43:50 +00001439
1440 leaks_iterator leaks_begin() { return Leaks.begin(); }
1441 leaks_iterator leaks_end() { return Leaks.end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001442};
1443
1444} // end anonymous namespace
1445
Ted Kremenek8dd56462008-04-18 03:39:05 +00001446
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001447
1448
Ted Kremenekae6814e2008-08-13 21:24:49 +00001449void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state,
1450 const char* nl, const char* sep) {
1451
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001452 RefBindings B = state->get<RefBindings>();
Ted Kremenekf3948042008-03-11 19:44:10 +00001453
Ted Kremenekae6814e2008-08-13 21:24:49 +00001454 if (!B.isEmpty())
Ted Kremenekf3948042008-03-11 19:44:10 +00001455 Out << sep << nl;
1456
1457 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1458 Out << (*I).first << " : ";
1459 (*I).second.print(Out);
1460 Out << nl;
1461 }
1462}
1463
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001464static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) {
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001465 return Summ ? Summ->getArg(idx) : MayEscape;
Ted Kremenekf9561e52008-04-11 20:23:24 +00001466}
1467
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001468static inline RetEffect GetRetEffect(RetainSummary* Summ) {
1469 return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
Ted Kremenekf9561e52008-04-11 20:23:24 +00001470}
1471
Ted Kremenek14993892008-05-06 02:41:27 +00001472static inline ArgEffect GetReceiverE(RetainSummary* Summ) {
1473 return Summ ? Summ->getReceiverEffect() : DoNothing;
1474}
1475
Ted Kremenek70a733e2008-07-18 17:24:20 +00001476static inline bool IsEndPath(RetainSummary* Summ) {
1477 return Summ ? Summ->isEndPath() : false;
1478}
1479
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001480void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1481 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001482 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001483 ExplodedNode<GRState>* Pred,
1484 const GRState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001485 RefVal::Kind hasErr, SymbolID Sym) {
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001486 Builder.BuildSinks = true;
1487 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
1488
1489 if (!N) return;
1490
1491 switch (hasErr) {
1492 default: assert(false);
1493 case RefVal::ErrorUseAfterRelease:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001494 UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001495 break;
1496
1497 case RefVal::ErrorReleaseNotOwned:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001498 ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001499 break;
1500 }
1501}
1502
Ted Kremenek553cf182008-06-25 21:21:56 +00001503/// GetReturnType - Used to get the return type of a message expression or
1504/// function call with the intention of affixing that type to a tracked symbol.
1505/// While the the return type can be queried directly from RetEx, when
1506/// invoking class methods we augment to the return type to be that of
1507/// a pointer to the class (as opposed it just being id).
1508static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) {
1509
1510 QualType RetTy = RetE->getType();
1511
1512 // FIXME: We aren't handling id<...>.
Chris Lattner8b51fd72008-07-26 22:36:27 +00001513 const PointerType* PT = RetTy->getAsPointerType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001514 if (!PT)
1515 return RetTy;
1516
1517 // If RetEx is not a message expression just return its type.
1518 // If RetEx is a message expression, return its types if it is something
1519 /// more specific than id.
1520
1521 ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE);
1522
1523 if (!ME || !Ctx.isObjCIdType(PT->getPointeeType()))
1524 return RetTy;
1525
1526 ObjCInterfaceDecl* D = ME->getClassInfo().first;
1527
1528 // At this point we know the return type of the message expression is id.
1529 // If we have an ObjCInterceDecl, we know this is a call to a class method
1530 // whose type we can resolve. In such cases, promote the return type to
1531 // Class*.
1532 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
1533}
1534
1535
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001536void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001537 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001538 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001539 Expr* Ex,
1540 Expr* Receiver,
1541 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001542 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001543 ExplodedNode<GRState>* Pred) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001544
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001545 // Get the state.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001546 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001547 ASTContext& Ctx = Eng.getStateManager().getContext();
Ted Kremenek14993892008-05-06 02:41:27 +00001548
1549 // Evaluate the effect of the arguments.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001550 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001551 unsigned idx = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001552 Expr* ErrorExpr = NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001553 SymbolID ErrorSym = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001554
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001555 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001556 SVal V = state.GetSVal(*I);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001557
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001558 if (isa<loc::SymbolVal>(V)) {
1559 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001560 if (RefBindings::data_type* T = state.get<RefBindings>(Sym))
1561 if (Update(state, Sym, *T, GetArgE(Summ, idx), hasErr)) {
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001562 ErrorExpr = *I;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001563 ErrorSym = Sym;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001564 break;
1565 }
Ted Kremenekb8873552008-04-11 20:51:02 +00001566 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001567 else if (isa<Loc>(V)) {
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001568#if 0
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001569 // Nuke all arguments passed by reference.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001570 StateMgr.Unbind(StVals, cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001571#else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001572 if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
Ted Kremenek070a8252008-07-09 18:11:16 +00001573
1574 if (GetArgE(Summ, idx) == DoNothingByRef)
1575 continue;
1576
1577 // Invalidate the value of the variable passed by reference.
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001578
1579 // FIXME: Either this logic should also be replicated in GRSimpleVals
1580 // or should be pulled into a separate "constraint engine."
Ted Kremenek070a8252008-07-09 18:11:16 +00001581
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001582 // FIXME: We can have collisions on the conjured symbol if the
1583 // expression *I also creates conjured symbols. We probably want
1584 // to identify conjured symbols by an expression pair: the enclosing
1585 // expression (the context) and the expression itself. This should
Ted Kremenek070a8252008-07-09 18:11:16 +00001586 // disambiguate conjured symbols.
1587
1588 // Is the invalidated variable something that we were tracking?
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001589 SVal X = state.GetSVal(*MR);
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001590
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001591 if (isa<loc::SymbolVal>(X)) {
1592 SymbolID Sym = cast<loc::SymbolVal>(X).getSymbol();
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001593 state = state.remove<RefBindings>(Sym);
Ted Kremenek070a8252008-07-09 18:11:16 +00001594 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001595
Ted Kremenek993f1c72008-10-17 20:28:54 +00001596 const TypedRegion* R = dyn_cast<TypedRegion>(MR->getRegion());
Ted Kremenek9e240492008-10-04 05:50:14 +00001597 if (R) {
1598 // Set the value of the variable to be a conjured symbol.
1599 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001600 QualType T = R->getType(Ctx);
Ted Kremenek9e240492008-10-04 05:50:14 +00001601
Ted Kremenekfd301942008-10-17 22:23:12 +00001602 // FIXME: handle structs.
1603 if (T->isIntegerType() || Loc::IsLocType(T)) {
1604 SymbolID NewSym =
1605 Eng.getSymbolManager().getConjuredSymbol(*I, T, Count);
1606
1607 state = state.SetSVal(*MR,
1608 Loc::IsLocType(T)
1609 ? cast<SVal>(loc::SymbolVal(NewSym))
1610 : cast<SVal>(nonloc::SymbolVal(NewSym)));
1611 }
1612 else {
1613 state = state.SetSVal(*MR, UnknownVal());
1614 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001615 }
1616 else
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001617 state = state.SetSVal(*MR, UnknownVal());
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001618 }
1619 else {
1620 // Nuke all other arguments passed by reference.
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001621 state = state.Unbind(cast<Loc>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001622 }
1623#endif
Ted Kremenekb8873552008-04-11 20:51:02 +00001624 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001625 else if (isa<nonloc::LocAsInteger>(V))
1626 state = state.Unbind(cast<nonloc::LocAsInteger>(V).getLoc());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001627 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001628
Ted Kremenek553cf182008-06-25 21:21:56 +00001629 // Evaluate the effect on the message receiver.
Ted Kremenek14993892008-05-06 02:41:27 +00001630 if (!ErrorExpr && Receiver) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001631 SVal V = state.GetSVal(Receiver);
1632 if (isa<loc::SymbolVal>(V)) {
1633 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001634 if (const RefVal* T = state.get<RefBindings>(Sym))
1635 if (Update(state, Sym, *T, GetReceiverE(Summ), hasErr)) {
Ted Kremenek14993892008-05-06 02:41:27 +00001636 ErrorExpr = Receiver;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001637 ErrorSym = Sym;
Ted Kremenek14993892008-05-06 02:41:27 +00001638 }
Ted Kremenek14993892008-05-06 02:41:27 +00001639 }
1640 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001641
Ted Kremenek553cf182008-06-25 21:21:56 +00001642 // Process any errors.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001643 if (hasErr) {
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001644 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001645 hasErr, ErrorSym);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001646 return;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001647 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001648
Ted Kremenek70a733e2008-07-18 17:24:20 +00001649 // Consult the summary for the return value.
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001650 RetEffect RE = GetRetEffect(Summ);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001651
1652 switch (RE.getKind()) {
1653 default:
1654 assert (false && "Unhandled RetEffect."); break;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001655
Ted Kremenekfd301942008-10-17 22:23:12 +00001656 case RetEffect::NoRet: {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001657
Ted Kremenekf9561e52008-04-11 20:23:24 +00001658 // Make up a symbol for the return value (not reference counted).
Ted Kremenekb8873552008-04-11 20:51:02 +00001659 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
1660 // should compose behavior, not copy it.
Ted Kremenekf9561e52008-04-11 20:23:24 +00001661
Ted Kremenekfd301942008-10-17 22:23:12 +00001662 // FIXME: We eventually should handle structs and other compound types
1663 // that are returned by value.
1664
1665 QualType T = Ex->getType();
1666
1667 if (T->isIntegerType() || Loc::IsLocType(T)) {
Ted Kremenekf9561e52008-04-11 20:23:24 +00001668 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001669 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001670
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001671 SVal X = Loc::IsLocType(Ex->getType())
1672 ? cast<SVal>(loc::SymbolVal(Sym))
1673 : cast<SVal>(nonloc::SymbolVal(Sym));
Ted Kremenekf9561e52008-04-11 20:23:24 +00001674
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001675 state = state.SetSVal(Ex, X, false);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001676 }
1677
Ted Kremenek940b1d82008-04-10 23:44:06 +00001678 break;
Ted Kremenekfd301942008-10-17 22:23:12 +00001679 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00001680
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001681 case RetEffect::Alias: {
Ted Kremenek553cf182008-06-25 21:21:56 +00001682 unsigned idx = RE.getIndex();
Ted Kremenek55499762008-06-17 02:43:46 +00001683 assert (arg_end >= arg_beg);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001684 assert (idx < (unsigned) (arg_end - arg_beg));
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001685 SVal V = state.GetSVal(*(arg_beg+idx));
1686 state = state.SetSVal(Ex, V, false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001687 break;
1688 }
1689
Ted Kremenek14993892008-05-06 02:41:27 +00001690 case RetEffect::ReceiverAlias: {
1691 assert (Receiver);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001692 SVal V = state.GetSVal(Receiver);
1693 state = state.SetSVal(Ex, V, false);
Ted Kremenek14993892008-05-06 02:41:27 +00001694 break;
1695 }
1696
Ted Kremeneka7344702008-06-23 18:02:52 +00001697 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001698 case RetEffect::OwnedSymbol: {
1699 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001700 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001701 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001702
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001703 state = state.set<RefBindings>(Sym, RefVal::makeOwned(RetT));
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001704 state = state.SetSVal(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001705
1706#if 0
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001707 RefBindings B = GetRefBindings(StImpl);
Ted Kremenek553cf182008-06-25 21:21:56 +00001708 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(RetT)));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001709#endif
1710
Ted Kremeneka7344702008-06-23 18:02:52 +00001711 // FIXME: Add a flag to the checker where allocations are allowed to fail.
1712 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol)
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001713 state = state.AddNE(Sym, Eng.getBasicVals().getZeroWithPtrWidth());
Ted Kremeneka7344702008-06-23 18:02:52 +00001714
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001715 break;
1716 }
1717
1718 case RetEffect::NotOwnedSymbol: {
1719 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001720 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001721 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001722
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001723 state = state.set<RefBindings>(Sym, RefVal::makeNotOwned(RetT));
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001724 state = state.SetSVal(Ex, loc::SymbolVal(Sym), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001725 break;
1726 }
1727 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001728
Ted Kremenek70a733e2008-07-18 17:24:20 +00001729 // Is this a sink?
1730 if (IsEndPath(Summ))
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001731 Builder.MakeSinkNode(Dst, Ex, Pred, state);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001732 else
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001733 Builder.MakeNode(Dst, Ex, Pred, state);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001734}
1735
1736
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001737void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001738 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001739 GRStmtNodeBuilder<GRState>& Builder,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001740 CallExpr* CE, SVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001741 ExplodedNode<GRState>* Pred) {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001742
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001743 RetainSummary* Summ = !isa<loc::FuncVal>(L) ? 0
1744 : Summaries.getSummary(cast<loc::FuncVal>(L).getDecl());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001745
1746 EvalSummary(Dst, Eng, Builder, CE, 0, Summ,
1747 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenek2fff37e2008-03-06 00:08:09 +00001748}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001749
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001750void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001751 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001752 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001753 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001754 ExplodedNode<GRState>* Pred) {
Ted Kremenekb3095252008-05-06 04:20:12 +00001755 RetainSummary* Summ;
Ted Kremenek9040c652008-05-01 21:31:50 +00001756
Ted Kremenek553cf182008-06-25 21:21:56 +00001757 if (Expr* Receiver = ME->getReceiver()) {
1758 // We need the type-information of the tracked receiver object
1759 // Retrieve it from the state.
1760 ObjCInterfaceDecl* ID = 0;
1761
1762 // FIXME: Wouldn't it be great if this code could be reduced? It's just
1763 // a chain of lookups.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001764 const GRState* St = Builder.GetState(Pred);
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001765 SVal V = Eng.getStateManager().GetSVal(St, Receiver );
Ted Kremenek553cf182008-06-25 21:21:56 +00001766
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001767 if (isa<loc::SymbolVal>(V)) {
1768 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek553cf182008-06-25 21:21:56 +00001769
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001770 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001771 QualType Ty = T->getType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001772
1773 if (const PointerType* PT = Ty->getAsPointerType()) {
1774 QualType PointeeTy = PT->getPointeeType();
1775
1776 if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy))
1777 ID = IT->getDecl();
1778 }
1779 }
1780 }
1781
1782 Summ = Summaries.getMethodSummary(ME, ID);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001783
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001784 // Special-case: are we sending a mesage to "self"?
1785 // This is a hack. When we have full-IP this should be removed.
1786 if (!Summ) {
1787 ObjCMethodDecl* MD =
1788 dyn_cast<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl());
1789
1790 if (MD) {
1791 if (Expr* Receiver = ME->getReceiver()) {
1792 SVal X = Eng.getStateManager().GetSVal(St, Receiver);
1793 if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X))
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001794 if (L->getRegion() == Eng.getStateManager().getSelfRegion(St)) {
1795 // Create a summmary where all of the arguments "StopTracking".
1796 Summ = Summaries.getPersistentSummary(RetEffect::MakeNoRet(),
1797 DoNothing,
1798 StopTracking);
1799 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001800 }
1801 }
1802 }
Ted Kremenek553cf182008-06-25 21:21:56 +00001803 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001804 else
Ted Kremenek1f180c32008-06-23 22:21:20 +00001805 Summ = Summaries.getClassMethodSummary(ME->getClassName(),
1806 ME->getSelector());
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001807
Ted Kremenekb3095252008-05-06 04:20:12 +00001808 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ,
1809 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001810}
Ted Kremenekb3095252008-05-06 04:20:12 +00001811
Ted Kremenek13922612008-04-16 20:40:59 +00001812// Stores.
1813
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001814void CFRefCount::EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001815 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001816 GRStmtNodeBuilder<GRState>& Builder,
1817 Expr* E, ExplodedNode<GRState>* Pred,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001818 const GRState* St, SVal TargetLV, SVal Val) {
Ted Kremenek13922612008-04-16 20:40:59 +00001819
1820 // Check if we have a binding for "Val" and if we are storing it to something
1821 // we don't understand or otherwise the value "escapes" the function.
1822
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001823 if (!isa<loc::SymbolVal>(Val))
Ted Kremenek13922612008-04-16 20:40:59 +00001824 return;
1825
1826 // Are we storing to something that causes the value to "escape"?
1827
1828 bool escapes = false;
1829
Ted Kremeneka496d162008-10-18 03:49:51 +00001830 // A value escapes in three possible cases (this may change):
1831 //
1832 // (1) we are binding to something that is not a memory region.
1833 // (2) we are binding to a memregion that does not have stack storage
1834 // (3) we are binding to a memregion with stack storage that the store
1835 // does not understand.
1836
1837 SymbolID Sym = cast<loc::SymbolVal>(Val).getSymbol();
1838 GRStateRef state(St, Eng.getStateManager());
1839
Zhongxing Xu1c96b242008-10-17 05:57:07 +00001840 if (!isa<loc::MemRegionVal>(TargetLV))
Ted Kremenek13922612008-04-16 20:40:59 +00001841 escapes = true;
Ted Kremenek9e240492008-10-04 05:50:14 +00001842 else {
Ted Kremenek993f1c72008-10-17 20:28:54 +00001843 const MemRegion* R = cast<loc::MemRegionVal>(TargetLV).getRegion();
Ted Kremenek9e240492008-10-04 05:50:14 +00001844 escapes = !Eng.getStateManager().hasStackStorage(R);
Ted Kremeneka496d162008-10-18 03:49:51 +00001845
1846 if (!escapes) {
1847 // To test (3), generate a new state with the binding removed. If it is
1848 // the same state, then it escapes (since the store cannot represent
1849 // the binding).
1850 GRStateRef stateNew = state.SetSVal(cast<Loc>(TargetLV), Val);
1851 escapes = (stateNew == state);
1852 }
Ted Kremenek9e240492008-10-04 05:50:14 +00001853 }
Ted Kremenek13922612008-04-16 20:40:59 +00001854
1855 if (!escapes)
1856 return;
Ted Kremeneka496d162008-10-18 03:49:51 +00001857
1858 // Do we have a reference count binding?
1859 // FIXME: Is this step even needed? We do blow away the binding anyway.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001860 if (!state.get<RefBindings>(Sym))
Ted Kremenek13922612008-04-16 20:40:59 +00001861 return;
1862
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001863 // Nuke the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001864 state = state.remove<RefBindings>(Sym);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001865
Ted Kremenek13922612008-04-16 20:40:59 +00001866 // Hand of the remaining logic to the parent implementation.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001867 GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, state, TargetLV, Val);
Ted Kremenekdb863712008-04-16 22:32:20 +00001868}
1869
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001870// End-of-path.
1871
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001872
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001873std::pair<GRStateRef,bool>
1874CFRefCount::HandleSymbolDeath(GRStateManager& VMgr,
1875 const GRState* St, const Decl* CD,
1876 SymbolID sid,
1877 RefVal V, bool& hasLeak) {
Ted Kremenekdb863712008-04-16 22:32:20 +00001878
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001879 GRStateRef state(St, VMgr);
Sanjiv Gupta31fc07d2008-10-31 09:52:39 +00001880 assert ((!V.isReturnedOwned() || CD) &&
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001881 "CodeDecl must be available for reporting ReturnOwned errors.");
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001882
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001883 if (V.isReturnedOwned() && V.getCount() == 0)
1884 if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
1885 std::string s = MD->getSelector().getName();
Ted Kremenek4c79e552008-11-05 16:54:44 +00001886 if (!followsReturnRule(s.c_str())) {
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001887 hasLeak = true;
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001888 state = state.set<RefBindings>(sid, V ^ RefVal::ErrorLeakReturned);
1889 return std::make_pair(state, true);
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001890 }
1891 }
Ted Kremenek896cd9d2008-10-23 01:56:15 +00001892
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001893 // All other cases.
1894
1895 hasLeak = V.isOwned() ||
1896 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001897
Ted Kremenekdb863712008-04-16 22:32:20 +00001898 if (!hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001899 return std::make_pair(state.remove<RefBindings>(sid), false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001900
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001901 return std::make_pair(state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak),
1902 false);
Ted Kremenekdb863712008-04-16 22:32:20 +00001903}
1904
1905void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001906 GREndPathNodeBuilder<GRState>& Builder) {
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001907
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001908 const GRState* St = Builder.getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001909 RefBindings B = St->get<RefBindings>();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001910
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001911 llvm::SmallVector<std::pair<SymbolID, bool>, 10> Leaked;
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00001912 const Decl* CodeDecl = &Eng.getGraph().getCodeDecl();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001913
Ted Kremenekdb863712008-04-16 22:32:20 +00001914 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1915 bool hasLeak = false;
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001916
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001917 std::pair<GRStateRef, bool> X =
1918 HandleSymbolDeath(Eng.getStateManager(), St, CodeDecl,
1919 (*I).first, (*I).second, hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001920
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001921 St = X.first;
1922 if (hasLeak) Leaked.push_back(std::make_pair((*I).first, X.second));
Ted Kremenekdb863712008-04-16 22:32:20 +00001923 }
Ted Kremenek652adc62008-04-24 23:57:27 +00001924
1925 if (Leaked.empty())
1926 return;
1927
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001928 ExplodedNode<GRState>* N = Builder.MakeNode(St);
Ted Kremenek4f285152008-04-18 16:30:14 +00001929
Ted Kremenek652adc62008-04-24 23:57:27 +00001930 if (!N)
Ted Kremenek4f285152008-04-18 16:30:14 +00001931 return;
Ted Kremenekcb612922008-04-18 19:23:43 +00001932
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001933 std::vector<std::pair<SymbolID,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek8dd56462008-04-18 03:39:05 +00001934 assert (!LeaksAtNode);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001935 LeaksAtNode = new std::vector<std::pair<SymbolID,bool> >();
Ted Kremenekdb863712008-04-16 22:32:20 +00001936
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001937 for (llvm::SmallVector<std::pair<SymbolID,bool>, 10>::iterator
1938 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek8dd56462008-04-18 03:39:05 +00001939 (*LeaksAtNode).push_back(*I);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001940}
1941
Ted Kremenek652adc62008-04-24 23:57:27 +00001942// Dead symbols.
1943
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001944void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001945 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001946 GRStmtNodeBuilder<GRState>& Builder,
1947 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001948 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001949 const GRState* St,
1950 const GRStateManager::DeadSymbolsTy& Dead) {
Ted Kremenek910e9992008-04-25 01:25:15 +00001951
Ted Kremenek652adc62008-04-24 23:57:27 +00001952 // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
1953
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001954 RefBindings B = St->get<RefBindings>();
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001955 llvm::SmallVector<std::pair<SymbolID,bool>, 10> Leaked;
Ted Kremenek652adc62008-04-24 23:57:27 +00001956
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001957 for (GRStateManager::DeadSymbolsTy::const_iterator
Ted Kremenek652adc62008-04-24 23:57:27 +00001958 I=Dead.begin(), E=Dead.end(); I!=E; ++I) {
1959
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001960 const RefVal* T = B.lookup(*I);
Ted Kremenek652adc62008-04-24 23:57:27 +00001961
1962 if (!T)
1963 continue;
1964
1965 bool hasLeak = false;
1966
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001967 std::pair<GRStateRef, bool> X
1968 = HandleSymbolDeath(Eng.getStateManager(), St, 0, *I, *T, hasLeak);
1969
1970 St = X.first;
Ted Kremenek652adc62008-04-24 23:57:27 +00001971
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001972 if (hasLeak)
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001973 Leaked.push_back(std::make_pair(*I,X.second));
Ted Kremenek652adc62008-04-24 23:57:27 +00001974 }
1975
1976 if (Leaked.empty())
1977 return;
1978
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001979 ExplodedNode<GRState>* N = Builder.MakeNode(Dst, S, Pred, St);
Ted Kremenek652adc62008-04-24 23:57:27 +00001980
1981 if (!N)
1982 return;
1983
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001984 std::vector<std::pair<SymbolID,bool> >*& LeaksAtNode = Leaks[N];
Ted Kremenek652adc62008-04-24 23:57:27 +00001985 assert (!LeaksAtNode);
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001986 LeaksAtNode = new std::vector<std::pair<SymbolID,bool> >();
Ted Kremenek652adc62008-04-24 23:57:27 +00001987
Ted Kremenekf9790ae2008-10-24 20:32:50 +00001988 for (llvm::SmallVector<std::pair<SymbolID,bool>, 10>::iterator
1989 I = Leaked.begin(), E = Leaked.end(); I != E; ++I)
Ted Kremenek652adc62008-04-24 23:57:27 +00001990 (*LeaksAtNode).push_back(*I);
1991}
1992
Ted Kremenek4fd88972008-04-17 18:12:53 +00001993 // Return statements.
1994
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001995void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001996 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001997 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001998 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001999 ExplodedNode<GRState>* Pred) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00002000
2001 Expr* RetE = S->getRetValue();
2002 if (!RetE) return;
2003
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002004 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002005 SVal V = state.GetSVal(RetE);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002006
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002007 if (!isa<loc::SymbolVal>(V))
Ted Kremenek4fd88972008-04-17 18:12:53 +00002008 return;
2009
2010 // Get the reference count binding (if any).
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002011 SymbolID Sym = cast<loc::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002012 const RefVal* T = state.get<RefBindings>(Sym);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002013
2014 if (!T)
2015 return;
2016
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002017 // Change the reference count.
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002018 RefVal X = *T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00002019
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002020 switch (X.getKind()) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00002021 case RefVal::Owned: {
2022 unsigned cnt = X.getCount();
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002023 assert (cnt > 0);
2024 X = RefVal::makeReturnedOwned(cnt - 1);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002025 break;
2026 }
2027
2028 case RefVal::NotOwned: {
2029 unsigned cnt = X.getCount();
2030 X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
2031 : RefVal::makeReturnedNotOwned();
2032 break;
2033 }
2034
2035 default:
Ted Kremenek4fd88972008-04-17 18:12:53 +00002036 return;
2037 }
2038
2039 // Update the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002040 state = state.set<RefBindings>(Sym, X);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002041 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek4fd88972008-04-17 18:12:53 +00002042}
2043
Ted Kremenekcb612922008-04-18 19:23:43 +00002044// Assumptions.
2045
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002046const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr,
2047 const GRState* St,
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002048 SVal Cond, bool Assumption,
Ted Kremenek4323a572008-07-10 22:03:41 +00002049 bool& isFeasible) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002050
2051 // FIXME: We may add to the interface of EvalAssume the list of symbols
2052 // whose assumptions have changed. For now we just iterate through the
2053 // bindings and check if any of the tracked symbols are NULL. This isn't
2054 // too bad since the number of symbols we will track in practice are
2055 // probably small and EvalAssume is only called at branches and a few
2056 // other places.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002057 RefBindings B = St->get<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002058
2059 if (B.isEmpty())
2060 return St;
2061
2062 bool changed = false;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002063
2064 GRStateRef state(St, VMgr);
2065 RefBindings::Factory& RefBFactory = state.get_context<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00002066
2067 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002068 // Check if the symbol is null (or equal to any constant).
2069 // If this is the case, stop tracking the symbol.
Zhongxing Xu39cfed32008-08-29 14:52:36 +00002070 if (VMgr.getSymVal(St, I.getKey())) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002071 changed = true;
2072 B = RefBFactory.Remove(B, I.getKey());
2073 }
2074 }
2075
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002076 if (changed)
2077 state = state.set<RefBindings>(B);
Ted Kremenekcb612922008-04-18 19:23:43 +00002078
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002079 return state;
Ted Kremenekcb612922008-04-18 19:23:43 +00002080}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002081
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002082RefBindings CFRefCount::Update(RefBindings B, SymbolID sym,
2083 RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00002084 RefVal::Kind& hasErr,
2085 RefBindings::Factory& RefBFactory) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002086
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002087 // FIXME: This dispatch can potentially be sped up by unifiying it into
2088 // a single switch statement. Opt for simplicity for now.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002089
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002090 switch (E) {
2091 default:
2092 assert (false && "Unhandled CFRef transition.");
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002093
2094 case MayEscape:
2095 if (V.getKind() == RefVal::Owned) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002096 V = V ^ RefVal::NotOwned;
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002097 break;
2098 }
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002099 // Fall-through.
Ted Kremenek070a8252008-07-09 18:11:16 +00002100 case DoNothingByRef:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002101 case DoNothing:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002102 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
Ted Kremenek553cf182008-06-25 21:21:56 +00002103 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002104 hasErr = V.getKind();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00002105 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002106 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002107 return B;
Ted Kremeneke19f4492008-06-30 16:57:41 +00002108
Ted Kremenek80d753f2008-07-01 00:01:02 +00002109 case Autorelease:
Ted Kremenek14993892008-05-06 02:41:27 +00002110 case StopTracking:
2111 return RefBFactory.Remove(B, sym);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002112
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002113 case IncRef:
2114 switch (V.getKind()) {
2115 default:
2116 assert(false);
2117
2118 case RefVal::Owned:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002119 case RefVal::NotOwned:
Ted Kremenek553cf182008-06-25 21:21:56 +00002120 V = V + 1;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002121 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002122 case RefVal::Released:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00002123 if (isGCEnabled())
Ted Kremenek553cf182008-06-25 21:21:56 +00002124 V = V ^ RefVal::Owned;
Ted Kremenek65c91652008-04-29 05:44:10 +00002125 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002126 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek65c91652008-04-29 05:44:10 +00002127 hasErr = V.getKind();
2128 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002129 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002130 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002131 break;
2132
Ted Kremenek553cf182008-06-25 21:21:56 +00002133 case SelfOwn:
2134 V = V ^ RefVal::NotOwned;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002135 // Fall-through.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002136 case DecRef:
2137 switch (V.getKind()) {
2138 default:
2139 assert (false);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002140
Ted Kremenek553cf182008-06-25 21:21:56 +00002141 case RefVal::Owned:
2142 V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002143 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002144
Ted Kremenek553cf182008-06-25 21:21:56 +00002145 case RefVal::NotOwned:
2146 if (V.getCount() > 0)
2147 V = V - 1;
Ted Kremenek61b9f872008-04-10 23:09:18 +00002148 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00002149 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002150 hasErr = V.getKind();
Ted Kremenek9e476de2008-08-12 18:30:56 +00002151 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002152 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002153
2154 case RefVal::Released:
Ted Kremenek553cf182008-06-25 21:21:56 +00002155 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00002156 hasErr = V.getKind();
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002157 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00002158 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00002159 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002160 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00002161 return RefBFactory.Add(B, sym, V);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002162}
2163
Ted Kremenekfa34b332008-04-09 01:10:13 +00002164//===----------------------------------------------------------------------===//
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002165// Error reporting.
Ted Kremenekfa34b332008-04-09 01:10:13 +00002166//===----------------------------------------------------------------------===//
2167
Ted Kremenek8dd56462008-04-18 03:39:05 +00002168namespace {
2169
2170 //===-------------===//
2171 // Bug Descriptions. //
2172 //===-------------===//
2173
Ted Kremenek95cc1ba2008-04-18 20:54:29 +00002174 class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002175 protected:
2176 CFRefCount& TF;
2177
2178 public:
2179 CFRefBug(CFRefCount& tf) : TF(tf) {}
Ted Kremenek072192b2008-04-30 23:47:44 +00002180
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002181 CFRefCount& getTF() { return TF; }
Ted Kremenek789deac2008-05-05 23:16:31 +00002182 const CFRefCount& getTF() const { return TF; }
2183
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002184 virtual bool isLeak() const { return false; }
Ted Kremenek8c036c72008-09-20 04:23:38 +00002185
2186 const char* getCategory() const {
Ted Kremenek062bae02008-09-27 22:02:42 +00002187 return "Memory (Core Foundation/Objective-C)";
Ted Kremenek8c036c72008-09-20 04:23:38 +00002188 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002189 };
2190
2191 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2192 public:
2193 UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
2194
2195 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002196 return "use-after-release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002197 }
2198 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002199 return "Reference-counted object is used after it is released.";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002200 }
2201
2202 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002203 };
2204
2205 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2206 public:
2207 BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
2208
2209 virtual const char* getName() const {
Ted Kremenek8c036c72008-09-20 04:23:38 +00002210 return "bad release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002211 }
2212 virtual const char* getDescription() const {
2213 return "Incorrect decrement of the reference count of a "
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002214 "CoreFoundation object: "
Ted Kremenek8dd56462008-04-18 03:39:05 +00002215 "The object is not owned at this point by the caller.";
2216 }
2217
2218 virtual void EmitWarnings(BugReporter& BR);
2219 };
2220
2221 class VISIBILITY_HIDDEN Leak : public CFRefBug {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002222 bool isReturn;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002223 public:
2224 Leak(CFRefCount& tf) : CFRefBug(tf) {}
2225
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002226 void setIsReturn(bool x) { isReturn = x; }
2227
Ted Kremenek8dd56462008-04-18 03:39:05 +00002228 virtual const char* getName() const {
Ted Kremenek432af592008-05-06 18:11:36 +00002229
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002230 if (!isReturn) {
2231 if (getTF().isGCEnabled())
2232 return "leak (GC)";
2233
2234 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
2235 return "leak (hybrid MM, non-GC)";
2236
2237 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
2238 return "leak";
2239 }
2240 else {
2241 if (getTF().isGCEnabled())
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002242 return "[naming convention] leak of returned object (GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002243
2244 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002245 return "[naming convention] leak of returned object (hybrid MM, "
2246 "non-GC)";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002247
2248 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002249 return "[naming convention] leak of returned object";
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002250 }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002251 }
2252
2253 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002254 return "Object leaked";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002255 }
2256
2257 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002258 virtual void GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002259 virtual bool isLeak() const { return true; }
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002260 virtual bool isCached(BugReport& R);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002261 };
2262
2263 //===---------===//
2264 // Bug Reports. //
2265 //===---------===//
2266
2267 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
2268 SymbolID Sym;
2269 public:
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002270 CFRefReport(CFRefBug& D, ExplodedNode<GRState> *n, SymbolID sym)
Ted Kremenek8dd56462008-04-18 03:39:05 +00002271 : RangedBugReport(D, n), Sym(sym) {}
2272
2273 virtual ~CFRefReport() {}
2274
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002275 CFRefBug& getBugType() {
2276 return (CFRefBug&) RangedBugReport::getBugType();
2277 }
2278 const CFRefBug& getBugType() const {
2279 return (const CFRefBug&) RangedBugReport::getBugType();
2280 }
2281
2282 virtual void getRanges(BugReporter& BR, const SourceRange*& beg,
2283 const SourceRange*& end) {
2284
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002285 if (!getBugType().isLeak())
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002286 RangedBugReport::getRanges(BR, beg, end);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002287 else
2288 beg = end = 0;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002289 }
2290
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002291 SymbolID getSymbol() const { return Sym; }
2292
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002293 virtual PathDiagnosticPiece* getEndPath(BugReporter& BR,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002294 ExplodedNode<GRState>* N);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002295
Ted Kremenek072192b2008-04-30 23:47:44 +00002296 virtual std::pair<const char**,const char**> getExtraDescriptiveText();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002297
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002298 virtual PathDiagnosticPiece* VisitNode(ExplodedNode<GRState>* N,
2299 ExplodedNode<GRState>* PrevN,
2300 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002301 BugReporter& BR);
2302 };
2303
2304
2305} // end anonymous namespace
2306
2307void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002308 Eng.Register(new UseAfterRelease(*this));
2309 Eng.Register(new BadRelease(*this));
2310 Eng.Register(new Leak(*this));
2311}
2312
Ted Kremenek072192b2008-04-30 23:47:44 +00002313
2314static const char* Msgs[] = {
2315 "Code is compiled in garbage collection only mode" // GC only
2316 " (the bug occurs with garbage collection enabled).",
2317
2318 "Code is compiled without garbage collection.", // No GC.
2319
2320 "Code is compiled for use with and without garbage collection (GC)."
2321 " The bug occurs with GC enabled.", // Hybrid, with GC.
2322
2323 "Code is compiled for use with and without garbage collection (GC)."
2324 " The bug occurs in non-GC mode." // Hyrbird, without GC/
2325};
2326
2327std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2328 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
2329
2330 switch (TF.getLangOptions().getGCMode()) {
2331 default:
2332 assert(false);
Ted Kremenek31593ac2008-05-01 04:02:04 +00002333
2334 case LangOptions::GCOnly:
2335 assert (TF.isGCEnabled());
Ted Kremenek9e476de2008-08-12 18:30:56 +00002336 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2337
Ted Kremenek072192b2008-04-30 23:47:44 +00002338 case LangOptions::NonGC:
2339 assert (!TF.isGCEnabled());
Ted Kremenek072192b2008-04-30 23:47:44 +00002340 return std::make_pair(&Msgs[1], &Msgs[1]+1);
2341
2342 case LangOptions::HybridGC:
2343 if (TF.isGCEnabled())
2344 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2345 else
2346 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2347 }
2348}
2349
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002350PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<GRState>* N,
2351 ExplodedNode<GRState>* PrevN,
2352 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002353 BugReporter& BR) {
2354
2355 // Check if the type state has changed.
2356
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002357 const GRState* PrevSt = PrevN->getState();
2358 const GRState* CurrSt = N->getState();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002359
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002360 RefBindings PrevB = PrevSt->get<RefBindings>();
2361 RefBindings CurrB = CurrSt->get<RefBindings>();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002362
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002363 const RefVal* PrevT = PrevB.lookup(Sym);
2364 const RefVal* CurrT = CurrB.lookup(Sym);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002365
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002366 if (!CurrT)
2367 return NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002368
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002369 const char* Msg = NULL;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002370 const RefVal& CurrV = *CurrB.lookup(Sym);
Ted Kremenekce48e002008-05-05 17:53:17 +00002371
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002372 if (!PrevT) {
2373
Ted Kremenekce48e002008-05-05 17:53:17 +00002374 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2375
2376 if (CurrV.isOwned()) {
2377
2378 if (isa<CallExpr>(S))
2379 Msg = "Function call returns an object with a +1 retain count"
2380 " (owning reference).";
2381 else {
2382 assert (isa<ObjCMessageExpr>(S));
2383 Msg = "Method returns an object with a +1 retain count"
2384 " (owning reference).";
2385 }
2386 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002387 else {
2388 assert (CurrV.isNotOwned());
Ted Kremenekce48e002008-05-05 17:53:17 +00002389
2390 if (isa<CallExpr>(S))
2391 Msg = "Function call returns an object with a +0 retain count"
2392 " (non-owning reference).";
2393 else {
2394 assert (isa<ObjCMessageExpr>(S));
2395 Msg = "Method returns an object with a +0 retain count"
2396 " (non-owning reference).";
2397 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002398 }
Ted Kremenekce48e002008-05-05 17:53:17 +00002399
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002400 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2401 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2402
2403 if (Expr* Exp = dyn_cast<Expr>(S))
2404 P->addRange(Exp->getSourceRange());
2405
2406 return P;
2407 }
2408
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002409 // Determine if the typestate has changed.
2410 RefVal PrevV = *PrevB.lookup(Sym);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002411
2412 if (PrevV == CurrV)
2413 return NULL;
2414
2415 // The typestate has changed.
2416
2417 std::ostringstream os;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002418 std::string s;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002419
2420 switch (CurrV.getKind()) {
2421 case RefVal::Owned:
2422 case RefVal::NotOwned:
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002423
2424 if (PrevV.getCount() == CurrV.getCount())
2425 return 0;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002426
2427 if (PrevV.getCount() > CurrV.getCount())
2428 os << "Reference count decremented.";
2429 else
2430 os << "Reference count incremented.";
2431
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002432 if (unsigned Count = CurrV.getCount()) {
Ted Kremenekce48e002008-05-05 17:53:17 +00002433
2434 os << " Object has +" << Count;
Ted Kremenek79c140b2008-04-18 05:32:44 +00002435
Ted Kremenekce48e002008-05-05 17:53:17 +00002436 if (Count > 1)
2437 os << " retain counts.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002438 else
Ted Kremenekce48e002008-05-05 17:53:17 +00002439 os << " retain count.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002440 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002441
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002442 s = os.str();
2443 Msg = s.c_str();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002444
2445 break;
2446
2447 case RefVal::Released:
2448 Msg = "Object released.";
2449 break;
2450
2451 case RefVal::ReturnedOwned:
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002452 Msg = "Object returned to caller as an owning reference (single retain "
2453 "count transferred to caller).";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002454 break;
2455
2456 case RefVal::ReturnedNotOwned:
Ted Kremenekce48e002008-05-05 17:53:17 +00002457 Msg = "Object returned to caller with a +0 (non-owning) retain count.";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002458 break;
2459
2460 default:
2461 return NULL;
2462 }
2463
2464 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2465 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2466 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2467
2468 // Add the range by scanning the children of the statement for any bindings
2469 // to Sym.
2470
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002471 GRStateManager& VSM = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002472
2473 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
2474 if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) {
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002475 SVal X = VSM.GetSVal(CurrSt, Exp);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002476
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002477 if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&X))
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002478 if (SV->getSymbol() == Sym) {
2479 P->addRange(Exp->getSourceRange()); break;
2480 }
2481 }
2482
2483 return P;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002484}
2485
Ted Kremenek9e240492008-10-04 05:50:14 +00002486namespace {
2487class VISIBILITY_HIDDEN FindUniqueBinding :
2488 public StoreManager::BindingsHandler {
2489 SymbolID Sym;
2490 MemRegion* Binding;
2491 bool First;
2492
2493 public:
2494 FindUniqueBinding(SymbolID sym) : Sym(sym), Binding(0), First(true) {}
2495
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002496 bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal val) {
2497 if (const loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002498 if (SV->getSymbol() != Sym)
2499 return true;
2500 }
Zhongxing Xu1c96b242008-10-17 05:57:07 +00002501 else if (const nonloc::SymbolVal* SV=dyn_cast<nonloc::SymbolVal>(&val)) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002502 if (SV->getSymbol() != Sym)
2503 return true;
2504 }
2505 else
2506 return true;
2507
2508 if (Binding) {
2509 First = false;
2510 return false;
2511 }
2512 else
2513 Binding = R;
2514
2515 return true;
2516 }
2517
2518 operator bool() { return First && Binding; }
2519 MemRegion* getRegion() { return Binding; }
2520};
2521}
2522
2523static std::pair<ExplodedNode<GRState>*,MemRegion*>
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002524GetAllocationSite(GRStateManager* StateMgr, ExplodedNode<GRState>* N,
2525 SymbolID Sym) {
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002526
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002527 // Find both first node that referred to the tracked symbol and the
2528 // memory location that value was store to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002529 ExplodedNode<GRState>* Last = N;
Ted Kremenek9e240492008-10-04 05:50:14 +00002530 MemRegion* FirstBinding = 0;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002531
2532 while (N) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002533 const GRState* St = N->getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002534 RefBindings B = St->get<RefBindings>();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002535
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002536 if (!B.lookup(Sym))
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002537 break;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002538
2539 if (StateMgr) {
Ted Kremenek9e240492008-10-04 05:50:14 +00002540 FindUniqueBinding FB(Sym);
2541 StateMgr->iterBindings(St, FB);
2542 if (FB) FirstBinding = FB.getRegion();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002543 }
2544
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002545 Last = N;
2546 N = N->pred_empty() ? NULL : *(N->pred_begin());
2547 }
2548
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002549 return std::make_pair(Last, FirstBinding);
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002550}
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002551
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002552PathDiagnosticPiece* CFRefReport::getEndPath(BugReporter& br,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002553 ExplodedNode<GRState>* EndN) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002554
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002555 GRBugReporter& BR = cast<GRBugReporter>(br);
2556
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002557 // Tell the BugReporter to report cases when the tracked symbol is
2558 // assigned to different variables, etc.
Ted Kremenekc0959972008-07-02 21:24:01 +00002559 cast<GRBugReporter>(BR).addNotableSymbol(Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002560
2561 if (!getBugType().isLeak())
Ted Kremeneke28565b2008-05-05 18:50:19 +00002562 return RangedBugReport::getEndPath(BR, EndN);
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002563
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002564 // We are a leak. Walk up the graph to get to the first node where the
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002565 // symbol appeared, and also get the first VarDecl that tracked object
2566 // is stored to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002567 ExplodedNode<GRState>* AllocNode = 0;
Ted Kremenek9e240492008-10-04 05:50:14 +00002568 MemRegion* FirstBinding = 0;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002569
2570 llvm::tie(AllocNode, FirstBinding) =
2571 GetAllocationSite(&BR.getStateManager(), EndN, Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002572
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002573 // Get the allocate site.
2574 assert (AllocNode);
2575 Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002576
Ted Kremeneke28565b2008-05-05 18:50:19 +00002577 SourceManager& SMgr = BR.getContext().getSourceManager();
2578 unsigned AllocLine = SMgr.getLogicalLineNumber(FirstStmt->getLocStart());
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002579
Ted Kremeneke28565b2008-05-05 18:50:19 +00002580 // Get the leak site. We may have multiple ExplodedNodes (one with the
2581 // leak) that occur on the same line number; if the node with the leak
2582 // has any immediate predecessor nodes with the same line number, find
2583 // any transitive-successors that have a different statement and use that
2584 // line number instead. This avoids emiting a diagnostic like:
2585 //
2586 // // 'y' is leaked.
2587 // int x = foo(y);
2588 //
2589 // instead we want:
2590 //
2591 // int x = foo(y);
2592 // // 'y' is leaked.
2593
2594 Stmt* S = getStmt(BR); // This is the statement where the leak occured.
2595 assert (S);
2596 unsigned EndLine = SMgr.getLogicalLineNumber(S->getLocStart());
2597
2598 // Look in the *trimmed* graph at the immediate predecessor of EndN. Does
2599 // it occur on the same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002600 PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above;
Ted Kremeneke28565b2008-05-05 18:50:19 +00002601
2602 assert (!EndN->pred_empty()); // Not possible to have 0 predecessors.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002603 ExplodedNode<GRState> *Pred = *(EndN->pred_begin());
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002604 ProgramPoint PredPos = Pred->getLocation();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002605
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002606 if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) {
Ted Kremeneke28565b2008-05-05 18:50:19 +00002607
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002608 Stmt* SPred = PredPS->getStmt();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002609
2610 // Predecessor at same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002611 if (SMgr.getLogicalLineNumber(SPred->getLocStart()) != EndLine) {
2612 Hint = PathDiagnosticPiece::Below;
2613 S = SPred;
2614 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002615 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002616
2617 // Generate the diagnostic.
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002618 FullSourceLoc L( S->getLocStart(), SMgr);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002619 std::ostringstream os;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002620
Ted Kremeneke28565b2008-05-05 18:50:19 +00002621 os << "Object allocated on line " << AllocLine;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002622
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002623 if (FirstBinding)
Ted Kremenek9e240492008-10-04 05:50:14 +00002624 os << " and stored into '" << FirstBinding->getString() << '\'';
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002625
Ted Kremenek9e240492008-10-04 05:50:14 +00002626
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002627 // Get the retain count.
2628 const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
2629
2630 if (RV->getKind() == RefVal::ErrorLeakReturned) {
2631 ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BR.getGraph().getCodeDecl());
2632 os << " is returned from a method whose name ('"
2633 << MD.getSelector().getName()
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002634 << "') does not contain 'create' or 'copy' or otherwise starts with"
2635 " 'new' or 'alloc'. This violates the naming convention rules given"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002636 " in the Memory Management Guide for Cocoa (object leaked).";
2637 }
2638 else
Ted Kremenek9d1d5702008-10-24 21:22:44 +00002639 os << " is no longer referenced after this point and has a retain count of"
2640 " +"
Ted Kremenek3ad2cc82008-10-22 23:56:21 +00002641 << RV->getCount() << " (object leaked).";
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002642
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002643 return new PathDiagnosticPiece(L, os.str(), Hint);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002644}
2645
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002646void UseAfterRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002647
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002648 for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
2649 E = TF.use_after_end(); I != E; ++I) {
2650
Ted Kremenek8dd56462008-04-18 03:39:05 +00002651 CFRefReport report(*this, I->first, I->second.second);
2652 report.addRange(I->second.first->getSourceRange());
Ted Kremenek75840e12008-04-18 01:56:37 +00002653 BR.EmitWarning(report);
Ted Kremenekfa34b332008-04-09 01:10:13 +00002654 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002655}
2656
2657void BadRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002658
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002659 for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
2660 E = TF.bad_release_end(); I != E; ++I) {
2661
Ted Kremenek8dd56462008-04-18 03:39:05 +00002662 CFRefReport report(*this, I->first, I->second.second);
2663 report.addRange(I->second.first->getSourceRange());
2664 BR.EmitWarning(report);
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002665 }
2666}
Ted Kremenekfa34b332008-04-09 01:10:13 +00002667
Ted Kremenek989d5192008-04-17 23:43:50 +00002668void Leak::EmitWarnings(BugReporter& BR) {
2669
2670 for (CFRefCount::leaks_iterator I = TF.leaks_begin(),
2671 E = TF.leaks_end(); I != E; ++I) {
2672
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002673 std::vector<std::pair<SymbolID, bool> >& SymV = *(I->second);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002674 unsigned n = SymV.size();
2675
2676 for (unsigned i = 0; i < n; ++i) {
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002677 setIsReturn(SymV[i].second);
2678 CFRefReport report(*this, I->first, SymV[i].first);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002679 BR.EmitWarning(report);
2680 }
Ted Kremenek989d5192008-04-17 23:43:50 +00002681 }
2682}
2683
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002684void Leak::GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002685 for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end();
2686 I!=E; ++I)
2687 Nodes.push_back(I->first);
2688}
2689
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002690bool Leak::isCached(BugReport& R) {
2691
2692 // Most bug reports are cached at the location where they occured.
2693 // With leaks, we want to unique them by the location where they were
Ted Kremenekf9790ae2008-10-24 20:32:50 +00002694 // allocated, and only report a single path.
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002695
2696 SymbolID Sym = static_cast<CFRefReport&>(R).getSymbol();
2697
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002698 ExplodedNode<GRState>* AllocNode =
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002699 GetAllocationSite(0, R.getEndNode(), Sym).first;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002700
2701 if (!AllocNode)
2702 return false;
2703
2704 return BugTypeCacheLocation::isCached(AllocNode->getLocation());
2705}
2706
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002707//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +00002708// Transfer function creation for external clients.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002709//===----------------------------------------------------------------------===//
2710
Ted Kremenek072192b2008-04-30 23:47:44 +00002711GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
2712 const LangOptions& lopts) {
Ted Kremenek78d46242008-07-22 16:21:24 +00002713 return new CFRefCount(Ctx, GCEnabled, lopts);
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +00002714}