blob: 25841cd8db57e87abc7cc27dcc4c649d8fa96824 [file] [log] [blame]
Chris Lattnerbda0b622008-03-15 23:59:48 +00001// CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
Ted Kremenek2fff37e2008-03-06 00:08:09 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif843e9342008-03-06 10:40:09 +000010// This file defines the methods for CFRefCount, which implements
Ted Kremenek2fff37e2008-03-06 00:08:09 +000011// a reference count checker for Core Foundation (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000015#include "GRSimpleVals.h"
Ted Kremenek072192b2008-04-30 23:47:44 +000016#include "clang/Basic/LangOptions.h"
Ted Kremenekc9fa2f72008-05-01 23:13:35 +000017#include "clang/Basic/SourceManager.h"
Ted Kremenek4adc81e2008-08-13 04:27:00 +000018#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenekb9d17f92008-08-17 03:20:02 +000019#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Ted Kremenek4dc41cc2008-03-31 18:26:32 +000020#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek2fff37e2008-03-06 00:08:09 +000021#include "clang/Analysis/LocalCheckers.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000022#include "clang/Analysis/PathDiagnostic.h"
23#include "clang/Analysis/PathSensitive/BugReporter.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000024#include "clang/AST/DeclObjC.h"
Ted Kremenek6b3a0f72008-03-11 06:39:11 +000025#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/FoldingSet.h"
27#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek900a2d72008-05-07 18:36:45 +000028#include "llvm/ADT/StringExtras.h"
Ted Kremenekfa34b332008-04-09 01:10:13 +000029#include "llvm/Support/Compiler.h"
Ted Kremenek6ed9afc2008-05-16 18:33:44 +000030#include "llvm/ADT/STLExtras.h"
Ted Kremenekf3948042008-03-11 19:44:10 +000031#include <ostream>
Ted Kremenek2cf943a2008-04-18 04:55:01 +000032#include <sstream>
Ted Kremenek98530452008-08-12 20:41:56 +000033#include <stdarg.h>
Ted Kremenek2fff37e2008-03-06 00:08:09 +000034
35using namespace clang;
Ted Kremenek900a2d72008-05-07 18:36:45 +000036using llvm::CStrInCStrNoCase;
Ted Kremenek2fff37e2008-03-06 00:08:09 +000037
Ted Kremenek05cbe1a2008-04-09 23:49:11 +000038//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +000039// Selector creation functions.
Ted Kremenek4fd88972008-04-17 18:12:53 +000040//===----------------------------------------------------------------------===//
41
Ted Kremenekb83e02e2008-05-01 18:31:44 +000042static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
Ted Kremenek4fd88972008-04-17 18:12:53 +000043 IdentifierInfo* II = &Ctx.Idents.get(name);
44 return Ctx.Selectors.getSelector(0, &II);
45}
46
Ted Kremenek9c32d082008-05-06 00:30:21 +000047static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
48 IdentifierInfo* II = &Ctx.Idents.get(name);
49 return Ctx.Selectors.getSelector(1, &II);
50}
51
Ted Kremenek553cf182008-06-25 21:21:56 +000052//===----------------------------------------------------------------------===//
53// Type querying functions.
54//===----------------------------------------------------------------------===//
55
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000056static bool isCFRefType(QualType T) {
57
58 if (!T->isPointerType())
59 return false;
60
Ted Kremenek553cf182008-06-25 21:21:56 +000061 // Check the typedef for the name "CF" and the substring "Ref".
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +000062 TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
63
64 if (!TD)
65 return false;
66
67 const char* TDName = TD->getDecl()->getIdentifier()->getName();
68 assert (TDName);
69
70 if (TDName[0] != 'C' || TDName[1] != 'F')
71 return false;
72
73 if (strstr(TDName, "Ref") == 0)
74 return false;
75
76 return true;
77}
78
Ted Kremenek37d785b2008-07-15 16:50:12 +000079static bool isCGRefType(QualType T) {
80
81 if (!T->isPointerType())
82 return false;
83
84 // Check the typedef for the name "CG" and the substring "Ref".
85 TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
86
87 if (!TD)
88 return false;
89
90 const char* TDName = TD->getDecl()->getIdentifier()->getName();
91 assert (TDName);
92
93 if (TDName[0] != 'C' || TDName[1] != 'G')
94 return false;
95
96 if (strstr(TDName, "Ref") == 0)
97 return false;
98
99 return true;
100}
101
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000102static bool isNSType(QualType T) {
103
104 if (!T->isPointerType())
105 return false;
106
107 ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
108
109 if (!OT)
110 return false;
111
112 const char* ClsName = OT->getDecl()->getIdentifier()->getName();
113 assert (ClsName);
114
115 if (ClsName[0] != 'N' || ClsName[1] != 'S')
116 return false;
117
118 return true;
119}
120
Ted Kremenek4fd88972008-04-17 18:12:53 +0000121//===----------------------------------------------------------------------===//
Ted Kremenek553cf182008-06-25 21:21:56 +0000122// Primitives used for constructing summaries for function/method calls.
Ted Kremenek05cbe1a2008-04-09 23:49:11 +0000123//===----------------------------------------------------------------------===//
124
Ted Kremenek553cf182008-06-25 21:21:56 +0000125namespace {
126/// ArgEffect is used to summarize a function/method call's effect on a
127/// particular argument.
Ted Kremenek070a8252008-07-09 18:11:16 +0000128enum ArgEffect { IncRef, DecRef, DoNothing, DoNothingByRef,
129 StopTracking, MayEscape, SelfOwn, Autorelease };
Ted Kremenek553cf182008-06-25 21:21:56 +0000130
131/// ArgEffects summarizes the effects of a function/method call on all of
132/// its arguments.
133typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000134}
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000135
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000136namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000137template <> struct FoldingSetTrait<ArgEffects> {
138 static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
139 for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) {
140 ID.AddInteger(I->first);
141 ID.AddInteger((unsigned) I->second);
142 }
143 }
144};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000145} // end llvm namespace
146
147namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000148
149/// RetEffect is used to summarize a function/method call's behavior with
150/// respect to its return value.
151class VISIBILITY_HIDDEN RetEffect {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000152public:
Ted Kremeneka7344702008-06-23 18:02:52 +0000153 enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
154 NotOwnedSymbol, ReceiverAlias };
Ted Kremenek553cf182008-06-25 21:21:56 +0000155
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000156private:
157 unsigned Data;
Ted Kremenek553cf182008-06-25 21:21:56 +0000158 RetEffect(Kind k, unsigned D = 0) { Data = (D << 3) | (unsigned) k; }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000159
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000160public:
Ted Kremenek553cf182008-06-25 21:21:56 +0000161
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000162 Kind getKind() const { return (Kind) (Data & 0x7); }
Ted Kremenek553cf182008-06-25 21:21:56 +0000163
164 unsigned getIndex() const {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000165 assert(getKind() == Alias);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000166 return Data >> 3;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000167 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000168
Ted Kremenek553cf182008-06-25 21:21:56 +0000169 static RetEffect MakeAlias(unsigned Idx) {
170 return RetEffect(Alias, Idx);
171 }
172 static RetEffect MakeReceiverAlias() {
173 return RetEffect(ReceiverAlias);
174 }
Ted Kremeneka7344702008-06-23 18:02:52 +0000175 static RetEffect MakeOwned(bool isAllocated = false) {
Ted Kremenek553cf182008-06-25 21:21:56 +0000176 return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol);
177 }
178 static RetEffect MakeNotOwned() {
179 return RetEffect(NotOwnedSymbol);
180 }
181 static RetEffect MakeNoRet() {
182 return RetEffect(NoRet);
Ted Kremeneka7344702008-06-23 18:02:52 +0000183 }
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000184
Ted Kremenek553cf182008-06-25 21:21:56 +0000185 operator Kind() const {
186 return getKind();
187 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000188
Ted Kremenek553cf182008-06-25 21:21:56 +0000189 void Profile(llvm::FoldingSetNodeID& ID) const {
190 ID.AddInteger(Data);
191 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000192};
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000193
Ted Kremenek553cf182008-06-25 21:21:56 +0000194
195class VISIBILITY_HIDDEN RetainSummary : public llvm::FoldingSetNode {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000196 /// Args - an ordered vector of (index, ArgEffect) pairs, where index
197 /// specifies the argument (starting from 0). This can be sparsely
198 /// populated; arguments with no entry in Args use 'DefaultArgEffect'.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000199 ArgEffects* Args;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000200
201 /// DefaultArgEffect - The default ArgEffect to apply to arguments that
202 /// do not have an entry in Args.
203 ArgEffect DefaultArgEffect;
204
Ted Kremenek553cf182008-06-25 21:21:56 +0000205 /// Receiver - If this summary applies to an Objective-C message expression,
206 /// this is the effect applied to the state of the receiver.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000207 ArgEffect Receiver;
Ted Kremenek553cf182008-06-25 21:21:56 +0000208
209 /// Ret - The effect on the return value. Used to indicate if the
210 /// function/method call returns a new tracked symbol, returns an
211 /// alias of one of the arguments in the call, and so on.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000212 RetEffect Ret;
Ted Kremenek553cf182008-06-25 21:21:56 +0000213
Ted Kremenek70a733e2008-07-18 17:24:20 +0000214 /// EndPath - Indicates that execution of this method/function should
215 /// terminate the simulation of a path.
216 bool EndPath;
217
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000218public:
219
Ted Kremenek1bffd742008-05-06 15:44:25 +0000220 RetainSummary(ArgEffects* A, RetEffect R, ArgEffect defaultEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000221 ArgEffect ReceiverEff, bool endpath = false)
222 : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
223 EndPath(endpath) {}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000224
Ted Kremenek553cf182008-06-25 21:21:56 +0000225 /// getArg - Return the argument effect on the argument specified by
226 /// idx (starting from 0).
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000227 ArgEffect getArg(unsigned idx) const {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000228
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000229 if (!Args)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000230 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000231
232 // If Args is present, it is likely to contain only 1 element.
233 // Just do a linear search. Do it from the back because functions with
234 // large numbers of arguments will be tail heavy with respect to which
Ted Kremenek553cf182008-06-25 21:21:56 +0000235 // argument they actually modify with respect to the reference count.
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000236 for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend();
237 I!=E; ++I) {
238
239 if (idx > I->first)
Ted Kremenek1bffd742008-05-06 15:44:25 +0000240 return DefaultArgEffect;
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000241
242 if (idx == I->first)
243 return I->second;
244 }
245
Ted Kremenek1bffd742008-05-06 15:44:25 +0000246 return DefaultArgEffect;
Ted Kremenek1ac08d62008-03-11 17:48:22 +0000247 }
248
Ted Kremenek553cf182008-06-25 21:21:56 +0000249 /// getRetEffect - Returns the effect on the return value of the call.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000250 RetEffect getRetEffect() const {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000251 return Ret;
252 }
253
Ted Kremenek70a733e2008-07-18 17:24:20 +0000254 /// isEndPath - Returns true if executing the given method/function should
255 /// terminate the path.
256 bool isEndPath() const { return EndPath; }
257
Ted Kremenek553cf182008-06-25 21:21:56 +0000258 /// getReceiverEffect - Returns the effect on the receiver of the call.
259 /// This is only meaningful if the summary applies to an ObjCMessageExpr*.
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000260 ArgEffect getReceiverEffect() const {
261 return Receiver;
262 }
263
Ted Kremenek55499762008-06-17 02:43:46 +0000264 typedef ArgEffects::const_iterator ExprIterator;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000265
Ted Kremenek55499762008-06-17 02:43:46 +0000266 ExprIterator begin_args() const { return Args->begin(); }
267 ExprIterator end_args() const { return Args->end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000268
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000269 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000270 RetEffect RetEff, ArgEffect DefaultEff,
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000271 ArgEffect ReceiverEff, bool EndPath) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000272 ID.AddPointer(A);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000273 ID.Add(RetEff);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000274 ID.AddInteger((unsigned) DefaultEff);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000275 ID.AddInteger((unsigned) ReceiverEff);
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000276 ID.AddInteger((unsigned) EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000277 }
278
279 void Profile(llvm::FoldingSetNodeID& ID) const {
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000280 Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000281 }
282};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000283} // end anonymous namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000284
Ted Kremenek553cf182008-06-25 21:21:56 +0000285//===----------------------------------------------------------------------===//
286// Data structures for constructing summaries.
287//===----------------------------------------------------------------------===//
Ted Kremenek53301ba2008-06-24 03:49:48 +0000288
Ted Kremenek553cf182008-06-25 21:21:56 +0000289namespace {
290class VISIBILITY_HIDDEN ObjCSummaryKey {
291 IdentifierInfo* II;
292 Selector S;
293public:
294 ObjCSummaryKey(IdentifierInfo* ii, Selector s)
295 : II(ii), S(s) {}
296
297 ObjCSummaryKey(ObjCInterfaceDecl* d, Selector s)
298 : II(d ? d->getIdentifier() : 0), S(s) {}
299
300 ObjCSummaryKey(Selector s)
301 : II(0), S(s) {}
302
303 IdentifierInfo* getIdentifier() const { return II; }
304 Selector getSelector() const { return S; }
305};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000306}
307
308namespace llvm {
Ted Kremenek553cf182008-06-25 21:21:56 +0000309template <> struct DenseMapInfo<ObjCSummaryKey> {
310 static inline ObjCSummaryKey getEmptyKey() {
311 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
312 DenseMapInfo<Selector>::getEmptyKey());
313 }
Ted Kremenek4f22a782008-06-23 23:30:29 +0000314
Ted Kremenek553cf182008-06-25 21:21:56 +0000315 static inline ObjCSummaryKey getTombstoneKey() {
316 return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
317 DenseMapInfo<Selector>::getTombstoneKey());
318 }
319
320 static unsigned getHashValue(const ObjCSummaryKey &V) {
321 return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
322 & 0x88888888)
323 | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
324 & 0x55555555);
325 }
326
327 static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
328 return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
329 RHS.getIdentifier()) &&
330 DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
331 RHS.getSelector());
332 }
333
334 static bool isPod() {
335 return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
336 DenseMapInfo<Selector>::isPod();
337 }
338};
Ted Kremenek4f22a782008-06-23 23:30:29 +0000339} // end llvm namespace
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000340
Ted Kremenek4f22a782008-06-23 23:30:29 +0000341namespace {
Ted Kremenek553cf182008-06-25 21:21:56 +0000342class VISIBILITY_HIDDEN ObjCSummaryCache {
343 typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
344 MapTy M;
345public:
346 ObjCSummaryCache() {}
347
348 typedef MapTy::iterator iterator;
349
350 iterator find(ObjCInterfaceDecl* D, Selector S) {
351
352 // Do a lookup with the (D,S) pair. If we find a match return
353 // the iterator.
354 ObjCSummaryKey K(D, S);
355 MapTy::iterator I = M.find(K);
356
357 if (I != M.end() || !D)
358 return I;
359
360 // Walk the super chain. If we find a hit with a parent, we'll end
361 // up returning that summary. We actually allow that key (null,S), as
362 // we cache summaries for the null ObjCInterfaceDecl* to allow us to
363 // generate initial summaries without having to worry about NSObject
364 // being declared.
365 // FIXME: We may change this at some point.
366 for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
367 if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
368 break;
369
370 if (!C)
371 return I;
372 }
373
374 // Cache the summary with original key to make the next lookup faster
375 // and return the iterator.
376 M[K] = I->second;
377 return I;
378 }
379
Ted Kremenek98530452008-08-12 20:41:56 +0000380
Ted Kremenek553cf182008-06-25 21:21:56 +0000381 iterator find(Expr* Receiver, Selector S) {
382 return find(getReceiverDecl(Receiver), S);
383 }
384
385 iterator find(IdentifierInfo* II, Selector S) {
386 // FIXME: Class method lookup. Right now we dont' have a good way
387 // of going between IdentifierInfo* and the class hierarchy.
388 iterator I = M.find(ObjCSummaryKey(II, S));
389 return I == M.end() ? M.find(ObjCSummaryKey(S)) : I;
390 }
391
392 ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
393
394 const PointerType* PT = E->getType()->getAsPointerType();
395 if (!PT) return 0;
396
397 ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType());
398 if (!OI) return 0;
399
400 return OI ? OI->getDecl() : 0;
401 }
402
403 iterator end() { return M.end(); }
404
405 RetainSummary*& operator[](ObjCMessageExpr* ME) {
406
407 Selector S = ME->getSelector();
408
409 if (Expr* Receiver = ME->getReceiver()) {
410 ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
411 return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
412 }
413
414 return M[ObjCSummaryKey(ME->getClassName(), S)];
415 }
416
417 RetainSummary*& operator[](ObjCSummaryKey K) {
418 return M[K];
419 }
420
421 RetainSummary*& operator[](Selector S) {
422 return M[ ObjCSummaryKey(S) ];
423 }
424};
425} // end anonymous namespace
426
427//===----------------------------------------------------------------------===//
428// Data structures for managing collections of summaries.
429//===----------------------------------------------------------------------===//
430
431namespace {
432class VISIBILITY_HIDDEN RetainSummaryManager {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000433
434 //==-----------------------------------------------------------------==//
435 // Typedefs.
436 //==-----------------------------------------------------------------==//
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000437
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000438 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> >
439 ArgEffectsSetTy;
440
441 typedef llvm::FoldingSet<RetainSummary>
442 SummarySetTy;
443
444 typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
445 FuncSummariesTy;
446
Ted Kremenek4f22a782008-06-23 23:30:29 +0000447 typedef ObjCSummaryCache ObjCMethodSummariesTy;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000448
449 //==-----------------------------------------------------------------==//
450 // Data.
451 //==-----------------------------------------------------------------==//
452
Ted Kremenek553cf182008-06-25 21:21:56 +0000453 /// Ctx - The ASTContext object for the analyzed ASTs.
Ted Kremenek377e2302008-04-29 05:33:51 +0000454 ASTContext& Ctx;
Ted Kremenek179064e2008-07-01 17:21:27 +0000455
Ted Kremenek070a8252008-07-09 18:11:16 +0000456 /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
457 /// "CFDictionaryCreate".
458 IdentifierInfo* CFDictionaryCreateII;
459
Ted Kremenek553cf182008-06-25 21:21:56 +0000460 /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
Ted Kremenek377e2302008-04-29 05:33:51 +0000461 const bool GCEnabled;
462
Ted Kremenek553cf182008-06-25 21:21:56 +0000463 /// SummarySet - A FoldingSet of uniqued summaries.
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +0000464 SummarySetTy SummarySet;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000465
Ted Kremenek553cf182008-06-25 21:21:56 +0000466 /// FuncSummaries - A map from FunctionDecls to summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000467 FuncSummariesTy FuncSummaries;
468
Ted Kremenek553cf182008-06-25 21:21:56 +0000469 /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
470 /// to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000471 ObjCMethodSummariesTy ObjCClassMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000472
Ted Kremenek553cf182008-06-25 21:21:56 +0000473 /// ObjCMethodSummaries - A map from selectors to summaries.
Ted Kremenek1f180c32008-06-23 22:21:20 +0000474 ObjCMethodSummariesTy ObjCMethodSummaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000475
Ted Kremenek553cf182008-06-25 21:21:56 +0000476 /// ArgEffectsSet - A FoldingSet of uniqued ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000477 ArgEffectsSetTy ArgEffectsSet;
478
Ted Kremenek553cf182008-06-25 21:21:56 +0000479 /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
480 /// and all other data used by the checker.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000481 llvm::BumpPtrAllocator BPAlloc;
482
Ted Kremenek553cf182008-06-25 21:21:56 +0000483 /// ScratchArgs - A holding buffer for construct ArgEffects.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000484 ArgEffects ScratchArgs;
485
Ted Kremenek432af592008-05-06 18:11:36 +0000486 RetainSummary* StopSummary;
487
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000488 //==-----------------------------------------------------------------==//
489 // Methods.
490 //==-----------------------------------------------------------------==//
491
Ted Kremenek553cf182008-06-25 21:21:56 +0000492 /// getArgEffects - Returns a persistent ArgEffects object based on the
493 /// data in ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000494 ArgEffects* getArgEffects();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000495
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000496 enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000497 RetainSummary* getUnarySummary(FunctionDecl* FD, UnaryFuncKind func);
Ted Kremenek377e2302008-04-29 05:33:51 +0000498
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000499 RetainSummary* getNSSummary(FunctionDecl* FD, const char* FName);
500 RetainSummary* getCFSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000501 RetainSummary* getCGSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000502
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000503 RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
504 RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000505 RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000506
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000507 RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000508 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000509 ArgEffect DefaultEff = MayEscape,
510 bool isEndPath = false);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000511
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000512 RetainSummary* getPersistentSummary(RetEffect RE,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000513 ArgEffect ReceiverEff = DoNothing,
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000514 ArgEffect DefaultEff = MayEscape) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000515 return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
Ted Kremenek9c32d082008-05-06 00:30:21 +0000516 }
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000517
Ted Kremenek1bffd742008-05-06 15:44:25 +0000518 RetainSummary* getPersistentStopSummary() {
Ted Kremenek432af592008-05-06 18:11:36 +0000519 if (StopSummary)
520 return StopSummary;
521
522 StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
523 StopTracking, StopTracking);
524
525 return StopSummary;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000526 }
Ted Kremenekb3095252008-05-06 04:20:12 +0000527
Ted Kremenek553cf182008-06-25 21:21:56 +0000528 RetainSummary* getInitMethodSummary(ObjCMessageExpr* ME);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000529
Ted Kremenek1f180c32008-06-23 22:21:20 +0000530 void InitializeClassMethodSummaries();
531 void InitializeMethodSummaries();
Ted Kremenek70a733e2008-07-18 17:24:20 +0000532
533 void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
534 RetainSummary* Summ) {
535 ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
536 }
537
Ted Kremenek553cf182008-06-25 21:21:56 +0000538 void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
539 ObjCClassMethodSummaries[S] = Summ;
540 }
541
542 void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
543 ObjCMethodSummaries[S] = Summ;
544 }
545
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000546 void addInstMethSummary(const char* Cls, RetainSummary* Summ, va_list argp) {
Ted Kremenek70a733e2008-07-18 17:24:20 +0000547
Ted Kremenek9e476de2008-08-12 18:30:56 +0000548 IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
549 llvm::SmallVector<IdentifierInfo*, 10> II;
550
551 while (const char* s = va_arg(argp, const char*))
552 II.push_back(&Ctx.Idents.get(s));
553
554 Selector S = Ctx.Selectors.getSelector(II.size(), &II[0]);
Ted Kremenek70a733e2008-07-18 17:24:20 +0000555 ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
556 }
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000557
558 void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
559 va_list argp;
560 va_start(argp, Summ);
561 addInstMethSummary(Cls, Summ, argp);
562 va_end(argp);
563 }
Ted Kremenek9e476de2008-08-12 18:30:56 +0000564
565 void addPanicSummary(const char* Cls, ...) {
566 RetainSummary* Summ = getPersistentSummary(0, RetEffect::MakeNoRet(),
567 DoNothing, DoNothing, true);
568 va_list argp;
569 va_start (argp, Cls);
Ted Kremenekaf9dc272008-08-12 18:48:50 +0000570 addInstMethSummary(Cls, Summ, argp);
Ted Kremenek9e476de2008-08-12 18:30:56 +0000571 va_end(argp);
572 }
Ted Kremenek70a733e2008-07-18 17:24:20 +0000573
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000574public:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000575
576 RetainSummaryManager(ASTContext& ctx, bool gcenabled)
Ted Kremenek179064e2008-07-01 17:21:27 +0000577 : Ctx(ctx),
Ted Kremenek070a8252008-07-09 18:11:16 +0000578 CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
Ted Kremenek553cf182008-06-25 21:21:56 +0000579 GCEnabled(gcenabled), StopSummary(0) {
580
581 InitializeClassMethodSummaries();
582 InitializeMethodSummaries();
583 }
Ted Kremenek377e2302008-04-29 05:33:51 +0000584
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000585 ~RetainSummaryManager();
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000586
Ted Kremenekab592272008-06-24 03:56:45 +0000587 RetainSummary* getSummary(FunctionDecl* FD);
Ted Kremenek553cf182008-06-25 21:21:56 +0000588 RetainSummary* getMethodSummary(ObjCMessageExpr* ME, ObjCInterfaceDecl* ID);
Ted Kremenek1f180c32008-06-23 22:21:20 +0000589 RetainSummary* getClassMethodSummary(IdentifierInfo* ClsName, Selector S);
Ted Kremenekb3095252008-05-06 04:20:12 +0000590
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000591 bool isGCEnabled() const { return GCEnabled; }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000592};
593
594} // end anonymous namespace
595
596//===----------------------------------------------------------------------===//
597// Implementation of checker data structures.
598//===----------------------------------------------------------------------===//
599
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000600RetainSummaryManager::~RetainSummaryManager() {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000601
602 // FIXME: The ArgEffects could eventually be allocated from BPAlloc,
603 // mitigating the need to do explicit cleanup of the
604 // Argument-Effect summaries.
605
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000606 for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(),
607 E = ArgEffectsSet.end(); I!=E; ++I)
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000608 I->getValue().~ArgEffects();
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000609}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000610
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000611ArgEffects* RetainSummaryManager::getArgEffects() {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000612
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000613 if (ScratchArgs.empty())
614 return NULL;
615
616 // Compute a profile for a non-empty ScratchArgs.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000617 llvm::FoldingSetNodeID profile;
618 profile.Add(ScratchArgs);
619 void* InsertPos;
620
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000621 // Look up the uniqued copy, or create a new one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000622 llvm::FoldingSetNodeWrapper<ArgEffects>* E =
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000623 ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000624
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000625 if (E) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000626 ScratchArgs.clear();
627 return &E->getValue();
628 }
629
630 E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
Ted Kremenek553cf182008-06-25 21:21:56 +0000631 BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000632
633 new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000634 ArgEffectsSet.InsertNode(E, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000635
636 ScratchArgs.clear();
637 return &E->getValue();
638}
639
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000640RetainSummary*
641RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
Ted Kremenek1bffd742008-05-06 15:44:25 +0000642 ArgEffect ReceiverEff,
Ted Kremenek70a733e2008-07-18 17:24:20 +0000643 ArgEffect DefaultEff,
644 bool isEndPath) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000645
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000646 // Generate a profile for the summary.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000647 llvm::FoldingSetNodeID profile;
Ted Kremenek2d1086c2008-07-18 17:39:56 +0000648 RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff,
649 isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000650
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000651 // Look up the uniqued summary, or create one if it doesn't exist.
652 void* InsertPos;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000653 RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000654
655 if (Summ)
656 return Summ;
657
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000658 // Create the summary and return it.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000659 Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
Ted Kremenek70a733e2008-07-18 17:24:20 +0000660 new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000661 SummarySet.InsertNode(Summ, InsertPos);
662
663 return Summ;
664}
665
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000666//===----------------------------------------------------------------------===//
667// Summary creation for functions (largely uses of Core Foundation).
668//===----------------------------------------------------------------------===//
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000669
Ted Kremenekab592272008-06-24 03:56:45 +0000670RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000671
672 SourceLocation Loc = FD->getLocation();
673
674 if (!Loc.isFileID())
675 return NULL;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000676
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000677 // Look up a summary in our cache of FunctionDecls -> Summaries.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000678 FuncSummariesTy::iterator I = FuncSummaries.find(FD);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000679
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000680 if (I != FuncSummaries.end())
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000681 return I->second;
682
683 // No summary. Generate one.
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000684 const char* FName = FD->getIdentifier()->getName();
685
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000686 RetainSummary *S = 0;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000687
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000688 FunctionType* FT = dyn_cast<FunctionType>(FD->getType());
Ted Kremenek37d785b2008-07-15 16:50:12 +0000689
690 do {
691 if (FT) {
692
693 QualType T = FT->getResultType();
694
695 if (isCFRefType(T)) {
696 S = getCFSummary(FD, FName);
697 break;
698 }
699
700 if (isCGRefType(T)) {
701 S = getCGSummary(FD, FName );
702 break;
703 }
704 }
705
706 if (FName[0] == 'C' && FName[1] == 'F')
707 S = getCFSummary(FD, FName);
708 else if (FName[0] == 'N' && FName[1] == 'S')
709 S = getNSSummary(FD, FName);
710 }
711 while (0);
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000712
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000713 FuncSummaries[FD] = S;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000714 return S;
Ted Kremenek2fff37e2008-03-06 00:08:09 +0000715}
716
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000717RetainSummary* RetainSummaryManager::getNSSummary(FunctionDecl* FD,
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000718 const char* FName) {
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000719 FName += 2;
720
721 if (strcmp(FName, "MakeCollectable") == 0)
722 return getUnarySummary(FD, cfmakecollectable);
723
724 return 0;
725}
Ted Kremenek37d785b2008-07-15 16:50:12 +0000726
727static bool isRetain(FunctionDecl* FD, const char* FName) {
Ted Kremenek10161bf2008-07-15 17:43:41 +0000728 const char* loc = strstr(FName, "Retain");
729 return loc && loc[sizeof("Retain")-1] == '\0';
Ted Kremenek37d785b2008-07-15 16:50:12 +0000730}
731
732static bool isRelease(FunctionDecl* FD, const char* FName) {
Ted Kremenek10161bf2008-07-15 17:43:41 +0000733 const char* loc = strstr(FName, "Release");
734 return loc && loc[sizeof("Release")-1] == '\0';
Ted Kremenek37d785b2008-07-15 16:50:12 +0000735}
736
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000737RetainSummary* RetainSummaryManager::getCFSummary(FunctionDecl* FD,
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000738 const char* FName) {
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000739
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000740 if (FName[0] == 'C' && FName[1] == 'F')
741 FName += 2;
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000742
Ted Kremenek37d785b2008-07-15 16:50:12 +0000743 if (isRetain(FD, FName))
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000744 return getUnarySummary(FD, cfretain);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000745
Ted Kremenek37d785b2008-07-15 16:50:12 +0000746 if (isRelease(FD, FName))
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000747 return getUnarySummary(FD, cfrelease);
Ted Kremenek070a8252008-07-09 18:11:16 +0000748
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000749 if (strcmp(FName, "MakeCollectable") == 0)
750 return getUnarySummary(FD, cfmakecollectable);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000751
752 return getCFCreateGetRuleSummary(FD, FName);
753}
754
755RetainSummary* RetainSummaryManager::getCGSummary(FunctionDecl* FD,
756 const char* FName) {
757
758 if (FName[0] == 'C' && FName[1] == 'G')
759 FName += 2;
760
761 if (isRelease(FD, FName))
762 return getUnarySummary(FD, cfrelease);
763
764 if (isRetain(FD, FName))
765 return getUnarySummary(FD, cfretain);
766
767 return getCFCreateGetRuleSummary(FD, FName);
768}
769
770RetainSummary*
771RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
772 const char* FName) {
773
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000774 if (strstr(FName, "Create") || strstr(FName, "Copy"))
775 return getCFSummaryCreateRule(FD);
Ted Kremenek37d785b2008-07-15 16:50:12 +0000776
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000777 if (strstr(FName, "Get"))
778 return getCFSummaryGetRule(FD);
779
780 return 0;
781}
782
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000783RetainSummary*
784RetainSummaryManager::getUnarySummary(FunctionDecl* FD, UnaryFuncKind func) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000785
786 FunctionTypeProto* FT =
787 dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
788
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000789 if (FT) {
790
791 if (FT->getNumArgs() != 1)
792 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000793
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000794 TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr());
795
796 if (!ArgT)
797 return 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000798
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000799 if (!ArgT->isPointerType())
800 return NULL;
801 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000802
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000803 assert (ScratchArgs.empty());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000804
Ted Kremenek377e2302008-04-29 05:33:51 +0000805 switch (func) {
806 case cfretain: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000807 ScratchArgs.push_back(std::make_pair(0, IncRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000808 return getPersistentSummary(RetEffect::MakeAlias(0),
809 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000810 }
811
812 case cfrelease: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000813 ScratchArgs.push_back(std::make_pair(0, DecRef));
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000814 return getPersistentSummary(RetEffect::MakeNoRet(),
815 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000816 }
817
818 case cfmakecollectable: {
Ted Kremenek377e2302008-04-29 05:33:51 +0000819 if (GCEnabled)
820 ScratchArgs.push_back(std::make_pair(0, DecRef));
821
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000822 return getPersistentSummary(RetEffect::MakeAlias(0),
823 DoNothing, DoNothing);
Ted Kremenek377e2302008-04-29 05:33:51 +0000824 }
825
826 default:
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000827 assert (false && "Not a supported unary function.");
Ted Kremenek98530452008-08-12 20:41:56 +0000828 return 0;
Ted Kremenek940b1d82008-04-10 23:44:06 +0000829 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000830}
831
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000832RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000833
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000834 FunctionType* FT =
835 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000836
837 if (FT && !isCFRefType(FT->getResultType()))
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000838 return getPersistentSummary(RetEffect::MakeNoRet());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000839
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000840 assert (ScratchArgs.empty());
Ted Kremenek070a8252008-07-09 18:11:16 +0000841
842 if (FD->getIdentifier() == CFDictionaryCreateII) {
843 ScratchArgs.push_back(std::make_pair(1, DoNothingByRef));
844 ScratchArgs.push_back(std::make_pair(2, DoNothingByRef));
845 }
846
Ted Kremeneka7344702008-06-23 18:02:52 +0000847 return getPersistentSummary(RetEffect::MakeOwned(true));
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000848}
849
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000850RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000851
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000852 FunctionType* FT =
853 dyn_cast<FunctionType>(FD->getType().getTypePtr());
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000854
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000855 if (FT) {
856 QualType RetTy = FT->getResultType();
Ted Kremeneka0df99f2008-04-11 20:11:19 +0000857
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000858 // FIXME: For now we assume that all pointer types returned are referenced
859 // counted. Since this is the "Get" rule, we assume non-ownership, which
860 // works fine for things that are not reference counted. We do this because
861 // some generic data structures return "void*". We need something better
862 // in the future.
863
864 if (!isCFRefType(RetTy) && !RetTy->isPointerType())
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000865 return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
Ted Kremenek86ad3bc2008-05-05 16:51:50 +0000866 }
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000867
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000868 // FIXME: Add special-cases for functions that retain/release. For now
869 // just handle the default case.
870
Ted Kremenek891d5cc2008-04-24 17:22:33 +0000871 assert (ScratchArgs.empty());
Ted Kremenek3eabf1c2008-05-22 17:31:13 +0000872 return getPersistentSummary(RetEffect::MakeNotOwned(), DoNothing, DoNothing);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +0000873}
874
Ted Kremenek6b3a0f72008-03-11 06:39:11 +0000875//===----------------------------------------------------------------------===//
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000876// Summary creation for Selectors.
877//===----------------------------------------------------------------------===//
878
Ted Kremenek1bffd742008-05-06 15:44:25 +0000879RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000880RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) {
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000881 assert(ScratchArgs.empty());
882
883 RetainSummary* Summ =
Ted Kremenek9c32d082008-05-06 00:30:21 +0000884 getPersistentSummary(RetEffect::MakeReceiverAlias());
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000885
Ted Kremenek553cf182008-06-25 21:21:56 +0000886 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000887 return Summ;
888}
Ted Kremenekd3dbcf42008-05-05 22:11:16 +0000889
Ted Kremenek553cf182008-06-25 21:21:56 +0000890
Ted Kremenek1bffd742008-05-06 15:44:25 +0000891RetainSummary*
Ted Kremenek553cf182008-06-25 21:21:56 +0000892RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
893 ObjCInterfaceDecl* ID) {
Ted Kremenek1bffd742008-05-06 15:44:25 +0000894
895 Selector S = ME->getSelector();
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000896
Ted Kremenek553cf182008-06-25 21:21:56 +0000897 // Look up a summary in our summary cache.
898 ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S);
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000899
Ted Kremenek1f180c32008-06-23 22:21:20 +0000900 if (I != ObjCMethodSummaries.end())
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000901 return I->second;
Ted Kremenek553cf182008-06-25 21:21:56 +0000902
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000903 if (!ME->getType()->isPointerType())
904 return 0;
905
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000906 // "initXXX": pass-through for receiver.
907
908 const char* s = S.getIdentifierInfoForSlot(0)->getName();
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000909 assert (ScratchArgs.empty());
Ted Kremenekaee9e572008-05-06 06:09:09 +0000910
Ted Kremenek0327f772008-06-02 17:14:13 +0000911 if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0)
Ted Kremenek553cf182008-06-25 21:21:56 +0000912 return getInitMethodSummary(ME);
Ted Kremenek1bffd742008-05-06 15:44:25 +0000913
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000914 // "copyXXX", "createXXX", "newXXX": allocators.
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000915
Ted Kremenek84060db2008-05-07 04:25:59 +0000916 if (!isNSType(ME->getReceiver()->getType()))
917 return 0;
918
Ted Kremenek0fcbf8e2008-05-07 20:06:41 +0000919 if (CStrInCStrNoCase(s, "create") || CStrInCStrNoCase(s, "copy") ||
920 CStrInCStrNoCase(s, "new")) {
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000921
922 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
Ted Kremeneka7344702008-06-23 18:02:52 +0000923 : RetEffect::MakeOwned(true);
Ted Kremeneka4b695a2008-05-07 03:45:05 +0000924
925 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenek553cf182008-06-25 21:21:56 +0000926 ObjCMethodSummaries[ME] = Summ;
Ted Kremenek1bffd742008-05-06 15:44:25 +0000927 return Summ;
928 }
Ted Kremenek1bffd742008-05-06 15:44:25 +0000929
Ted Kremenek46e49ee2008-05-05 23:55:01 +0000930 return 0;
931}
932
Ted Kremenekc8395602008-05-06 21:26:51 +0000933RetainSummary*
Ted Kremenek1f180c32008-06-23 22:21:20 +0000934RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
935 Selector S) {
Ted Kremenekc8395602008-05-06 21:26:51 +0000936
Ted Kremenek553cf182008-06-25 21:21:56 +0000937 // FIXME: Eventually we should properly do class method summaries, but
938 // it requires us being able to walk the type hierarchy. Unfortunately,
939 // we cannot do this with just an IdentifierInfo* for the class name.
940
Ted Kremenekc8395602008-05-06 21:26:51 +0000941 // Look up a summary in our cache of Selectors -> Summaries.
Ted Kremenek553cf182008-06-25 21:21:56 +0000942 ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S);
Ted Kremenekc8395602008-05-06 21:26:51 +0000943
Ted Kremenek1f180c32008-06-23 22:21:20 +0000944 if (I != ObjCClassMethodSummaries.end())
Ted Kremenekc8395602008-05-06 21:26:51 +0000945 return I->second;
946
Ted Kremeneka22cc2f2008-05-06 23:07:13 +0000947 return 0;
Ted Kremenekc8395602008-05-06 21:26:51 +0000948}
949
Ted Kremenek1f180c32008-06-23 22:21:20 +0000950void RetainSummaryManager::InitializeClassMethodSummaries() {
Ted Kremenek9c32d082008-05-06 00:30:21 +0000951
952 assert (ScratchArgs.empty());
953
Ted Kremeneka7344702008-06-23 18:02:52 +0000954 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
955 : RetEffect::MakeOwned(true);
956
Ted Kremenek9c32d082008-05-06 00:30:21 +0000957 RetainSummary* Summ = getPersistentSummary(E);
958
Ted Kremenek553cf182008-06-25 21:21:56 +0000959 // Create the summaries for "alloc", "new", and "allocWithZone:" for
960 // NSObject and its derivatives.
961 addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
962 addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
963 addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
Ted Kremenek70a733e2008-07-18 17:24:20 +0000964
965 // Create the [NSAssertionHandler currentHander] summary.
Ted Kremenek9e476de2008-08-12 18:30:56 +0000966 addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"),
Ted Kremenek1a804482008-07-18 18:14:26 +0000967 GetNullarySelector("currentHandler", Ctx),
Ted Kremenek70a733e2008-07-18 17:24:20 +0000968 getPersistentSummary(RetEffect::MakeNotOwned()));
Ted Kremenek9c32d082008-05-06 00:30:21 +0000969}
970
Ted Kremenek1f180c32008-06-23 22:21:20 +0000971void RetainSummaryManager::InitializeMethodSummaries() {
Ted Kremenekb3c3c282008-05-06 00:38:54 +0000972
973 assert (ScratchArgs.empty());
974
Ted Kremenekc8395602008-05-06 21:26:51 +0000975 // Create the "init" selector. It just acts as a pass-through for the
976 // receiver.
Ted Kremenek179064e2008-07-01 17:21:27 +0000977 RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias());
978 addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
Ted Kremenekc8395602008-05-06 21:26:51 +0000979
980 // The next methods are allocators.
Ted Kremeneka7344702008-06-23 18:02:52 +0000981 RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
982 : RetEffect::MakeOwned(true);
983
Ted Kremenek179064e2008-07-01 17:21:27 +0000984 RetainSummary* Summ = getPersistentSummary(E);
Ted Kremenekc8395602008-05-06 21:26:51 +0000985
986 // Create the "copy" selector.
Ted Kremenek98530452008-08-12 20:41:56 +0000987 addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ);
988
Ted Kremenekb3c3c282008-05-06 00:38:54 +0000989 // Create the "mutableCopy" selector.
Ted Kremenek553cf182008-06-25 21:21:56 +0000990 addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +0000991
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000992 // Create the "retain" selector.
993 E = RetEffect::MakeReceiverAlias();
994 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef);
Ted Kremenek553cf182008-06-25 21:21:56 +0000995 addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +0000996
997 // Create the "release" selector.
998 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +0000999 addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
Ted Kremenek299e8152008-05-07 21:17:39 +00001000
1001 // Create the "drain" selector.
1002 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
Ted Kremenek553cf182008-06-25 21:21:56 +00001003 addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001004
1005 // Create the "autorelease" selector.
Ted Kremeneke19f4492008-06-30 16:57:41 +00001006 Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : Autorelease);
Ted Kremenek553cf182008-06-25 21:21:56 +00001007 addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
Ted Kremenek98530452008-08-12 20:41:56 +00001008
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001009 // For NSWindow, allocated objects are (initially) self-owned.
Ted Kremenek179064e2008-07-01 17:21:27 +00001010 RetainSummary *NSWindowSumm =
1011 getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn);
Ted Kremenekaf9dc272008-08-12 18:48:50 +00001012
1013 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1014 "styleMask", "backing", "defer", NULL);
1015
1016 addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
1017 "styleMask", "backing", "defer", "screen", NULL);
1018
1019 // For NSPanel (which subclasses NSWindow), allocated objects are not
1020 // self-owned.
1021 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1022 "styleMask", "backing", "defer", NULL);
1023
1024 addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
1025 "styleMask", "backing", "defer", "screen", NULL);
Ted Kremenek553cf182008-06-25 21:21:56 +00001026
Ted Kremenek70a733e2008-07-18 17:24:20 +00001027 // Create NSAssertionHandler summaries.
Ted Kremenek9e476de2008-08-12 18:30:56 +00001028 addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
1029 "lineNumber", "description", NULL);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001030
Ted Kremenek9e476de2008-08-12 18:30:56 +00001031 addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1032 "file", "lineNumber", "description", NULL);
Ted Kremenekb3c3c282008-05-06 00:38:54 +00001033}
1034
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001035//===----------------------------------------------------------------------===//
Ted Kremenek13922612008-04-16 20:40:59 +00001036// Reference-counting logic (typestate + counts).
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001037//===----------------------------------------------------------------------===//
1038
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001039namespace {
1040
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001041class VISIBILITY_HIDDEN RefVal {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001042public:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001043
Ted Kremenek4fd88972008-04-17 18:12:53 +00001044 enum Kind {
1045 Owned = 0, // Owning reference.
1046 NotOwned, // Reference is not owned by still valid (not freed).
1047 Released, // Object has been released.
1048 ReturnedOwned, // Returned object passes ownership to caller.
1049 ReturnedNotOwned, // Return object does not pass ownership to caller.
1050 ErrorUseAfterRelease, // Object used after released.
1051 ErrorReleaseNotOwned, // Release of an object that was not owned.
1052 ErrorLeak // A memory leak due to excessive reference counts.
1053 };
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001054
Ted Kremenek4fd88972008-04-17 18:12:53 +00001055private:
1056
1057 Kind kind;
1058 unsigned Cnt;
Ted Kremenek553cf182008-06-25 21:21:56 +00001059 QualType T;
1060
1061 RefVal(Kind k, unsigned cnt, QualType t) : kind(k), Cnt(cnt), T(t) {}
1062 RefVal(Kind k, unsigned cnt = 0) : kind(k), Cnt(cnt) {}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001063
1064public:
Ted Kremenekdb863712008-04-16 22:32:20 +00001065
Ted Kremenek4fd88972008-04-17 18:12:53 +00001066 Kind getKind() const { return kind; }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001067
Ted Kremenek553cf182008-06-25 21:21:56 +00001068 unsigned getCount() const { return Cnt; }
1069 QualType getType() const { return T; }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001070
1071 // Useful predicates.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001072
Ted Kremenek73c750b2008-03-11 18:14:09 +00001073 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
1074
Ted Kremenekdb863712008-04-16 22:32:20 +00001075 static bool isLeak(Kind k) { return k == ErrorLeak; }
1076
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001077 bool isOwned() const {
1078 return getKind() == Owned;
1079 }
1080
Ted Kremenekdb863712008-04-16 22:32:20 +00001081 bool isNotOwned() const {
1082 return getKind() == NotOwned;
1083 }
1084
Ted Kremenek4fd88972008-04-17 18:12:53 +00001085 bool isReturnedOwned() const {
1086 return getKind() == ReturnedOwned;
1087 }
1088
1089 bool isReturnedNotOwned() const {
1090 return getKind() == ReturnedNotOwned;
1091 }
1092
1093 bool isNonLeakError() const {
1094 Kind k = getKind();
1095 return isError(k) && !isLeak(k);
1096 }
1097
1098 // State creation: normal state.
1099
Ted Kremenek553cf182008-06-25 21:21:56 +00001100 static RefVal makeOwned(QualType t, unsigned Count = 1) {
1101 return RefVal(Owned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001102 }
1103
Ted Kremenek553cf182008-06-25 21:21:56 +00001104 static RefVal makeNotOwned(QualType t, unsigned Count = 0) {
1105 return RefVal(NotOwned, Count, t);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001106 }
Ted Kremenek4fd88972008-04-17 18:12:53 +00001107
1108 static RefVal makeReturnedOwned(unsigned Count) {
1109 return RefVal(ReturnedOwned, Count);
1110 }
1111
1112 static RefVal makeReturnedNotOwned() {
1113 return RefVal(ReturnedNotOwned);
1114 }
1115
Ted Kremenek4fd88972008-04-17 18:12:53 +00001116 // Comparison, profiling, and pretty-printing.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001117
Ted Kremenek4fd88972008-04-17 18:12:53 +00001118 bool operator==(const RefVal& X) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001119 return kind == X.kind && Cnt == X.Cnt && T == X.T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001120 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001121
Ted Kremenek553cf182008-06-25 21:21:56 +00001122 RefVal operator-(size_t i) const {
1123 return RefVal(getKind(), getCount() - i, getType());
1124 }
1125
1126 RefVal operator+(size_t i) const {
1127 return RefVal(getKind(), getCount() + i, getType());
1128 }
1129
1130 RefVal operator^(Kind k) const {
1131 return RefVal(k, getCount(), getType());
1132 }
1133
1134
Ted Kremenek4fd88972008-04-17 18:12:53 +00001135 void Profile(llvm::FoldingSetNodeID& ID) const {
1136 ID.AddInteger((unsigned) kind);
1137 ID.AddInteger(Cnt);
Ted Kremenek553cf182008-06-25 21:21:56 +00001138 ID.Add(T);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001139 }
1140
Ted Kremenekf3948042008-03-11 19:44:10 +00001141 void print(std::ostream& Out) const;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001142};
Ted Kremenekf3948042008-03-11 19:44:10 +00001143
1144void RefVal::print(std::ostream& Out) const {
Ted Kremenek553cf182008-06-25 21:21:56 +00001145 if (!T.isNull())
1146 Out << "Tracked Type:" << T.getAsString() << '\n';
1147
Ted Kremenekf3948042008-03-11 19:44:10 +00001148 switch (getKind()) {
1149 default: assert(false);
Ted Kremenek61b9f872008-04-10 23:09:18 +00001150 case Owned: {
1151 Out << "Owned";
1152 unsigned cnt = getCount();
1153 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001154 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001155 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001156
Ted Kremenek61b9f872008-04-10 23:09:18 +00001157 case NotOwned: {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001158 Out << "NotOwned";
Ted Kremenek61b9f872008-04-10 23:09:18 +00001159 unsigned cnt = getCount();
1160 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenekf3948042008-03-11 19:44:10 +00001161 break;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001162 }
Ted Kremenekf3948042008-03-11 19:44:10 +00001163
Ted Kremenek4fd88972008-04-17 18:12:53 +00001164 case ReturnedOwned: {
1165 Out << "ReturnedOwned";
1166 unsigned cnt = getCount();
1167 if (cnt) Out << " (+ " << cnt << ")";
1168 break;
1169 }
1170
1171 case ReturnedNotOwned: {
1172 Out << "ReturnedNotOwned";
1173 unsigned cnt = getCount();
1174 if (cnt) Out << " (+ " << cnt << ")";
1175 break;
1176 }
1177
Ted Kremenekf3948042008-03-11 19:44:10 +00001178 case Released:
1179 Out << "Released";
1180 break;
1181
Ted Kremenekdb863712008-04-16 22:32:20 +00001182 case ErrorLeak:
1183 Out << "Leaked";
1184 break;
1185
Ted Kremenekf3948042008-03-11 19:44:10 +00001186 case ErrorUseAfterRelease:
1187 Out << "Use-After-Release [ERROR]";
1188 break;
1189
1190 case ErrorReleaseNotOwned:
1191 Out << "Release of Not-Owned [ERROR]";
1192 break;
1193 }
1194}
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001195
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001196} // end anonymous namespace
1197
1198//===----------------------------------------------------------------------===//
1199// RefBindings - State used to track object reference counts.
1200//===----------------------------------------------------------------------===//
1201
1202typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001203static int RefBIndex = 0;
1204
1205namespace clang {
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001206 template<>
1207 struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
1208 static inline void* GDMIndex() { return &RefBIndex; }
1209 };
1210}
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001211
Ted Kremenek13922612008-04-16 20:40:59 +00001212//===----------------------------------------------------------------------===//
1213// Transfer functions.
1214//===----------------------------------------------------------------------===//
1215
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001216namespace {
1217
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001218class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenek8dd56462008-04-18 03:39:05 +00001219public:
Ted Kremenek553cf182008-06-25 21:21:56 +00001220 // Type definitions.
Ted Kremenek8dd56462008-04-18 03:39:05 +00001221 typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolID> >
1222 ReleasesNotOwnedTy;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001223
Ted Kremenek8dd56462008-04-18 03:39:05 +00001224 typedef ReleasesNotOwnedTy UseAfterReleasesTy;
1225
1226 typedef llvm::DenseMap<GRExprEngine::NodeTy*, std::vector<SymbolID>*>
Ted Kremenekdb863712008-04-16 22:32:20 +00001227 LeaksTy;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001228
Ted Kremenekae6814e2008-08-13 21:24:49 +00001229 class BindingsPrinter : public GRState::Printer {
Ted Kremenekf3948042008-03-11 19:44:10 +00001230 public:
Ted Kremenekae6814e2008-08-13 21:24:49 +00001231 virtual void Print(std::ostream& Out, const GRState* state,
1232 const char* nl, const char* sep);
Ted Kremenekf3948042008-03-11 19:44:10 +00001233 };
Ted Kremenek8dd56462008-04-18 03:39:05 +00001234
1235private:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001236 RetainSummaryManager Summaries;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001237 const LangOptions& LOpts;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001238
Ted Kremenek9e476de2008-08-12 18:30:56 +00001239 UseAfterReleasesTy UseAfterReleases;
1240 ReleasesNotOwnedTy ReleasesNotOwned;
1241 LeaksTy Leaks;
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001242
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001243 RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001244 RefVal::Kind& hasErr, RefBindings::Factory& RefBFactory);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001245
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001246 RefVal::Kind& Update(GRStateRef& state, SymbolID sym, RefVal V,
1247 ArgEffect E, RefVal::Kind& hasErr) {
1248
1249 state = state.set<RefBindings>(Update(state.get<RefBindings>(), sym, V,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001250 E, hasErr,
1251 state.get_context<RefBindings>()));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001252 return hasErr;
1253 }
1254
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001255 void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1256 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001257 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001258 ExplodedNode<GRState>* Pred,
1259 const GRState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001260 RefVal::Kind hasErr, SymbolID Sym);
Ted Kremenekdb863712008-04-16 22:32:20 +00001261
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001262 const GRState* HandleSymbolDeath(GRStateManager& VMgr,
1263 const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +00001264 SymbolID sid, RefVal V, bool& hasLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001265
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001266public:
Ted Kremenek13922612008-04-16 20:40:59 +00001267
Ted Kremenek78d46242008-07-22 16:21:24 +00001268 CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
Ted Kremenek377e2302008-04-29 05:33:51 +00001269 : Summaries(Ctx, gcenabled),
Ted Kremenek9e476de2008-08-12 18:30:56 +00001270 LOpts(lopts) {}
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001271
Ted Kremenek8dd56462008-04-18 03:39:05 +00001272 virtual ~CFRefCount() {
1273 for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I)
1274 delete I->second;
1275 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001276
1277 virtual void RegisterChecks(GRExprEngine& Eng);
Ted Kremenekf3948042008-03-11 19:44:10 +00001278
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001279 virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1280 Printers.push_back(new BindingsPrinter());
Ted Kremenekf3948042008-03-11 19:44:10 +00001281 }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001282
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001283 bool isGCEnabled() const { return Summaries.isGCEnabled(); }
Ted Kremenek072192b2008-04-30 23:47:44 +00001284 const LangOptions& getLangOptions() const { return LOpts; }
1285
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001286 // Calls.
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001287
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001288 void EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001289 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001290 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001291 Expr* Ex,
1292 Expr* Receiver,
1293 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001294 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001295 ExplodedNode<GRState>* Pred);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001296
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001297 virtual void EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek199e1a02008-03-12 21:06:49 +00001298 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001299 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek186350f2008-04-23 20:12:28 +00001300 CallExpr* CE, RVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001301 ExplodedNode<GRState>* Pred);
Ted Kremenekfa34b332008-04-09 01:10:13 +00001302
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001303
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001304 virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001305 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001306 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001307 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001308 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001309
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001310 bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001311 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001312 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001313 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001314 ExplodedNode<GRState>* Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001315
Ted Kremenek13922612008-04-16 20:40:59 +00001316 // Stores.
1317
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001318 virtual void EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001319 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001320 GRStmtNodeBuilder<GRState>& Builder,
1321 Expr* E, ExplodedNode<GRState>* Pred,
1322 const GRState* St, RVal TargetLV, RVal Val);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001323 // End-of-path.
1324
1325 virtual void EvalEndPath(GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001326 GREndPathNodeBuilder<GRState>& Builder);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001327
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001328 virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001329 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001330 GRStmtNodeBuilder<GRState>& Builder,
1331 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001332 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001333 const GRState* St,
1334 const GRStateManager::DeadSymbolsTy& Dead);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001335 // Return statements.
1336
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001337 virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001338 GRExprEngine& Engine,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001339 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001340 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001341 ExplodedNode<GRState>* Pred);
Ted Kremenekcb612922008-04-18 19:23:43 +00001342
1343 // Assumptions.
1344
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001345 virtual const GRState* EvalAssume(GRStateManager& VMgr,
1346 const GRState* St, RVal Cond,
Ted Kremenek4323a572008-07-10 22:03:41 +00001347 bool Assumption, bool& isFeasible);
Ted Kremenekcb612922008-04-18 19:23:43 +00001348
Ted Kremenekfa34b332008-04-09 01:10:13 +00001349 // Error iterators.
1350
1351 typedef UseAfterReleasesTy::iterator use_after_iterator;
1352 typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
Ted Kremenek989d5192008-04-17 23:43:50 +00001353 typedef LeaksTy::iterator leaks_iterator;
Ted Kremenekfa34b332008-04-09 01:10:13 +00001354
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001355 use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
1356 use_after_iterator use_after_end() { return UseAfterReleases.end(); }
Ted Kremenekfa34b332008-04-09 01:10:13 +00001357
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001358 bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
1359 bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
Ted Kremenek989d5192008-04-17 23:43:50 +00001360
1361 leaks_iterator leaks_begin() { return Leaks.begin(); }
1362 leaks_iterator leaks_end() { return Leaks.end(); }
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001363};
1364
1365} // end anonymous namespace
1366
Ted Kremenek8dd56462008-04-18 03:39:05 +00001367
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00001368
1369
Ted Kremenekae6814e2008-08-13 21:24:49 +00001370void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state,
1371 const char* nl, const char* sep) {
1372
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001373 RefBindings B = state->get<RefBindings>();
Ted Kremenekf3948042008-03-11 19:44:10 +00001374
Ted Kremenekae6814e2008-08-13 21:24:49 +00001375 if (!B.isEmpty())
Ted Kremenekf3948042008-03-11 19:44:10 +00001376 Out << sep << nl;
1377
1378 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1379 Out << (*I).first << " : ";
1380 (*I).second.print(Out);
1381 Out << nl;
1382 }
1383}
1384
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001385static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) {
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001386 return Summ ? Summ->getArg(idx) : MayEscape;
Ted Kremenekf9561e52008-04-11 20:23:24 +00001387}
1388
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001389static inline RetEffect GetRetEffect(RetainSummary* Summ) {
1390 return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
Ted Kremenekf9561e52008-04-11 20:23:24 +00001391}
1392
Ted Kremenek14993892008-05-06 02:41:27 +00001393static inline ArgEffect GetReceiverE(RetainSummary* Summ) {
1394 return Summ ? Summ->getReceiverEffect() : DoNothing;
1395}
1396
Ted Kremenek70a733e2008-07-18 17:24:20 +00001397static inline bool IsEndPath(RetainSummary* Summ) {
1398 return Summ ? Summ->isEndPath() : false;
1399}
1400
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001401void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst,
1402 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekdb863712008-04-16 22:32:20 +00001403 Expr* NodeExpr, Expr* ErrorExpr,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001404 ExplodedNode<GRState>* Pred,
1405 const GRState* St,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001406 RefVal::Kind hasErr, SymbolID Sym) {
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001407 Builder.BuildSinks = true;
1408 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
1409
1410 if (!N) return;
1411
1412 switch (hasErr) {
1413 default: assert(false);
1414 case RefVal::ErrorUseAfterRelease:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001415 UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001416 break;
1417
1418 case RefVal::ErrorReleaseNotOwned:
Ted Kremenek8dd56462008-04-18 03:39:05 +00001419 ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym);
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001420 break;
1421 }
1422}
1423
Ted Kremenek553cf182008-06-25 21:21:56 +00001424/// GetReturnType - Used to get the return type of a message expression or
1425/// function call with the intention of affixing that type to a tracked symbol.
1426/// While the the return type can be queried directly from RetEx, when
1427/// invoking class methods we augment to the return type to be that of
1428/// a pointer to the class (as opposed it just being id).
1429static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) {
1430
1431 QualType RetTy = RetE->getType();
1432
1433 // FIXME: We aren't handling id<...>.
Chris Lattner8b51fd72008-07-26 22:36:27 +00001434 const PointerType* PT = RetTy->getAsPointerType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001435 if (!PT)
1436 return RetTy;
1437
1438 // If RetEx is not a message expression just return its type.
1439 // If RetEx is a message expression, return its types if it is something
1440 /// more specific than id.
1441
1442 ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE);
1443
1444 if (!ME || !Ctx.isObjCIdType(PT->getPointeeType()))
1445 return RetTy;
1446
1447 ObjCInterfaceDecl* D = ME->getClassInfo().first;
1448
1449 // At this point we know the return type of the message expression is id.
1450 // If we have an ObjCInterceDecl, we know this is a call to a class method
1451 // whose type we can resolve. In such cases, promote the return type to
1452 // Class*.
1453 return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
1454}
1455
1456
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001457void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001458 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001459 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001460 Expr* Ex,
1461 Expr* Receiver,
1462 RetainSummary* Summ,
Ted Kremenek55499762008-06-17 02:43:46 +00001463 ExprIterator arg_beg, ExprIterator arg_end,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001464 ExplodedNode<GRState>* Pred) {
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001465
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001466 // Get the state.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001467 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
Ted Kremenek14993892008-05-06 02:41:27 +00001468
1469 // Evaluate the effect of the arguments.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001470 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001471 unsigned idx = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001472 Expr* ErrorExpr = NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00001473 SymbolID ErrorSym = 0;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001474
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001475 for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
1476 RVal V = state.GetRVal(*I);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001477
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001478 if (isa<lval::SymbolVal>(V)) {
1479 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001480 if (RefBindings::data_type* T = state.get<RefBindings>(Sym))
1481 if (Update(state, Sym, *T, GetArgE(Summ, idx), hasErr)) {
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001482 ErrorExpr = *I;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001483 ErrorSym = Sym;
Ted Kremenekbcf50ad2008-04-11 18:40:51 +00001484 break;
1485 }
Ted Kremenekb8873552008-04-11 20:51:02 +00001486 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001487 else if (isa<LVal>(V)) {
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001488#if 0
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001489 // Nuke all arguments passed by reference.
Ted Kremenekf9561e52008-04-11 20:23:24 +00001490 StateMgr.Unbind(StVals, cast<LVal>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001491#else
Ted Kremenek070a8252008-07-09 18:11:16 +00001492 if (lval::DeclVal* DV = dyn_cast<lval::DeclVal>(&V)) {
1493
1494 if (GetArgE(Summ, idx) == DoNothingByRef)
1495 continue;
1496
1497 // Invalidate the value of the variable passed by reference.
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001498
1499 // FIXME: Either this logic should also be replicated in GRSimpleVals
1500 // or should be pulled into a separate "constraint engine."
Ted Kremenek070a8252008-07-09 18:11:16 +00001501
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001502 // FIXME: We can have collisions on the conjured symbol if the
1503 // expression *I also creates conjured symbols. We probably want
1504 // to identify conjured symbols by an expression pair: the enclosing
1505 // expression (the context) and the expression itself. This should
Ted Kremenek070a8252008-07-09 18:11:16 +00001506 // disambiguate conjured symbols.
1507
1508 // Is the invalidated variable something that we were tracking?
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001509 RVal X = state.GetRVal(*DV);
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001510
Ted Kremenek070a8252008-07-09 18:11:16 +00001511 if (isa<lval::SymbolVal>(X)) {
1512 SymbolID Sym = cast<lval::SymbolVal>(X).getSymbol();
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001513 state = state.remove<RefBindings>(Sym);
Ted Kremenek070a8252008-07-09 18:11:16 +00001514 }
1515
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001516 // Set the value of the variable to be a conjured symbol.
1517 unsigned Count = Builder.getCurrentBlockCount();
1518 SymbolID NewSym = Eng.getSymbolManager().getConjuredSymbol(*I, Count);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001519
1520 state = state.SetRVal(*DV,
1521 LVal::IsLValType(DV->getDecl()->getType())
1522 ? cast<RVal>(lval::SymbolVal(NewSym))
1523 : cast<RVal>(nonlval::SymbolVal(NewSym)));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001524 }
1525 else {
1526 // Nuke all other arguments passed by reference.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001527 state = state.Unbind(cast<LVal>(V));
Ted Kremenek8c5633e2008-07-03 23:26:32 +00001528 }
1529#endif
Ted Kremenekb8873552008-04-11 20:51:02 +00001530 }
Ted Kremeneka5488462008-04-22 21:39:21 +00001531 else if (isa<nonlval::LValAsInteger>(V))
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001532 state = state.Unbind(cast<nonlval::LValAsInteger>(V).getLVal());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001533 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001534
Ted Kremenek553cf182008-06-25 21:21:56 +00001535 // Evaluate the effect on the message receiver.
Ted Kremenek14993892008-05-06 02:41:27 +00001536 if (!ErrorExpr && Receiver) {
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001537 RVal V = state.GetRVal(Receiver);
Ted Kremenek14993892008-05-06 02:41:27 +00001538 if (isa<lval::SymbolVal>(V)) {
1539 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001540 if (const RefVal* T = state.get<RefBindings>(Sym))
1541 if (Update(state, Sym, *T, GetReceiverE(Summ), hasErr)) {
Ted Kremenek14993892008-05-06 02:41:27 +00001542 ErrorExpr = Receiver;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001543 ErrorSym = Sym;
Ted Kremenek14993892008-05-06 02:41:27 +00001544 }
Ted Kremenek14993892008-05-06 02:41:27 +00001545 }
1546 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001547
Ted Kremenek553cf182008-06-25 21:21:56 +00001548 // Process any errors.
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001549 if (hasErr) {
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001550 ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state,
Ted Kremenek8dd56462008-04-18 03:39:05 +00001551 hasErr, ErrorSym);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001552 return;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001553 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001554
Ted Kremenek70a733e2008-07-18 17:24:20 +00001555 // Consult the summary for the return value.
Ted Kremenek3c0cea32008-05-06 02:26:56 +00001556 RetEffect RE = GetRetEffect(Summ);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001557
1558 switch (RE.getKind()) {
1559 default:
1560 assert (false && "Unhandled RetEffect."); break;
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001561
Ted Kremenek940b1d82008-04-10 23:44:06 +00001562 case RetEffect::NoRet:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001563
Ted Kremenekf9561e52008-04-11 20:23:24 +00001564 // Make up a symbol for the return value (not reference counted).
Ted Kremenekb8873552008-04-11 20:51:02 +00001565 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
1566 // should compose behavior, not copy it.
Ted Kremenekf9561e52008-04-11 20:23:24 +00001567
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001568 if (Ex->getType() != Eng.getContext().VoidTy) {
Ted Kremenekf9561e52008-04-11 20:23:24 +00001569 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001570 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001571
Ted Kremenek0e470a52008-05-09 23:45:33 +00001572 RVal X = LVal::IsLValType(Ex->getType())
1573 ? cast<RVal>(lval::SymbolVal(Sym))
1574 : cast<RVal>(nonlval::SymbolVal(Sym));
Ted Kremenekf9561e52008-04-11 20:23:24 +00001575
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001576 state = state.SetRVal(Ex, X, false);
Ted Kremenekf9561e52008-04-11 20:23:24 +00001577 }
1578
Ted Kremenek940b1d82008-04-10 23:44:06 +00001579 break;
1580
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001581 case RetEffect::Alias: {
Ted Kremenek553cf182008-06-25 21:21:56 +00001582 unsigned idx = RE.getIndex();
Ted Kremenek55499762008-06-17 02:43:46 +00001583 assert (arg_end >= arg_beg);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001584 assert (idx < (unsigned) (arg_end - arg_beg));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001585 RVal V = state.GetRVal(*(arg_beg+idx));
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001586 state = state.SetRVal(Ex, V, false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001587 break;
1588 }
1589
Ted Kremenek14993892008-05-06 02:41:27 +00001590 case RetEffect::ReceiverAlias: {
1591 assert (Receiver);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001592 RVal V = state.GetRVal(Receiver);
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001593 state = state.SetRVal(Ex, V, false);
Ted Kremenek14993892008-05-06 02:41:27 +00001594 break;
1595 }
1596
Ted Kremeneka7344702008-06-23 18:02:52 +00001597 case RetEffect::OwnedAllocatedSymbol:
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001598 case RetEffect::OwnedSymbol: {
1599 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001600 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001601 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001602
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001603 state = state.set<RefBindings>(Sym, RefVal::makeOwned(RetT));
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001604 state = state.SetRVal(Ex, lval::SymbolVal(Sym), false);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001605
1606#if 0
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001607 RefBindings B = GetRefBindings(StImpl);
Ted Kremenek553cf182008-06-25 21:21:56 +00001608 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(RetT)));
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001609#endif
1610
Ted Kremeneka7344702008-06-23 18:02:52 +00001611 // FIXME: Add a flag to the checker where allocations are allowed to fail.
1612 if (RE.getKind() == RetEffect::OwnedAllocatedSymbol)
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001613 state = state.AddNE(Sym, Eng.getBasicVals().getZeroWithPtrWidth());
Ted Kremeneka7344702008-06-23 18:02:52 +00001614
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001615 break;
1616 }
1617
1618 case RetEffect::NotOwnedSymbol: {
1619 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001620 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
Ted Kremenek553cf182008-06-25 21:21:56 +00001621 QualType RetT = GetReturnType(Ex, Eng.getContext());
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001622
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001623 state = state.set<RefBindings>(Sym, RefVal::makeNotOwned(RetT));
Ted Kremenek1c72ef02008-08-16 00:49:49 +00001624 state = state.SetRVal(Ex, lval::SymbolVal(Sym), false);
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001625 break;
1626 }
1627 }
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001628
Ted Kremenek70a733e2008-07-18 17:24:20 +00001629 // Is this a sink?
1630 if (IsEndPath(Summ))
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001631 Builder.MakeSinkNode(Dst, Ex, Pred, state);
Ted Kremenek70a733e2008-07-18 17:24:20 +00001632 else
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001633 Builder.MakeNode(Dst, Ex, Pred, state);
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001634}
1635
1636
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001637void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001638 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001639 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001640 CallExpr* CE, RVal L,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001641 ExplodedNode<GRState>* Pred) {
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001642
Ted Kremenek9e476de2008-08-12 18:30:56 +00001643 RetainSummary* Summ = !isa<lval::FuncVal>(L) ? 0
1644 : Summaries.getSummary(cast<lval::FuncVal>(L).getDecl());
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001645
1646 EvalSummary(Dst, Eng, Builder, CE, 0, Summ,
1647 CE->arg_begin(), CE->arg_end(), Pred);
Ted Kremenek2fff37e2008-03-06 00:08:09 +00001648}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001649
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001650void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek85348202008-04-15 23:44:31 +00001651 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001652 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek85348202008-04-15 23:44:31 +00001653 ObjCMessageExpr* ME,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001654 ExplodedNode<GRState>* Pred) {
Ted Kremenekb3095252008-05-06 04:20:12 +00001655 RetainSummary* Summ;
Ted Kremenek9040c652008-05-01 21:31:50 +00001656
Ted Kremenek553cf182008-06-25 21:21:56 +00001657 if (Expr* Receiver = ME->getReceiver()) {
1658 // We need the type-information of the tracked receiver object
1659 // Retrieve it from the state.
1660 ObjCInterfaceDecl* ID = 0;
1661
1662 // FIXME: Wouldn't it be great if this code could be reduced? It's just
1663 // a chain of lookups.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001664 const GRState* St = Builder.GetState(Pred);
Ted Kremenek553cf182008-06-25 21:21:56 +00001665 RVal V = Eng.getStateManager().GetRVal(St, Receiver );
1666
1667 if (isa<lval::SymbolVal>(V)) {
1668 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
1669
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001670 if (const RefVal* T = St->get<RefBindings>(Sym)) {
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001671 QualType Ty = T->getType();
Ted Kremenek553cf182008-06-25 21:21:56 +00001672
1673 if (const PointerType* PT = Ty->getAsPointerType()) {
1674 QualType PointeeTy = PT->getPointeeType();
1675
1676 if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy))
1677 ID = IT->getDecl();
1678 }
1679 }
1680 }
1681
1682 Summ = Summaries.getMethodSummary(ME, ID);
1683 }
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001684 else
Ted Kremenek1f180c32008-06-23 22:21:20 +00001685 Summ = Summaries.getClassMethodSummary(ME->getClassName(),
1686 ME->getSelector());
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001687
Ted Kremenekb3095252008-05-06 04:20:12 +00001688 EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ,
1689 ME->arg_begin(), ME->arg_end(), Pred);
Ted Kremenek85348202008-04-15 23:44:31 +00001690}
Ted Kremenekb3095252008-05-06 04:20:12 +00001691
Ted Kremenek13922612008-04-16 20:40:59 +00001692// Stores.
1693
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001694void CFRefCount::EvalStore(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek13922612008-04-16 20:40:59 +00001695 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001696 GRStmtNodeBuilder<GRState>& Builder,
1697 Expr* E, ExplodedNode<GRState>* Pred,
1698 const GRState* St, RVal TargetLV, RVal Val) {
Ted Kremenek13922612008-04-16 20:40:59 +00001699
1700 // Check if we have a binding for "Val" and if we are storing it to something
1701 // we don't understand or otherwise the value "escapes" the function.
1702
1703 if (!isa<lval::SymbolVal>(Val))
1704 return;
1705
1706 // Are we storing to something that causes the value to "escape"?
1707
1708 bool escapes = false;
1709
1710 if (!isa<lval::DeclVal>(TargetLV))
1711 escapes = true;
1712 else
1713 escapes = cast<lval::DeclVal>(TargetLV).getDecl()->hasGlobalStorage();
1714
1715 if (!escapes)
1716 return;
1717
1718 SymbolID Sym = cast<lval::SymbolVal>(Val).getSymbol();
Ted Kremenek13922612008-04-16 20:40:59 +00001719
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001720 GRStateRef state(St, Eng.getStateManager());
1721
1722 if (!state.get<RefBindings>(Sym))
Ted Kremenek13922612008-04-16 20:40:59 +00001723 return;
1724
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001725 // Nuke the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001726 state = state.remove<RefBindings>(Sym);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001727
Ted Kremenek13922612008-04-16 20:40:59 +00001728 // Hand of the remaining logic to the parent implementation.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001729 GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, state, TargetLV, Val);
Ted Kremenekdb863712008-04-16 22:32:20 +00001730}
1731
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001732// End-of-path.
1733
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001734const GRState* CFRefCount::HandleSymbolDeath(GRStateManager& VMgr,
1735 const GRState* St, SymbolID sid,
Ted Kremenekdb863712008-04-16 22:32:20 +00001736 RefVal V, bool& hasLeak) {
1737
Ted Kremenek4fd88972008-04-17 18:12:53 +00001738 hasLeak = V.isOwned() ||
1739 ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
Ted Kremenekdb863712008-04-16 22:32:20 +00001740
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001741 GRStateRef state(St, VMgr);
1742
Ted Kremenekdb863712008-04-16 22:32:20 +00001743 if (!hasLeak)
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001744 return state.remove<RefBindings>(sid);
Ted Kremenekdb863712008-04-16 22:32:20 +00001745
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001746 return state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak);
Ted Kremenekdb863712008-04-16 22:32:20 +00001747}
1748
1749void CFRefCount::EvalEndPath(GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001750 GREndPathNodeBuilder<GRState>& Builder) {
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001751
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001752 const GRState* St = Builder.getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001753 RefBindings B = St->get<RefBindings>();
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001754
Ted Kremenekdb863712008-04-16 22:32:20 +00001755 llvm::SmallVector<SymbolID, 10> Leaked;
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001756
Ted Kremenekdb863712008-04-16 22:32:20 +00001757 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1758 bool hasLeak = false;
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001759
Ted Kremenekdb863712008-04-16 22:32:20 +00001760 St = HandleSymbolDeath(Eng.getStateManager(), St,
1761 (*I).first, (*I).second, hasLeak);
1762
1763 if (hasLeak) Leaked.push_back((*I).first);
1764 }
Ted Kremenek652adc62008-04-24 23:57:27 +00001765
1766 if (Leaked.empty())
1767 return;
1768
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001769 ExplodedNode<GRState>* N = Builder.MakeNode(St);
Ted Kremenek4f285152008-04-18 16:30:14 +00001770
Ted Kremenek652adc62008-04-24 23:57:27 +00001771 if (!N)
Ted Kremenek4f285152008-04-18 16:30:14 +00001772 return;
Ted Kremenekcb612922008-04-18 19:23:43 +00001773
Ted Kremenek8dd56462008-04-18 03:39:05 +00001774 std::vector<SymbolID>*& LeaksAtNode = Leaks[N];
1775 assert (!LeaksAtNode);
1776 LeaksAtNode = new std::vector<SymbolID>();
Ted Kremenekdb863712008-04-16 22:32:20 +00001777
1778 for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(),
1779 E = Leaked.end(); I != E; ++I)
Ted Kremenek8dd56462008-04-18 03:39:05 +00001780 (*LeaksAtNode).push_back(*I);
Ted Kremeneke7bd9c22008-04-11 22:25:11 +00001781}
1782
Ted Kremenek652adc62008-04-24 23:57:27 +00001783// Dead symbols.
1784
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001785void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek652adc62008-04-24 23:57:27 +00001786 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001787 GRStmtNodeBuilder<GRState>& Builder,
1788 ExplodedNode<GRState>* Pred,
Ted Kremenek910e9992008-04-25 01:25:15 +00001789 Stmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001790 const GRState* St,
1791 const GRStateManager::DeadSymbolsTy& Dead) {
Ted Kremenek910e9992008-04-25 01:25:15 +00001792
Ted Kremenek652adc62008-04-24 23:57:27 +00001793 // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
1794
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001795 RefBindings B = St->get<RefBindings>();
Ted Kremenek652adc62008-04-24 23:57:27 +00001796 llvm::SmallVector<SymbolID, 10> Leaked;
1797
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001798 for (GRStateManager::DeadSymbolsTy::const_iterator
Ted Kremenek652adc62008-04-24 23:57:27 +00001799 I=Dead.begin(), E=Dead.end(); I!=E; ++I) {
1800
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001801 const RefVal* T = B.lookup(*I);
Ted Kremenek652adc62008-04-24 23:57:27 +00001802
1803 if (!T)
1804 continue;
1805
1806 bool hasLeak = false;
1807
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001808 St = HandleSymbolDeath(Eng.getStateManager(), St, *I, *T, hasLeak);
Ted Kremenek652adc62008-04-24 23:57:27 +00001809
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001810 if (hasLeak)
1811 Leaked.push_back(*I);
Ted Kremenek652adc62008-04-24 23:57:27 +00001812 }
1813
1814 if (Leaked.empty())
1815 return;
1816
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001817 ExplodedNode<GRState>* N = Builder.MakeNode(Dst, S, Pred, St);
Ted Kremenek652adc62008-04-24 23:57:27 +00001818
1819 if (!N)
1820 return;
1821
1822 std::vector<SymbolID>*& LeaksAtNode = Leaks[N];
1823 assert (!LeaksAtNode);
1824 LeaksAtNode = new std::vector<SymbolID>();
1825
1826 for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(),
1827 E = Leaked.end(); I != E; ++I)
1828 (*LeaksAtNode).push_back(*I);
1829}
1830
Ted Kremenek4fd88972008-04-17 18:12:53 +00001831 // Return statements.
1832
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001833void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001834 GRExprEngine& Eng,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001835 GRStmtNodeBuilder<GRState>& Builder,
Ted Kremenek4fd88972008-04-17 18:12:53 +00001836 ReturnStmt* S,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001837 ExplodedNode<GRState>* Pred) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001838
1839 Expr* RetE = S->getRetValue();
1840 if (!RetE) return;
1841
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001842 GRStateRef state(Builder.GetState(Pred), Eng.getStateManager());
1843 RVal V = state.GetRVal(RetE);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001844
1845 if (!isa<lval::SymbolVal>(V))
1846 return;
1847
1848 // Get the reference count binding (if any).
1849 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001850 const RefVal* T = state.get<RefBindings>(Sym);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001851
1852 if (!T)
1853 return;
1854
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001855 // Change the reference count.
Ted Kremeneke8fdc832008-07-07 16:21:19 +00001856 RefVal X = *T;
Ted Kremenek4fd88972008-04-17 18:12:53 +00001857
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001858 switch (X.getKind()) {
Ted Kremenek4fd88972008-04-17 18:12:53 +00001859 case RefVal::Owned: {
1860 unsigned cnt = X.getCount();
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001861 assert (cnt > 0);
1862 X = RefVal::makeReturnedOwned(cnt - 1);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001863 break;
1864 }
1865
1866 case RefVal::NotOwned: {
1867 unsigned cnt = X.getCount();
1868 X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
1869 : RefVal::makeReturnedNotOwned();
1870 break;
1871 }
1872
1873 default:
Ted Kremenek4fd88972008-04-17 18:12:53 +00001874 return;
1875 }
1876
1877 // Update the binding.
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001878 state = state.set<RefBindings>(Sym, X);
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001879 Builder.MakeNode(Dst, S, Pred, state);
Ted Kremenek4fd88972008-04-17 18:12:53 +00001880}
1881
Ted Kremenekcb612922008-04-18 19:23:43 +00001882// Assumptions.
1883
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001884const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr,
1885 const GRState* St,
Ted Kremenek4323a572008-07-10 22:03:41 +00001886 RVal Cond, bool Assumption,
1887 bool& isFeasible) {
Ted Kremenekcb612922008-04-18 19:23:43 +00001888
1889 // FIXME: We may add to the interface of EvalAssume the list of symbols
1890 // whose assumptions have changed. For now we just iterate through the
1891 // bindings and check if any of the tracked symbols are NULL. This isn't
1892 // too bad since the number of symbols we will track in practice are
1893 // probably small and EvalAssume is only called at branches and a few
1894 // other places.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001895 RefBindings B = St->get<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00001896
1897 if (B.isEmpty())
1898 return St;
1899
1900 bool changed = false;
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001901
1902 GRStateRef state(St, VMgr);
1903 RefBindings::Factory& RefBFactory = state.get_context<RefBindings>();
Ted Kremenekcb612922008-04-18 19:23:43 +00001904
1905 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
Ted Kremenekcb612922008-04-18 19:23:43 +00001906 // Check if the symbol is null (or equal to any constant).
1907 // If this is the case, stop tracking the symbol.
Zhongxing Xu39cfed32008-08-29 14:52:36 +00001908 if (VMgr.getSymVal(St, I.getKey())) {
Ted Kremenekcb612922008-04-18 19:23:43 +00001909 changed = true;
1910 B = RefBFactory.Remove(B, I.getKey());
1911 }
1912 }
1913
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001914 if (changed)
1915 state = state.set<RefBindings>(B);
Ted Kremenekcb612922008-04-18 19:23:43 +00001916
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001917 return state;
Ted Kremenekcb612922008-04-18 19:23:43 +00001918}
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001919
Ted Kremenek72cd17f2008-08-14 21:16:54 +00001920RefBindings CFRefCount::Update(RefBindings B, SymbolID sym,
1921 RefVal V, ArgEffect E,
Ted Kremenekb9d17f92008-08-17 03:20:02 +00001922 RefVal::Kind& hasErr,
1923 RefBindings::Factory& RefBFactory) {
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001924
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001925 // FIXME: This dispatch can potentially be sped up by unifiying it into
1926 // a single switch statement. Opt for simplicity for now.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00001927
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001928 switch (E) {
1929 default:
1930 assert (false && "Unhandled CFRef transition.");
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001931
1932 case MayEscape:
1933 if (V.getKind() == RefVal::Owned) {
Ted Kremenek553cf182008-06-25 21:21:56 +00001934 V = V ^ RefVal::NotOwned;
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001935 break;
1936 }
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00001937 // Fall-through.
Ted Kremenek070a8252008-07-09 18:11:16 +00001938 case DoNothingByRef:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001939 case DoNothing:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001940 if (!isGCEnabled() && V.getKind() == RefVal::Released) {
Ted Kremenek553cf182008-06-25 21:21:56 +00001941 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001942 hasErr = V.getKind();
Ted Kremenek00a3a5f2008-03-12 01:21:45 +00001943 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00001944 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001945 return B;
Ted Kremeneke19f4492008-06-30 16:57:41 +00001946
Ted Kremenek80d753f2008-07-01 00:01:02 +00001947 case Autorelease:
Ted Kremenek14993892008-05-06 02:41:27 +00001948 case StopTracking:
1949 return RefBFactory.Remove(B, sym);
Ted Kremenek9e476de2008-08-12 18:30:56 +00001950
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001951 case IncRef:
1952 switch (V.getKind()) {
1953 default:
1954 assert(false);
1955
1956 case RefVal::Owned:
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001957 case RefVal::NotOwned:
Ted Kremenek553cf182008-06-25 21:21:56 +00001958 V = V + 1;
Ted Kremenek9e476de2008-08-12 18:30:56 +00001959 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001960 case RefVal::Released:
Ted Kremenekd3dbcf42008-05-05 22:11:16 +00001961 if (isGCEnabled())
Ted Kremenek553cf182008-06-25 21:21:56 +00001962 V = V ^ RefVal::Owned;
Ted Kremenek65c91652008-04-29 05:44:10 +00001963 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00001964 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek65c91652008-04-29 05:44:10 +00001965 hasErr = V.getKind();
1966 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001967 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00001968 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00001969 break;
1970
Ted Kremenek553cf182008-06-25 21:21:56 +00001971 case SelfOwn:
1972 V = V ^ RefVal::NotOwned;
Ted Kremenek9e476de2008-08-12 18:30:56 +00001973 // Fall-through.
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001974 case DecRef:
1975 switch (V.getKind()) {
1976 default:
1977 assert (false);
Ted Kremenek9e476de2008-08-12 18:30:56 +00001978
Ted Kremenek553cf182008-06-25 21:21:56 +00001979 case RefVal::Owned:
1980 V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001981 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001982
Ted Kremenek553cf182008-06-25 21:21:56 +00001983 case RefVal::NotOwned:
1984 if (V.getCount() > 0)
1985 V = V - 1;
Ted Kremenek61b9f872008-04-10 23:09:18 +00001986 else {
Ted Kremenek553cf182008-06-25 21:21:56 +00001987 V = V ^ RefVal::ErrorReleaseNotOwned;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001988 hasErr = V.getKind();
Ted Kremenek9e476de2008-08-12 18:30:56 +00001989 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001990 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001991
1992 case RefVal::Released:
Ted Kremenek553cf182008-06-25 21:21:56 +00001993 V = V ^ RefVal::ErrorUseAfterRelease;
Ted Kremenek9ed18e62008-04-16 04:28:53 +00001994 hasErr = V.getKind();
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001995 break;
Ted Kremenek9e476de2008-08-12 18:30:56 +00001996 }
Ted Kremenek940b1d82008-04-10 23:44:06 +00001997 break;
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001998 }
Ted Kremenek1ac08d62008-03-11 17:48:22 +00001999 return RefBFactory.Add(B, sym, V);
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002000}
2001
Ted Kremenekfa34b332008-04-09 01:10:13 +00002002//===----------------------------------------------------------------------===//
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002003// Error reporting.
Ted Kremenekfa34b332008-04-09 01:10:13 +00002004//===----------------------------------------------------------------------===//
2005
Ted Kremenek8dd56462008-04-18 03:39:05 +00002006namespace {
2007
2008 //===-------------===//
2009 // Bug Descriptions. //
2010 //===-------------===//
2011
Ted Kremenek95cc1ba2008-04-18 20:54:29 +00002012 class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002013 protected:
2014 CFRefCount& TF;
2015
2016 public:
2017 CFRefBug(CFRefCount& tf) : TF(tf) {}
Ted Kremenek072192b2008-04-30 23:47:44 +00002018
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002019 CFRefCount& getTF() { return TF; }
Ted Kremenek789deac2008-05-05 23:16:31 +00002020 const CFRefCount& getTF() const { return TF; }
2021
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002022 virtual bool isLeak() const { return false; }
Ted Kremenek8dd56462008-04-18 03:39:05 +00002023 };
2024
2025 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
2026 public:
2027 UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
2028
2029 virtual const char* getName() const {
Ted Kremenek789deac2008-05-05 23:16:31 +00002030 return "Use-After-Release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002031 }
2032 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002033 return "Reference-counted object is used after it is released.";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002034 }
2035
2036 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002037 };
2038
2039 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
2040 public:
2041 BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
2042
2043 virtual const char* getName() const {
Ted Kremenek789deac2008-05-05 23:16:31 +00002044 return "Bad Release";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002045 }
2046 virtual const char* getDescription() const {
2047 return "Incorrect decrement of the reference count of a "
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002048 "CoreFoundation object: "
Ted Kremenek8dd56462008-04-18 03:39:05 +00002049 "The object is not owned at this point by the caller.";
2050 }
2051
2052 virtual void EmitWarnings(BugReporter& BR);
2053 };
2054
2055 class VISIBILITY_HIDDEN Leak : public CFRefBug {
2056 public:
2057 Leak(CFRefCount& tf) : CFRefBug(tf) {}
2058
2059 virtual const char* getName() const {
Ted Kremenek432af592008-05-06 18:11:36 +00002060
2061 if (getTF().isGCEnabled())
2062 return "Memory Leak (GC)";
2063
2064 if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
2065 return "Memory Leak (Hybrid MM, non-GC)";
2066
2067 assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
2068 return "Memory Leak";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002069 }
2070
2071 virtual const char* getDescription() const {
Ted Kremenek9e476de2008-08-12 18:30:56 +00002072 return "Object leaked";
Ted Kremenek8dd56462008-04-18 03:39:05 +00002073 }
2074
2075 virtual void EmitWarnings(BugReporter& BR);
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002076 virtual void GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002077 virtual bool isLeak() const { return true; }
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002078 virtual bool isCached(BugReport& R);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002079 };
2080
2081 //===---------===//
2082 // Bug Reports. //
2083 //===---------===//
2084
2085 class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
2086 SymbolID Sym;
2087 public:
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002088 CFRefReport(CFRefBug& D, ExplodedNode<GRState> *n, SymbolID sym)
Ted Kremenek8dd56462008-04-18 03:39:05 +00002089 : RangedBugReport(D, n), Sym(sym) {}
2090
2091 virtual ~CFRefReport() {}
2092
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002093 CFRefBug& getBugType() {
2094 return (CFRefBug&) RangedBugReport::getBugType();
2095 }
2096 const CFRefBug& getBugType() const {
2097 return (const CFRefBug&) RangedBugReport::getBugType();
2098 }
2099
2100 virtual void getRanges(BugReporter& BR, const SourceRange*& beg,
2101 const SourceRange*& end) {
2102
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002103 if (!getBugType().isLeak())
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002104 RangedBugReport::getRanges(BR, beg, end);
Ted Kremenek9e476de2008-08-12 18:30:56 +00002105 else
2106 beg = end = 0;
Ted Kremenekbb77e9b2008-05-01 22:50:36 +00002107 }
2108
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002109 SymbolID getSymbol() const { return Sym; }
2110
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002111 virtual PathDiagnosticPiece* getEndPath(BugReporter& BR,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002112 ExplodedNode<GRState>* N);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002113
Ted Kremenek072192b2008-04-30 23:47:44 +00002114 virtual std::pair<const char**,const char**> getExtraDescriptiveText();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002115
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002116 virtual PathDiagnosticPiece* VisitNode(ExplodedNode<GRState>* N,
2117 ExplodedNode<GRState>* PrevN,
2118 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002119 BugReporter& BR);
2120 };
2121
2122
2123} // end anonymous namespace
2124
2125void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
Ted Kremenek8dd56462008-04-18 03:39:05 +00002126 Eng.Register(new UseAfterRelease(*this));
2127 Eng.Register(new BadRelease(*this));
2128 Eng.Register(new Leak(*this));
2129}
2130
Ted Kremenek072192b2008-04-30 23:47:44 +00002131
2132static const char* Msgs[] = {
2133 "Code is compiled in garbage collection only mode" // GC only
2134 " (the bug occurs with garbage collection enabled).",
2135
2136 "Code is compiled without garbage collection.", // No GC.
2137
2138 "Code is compiled for use with and without garbage collection (GC)."
2139 " The bug occurs with GC enabled.", // Hybrid, with GC.
2140
2141 "Code is compiled for use with and without garbage collection (GC)."
2142 " The bug occurs in non-GC mode." // Hyrbird, without GC/
2143};
2144
2145std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2146 CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
2147
2148 switch (TF.getLangOptions().getGCMode()) {
2149 default:
2150 assert(false);
Ted Kremenek31593ac2008-05-01 04:02:04 +00002151
2152 case LangOptions::GCOnly:
2153 assert (TF.isGCEnabled());
Ted Kremenek9e476de2008-08-12 18:30:56 +00002154 return std::make_pair(&Msgs[0], &Msgs[0]+1);
2155
Ted Kremenek072192b2008-04-30 23:47:44 +00002156 case LangOptions::NonGC:
2157 assert (!TF.isGCEnabled());
Ted Kremenek072192b2008-04-30 23:47:44 +00002158 return std::make_pair(&Msgs[1], &Msgs[1]+1);
2159
2160 case LangOptions::HybridGC:
2161 if (TF.isGCEnabled())
2162 return std::make_pair(&Msgs[2], &Msgs[2]+1);
2163 else
2164 return std::make_pair(&Msgs[3], &Msgs[3]+1);
2165 }
2166}
2167
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002168PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<GRState>* N,
2169 ExplodedNode<GRState>* PrevN,
2170 ExplodedGraph<GRState>& G,
Ted Kremenek8dd56462008-04-18 03:39:05 +00002171 BugReporter& BR) {
2172
2173 // Check if the type state has changed.
2174
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002175 const GRState* PrevSt = PrevN->getState();
2176 const GRState* CurrSt = N->getState();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002177
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002178 RefBindings PrevB = PrevSt->get<RefBindings>();
2179 RefBindings CurrB = CurrSt->get<RefBindings>();
Ted Kremenek8dd56462008-04-18 03:39:05 +00002180
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002181 const RefVal* PrevT = PrevB.lookup(Sym);
2182 const RefVal* CurrT = CurrB.lookup(Sym);
Ted Kremenek8dd56462008-04-18 03:39:05 +00002183
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002184 if (!CurrT)
2185 return NULL;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002186
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002187 const char* Msg = NULL;
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002188 const RefVal& CurrV = *CurrB.lookup(Sym);
Ted Kremenekce48e002008-05-05 17:53:17 +00002189
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002190 if (!PrevT) {
2191
Ted Kremenekce48e002008-05-05 17:53:17 +00002192 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2193
2194 if (CurrV.isOwned()) {
2195
2196 if (isa<CallExpr>(S))
2197 Msg = "Function call returns an object with a +1 retain count"
2198 " (owning reference).";
2199 else {
2200 assert (isa<ObjCMessageExpr>(S));
2201 Msg = "Method returns an object with a +1 retain count"
2202 " (owning reference).";
2203 }
2204 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002205 else {
2206 assert (CurrV.isNotOwned());
Ted Kremenekce48e002008-05-05 17:53:17 +00002207
2208 if (isa<CallExpr>(S))
2209 Msg = "Function call returns an object with a +0 retain count"
2210 " (non-owning reference).";
2211 else {
2212 assert (isa<ObjCMessageExpr>(S));
2213 Msg = "Method returns an object with a +0 retain count"
2214 " (non-owning reference).";
2215 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002216 }
Ted Kremenekce48e002008-05-05 17:53:17 +00002217
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002218 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2219 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2220
2221 if (Expr* Exp = dyn_cast<Expr>(S))
2222 P->addRange(Exp->getSourceRange());
2223
2224 return P;
2225 }
2226
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002227 // Determine if the typestate has changed.
2228 RefVal PrevV = *PrevB.lookup(Sym);
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002229
2230 if (PrevV == CurrV)
2231 return NULL;
2232
2233 // The typestate has changed.
2234
2235 std::ostringstream os;
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002236 std::string s;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002237
2238 switch (CurrV.getKind()) {
2239 case RefVal::Owned:
2240 case RefVal::NotOwned:
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002241
2242 if (PrevV.getCount() == CurrV.getCount())
2243 return 0;
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002244
2245 if (PrevV.getCount() > CurrV.getCount())
2246 os << "Reference count decremented.";
2247 else
2248 os << "Reference count incremented.";
2249
Ted Kremenek3eabf1c2008-05-22 17:31:13 +00002250 if (unsigned Count = CurrV.getCount()) {
Ted Kremenekce48e002008-05-05 17:53:17 +00002251
2252 os << " Object has +" << Count;
Ted Kremenek79c140b2008-04-18 05:32:44 +00002253
Ted Kremenekce48e002008-05-05 17:53:17 +00002254 if (Count > 1)
2255 os << " retain counts.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002256 else
Ted Kremenekce48e002008-05-05 17:53:17 +00002257 os << " retain count.";
Ted Kremenek79c140b2008-04-18 05:32:44 +00002258 }
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002259
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002260 s = os.str();
2261 Msg = s.c_str();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002262
2263 break;
2264
2265 case RefVal::Released:
2266 Msg = "Object released.";
2267 break;
2268
2269 case RefVal::ReturnedOwned:
Ted Kremenekce48e002008-05-05 17:53:17 +00002270 Msg = "Object returned to caller as owning reference (single retain count"
2271 " transferred to caller).";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002272 break;
2273
2274 case RefVal::ReturnedNotOwned:
Ted Kremenekce48e002008-05-05 17:53:17 +00002275 Msg = "Object returned to caller with a +0 (non-owning) retain count.";
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002276 break;
2277
2278 default:
2279 return NULL;
2280 }
2281
2282 Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2283 FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
2284 PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
2285
2286 // Add the range by scanning the children of the statement for any bindings
2287 // to Sym.
2288
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002289 GRStateManager& VSM = cast<GRBugReporter>(BR).getStateManager();
Ted Kremenek2cf943a2008-04-18 04:55:01 +00002290
2291 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
2292 if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) {
2293 RVal X = VSM.GetRVal(CurrSt, Exp);
2294
2295 if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&X))
2296 if (SV->getSymbol() == Sym) {
2297 P->addRange(Exp->getSourceRange()); break;
2298 }
2299 }
2300
2301 return P;
Ted Kremenek8dd56462008-04-18 03:39:05 +00002302}
2303
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002304static std::pair<ExplodedNode<GRState>*,store::Binding>
2305GetAllocationSite(GRStateManager* StateMgr, ExplodedNode<GRState>* N,
2306 SymbolID Sym) {
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002307
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002308 // Find both first node that referred to the tracked symbol and the
2309 // memory location that value was store to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002310 ExplodedNode<GRState>* Last = N;
Ted Kremenek60dbad82008-09-03 03:06:11 +00002311 store::Binding FirstBinding;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002312
2313 while (N) {
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002314 const GRState* St = N->getState();
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002315 RefBindings B = St->get<RefBindings>();
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002316
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002317 if (!B.lookup(Sym))
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002318 break;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002319
2320 if (StateMgr) {
2321 llvm::SmallVector<store::Binding, 5> Bindings;
2322 StateMgr->getBindings(Bindings, St, Sym);
2323
2324 if (Bindings.size() == 1)
2325 FirstBinding = Bindings[0];
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002326 }
2327
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002328 Last = N;
2329 N = N->pred_empty() ? NULL : *(N->pred_begin());
2330 }
2331
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002332 return std::make_pair(Last, FirstBinding);
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002333}
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002334
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002335PathDiagnosticPiece* CFRefReport::getEndPath(BugReporter& br,
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002336 ExplodedNode<GRState>* EndN) {
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002337
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002338 GRBugReporter& BR = cast<GRBugReporter>(br);
2339
Ted Kremenek1aa44c72008-05-22 23:45:19 +00002340 // Tell the BugReporter to report cases when the tracked symbol is
2341 // assigned to different variables, etc.
Ted Kremenekc0959972008-07-02 21:24:01 +00002342 cast<GRBugReporter>(BR).addNotableSymbol(Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002343
2344 if (!getBugType().isLeak())
Ted Kremeneke28565b2008-05-05 18:50:19 +00002345 return RangedBugReport::getEndPath(BR, EndN);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002346
Ted Kremenekce48e002008-05-05 17:53:17 +00002347 // Get the retain count.
Ted Kremenek72cd17f2008-08-14 21:16:54 +00002348 unsigned long RetCount = EndN->getState()->get<RefBindings>(Sym)->getCount();
Ted Kremeneke8fdc832008-07-07 16:21:19 +00002349
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002350 // We are a leak. Walk up the graph to get to the first node where the
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002351 // symbol appeared, and also get the first VarDecl that tracked object
2352 // is stored to.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002353 ExplodedNode<GRState>* AllocNode = 0;
Ted Kremenek60dbad82008-09-03 03:06:11 +00002354 store::Binding FirstBinding;
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002355
2356 llvm::tie(AllocNode, FirstBinding) =
2357 GetAllocationSite(&BR.getStateManager(), EndN, Sym);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002358
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002359 // Get the allocate site.
2360 assert (AllocNode);
2361 Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002362
Ted Kremeneke28565b2008-05-05 18:50:19 +00002363 SourceManager& SMgr = BR.getContext().getSourceManager();
2364 unsigned AllocLine = SMgr.getLogicalLineNumber(FirstStmt->getLocStart());
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002365
Ted Kremeneke28565b2008-05-05 18:50:19 +00002366 // Get the leak site. We may have multiple ExplodedNodes (one with the
2367 // leak) that occur on the same line number; if the node with the leak
2368 // has any immediate predecessor nodes with the same line number, find
2369 // any transitive-successors that have a different statement and use that
2370 // line number instead. This avoids emiting a diagnostic like:
2371 //
2372 // // 'y' is leaked.
2373 // int x = foo(y);
2374 //
2375 // instead we want:
2376 //
2377 // int x = foo(y);
2378 // // 'y' is leaked.
2379
2380 Stmt* S = getStmt(BR); // This is the statement where the leak occured.
2381 assert (S);
2382 unsigned EndLine = SMgr.getLogicalLineNumber(S->getLocStart());
2383
2384 // Look in the *trimmed* graph at the immediate predecessor of EndN. Does
2385 // it occur on the same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002386 PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above;
Ted Kremeneke28565b2008-05-05 18:50:19 +00002387
2388 assert (!EndN->pred_empty()); // Not possible to have 0 predecessors.
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002389 ExplodedNode<GRState> *Pred = *(EndN->pred_begin());
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002390 ProgramPoint PredPos = Pred->getLocation();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002391
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002392 if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) {
Ted Kremeneke28565b2008-05-05 18:50:19 +00002393
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002394 Stmt* SPred = PredPS->getStmt();
Ted Kremeneke28565b2008-05-05 18:50:19 +00002395
2396 // Predecessor at same line?
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002397 if (SMgr.getLogicalLineNumber(SPred->getLocStart()) != EndLine) {
2398 Hint = PathDiagnosticPiece::Below;
2399 S = SPred;
2400 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002401 }
Ted Kremeneke28565b2008-05-05 18:50:19 +00002402
2403 // Generate the diagnostic.
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002404 FullSourceLoc L( S->getLocStart(), SMgr);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002405 std::ostringstream os;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002406
Ted Kremeneke28565b2008-05-05 18:50:19 +00002407 os << "Object allocated on line " << AllocLine;
Ted Kremeneke92c1b22008-05-02 20:53:50 +00002408
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002409 if (FirstBinding)
2410 os << " and stored into '"
2411 << BR.getStateManager().BindingAsString(FirstBinding) << '\'';
Ted Kremenekce48e002008-05-05 17:53:17 +00002412 os << " is no longer referenced after this point and has a retain count of +"
2413 << RetCount << " (object leaked).";
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002414
Ted Kremeneka22cc2f2008-05-06 23:07:13 +00002415 return new PathDiagnosticPiece(L, os.str(), Hint);
Ted Kremenekc9fa2f72008-05-01 23:13:35 +00002416}
2417
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002418void UseAfterRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002419
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002420 for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
2421 E = TF.use_after_end(); I != E; ++I) {
2422
Ted Kremenek8dd56462008-04-18 03:39:05 +00002423 CFRefReport report(*this, I->first, I->second.second);
2424 report.addRange(I->second.first->getSourceRange());
Ted Kremenek75840e12008-04-18 01:56:37 +00002425 BR.EmitWarning(report);
Ted Kremenekfa34b332008-04-09 01:10:13 +00002426 }
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002427}
2428
2429void BadRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenekfa34b332008-04-09 01:10:13 +00002430
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002431 for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
2432 E = TF.bad_release_end(); I != E; ++I) {
2433
Ted Kremenek8dd56462008-04-18 03:39:05 +00002434 CFRefReport report(*this, I->first, I->second.second);
2435 report.addRange(I->second.first->getSourceRange());
2436 BR.EmitWarning(report);
Ted Kremenek05cbe1a2008-04-09 23:49:11 +00002437 }
2438}
Ted Kremenekfa34b332008-04-09 01:10:13 +00002439
Ted Kremenek989d5192008-04-17 23:43:50 +00002440void Leak::EmitWarnings(BugReporter& BR) {
2441
2442 for (CFRefCount::leaks_iterator I = TF.leaks_begin(),
2443 E = TF.leaks_end(); I != E; ++I) {
2444
Ted Kremenek8dd56462008-04-18 03:39:05 +00002445 std::vector<SymbolID>& SymV = *(I->second);
2446 unsigned n = SymV.size();
2447
2448 for (unsigned i = 0; i < n; ++i) {
2449 CFRefReport report(*this, I->first, SymV[i]);
2450 BR.EmitWarning(report);
2451 }
Ted Kremenek989d5192008-04-17 23:43:50 +00002452 }
2453}
2454
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002455void Leak::GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes) {
Ted Kremenekcb612922008-04-18 19:23:43 +00002456 for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end();
2457 I!=E; ++I)
2458 Nodes.push_back(I->first);
2459}
2460
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002461bool Leak::isCached(BugReport& R) {
2462
2463 // Most bug reports are cached at the location where they occured.
2464 // With leaks, we want to unique them by the location where they were
2465 // allocated, and only report only a single path.
2466
2467 SymbolID Sym = static_cast<CFRefReport&>(R).getSymbol();
2468
Ted Kremenek4adc81e2008-08-13 04:27:00 +00002469 ExplodedNode<GRState>* AllocNode =
Ted Kremenek2bc39c62008-08-29 00:47:32 +00002470 GetAllocationSite(0, R.getEndNode(), Sym).first;
Ted Kremenek6ed9afc2008-05-16 18:33:44 +00002471
2472 if (!AllocNode)
2473 return false;
2474
2475 return BugTypeCacheLocation::isCached(AllocNode->getLocation());
2476}
2477
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002478//===----------------------------------------------------------------------===//
Ted Kremenekd71ed262008-04-10 22:16:52 +00002479// Transfer function creation for external clients.
Ted Kremenek6b3a0f72008-03-11 06:39:11 +00002480//===----------------------------------------------------------------------===//
2481
Ted Kremenek072192b2008-04-30 23:47:44 +00002482GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
2483 const LangOptions& lopts) {
Ted Kremenek78d46242008-07-22 16:21:24 +00002484 return new CFRefCount(Ctx, GCEnabled, lopts);
Ted Kremenek3ea0b6a2008-04-10 22:58:08 +00002485}