blob: 7a3df3153ea87e199caff156bbc31b135e7f6047 [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 Kremenek2fff37e2008-03-06 00:08:09 +000018#include "clang/Analysis/PathSensitive/ValueState.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000019#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek2fff37e2008-03-06 00:08:09 +000020#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000021#include "clang/Analysis/PathDiagnostic.h"
22#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000023#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/FoldingSet.h"
25#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek900a2d72008-05-07 18:36:45 +000026#include "llvm/ADT/StringExtras.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000027#include "llvm/Support/Compiler.h"
Ted Kremenek6ed9afc2008-05-16 18:33:44 +000028#include "llvm/ADT/STLExtras.h"
Ted Kremenekf3948042008-03-11 19:44:10 +000029#include <ostream>
Ted Kremenek2cf943a2008-04-18 04:55:01 +000030#include <sstream>
Ted Kremenek2fff37e2008-03-06 00:08:09 +000031
32using namespace clang;
Ted Kremenek900a2d72008-05-07 18:36:45 +000033using llvm::CStrInCStrNoCase;
Ted Kremenek2fff37e2008-03-06 00:08:09 +000034
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000035//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +000036// Selector creation functions.
Ted Kremenek4fd88972008-04-17 18:12:53 +000037//===----------------------------------------------------------------------===//
38
Ted Kremenekb83e02e2008-05-01 18:31:44 +000039static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
Ted Kremenek4fd88972008-04-17 18:12:53 +000040 IdentifierInfo* II = &Ctx.Idents.get(name);
41 return Ctx.Selectors.getSelector(0, &II);
42}
43
Ted Kremenek9c32d082008-05-06 00:30:21 +000044static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
45 IdentifierInfo* II = &Ctx.Idents.get(name);
46 return Ctx.Selectors.getSelector(1, &II);
47}
48
Ted Kremenek553cf182008-06-25 21:21:56 +000049//===----------------------------------------------------------------------===//
50// Type querying functions.
51//===----------------------------------------------------------------------===//
52
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000053static bool isCFRefType(QualType T) {
54
55 if (!T->isPointerType())
56 return false;
57
Ted Kremenek553cf182008-06-25 21:21:56 +000058 // Check the typedef for the name "CF" and the substring "Ref".
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000059 TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
60
61 if (!TD)
62 return false;
63
64 const char* TDName = TD->getDecl()->getIdentifier()->getName();
65 assert (TDName);
66
67 if (TDName[0] != 'C' || TDName[1] != 'F')
68 return false;
69
70 if (strstr(TDName, "Ref") == 0)
71 return false;
72
73 return true;
74}
75
76static bool isNSType(QualType T) {
77
78 if (!T->isPointerType())
79 return false;
80
81 ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
82
83 if (!OT)
84 return false;
85
86 const char* ClsName = OT->getDecl()->getIdentifier()->getName();
87 assert (ClsName);
88
89 if (ClsName[0] != 'N' || ClsName[1] != 'S')
90 return false;
91
92 return true;
93}
94
Ted Kremenek4fd88972008-04-17 18:12:53 +000095//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +000096// Primitives used for constructing summaries for function/method calls.
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000097//===----------------------------------------------------------------------===//
98
Ted Kremenek553cf182008-06-25 21:21:56 +000099namespace {
100/// ArgEffect is used to summarize a function/method call's effect on a
101/// particular argument.
102enum ArgEffect { IncRef, DecRef, DoNothing, StopTracking, MayEscape,
Ted Kremeneke19f4492008-06-30 16:57:41 +0000103 SelfOwn, Autorelease };
Ted Kremenek553cf182008-06-25 21:21:56 +0000104
105/// ArgEffects summarizes the effects of a function/method call on all of
106/// its arguments.
107typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000108}
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000109
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000110namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000111template <> struct FoldingSetTrait<ArgEffects> {
112 static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
113 for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) {
114 ID.AddInteger(I->first);
115 ID.AddInteger((unsigned) I->second);
116 }
117 }
118};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000119} // end llvm namespace
120
121namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000122
123/// RetEffect is used to summarize a function/method call's behavior with
124/// respect to its return value.
125class VISIBILITY_HIDDEN RetEffect {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000126public:
Ted Kremeneka7344702008-06-23 18:02:52 +0000127 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
128 NotOwnedSymbol, ReceiverAlias };
Ted Kremenek553cf182008-06-25 21:21:56 +0000129
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000130private:
131 unsigned Data;
Ted Kremenek553cf182008-06-25 21:21:56 +0000132 RetEffect(Kind k, unsigned D = 0) { Data = (D << 3) | (unsigned) k; }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000133
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000134public:
Ted Kremenek553cf182008-06-25 21:21:56 +0000135
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000136 Kind getKind() const { return (Kind) (Data & 0x7); }
Ted Kremenek553cf182008-06-25 21:21:56 +0000137
138 unsigned getIndex() const {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000139 assert(getKind() == Alias);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000140 return Data >> 3;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000141 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000142
Ted Kremenek553cf182008-06-25 21:21:56 +0000143 static RetEffect MakeAlias(unsigned Idx) {
144 return RetEffect(Alias, Idx);
145 }
146 static RetEffect MakeReceiverAlias() {
147 return RetEffect(ReceiverAlias);
148 }
Ted Kremeneka7344702008-06-23 18:02:52 +0000149 static RetEffect MakeOwned(bool isAllocated = false) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000150 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol);
151 }
152 static RetEffect MakeNotOwned() {
153 return RetEffect(NotOwnedSymbol);
154 }
155 static RetEffect MakeNoRet() {
156 return RetEffect(NoRet);
Ted Kremeneka7344702008-06-23 18:02:52 +0000157 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000158
Ted Kremenek553cf182008-06-25 21:21:56 +0000159 operator Kind() const {
160 return getKind();
161 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000162
Ted Kremenek553cf182008-06-25 21:21:56 +0000163 void Profile(llvm::FoldingSetNodeID& ID) const {
164 ID.AddInteger(Data);
165 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000166};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000167
Ted Kremenek553cf182008-06-25 21:21:56 +0000168
169class VISIBILITY_HIDDEN RetainSummary : public llvm::FoldingSetNode {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000170 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
171 /// specifies the argument (starting from 0). This can be sparsely
172 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000173 ArgEffects* Args;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000174
175 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
176 /// do not have an entry in Args.
177 ArgEffect DefaultArgEffect;
178
Ted Kremenek553cf182008-06-25 21:21:56 +0000179 /// Receiver - If this summary applies to an Objective-C message expression,
180 /// this is the effect applied to the state of the receiver.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000181 ArgEffect Receiver;
Ted Kremenek553cf182008-06-25 21:21:56 +0000182
183 /// Ret - The effect on the return value. Used to indicate if the
184 /// function/method call returns a new tracked symbol, returns an
185 /// alias of one of the arguments in the call, and so on.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000186 RetEffect Ret;
Ted Kremenek553cf182008-06-25 21:21:56 +0000187
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000188public:
189
Ted Kremenek1bffd742008-05-06 15:44:25 +0000190 RetainSummary(ArgEffects* A, RetEffect R, ArgEffect defaultEff,
191 ArgEffect ReceiverEff)
192 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R) {}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000193
Ted Kremenek553cf182008-06-25 21:21:56 +0000194 /// getArg - Return the argument effect on the argument specified by
195 /// idx (starting from 0).
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000196 ArgEffect getArg(unsigned idx) const {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000197
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000198 if (!Args)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000199 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000200
201 // If Args is present, it is likely to contain only 1 element.
202 // Just do a linear search. Do it from the back because functions with
203 // large numbers of arguments will be tail heavy with respect to which
Ted Kremenek553cf182008-06-25 21:21:56 +0000204 // argument they actually modify with respect to the reference count.
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000205 for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend();
206 I!=E; ++I) {
207
208 if (idx > I->first)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000209 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000210
211 if (idx == I->first)
212 return I->second;
213 }
214
Ted Kremenek1bffd742008-05-06 15:44:25 +0000215 return DefaultArgEffect;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000216 }
217
Ted Kremenek553cf182008-06-25 21:21:56 +0000218 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000219 RetEffect getRetEffect() const {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000220 return Ret;
221 }
222
Ted Kremenek553cf182008-06-25 21:21:56 +0000223 /// getReceiverEffect - Returns the effect on the receiver of the call.
224 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000225 ArgEffect getReceiverEffect() const {
226 return Receiver;
227 }
228
Ted Kremenek55499762008-06-17 02:43:46 +0000229 typedef ArgEffects::const_iterator ExprIterator;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000230
Ted Kremenek55499762008-06-17 02:43:46 +0000231 ExprIterator begin_args() const { return Args->begin(); }
232 ExprIterator end_args() const { return Args->end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000233
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000234 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000235 RetEffect RetEff, ArgEffect DefaultEff,
236 ArgEffect ReceiverEff) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000237 ID.AddPointer(A);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000238 ID.Add(RetEff);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000239 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000240 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000241 }
242
243 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000244 Profile(ID, Args, Ret, DefaultArgEffect, Receiver);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000245 }
246};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000247} // end anonymous namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000248
Ted Kremenek553cf182008-06-25 21:21:56 +0000249//===----------------------------------------------------------------------===//
250// Data structures for constructing summaries.
251//===----------------------------------------------------------------------===//
Ted Kremenek53301ba2008-06-24 03:49:48 +0000252
Ted Kremenek553cf182008-06-25 21:21:56 +0000253namespace {
254class VISIBILITY_HIDDEN ObjCSummaryKey {
255 IdentifierInfo* II;
256 Selector S;
257public:
258 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
259 : II(ii), S(s) {}
260
261 ObjCSummaryKey(ObjCInterfaceDecl* d, Selector s)
262 : II(d ? d->getIdentifier() : 0), S(s) {}
263
264 ObjCSummaryKey(Selector s)
265 : II(0), S(s) {}
266
267 IdentifierInfo* getIdentifier() const { return II; }
268 Selector getSelector() const { return S; }
269};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000270}
271
272namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000273template <> struct DenseMapInfo<ObjCSummaryKey> {
274 static inline ObjCSummaryKey getEmptyKey() {
275 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
276 DenseMapInfo<Selector>::getEmptyKey());
277 }
Ted Kremenek4f22a782008-06-23 23:30:29 +0000278
Ted Kremenek553cf182008-06-25 21:21:56 +0000279 static inline ObjCSummaryKey getTombstoneKey() {
280 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
281 DenseMapInfo<Selector>::getTombstoneKey());
282 }
283
284 static unsigned getHashValue(const ObjCSummaryKey &V) {
285 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
286 & 0x88888888)
287 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
288 & 0x55555555);
289 }
290
291 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
292 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
293 RHS.getIdentifier()) &&
294 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
295 RHS.getSelector());
296 }
297
298 static bool isPod() {
299 return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
300 DenseMapInfo<Selector>::isPod();
301 }
302};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000303} // end llvm namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000304
Ted Kremenek4f22a782008-06-23 23:30:29 +0000305namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000306class VISIBILITY_HIDDEN ObjCSummaryCache {
307 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
308 MapTy M;
309public:
310 ObjCSummaryCache() {}
311
312 typedef MapTy::iterator iterator;
313
314 iterator find(ObjCInterfaceDecl* D, Selector S) {
315
316 // Do a lookup with the (D,S) pair. If we find a match return
317 // the iterator.
318 ObjCSummaryKey K(D, S);
319 MapTy::iterator I = M.find(K);
320
321 if (I != M.end() || !D)
322 return I;
323
324 // Walk the super chain. If we find a hit with a parent, we'll end
325 // up returning that summary. We actually allow that key (null,S), as
326 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
327 // generate initial summaries without having to worry about NSObject
328 // being declared.
329 // FIXME: We may change this at some point.
330 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
331 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
332 break;
333
334 if (!C)
335 return I;
336 }
337
338 // Cache the summary with original key to make the next lookup faster
339 // and return the iterator.
340 M[K] = I->second;
341 return I;
342 }
343
344
345 iterator find(Expr* Receiver, Selector S) {
346 return find(getReceiverDecl(Receiver), S);
347 }
348
349 iterator find(IdentifierInfo* II, Selector S) {
350 // FIXME: Class method lookup. Right now we dont' have a good way
351 // of going between IdentifierInfo* and the class hierarchy.
352 iterator I = M.find(ObjCSummaryKey(II, S));
353 return I == M.end() ? M.find(ObjCSummaryKey(S)) : I;
354 }
355
356 ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
357
358 const PointerType* PT = E->getType()->getAsPointerType();
359 if (!PT) return 0;
360
361 ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType());
362 if (!OI) return 0;
363
364 return OI ? OI->getDecl() : 0;
365 }
366
367 iterator end() { return M.end(); }
368
369 RetainSummary*& operator[](ObjCMessageExpr* ME) {
370
371 Selector S = ME->getSelector();
372
373 if (Expr* Receiver = ME->getReceiver()) {
374 ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
375 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
376 }
377
378 return M[ObjCSummaryKey(ME->getClassName(), S)];
379 }
380
381 RetainSummary*& operator[](ObjCSummaryKey K) {
382 return M[K];
383 }
384
385 RetainSummary*& operator[](Selector S) {
386 return M[ ObjCSummaryKey(S) ];
387 }
388};
389} // end anonymous namespace
390
391//===----------------------------------------------------------------------===//
392// Data structures for managing collections of summaries.
393//===----------------------------------------------------------------------===//
394
395namespace {
396class VISIBILITY_HIDDEN RetainSummaryManager {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000397
398 //==-----------------------------------------------------------------==//
399 // Typedefs.
400 //==-----------------------------------------------------------------==//
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000401
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000402 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> >
403 ArgEffectsSetTy;
404
405 typedef llvm::FoldingSet<RetainSummary>
406 SummarySetTy;
407
408 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
409 FuncSummariesTy;
410
Ted Kremenek4f22a782008-06-23 23:30:29 +0000411 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000412
413 //==-----------------------------------------------------------------==//
414 // Data.
415 //==-----------------------------------------------------------------==//
416
Ted Kremenek553cf182008-06-25 21:21:56 +0000417 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek377e2302008-04-29 05:33:51 +0000418 ASTContext& Ctx;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000419
Ted Kremenek553cf182008-06-25 21:21:56 +0000420 /// NSWindowII - An IdentifierInfo* representing the identifier "NSWindow."
421 IdentifierInfo* NSWindowII;
Ted Kremenek179064e2008-07-01 17:21:27 +0000422
423 /// NSPanelII - An IdentifierInfo* representing the identifier "NSPanel."
424 IdentifierInfo* NSPanelII;
Ted Kremenek553cf182008-06-25 21:21:56 +0000425
426 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek377e2302008-04-29 05:33:51 +0000427 const bool GCEnabled;
428
Ted Kremenek553cf182008-06-25 21:21:56 +0000429 /// SummarySet - A FoldingSet of uniqued summaries.
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000430 SummarySetTy SummarySet;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000431
Ted Kremenek553cf182008-06-25 21:21:56 +0000432 /// FuncSummaries - A map from FunctionDecls to summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000433 FuncSummariesTy FuncSummaries;
434
Ted Kremenek553cf182008-06-25 21:21:56 +0000435 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
436 /// to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000437 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000438
Ted Kremenek553cf182008-06-25 21:21:56 +0000439 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000440 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000441
Ted Kremenek553cf182008-06-25 21:21:56 +0000442 /// ArgEffectsSet - A FoldingSet of uniqued ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000443 ArgEffectsSetTy ArgEffectsSet;
444
Ted Kremenek553cf182008-06-25 21:21:56 +0000445 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
446 /// and all other data used by the checker.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000447 llvm::BumpPtrAllocator BPAlloc;
448
Ted Kremenek553cf182008-06-25 21:21:56 +0000449 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000450 ArgEffects ScratchArgs;
451
Ted Kremenek432af592008-05-06 18:11:36 +0000452 RetainSummary* StopSummary;
453
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000454 //==-----------------------------------------------------------------==//
455 // Methods.
456 //==-----------------------------------------------------------------==//
457
Ted Kremenek553cf182008-06-25 21:21:56 +0000458 /// getArgEffects - Returns a persistent ArgEffects object based on the
459 /// data in ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000460 ArgEffects* getArgEffects();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000461
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000462 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000463 RetainSummary* getUnarySummary(FunctionDecl* FD, UnaryFuncKind func);
Ted Kremenek377e2302008-04-29 05:33:51 +0000464
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000465 RetainSummary* getNSSummary(FunctionDecl* FD, const char* FName);
466 RetainSummary* getCFSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000467
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000468 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
469 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000470
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000471 RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000472 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000473 ArgEffect DefaultEff = MayEscape);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000474
Ted Kremenek9c32d082008-05-06 00:30:21 +0000475
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000476 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000477 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000478 ArgEffect DefaultEff = MayEscape) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000479 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek9c32d082008-05-06 00:30:21 +0000480 }
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000481
Ted Kremenek432af592008-05-06 18:11:36 +0000482
Ted Kremenek1bffd742008-05-06 15:44:25 +0000483 RetainSummary* getPersistentStopSummary() {
Ted Kremenek432af592008-05-06 18:11:36 +0000484 if (StopSummary)
485 return StopSummary;
486
487 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
488 StopTracking, StopTracking);
489
490 return StopSummary;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000491 }
Ted Kremenekb3095252008-05-06 04:20:12 +0000492
Ted Kremenek553cf182008-06-25 21:21:56 +0000493 RetainSummary* getInitMethodSummary(ObjCMessageExpr* ME);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000494
Ted Kremenek1f180c32008-06-23 22:21:20 +0000495 void InitializeClassMethodSummaries();
496 void InitializeMethodSummaries();
Ted Kremenek9c32d082008-05-06 00:30:21 +0000497
Ted Kremenek553cf182008-06-25 21:21:56 +0000498 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
499 ObjCClassMethodSummaries[S] = Summ;
500 }
501
502 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
503 ObjCMethodSummaries[S] = Summ;
504 }
505
506 void addNSWindowMethSummary(Selector S, RetainSummary *Summ) {
507 ObjCMethodSummaries[ObjCSummaryKey(NSWindowII, S)] = Summ;
508 }
509
Ted Kremenek179064e2008-07-01 17:21:27 +0000510 void addNSPanelMethSummary(Selector S, RetainSummary *Summ) {
511 ObjCMethodSummaries[ObjCSummaryKey(NSPanelII, S)] = Summ;
512 }
513
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000514public:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000515
516 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenek179064e2008-07-01 17:21:27 +0000517 : Ctx(ctx),
518 NSWindowII(&ctx.Idents.get("NSWindow")),
519 NSPanelII(&ctx.Idents.get("NSPanel")),
Ted Kremenek553cf182008-06-25 21:21:56 +0000520 GCEnabled(gcenabled), StopSummary(0) {
521
522 InitializeClassMethodSummaries();
523 InitializeMethodSummaries();
524 }
Ted Kremenek377e2302008-04-29 05:33:51 +0000525
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000526 ~RetainSummaryManager();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000527
Ted Kremenekab592272008-06-24 03:56:45 +0000528 RetainSummary* getSummary(FunctionDecl* FD);
Ted Kremenek553cf182008-06-25 21:21:56 +0000529 RetainSummary* getMethodSummary(ObjCMessageExpr* ME, ObjCInterfaceDecl* ID);
Ted Kremenek1f180c32008-06-23 22:21:20 +0000530 RetainSummary* getClassMethodSummary(IdentifierInfo* ClsName, Selector S);
Ted Kremenekb3095252008-05-06 04:20:12 +0000531
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000532 bool isGCEnabled() const { return GCEnabled; }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000533};
534
535} // end anonymous namespace
536
537//===----------------------------------------------------------------------===//
538// Implementation of checker data structures.
539//===----------------------------------------------------------------------===//
540
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000541RetainSummaryManager::~RetainSummaryManager() {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000542
543 // FIXME: The ArgEffects could eventually be allocated from BPAlloc,
544 // mitigating the need to do explicit cleanup of the
545 // Argument-Effect summaries.
546
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000547 for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(),
548 E = ArgEffectsSet.end(); I!=E; ++I)
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000549 I->getValue().~ArgEffects();
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000550}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000551
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000552ArgEffects* RetainSummaryManager::getArgEffects() {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000553
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000554 if (ScratchArgs.empty())
555 return NULL;
556
557 // Compute a profile for a non-empty ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000558 llvm::FoldingSetNodeID profile;
559 profile.Add(ScratchArgs);
560 void* InsertPos;
561
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000562 // Look up the uniqued copy, or create a new one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000563 llvm::FoldingSetNodeWrapper<ArgEffects>* E =
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000564 ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000565
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000566 if (E) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000567 ScratchArgs.clear();
568 return &E->getValue();
569 }
570
571 E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
Ted Kremenek553cf182008-06-25 21:21:56 +0000572 BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000573
574 new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000575 ArgEffectsSet.InsertNode(E, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000576
577 ScratchArgs.clear();
578 return &E->getValue();
579}
580
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000581RetainSummary*
582RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000583 ArgEffect ReceiverEff,
584 ArgEffect DefaultEff) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000585
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000586 // Generate a profile for the summary.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000587 llvm::FoldingSetNodeID profile;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000588 RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000589
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000590 // Look up the uniqued summary, or create one if it doesn't exist.
591 void* InsertPos;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000592 RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000593
594 if (Summ)
595 return Summ;
596
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000597 // Create the summary and return it.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000598 Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek1bffd742008-05-06 15:44:25 +0000599 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000600 SummarySet.InsertNode(Summ, InsertPos);
601
602 return Summ;
603}
604
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000605//===----------------------------------------------------------------------===//
606// Summary creation for functions (largely uses of Core Foundation).
607//===----------------------------------------------------------------------===//
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000608
Ted Kremenekab592272008-06-24 03:56:45 +0000609RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000610
611 SourceLocation Loc = FD->getLocation();
612
613 if (!Loc.isFileID())
614 return NULL;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000615
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000616 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000617 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000618
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000619 if (I != FuncSummaries.end())
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000620 return I->second;
621
622 // No summary. Generate one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000623 const char* FName = FD->getIdentifier()->getName();
624
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000625 RetainSummary *S = 0;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000626
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000627 FunctionType* FT = dyn_cast<FunctionType>(FD->getType());
628
629 if (FT && isCFRefType(FT->getResultType()))
630 S = getCFSummary(FD, FName);
631 else if (FName[0] == 'C' && FName[1] == 'F')
632 S = getCFSummary(FD, FName);
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000633 else if (FName[0] == 'N' && FName[1] == 'S')
634 S = getNSSummary(FD, FName);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000635
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000636 FuncSummaries[FD] = S;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000637 return S;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000638}
639
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000640RetainSummary* RetainSummaryManager::getNSSummary(FunctionDecl* FD,
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000641 const char* FName) {
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000642 FName += 2;
643
644 if (strcmp(FName, "MakeCollectable") == 0)
645 return getUnarySummary(FD, cfmakecollectable);
646
647 return 0;
648}
649
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000650RetainSummary* RetainSummaryManager::getCFSummary(FunctionDecl* FD,
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000651 const char* FName) {
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000652
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000653 if (FName[0] == 'C' && FName[1] == 'F')
654 FName += 2;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000655
656 if (strcmp(FName, "Retain") == 0)
657 return getUnarySummary(FD, cfretain);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000658
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000659 if (strcmp(FName, "Release") == 0)
660 return getUnarySummary(FD, cfrelease);
661
662 if (strcmp(FName, "MakeCollectable") == 0)
663 return getUnarySummary(FD, cfmakecollectable);
664
665 if (strstr(FName, "Create") || strstr(FName, "Copy"))
666 return getCFSummaryCreateRule(FD);
667
668 if (strstr(FName, "Get"))
669 return getCFSummaryGetRule(FD);
670
671 return 0;
672}
673
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000674RetainSummary*
675RetainSummaryManager::getUnarySummary(FunctionDecl* FD, UnaryFuncKind func) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000676
677 FunctionTypeProto* FT =
678 dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
679
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000680 if (FT) {
681
682 if (FT->getNumArgs() != 1)
683 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000684
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000685 TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr());
686
687 if (!ArgT)
688 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000689
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000690 if (!ArgT->isPointerType())
691 return NULL;
692 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000693
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000694 assert (ScratchArgs.empty());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000695
Ted Kremenek377e2302008-04-29 05:33:51 +0000696 switch (func) {
697 case cfretain: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000698 ScratchArgs.push_back(std::make_pair(0, IncRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000699 return getPersistentSummary(RetEffect::MakeAlias(0),
700 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000701 }
702
703 case cfrelease: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000704 ScratchArgs.push_back(std::make_pair(0, DecRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000705 return getPersistentSummary(RetEffect::MakeNoRet(),
706 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000707 }
708
709 case cfmakecollectable: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000710 if (GCEnabled)
711 ScratchArgs.push_back(std::make_pair(0, DecRef));
712
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000713 return getPersistentSummary(RetEffect::MakeAlias(0),
714 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000715 }
716
717 default:
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000718 assert (false && "Not a supported unary function.");
Ted Kremenek940b1d82008-04-10 23:44:06 +0000719 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000720}
721
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000722RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000723
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000724 FunctionType* FT =
725 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000726
727 if (FT && !isCFRefType(FT->getResultType()))
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000728 return getPersistentSummary(RetEffect::MakeNoRet());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000729
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000730 // FIXME: Add special-cases for functions that retain/release. For now
731 // just handle the default case.
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000732
733 assert (ScratchArgs.empty());
Ted Kremeneka7344702008-06-23 18:02:52 +0000734 return getPersistentSummary(RetEffect::MakeOwned(true));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000735}
736
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000737RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000738
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000739 FunctionType* FT =
740 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000741
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000742 if (FT) {
743 QualType RetTy = FT->getResultType();
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000744
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000745 // FIXME: For now we assume that all pointer types returned are referenced
746 // counted. Since this is the "Get" rule, we assume non-ownership, which
747 // works fine for things that are not reference counted. We do this because
748 // some generic data structures return "void*". We need something better
749 // in the future.
750
751 if (!isCFRefType(RetTy) && !RetTy->isPointerType())
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000752 return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000753 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000754
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000755 // FIXME: Add special-cases for functions that retain/release. For now
756 // just handle the default case.
757
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000758 assert (ScratchArgs.empty());
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000759 return getPersistentSummary(RetEffect::MakeNotOwned(), DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000760}
761
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000762//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000763// Summary creation for Selectors.
764//===----------------------------------------------------------------------===//
765
Ted Kremenek1bffd742008-05-06 15:44:25 +0000766RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000767RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) {
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000768 assert(ScratchArgs.empty());
769
770 RetainSummary* Summ =
Ted Kremenek9c32d082008-05-06 00:30:21 +0000771 getPersistentSummary(RetEffect::MakeReceiverAlias());
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000772
Ted Kremenek553cf182008-06-25 21:21:56 +0000773 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000774 return Summ;
775}
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000776
Ted Kremenek553cf182008-06-25 21:21:56 +0000777
Ted Kremenek1bffd742008-05-06 15:44:25 +0000778RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000779RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
780 ObjCInterfaceDecl* ID) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000781
782 Selector S = ME->getSelector();
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000783
Ted Kremenek553cf182008-06-25 21:21:56 +0000784 // Look up a summary in our summary cache.
785 ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000786
Ted Kremenek1f180c32008-06-23 22:21:20 +0000787 if (I != ObjCMethodSummaries.end())
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000788 return I->second;
Ted Kremenek553cf182008-06-25 21:21:56 +0000789
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000790 if (!ME->getType()->isPointerType())
791 return 0;
792
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000793 // "initXXX": pass-through for receiver.
794
795 const char* s = S.getIdentifierInfoForSlot(0)->getName();
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000796 assert (ScratchArgs.empty());
Ted Kremenekaee9e572008-05-06 06:09:09 +0000797
Ted Kremenek0327f772008-06-02 17:14:13 +0000798 if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0)
Ted Kremenek553cf182008-06-25 21:21:56 +0000799 return getInitMethodSummary(ME);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000800
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000801 // "copyXXX", "createXXX", "newXXX": allocators.
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000802
Ted Kremenek84060db2008-05-07 04:25:59 +0000803 if (!isNSType(ME->getReceiver()->getType()))
804 return 0;
805
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000806 if (CStrInCStrNoCase(s, "create") || CStrInCStrNoCase(s, "copy") ||
807 CStrInCStrNoCase(s, "new")) {
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000808
809 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremeneka7344702008-06-23 18:02:52 +0000810 : RetEffect::MakeOwned(true);
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000811
812 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenek553cf182008-06-25 21:21:56 +0000813 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000814 return Summ;
815 }
Ted Kremenek1bffd742008-05-06 15:44:25 +0000816
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000817 return 0;
818}
819
Ted Kremenekc8395602008-05-06 21:26:51 +0000820RetainSummary*
Ted Kremenek1f180c32008-06-23 22:21:20 +0000821RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
822 Selector S) {
Ted Kremenekc8395602008-05-06 21:26:51 +0000823
Ted Kremenek553cf182008-06-25 21:21:56 +0000824 // FIXME: Eventually we should properly do class method summaries, but
825 // it requires us being able to walk the type hierarchy. Unfortunately,
826 // we cannot do this with just an IdentifierInfo* for the class name.
827
Ted Kremenekc8395602008-05-06 21:26:51 +0000828 // Look up a summary in our cache of Selectors -> Summaries.
Ted Kremenek553cf182008-06-25 21:21:56 +0000829 ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S);
Ted Kremenekc8395602008-05-06 21:26:51 +0000830
Ted Kremenek1f180c32008-06-23 22:21:20 +0000831 if (I != ObjCClassMethodSummaries.end())
Ted Kremenekc8395602008-05-06 21:26:51 +0000832 return I->second;
833
Ted Kremeneka22cc2f2008-05-06 23:07:13 +0000834 return 0;
Ted Kremenekc8395602008-05-06 21:26:51 +0000835}
836
Ted Kremenek1f180c32008-06-23 22:21:20 +0000837void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9c32d082008-05-06 00:30:21 +0000838
839 assert (ScratchArgs.empty());
840
Ted Kremeneka7344702008-06-23 18:02:52 +0000841 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
842 : RetEffect::MakeOwned(true);
843
Ted Kremenek9c32d082008-05-06 00:30:21 +0000844 RetainSummary* Summ = getPersistentSummary(E);
845
Ted Kremenek553cf182008-06-25 21:21:56 +0000846 // Create the summaries for "alloc", "new", and "allocWithZone:" for
847 // NSObject and its derivatives.
848 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
849 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
850 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Ted Kremenek9c32d082008-05-06 00:30:21 +0000851}
852
Ted Kremenek1f180c32008-06-23 22:21:20 +0000853void RetainSummaryManager::InitializeMethodSummaries() {
Ted Kremenekb3c3c282008-05-06 00:38:54 +0000854
855 assert (ScratchArgs.empty());
856
Ted Kremenekc8395602008-05-06 21:26:51 +0000857 // Create the "init" selector. It just acts as a pass-through for the
858 // receiver.
Ted Kremenek179064e2008-07-01 17:21:27 +0000859 RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias());
860 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
Ted Kremenekc8395602008-05-06 21:26:51 +0000861
862 // The next methods are allocators.
Ted Kremeneka7344702008-06-23 18:02:52 +0000863 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
864 : RetEffect::MakeOwned(true);
865
Ted Kremenek179064e2008-07-01 17:21:27 +0000866 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenekc8395602008-05-06 21:26:51 +0000867
868 // Create the "copy" selector.
Ted Kremenek553cf182008-06-25 21:21:56 +0000869 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ);
Ted Kremenekb3c3c282008-05-06 00:38:54 +0000870
871 // Create the "mutableCopy" selector.
Ted Kremenek553cf182008-06-25 21:21:56 +0000872 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000873
874 // Create the "retain" selector.
875 E = RetEffect::MakeReceiverAlias();
876 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef);
Ted Kremenek553cf182008-06-25 21:21:56 +0000877 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000878
879 // Create the "release" selector.
880 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +0000881 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Ted Kremenek299e8152008-05-07 21:17:39 +0000882
883 // Create the "drain" selector.
884 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +0000885 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000886
887 // Create the "autorelease" selector.
Ted Kremeneke19f4492008-06-30 16:57:41 +0000888 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : Autorelease);
Ted Kremenek553cf182008-06-25 21:21:56 +0000889 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
890
891 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek179064e2008-07-01 17:21:27 +0000892 // For NSPanel (which subclasses NSWindow), allocated objects are not
893 // self-owned.
894
895 RetainSummary *NSWindowSumm =
896 getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn);
Ted Kremenek553cf182008-06-25 21:21:56 +0000897
898 // Create the "initWithContentRect:styleMask:backing:defer:" selector.
899 llvm::SmallVector<IdentifierInfo*, 5> II;
900 II.push_back(&Ctx.Idents.get("initWithContentRect"));
901 II.push_back(&Ctx.Idents.get("styleMask"));
902 II.push_back(&Ctx.Idents.get("backing"));
903 II.push_back(&Ctx.Idents.get("defer"));
904 Selector S = Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek179064e2008-07-01 17:21:27 +0000905 addNSWindowMethSummary(S, NSWindowSumm);
906 addNSPanelMethSummary(S, InitSumm);
907
Ted Kremenek553cf182008-06-25 21:21:56 +0000908 // Create the "initWithContentRect:styleMask:backing:defer:screen:" selector.
909 II.push_back(&Ctx.Idents.get("screen"));
910 S = Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek179064e2008-07-01 17:21:27 +0000911 addNSWindowMethSummary(S, NSWindowSumm);
912 addNSPanelMethSummary(S, InitSumm);
Ted Kremenekb3c3c282008-05-06 00:38:54 +0000913}
914
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000915//===----------------------------------------------------------------------===//
Ted Kremenek13922612008-04-16 20:40:59 +0000916// Reference-counting logic (typestate + counts).
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000917//===----------------------------------------------------------------------===//
918
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000919namespace {
920
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000921class VISIBILITY_HIDDEN RefVal {
Ted Kremenek4fd88972008-04-17 18:12:53 +0000922public:
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000923
Ted Kremenek4fd88972008-04-17 18:12:53 +0000924 enum Kind {
925 Owned = 0, // Owning reference.
926 NotOwned, // Reference is not owned by still valid (not freed).
927 Released, // Object has been released.
928 ReturnedOwned, // Returned object passes ownership to caller.
929 ReturnedNotOwned, // Return object does not pass ownership to caller.
930 ErrorUseAfterRelease, // Object used after released.
931 ErrorReleaseNotOwned, // Release of an object that was not owned.
932 ErrorLeak // A memory leak due to excessive reference counts.
933 };
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000934
Ted Kremenek4fd88972008-04-17 18:12:53 +0000935private:
936
937 Kind kind;
938 unsigned Cnt;
Ted Kremenek553cf182008-06-25 21:21:56 +0000939 QualType T;
940
941 RefVal(Kind k, unsigned cnt, QualType t) : kind(k), Cnt(cnt), T(t) {}
942 RefVal(Kind k, unsigned cnt = 0) : kind(k), Cnt(cnt) {}
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000943
944public:
Ted Kremenekdb863712008-04-16 22:32:20 +0000945
Ted Kremenek4fd88972008-04-17 18:12:53 +0000946 Kind getKind() const { return kind; }
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000947
Ted Kremenek553cf182008-06-25 21:21:56 +0000948 unsigned getCount() const { return Cnt; }
949 QualType getType() const { return T; }
Ted Kremenek4fd88972008-04-17 18:12:53 +0000950
951 // Useful predicates.
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000952
Ted Kremenek73c750b2008-03-11 18:14:09 +0000953 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
954
Ted Kremenekdb863712008-04-16 22:32:20 +0000955 static bool isLeak(Kind k) { return k == ErrorLeak; }
956
Ted Kremeneke7bd9c22008-04-11 22:25:11 +0000957 bool isOwned() const {
958 return getKind() == Owned;
959 }
960
Ted Kremenekdb863712008-04-16 22:32:20 +0000961 bool isNotOwned() const {
962 return getKind() == NotOwned;
963 }
964
Ted Kremenek4fd88972008-04-17 18:12:53 +0000965 bool isReturnedOwned() const {
966 return getKind() == ReturnedOwned;
967 }
968
969 bool isReturnedNotOwned() const {
970 return getKind() == ReturnedNotOwned;
971 }
972
973 bool isNonLeakError() const {
974 Kind k = getKind();
975 return isError(k) && !isLeak(k);
976 }
977
978 // State creation: normal state.
979
Ted Kremenek553cf182008-06-25 21:21:56 +0000980 static RefVal makeOwned(QualType t, unsigned Count = 1) {
981 return RefVal(Owned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +0000982 }
983
Ted Kremenek553cf182008-06-25 21:21:56 +0000984 static RefVal makeNotOwned(QualType t, unsigned Count = 0) {
985 return RefVal(NotOwned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +0000986 }
Ted Kremenek4fd88972008-04-17 18:12:53 +0000987
988 static RefVal makeReturnedOwned(unsigned Count) {
989 return RefVal(ReturnedOwned, Count);
990 }
991
992 static RefVal makeReturnedNotOwned() {
993 return RefVal(ReturnedNotOwned);
994 }
995
996 // State creation: errors.
Ted Kremenek553cf182008-06-25 21:21:56 +0000997
998#if 0
Ted Kremenekce48e002008-05-05 17:53:17 +0000999 static RefVal makeLeak(unsigned Count) { return RefVal(ErrorLeak, Count); }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001000 static RefVal makeReleased() { return RefVal(Released); }
1001 static RefVal makeUseAfterRelease() { return RefVal(ErrorUseAfterRelease); }
1002 static RefVal makeReleaseNotOwned() { return RefVal(ErrorReleaseNotOwned); }
Ted Kremenek553cf182008-06-25 21:21:56 +00001003#endif
1004
Ted Kremenek4fd88972008-04-17 18:12:53 +00001005 // Comparison, profiling, and pretty-printing.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001006
Ted Kremenek4fd88972008-04-17 18:12:53 +00001007 bool operator==(const RefVal& X) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001008 return kind == X.kind && Cnt == X.Cnt && T == X.T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001009 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001010
Ted Kremenek553cf182008-06-25 21:21:56 +00001011 RefVal operator-(size_t i) const {
1012 return RefVal(getKind(), getCount() - i, getType());
1013 }
1014
1015 RefVal operator+(size_t i) const {
1016 return RefVal(getKind(), getCount() + i, getType());
1017 }
1018
1019 RefVal operator^(Kind k) const {
1020 return RefVal(k, getCount(), getType());
1021 }
1022
1023
Ted Kremenek4fd88972008-04-17 18:12:53 +00001024 void Profile(llvm::FoldingSetNodeID& ID) const {
1025 ID.AddInteger((unsigned) kind);
1026 ID.AddInteger(Cnt);
Ted Kremenek553cf182008-06-25 21:21:56 +00001027 ID.Add(T);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001028 }
1029
Ted Kremenekf3948042008-03-11 19:44:10 +00001030 void print(std::ostream& Out) const;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001031};
Ted Kremenekf3948042008-03-11 19:44:10 +00001032
1033void RefVal::print(std::ostream& Out) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001034 if (!T.isNull())
1035 Out << "Tracked Type:" << T.getAsString() << '\n';
1036
Ted Kremenekf3948042008-03-11 19:44:10 +00001037 switch (getKind()) {
1038 default: assert(false);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001039 case Owned: {
1040 Out << "Owned";
1041 unsigned cnt = getCount();
1042 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001043 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001044 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001045
Ted Kremenek61b9f872008-04-10 23:09:18 +00001046 case NotOwned: {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001047 Out << "NotOwned";
Ted Kremenek61b9f872008-04-10 23:09:18 +00001048 unsigned cnt = getCount();
1049 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001050 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001051 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001052
Ted Kremenek4fd88972008-04-17 18:12:53 +00001053 case ReturnedOwned: {
1054 Out << "ReturnedOwned";
1055 unsigned cnt = getCount();
1056 if (cnt) Out << " (+ " << cnt << ")";
1057 break;
1058 }
1059
1060 case ReturnedNotOwned: {
1061 Out << "ReturnedNotOwned";
1062 unsigned cnt = getCount();
1063 if (cnt) Out << " (+ " << cnt << ")";
1064 break;
1065 }
1066
Ted Kremenekf3948042008-03-11 19:44:10 +00001067 case Released:
1068 Out << "Released";
1069 break;
1070
Ted Kremenekdb863712008-04-16 22:32:20 +00001071 case ErrorLeak:
1072 Out << "Leaked";
1073 break;
1074
Ted Kremenekf3948042008-03-11 19:44:10 +00001075 case ErrorUseAfterRelease:
1076 Out << "Use-After-Release [ERROR]";
1077 break;
1078
1079 case ErrorReleaseNotOwned:
1080 Out << "Release of Not-Owned [ERROR]";
1081 break;
1082 }
1083}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001084
Ted Kremenek13922612008-04-16 20:40:59 +00001085//===----------------------------------------------------------------------===//
1086// Transfer functions.
1087//===----------------------------------------------------------------------===//
1088
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001089class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenek8dd56462008-04-18 03:39:05 +00001090public:
Ted Kremenek553cf182008-06-25 21:21:56 +00001091 // Type definitions.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001092 typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings;
Ted Kremenek553cf182008-06-25 21:21:56 +00001093
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001094 typedef RefBindings::Factory RefBFactoryTy;
Ted Kremenek73c750b2008-03-11 18:14:09 +00001095
Ted Kremenek8dd56462008-04-18 03:39:05 +00001096 typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolID> >
1097 ReleasesNotOwnedTy;
1098
1099 typedef ReleasesNotOwnedTy UseAfterReleasesTy;
1100
1101 typedef llvm::DenseMap<GRExprEngine::NodeTy*, std::vector<SymbolID>*>
Ted Kremenekdb863712008-04-16 22:32:20 +00001102 LeaksTy;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001103
Ted Kremenekf3948042008-03-11 19:44:10 +00001104 class BindingsPrinter : public ValueState::CheckerStatePrinter {
1105 public:
1106 virtual void PrintCheckerState(std::ostream& Out, void* State,
1107 const char* nl, const char* sep);
1108 };
Ted Kremenek8dd56462008-04-18 03:39:05 +00001109
1110private:
Ted Kremenekf3948042008-03-11 19:44:10 +00001111 // Instance variables.
1112
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001113 RetainSummaryManager Summaries;
1114 const bool EmitStandardWarnings;
1115 const LangOptions& LOpts;
1116 RefBFactoryTy RefBFactory;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001117
Ted Kremenek73c750b2008-03-11 18:14:09 +00001118 UseAfterReleasesTy UseAfterReleases;
1119 ReleasesNotOwnedTy ReleasesNotOwned;
Ted Kremenekdb863712008-04-16 22:32:20 +00001120 LeaksTy Leaks;
Ted Kremenek73c750b2008-03-11 18:14:09 +00001121
Ted Kremenekf3948042008-03-11 19:44:10 +00001122 BindingsPrinter Printer;
1123
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001124 Selector RetainSelector;
1125 Selector ReleaseSelector;
Ted Kremenek5934cee2008-05-01 02:18:37 +00001126 Selector AutoreleaseSelector;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001127
Ted Kremenek8dd56462008-04-18 03:39:05 +00001128public:
1129
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001130 static RefBindings GetRefBindings(ValueState& StImpl) {
1131 return RefBindings((RefBindings::TreeTy*) StImpl.CheckerState);
1132 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001133
Ted Kremenek8dd56462008-04-18 03:39:05 +00001134private:
1135
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001136 static void SetRefBindings(ValueState& StImpl, RefBindings B) {
1137 StImpl.CheckerState = B.getRoot();
1138 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001139
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001140 RefBindings Remove(RefBindings B, SymbolID sym) {
1141 return RefBFactory.Remove(B, sym);
1142 }
1143
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001144 RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E,
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001145 RefVal::Kind& hasErr);
1146
Ted Kremenekdb863712008-04-16 22:32:20 +00001147 void ProcessNonLeakError(ExplodedNodeSet<ValueState>& Dst,
1148 GRStmtNodeBuilder<ValueState>& Builder,
1149 Expr* NodeExpr, Expr* ErrorExpr,
1150 ExplodedNode<ValueState>* Pred,
1151 ValueState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001152 RefVal::Kind hasErr, SymbolID Sym);
Ted Kremenekdb863712008-04-16 22:32:20 +00001153
1154 ValueState* HandleSymbolDeath(ValueStateManager& VMgr, ValueState* St,
1155 SymbolID sid, RefVal V, bool& hasLeak);
1156
1157 ValueState* NukeBinding(ValueStateManager& VMgr, ValueState* St,
1158 SymbolID sid);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001159
1160public:
Ted Kremenek13922612008-04-16 20:40:59 +00001161
Ted Kremenek9f741612008-05-02 18:01:49 +00001162 CFRefCount(ASTContext& Ctx, bool gcenabled, bool StandardWarnings,
1163 const LangOptions& lopts)
Ted Kremenek377e2302008-04-29 05:33:51 +00001164 : Summaries(Ctx, gcenabled),
Ted Kremenek9f741612008-05-02 18:01:49 +00001165 EmitStandardWarnings(StandardWarnings),
Ted Kremenek072192b2008-04-30 23:47:44 +00001166 LOpts(lopts),
Ted Kremenekb83e02e2008-05-01 18:31:44 +00001167 RetainSelector(GetNullarySelector("retain", Ctx)),
1168 ReleaseSelector(GetNullarySelector("release", Ctx)),
1169 AutoreleaseSelector(GetNullarySelector("autorelease", Ctx)) {}
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001170
Ted Kremenek8dd56462008-04-18 03:39:05 +00001171 virtual ~CFRefCount() {
1172 for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I)
1173 delete I->second;
1174 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001175
1176 virtual void RegisterChecks(GRExprEngine& Eng);
Ted Kremenekf3948042008-03-11 19:44:10 +00001177
1178 virtual ValueState::CheckerStatePrinter* getCheckerStatePrinter() {
1179 return &Printer;
1180 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001181
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001182 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenek072192b2008-04-30 23:47:44 +00001183 const LangOptions& getLangOptions() const { return LOpts; }
1184
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001185 // Calls.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001186
1187 void EvalSummary(ExplodedNodeSet<ValueState>& Dst,
1188 GRExprEngine& Eng,
1189 GRStmtNodeBuilder<ValueState>& Builder,
1190 Expr* Ex,
1191 Expr* Receiver,
1192 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001193 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001194 ExplodedNode<ValueState>* Pred);
1195
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001196 virtual void EvalCall(ExplodedNodeSet<ValueState>& Dst,
Ted Kremenek199e1a02008-03-12 21:06:49 +00001197 GRExprEngine& Eng,
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001198 GRStmtNodeBuilder<ValueState>& Builder,
Ted Kremenek186350f2008-04-23 20:12:28 +00001199 CallExpr* CE, RVal L,
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001200 ExplodedNode<ValueState>* Pred);
Ted Kremenekfa34b332008-04-09 01:10:13 +00001201
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001202
Ted Kremenek85348202008-04-15 23:44:31 +00001203 virtual void EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst,
1204 GRExprEngine& Engine,
1205 GRStmtNodeBuilder<ValueState>& Builder,
1206 ObjCMessageExpr* ME,
1207 ExplodedNode<ValueState>* Pred);
1208
1209 bool EvalObjCMessageExprAux(ExplodedNodeSet<ValueState>& Dst,
1210 GRExprEngine& Engine,
1211 GRStmtNodeBuilder<ValueState>& Builder,
1212 ObjCMessageExpr* ME,
1213 ExplodedNode<ValueState>* Pred);
1214
Ted Kremenek13922612008-04-16 20:40:59 +00001215 // Stores.
1216
1217 virtual void EvalStore(ExplodedNodeSet<ValueState>& Dst,
1218 GRExprEngine& Engine,
1219 GRStmtNodeBuilder<ValueState>& Builder,
1220 Expr* E, ExplodedNode<ValueState>* Pred,
1221 ValueState* St, RVal TargetLV, RVal Val);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001222 // End-of-path.
1223
1224 virtual void EvalEndPath(GRExprEngine& Engine,
1225 GREndPathNodeBuilder<ValueState>& Builder);
1226
Ted Kremenek652adc62008-04-24 23:57:27 +00001227 virtual void EvalDeadSymbols(ExplodedNodeSet<ValueState>& Dst,
1228 GRExprEngine& Engine,
1229 GRStmtNodeBuilder<ValueState>& Builder,
Ted Kremenek910e9992008-04-25 01:25:15 +00001230 ExplodedNode<ValueState>* Pred,
1231 Stmt* S,
Ted Kremenek652adc62008-04-24 23:57:27 +00001232 ValueState* St,
1233 const ValueStateManager::DeadSymbolsTy& Dead);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001234 // Return statements.
1235
1236 virtual void EvalReturn(ExplodedNodeSet<ValueState>& Dst,
1237 GRExprEngine& Engine,
1238 GRStmtNodeBuilder<ValueState>& Builder,
1239 ReturnStmt* S,
1240 ExplodedNode<ValueState>* Pred);
Ted Kremenekcb612922008-04-18 19:23:43 +00001241
1242 // Assumptions.
1243
1244 virtual ValueState* EvalAssume(GRExprEngine& Engine, ValueState* St,
1245 RVal Cond, bool Assumption, bool& isFeasible);
1246
Ted Kremenekfa34b332008-04-09 01:10:13 +00001247 // Error iterators.
1248
1249 typedef UseAfterReleasesTy::iterator use_after_iterator;
1250 typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
Ted Kremenek989d5192008-04-17 23:43:50 +00001251 typedef LeaksTy::iterator leaks_iterator;
Ted Kremenekfa34b332008-04-09 01:10:13 +00001252
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001253 use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
1254 use_after_iterator use_after_end() { return UseAfterReleases.end(); }
Ted Kremenekfa34b332008-04-09 01:10:13 +00001255
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001256 bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
1257 bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
Ted Kremenek989d5192008-04-17 23:43:50 +00001258
1259 leaks_iterator leaks_begin() { return Leaks.begin(); }
1260 leaks_iterator leaks_end() { return Leaks.end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001261};
1262
1263} // end anonymous namespace
1264
Ted Kremenek8dd56462008-04-18 03:39:05 +00001265
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001266
1267
Ted Kremenekf3948042008-03-11 19:44:10 +00001268void CFRefCount::BindingsPrinter::PrintCheckerState(std::ostream& Out,
1269 void* State, const char* nl,
1270 const char* sep) {
1271 RefBindings B((RefBindings::TreeTy*) State);
1272
1273 if (State)
1274 Out << sep << nl;
1275
1276 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1277 Out << (*I).first << " : ";
1278 (*I).second.print(Out);
1279 Out << nl;
1280 }
1281}
1282
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001283static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) {
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001284 return Summ ? Summ->getArg(idx) : MayEscape;
Ted Kremenekf9561e52008-04-11 20:23:24 +00001285}
1286
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001287static inline RetEffect GetRetEffect(RetainSummary* Summ) {
1288 return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
Ted Kremenekf9561e52008-04-11 20:23:24 +00001289}
1290
Ted Kremenek14993892008-05-06 02:41:27 +00001291static inline ArgEffect GetReceiverE(RetainSummary* Summ) {
1292 return Summ ? Summ->getReceiverEffect() : DoNothing;
1293}
1294
Ted Kremenekdb863712008-04-16 22:32:20 +00001295void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<ValueState>& Dst,
1296 GRStmtNodeBuilder<ValueState>& Builder,
1297 Expr* NodeExpr, Expr* ErrorExpr,
1298 ExplodedNode<ValueState>* Pred,
1299 ValueState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001300 RefVal::Kind hasErr, SymbolID Sym) {
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001301 Builder.BuildSinks = true;
1302 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
1303
1304 if (!N) return;
1305
1306 switch (hasErr) {
1307 default: assert(false);
1308 case RefVal::ErrorUseAfterRelease:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001309 UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001310 break;
1311
1312 case RefVal::ErrorReleaseNotOwned:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001313 ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001314 break;
1315 }
1316}
1317
Ted Kremenek553cf182008-06-25 21:21:56 +00001318/// GetReturnType - Used to get the return type of a message expression or
1319/// function call with the intention of affixing that type to a tracked symbol.
1320/// While the the return type can be queried directly from RetEx, when
1321/// invoking class methods we augment to the return type to be that of
1322/// a pointer to the class (as opposed it just being id).
1323static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) {
1324
1325 QualType RetTy = RetE->getType();
1326
1327 // FIXME: We aren't handling id<...>.
1328 const PointerType* PT = RetTy.getCanonicalType()->getAsPointerType();
1329
1330 if (!PT)
1331 return RetTy;
1332
1333 // If RetEx is not a message expression just return its type.
1334 // If RetEx is a message expression, return its types if it is something
1335 /// more specific than id.
1336
1337 ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE);
1338
1339 if (!ME || !Ctx.isObjCIdType(PT->getPointeeType()))
1340 return RetTy;
1341
1342 ObjCInterfaceDecl* D = ME->getClassInfo().first;
1343
1344 // At this point we know the return type of the message expression is id.
1345 // If we have an ObjCInterceDecl, we know this is a call to a class method
1346 // whose type we can resolve. In such cases, promote the return type to
1347 // Class*.
1348 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
1349}
1350
1351
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001352void CFRefCount::EvalSummary(ExplodedNodeSet<ValueState>& Dst,
1353 GRExprEngine& Eng,
1354 GRStmtNodeBuilder<ValueState>& Builder,
1355 Expr* Ex,
1356 Expr* Receiver,
1357 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001358 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001359 ExplodedNode<ValueState>* Pred) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001360
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001361 // Get the state.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001362 ValueStateManager& StateMgr = Eng.getStateManager();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001363 ValueState* St = Builder.GetState(Pred);
Ted Kremenek14993892008-05-06 02:41:27 +00001364
1365 // Evaluate the effect of the arguments.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001366 ValueState StVals = *St;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001367 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001368 unsigned idx = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001369 Expr* ErrorExpr = NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001370 SymbolID ErrorSym = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001371
Ted Kremenek55499762008-06-17 02:43:46 +00001372 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001373
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001374 RVal V = StateMgr.GetRVal(St, *I);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001375
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001376 if (isa<lval::SymbolVal>(V)) {
1377 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
Ted Kremenekf9561e52008-04-11 20:23:24 +00001378 RefBindings B = GetRefBindings(StVals);
1379
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001380 if (RefBindings::data_type* T = B.lookup(Sym)) {
1381 B = Update(B, Sym, *T, GetArgE(Summ, idx), hasErr);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001382 SetRefBindings(StVals, B);
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001383
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001384 if (hasErr) {
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001385 ErrorExpr = *I;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001386 ErrorSym = Sym;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001387 break;
1388 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001389 }
Ted Kremenekb8873552008-04-11 20:51:02 +00001390 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001391 else if (isa<LVal>(V)) {
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001392#if 0
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001393 // Nuke all arguments passed by reference.
Ted Kremenekf9561e52008-04-11 20:23:24 +00001394 StateMgr.Unbind(StVals, cast<LVal>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001395#else
1396 if (lval::DeclVal* DV = dyn_cast<lval::DeclVal>(&V)) {
1397
1398 // FIXME: Either this logic should also be replicated in GRSimpleVals
1399 // or should be pulled into a separate "constraint engine."
1400 // FIXME: We can have collisions on the conjured symbol if the
1401 // expression *I also creates conjured symbols. We probably want
1402 // to identify conjured symbols by an expression pair: the enclosing
1403 // expression (the context) and the expression itself. This should
1404 // disambiguate conjured symbols.
1405
1406 // Invalidate the values of all variables passed by reference.
1407 // Set the value of the variable to be a conjured symbol.
1408 unsigned Count = Builder.getCurrentBlockCount();
1409 SymbolID NewSym = Eng.getSymbolManager().getConjuredSymbol(*I, Count);
1410
1411 StateMgr.BindVar(StVals, DV->getDecl(),
1412 LVal::IsLValType(DV->getDecl()->getType())
1413 ? cast<RVal>(lval::SymbolVal(NewSym))
1414 : cast<RVal>(nonlval::SymbolVal(NewSym)));
1415 }
1416 else {
1417 // Nuke all other arguments passed by reference.
1418 StateMgr.Unbind(StVals, cast<LVal>(V));
1419 }
1420#endif
Ted Kremenekb8873552008-04-11 20:51:02 +00001421 }
Ted Kremeneka5488462008-04-22 21:39:21 +00001422 else if (isa<nonlval::LValAsInteger>(V))
1423 StateMgr.Unbind(StVals, cast<nonlval::LValAsInteger>(V).getLVal());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001424 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001425
Ted Kremenek553cf182008-06-25 21:21:56 +00001426 // Evaluate the effect on the message receiver.
Ted Kremenek14993892008-05-06 02:41:27 +00001427 if (!ErrorExpr && Receiver) {
1428 RVal V = StateMgr.GetRVal(St, Receiver);
1429
1430 if (isa<lval::SymbolVal>(V)) {
1431 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
1432 RefBindings B = GetRefBindings(StVals);
1433
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001434 if (const RefVal* T = B.lookup(Sym)) {
1435 B = Update(B, Sym, *T, GetReceiverE(Summ), hasErr);
Ted Kremenek14993892008-05-06 02:41:27 +00001436 SetRefBindings(StVals, B);
1437
1438 if (hasErr) {
1439 ErrorExpr = Receiver;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001440 ErrorSym = Sym;
Ted Kremenek14993892008-05-06 02:41:27 +00001441 }
1442 }
1443 }
1444 }
1445
Ted Kremenek553cf182008-06-25 21:21:56 +00001446 // Get the persistent state.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001447 St = StateMgr.getPersistentState(StVals);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001448
Ted Kremenek553cf182008-06-25 21:21:56 +00001449 // Process any errors.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001450 if (hasErr) {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001451 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001452 hasErr, ErrorSym);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001453 return;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001454 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001455
Ted Kremenek553cf182008-06-25 21:21:56 +00001456 // Finally, consult the summary for the return value.
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001457 RetEffect RE = GetRetEffect(Summ);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001458
1459 switch (RE.getKind()) {
1460 default:
1461 assert (false && "Unhandled RetEffect."); break;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001462
Ted Kremenek940b1d82008-04-10 23:44:06 +00001463 case RetEffect::NoRet:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001464
Ted Kremenekf9561e52008-04-11 20:23:24 +00001465 // Make up a symbol for the return value (not reference counted).
Ted Kremenekb8873552008-04-11 20:51:02 +00001466 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
1467 // should compose behavior, not copy it.
Ted Kremenekf9561e52008-04-11 20:23:24 +00001468
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001469 if (Ex->getType() != Eng.getContext().VoidTy) {
Ted Kremenekf9561e52008-04-11 20:23:24 +00001470 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001471 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001472
Ted Kremenek0e470a52008-05-09 23:45:33 +00001473 RVal X = LVal::IsLValType(Ex->getType())
1474 ? cast<RVal>(lval::SymbolVal(Sym))
1475 : cast<RVal>(nonlval::SymbolVal(Sym));
Ted Kremenekf9561e52008-04-11 20:23:24 +00001476
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001477 St = StateMgr.SetRVal(St, Ex, X, Eng.getCFG().isBlkExpr(Ex), false);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001478 }
1479
Ted Kremenek940b1d82008-04-10 23:44:06 +00001480 break;
1481
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001482 case RetEffect::Alias: {
Ted Kremenek553cf182008-06-25 21:21:56 +00001483 unsigned idx = RE.getIndex();
Ted Kremenek55499762008-06-17 02:43:46 +00001484 assert (arg_end >= arg_beg);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001485 assert (idx < (unsigned) (arg_end - arg_beg));
Ted Kremenek55499762008-06-17 02:43:46 +00001486 RVal V = StateMgr.GetRVal(St, *(arg_beg+idx));
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001487 St = StateMgr.SetRVal(St, Ex, V, Eng.getCFG().isBlkExpr(Ex), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001488 break;
1489 }
1490
Ted Kremenek14993892008-05-06 02:41:27 +00001491 case RetEffect::ReceiverAlias: {
1492 assert (Receiver);
1493 RVal V = StateMgr.GetRVal(St, Receiver);
1494 St = StateMgr.SetRVal(St, Ex, V, Eng.getCFG().isBlkExpr(Ex), false);
1495 break;
1496 }
1497
Ted Kremeneka7344702008-06-23 18:02:52 +00001498 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001499 case RetEffect::OwnedSymbol: {
1500 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001501 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001502 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001503
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001504 ValueState StImpl = *St;
1505 RefBindings B = GetRefBindings(StImpl);
Ted Kremenek553cf182008-06-25 21:21:56 +00001506 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(RetT)));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001507
1508 St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl),
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001509 Ex, lval::SymbolVal(Sym),
1510 Eng.getCFG().isBlkExpr(Ex), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001511
Ted Kremeneka7344702008-06-23 18:02:52 +00001512 // FIXME: Add a flag to the checker where allocations are allowed to fail.
1513 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol)
1514 St = StateMgr.AddNE(St, Sym, Eng.getBasicVals().getZeroWithPtrWidth());
1515
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001516 break;
1517 }
1518
1519 case RetEffect::NotOwnedSymbol: {
1520 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001521 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001522 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001523
1524 ValueState StImpl = *St;
1525 RefBindings B = GetRefBindings(StImpl);
Ted Kremenek553cf182008-06-25 21:21:56 +00001526 SetRefBindings(StImpl, RefBFactory.Add(B, Sym,
1527 RefVal::makeNotOwned(RetT)));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001528
1529 St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl),
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001530 Ex, lval::SymbolVal(Sym),
1531 Eng.getCFG().isBlkExpr(Ex), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001532
1533 break;
1534 }
1535 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001536
1537 Builder.MakeNode(Dst, Ex, Pred, St);
1538}
1539
1540
1541void CFRefCount::EvalCall(ExplodedNodeSet<ValueState>& Dst,
1542 GRExprEngine& Eng,
1543 GRStmtNodeBuilder<ValueState>& Builder,
1544 CallExpr* CE, RVal L,
1545 ExplodedNode<ValueState>* Pred) {
1546
1547
1548 RetainSummary* Summ = NULL;
1549
1550 // Get the summary.
1551
1552 if (isa<lval::FuncVal>(L)) {
1553 lval::FuncVal FV = cast<lval::FuncVal>(L);
1554 FunctionDecl* FD = FV.getDecl();
Ted Kremenekab592272008-06-24 03:56:45 +00001555 Summ = Summaries.getSummary(FD);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001556 }
1557
1558 EvalSummary(Dst, Eng, Builder, CE, 0, Summ,
1559 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenek2fff37e2008-03-06 00:08:09 +00001560}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001561
Ted Kremenek85348202008-04-15 23:44:31 +00001562
1563void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst,
1564 GRExprEngine& Eng,
1565 GRStmtNodeBuilder<ValueState>& Builder,
1566 ObjCMessageExpr* ME,
1567 ExplodedNode<ValueState>* Pred) {
1568
Ted Kremenekb3095252008-05-06 04:20:12 +00001569 RetainSummary* Summ;
Ted Kremenek9040c652008-05-01 21:31:50 +00001570
Ted Kremenek553cf182008-06-25 21:21:56 +00001571 if (Expr* Receiver = ME->getReceiver()) {
1572 // We need the type-information of the tracked receiver object
1573 // Retrieve it from the state.
1574 ObjCInterfaceDecl* ID = 0;
1575
1576 // FIXME: Wouldn't it be great if this code could be reduced? It's just
1577 // a chain of lookups.
1578 ValueState* St = Builder.GetState(Pred);
1579 RVal V = Eng.getStateManager().GetRVal(St, Receiver );
1580
1581 if (isa<lval::SymbolVal>(V)) {
1582 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
1583
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001584 if (const RefVal* T = GetRefBindings(*St).lookup(Sym)) {
1585 QualType Ty = T->getType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001586
1587 if (const PointerType* PT = Ty->getAsPointerType()) {
1588 QualType PointeeTy = PT->getPointeeType();
1589
1590 if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy))
1591 ID = IT->getDecl();
1592 }
1593 }
1594 }
1595
1596 Summ = Summaries.getMethodSummary(ME, ID);
1597 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001598 else
Ted Kremenek1f180c32008-06-23 22:21:20 +00001599 Summ = Summaries.getClassMethodSummary(ME->getClassName(),
1600 ME->getSelector());
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001601
Ted Kremenekb3095252008-05-06 04:20:12 +00001602 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ,
1603 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001604}
Ted Kremenekb3095252008-05-06 04:20:12 +00001605
Ted Kremenek13922612008-04-16 20:40:59 +00001606// Stores.
1607
1608void CFRefCount::EvalStore(ExplodedNodeSet<ValueState>& Dst,
1609 GRExprEngine& Eng,
1610 GRStmtNodeBuilder<ValueState>& Builder,
1611 Expr* E, ExplodedNode<ValueState>* Pred,
1612 ValueState* St, RVal TargetLV, RVal Val) {
1613
1614 // Check if we have a binding for "Val" and if we are storing it to something
1615 // we don't understand or otherwise the value "escapes" the function.
1616
1617 if (!isa<lval::SymbolVal>(Val))
1618 return;
1619
1620 // Are we storing to something that causes the value to "escape"?
1621
1622 bool escapes = false;
1623
1624 if (!isa<lval::DeclVal>(TargetLV))
1625 escapes = true;
1626 else
1627 escapes = cast<lval::DeclVal>(TargetLV).getDecl()->hasGlobalStorage();
1628
1629 if (!escapes)
1630 return;
1631
1632 SymbolID Sym = cast<lval::SymbolVal>(Val).getSymbol();
Ted Kremenek13922612008-04-16 20:40:59 +00001633
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001634 if (!GetRefBindings(*St).lookup(Sym))
Ted Kremenek13922612008-04-16 20:40:59 +00001635 return;
1636
Ted Kremenekdb863712008-04-16 22:32:20 +00001637 // Nuke the binding.
1638 St = NukeBinding(Eng.getStateManager(), St, Sym);
Ted Kremenek13922612008-04-16 20:40:59 +00001639
1640 // Hand of the remaining logic to the parent implementation.
1641 GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, St, TargetLV, Val);
1642}
1643
Ted Kremenekdb863712008-04-16 22:32:20 +00001644
1645ValueState* CFRefCount::NukeBinding(ValueStateManager& VMgr, ValueState* St,
1646 SymbolID sid) {
1647 ValueState StImpl = *St;
1648 RefBindings B = GetRefBindings(StImpl);
1649 StImpl.CheckerState = RefBFactory.Remove(B, sid).getRoot();
1650 return VMgr.getPersistentState(StImpl);
1651}
1652
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001653// End-of-path.
1654
Ted Kremenekdb863712008-04-16 22:32:20 +00001655ValueState* CFRefCount::HandleSymbolDeath(ValueStateManager& VMgr,
1656 ValueState* St, SymbolID sid,
1657 RefVal V, bool& hasLeak) {
1658
Ted Kremenek4fd88972008-04-17 18:12:53 +00001659 hasLeak = V.isOwned() ||
1660 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Ted Kremenekdb863712008-04-16 22:32:20 +00001661
1662 if (!hasLeak)
1663 return NukeBinding(VMgr, St, sid);
1664
1665 RefBindings B = GetRefBindings(*St);
Ted Kremenek553cf182008-06-25 21:21:56 +00001666 ValueState StImpl = *St;
1667 StImpl.CheckerState = RefBFactory.Add(B, sid, V^RefVal::ErrorLeak).getRoot();
Ted Kremenekce48e002008-05-05 17:53:17 +00001668
Ted Kremenekdb863712008-04-16 22:32:20 +00001669 return VMgr.getPersistentState(StImpl);
1670}
1671
1672void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001673 GREndPathNodeBuilder<ValueState>& Builder) {
1674
Ted Kremenekdb863712008-04-16 22:32:20 +00001675 ValueState* St = Builder.getState();
1676 RefBindings B = GetRefBindings(*St);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001677
Ted Kremenekdb863712008-04-16 22:32:20 +00001678 llvm::SmallVector<SymbolID, 10> Leaked;
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001679
Ted Kremenekdb863712008-04-16 22:32:20 +00001680 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1681 bool hasLeak = false;
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001682
Ted Kremenekdb863712008-04-16 22:32:20 +00001683 St = HandleSymbolDeath(Eng.getStateManager(), St,
1684 (*I).first, (*I).second, hasLeak);
1685
1686 if (hasLeak) Leaked.push_back((*I).first);
1687 }
Ted Kremenek652adc62008-04-24 23:57:27 +00001688
1689 if (Leaked.empty())
1690 return;
1691
Ted Kremenek8dd56462008-04-18 03:39:05 +00001692 ExplodedNode<ValueState>* N = Builder.MakeNode(St);
Ted Kremenek4f285152008-04-18 16:30:14 +00001693
Ted Kremenek652adc62008-04-24 23:57:27 +00001694 if (!N)
Ted Kremenek4f285152008-04-18 16:30:14 +00001695 return;
Ted Kremenekcb612922008-04-18 19:23:43 +00001696
Ted Kremenek8dd56462008-04-18 03:39:05 +00001697 std::vector<SymbolID>*& LeaksAtNode = Leaks[N];
1698 assert (!LeaksAtNode);
1699 LeaksAtNode = new std::vector<SymbolID>();
Ted Kremenekdb863712008-04-16 22:32:20 +00001700
1701 for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(),
1702 E = Leaked.end(); I != E; ++I)
Ted Kremenek8dd56462008-04-18 03:39:05 +00001703 (*LeaksAtNode).push_back(*I);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001704}
1705
Ted Kremenek652adc62008-04-24 23:57:27 +00001706// Dead symbols.
1707
1708void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<ValueState>& Dst,
1709 GRExprEngine& Eng,
1710 GRStmtNodeBuilder<ValueState>& Builder,
Ted Kremenek910e9992008-04-25 01:25:15 +00001711 ExplodedNode<ValueState>* Pred,
1712 Stmt* S,
Ted Kremenek652adc62008-04-24 23:57:27 +00001713 ValueState* St,
1714 const ValueStateManager::DeadSymbolsTy& Dead) {
Ted Kremenek910e9992008-04-25 01:25:15 +00001715
Ted Kremenek652adc62008-04-24 23:57:27 +00001716 // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
1717
1718 RefBindings B = GetRefBindings(*St);
1719 llvm::SmallVector<SymbolID, 10> Leaked;
1720
1721 for (ValueStateManager::DeadSymbolsTy::const_iterator
1722 I=Dead.begin(), E=Dead.end(); I!=E; ++I) {
1723
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001724 const RefVal* T = B.lookup(*I);
Ted Kremenek652adc62008-04-24 23:57:27 +00001725
1726 if (!T)
1727 continue;
1728
1729 bool hasLeak = false;
1730
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001731 St = HandleSymbolDeath(Eng.getStateManager(), St, *I, *T, hasLeak);
Ted Kremenek652adc62008-04-24 23:57:27 +00001732
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001733 if (hasLeak)
1734 Leaked.push_back(*I);
Ted Kremenek652adc62008-04-24 23:57:27 +00001735 }
1736
1737 if (Leaked.empty())
1738 return;
1739
1740 ExplodedNode<ValueState>* N = Builder.MakeNode(Dst, S, Pred, St);
1741
1742 if (!N)
1743 return;
1744
1745 std::vector<SymbolID>*& LeaksAtNode = Leaks[N];
1746 assert (!LeaksAtNode);
1747 LeaksAtNode = new std::vector<SymbolID>();
1748
1749 for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(),
1750 E = Leaked.end(); I != E; ++I)
1751 (*LeaksAtNode).push_back(*I);
1752}
1753
Ted Kremenek4fd88972008-04-17 18:12:53 +00001754 // Return statements.
1755
1756void CFRefCount::EvalReturn(ExplodedNodeSet<ValueState>& Dst,
1757 GRExprEngine& Eng,
1758 GRStmtNodeBuilder<ValueState>& Builder,
1759 ReturnStmt* S,
1760 ExplodedNode<ValueState>* Pred) {
1761
1762 Expr* RetE = S->getRetValue();
1763 if (!RetE) return;
1764
1765 ValueStateManager& StateMgr = Eng.getStateManager();
1766 ValueState* St = Builder.GetState(Pred);
1767 RVal V = StateMgr.GetRVal(St, RetE);
1768
1769 if (!isa<lval::SymbolVal>(V))
1770 return;
1771
1772 // Get the reference count binding (if any).
1773 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
1774 RefBindings B = GetRefBindings(*St);
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001775 const RefVal* T = B.lookup(Sym);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001776
1777 if (!T)
1778 return;
1779
1780 // Change the reference count.
1781
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001782 RefVal X = *T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001783
1784 switch (X.getKind()) {
1785
1786 case RefVal::Owned: {
1787 unsigned cnt = X.getCount();
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001788 assert (cnt > 0);
1789 X = RefVal::makeReturnedOwned(cnt - 1);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001790 break;
1791 }
1792
1793 case RefVal::NotOwned: {
1794 unsigned cnt = X.getCount();
1795 X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
1796 : RefVal::makeReturnedNotOwned();
1797 break;
1798 }
1799
1800 default:
Ted Kremenek4fd88972008-04-17 18:12:53 +00001801 return;
1802 }
1803
1804 // Update the binding.
1805
1806 ValueState StImpl = *St;
1807 StImpl.CheckerState = RefBFactory.Add(B, Sym, X).getRoot();
1808 Builder.MakeNode(Dst, S, Pred, StateMgr.getPersistentState(StImpl));
1809}
1810
Ted Kremenekcb612922008-04-18 19:23:43 +00001811// Assumptions.
1812
1813ValueState* CFRefCount::EvalAssume(GRExprEngine& Eng, ValueState* St,
1814 RVal Cond, bool Assumption,
1815 bool& isFeasible) {
1816
1817 // FIXME: We may add to the interface of EvalAssume the list of symbols
1818 // whose assumptions have changed. For now we just iterate through the
1819 // bindings and check if any of the tracked symbols are NULL. This isn't
1820 // too bad since the number of symbols we will track in practice are
1821 // probably small and EvalAssume is only called at branches and a few
1822 // other places.
1823
1824 RefBindings B = GetRefBindings(*St);
1825
1826 if (B.isEmpty())
1827 return St;
1828
1829 bool changed = false;
1830
1831 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1832
1833 // Check if the symbol is null (or equal to any constant).
1834 // If this is the case, stop tracking the symbol.
1835
1836 if (St->getSymVal(I.getKey())) {
1837 changed = true;
1838 B = RefBFactory.Remove(B, I.getKey());
1839 }
1840 }
1841
1842 if (!changed)
1843 return St;
1844
1845 ValueState StImpl = *St;
1846 StImpl.CheckerState = B.getRoot();
1847 return Eng.getStateManager().getPersistentState(StImpl);
1848}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001849
1850CFRefCount::RefBindings CFRefCount::Update(RefBindings B, SymbolID sym,
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001851 RefVal V, ArgEffect E,
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001852 RefVal::Kind& hasErr) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001853
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001854 // FIXME: This dispatch can potentially be sped up by unifiying it into
1855 // a single switch statement. Opt for simplicity for now.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001856
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001857 switch (E) {
1858 default:
1859 assert (false && "Unhandled CFRef transition.");
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001860
1861 case MayEscape:
1862 if (V.getKind() == RefVal::Owned) {
Ted Kremenek553cf182008-06-25 21:21:56 +00001863 V = V ^ RefVal::NotOwned;
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001864 break;
1865 }
1866
1867 // Fall-through.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001868
1869 case DoNothing:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001870 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
Ted Kremenek553cf182008-06-25 21:21:56 +00001871 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001872 hasErr = V.getKind();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001873 break;
1874 }
1875
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001876 return B;
Ted Kremeneke19f4492008-06-30 16:57:41 +00001877
Ted Kremenek80d753f2008-07-01 00:01:02 +00001878 case Autorelease:
Ted Kremenek14993892008-05-06 02:41:27 +00001879 case StopTracking:
1880 return RefBFactory.Remove(B, sym);
1881
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001882 case IncRef:
1883 switch (V.getKind()) {
1884 default:
1885 assert(false);
1886
1887 case RefVal::Owned:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001888 case RefVal::NotOwned:
Ted Kremenek553cf182008-06-25 21:21:56 +00001889 V = V + 1;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001890 break;
1891
1892 case RefVal::Released:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001893 if (isGCEnabled())
Ted Kremenek553cf182008-06-25 21:21:56 +00001894 V = V ^ RefVal::Owned;
Ted Kremenek65c91652008-04-29 05:44:10 +00001895 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00001896 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek65c91652008-04-29 05:44:10 +00001897 hasErr = V.getKind();
1898 }
1899
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001900 break;
1901 }
1902
Ted Kremenek940b1d82008-04-10 23:44:06 +00001903 break;
1904
Ted Kremenek553cf182008-06-25 21:21:56 +00001905 case SelfOwn:
1906 V = V ^ RefVal::NotOwned;
1907
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001908 case DecRef:
1909 switch (V.getKind()) {
1910 default:
1911 assert (false);
1912
Ted Kremenek553cf182008-06-25 21:21:56 +00001913 case RefVal::Owned:
1914 V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001915 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001916
Ted Kremenek553cf182008-06-25 21:21:56 +00001917 case RefVal::NotOwned:
1918 if (V.getCount() > 0)
1919 V = V - 1;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001920 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00001921 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001922 hasErr = V.getKind();
Ted Kremenek61b9f872008-04-10 23:09:18 +00001923 }
1924
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001925 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001926
1927 case RefVal::Released:
Ted Kremenek553cf182008-06-25 21:21:56 +00001928 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001929 hasErr = V.getKind();
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001930 break;
1931 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00001932
1933 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001934 }
1935
1936 return RefBFactory.Add(B, sym, V);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001937}
1938
Ted Kremenekfa34b332008-04-09 01:10:13 +00001939
1940//===----------------------------------------------------------------------===//
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001941// Error reporting.
Ted Kremenekfa34b332008-04-09 01:10:13 +00001942//===----------------------------------------------------------------------===//
1943
Ted Kremenek8dd56462008-04-18 03:39:05 +00001944namespace {
1945
1946 //===-------------===//
1947 // Bug Descriptions. //
1948 //===-------------===//
1949
Ted Kremenek95cc1ba2008-04-18 20:54:29 +00001950 class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation {
Ted Kremenek8dd56462008-04-18 03:39:05 +00001951 protected:
1952 CFRefCount& TF;
1953
1954 public:
1955 CFRefBug(CFRefCount& tf) : TF(tf) {}
Ted Kremenek072192b2008-04-30 23:47:44 +00001956
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00001957 CFRefCount& getTF() { return TF; }
Ted Kremenek789deac2008-05-05 23:16:31 +00001958 const CFRefCount& getTF() const { return TF; }
1959
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00001960 virtual bool isLeak() const { return false; }
Ted Kremenek8dd56462008-04-18 03:39:05 +00001961 };
1962
1963 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
1964 public:
1965 UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
1966
1967 virtual const char* getName() const {
Ted Kremenek789deac2008-05-05 23:16:31 +00001968 return "Use-After-Release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00001969 }
1970 virtual const char* getDescription() const {
Ted Kremenek2cf943a2008-04-18 04:55:01 +00001971 return "Reference-counted object is used"
1972 " after it is released.";
Ted Kremenek8dd56462008-04-18 03:39:05 +00001973 }
1974
1975 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek8dd56462008-04-18 03:39:05 +00001976 };
1977
1978 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
1979 public:
1980 BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
1981
1982 virtual const char* getName() const {
Ted Kremenek789deac2008-05-05 23:16:31 +00001983 return "Bad Release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00001984 }
1985 virtual const char* getDescription() const {
1986 return "Incorrect decrement of the reference count of a "
Ted Kremenek2cf943a2008-04-18 04:55:01 +00001987 "CoreFoundation object: "
Ted Kremenek8dd56462008-04-18 03:39:05 +00001988 "The object is not owned at this point by the caller.";
1989 }
1990
1991 virtual void EmitWarnings(BugReporter& BR);
1992 };
1993
1994 class VISIBILITY_HIDDEN Leak : public CFRefBug {
1995 public:
1996 Leak(CFRefCount& tf) : CFRefBug(tf) {}
1997
1998 virtual const char* getName() const {
Ted Kremenek432af592008-05-06 18:11:36 +00001999
2000 if (getTF().isGCEnabled())
2001 return "Memory Leak (GC)";
2002
2003 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
2004 return "Memory Leak (Hybrid MM, non-GC)";
2005
2006 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
2007 return "Memory Leak";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002008 }
2009
2010 virtual const char* getDescription() const {
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002011 return "Object leaked.";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002012 }
2013
2014 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002015 virtual void GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002016 virtual bool isLeak() const { return true; }
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002017 virtual bool isCached(BugReport& R);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002018 };
2019
2020 //===---------===//
2021 // Bug Reports. //
2022 //===---------===//
2023
2024 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
2025 SymbolID Sym;
2026 public:
Ted Kremenek072192b2008-04-30 23:47:44 +00002027 CFRefReport(CFRefBug& D, ExplodedNode<ValueState> *n, SymbolID sym)
Ted Kremenek8dd56462008-04-18 03:39:05 +00002028 : RangedBugReport(D, n), Sym(sym) {}
2029
2030 virtual ~CFRefReport() {}
2031
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002032 CFRefBug& getBugType() {
2033 return (CFRefBug&) RangedBugReport::getBugType();
2034 }
2035 const CFRefBug& getBugType() const {
2036 return (const CFRefBug&) RangedBugReport::getBugType();
2037 }
2038
2039 virtual void getRanges(BugReporter& BR, const SourceRange*& beg,
2040 const SourceRange*& end) {
2041
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002042 if (!getBugType().isLeak())
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002043 RangedBugReport::getRanges(BR, beg, end);
2044 else {
2045 beg = 0;
2046 end = 0;
2047 }
2048 }
2049
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002050 SymbolID getSymbol() const { return Sym; }
2051
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002052 virtual PathDiagnosticPiece* getEndPath(BugReporter& BR,
2053 ExplodedNode<ValueState>* N);
2054
Ted Kremenek072192b2008-04-30 23:47:44 +00002055 virtual std::pair<const char**,const char**> getExtraDescriptiveText();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002056
2057 virtual PathDiagnosticPiece* VisitNode(ExplodedNode<ValueState>* N,
2058 ExplodedNode<ValueState>* PrevN,
2059 ExplodedGraph<ValueState>& G,
2060 BugReporter& BR);
2061 };
2062
2063
2064} // end anonymous namespace
2065
2066void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
Ted Kremenek9f741612008-05-02 18:01:49 +00002067 if (EmitStandardWarnings) GRSimpleVals::RegisterChecks(Eng);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002068 Eng.Register(new UseAfterRelease(*this));
2069 Eng.Register(new BadRelease(*this));
2070 Eng.Register(new Leak(*this));
2071}
2072
Ted Kremenek072192b2008-04-30 23:47:44 +00002073
2074static const char* Msgs[] = {
2075 "Code is compiled in garbage collection only mode" // GC only
2076 " (the bug occurs with garbage collection enabled).",
2077
2078 "Code is compiled without garbage collection.", // No GC.
2079
2080 "Code is compiled for use with and without garbage collection (GC)."
2081 " The bug occurs with GC enabled.", // Hybrid, with GC.
2082
2083 "Code is compiled for use with and without garbage collection (GC)."
2084 " The bug occurs in non-GC mode." // Hyrbird, without GC/
2085};
2086
2087std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2088 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
2089
2090 switch (TF.getLangOptions().getGCMode()) {
2091 default:
2092 assert(false);
Ted Kremenek31593ac2008-05-01 04:02:04 +00002093
2094 case LangOptions::GCOnly:
2095 assert (TF.isGCEnabled());
2096 return std::make_pair(&Msgs[0], &Msgs[0]+1);
Ted Kremenek072192b2008-04-30 23:47:44 +00002097
2098 case LangOptions::NonGC:
2099 assert (!TF.isGCEnabled());
Ted Kremenek072192b2008-04-30 23:47:44 +00002100 return std::make_pair(&Msgs[1], &Msgs[1]+1);
2101
2102 case LangOptions::HybridGC:
2103 if (TF.isGCEnabled())
2104 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2105 else
2106 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2107 }
2108}
2109
Ted Kremenek8dd56462008-04-18 03:39:05 +00002110PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<ValueState>* N,
2111 ExplodedNode<ValueState>* PrevN,
2112 ExplodedGraph<ValueState>& G,
2113 BugReporter& BR) {
2114
2115 // Check if the type state has changed.
2116
2117 ValueState* PrevSt = PrevN->getState();
2118 ValueState* CurrSt = N->getState();
2119
2120 CFRefCount::RefBindings PrevB = CFRefCount::GetRefBindings(*PrevSt);
2121 CFRefCount::RefBindings CurrB = CFRefCount::GetRefBindings(*CurrSt);
2122
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002123 const RefVal* PrevT = PrevB.lookup(Sym);
2124 const RefVal* CurrT = CurrB.lookup(Sym);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002125
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002126 if (!CurrT)
2127 return NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002128
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002129 const char* Msg = NULL;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002130 const RefVal& CurrV = *CurrB.lookup(Sym);
Ted Kremenekce48e002008-05-05 17:53:17 +00002131
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002132 if (!PrevT) {
2133
Ted Kremenekce48e002008-05-05 17:53:17 +00002134 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2135
2136 if (CurrV.isOwned()) {
2137
2138 if (isa<CallExpr>(S))
2139 Msg = "Function call returns an object with a +1 retain count"
2140 " (owning reference).";
2141 else {
2142 assert (isa<ObjCMessageExpr>(S));
2143 Msg = "Method returns an object with a +1 retain count"
2144 " (owning reference).";
2145 }
2146 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002147 else {
2148 assert (CurrV.isNotOwned());
Ted Kremenekce48e002008-05-05 17:53:17 +00002149
2150 if (isa<CallExpr>(S))
2151 Msg = "Function call returns an object with a +0 retain count"
2152 " (non-owning reference).";
2153 else {
2154 assert (isa<ObjCMessageExpr>(S));
2155 Msg = "Method returns an object with a +0 retain count"
2156 " (non-owning reference).";
2157 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002158 }
Ted Kremenekce48e002008-05-05 17:53:17 +00002159
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002160 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2161 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2162
2163 if (Expr* Exp = dyn_cast<Expr>(S))
2164 P->addRange(Exp->getSourceRange());
2165
2166 return P;
2167 }
2168
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002169 // Determine if the typestate has changed.
2170 RefVal PrevV = *PrevB.lookup(Sym);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002171
2172 if (PrevV == CurrV)
2173 return NULL;
2174
2175 // The typestate has changed.
2176
2177 std::ostringstream os;
2178
2179 switch (CurrV.getKind()) {
2180 case RefVal::Owned:
2181 case RefVal::NotOwned:
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002182
2183 if (PrevV.getCount() == CurrV.getCount())
2184 return 0;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002185
2186 if (PrevV.getCount() > CurrV.getCount())
2187 os << "Reference count decremented.";
2188 else
2189 os << "Reference count incremented.";
2190
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002191 if (unsigned Count = CurrV.getCount()) {
Ted Kremenekce48e002008-05-05 17:53:17 +00002192
2193 os << " Object has +" << Count;
Ted Kremenek79c140b2008-04-18 05:32:44 +00002194
Ted Kremenekce48e002008-05-05 17:53:17 +00002195 if (Count > 1)
2196 os << " retain counts.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002197 else
Ted Kremenekce48e002008-05-05 17:53:17 +00002198 os << " retain count.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002199 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002200
2201 Msg = os.str().c_str();
2202
2203 break;
2204
2205 case RefVal::Released:
2206 Msg = "Object released.";
2207 break;
2208
2209 case RefVal::ReturnedOwned:
Ted Kremenekce48e002008-05-05 17:53:17 +00002210 Msg = "Object returned to caller as owning reference (single retain count"
2211 " transferred to caller).";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002212 break;
2213
2214 case RefVal::ReturnedNotOwned:
Ted Kremenekce48e002008-05-05 17:53:17 +00002215 Msg = "Object returned to caller with a +0 (non-owning) retain count.";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002216 break;
2217
2218 default:
2219 return NULL;
2220 }
2221
2222 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2223 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2224 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2225
2226 // Add the range by scanning the children of the statement for any bindings
2227 // to Sym.
2228
Ted Kremenekc0959972008-07-02 21:24:01 +00002229 ValueStateManager& VSM = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002230
2231 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
2232 if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) {
2233 RVal X = VSM.GetRVal(CurrSt, Exp);
2234
2235 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&X))
2236 if (SV->getSymbol() == Sym) {
2237 P->addRange(Exp->getSourceRange()); break;
2238 }
2239 }
2240
2241 return P;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002242}
2243
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002244static std::pair<ExplodedNode<ValueState>*,VarDecl*>
2245GetAllocationSite(ExplodedNode<ValueState>* N, SymbolID Sym) {
2246
2247 typedef CFRefCount::RefBindings RefBindings;
2248 ExplodedNode<ValueState>* Last = N;
2249
2250 // Find the first node that referred to the tracked symbol. We also
2251 // try and find the first VarDecl the value was stored to.
2252
2253 VarDecl* FirstDecl = 0;
2254
2255 while (N) {
2256 ValueState* St = N->getState();
2257 RefBindings B = RefBindings((RefBindings::TreeTy*) St->CheckerState);
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002258
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002259 if (!B.lookup(Sym))
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002260 break;
2261
2262 VarDecl* VD = 0;
2263
2264 // Determine if there is an LVal binding to the symbol.
2265 for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I) {
2266 if (!isa<lval::SymbolVal>(I->second) // Is the value a symbol?
2267 || cast<lval::SymbolVal>(I->second).getSymbol() != Sym)
2268 continue;
2269
2270 if (VD) { // Multiple decls map to this symbol.
2271 VD = 0;
2272 break;
2273 }
2274
2275 VD = I->first;
2276 }
2277
2278 if (VD) FirstDecl = VD;
2279
2280 Last = N;
2281 N = N->pred_empty() ? NULL : *(N->pred_begin());
2282 }
2283
2284 return std::make_pair(Last, FirstDecl);
2285}
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002286
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002287PathDiagnosticPiece* CFRefReport::getEndPath(BugReporter& BR,
Ted Kremeneke28565b2008-05-05 18:50:19 +00002288 ExplodedNode<ValueState>* EndN) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002289
2290 // Tell the BugReporter to report cases when the tracked symbol is
2291 // assigned to different variables, etc.
Ted Kremenekc0959972008-07-02 21:24:01 +00002292 cast<GRBugReporter>(BR).addNotableSymbol(Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002293
2294 if (!getBugType().isLeak())
Ted Kremeneke28565b2008-05-05 18:50:19 +00002295 return RangedBugReport::getEndPath(BR, EndN);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002296
Ted Kremenekce48e002008-05-05 17:53:17 +00002297 typedef CFRefCount::RefBindings RefBindings;
2298
2299 // Get the retain count.
Ted Kremenekce48e002008-05-05 17:53:17 +00002300
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002301 unsigned long RetCount =
2302 CFRefCount::GetRefBindings(*EndN->getState()).lookup(Sym)->getCount();
2303
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002304 // We are a leak. Walk up the graph to get to the first node where the
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002305 // symbol appeared, and also get the first VarDecl that tracked object
2306 // is stored to.
2307
2308 ExplodedNode<ValueState>* AllocNode = 0;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002309 VarDecl* FirstDecl = 0;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002310 llvm::tie(AllocNode, FirstDecl) = GetAllocationSite(EndN, Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002311
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002312 // Get the allocate site.
2313 assert (AllocNode);
2314 Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002315
Ted Kremeneke28565b2008-05-05 18:50:19 +00002316 SourceManager& SMgr = BR.getContext().getSourceManager();
2317 unsigned AllocLine = SMgr.getLogicalLineNumber(FirstStmt->getLocStart());
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002318
Ted Kremeneke28565b2008-05-05 18:50:19 +00002319 // Get the leak site. We may have multiple ExplodedNodes (one with the
2320 // leak) that occur on the same line number; if the node with the leak
2321 // has any immediate predecessor nodes with the same line number, find
2322 // any transitive-successors that have a different statement and use that
2323 // line number instead. This avoids emiting a diagnostic like:
2324 //
2325 // // 'y' is leaked.
2326 // int x = foo(y);
2327 //
2328 // instead we want:
2329 //
2330 // int x = foo(y);
2331 // // 'y' is leaked.
2332
2333 Stmt* S = getStmt(BR); // This is the statement where the leak occured.
2334 assert (S);
2335 unsigned EndLine = SMgr.getLogicalLineNumber(S->getLocStart());
2336
2337 // Look in the *trimmed* graph at the immediate predecessor of EndN. Does
2338 // it occur on the same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002339
2340 PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above;
Ted Kremeneke28565b2008-05-05 18:50:19 +00002341
2342 assert (!EndN->pred_empty()); // Not possible to have 0 predecessors.
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002343 ExplodedNode<ValueState> *Pred = *(EndN->pred_begin());
2344 ProgramPoint PredPos = Pred->getLocation();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002345
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002346 if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) {
Ted Kremeneke28565b2008-05-05 18:50:19 +00002347
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002348 Stmt* SPred = PredPS->getStmt();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002349
2350 // Predecessor at same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002351 if (SMgr.getLogicalLineNumber(SPred->getLocStart()) != EndLine) {
2352 Hint = PathDiagnosticPiece::Below;
2353 S = SPred;
2354 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002355 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002356
2357 // Generate the diagnostic.
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002358 FullSourceLoc L( S->getLocStart(), SMgr);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002359 std::ostringstream os;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002360
Ted Kremeneke28565b2008-05-05 18:50:19 +00002361 os << "Object allocated on line " << AllocLine;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002362
2363 if (FirstDecl)
2364 os << " and stored into '" << FirstDecl->getName() << '\'';
2365
Ted Kremenekce48e002008-05-05 17:53:17 +00002366 os << " is no longer referenced after this point and has a retain count of +"
2367 << RetCount << " (object leaked).";
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002368
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002369 return new PathDiagnosticPiece(L, os.str(), Hint);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002370}
2371
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002372void UseAfterRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002373
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002374 for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
2375 E = TF.use_after_end(); I != E; ++I) {
2376
Ted Kremenek8dd56462008-04-18 03:39:05 +00002377 CFRefReport report(*this, I->first, I->second.second);
2378 report.addRange(I->second.first->getSourceRange());
Ted Kremenek75840e12008-04-18 01:56:37 +00002379 BR.EmitWarning(report);
Ted Kremenekfa34b332008-04-09 01:10:13 +00002380 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002381}
2382
2383void BadRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002384
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002385 for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
2386 E = TF.bad_release_end(); I != E; ++I) {
2387
Ted Kremenek8dd56462008-04-18 03:39:05 +00002388 CFRefReport report(*this, I->first, I->second.second);
2389 report.addRange(I->second.first->getSourceRange());
2390 BR.EmitWarning(report);
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002391 }
2392}
Ted Kremenekfa34b332008-04-09 01:10:13 +00002393
Ted Kremenek989d5192008-04-17 23:43:50 +00002394void Leak::EmitWarnings(BugReporter& BR) {
2395
2396 for (CFRefCount::leaks_iterator I = TF.leaks_begin(),
2397 E = TF.leaks_end(); I != E; ++I) {
2398
Ted Kremenek8dd56462008-04-18 03:39:05 +00002399 std::vector<SymbolID>& SymV = *(I->second);
2400 unsigned n = SymV.size();
2401
2402 for (unsigned i = 0; i < n; ++i) {
2403 CFRefReport report(*this, I->first, SymV[i]);
2404 BR.EmitWarning(report);
2405 }
Ted Kremenek989d5192008-04-17 23:43:50 +00002406 }
2407}
2408
Ted Kremenekcb612922008-04-18 19:23:43 +00002409void Leak::GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes) {
2410 for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end();
2411 I!=E; ++I)
2412 Nodes.push_back(I->first);
2413}
2414
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002415bool Leak::isCached(BugReport& R) {
2416
2417 // Most bug reports are cached at the location where they occured.
2418 // With leaks, we want to unique them by the location where they were
2419 // allocated, and only report only a single path.
2420
2421 SymbolID Sym = static_cast<CFRefReport&>(R).getSymbol();
2422
2423 ExplodedNode<ValueState>* AllocNode =
2424 GetAllocationSite(R.getEndNode(), Sym).first;
2425
2426 if (!AllocNode)
2427 return false;
2428
2429 return BugTypeCacheLocation::isCached(AllocNode->getLocation());
2430}
2431
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002432//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +00002433// Transfer function creation for external clients.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002434//===----------------------------------------------------------------------===//
2435
Ted Kremenek072192b2008-04-30 23:47:44 +00002436GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
Ted Kremenek9f741612008-05-02 18:01:49 +00002437 bool StandardWarnings,
Ted Kremenek072192b2008-04-30 23:47:44 +00002438 const LangOptions& lopts) {
Ted Kremenek9f741612008-05-02 18:01:49 +00002439 return new CFRefCount(Ctx, GCEnabled, StandardWarnings, lopts);
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +00002440}