]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/StaticAnalyzer/Core/CFRefCount.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / StaticAnalyzer / Core / CFRefCount.cpp
1 // CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
2 //
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 //
10 //  This file defines the methods for CFRefCount, which implements
11 //  a reference count checker for Core Foundation (Mac OS X).
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/StaticAnalyzer/Core/Checker.h"
16 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/StmtVisitor.h"
21 #include "clang/Basic/LangOptions.h"
22 #include "clang/Basic/SourceManager.h"
23 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
24 #include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h"
25 #include "clang/StaticAnalyzer/Checkers/LocalCheckers.h"
26 #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngineBuilders.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h"
29 #include "clang/StaticAnalyzer/Core/PathSensitive/TransferFuncs.h"
30 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
31 #include "llvm/ADT/DenseMap.h"
32 #include "llvm/ADT/FoldingSet.h"
33 #include "llvm/ADT/ImmutableList.h"
34 #include "llvm/ADT/ImmutableMap.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/ADT/StringExtras.h"
37 #include <stdarg.h>
38
39 using namespace clang;
40 using namespace ento;
41 using llvm::StringRef;
42 using llvm::StrInStrNoCase;
43
44 namespace {
45 class InstanceReceiver {
46   ObjCMessage Msg;
47   const LocationContext *LC;
48 public:
49   InstanceReceiver() : LC(0) { }
50   InstanceReceiver(const ObjCMessage &msg,
51                    const LocationContext *lc = 0) : Msg(msg), LC(lc) {}
52
53   bool isValid() const {
54     return Msg.isValid() && Msg.isInstanceMessage();
55   }
56   operator bool() const {
57     return isValid();
58   }
59
60   SVal getSValAsScalarOrLoc(const GRState *state) {
61     assert(isValid());
62     // We have an expression for the receiver?  Fetch the value
63     // of that expression.
64     if (const Expr *Ex = Msg.getInstanceReceiver())
65       return state->getSValAsScalarOrLoc(Ex);
66
67     // Otherwise we are sending a message to super.  In this case the
68     // object reference is the same as 'self'.
69     if (const ImplicitParamDecl *SelfDecl = LC->getSelfDecl())
70       return state->getSVal(state->getRegion(SelfDecl, LC));
71
72     return UnknownVal();
73   }
74
75   SourceRange getSourceRange() const {
76     assert(isValid());
77     if (const Expr *Ex = Msg.getInstanceReceiver())
78       return Ex->getSourceRange();
79
80     // Otherwise we are sending a message to super.
81     SourceLocation L = Msg.getSuperLoc();
82     assert(L.isValid());
83     return SourceRange(L, L);
84   }
85 };
86 }
87
88 static const ObjCMethodDecl*
89 ResolveToInterfaceMethodDecl(const ObjCMethodDecl *MD) {
90   const ObjCInterfaceDecl *ID = MD->getClassInterface();
91
92   return MD->isInstanceMethod()
93          ? ID->lookupInstanceMethod(MD->getSelector())
94          : ID->lookupClassMethod(MD->getSelector());
95 }
96
97 namespace {
98 class GenericNodeBuilderRefCount {
99   StmtNodeBuilder *SNB;
100   const Stmt *S;
101   const void *tag;
102   EndOfFunctionNodeBuilder *ENB;
103 public:
104   GenericNodeBuilderRefCount(StmtNodeBuilder &snb, const Stmt *s,
105                      const void *t)
106   : SNB(&snb), S(s), tag(t), ENB(0) {}
107
108   GenericNodeBuilderRefCount(EndOfFunctionNodeBuilder &enb)
109   : SNB(0), S(0), tag(0), ENB(&enb) {}
110
111   ExplodedNode *MakeNode(const GRState *state, ExplodedNode *Pred) {
112     if (SNB)
113       return SNB->generateNode(PostStmt(S, Pred->getLocationContext(), tag),
114                                state, Pred);
115
116     assert(ENB);
117     return ENB->generateNode(state, Pred);
118   }
119 };
120 } // end anonymous namespace
121
122 //===----------------------------------------------------------------------===//
123 // Primitives used for constructing summaries for function/method calls.
124 //===----------------------------------------------------------------------===//
125
126 /// ArgEffect is used to summarize a function/method call's effect on a
127 /// particular argument.
128 enum ArgEffect { Autorelease, Dealloc, DecRef, DecRefMsg, DoNothing,
129                  DecRefBridgedTransfered,
130                  DoNothingByRef, IncRefMsg, IncRef, MakeCollectable, MayEscape,
131                  NewAutoreleasePool, SelfOwn, StopTracking };
132
133 namespace llvm {
134 template <> struct FoldingSetTrait<ArgEffect> {
135 static inline void Profile(const ArgEffect X, FoldingSetNodeID& ID) {
136   ID.AddInteger((unsigned) X);
137 }
138 };
139 } // end llvm namespace
140
141 /// ArgEffects summarizes the effects of a function/method call on all of
142 /// its arguments.
143 typedef llvm::ImmutableMap<unsigned,ArgEffect> ArgEffects;
144
145 namespace {
146
147 ///  RetEffect is used to summarize a function/method call's behavior with
148 ///  respect to its return value.
149 class RetEffect {
150 public:
151   enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
152               NotOwnedSymbol, GCNotOwnedSymbol, ARCNotOwnedSymbol,
153               ReceiverAlias,
154               OwnedWhenTrackedReceiver };
155
156   enum ObjKind { CF, ObjC, AnyObj };
157
158 private:
159   Kind K;
160   ObjKind O;
161   unsigned index;
162
163   RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {}
164   RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {}
165
166 public:
167   Kind getKind() const { return K; }
168
169   ObjKind getObjKind() const { return O; }
170
171   unsigned getIndex() const {
172     assert(getKind() == Alias);
173     return index;
174   }
175
176   bool isOwned() const {
177     return K == OwnedSymbol || K == OwnedAllocatedSymbol ||
178            K == OwnedWhenTrackedReceiver;
179   }
180
181   static RetEffect MakeOwnedWhenTrackedReceiver() {
182     return RetEffect(OwnedWhenTrackedReceiver, ObjC);
183   }
184
185   static RetEffect MakeAlias(unsigned Idx) {
186     return RetEffect(Alias, Idx);
187   }
188   static RetEffect MakeReceiverAlias() {
189     return RetEffect(ReceiverAlias);
190   }
191   static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) {
192     return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o);
193   }
194   static RetEffect MakeNotOwned(ObjKind o) {
195     return RetEffect(NotOwnedSymbol, o);
196   }
197   static RetEffect MakeGCNotOwned() {
198     return RetEffect(GCNotOwnedSymbol, ObjC);
199   }
200   static RetEffect MakeARCNotOwned() {
201     return RetEffect(ARCNotOwnedSymbol, ObjC);
202   }
203   static RetEffect MakeNoRet() {
204     return RetEffect(NoRet);
205   }
206 };
207
208 //===----------------------------------------------------------------------===//
209 // Reference-counting logic (typestate + counts).
210 //===----------------------------------------------------------------------===//
211
212 class RefVal {
213 public:
214   enum Kind {
215     Owned = 0, // Owning reference.
216     NotOwned,  // Reference is not owned by still valid (not freed).
217     Released,  // Object has been released.
218     ReturnedOwned, // Returned object passes ownership to caller.
219     ReturnedNotOwned, // Return object does not pass ownership to caller.
220     ERROR_START,
221     ErrorDeallocNotOwned, // -dealloc called on non-owned object.
222     ErrorDeallocGC, // Calling -dealloc with GC enabled.
223     ErrorUseAfterRelease, // Object used after released.
224     ErrorReleaseNotOwned, // Release of an object that was not owned.
225     ERROR_LEAK_START,
226     ErrorLeak,  // A memory leak due to excessive reference counts.
227     ErrorLeakReturned, // A memory leak due to the returning method not having
228                        // the correct naming conventions.
229     ErrorGCLeakReturned,
230     ErrorOverAutorelease,
231     ErrorReturnedNotOwned
232   };
233
234 private:
235   Kind kind;
236   RetEffect::ObjKind okind;
237   unsigned Cnt;
238   unsigned ACnt;
239   QualType T;
240
241   RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t)
242   : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {}
243
244 public:
245   Kind getKind() const { return kind; }
246
247   RetEffect::ObjKind getObjKind() const { return okind; }
248
249   unsigned getCount() const { return Cnt; }
250   unsigned getAutoreleaseCount() const { return ACnt; }
251   unsigned getCombinedCounts() const { return Cnt + ACnt; }
252   void clearCounts() { Cnt = 0; ACnt = 0; }
253   void setCount(unsigned i) { Cnt = i; }
254   void setAutoreleaseCount(unsigned i) { ACnt = i; }
255
256   QualType getType() const { return T; }
257
258   bool isOwned() const {
259     return getKind() == Owned;
260   }
261
262   bool isNotOwned() const {
263     return getKind() == NotOwned;
264   }
265
266   bool isReturnedOwned() const {
267     return getKind() == ReturnedOwned;
268   }
269
270   bool isReturnedNotOwned() const {
271     return getKind() == ReturnedNotOwned;
272   }
273
274   static RefVal makeOwned(RetEffect::ObjKind o, QualType t,
275                           unsigned Count = 1) {
276     return RefVal(Owned, o, Count, 0, t);
277   }
278
279   static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t,
280                              unsigned Count = 0) {
281     return RefVal(NotOwned, o, Count, 0, t);
282   }
283
284   // Comparison, profiling, and pretty-printing.
285
286   bool operator==(const RefVal& X) const {
287     return kind == X.kind && Cnt == X.Cnt && T == X.T && ACnt == X.ACnt;
288   }
289
290   RefVal operator-(size_t i) const {
291     return RefVal(getKind(), getObjKind(), getCount() - i,
292                   getAutoreleaseCount(), getType());
293   }
294
295   RefVal operator+(size_t i) const {
296     return RefVal(getKind(), getObjKind(), getCount() + i,
297                   getAutoreleaseCount(), getType());
298   }
299
300   RefVal operator^(Kind k) const {
301     return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(),
302                   getType());
303   }
304
305   RefVal autorelease() const {
306     return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1,
307                   getType());
308   }
309
310   void Profile(llvm::FoldingSetNodeID& ID) const {
311     ID.AddInteger((unsigned) kind);
312     ID.AddInteger(Cnt);
313     ID.AddInteger(ACnt);
314     ID.Add(T);
315   }
316
317   void print(llvm::raw_ostream& Out) const;
318 };
319
320 void RefVal::print(llvm::raw_ostream& Out) const {
321   if (!T.isNull())
322     Out << "Tracked Type:" << T.getAsString() << '\n';
323
324   switch (getKind()) {
325     default: assert(false);
326     case Owned: {
327       Out << "Owned";
328       unsigned cnt = getCount();
329       if (cnt) Out << " (+ " << cnt << ")";
330       break;
331     }
332
333     case NotOwned: {
334       Out << "NotOwned";
335       unsigned cnt = getCount();
336       if (cnt) Out << " (+ " << cnt << ")";
337       break;
338     }
339
340     case ReturnedOwned: {
341       Out << "ReturnedOwned";
342       unsigned cnt = getCount();
343       if (cnt) Out << " (+ " << cnt << ")";
344       break;
345     }
346
347     case ReturnedNotOwned: {
348       Out << "ReturnedNotOwned";
349       unsigned cnt = getCount();
350       if (cnt) Out << " (+ " << cnt << ")";
351       break;
352     }
353
354     case Released:
355       Out << "Released";
356       break;
357
358     case ErrorDeallocGC:
359       Out << "-dealloc (GC)";
360       break;
361
362     case ErrorDeallocNotOwned:
363       Out << "-dealloc (not-owned)";
364       break;
365
366     case ErrorLeak:
367       Out << "Leaked";
368       break;
369
370     case ErrorLeakReturned:
371       Out << "Leaked (Bad naming)";
372       break;
373
374     case ErrorGCLeakReturned:
375       Out << "Leaked (GC-ed at return)";
376       break;
377
378     case ErrorUseAfterRelease:
379       Out << "Use-After-Release [ERROR]";
380       break;
381
382     case ErrorReleaseNotOwned:
383       Out << "Release of Not-Owned [ERROR]";
384       break;
385
386     case RefVal::ErrorOverAutorelease:
387       Out << "Over autoreleased";
388       break;
389
390     case RefVal::ErrorReturnedNotOwned:
391       Out << "Non-owned object returned instead of owned";
392       break;
393   }
394
395   if (ACnt) {
396     Out << " [ARC +" << ACnt << ']';
397   }
398 }
399 } //end anonymous namespace
400
401 //===----------------------------------------------------------------------===//
402 // RefBindings - State used to track object reference counts.
403 //===----------------------------------------------------------------------===//
404
405 typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings;
406
407 namespace clang {
408 namespace ento {
409   template<>
410   struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> {
411     static void* GDMIndex() {
412       static int RefBIndex = 0;
413       return &RefBIndex;
414     }
415   };
416 }
417 }
418
419 //===----------------------------------------------------------------------===//
420 // Summaries
421 //===----------------------------------------------------------------------===//
422
423 namespace {
424 class RetainSummary {
425   /// Args - an ordered vector of (index, ArgEffect) pairs, where index
426   ///  specifies the argument (starting from 0).  This can be sparsely
427   ///  populated; arguments with no entry in Args use 'DefaultArgEffect'.
428   ArgEffects Args;
429
430   /// DefaultArgEffect - The default ArgEffect to apply to arguments that
431   ///  do not have an entry in Args.
432   ArgEffect   DefaultArgEffect;
433
434   /// Receiver - If this summary applies to an Objective-C message expression,
435   ///  this is the effect applied to the state of the receiver.
436   ArgEffect   Receiver;
437
438   /// Ret - The effect on the return value.  Used to indicate if the
439   ///  function/method call returns a new tracked symbol, returns an
440   ///  alias of one of the arguments in the call, and so on.
441   RetEffect   Ret;
442
443   /// EndPath - Indicates that execution of this method/function should
444   ///  terminate the simulation of a path.
445   bool EndPath;
446
447 public:
448   RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff,
449                 ArgEffect ReceiverEff, bool endpath = false)
450     : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
451       EndPath(endpath) {}
452
453   /// getArg - Return the argument effect on the argument specified by
454   ///  idx (starting from 0).
455   ArgEffect getArg(unsigned idx) const {
456     if (const ArgEffect *AE = Args.lookup(idx))
457       return *AE;
458
459     return DefaultArgEffect;
460   }
461   
462   void addArg(ArgEffects::Factory &af, unsigned idx, ArgEffect e) {
463     Args = af.add(Args, idx, e);
464   }
465
466   /// setDefaultArgEffect - Set the default argument effect.
467   void setDefaultArgEffect(ArgEffect E) {
468     DefaultArgEffect = E;
469   }
470
471   /// getRetEffect - Returns the effect on the return value of the call.
472   RetEffect getRetEffect() const { return Ret; }
473
474   /// setRetEffect - Set the effect of the return value of the call.
475   void setRetEffect(RetEffect E) { Ret = E; }
476
477   /// isEndPath - Returns true if executing the given method/function should
478   ///  terminate the path.
479   bool isEndPath() const { return EndPath; }
480
481   
482   /// Sets the effect on the receiver of the message.
483   void setReceiverEffect(ArgEffect e) { Receiver = e; }
484   
485   /// getReceiverEffect - Returns the effect on the receiver of the call.
486   ///  This is only meaningful if the summary applies to an ObjCMessageExpr*.
487   ArgEffect getReceiverEffect() const { return Receiver; }
488 };
489 } // end anonymous namespace
490
491 //===----------------------------------------------------------------------===//
492 // Data structures for constructing summaries.
493 //===----------------------------------------------------------------------===//
494
495 namespace {
496 class ObjCSummaryKey {
497   IdentifierInfo* II;
498   Selector S;
499 public:
500   ObjCSummaryKey(IdentifierInfo* ii, Selector s)
501     : II(ii), S(s) {}
502
503   ObjCSummaryKey(const ObjCInterfaceDecl* d, Selector s)
504     : II(d ? d->getIdentifier() : 0), S(s) {}
505
506   ObjCSummaryKey(const ObjCInterfaceDecl* d, IdentifierInfo *ii, Selector s)
507     : II(d ? d->getIdentifier() : ii), S(s) {}
508
509   ObjCSummaryKey(Selector s)
510     : II(0), S(s) {}
511
512   IdentifierInfo* getIdentifier() const { return II; }
513   Selector getSelector() const { return S; }
514 };
515 }
516
517 namespace llvm {
518 template <> struct DenseMapInfo<ObjCSummaryKey> {
519   static inline ObjCSummaryKey getEmptyKey() {
520     return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
521                           DenseMapInfo<Selector>::getEmptyKey());
522   }
523
524   static inline ObjCSummaryKey getTombstoneKey() {
525     return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
526                           DenseMapInfo<Selector>::getTombstoneKey());
527   }
528
529   static unsigned getHashValue(const ObjCSummaryKey &V) {
530     return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
531             & 0x88888888)
532         | (DenseMapInfo<Selector>::getHashValue(V.getSelector())
533             & 0x55555555);
534   }
535
536   static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
537     return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
538                                                   RHS.getIdentifier()) &&
539            DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
540                                            RHS.getSelector());
541   }
542
543 };
544 template <>
545 struct isPodLike<ObjCSummaryKey> { static const bool value = true; };
546 } // end llvm namespace
547
548 namespace {
549 class ObjCSummaryCache {
550   typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
551   MapTy M;
552 public:
553   ObjCSummaryCache() {}
554
555   RetainSummary* find(const ObjCInterfaceDecl* D, IdentifierInfo *ClsName,
556                 Selector S) {
557     // Lookup the method using the decl for the class @interface.  If we
558     // have no decl, lookup using the class name.
559     return D ? find(D, S) : find(ClsName, S);
560   }
561
562   RetainSummary* find(const ObjCInterfaceDecl* D, Selector S) {
563     // Do a lookup with the (D,S) pair.  If we find a match return
564     // the iterator.
565     ObjCSummaryKey K(D, S);
566     MapTy::iterator I = M.find(K);
567
568     if (I != M.end() || !D)
569       return I->second;
570
571     // Walk the super chain.  If we find a hit with a parent, we'll end
572     // up returning that summary.  We actually allow that key (null,S), as
573     // we cache summaries for the null ObjCInterfaceDecl* to allow us to
574     // generate initial summaries without having to worry about NSObject
575     // being declared.
576     // FIXME: We may change this at some point.
577     for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
578       if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
579         break;
580
581       if (!C)
582         return NULL;
583     }
584
585     // Cache the summary with original key to make the next lookup faster
586     // and return the iterator.
587     RetainSummary *Summ = I->second;
588     M[K] = Summ;
589     return Summ;
590   }
591
592   RetainSummary* find(IdentifierInfo* II, Selector S) {
593     // FIXME: Class method lookup.  Right now we dont' have a good way
594     // of going between IdentifierInfo* and the class hierarchy.
595     MapTy::iterator I = M.find(ObjCSummaryKey(II, S));
596
597     if (I == M.end())
598       I = M.find(ObjCSummaryKey(S));
599
600     return I == M.end() ? NULL : I->second;
601   }
602
603   RetainSummary*& operator[](ObjCSummaryKey K) {
604     return M[K];
605   }
606
607   RetainSummary*& operator[](Selector S) {
608     return M[ ObjCSummaryKey(S) ];
609   }
610 };
611 } // end anonymous namespace
612
613 //===----------------------------------------------------------------------===//
614 // Data structures for managing collections of summaries.
615 //===----------------------------------------------------------------------===//
616
617 namespace {
618 class RetainSummaryManager {
619
620   //==-----------------------------------------------------------------==//
621   //  Typedefs.
622   //==-----------------------------------------------------------------==//
623
624   typedef llvm::DenseMap<const FunctionDecl*, RetainSummary*>
625           FuncSummariesTy;
626
627   typedef ObjCSummaryCache ObjCMethodSummariesTy;
628
629   //==-----------------------------------------------------------------==//
630   //  Data.
631   //==-----------------------------------------------------------------==//
632
633   /// Ctx - The ASTContext object for the analyzed ASTs.
634   ASTContext& Ctx;
635
636   /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
637   ///  "CFDictionaryCreate".
638   IdentifierInfo* CFDictionaryCreateII;
639
640   /// GCEnabled - Records whether or not the analyzed code runs in GC mode.
641   const bool GCEnabled;
642
643   /// Records whether or not the analyzed code runs in ARC mode.
644   const bool ARCEnabled;
645
646   /// FuncSummaries - A map from FunctionDecls to summaries.
647   FuncSummariesTy FuncSummaries;
648
649   /// ObjCClassMethodSummaries - A map from selectors (for instance methods)
650   ///  to summaries.
651   ObjCMethodSummariesTy ObjCClassMethodSummaries;
652
653   /// ObjCMethodSummaries - A map from selectors to summaries.
654   ObjCMethodSummariesTy ObjCMethodSummaries;
655
656   /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
657   ///  and all other data used by the checker.
658   llvm::BumpPtrAllocator BPAlloc;
659
660   /// AF - A factory for ArgEffects objects.
661   ArgEffects::Factory AF;
662
663   /// ScratchArgs - A holding buffer for construct ArgEffects.
664   ArgEffects ScratchArgs;
665
666   /// ObjCAllocRetE - Default return effect for methods returning Objective-C
667   ///  objects.
668   RetEffect ObjCAllocRetE;
669
670   /// ObjCInitRetE - Default return effect for init methods returning
671   ///   Objective-C objects.
672   RetEffect ObjCInitRetE;
673
674   RetainSummary DefaultSummary;
675   RetainSummary* StopSummary;
676
677   //==-----------------------------------------------------------------==//
678   //  Methods.
679   //==-----------------------------------------------------------------==//
680
681   /// getArgEffects - Returns a persistent ArgEffects object based on the
682   ///  data in ScratchArgs.
683   ArgEffects getArgEffects();
684
685   enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
686
687 public:
688   RetEffect getObjAllocRetEffect() const { return ObjCAllocRetE; }
689
690   RetainSummary *getDefaultSummary() {
691     RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
692     return new (Summ) RetainSummary(DefaultSummary);
693   }
694
695   RetainSummary* getUnarySummary(const FunctionType* FT, UnaryFuncKind func);
696
697   RetainSummary* getCFSummaryCreateRule(const FunctionDecl* FD);
698   RetainSummary* getCFSummaryGetRule(const FunctionDecl* FD);
699   RetainSummary* getCFCreateGetRuleSummary(const FunctionDecl* FD, 
700                                            StringRef FName);
701
702   RetainSummary* getPersistentSummary(ArgEffects AE, RetEffect RetEff,
703                                       ArgEffect ReceiverEff = DoNothing,
704                                       ArgEffect DefaultEff = MayEscape,
705                                       bool isEndPath = false);
706
707   RetainSummary* getPersistentSummary(RetEffect RE,
708                                       ArgEffect ReceiverEff = DoNothing,
709                                       ArgEffect DefaultEff = MayEscape) {
710     return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
711   }
712
713   RetainSummary *getPersistentStopSummary() {
714     if (StopSummary)
715       return StopSummary;
716
717     StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
718                                        StopTracking, StopTracking);
719
720     return StopSummary;
721   }
722
723   RetainSummary *getInitMethodSummary(QualType RetTy);
724
725   void InitializeClassMethodSummaries();
726   void InitializeMethodSummaries();
727 private:
728   void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
729     ObjCClassMethodSummaries[S] = Summ;
730   }
731
732   void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
733     ObjCMethodSummaries[S] = Summ;
734   }
735
736   void addClassMethSummary(const char* Cls, const char* nullaryName,
737                            RetainSummary *Summ) {
738     IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
739     Selector S = GetNullarySelector(nullaryName, Ctx);
740     ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)]  = Summ;
741   }
742
743   void addInstMethSummary(const char* Cls, const char* nullaryName,
744                           RetainSummary *Summ) {
745     IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
746     Selector S = GetNullarySelector(nullaryName, Ctx);
747     ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)]  = Summ;
748   }
749
750   Selector generateSelector(va_list argp) {
751     llvm::SmallVector<IdentifierInfo*, 10> II;
752
753     while (const char* s = va_arg(argp, const char*))
754       II.push_back(&Ctx.Idents.get(s));
755
756     return Ctx.Selectors.getSelector(II.size(), &II[0]);
757   }
758
759   void addMethodSummary(IdentifierInfo *ClsII, ObjCMethodSummariesTy& Summaries,
760                         RetainSummary* Summ, va_list argp) {
761     Selector S = generateSelector(argp);
762     Summaries[ObjCSummaryKey(ClsII, S)] = Summ;
763   }
764
765   void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
766     va_list argp;
767     va_start(argp, Summ);
768     addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
769     va_end(argp);
770   }
771
772   void addClsMethSummary(const char* Cls, RetainSummary* Summ, ...) {
773     va_list argp;
774     va_start(argp, Summ);
775     addMethodSummary(&Ctx.Idents.get(Cls),ObjCClassMethodSummaries, Summ, argp);
776     va_end(argp);
777   }
778
779   void addClsMethSummary(IdentifierInfo *II, RetainSummary* Summ, ...) {
780     va_list argp;
781     va_start(argp, Summ);
782     addMethodSummary(II, ObjCClassMethodSummaries, Summ, argp);
783     va_end(argp);
784   }
785
786   void addPanicSummary(const char* Cls, ...) {
787     RetainSummary* Summ = getPersistentSummary(AF.getEmptyMap(),
788                                                RetEffect::MakeNoRet(),
789                                                DoNothing,  DoNothing, true);
790     va_list argp;
791     va_start (argp, Cls);
792     addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp);
793     va_end(argp);
794   }
795
796 public:
797
798   RetainSummaryManager(ASTContext& ctx, bool gcenabled, bool usesARC)
799    : Ctx(ctx),
800      CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
801      GCEnabled(gcenabled),
802      ARCEnabled(usesARC),
803      AF(BPAlloc), ScratchArgs(AF.getEmptyMap()),
804      ObjCAllocRetE(gcenabled
805                     ? RetEffect::MakeGCNotOwned()
806                     : (usesARC ? RetEffect::MakeARCNotOwned()
807                                : RetEffect::MakeOwned(RetEffect::ObjC, true))),
808      ObjCInitRetE(gcenabled 
809                     ? RetEffect::MakeGCNotOwned()
810                     : (usesARC ? RetEffect::MakeARCNotOwned()
811                                : RetEffect::MakeOwnedWhenTrackedReceiver())),
812      DefaultSummary(AF.getEmptyMap() /* per-argument effects (none) */,
813                     RetEffect::MakeNoRet() /* return effect */,
814                     MayEscape, /* default argument effect */
815                     DoNothing /* receiver effect */),
816      StopSummary(0) {
817
818     InitializeClassMethodSummaries();
819     InitializeMethodSummaries();
820   }
821
822   ~RetainSummaryManager();
823
824   RetainSummary* getSummary(const FunctionDecl* FD);
825
826   RetainSummary *getInstanceMethodSummary(const ObjCMessage &msg,
827                                           const GRState *state,
828                                           const LocationContext *LC);
829
830   RetainSummary* getInstanceMethodSummary(const ObjCMessage &msg,
831                                           const ObjCInterfaceDecl* ID) {
832     return getInstanceMethodSummary(msg.getSelector(), 0,
833                             ID, msg.getMethodDecl(), msg.getType(Ctx));
834   }
835
836   RetainSummary* getInstanceMethodSummary(Selector S, IdentifierInfo *ClsName,
837                                           const ObjCInterfaceDecl* ID,
838                                           const ObjCMethodDecl *MD,
839                                           QualType RetTy);
840
841   RetainSummary *getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
842                                        const ObjCInterfaceDecl *ID,
843                                        const ObjCMethodDecl *MD,
844                                        QualType RetTy);
845
846   RetainSummary *getClassMethodSummary(const ObjCMessage &msg) {
847     const ObjCInterfaceDecl *Class = 0;
848     if (!msg.isInstanceMessage())
849       Class = msg.getReceiverInterface();
850
851     return getClassMethodSummary(msg.getSelector(),
852                                  Class? Class->getIdentifier() : 0,
853                                  Class,
854                                  msg.getMethodDecl(), msg.getType(Ctx));
855   }
856
857   /// getMethodSummary - This version of getMethodSummary is used to query
858   ///  the summary for the current method being analyzed.
859   RetainSummary *getMethodSummary(const ObjCMethodDecl *MD) {
860     // FIXME: Eventually this should be unneeded.
861     const ObjCInterfaceDecl *ID = MD->getClassInterface();
862     Selector S = MD->getSelector();
863     IdentifierInfo *ClsName = ID->getIdentifier();
864     QualType ResultTy = MD->getResultType();
865
866     // Resolve the method decl last.
867     if (const ObjCMethodDecl *InterfaceMD = ResolveToInterfaceMethodDecl(MD))
868       MD = InterfaceMD;
869
870     if (MD->isInstanceMethod())
871       return getInstanceMethodSummary(S, ClsName, ID, MD, ResultTy);
872     else
873       return getClassMethodSummary(S, ClsName, ID, MD, ResultTy);
874   }
875
876   RetainSummary* getCommonMethodSummary(const ObjCMethodDecl* MD,
877                                         Selector S, QualType RetTy);
878
879   void updateSummaryFromAnnotations(RetainSummary &Summ,
880                                     const ObjCMethodDecl *MD);
881
882   void updateSummaryFromAnnotations(RetainSummary &Summ,
883                                     const FunctionDecl *FD);
884
885   bool isGCEnabled() const { return GCEnabled; }
886
887   bool isARCEnabled() const { return ARCEnabled; }
888   
889   bool isARCorGCEnabled() const { return GCEnabled || ARCEnabled; }
890
891   RetainSummary *copySummary(RetainSummary *OldSumm) {
892     RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
893     new (Summ) RetainSummary(*OldSumm);
894     return Summ;
895   }
896 };
897
898 } // end anonymous namespace
899
900 //===----------------------------------------------------------------------===//
901 // Implementation of checker data structures.
902 //===----------------------------------------------------------------------===//
903
904 RetainSummaryManager::~RetainSummaryManager() {}
905
906 ArgEffects RetainSummaryManager::getArgEffects() {
907   ArgEffects AE = ScratchArgs;
908   ScratchArgs = AF.getEmptyMap();
909   return AE;
910 }
911
912 RetainSummary*
913 RetainSummaryManager::getPersistentSummary(ArgEffects AE, RetEffect RetEff,
914                                            ArgEffect ReceiverEff,
915                                            ArgEffect DefaultEff,
916                                            bool isEndPath) {
917   // Create the summary and return it.
918   RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
919   new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
920   return Summ;
921 }
922
923 //===----------------------------------------------------------------------===//
924 // Summary creation for functions (largely uses of Core Foundation).
925 //===----------------------------------------------------------------------===//
926
927 static bool isRetain(const FunctionDecl* FD, StringRef FName) {
928   return FName.endswith("Retain");
929 }
930
931 static bool isRelease(const FunctionDecl* FD, StringRef FName) {
932   return FName.endswith("Release");
933 }
934
935 RetainSummary* RetainSummaryManager::getSummary(const FunctionDecl* FD) {
936   // Look up a summary in our cache of FunctionDecls -> Summaries.
937   FuncSummariesTy::iterator I = FuncSummaries.find(FD);
938   if (I != FuncSummaries.end())
939     return I->second;
940
941   // No summary?  Generate one.
942   RetainSummary *S = 0;
943
944   do {
945     // We generate "stop" summaries for implicitly defined functions.
946     if (FD->isImplicit()) {
947       S = getPersistentStopSummary();
948       break;
949     }
950     // For C++ methods, generate an implicit "stop" summary as well.  We
951     // can relax this once we have a clear policy for C++ methods and
952     // ownership attributes.
953     if (isa<CXXMethodDecl>(FD)) {
954       S = getPersistentStopSummary();
955       break;
956     }
957
958     // [PR 3337] Use 'getAs<FunctionType>' to strip away any typedefs on the
959     // function's type.
960     const FunctionType* FT = FD->getType()->getAs<FunctionType>();
961     const IdentifierInfo *II = FD->getIdentifier();
962     if (!II)
963       break;
964
965     StringRef FName = II->getName();
966
967     // Strip away preceding '_'.  Doing this here will effect all the checks
968     // down below.
969     FName = FName.substr(FName.find_first_not_of('_'));
970
971     // Inspect the result type.
972     QualType RetTy = FT->getResultType();
973
974     // FIXME: This should all be refactored into a chain of "summary lookup"
975     //  filters.
976     assert(ScratchArgs.isEmpty());
977
978     if (FName == "pthread_create") {
979       // Part of: <rdar://problem/7299394>.  This will be addressed
980       // better with IPA.
981       S = getPersistentStopSummary();
982     } else if (FName == "NSMakeCollectable") {
983       // Handle: id NSMakeCollectable(CFTypeRef)
984       S = (RetTy->isObjCIdType())
985           ? getUnarySummary(FT, cfmakecollectable)
986           : getPersistentStopSummary();
987     } else if (FName == "IOBSDNameMatching" ||
988                FName == "IOServiceMatching" ||
989                FName == "IOServiceNameMatching" ||
990                FName == "IORegistryEntryIDMatching" ||
991                FName == "IOOpenFirmwarePathMatching") {
992       // Part of <rdar://problem/6961230>. (IOKit)
993       // This should be addressed using a API table.
994       S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
995                                DoNothing, DoNothing);
996     } else if (FName == "IOServiceGetMatchingService" ||
997                FName == "IOServiceGetMatchingServices") {
998       // FIXES: <rdar://problem/6326900>
999       // This should be addressed using a API table.  This strcmp is also
1000       // a little gross, but there is no need to super optimize here.
1001       ScratchArgs = AF.add(ScratchArgs, 1, DecRef);
1002       S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
1003     } else if (FName == "IOServiceAddNotification" ||
1004                FName == "IOServiceAddMatchingNotification") {
1005       // Part of <rdar://problem/6961230>. (IOKit)
1006       // This should be addressed using a API table.
1007       ScratchArgs = AF.add(ScratchArgs, 2, DecRef);
1008       S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
1009     } else if (FName == "CVPixelBufferCreateWithBytes") {
1010       // FIXES: <rdar://problem/7283567>
1011       // Eventually this can be improved by recognizing that the pixel
1012       // buffer passed to CVPixelBufferCreateWithBytes is released via
1013       // a callback and doing full IPA to make sure this is done correctly.
1014       // FIXME: This function has an out parameter that returns an
1015       // allocated object.
1016       ScratchArgs = AF.add(ScratchArgs, 7, StopTracking);
1017       S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
1018     } else if (FName == "CGBitmapContextCreateWithData") {
1019       // FIXES: <rdar://problem/7358899>
1020       // Eventually this can be improved by recognizing that 'releaseInfo'
1021       // passed to CGBitmapContextCreateWithData is released via
1022       // a callback and doing full IPA to make sure this is done correctly.
1023       ScratchArgs = AF.add(ScratchArgs, 8, StopTracking);
1024       S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true),
1025                                DoNothing, DoNothing);
1026     } else if (FName == "CVPixelBufferCreateWithPlanarBytes") {
1027       // FIXES: <rdar://problem/7283567>
1028       // Eventually this can be improved by recognizing that the pixel
1029       // buffer passed to CVPixelBufferCreateWithPlanarBytes is released
1030       // via a callback and doing full IPA to make sure this is done
1031       // correctly.
1032       ScratchArgs = AF.add(ScratchArgs, 12, StopTracking);
1033       S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
1034     }
1035
1036     // Did we get a summary?
1037     if (S)
1038       break;
1039
1040     // Enable this code once the semantics of NSDeallocateObject are resolved
1041     // for GC.  <rdar://problem/6619988>
1042 #if 0
1043     // Handle: NSDeallocateObject(id anObject);
1044     // This method does allow 'nil' (although we don't check it now).
1045     if (strcmp(FName, "NSDeallocateObject") == 0) {
1046       return RetTy == Ctx.VoidTy
1047         ? getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, Dealloc)
1048         : getPersistentStopSummary();
1049     }
1050 #endif
1051
1052     if (RetTy->isPointerType()) {
1053       // For CoreFoundation ('CF') types.
1054       if (cocoa::isRefType(RetTy, "CF", FName)) {
1055         if (isRetain(FD, FName))
1056           S = getUnarySummary(FT, cfretain);
1057         else if (FName.find("MakeCollectable") != StringRef::npos)
1058           S = getUnarySummary(FT, cfmakecollectable);
1059         else
1060           S = getCFCreateGetRuleSummary(FD, FName);
1061
1062         break;
1063       }
1064
1065       // For CoreGraphics ('CG') types.
1066       if (cocoa::isRefType(RetTy, "CG", FName)) {
1067         if (isRetain(FD, FName))
1068           S = getUnarySummary(FT, cfretain);
1069         else
1070           S = getCFCreateGetRuleSummary(FD, FName);
1071
1072         break;
1073       }
1074
1075       // For the Disk Arbitration API (DiskArbitration/DADisk.h)
1076       if (cocoa::isRefType(RetTy, "DADisk") ||
1077           cocoa::isRefType(RetTy, "DADissenter") ||
1078           cocoa::isRefType(RetTy, "DASessionRef")) {
1079         S = getCFCreateGetRuleSummary(FD, FName);
1080         break;
1081       }
1082
1083       break;
1084     }
1085
1086     // Check for release functions, the only kind of functions that we care
1087     // about that don't return a pointer type.
1088     if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) {
1089       // Test for 'CGCF'.
1090       FName = FName.substr(FName.startswith("CGCF") ? 4 : 2);
1091
1092       if (isRelease(FD, FName))
1093         S = getUnarySummary(FT, cfrelease);
1094       else {
1095         assert (ScratchArgs.isEmpty());
1096         // Remaining CoreFoundation and CoreGraphics functions.
1097         // We use to assume that they all strictly followed the ownership idiom
1098         // and that ownership cannot be transferred.  While this is technically
1099         // correct, many methods allow a tracked object to escape.  For example:
1100         //
1101         //   CFMutableDictionaryRef x = CFDictionaryCreateMutable(...);
1102         //   CFDictionaryAddValue(y, key, x);
1103         //   CFRelease(x);
1104         //   ... it is okay to use 'x' since 'y' has a reference to it
1105         //
1106         // We handle this and similar cases with the follow heuristic.  If the
1107         // function name contains "InsertValue", "SetValue", "AddValue",
1108         // "AppendValue", or "SetAttribute", then we assume that arguments may
1109         // "escape."  This means that something else holds on to the object,
1110         // allowing it be used even after its local retain count drops to 0.
1111         ArgEffect E = (StrInStrNoCase(FName, "InsertValue") != StringRef::npos||
1112                        StrInStrNoCase(FName, "AddValue") != StringRef::npos ||
1113                        StrInStrNoCase(FName, "SetValue") != StringRef::npos ||
1114                        StrInStrNoCase(FName, "AppendValue") != StringRef::npos||
1115                        StrInStrNoCase(FName, "SetAttribute") != StringRef::npos)
1116                       ? MayEscape : DoNothing;
1117
1118         S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E);
1119       }
1120     }
1121   }
1122   while (0);
1123
1124   if (!S)
1125     S = getDefaultSummary();
1126
1127   // Annotations override defaults.
1128   assert(S);
1129   updateSummaryFromAnnotations(*S, FD);
1130
1131   FuncSummaries[FD] = S;
1132   return S;
1133 }
1134
1135 RetainSummary*
1136 RetainSummaryManager::getCFCreateGetRuleSummary(const FunctionDecl* FD,
1137                                                 StringRef FName) {
1138   if (coreFoundation::followsCreateRule(FName))
1139     return getCFSummaryCreateRule(FD);
1140
1141   return getCFSummaryGetRule(FD);
1142 }
1143
1144 RetainSummary*
1145 RetainSummaryManager::getUnarySummary(const FunctionType* FT,
1146                                       UnaryFuncKind func) {
1147
1148   // Sanity check that this is *really* a unary function.  This can
1149   // happen if people do weird things.
1150   const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT);
1151   if (!FTP || FTP->getNumArgs() != 1)
1152     return getPersistentStopSummary();
1153
1154   assert (ScratchArgs.isEmpty());
1155
1156   switch (func) {
1157     case cfretain: {
1158       ScratchArgs = AF.add(ScratchArgs, 0, IncRef);
1159       return getPersistentSummary(RetEffect::MakeAlias(0),
1160                                   DoNothing, DoNothing);
1161     }
1162
1163     case cfrelease: {
1164       ScratchArgs = AF.add(ScratchArgs, 0, DecRef);
1165       return getPersistentSummary(RetEffect::MakeNoRet(),
1166                                   DoNothing, DoNothing);
1167     }
1168
1169     case cfmakecollectable: {
1170       ScratchArgs = AF.add(ScratchArgs, 0, MakeCollectable);
1171       return getPersistentSummary(RetEffect::MakeAlias(0),DoNothing, DoNothing);
1172     }
1173
1174     default:
1175       assert (false && "Not a supported unary function.");
1176       return getDefaultSummary();
1177   }
1178 }
1179
1180 RetainSummary* 
1181 RetainSummaryManager::getCFSummaryCreateRule(const FunctionDecl* FD) {
1182   assert (ScratchArgs.isEmpty());
1183
1184   if (FD->getIdentifier() == CFDictionaryCreateII) {
1185     ScratchArgs = AF.add(ScratchArgs, 1, DoNothingByRef);
1186     ScratchArgs = AF.add(ScratchArgs, 2, DoNothingByRef);
1187   }
1188
1189   return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
1190 }
1191
1192 RetainSummary* 
1193 RetainSummaryManager::getCFSummaryGetRule(const FunctionDecl* FD) {
1194   assert (ScratchArgs.isEmpty());
1195   return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF),
1196                               DoNothing, DoNothing);
1197 }
1198
1199 //===----------------------------------------------------------------------===//
1200 // Summary creation for Selectors.
1201 //===----------------------------------------------------------------------===//
1202
1203 RetainSummary*
1204 RetainSummaryManager::getInitMethodSummary(QualType RetTy) {
1205   assert(ScratchArgs.isEmpty());
1206   // 'init' methods conceptually return a newly allocated object and claim
1207   // the receiver.
1208   if (cocoa::isCocoaObjectRef(RetTy) ||
1209       coreFoundation::isCFObjectRef(RetTy))
1210     return getPersistentSummary(ObjCInitRetE, DecRefMsg);
1211
1212   return getDefaultSummary();
1213 }
1214
1215 void
1216 RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1217                                                    const FunctionDecl *FD) {
1218   if (!FD)
1219     return;
1220
1221   // Effects on the parameters.
1222   unsigned parm_idx = 0;
1223   for (FunctionDecl::param_const_iterator pi = FD->param_begin(), 
1224          pe = FD->param_end(); pi != pe; ++pi, ++parm_idx) {
1225     const ParmVarDecl *pd = *pi;
1226     if (pd->getAttr<NSConsumedAttr>()) {
1227       if (!GCEnabled)
1228         Summ.addArg(AF, parm_idx, DecRef);      
1229     }
1230     else if(pd->getAttr<CFConsumedAttr>()) {
1231       Summ.addArg(AF, parm_idx, DecRef);      
1232     }   
1233   }
1234   
1235   QualType RetTy = FD->getResultType();
1236
1237   // Determine if there is a special return effect for this method.
1238   if (cocoa::isCocoaObjectRef(RetTy)) {
1239     if (FD->getAttr<NSReturnsRetainedAttr>()) {
1240       Summ.setRetEffect(ObjCAllocRetE);
1241     }
1242     else if (FD->getAttr<CFReturnsRetainedAttr>()) {
1243       Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
1244     }
1245     else if (FD->getAttr<NSReturnsNotRetainedAttr>()) {
1246       Summ.setRetEffect(RetEffect::MakeNotOwned(RetEffect::ObjC));
1247     }
1248     else if (FD->getAttr<CFReturnsNotRetainedAttr>()) {
1249       Summ.setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF));
1250     }
1251   }
1252   else if (RetTy->getAs<PointerType>()) {
1253     if (FD->getAttr<CFReturnsRetainedAttr>()) {
1254       Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
1255     }
1256     else if (FD->getAttr<CFReturnsNotRetainedAttr>()) {
1257       Summ.setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF));
1258     }
1259   }
1260 }
1261
1262 void
1263 RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ,
1264                                                   const ObjCMethodDecl *MD) {
1265   if (!MD)
1266     return;
1267
1268   bool isTrackedLoc = false;
1269
1270   // Effects on the receiver.
1271   if (MD->getAttr<NSConsumesSelfAttr>()) {
1272     if (!GCEnabled)
1273       Summ.setReceiverEffect(DecRefMsg);      
1274   }
1275   
1276   // Effects on the parameters.
1277   unsigned parm_idx = 0;
1278   for (ObjCMethodDecl::param_iterator pi=MD->param_begin(), pe=MD->param_end();
1279        pi != pe; ++pi, ++parm_idx) {
1280     const ParmVarDecl *pd = *pi;
1281     if (pd->getAttr<NSConsumedAttr>()) {
1282       if (!GCEnabled)
1283         Summ.addArg(AF, parm_idx, DecRef);      
1284     }
1285     else if(pd->getAttr<CFConsumedAttr>()) {
1286       Summ.addArg(AF, parm_idx, DecRef);      
1287     }   
1288   }
1289   
1290   // Determine if there is a special return effect for this method.
1291   if (cocoa::isCocoaObjectRef(MD->getResultType())) {
1292     if (MD->getAttr<NSReturnsRetainedAttr>()) {
1293       Summ.setRetEffect(ObjCAllocRetE);
1294       return;
1295     }
1296     if (MD->getAttr<NSReturnsNotRetainedAttr>()) {
1297       Summ.setRetEffect(RetEffect::MakeNotOwned(RetEffect::ObjC));
1298       return;
1299     }
1300
1301     isTrackedLoc = true;
1302   }
1303
1304   if (!isTrackedLoc)
1305     isTrackedLoc = MD->getResultType()->getAs<PointerType>() != NULL;
1306
1307   if (isTrackedLoc) {
1308     if (MD->getAttr<CFReturnsRetainedAttr>())
1309       Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true));
1310     else if (MD->getAttr<CFReturnsNotRetainedAttr>())
1311       Summ.setRetEffect(RetEffect::MakeNotOwned(RetEffect::CF));
1312   }
1313 }
1314
1315 RetainSummary*
1316 RetainSummaryManager::getCommonMethodSummary(const ObjCMethodDecl* MD,
1317                                              Selector S, QualType RetTy) {
1318
1319   if (MD) {
1320     // Scan the method decl for 'void*' arguments.  These should be treated
1321     // as 'StopTracking' because they are often used with delegates.
1322     // Delegates are a frequent form of false positives with the retain
1323     // count checker.
1324     unsigned i = 0;
1325     for (ObjCMethodDecl::param_iterator I = MD->param_begin(),
1326          E = MD->param_end(); I != E; ++I, ++i)
1327       if (ParmVarDecl *PD = *I) {
1328         QualType Ty = Ctx.getCanonicalType(PD->getType());
1329         if (Ty.getLocalUnqualifiedType() == Ctx.VoidPtrTy)
1330           ScratchArgs = AF.add(ScratchArgs, i, StopTracking);
1331       }
1332   }
1333
1334   // Any special effect for the receiver?
1335   ArgEffect ReceiverEff = DoNothing;
1336
1337   // If one of the arguments in the selector has the keyword 'delegate' we
1338   // should stop tracking the reference count for the receiver.  This is
1339   // because the reference count is quite possibly handled by a delegate
1340   // method.
1341   if (S.isKeywordSelector()) {
1342     const std::string &str = S.getAsString();
1343     assert(!str.empty());
1344     if (StrInStrNoCase(str, "delegate:") != StringRef::npos)
1345       ReceiverEff = StopTracking;
1346   }
1347
1348   // Look for methods that return an owned object.
1349   if (cocoa::isCocoaObjectRef(RetTy)) {
1350     // EXPERIMENTAL: assume the Cocoa conventions for all objects returned
1351     //  by instance methods.
1352     RetEffect E = cocoa::followsFundamentalRule(S, MD)
1353                   ? ObjCAllocRetE : RetEffect::MakeNotOwned(RetEffect::ObjC);
1354
1355     return getPersistentSummary(E, ReceiverEff, MayEscape);
1356   }
1357
1358   // Look for methods that return an owned core foundation object.
1359   if (coreFoundation::isCFObjectRef(RetTy)) {
1360     RetEffect E = cocoa::followsFundamentalRule(S, MD)
1361       ? RetEffect::MakeOwned(RetEffect::CF, true)
1362       : RetEffect::MakeNotOwned(RetEffect::CF);
1363
1364     return getPersistentSummary(E, ReceiverEff, MayEscape);
1365   }
1366
1367   if (ScratchArgs.isEmpty() && ReceiverEff == DoNothing)
1368     return getDefaultSummary();
1369
1370   return getPersistentSummary(RetEffect::MakeNoRet(), ReceiverEff, MayEscape);
1371 }
1372
1373 RetainSummary*
1374 RetainSummaryManager::getInstanceMethodSummary(const ObjCMessage &msg,
1375                                                const GRState *state,
1376                                                const LocationContext *LC) {
1377
1378   // We need the type-information of the tracked receiver object
1379   // Retrieve it from the state.
1380   const Expr *Receiver = msg.getInstanceReceiver();
1381   const ObjCInterfaceDecl* ID = 0;
1382
1383   // FIXME: Is this really working as expected?  There are cases where
1384   //  we just use the 'ID' from the message expression.
1385   SVal receiverV;
1386
1387   if (Receiver) {
1388     receiverV = state->getSValAsScalarOrLoc(Receiver);
1389
1390     // FIXME: Eventually replace the use of state->get<RefBindings> with
1391     // a generic API for reasoning about the Objective-C types of symbolic
1392     // objects.
1393     if (SymbolRef Sym = receiverV.getAsLocSymbol())
1394       if (const RefVal *T = state->get<RefBindings>(Sym))
1395         if (const ObjCObjectPointerType* PT =
1396             T->getType()->getAs<ObjCObjectPointerType>())
1397           ID = PT->getInterfaceDecl();
1398
1399     // FIXME: this is a hack.  This may or may not be the actual method
1400     //  that is called.
1401     if (!ID) {
1402       if (const ObjCObjectPointerType *PT =
1403           Receiver->getType()->getAs<ObjCObjectPointerType>())
1404         ID = PT->getInterfaceDecl();
1405     }
1406   } else {
1407     // FIXME: Hack for 'super'.
1408     ID = msg.getReceiverInterface();
1409   }
1410
1411   // FIXME: The receiver could be a reference to a class, meaning that
1412   //  we should use the class method.
1413   RetainSummary *Summ = getInstanceMethodSummary(msg, ID);
1414   return Summ ? Summ : getDefaultSummary();
1415 }
1416
1417 RetainSummary*
1418 RetainSummaryManager::getInstanceMethodSummary(Selector S,
1419                                                IdentifierInfo *ClsName,
1420                                                const ObjCInterfaceDecl* ID,
1421                                                const ObjCMethodDecl *MD,
1422                                                QualType RetTy) {
1423
1424   // Look up a summary in our summary cache.
1425   RetainSummary *Summ = ObjCMethodSummaries.find(ID, ClsName, S);
1426
1427   if (!Summ) {
1428     assert(ScratchArgs.isEmpty());
1429
1430     // "initXXX": pass-through for receiver.
1431     if (cocoa::deriveNamingConvention(S, MD) == cocoa::InitRule)
1432       Summ = getInitMethodSummary(RetTy);
1433     else
1434       Summ = getCommonMethodSummary(MD, S, RetTy);
1435
1436     // Annotations override defaults.
1437     updateSummaryFromAnnotations(*Summ, MD);
1438
1439     // Memoize the summary.
1440     ObjCMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1441   }
1442
1443   return Summ;
1444 }
1445
1446 RetainSummary*
1447 RetainSummaryManager::getClassMethodSummary(Selector S, IdentifierInfo *ClsName,
1448                                             const ObjCInterfaceDecl *ID,
1449                                             const ObjCMethodDecl *MD,
1450                                             QualType RetTy) {
1451
1452   assert(ClsName && "Class name must be specified.");
1453   RetainSummary *Summ = ObjCClassMethodSummaries.find(ID, ClsName, S);
1454
1455   if (!Summ) {
1456     Summ = getCommonMethodSummary(MD, S, RetTy);
1457     // Annotations override defaults.
1458     updateSummaryFromAnnotations(*Summ, MD);
1459     // Memoize the summary.
1460     ObjCClassMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ;
1461   }
1462
1463   return Summ;
1464 }
1465
1466 void RetainSummaryManager::InitializeClassMethodSummaries() {
1467   assert(ScratchArgs.isEmpty());
1468   RetainSummary* Summ = getPersistentSummary(ObjCAllocRetE);
1469
1470   // Create the [NSAssertionHandler currentHander] summary.
1471   addClassMethSummary("NSAssertionHandler", "currentHandler",
1472                 getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC)));
1473
1474   // Create the [NSAutoreleasePool addObject:] summary.
1475   ScratchArgs = AF.add(ScratchArgs, 0, Autorelease);
1476   addClassMethSummary("NSAutoreleasePool", "addObject",
1477                       getPersistentSummary(RetEffect::MakeNoRet(),
1478                                            DoNothing, Autorelease));
1479
1480   // Create the summaries for [NSObject performSelector...].  We treat
1481   // these as 'stop tracking' for the arguments because they are often
1482   // used for delegates that can release the object.  When we have better
1483   // inter-procedural analysis we can potentially do something better.  This
1484   // workaround is to remove false positives.
1485   Summ = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, StopTracking);
1486   IdentifierInfo *NSObjectII = &Ctx.Idents.get("NSObject");
1487   addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1488                     "afterDelay", NULL);
1489   addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject",
1490                     "afterDelay", "inModes", NULL);
1491   addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1492                     "withObject", "waitUntilDone", NULL);
1493   addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread",
1494                     "withObject", "waitUntilDone", "modes", NULL);
1495   addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1496                     "withObject", "waitUntilDone", NULL);
1497   addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread",
1498                     "withObject", "waitUntilDone", "modes", NULL);
1499   addClsMethSummary(NSObjectII, Summ, "performSelectorInBackground",
1500                     "withObject", NULL);
1501 }
1502
1503 void RetainSummaryManager::InitializeMethodSummaries() {
1504
1505   assert (ScratchArgs.isEmpty());
1506
1507   // Create the "init" selector.  It just acts as a pass-through for the
1508   // receiver.
1509   RetainSummary *InitSumm = getPersistentSummary(ObjCInitRetE, DecRefMsg);
1510   addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
1511
1512   // awakeAfterUsingCoder: behaves basically like an 'init' method.  It
1513   // claims the receiver and returns a retained object.
1514   addNSObjectMethSummary(GetUnarySelector("awakeAfterUsingCoder", Ctx),
1515                          InitSumm);
1516
1517   // The next methods are allocators.
1518   RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE);
1519   RetainSummary *CFAllocSumm =
1520     getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true));
1521
1522   // Create the "retain" selector.
1523   RetEffect E = RetEffect::MakeReceiverAlias();
1524   RetainSummary *Summ = getPersistentSummary(E, IncRefMsg);
1525   addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
1526
1527   // Create the "release" selector.
1528   Summ = getPersistentSummary(E, DecRefMsg);
1529   addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
1530
1531   // Create the "drain" selector.
1532   Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
1533   addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
1534
1535   // Create the -dealloc summary.
1536   Summ = getPersistentSummary(RetEffect::MakeNoRet(), Dealloc);
1537   addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ);
1538
1539   // Create the "autorelease" selector.
1540   Summ = getPersistentSummary(E, Autorelease);
1541   addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
1542
1543   // Specially handle NSAutoreleasePool.
1544   addInstMethSummary("NSAutoreleasePool", "init",
1545                      getPersistentSummary(RetEffect::MakeReceiverAlias(),
1546                                           NewAutoreleasePool));
1547
1548   // For NSWindow, allocated objects are (initially) self-owned.
1549   // FIXME: For now we opt for false negatives with NSWindow, as these objects
1550   //  self-own themselves.  However, they only do this once they are displayed.
1551   //  Thus, we need to track an NSWindow's display status.
1552   //  This is tracked in <rdar://problem/6062711>.
1553   //  See also http://llvm.org/bugs/show_bug.cgi?id=3714.
1554   RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet(),
1555                                                    StopTracking,
1556                                                    StopTracking);
1557
1558   addClassMethSummary("NSWindow", "alloc", NoTrackYet);
1559
1560 #if 0
1561   addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
1562                      "styleMask", "backing", "defer", NULL);
1563
1564   addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect",
1565                      "styleMask", "backing", "defer", "screen", NULL);
1566 #endif
1567
1568   // For NSPanel (which subclasses NSWindow), allocated objects are not
1569   //  self-owned.
1570   // FIXME: For now we don't track NSPanels. object for the same reason
1571   //   as for NSWindow objects.
1572   addClassMethSummary("NSPanel", "alloc", NoTrackYet);
1573
1574 #if 0
1575   addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
1576                      "styleMask", "backing", "defer", NULL);
1577
1578   addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect",
1579                      "styleMask", "backing", "defer", "screen", NULL);
1580 #endif
1581
1582   // Don't track allocated autorelease pools yet, as it is okay to prematurely
1583   // exit a method.
1584   addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet);
1585
1586   // Create NSAssertionHandler summaries.
1587   addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
1588                   "lineNumber", "description", NULL);
1589
1590   addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
1591                   "file", "lineNumber", "description", NULL);
1592
1593   // Create summaries QCRenderer/QCView -createSnapShotImageOfType:
1594   addInstMethSummary("QCRenderer", AllocSumm,
1595                      "createSnapshotImageOfType", NULL);
1596   addInstMethSummary("QCView", AllocSumm,
1597                      "createSnapshotImageOfType", NULL);
1598
1599   // Create summaries for CIContext, 'createCGImage' and
1600   // 'createCGLayerWithSize'.  These objects are CF objects, and are not
1601   // automatically garbage collected.
1602   addInstMethSummary("CIContext", CFAllocSumm,
1603                      "createCGImage", "fromRect", NULL);
1604   addInstMethSummary("CIContext", CFAllocSumm,
1605                      "createCGImage", "fromRect", "format", "colorSpace", NULL);
1606   addInstMethSummary("CIContext", CFAllocSumm, "createCGLayerWithSize",
1607            "info", NULL);
1608 }
1609
1610 //===----------------------------------------------------------------------===//
1611 // AutoreleaseBindings - State used to track objects in autorelease pools.
1612 //===----------------------------------------------------------------------===//
1613
1614 typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts;
1615 typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents;
1616 typedef llvm::ImmutableList<SymbolRef> ARStack;
1617
1618 static int AutoRCIndex = 0;
1619 static int AutoRBIndex = 0;
1620
1621 namespace { class AutoreleasePoolContents {}; }
1622 namespace { class AutoreleaseStack {}; }
1623
1624 namespace clang {
1625 namespace ento {
1626 template<> struct GRStateTrait<AutoreleaseStack>
1627   : public GRStatePartialTrait<ARStack> {
1628   static inline void* GDMIndex() { return &AutoRBIndex; }
1629 };
1630
1631 template<> struct GRStateTrait<AutoreleasePoolContents>
1632   : public GRStatePartialTrait<ARPoolContents> {
1633   static inline void* GDMIndex() { return &AutoRCIndex; }
1634 };
1635 } // end GR namespace
1636 } // end clang namespace
1637
1638 static SymbolRef GetCurrentAutoreleasePool(const GRState* state) {
1639   ARStack stack = state->get<AutoreleaseStack>();
1640   return stack.isEmpty() ? SymbolRef() : stack.getHead();
1641 }
1642
1643 static const GRState * SendAutorelease(const GRState *state,
1644                                        ARCounts::Factory &F, SymbolRef sym) {
1645
1646   SymbolRef pool = GetCurrentAutoreleasePool(state);
1647   const ARCounts *cnts = state->get<AutoreleasePoolContents>(pool);
1648   ARCounts newCnts(0);
1649
1650   if (cnts) {
1651     const unsigned *cnt = (*cnts).lookup(sym);
1652     newCnts = F.add(*cnts, sym, cnt ? *cnt  + 1 : 1);
1653   }
1654   else
1655     newCnts = F.add(F.getEmptyMap(), sym, 1);
1656
1657   return state->set<AutoreleasePoolContents>(pool, newCnts);
1658 }
1659
1660 //===----------------------------------------------------------------------===//
1661 // Transfer functions.
1662 //===----------------------------------------------------------------------===//
1663
1664 namespace {
1665
1666 class CFRefCount : public TransferFuncs {
1667 public:
1668   class BindingsPrinter : public GRState::Printer {
1669   public:
1670     virtual void Print(llvm::raw_ostream& Out, const GRState* state,
1671                        const char* nl, const char* sep);
1672   };
1673
1674   typedef llvm::DenseMap<const ExplodedNode*, const RetainSummary*>
1675     SummaryLogTy;
1676
1677   RetainSummaryManager Summaries;
1678   SummaryLogTy SummaryLog;
1679   const LangOptions&   LOpts;
1680   ARCounts::Factory    ARCountFactory;
1681
1682   BugType *useAfterRelease, *releaseNotOwned;
1683   BugType *deallocGC, *deallocNotOwned;
1684   BugType *leakWithinFunction, *leakAtReturn;
1685   BugType *overAutorelease;
1686   BugType *returnNotOwnedForOwned;
1687   BugReporter *BR;
1688
1689   const GRState * Update(const GRState * state, SymbolRef sym, RefVal V, ArgEffect E,
1690                     RefVal::Kind& hasErr);
1691
1692   void ProcessNonLeakError(ExplodedNodeSet& Dst,
1693                            StmtNodeBuilder& Builder,
1694                            const Expr* NodeExpr, SourceRange ErrorRange,
1695                            ExplodedNode* Pred,
1696                            const GRState* St,
1697                            RefVal::Kind hasErr, SymbolRef Sym);
1698
1699   const GRState * HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
1700                                llvm::SmallVectorImpl<SymbolRef> &Leaked);
1701
1702   ExplodedNode* ProcessLeaks(const GRState * state,
1703                                       llvm::SmallVectorImpl<SymbolRef> &Leaked,
1704                                       GenericNodeBuilderRefCount &Builder,
1705                                       ExprEngine &Eng,
1706                                       ExplodedNode *Pred = 0);
1707
1708 public:
1709   CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
1710     : Summaries(Ctx, gcenabled, (bool)lopts.ObjCAutoRefCount),
1711       LOpts(lopts), useAfterRelease(0), releaseNotOwned(0),
1712       deallocGC(0), deallocNotOwned(0),
1713       leakWithinFunction(0), leakAtReturn(0), overAutorelease(0),
1714       returnNotOwnedForOwned(0), BR(0) {}
1715
1716   virtual ~CFRefCount() {}
1717
1718   void RegisterChecks(ExprEngine &Eng);
1719
1720   virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) {
1721     Printers.push_back(new BindingsPrinter());
1722   }
1723
1724   bool isGCEnabled() const { return Summaries.isGCEnabled(); }
1725   bool isARCorGCEnabled() const { return Summaries.isARCorGCEnabled(); }
1726   
1727   const LangOptions& getLangOptions() const { return LOpts; }
1728
1729   const RetainSummary *getSummaryOfNode(const ExplodedNode *N) const {
1730     SummaryLogTy::const_iterator I = SummaryLog.find(N);
1731     return I == SummaryLog.end() ? 0 : I->second;
1732   }
1733
1734   // Calls.
1735
1736   void evalSummary(ExplodedNodeSet& Dst,
1737                    ExprEngine& Eng,
1738                    StmtNodeBuilder& Builder,
1739                    const Expr* Ex,
1740                    const CallOrObjCMessage &callOrMsg,
1741                    InstanceReceiver Receiver,
1742                    const RetainSummary& Summ,
1743                    const MemRegion *Callee,
1744                    ExplodedNode* Pred, const GRState *state);
1745
1746   virtual void evalCall(ExplodedNodeSet& Dst,
1747                         ExprEngine& Eng,
1748                         StmtNodeBuilder& Builder,
1749                         const CallExpr* CE, SVal L,
1750                         ExplodedNode* Pred);
1751
1752
1753   virtual void evalObjCMessage(ExplodedNodeSet& Dst,
1754                                ExprEngine& Engine,
1755                                StmtNodeBuilder& Builder,
1756                                ObjCMessage msg,
1757                                ExplodedNode* Pred,
1758                                const GRState *state);
1759   // Stores.
1760   virtual void evalBind(StmtNodeBuilderRef& B, SVal location, SVal val);
1761
1762   // End-of-path.
1763
1764   virtual void evalEndPath(ExprEngine& Engine,
1765                            EndOfFunctionNodeBuilder& Builder);
1766
1767   virtual void evalDeadSymbols(ExplodedNodeSet& Dst,
1768                                ExprEngine& Engine,
1769                                StmtNodeBuilder& Builder,
1770                                ExplodedNode* Pred,
1771                                const GRState* state,
1772                                SymbolReaper& SymReaper);
1773
1774   std::pair<ExplodedNode*, const GRState *>
1775   HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilderRefCount Bd,
1776                           ExplodedNode* Pred, ExprEngine &Eng,
1777                           SymbolRef Sym, RefVal V, bool &stop);
1778   // Return statements.
1779
1780   virtual void evalReturn(ExplodedNodeSet& Dst,
1781                           ExprEngine& Engine,
1782                           StmtNodeBuilder& Builder,
1783                           const ReturnStmt* S,
1784                           ExplodedNode* Pred);
1785   
1786   void evalReturnWithRetEffect(ExplodedNodeSet& Dst,
1787                                ExprEngine& Engine,
1788                                StmtNodeBuilder& Builder,
1789                                const ReturnStmt* S,
1790                                ExplodedNode* Pred,
1791                                RetEffect RE, RefVal X,
1792                                SymbolRef Sym, const GRState *state);
1793
1794
1795   // Assumptions.
1796
1797   virtual const GRState *evalAssume(const GRState* state, SVal condition,
1798                                     bool assumption);
1799 };
1800
1801 } // end anonymous namespace
1802
1803 static void PrintPool(llvm::raw_ostream &Out, SymbolRef Sym,
1804                       const GRState *state) {
1805   Out << ' ';
1806   if (Sym)
1807     Out << Sym->getSymbolID();
1808   else
1809     Out << "<pool>";
1810   Out << ":{";
1811
1812   // Get the contents of the pool.
1813   if (const ARCounts *cnts = state->get<AutoreleasePoolContents>(Sym))
1814     for (ARCounts::iterator J=cnts->begin(), EJ=cnts->end(); J != EJ; ++J)
1815       Out << '(' << J.getKey() << ',' << J.getData() << ')';
1816
1817   Out << '}';
1818 }
1819
1820 void CFRefCount::BindingsPrinter::Print(llvm::raw_ostream& Out,
1821                                         const GRState* state,
1822                                         const char* nl, const char* sep) {
1823
1824   RefBindings B = state->get<RefBindings>();
1825
1826   if (!B.isEmpty())
1827     Out << sep << nl;
1828
1829   for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
1830     Out << (*I).first << " : ";
1831     (*I).second.print(Out);
1832     Out << nl;
1833   }
1834
1835   // Print the autorelease stack.
1836   Out << sep << nl << "AR pool stack:";
1837   ARStack stack = state->get<AutoreleaseStack>();
1838
1839   PrintPool(Out, SymbolRef(), state);  // Print the caller's pool.
1840   for (ARStack::iterator I=stack.begin(), E=stack.end(); I!=E; ++I)
1841     PrintPool(Out, *I, state);
1842
1843   Out << nl;
1844 }
1845
1846 //===----------------------------------------------------------------------===//
1847 // Error reporting.
1848 //===----------------------------------------------------------------------===//
1849
1850 namespace {
1851
1852   //===-------------===//
1853   // Bug Descriptions. //
1854   //===-------------===//
1855
1856   class CFRefBug : public BugType {
1857   protected:
1858     CFRefCount& TF;
1859
1860     CFRefBug(CFRefCount* tf, llvm::StringRef name)
1861     : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {}
1862   public:
1863
1864     CFRefCount& getTF() { return TF; }
1865
1866     // FIXME: Eventually remove.
1867     virtual const char* getDescription() const = 0;
1868
1869     virtual bool isLeak() const { return false; }
1870   };
1871
1872   class UseAfterRelease : public CFRefBug {
1873   public:
1874     UseAfterRelease(CFRefCount* tf)
1875     : CFRefBug(tf, "Use-after-release") {}
1876
1877     const char* getDescription() const {
1878       return "Reference-counted object is used after it is released";
1879     }
1880   };
1881
1882   class BadRelease : public CFRefBug {
1883   public:
1884     BadRelease(CFRefCount* tf) : CFRefBug(tf, "Bad release") {}
1885
1886     const char* getDescription() const {
1887       return "Incorrect decrement of the reference count of an object that is "
1888              "not owned at this point by the caller";
1889     }
1890   };
1891
1892   class DeallocGC : public CFRefBug {
1893   public:
1894     DeallocGC(CFRefCount *tf)
1895       : CFRefBug(tf, "-dealloc called while using garbage collection") {}
1896
1897     const char *getDescription() const {
1898       return "-dealloc called while using garbage collection";
1899     }
1900   };
1901
1902   class DeallocNotOwned : public CFRefBug {
1903   public:
1904     DeallocNotOwned(CFRefCount *tf)
1905       : CFRefBug(tf, "-dealloc sent to non-exclusively owned object") {}
1906
1907     const char *getDescription() const {
1908       return "-dealloc sent to object that may be referenced elsewhere";
1909     }
1910   };
1911
1912   class OverAutorelease : public CFRefBug {
1913   public:
1914     OverAutorelease(CFRefCount *tf) :
1915       CFRefBug(tf, "Object sent -autorelease too many times") {}
1916
1917     const char *getDescription() const {
1918       return "Object sent -autorelease too many times";
1919     }
1920   };
1921
1922   class ReturnedNotOwnedForOwned : public CFRefBug {
1923   public:
1924     ReturnedNotOwnedForOwned(CFRefCount *tf) :
1925       CFRefBug(tf, "Method should return an owned object") {}
1926
1927     const char *getDescription() const {
1928       return "Object with a +0 retain count returned to caller where a +1 "
1929              "(owning) retain count is expected";
1930     }
1931   };
1932
1933   class Leak : public CFRefBug {
1934     const bool isReturn;
1935   protected:
1936     Leak(CFRefCount* tf, llvm::StringRef name, bool isRet)
1937     : CFRefBug(tf, name), isReturn(isRet) {}
1938   public:
1939
1940     const char* getDescription() const { return ""; }
1941
1942     bool isLeak() const { return true; }
1943   };
1944
1945   class LeakAtReturn : public Leak {
1946   public:
1947     LeakAtReturn(CFRefCount* tf, llvm::StringRef name)
1948     : Leak(tf, name, true) {}
1949   };
1950
1951   class LeakWithinFunction : public Leak {
1952   public:
1953     LeakWithinFunction(CFRefCount* tf, llvm::StringRef name)
1954     : Leak(tf, name, false) {}
1955   };
1956
1957   //===---------===//
1958   // Bug Reports.  //
1959   //===---------===//
1960
1961   class CFRefReport : public RangedBugReport {
1962   protected:
1963     SymbolRef Sym;
1964     const CFRefCount &TF;
1965   public:
1966     CFRefReport(CFRefBug& D, const CFRefCount &tf,
1967                 ExplodedNode *n, SymbolRef sym)
1968       : RangedBugReport(D, D.getDescription(), n), Sym(sym), TF(tf) {}
1969
1970     CFRefReport(CFRefBug& D, const CFRefCount &tf,
1971                 ExplodedNode *n, SymbolRef sym, llvm::StringRef endText)
1972       : RangedBugReport(D, D.getDescription(), endText, n), Sym(sym), TF(tf) {}
1973
1974     virtual ~CFRefReport() {}
1975
1976     CFRefBug& getBugType() const {
1977       return (CFRefBug&) RangedBugReport::getBugType();
1978     }
1979
1980     virtual std::pair<ranges_iterator, ranges_iterator> getRanges() const {
1981       if (!getBugType().isLeak())
1982         return RangedBugReport::getRanges();
1983       else
1984         return std::make_pair(ranges_iterator(), ranges_iterator());
1985     }
1986
1987     SymbolRef getSymbol() const { return Sym; }
1988
1989     PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
1990                                     const ExplodedNode* N);
1991
1992     std::pair<const char**,const char**> getExtraDescriptiveText();
1993
1994     PathDiagnosticPiece* VisitNode(const ExplodedNode* N,
1995                                    const ExplodedNode* PrevN,
1996                                    BugReporterContext& BRC);
1997   };
1998
1999   class CFRefLeakReport : public CFRefReport {
2000     SourceLocation AllocSite;
2001     const MemRegion* AllocBinding;
2002   public:
2003     CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
2004                     ExplodedNode *n, SymbolRef sym,
2005                     ExprEngine& Eng);
2006
2007     PathDiagnosticPiece* getEndPath(BugReporterContext& BRC,
2008                                     const ExplodedNode* N);
2009
2010     SourceLocation getLocation() const { return AllocSite; }
2011   };
2012 } // end anonymous namespace
2013
2014
2015
2016 static const char* Msgs[] = {
2017   // GC only
2018   "Code is compiled to only use garbage collection",
2019   // No GC.
2020   "Code is compiled to use reference counts",
2021   // Hybrid, with GC.
2022   "Code is compiled to use either garbage collection (GC) or reference counts"
2023   " (non-GC).  The bug occurs with GC enabled",
2024   // Hybrid, without GC
2025   "Code is compiled to use either garbage collection (GC) or reference counts"
2026   " (non-GC).  The bug occurs in non-GC mode"
2027 };
2028
2029 std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
2030   CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
2031
2032   switch (TF.getLangOptions().getGCMode()) {
2033     default:
2034       assert(false);
2035
2036     case LangOptions::GCOnly:
2037       assert (TF.isGCEnabled());
2038       return std::make_pair(&Msgs[0], &Msgs[0]+1);
2039
2040     case LangOptions::NonGC:
2041       assert (!TF.isGCEnabled());
2042       return std::make_pair(&Msgs[1], &Msgs[1]+1);
2043
2044     case LangOptions::HybridGC:
2045       if (TF.isGCEnabled())
2046         return std::make_pair(&Msgs[2], &Msgs[2]+1);
2047       else
2048         return std::make_pair(&Msgs[3], &Msgs[3]+1);
2049   }
2050 }
2051
2052 static inline bool contains(const llvm::SmallVectorImpl<ArgEffect>& V,
2053                             ArgEffect X) {
2054   for (llvm::SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end();
2055        I!=E; ++I)
2056     if (*I == X) return true;
2057
2058   return false;
2059 }
2060
2061 PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode* N,
2062                                             const ExplodedNode* PrevN,
2063                                             BugReporterContext& BRC) {
2064
2065   if (!isa<PostStmt>(N->getLocation()))
2066     return NULL;
2067
2068   // Check if the type state has changed.
2069   const GRState *PrevSt = PrevN->getState();
2070   const GRState *CurrSt = N->getState();
2071
2072   const RefVal* CurrT = CurrSt->get<RefBindings>(Sym);
2073   if (!CurrT) return NULL;
2074
2075   const RefVal &CurrV = *CurrT;
2076   const RefVal *PrevT = PrevSt->get<RefBindings>(Sym);
2077
2078   // Create a string buffer to constain all the useful things we want
2079   // to tell the user.
2080   std::string sbuf;
2081   llvm::raw_string_ostream os(sbuf);
2082
2083   // This is the allocation site since the previous node had no bindings
2084   // for this symbol.
2085   if (!PrevT) {
2086     const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2087
2088     if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
2089       // Get the name of the callee (if it is available).
2090       SVal X = CurrSt->getSValAsScalarOrLoc(CE->getCallee());
2091       if (const FunctionDecl* FD = X.getAsFunctionDecl())
2092         os << "Call to function '" << FD << '\'';
2093       else
2094         os << "function call";
2095     }
2096     else if (isa<ObjCMessageExpr>(S)) {
2097       os << "Method";
2098     } else {
2099       os << "Property";
2100     }
2101
2102     if (CurrV.getObjKind() == RetEffect::CF) {
2103       os << " returns a Core Foundation object with a ";
2104     }
2105     else {
2106       assert (CurrV.getObjKind() == RetEffect::ObjC);
2107       os << " returns an Objective-C object with a ";
2108     }
2109
2110     if (CurrV.isOwned()) {
2111       os << "+1 retain count";
2112
2113       if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) {
2114         assert(CurrV.getObjKind() == RetEffect::CF);
2115         os << ".  "
2116         "Core Foundation objects are not automatically garbage collected.";
2117       }
2118     }
2119     else {
2120       assert (CurrV.isNotOwned());
2121       os << "+0 retain count";
2122     }
2123
2124     PathDiagnosticLocation Pos(S, BRC.getSourceManager());
2125     return new PathDiagnosticEventPiece(Pos, os.str());
2126   }
2127
2128   // Gather up the effects that were performed on the object at this
2129   // program point
2130   llvm::SmallVector<ArgEffect, 2> AEffects;
2131
2132   if (const RetainSummary *Summ =
2133         TF.getSummaryOfNode(BRC.getNodeResolver().getOriginalNode(N))) {
2134     // We only have summaries attached to nodes after evaluating CallExpr and
2135     // ObjCMessageExprs.
2136     const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2137
2138     if (const CallExpr *CE = dyn_cast<CallExpr>(S)) {
2139       // Iterate through the parameter expressions and see if the symbol
2140       // was ever passed as an argument.
2141       unsigned i = 0;
2142
2143       for (CallExpr::const_arg_iterator AI=CE->arg_begin(), AE=CE->arg_end();
2144            AI!=AE; ++AI, ++i) {
2145
2146         // Retrieve the value of the argument.  Is it the symbol
2147         // we are interested in?
2148         if (CurrSt->getSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym)
2149           continue;
2150
2151         // We have an argument.  Get the effect!
2152         AEffects.push_back(Summ->getArg(i));
2153       }
2154     }
2155     else if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) {
2156       if (const Expr *receiver = ME->getInstanceReceiver())
2157         if (CurrSt->getSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) {
2158           // The symbol we are tracking is the receiver.
2159           AEffects.push_back(Summ->getReceiverEffect());
2160         }
2161     }
2162   }
2163
2164   do {
2165     // Get the previous type state.
2166     RefVal PrevV = *PrevT;
2167
2168     // Specially handle -dealloc.
2169     if (!TF.isGCEnabled() && contains(AEffects, Dealloc)) {
2170       // Determine if the object's reference count was pushed to zero.
2171       assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
2172       // We may not have transitioned to 'release' if we hit an error.
2173       // This case is handled elsewhere.
2174       if (CurrV.getKind() == RefVal::Released) {
2175         assert(CurrV.getCombinedCounts() == 0);
2176         os << "Object released by directly sending the '-dealloc' message";
2177         break;
2178       }
2179     }
2180
2181     // Specially handle CFMakeCollectable and friends.
2182     if (contains(AEffects, MakeCollectable)) {
2183       // Get the name of the function.
2184       const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2185       SVal X = CurrSt->getSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee());
2186       const FunctionDecl* FD = X.getAsFunctionDecl();
2187       const std::string& FName = FD->getNameAsString();
2188
2189       if (TF.isGCEnabled()) {
2190         // Determine if the object's reference count was pushed to zero.
2191         assert(!(PrevV == CurrV) && "The typestate *must* have changed.");
2192
2193         os << "In GC mode a call to '" << FName
2194         <<  "' decrements an object's retain count and registers the "
2195         "object with the garbage collector. ";
2196
2197         if (CurrV.getKind() == RefVal::Released) {
2198           assert(CurrV.getCount() == 0);
2199           os << "Since it now has a 0 retain count the object can be "
2200           "automatically collected by the garbage collector.";
2201         }
2202         else
2203           os << "An object must have a 0 retain count to be garbage collected. "
2204           "After this call its retain count is +" << CurrV.getCount()
2205           << '.';
2206       }
2207       else
2208         os << "When GC is not enabled a call to '" << FName
2209         << "' has no effect on its argument.";
2210
2211       // Nothing more to say.
2212       break;
2213     }
2214
2215     // Determine if the typestate has changed.
2216     if (!(PrevV == CurrV))
2217       switch (CurrV.getKind()) {
2218         case RefVal::Owned:
2219         case RefVal::NotOwned:
2220
2221           if (PrevV.getCount() == CurrV.getCount()) {
2222             // Did an autorelease message get sent?
2223             if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount())
2224               return 0;
2225
2226             assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount());
2227             os << "Object sent -autorelease message";
2228             break;
2229           }
2230
2231           if (PrevV.getCount() > CurrV.getCount())
2232             os << "Reference count decremented.";
2233           else
2234             os << "Reference count incremented.";
2235
2236           if (unsigned Count = CurrV.getCount())
2237             os << " The object now has a +" << Count << " retain count.";
2238
2239           if (PrevV.getKind() == RefVal::Released) {
2240             assert(TF.isGCEnabled() && CurrV.getCount() > 0);
2241             os << " The object is not eligible for garbage collection until the "
2242             "retain count reaches 0 again.";
2243           }
2244
2245           break;
2246
2247         case RefVal::Released:
2248           os << "Object released.";
2249           break;
2250
2251         case RefVal::ReturnedOwned:
2252           os << "Object returned to caller as an owning reference (single retain "
2253           "count transferred to caller)";
2254           break;
2255
2256         case RefVal::ReturnedNotOwned:
2257           os << "Object returned to caller with a +0 retain count";
2258           break;
2259
2260         default:
2261           return NULL;
2262       }
2263
2264     // Emit any remaining diagnostics for the argument effects (if any).
2265     for (llvm::SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(),
2266          E=AEffects.end(); I != E; ++I) {
2267
2268       // A bunch of things have alternate behavior under GC.
2269       if (TF.isGCEnabled())
2270         switch (*I) {
2271           default: break;
2272           case Autorelease:
2273             os << "In GC mode an 'autorelease' has no effect.";
2274             continue;
2275           case IncRefMsg:
2276             os << "In GC mode the 'retain' message has no effect.";
2277             continue;
2278           case DecRefMsg:
2279             os << "In GC mode the 'release' message has no effect.";
2280             continue;
2281         }
2282     }
2283   } while (0);
2284
2285   if (os.str().empty())
2286     return 0; // We have nothing to say!
2287
2288   const Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
2289   PathDiagnosticLocation Pos(S, BRC.getSourceManager());
2290   PathDiagnosticPiece* P = new PathDiagnosticEventPiece(Pos, os.str());
2291
2292   // Add the range by scanning the children of the statement for any bindings
2293   // to Sym.
2294   for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
2295        I!=E; ++I)
2296     if (const Expr* Exp = dyn_cast_or_null<Expr>(*I))
2297       if (CurrSt->getSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) {
2298         P->addRange(Exp->getSourceRange());
2299         break;
2300       }
2301
2302   return P;
2303 }
2304
2305 namespace {
2306   class FindUniqueBinding :
2307   public StoreManager::BindingsHandler {
2308     SymbolRef Sym;
2309     const MemRegion* Binding;
2310     bool First;
2311
2312   public:
2313     FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {}
2314
2315     bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
2316                        SVal val) {
2317
2318       SymbolRef SymV = val.getAsSymbol();
2319       if (!SymV || SymV != Sym)
2320         return true;
2321
2322       if (Binding) {
2323         First = false;
2324         return false;
2325       }
2326       else
2327         Binding = R;
2328
2329       return true;
2330     }
2331
2332     operator bool() { return First && Binding; }
2333     const MemRegion* getRegion() { return Binding; }
2334   };
2335 }
2336
2337 static std::pair<const ExplodedNode*,const MemRegion*>
2338 GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode* N,
2339                   SymbolRef Sym) {
2340
2341   // Find both first node that referred to the tracked symbol and the
2342   // memory location that value was store to.
2343   const ExplodedNode* Last = N;
2344   const MemRegion* FirstBinding = 0;
2345
2346   while (N) {
2347     const GRState* St = N->getState();
2348     RefBindings B = St->get<RefBindings>();
2349
2350     if (!B.lookup(Sym))
2351       break;
2352
2353     FindUniqueBinding FB(Sym);
2354     StateMgr.iterBindings(St, FB);
2355     if (FB) FirstBinding = FB.getRegion();
2356
2357     Last = N;
2358     N = N->pred_empty() ? NULL : *(N->pred_begin());
2359   }
2360
2361   return std::make_pair(Last, FirstBinding);
2362 }
2363
2364 PathDiagnosticPiece*
2365 CFRefReport::getEndPath(BugReporterContext& BRC,
2366                         const ExplodedNode* EndN) {
2367   // Tell the BugReporterContext to report cases when the tracked symbol is
2368   // assigned to different variables, etc.
2369   BRC.addNotableSymbol(Sym);
2370   return RangedBugReport::getEndPath(BRC, EndN);
2371 }
2372
2373 PathDiagnosticPiece*
2374 CFRefLeakReport::getEndPath(BugReporterContext& BRC,
2375                             const ExplodedNode* EndN){
2376
2377   // Tell the BugReporterContext to report cases when the tracked symbol is
2378   // assigned to different variables, etc.
2379   BRC.addNotableSymbol(Sym);
2380
2381   // We are reporting a leak.  Walk up the graph to get to the first node where
2382   // the symbol appeared, and also get the first VarDecl that tracked object
2383   // is stored to.
2384   const ExplodedNode* AllocNode = 0;
2385   const MemRegion* FirstBinding = 0;
2386
2387   llvm::tie(AllocNode, FirstBinding) =
2388     GetAllocationSite(BRC.getStateManager(), EndN, Sym);
2389
2390   SourceManager& SMgr = BRC.getSourceManager();
2391
2392   // Compute an actual location for the leak.  Sometimes a leak doesn't
2393   // occur at an actual statement (e.g., transition between blocks; end
2394   // of function) so we need to walk the graph and compute a real location.
2395   const ExplodedNode* LeakN = EndN;
2396   PathDiagnosticLocation L;
2397
2398   while (LeakN) {
2399     ProgramPoint P = LeakN->getLocation();
2400
2401     if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) {
2402       L = PathDiagnosticLocation(PS->getStmt()->getLocStart(), SMgr);
2403       break;
2404     }
2405     else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) {
2406       if (const Stmt* Term = BE->getSrc()->getTerminator()) {
2407         L = PathDiagnosticLocation(Term->getLocStart(), SMgr);
2408         break;
2409       }
2410     }
2411
2412     LeakN = LeakN->succ_empty() ? 0 : *(LeakN->succ_begin());
2413   }
2414
2415   if (!L.isValid()) {
2416     const Decl &D = EndN->getCodeDecl();
2417     L = PathDiagnosticLocation(D.getBodyRBrace(), SMgr);
2418   }
2419
2420   std::string sbuf;
2421   llvm::raw_string_ostream os(sbuf);
2422
2423   os << "Object leaked: ";
2424
2425   if (FirstBinding) {
2426     os << "object allocated and stored into '"
2427        << FirstBinding->getString() << '\'';
2428   }
2429   else
2430     os << "allocated object";
2431
2432   // Get the retain count.
2433   const RefVal* RV = EndN->getState()->get<RefBindings>(Sym);
2434
2435   if (RV->getKind() == RefVal::ErrorLeakReturned) {
2436     // FIXME: Per comments in rdar://6320065, "create" only applies to CF
2437     // objects.  Only "copy", "alloc", "retain" and "new" transfer ownership
2438     // to the caller for NS objects.
2439     const Decl *D = &EndN->getCodeDecl();
2440     if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
2441       os << " is returned from a method whose name ('"
2442          << MD->getSelector().getAsString()
2443          << "') does not start with 'copy', 'mutableCopy', 'alloc' or 'new'."
2444             "  This violates the naming convention rules"
2445             " given in the Memory Management Guide for Cocoa";
2446     }
2447     else {
2448       const FunctionDecl *FD = cast<FunctionDecl>(D);
2449       os << " is return from a function whose name ('"
2450          << FD->getNameAsString()
2451          << "') does not contain 'Copy' or 'Create'.  This violates the naming"
2452             " convention rules given the Memory Management Guide for Core"
2453             " Foundation";
2454     }    
2455   }
2456   else if (RV->getKind() == RefVal::ErrorGCLeakReturned) {
2457     ObjCMethodDecl& MD = cast<ObjCMethodDecl>(EndN->getCodeDecl());
2458     os << " and returned from method '" << MD.getSelector().getAsString()
2459        << "' is potentially leaked when using garbage collection.  Callers "
2460           "of this method do not expect a returned object with a +1 retain "
2461           "count since they expect the object to be managed by the garbage "
2462           "collector";
2463   }
2464   else
2465     os << " is not referenced later in this execution path and has a retain "
2466           "count of +" << RV->getCount();
2467
2468   return new PathDiagnosticEventPiece(L, os.str());
2469 }
2470
2471 CFRefLeakReport::CFRefLeakReport(CFRefBug& D, const CFRefCount &tf,
2472                                  ExplodedNode *n,
2473                                  SymbolRef sym, ExprEngine& Eng)
2474 : CFRefReport(D, tf, n, sym) {
2475
2476   // Most bug reports are cached at the location where they occurred.
2477   // With leaks, we want to unique them by the location where they were
2478   // allocated, and only report a single path.  To do this, we need to find
2479   // the allocation site of a piece of tracked memory, which we do via a
2480   // call to GetAllocationSite.  This will walk the ExplodedGraph backwards.
2481   // Note that this is *not* the trimmed graph; we are guaranteed, however,
2482   // that all ancestor nodes that represent the allocation site have the
2483   // same SourceLocation.
2484   const ExplodedNode* AllocNode = 0;
2485
2486   llvm::tie(AllocNode, AllocBinding) =  // Set AllocBinding.
2487     GetAllocationSite(Eng.getStateManager(), getErrorNode(), getSymbol());
2488
2489   // Get the SourceLocation for the allocation site.
2490   ProgramPoint P = AllocNode->getLocation();
2491   AllocSite = cast<PostStmt>(P).getStmt()->getLocStart();
2492
2493   // Fill in the description of the bug.
2494   Description.clear();
2495   llvm::raw_string_ostream os(Description);
2496   SourceManager& SMgr = Eng.getContext().getSourceManager();
2497   unsigned AllocLine = SMgr.getInstantiationLineNumber(AllocSite);
2498   os << "Potential leak ";
2499   if (tf.isGCEnabled()) {
2500     os << "(when using garbage collection) ";
2501   }
2502   os << "of an object allocated on line " << AllocLine;
2503
2504   // FIXME: AllocBinding doesn't get populated for RegionStore yet.
2505   if (AllocBinding)
2506     os << " and stored into '" << AllocBinding->getString() << '\'';
2507 }
2508
2509 //===----------------------------------------------------------------------===//
2510 // Main checker logic.
2511 //===----------------------------------------------------------------------===//
2512
2513 /// GetReturnType - Used to get the return type of a message expression or
2514 ///  function call with the intention of affixing that type to a tracked symbol.
2515 ///  While the the return type can be queried directly from RetEx, when
2516 ///  invoking class methods we augment to the return type to be that of
2517 ///  a pointer to the class (as opposed it just being id).
2518 static QualType GetReturnType(const Expr* RetE, ASTContext& Ctx) {
2519   QualType RetTy = RetE->getType();
2520   // If RetE is not a message expression just return its type.
2521   // If RetE is a message expression, return its types if it is something
2522   /// more specific than id.
2523   if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
2524     if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
2525       if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
2526           PT->isObjCClassType()) {
2527         // At this point we know the return type of the message expression is
2528         // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
2529         // is a call to a class method whose type we can resolve.  In such
2530         // cases, promote the return type to XXX* (where XXX is the class).
2531         const ObjCInterfaceDecl *D = ME->getReceiverInterface();
2532         return !D ? RetTy :
2533                     Ctx.getObjCObjectPointerType(Ctx.getObjCInterfaceType(D));
2534       }
2535
2536   return RetTy;
2537 }
2538
2539
2540 // HACK: Symbols that have ref-count state that are referenced directly
2541 //  (not as structure or array elements, or via bindings) by an argument
2542 //  should not have their ref-count state stripped after we have
2543 //  done an invalidation pass.
2544 //
2545 // FIXME: This is a global to currently share between CFRefCount and
2546 // RetainReleaseChecker.  Eventually all functionality in CFRefCount should
2547 // be migrated to RetainReleaseChecker, and we can make this a non-global.
2548 llvm::DenseSet<SymbolRef> WhitelistedSymbols;
2549 namespace {
2550 struct ResetWhiteList {
2551   ResetWhiteList() {}
2552   ~ResetWhiteList() { WhitelistedSymbols.clear(); } 
2553 };
2554 }
2555
2556 void CFRefCount::evalSummary(ExplodedNodeSet& Dst,
2557                              ExprEngine& Eng,
2558                              StmtNodeBuilder& Builder,
2559                              const Expr* Ex,
2560                              const CallOrObjCMessage &callOrMsg,
2561                              InstanceReceiver Receiver,
2562                              const RetainSummary& Summ,
2563                              const MemRegion *Callee,
2564                              ExplodedNode* Pred, const GRState *state) {
2565
2566   // Evaluate the effect of the arguments.
2567   RefVal::Kind hasErr = (RefVal::Kind) 0;
2568   SourceRange ErrorRange;
2569   SymbolRef ErrorSym = 0;
2570
2571   llvm::SmallVector<const MemRegion*, 10> RegionsToInvalidate;
2572   
2573   // Use RAII to make sure the whitelist is properly cleared.
2574   ResetWhiteList resetWhiteList;
2575
2576   // Invalidate all instance variables of the receiver of a message.
2577   // FIXME: We should be able to do better with inter-procedural analysis.
2578   if (Receiver) {
2579     SVal V = Receiver.getSValAsScalarOrLoc(state);
2580     if (SymbolRef Sym = V.getAsLocSymbol()) {
2581       if (state->get<RefBindings>(Sym))
2582         WhitelistedSymbols.insert(Sym);
2583     }
2584     if (const MemRegion *region = V.getAsRegion())
2585       RegionsToInvalidate.push_back(region);
2586   }
2587   
2588   // Invalidate all instance variables for the callee of a C++ method call.
2589   // FIXME: We should be able to do better with inter-procedural analysis.
2590   // FIXME: we can probably do better for const versus non-const methods.
2591   if (callOrMsg.isCXXCall()) {
2592     if (const MemRegion *callee = callOrMsg.getCXXCallee().getAsRegion())
2593       RegionsToInvalidate.push_back(callee);
2594   }
2595   
2596   for (unsigned idx = 0, e = callOrMsg.getNumArgs(); idx != e; ++idx) {
2597     SVal V = callOrMsg.getArgSValAsScalarOrLoc(idx);
2598     SymbolRef Sym = V.getAsLocSymbol();
2599
2600     if (Sym)
2601       if (RefBindings::data_type* T = state->get<RefBindings>(Sym)) {
2602         WhitelistedSymbols.insert(Sym);
2603         state = Update(state, Sym, *T, Summ.getArg(idx), hasErr);
2604         if (hasErr) {
2605           ErrorRange = callOrMsg.getArgSourceRange(idx);
2606           ErrorSym = Sym;
2607           break;
2608         }
2609       }
2610
2611   tryAgain:
2612     if (isa<Loc>(V)) {
2613       if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) {
2614         if (Summ.getArg(idx) == DoNothingByRef)
2615           continue;
2616
2617         // Invalidate the value of the variable passed by reference.
2618         const MemRegion *R = MR->getRegion();
2619
2620         // Are we dealing with an ElementRegion?  If the element type is
2621         // a basic integer type (e.g., char, int) and the underying region
2622         // is a variable region then strip off the ElementRegion.
2623         // FIXME: We really need to think about this for the general case
2624         //   as sometimes we are reasoning about arrays and other times
2625         //   about (char*), etc., is just a form of passing raw bytes.
2626         //   e.g., void *p = alloca(); foo((char*)p);
2627         if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) {
2628           // Checking for 'integral type' is probably too promiscuous, but
2629           // we'll leave it in for now until we have a systematic way of
2630           // handling all of these cases.  Eventually we need to come up
2631           // with an interface to StoreManager so that this logic can be
2632           // approriately delegated to the respective StoreManagers while
2633           // still allowing us to do checker-specific logic (e.g.,
2634           // invalidating reference counts), probably via callbacks.
2635           if (ER->getElementType()->isIntegralOrEnumerationType()) {
2636             const MemRegion *superReg = ER->getSuperRegion();
2637             if (isa<VarRegion>(superReg) || isa<FieldRegion>(superReg) ||
2638                 isa<ObjCIvarRegion>(superReg))
2639               R = cast<TypedRegion>(superReg);
2640           }
2641           // FIXME: What about layers of ElementRegions?
2642         }
2643
2644         // Mark this region for invalidation.  We batch invalidate regions
2645         // below for efficiency.
2646         RegionsToInvalidate.push_back(R);
2647         continue;
2648       }
2649       else {
2650         // Nuke all other arguments passed by reference.
2651         // FIXME: is this necessary or correct? This handles the non-Region
2652         //  cases.  Is it ever valid to store to these?
2653         state = state->unbindLoc(cast<Loc>(V));
2654       }
2655     }
2656     else if (isa<nonloc::LocAsInteger>(V)) {
2657       // If we are passing a location wrapped as an integer, unwrap it and
2658       // invalidate the values referred by the location.
2659       V = cast<nonloc::LocAsInteger>(V).getLoc();
2660       goto tryAgain;
2661     }
2662   }
2663
2664   // Block calls result in all captured values passed-via-reference to be
2665   // invalidated.
2666   if (const BlockDataRegion *BR = dyn_cast_or_null<BlockDataRegion>(Callee)) {
2667     RegionsToInvalidate.push_back(BR);
2668   }
2669
2670   // Invalidate regions we designed for invalidation use the batch invalidation
2671   // API.
2672
2673   // FIXME: We can have collisions on the conjured symbol if the
2674   //  expression *I also creates conjured symbols.  We probably want
2675   //  to identify conjured symbols by an expression pair: the enclosing
2676   //  expression (the context) and the expression itself.  This should
2677   //  disambiguate conjured symbols.
2678   unsigned Count = Builder.getCurrentBlockCount();
2679   StoreManager::InvalidatedSymbols IS;
2680
2681   // NOTE: Even if RegionsToInvalidate is empty, we must still invalidate
2682   //  global variables.
2683   // NOTE: RetainReleaseChecker handles the actual invalidation of symbols.
2684   state =
2685     state->invalidateRegions(RegionsToInvalidate.data(),
2686                              RegionsToInvalidate.data() +
2687                              RegionsToInvalidate.size(),
2688                              Ex, Count, &IS,
2689                              /* invalidateGlobals = */
2690                              Eng.doesInvalidateGlobals(callOrMsg));
2691
2692   // Evaluate the effect on the message receiver.
2693   if (!ErrorRange.isValid() && Receiver) {
2694     SymbolRef Sym = Receiver.getSValAsScalarOrLoc(state).getAsLocSymbol();
2695     if (Sym) {
2696       if (const RefVal* T = state->get<RefBindings>(Sym)) {
2697         state = Update(state, Sym, *T, Summ.getReceiverEffect(), hasErr);
2698         if (hasErr) {
2699           ErrorRange = Receiver.getSourceRange();
2700           ErrorSym = Sym;
2701         }
2702       }
2703     }
2704   }
2705
2706   // Process any errors.
2707   if (hasErr) {
2708     ProcessNonLeakError(Dst, Builder, Ex, ErrorRange, Pred, state,
2709                         hasErr, ErrorSym);
2710     return;
2711   }
2712
2713   // Consult the summary for the return value.
2714   RetEffect RE = Summ.getRetEffect();
2715
2716   if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
2717     bool found = false;
2718     if (Receiver) {
2719       SVal V = Receiver.getSValAsScalarOrLoc(state);
2720       if (SymbolRef Sym = V.getAsLocSymbol())
2721         if (state->get<RefBindings>(Sym)) {
2722           found = true;
2723           RE = Summaries.getObjAllocRetEffect();
2724         }
2725     } // FIXME: Otherwise, this is a send-to-super instance message.
2726     if (!found)
2727       RE = RetEffect::MakeNoRet();
2728   }
2729
2730   switch (RE.getKind()) {
2731     default:
2732       assert (false && "Unhandled RetEffect."); break;
2733
2734     case RetEffect::NoRet: {
2735       // Make up a symbol for the return value (not reference counted).
2736       // FIXME: Most of this logic is not specific to the retain/release
2737       // checker.
2738
2739       // FIXME: We eventually should handle structs and other compound types
2740       // that are returned by value.
2741
2742       // Use the result type from callOrMsg as it automatically adjusts
2743       // for methods/functions that return references.
2744       QualType resultTy = callOrMsg.getResultType(Eng.getContext());
2745       if (Loc::isLocType(resultTy) || 
2746             (resultTy->isIntegerType() && resultTy->isScalarType())) {
2747         unsigned Count = Builder.getCurrentBlockCount();
2748         SValBuilder &svalBuilder = Eng.getSValBuilder();
2749         SVal X = svalBuilder.getConjuredSymbolVal(NULL, Ex, resultTy, Count);
2750         state = state->BindExpr(Ex, X, false);
2751       }
2752
2753       break;
2754     }
2755
2756     case RetEffect::Alias: {
2757       unsigned idx = RE.getIndex();
2758       assert (idx < callOrMsg.getNumArgs());
2759       SVal V = callOrMsg.getArgSValAsScalarOrLoc(idx);
2760       state = state->BindExpr(Ex, V, false);
2761       break;
2762     }
2763
2764     case RetEffect::ReceiverAlias: {
2765       assert(Receiver);
2766       SVal V = Receiver.getSValAsScalarOrLoc(state);
2767       state = state->BindExpr(Ex, V, false);
2768       break;
2769     }
2770
2771     case RetEffect::OwnedAllocatedSymbol:
2772     case RetEffect::OwnedSymbol: {
2773       unsigned Count = Builder.getCurrentBlockCount();
2774       SValBuilder &svalBuilder = Eng.getSValBuilder();
2775       SymbolRef Sym = svalBuilder.getConjuredSymbol(Ex, Count);
2776
2777       // Use the result type from callOrMsg as it automatically adjusts
2778       // for methods/functions that return references.      
2779       QualType resultTy = callOrMsg.getResultType(Eng.getContext());
2780       state = state->set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(),
2781                                                             resultTy));
2782       state = state->BindExpr(Ex, svalBuilder.makeLoc(Sym), false);
2783
2784       // FIXME: Add a flag to the checker where allocations are assumed to
2785       // *not fail.
2786 #if 0
2787       if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) {
2788         bool isFeasible;
2789         state = state.assume(loc::SymbolVal(Sym), true, isFeasible);
2790         assert(isFeasible && "Cannot assume fresh symbol is non-null.");
2791       }
2792 #endif
2793
2794       break;
2795     }
2796
2797     case RetEffect::GCNotOwnedSymbol:
2798     case RetEffect::ARCNotOwnedSymbol:
2799     case RetEffect::NotOwnedSymbol: {
2800       unsigned Count = Builder.getCurrentBlockCount();
2801       SValBuilder &svalBuilder = Eng.getSValBuilder();
2802       SymbolRef Sym = svalBuilder.getConjuredSymbol(Ex, Count);
2803       QualType RetT = GetReturnType(Ex, svalBuilder.getContext());
2804       state = state->set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),
2805                                                                RetT));
2806       state = state->BindExpr(Ex, svalBuilder.makeLoc(Sym), false);
2807       break;
2808     }
2809   }
2810
2811   // Generate a sink node if we are at the end of a path.
2812   ExplodedNode *NewNode =
2813     Summ.isEndPath() ? Builder.MakeSinkNode(Dst, Ex, Pred, state)
2814                      : Builder.MakeNode(Dst, Ex, Pred, state);
2815
2816   // Annotate the edge with summary we used.
2817   if (NewNode) SummaryLog[NewNode] = &Summ;
2818 }
2819
2820
2821 void CFRefCount::evalCall(ExplodedNodeSet& Dst,
2822                           ExprEngine& Eng,
2823                           StmtNodeBuilder& Builder,
2824                           const CallExpr* CE, SVal L,
2825                           ExplodedNode* Pred) {
2826
2827   RetainSummary *Summ = 0;
2828
2829   // FIXME: Better support for blocks.  For now we stop tracking anything
2830   // that is passed to blocks.
2831   // FIXME: Need to handle variables that are "captured" by the block.
2832   if (dyn_cast_or_null<BlockDataRegion>(L.getAsRegion())) {
2833     Summ = Summaries.getPersistentStopSummary();
2834   }
2835   else if (const FunctionDecl* FD = L.getAsFunctionDecl()) {
2836     Summ = Summaries.getSummary(FD);
2837   }
2838   else if (const CXXMemberCallExpr *me = dyn_cast<CXXMemberCallExpr>(CE)) {
2839     if (const CXXMethodDecl *MD = me->getMethodDecl())
2840       Summ = Summaries.getSummary(MD);
2841     else
2842       Summ = Summaries.getDefaultSummary();    
2843   }
2844   else
2845     Summ = Summaries.getDefaultSummary();
2846
2847   assert(Summ);
2848   evalSummary(Dst, Eng, Builder, CE,
2849               CallOrObjCMessage(CE, Builder.GetState(Pred)),
2850               InstanceReceiver(), *Summ,L.getAsRegion(),
2851               Pred, Builder.GetState(Pred));
2852 }
2853
2854 void CFRefCount::evalObjCMessage(ExplodedNodeSet& Dst,
2855                                  ExprEngine& Eng,
2856                                  StmtNodeBuilder& Builder,
2857                                  ObjCMessage msg,
2858                                  ExplodedNode* Pred,
2859                                  const GRState *state) {
2860   RetainSummary *Summ =
2861     msg.isInstanceMessage()
2862       ? Summaries.getInstanceMethodSummary(msg, state,Pred->getLocationContext())
2863       : Summaries.getClassMethodSummary(msg);
2864
2865   assert(Summ && "RetainSummary is null");
2866   evalSummary(Dst, Eng, Builder, msg.getOriginExpr(),
2867               CallOrObjCMessage(msg, Builder.GetState(Pred)),
2868               InstanceReceiver(msg, Pred->getLocationContext()), *Summ, NULL,
2869               Pred, state);
2870 }
2871
2872 namespace {
2873 class StopTrackingCallback : public SymbolVisitor {
2874   const GRState *state;
2875 public:
2876   StopTrackingCallback(const GRState *st) : state(st) {}
2877   const GRState *getState() const { return state; }
2878
2879   bool VisitSymbol(SymbolRef sym) {
2880     state = state->remove<RefBindings>(sym);
2881     return true;
2882   }
2883 };
2884 } // end anonymous namespace
2885
2886
2887 void CFRefCount::evalBind(StmtNodeBuilderRef& B, SVal location, SVal val) {
2888   // Are we storing to something that causes the value to "escape"?
2889   bool escapes = false;
2890
2891   // A value escapes in three possible cases (this may change):
2892   //
2893   // (1) we are binding to something that is not a memory region.
2894   // (2) we are binding to a memregion that does not have stack storage
2895   // (3) we are binding to a memregion with stack storage that the store
2896   //     does not understand.
2897   const GRState *state = B.getState();
2898
2899   if (!isa<loc::MemRegionVal>(location))
2900     escapes = true;
2901   else {
2902     const MemRegion* R = cast<loc::MemRegionVal>(location).getRegion();
2903     escapes = !R->hasStackStorage();
2904
2905     if (!escapes) {
2906       // To test (3), generate a new state with the binding removed.  If it is
2907       // the same state, then it escapes (since the store cannot represent
2908       // the binding).
2909       escapes = (state == (state->bindLoc(cast<Loc>(location), UnknownVal())));
2910     }
2911   }
2912
2913   // If our store can represent the binding and we aren't storing to something
2914   // that doesn't have local storage then just return and have the simulation
2915   // state continue as is.
2916   if (!escapes)
2917       return;
2918
2919   // Otherwise, find all symbols referenced by 'val' that we are tracking
2920   // and stop tracking them.
2921   B.MakeNode(state->scanReachableSymbols<StopTrackingCallback>(val).getState());
2922 }
2923
2924  // Return statements.
2925
2926 void CFRefCount::evalReturn(ExplodedNodeSet& Dst,
2927                             ExprEngine& Eng,
2928                             StmtNodeBuilder& Builder,
2929                             const ReturnStmt* S,
2930                             ExplodedNode* Pred) {
2931
2932   const Expr* RetE = S->getRetValue();
2933   if (!RetE)
2934     return;
2935
2936   const GRState *state = Builder.GetState(Pred);
2937   SymbolRef Sym = state->getSValAsScalarOrLoc(RetE).getAsLocSymbol();
2938
2939   if (!Sym)
2940     return;
2941
2942   // Get the reference count binding (if any).
2943   const RefVal* T = state->get<RefBindings>(Sym);
2944
2945   if (!T)
2946     return;
2947
2948   // Change the reference count.
2949   RefVal X = *T;
2950
2951   switch (X.getKind()) {
2952     case RefVal::Owned: {
2953       unsigned cnt = X.getCount();
2954       assert (cnt > 0);
2955       X.setCount(cnt - 1);
2956       X = X ^ RefVal::ReturnedOwned;
2957       break;
2958     }
2959
2960     case RefVal::NotOwned: {
2961       unsigned cnt = X.getCount();
2962       if (cnt) {
2963         X.setCount(cnt - 1);
2964         X = X ^ RefVal::ReturnedOwned;
2965       }
2966       else {
2967         X = X ^ RefVal::ReturnedNotOwned;
2968       }
2969       break;
2970     }
2971
2972     default:
2973       return;
2974   }
2975
2976   // Update the binding.
2977   state = state->set<RefBindings>(Sym, X);
2978   Pred = Builder.MakeNode(Dst, S, Pred, state);
2979
2980   // Did we cache out?
2981   if (!Pred)
2982     return;
2983
2984   // Update the autorelease counts.
2985   static unsigned autoreleasetag = 0;
2986   GenericNodeBuilderRefCount Bd(Builder, S, &autoreleasetag);
2987   bool stop = false;
2988   llvm::tie(Pred, state) = HandleAutoreleaseCounts(state , Bd, Pred, Eng, Sym,
2989                                                    X, stop);
2990
2991   // Did we cache out?
2992   if (!Pred || stop)
2993     return;
2994
2995   // Get the updated binding.
2996   T = state->get<RefBindings>(Sym);
2997   assert(T);
2998   X = *T;
2999
3000   // Consult the summary of the enclosing method.
3001   Decl const *CD = &Pred->getCodeDecl();
3002
3003   if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) {
3004     const RetainSummary &Summ = *Summaries.getMethodSummary(MD);
3005     return evalReturnWithRetEffect(Dst, Eng, Builder, S, 
3006                                    Pred, Summ.getRetEffect(), X,
3007                                    Sym, state);
3008   }
3009
3010   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) {
3011     if (!isa<CXXMethodDecl>(FD))
3012       if (const RetainSummary *Summ = Summaries.getSummary(FD))
3013         return evalReturnWithRetEffect(Dst, Eng, Builder, S, 
3014                                        Pred, Summ->getRetEffect(), X,
3015                                        Sym, state);
3016   }
3017 }
3018
3019 void CFRefCount::evalReturnWithRetEffect(ExplodedNodeSet &Dst, 
3020                                          ExprEngine &Eng,
3021                                          StmtNodeBuilder &Builder,
3022                                          const ReturnStmt *S,
3023                                          ExplodedNode *Pred,
3024                                          RetEffect RE, RefVal X,
3025                                          SymbolRef Sym, const GRState *state) {
3026   // Any leaks or other errors?
3027   if (X.isReturnedOwned() && X.getCount() == 0) {
3028     if (RE.getKind() != RetEffect::NoRet) {
3029       bool hasError = false;
3030       if (isGCEnabled() && RE.getObjKind() == RetEffect::ObjC) {
3031         // Things are more complicated with garbage collection.  If the
3032         // returned object is suppose to be an Objective-C object, we have
3033         // a leak (as the caller expects a GC'ed object) because no
3034         // method should return ownership unless it returns a CF object.
3035         hasError = true;
3036         X = X ^ RefVal::ErrorGCLeakReturned;
3037       }
3038       else if (!RE.isOwned()) {
3039         // Either we are using GC and the returned object is a CF type
3040         // or we aren't using GC.  In either case, we expect that the
3041         // enclosing method is expected to return ownership.
3042         hasError = true;
3043         X = X ^ RefVal::ErrorLeakReturned;
3044       }
3045
3046       if (hasError) {
3047         // Generate an error node.
3048         static int ReturnOwnLeakTag = 0;
3049         state = state->set<RefBindings>(Sym, X);
3050         ExplodedNode *N =
3051           Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3052                                         &ReturnOwnLeakTag), state, Pred);
3053         if (N) {
3054           CFRefReport *report =
3055             new CFRefLeakReport(*static_cast<CFRefBug*>(leakAtReturn), *this,
3056                                 N, Sym, Eng);
3057           BR->EmitReport(report);
3058         }
3059       }
3060     }
3061     return;
3062   }
3063
3064   if (X.isReturnedNotOwned()) {
3065     if (RE.isOwned()) {
3066       // Trying to return a not owned object to a caller expecting an
3067       // owned object.
3068
3069       static int ReturnNotOwnedForOwnedTag = 0;
3070       state = state->set<RefBindings>(Sym, X ^ RefVal::ErrorReturnedNotOwned);
3071       if (ExplodedNode *N =
3072           Builder.generateNode(PostStmt(S, Pred->getLocationContext(),
3073                                         &ReturnNotOwnedForOwnedTag),
3074                                state, Pred)) {
3075           CFRefReport *report =
3076               new CFRefReport(*static_cast<CFRefBug*>(returnNotOwnedForOwned),
3077                               *this, N, Sym);
3078           BR->EmitReport(report);
3079       }
3080     }
3081   }
3082 }
3083
3084 // Assumptions.
3085
3086 const GRState* CFRefCount::evalAssume(const GRState *state,
3087                                       SVal Cond, bool Assumption) {
3088
3089   // FIXME: We may add to the interface of evalAssume the list of symbols
3090   //  whose assumptions have changed.  For now we just iterate through the
3091   //  bindings and check if any of the tracked symbols are NULL.  This isn't
3092   //  too bad since the number of symbols we will track in practice are
3093   //  probably small and evalAssume is only called at branches and a few
3094   //  other places.
3095   RefBindings B = state->get<RefBindings>();
3096
3097   if (B.isEmpty())
3098     return state;
3099
3100   bool changed = false;
3101   RefBindings::Factory& RefBFactory = state->get_context<RefBindings>();
3102
3103   for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
3104     // Check if the symbol is null (or equal to any constant).
3105     // If this is the case, stop tracking the symbol.
3106     if (state->getSymVal(I.getKey())) {
3107       changed = true;
3108       B = RefBFactory.remove(B, I.getKey());
3109     }
3110   }
3111
3112   if (changed)
3113     state = state->set<RefBindings>(B);
3114
3115   return state;
3116 }
3117
3118 const GRState * CFRefCount::Update(const GRState * state, SymbolRef sym,
3119                               RefVal V, ArgEffect E,
3120                               RefVal::Kind& hasErr) {
3121
3122   // In GC mode [... release] and [... retain] do nothing.
3123   switch (E) {
3124     default: break;
3125     case IncRefMsg: E = isARCorGCEnabled() ? DoNothing : IncRef; break;
3126     case DecRefMsg: E = isARCorGCEnabled() ? DoNothing : DecRef; break;
3127     case MakeCollectable: E = isGCEnabled() ? DecRef : DoNothing; break;
3128     case NewAutoreleasePool: E = isGCEnabled() ? DoNothing :
3129                                                  NewAutoreleasePool; break;
3130   }
3131
3132   // Handle all use-after-releases.
3133   if (!isGCEnabled() && V.getKind() == RefVal::Released) {
3134     V = V ^ RefVal::ErrorUseAfterRelease;
3135     hasErr = V.getKind();
3136     return state->set<RefBindings>(sym, V);
3137   }
3138
3139   switch (E) {
3140     case DecRefMsg:
3141     case IncRefMsg:
3142     case MakeCollectable:
3143       assert(false &&
3144              "DecRefMsg/IncRefMsg/MakeCollectable already transformed");
3145       return state;
3146       
3147     case Dealloc:
3148       // Any use of -dealloc in GC is *bad*.
3149       if (isGCEnabled()) {
3150         V = V ^ RefVal::ErrorDeallocGC;
3151         hasErr = V.getKind();
3152         break;
3153       }
3154
3155       switch (V.getKind()) {
3156         default:
3157           assert(false && "Invalid case.");
3158         case RefVal::Owned:
3159           // The object immediately transitions to the released state.
3160           V = V ^ RefVal::Released;
3161           V.clearCounts();
3162           return state->set<RefBindings>(sym, V);
3163         case RefVal::NotOwned:
3164           V = V ^ RefVal::ErrorDeallocNotOwned;
3165           hasErr = V.getKind();
3166           break;
3167       }
3168       break;
3169
3170     case NewAutoreleasePool:
3171       assert(!isGCEnabled());
3172       return state->add<AutoreleaseStack>(sym);
3173
3174     case MayEscape:
3175       if (V.getKind() == RefVal::Owned) {
3176         V = V ^ RefVal::NotOwned;
3177         break;
3178       }
3179
3180       // Fall-through.
3181
3182     case DoNothingByRef:
3183     case DoNothing:
3184       return state;
3185
3186     case Autorelease:
3187       if (isGCEnabled())
3188         return state;
3189
3190       // Update the autorelease counts.
3191       state = SendAutorelease(state, ARCountFactory, sym);
3192       V = V.autorelease();
3193       break;
3194
3195     case StopTracking:
3196       return state->remove<RefBindings>(sym);
3197
3198     case IncRef:
3199       switch (V.getKind()) {
3200         default:
3201           assert(false);
3202
3203         case RefVal::Owned:
3204         case RefVal::NotOwned:
3205           V = V + 1;
3206           break;
3207         case RefVal::Released:
3208           // Non-GC cases are handled above.
3209           assert(isGCEnabled());
3210           V = (V ^ RefVal::Owned) + 1;
3211           break;
3212       }
3213       break;
3214
3215     case SelfOwn:
3216       V = V ^ RefVal::NotOwned;
3217       // Fall-through.
3218     case DecRef:
3219     case DecRefBridgedTransfered:
3220       switch (V.getKind()) {
3221         default:
3222           // case 'RefVal::Released' handled above.
3223           assert (false);
3224
3225         case RefVal::Owned:
3226           assert(V.getCount() > 0);
3227           if (V.getCount() == 1)
3228             V = V ^ (E == DecRefBridgedTransfered ? 
3229                       RefVal::NotOwned : RefVal::Released);
3230           V = V - 1;
3231           break;
3232
3233         case RefVal::NotOwned:
3234           if (V.getCount() > 0)
3235             V = V - 1;
3236           else {
3237             V = V ^ RefVal::ErrorReleaseNotOwned;
3238             hasErr = V.getKind();
3239           }
3240           break;
3241
3242         case RefVal::Released:
3243           // Non-GC cases are handled above.
3244           assert(isGCEnabled());
3245           V = V ^ RefVal::ErrorUseAfterRelease;
3246           hasErr = V.getKind();
3247           break;
3248       }
3249       break;
3250   }
3251   return state->set<RefBindings>(sym, V);
3252 }
3253
3254 //===----------------------------------------------------------------------===//
3255 // Handle dead symbols and end-of-path.
3256 //===----------------------------------------------------------------------===//
3257
3258 std::pair<ExplodedNode*, const GRState *>
3259 CFRefCount::HandleAutoreleaseCounts(const GRState * state, 
3260                                     GenericNodeBuilderRefCount Bd,
3261                                     ExplodedNode* Pred,
3262                                     ExprEngine &Eng,
3263                                     SymbolRef Sym, RefVal V, bool &stop) {
3264
3265   unsigned ACnt = V.getAutoreleaseCount();
3266   stop = false;
3267
3268   // No autorelease counts?  Nothing to be done.
3269   if (!ACnt)
3270     return std::make_pair(Pred, state);
3271
3272   assert(!isGCEnabled() && "Autorelease counts in GC mode?");
3273   unsigned Cnt = V.getCount();
3274
3275   // FIXME: Handle sending 'autorelease' to already released object.
3276
3277   if (V.getKind() == RefVal::ReturnedOwned)
3278     ++Cnt;
3279
3280   if (ACnt <= Cnt) {
3281     if (ACnt == Cnt) {
3282       V.clearCounts();
3283       if (V.getKind() == RefVal::ReturnedOwned)
3284         V = V ^ RefVal::ReturnedNotOwned;
3285       else
3286         V = V ^ RefVal::NotOwned;
3287     }
3288     else {
3289       V.setCount(Cnt - ACnt);
3290       V.setAutoreleaseCount(0);
3291     }
3292     state = state->set<RefBindings>(Sym, V);
3293     ExplodedNode *N = Bd.MakeNode(state, Pred);
3294     stop = (N == 0);
3295     return std::make_pair(N, state);
3296   }
3297
3298   // Woah!  More autorelease counts then retain counts left.
3299   // Emit hard error.
3300   stop = true;
3301   V = V ^ RefVal::ErrorOverAutorelease;
3302   state = state->set<RefBindings>(Sym, V);
3303
3304   if (ExplodedNode *N = Bd.MakeNode(state, Pred)) {
3305     N->markAsSink();
3306
3307     std::string sbuf;
3308     llvm::raw_string_ostream os(sbuf);
3309     os << "Object over-autoreleased: object was sent -autorelease ";
3310     if (V.getAutoreleaseCount() > 1)
3311       os << V.getAutoreleaseCount() << " times ";
3312     os << "but the object has a +" << V.getCount() << " retain count";
3313
3314     CFRefReport *report =
3315       new CFRefReport(*static_cast<CFRefBug*>(overAutorelease),
3316                       *this, N, Sym, os.str());
3317     BR->EmitReport(report);
3318   }
3319
3320   return std::make_pair((ExplodedNode*)0, state);
3321 }
3322
3323 const GRState *
3324 CFRefCount::HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V,
3325                               llvm::SmallVectorImpl<SymbolRef> &Leaked) {
3326
3327   bool hasLeak = V.isOwned() ||
3328   ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
3329
3330   if (!hasLeak)
3331     return state->remove<RefBindings>(sid);
3332
3333   Leaked.push_back(sid);
3334   return state->set<RefBindings>(sid, V ^ RefVal::ErrorLeak);
3335 }
3336
3337 ExplodedNode*
3338 CFRefCount::ProcessLeaks(const GRState * state,
3339                          llvm::SmallVectorImpl<SymbolRef> &Leaked,
3340                          GenericNodeBuilderRefCount &Builder,
3341                          ExprEngine& Eng,
3342                          ExplodedNode *Pred) {
3343
3344   if (Leaked.empty())
3345     return Pred;
3346
3347   // Generate an intermediate node representing the leak point.
3348   ExplodedNode *N = Builder.MakeNode(state, Pred);
3349
3350   if (N) {
3351     for (llvm::SmallVectorImpl<SymbolRef>::iterator
3352          I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
3353
3354       CFRefBug *BT = static_cast<CFRefBug*>(Pred ? leakWithinFunction
3355                                                  : leakAtReturn);
3356       assert(BT && "BugType not initialized.");
3357       CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, *I, Eng);
3358       BR->EmitReport(report);
3359     }
3360   }
3361
3362   return N;
3363 }
3364
3365 void CFRefCount::evalEndPath(ExprEngine& Eng,
3366                              EndOfFunctionNodeBuilder& Builder) {
3367
3368   const GRState *state = Builder.getState();
3369   GenericNodeBuilderRefCount Bd(Builder);
3370   RefBindings B = state->get<RefBindings>();
3371   ExplodedNode *Pred = 0;
3372
3373   for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
3374     bool stop = false;
3375     llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3376                                                      (*I).first,
3377                                                      (*I).second, stop);
3378
3379     if (stop)
3380       return;
3381   }
3382
3383   B = state->get<RefBindings>();
3384   llvm::SmallVector<SymbolRef, 10> Leaked;
3385
3386   for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
3387     state = HandleSymbolDeath(state, (*I).first, (*I).second, Leaked);
3388
3389   ProcessLeaks(state, Leaked, Bd, Eng, Pred);
3390 }
3391
3392 void CFRefCount::evalDeadSymbols(ExplodedNodeSet& Dst,
3393                                  ExprEngine& Eng,
3394                                  StmtNodeBuilder& Builder,
3395                                  ExplodedNode* Pred,
3396                                  const GRState* state,
3397                                  SymbolReaper& SymReaper) {
3398   const Stmt *S = Builder.getStmt();
3399   RefBindings B = state->get<RefBindings>();
3400
3401   // Update counts from autorelease pools
3402   for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3403        E = SymReaper.dead_end(); I != E; ++I) {
3404     SymbolRef Sym = *I;
3405     if (const RefVal* T = B.lookup(Sym)){
3406       // Use the symbol as the tag.
3407       // FIXME: This might not be as unique as we would like.
3408       GenericNodeBuilderRefCount Bd(Builder, S, Sym);
3409       bool stop = false;
3410       llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng,
3411                                                        Sym, *T, stop);
3412       if (stop)
3413         return;
3414     }
3415   }
3416
3417   B = state->get<RefBindings>();
3418   llvm::SmallVector<SymbolRef, 10> Leaked;
3419
3420   for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3421        E = SymReaper.dead_end(); I != E; ++I) {
3422       if (const RefVal* T = B.lookup(*I))
3423         state = HandleSymbolDeath(state, *I, *T, Leaked);
3424   }
3425
3426   static unsigned LeakPPTag = 0;
3427   {
3428     GenericNodeBuilderRefCount Bd(Builder, S, &LeakPPTag);
3429     Pred = ProcessLeaks(state, Leaked, Bd, Eng, Pred);
3430   }
3431
3432   // Did we cache out?
3433   if (!Pred)
3434     return;
3435
3436   // Now generate a new node that nukes the old bindings.
3437   RefBindings::Factory& F = state->get_context<RefBindings>();
3438
3439   for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
3440        E = SymReaper.dead_end(); I!=E; ++I) B = F.remove(B, *I);
3441
3442   state = state->set<RefBindings>(B);
3443   Builder.MakeNode(Dst, S, Pred, state);
3444 }
3445
3446 void CFRefCount::ProcessNonLeakError(ExplodedNodeSet& Dst,
3447                                      StmtNodeBuilder& Builder,
3448                                      const Expr* NodeExpr, 
3449                                      SourceRange ErrorRange,
3450                                      ExplodedNode* Pred,
3451                                      const GRState* St,
3452                                      RefVal::Kind hasErr, SymbolRef Sym) {
3453   Builder.BuildSinks = true;
3454   ExplodedNode *N  = Builder.MakeNode(Dst, NodeExpr, Pred, St);
3455
3456   if (!N)
3457     return;
3458
3459   CFRefBug *BT = 0;
3460
3461   switch (hasErr) {
3462     default:
3463       assert(false && "Unhandled error.");
3464       return;
3465     case RefVal::ErrorUseAfterRelease:
3466       BT = static_cast<CFRefBug*>(useAfterRelease);
3467       break;
3468     case RefVal::ErrorReleaseNotOwned:
3469       BT = static_cast<CFRefBug*>(releaseNotOwned);
3470       break;
3471     case RefVal::ErrorDeallocGC:
3472       BT = static_cast<CFRefBug*>(deallocGC);
3473       break;
3474     case RefVal::ErrorDeallocNotOwned:
3475       BT = static_cast<CFRefBug*>(deallocNotOwned);
3476       break;
3477   }
3478
3479   CFRefReport *report = new CFRefReport(*BT, *this, N, Sym);
3480   report->addRange(ErrorRange);
3481   BR->EmitReport(report);
3482 }
3483
3484 //===----------------------------------------------------------------------===//
3485 // Pieces of the retain/release checker implemented using a CheckerVisitor.
3486 // More pieces of the retain/release checker will be migrated to this interface
3487 // (ideally, all of it some day).
3488 //===----------------------------------------------------------------------===//
3489
3490 namespace {
3491 class RetainReleaseChecker
3492   : public Checker< check::PostStmt<BlockExpr>,
3493                     check::PostStmt<CastExpr>,
3494                     check::RegionChanges > {
3495 public:
3496     bool wantsRegionUpdate;
3497     
3498     RetainReleaseChecker() : wantsRegionUpdate(true) {}
3499     
3500     
3501     void checkPostStmt(const BlockExpr *BE, CheckerContext &C) const;
3502                       
3503     void checkPostStmt(const CastExpr *CE, CheckerContext &C) const;
3504
3505     const GRState *checkRegionChanges(const GRState *state,
3506                             const StoreManager::InvalidatedSymbols *invalidated,
3507                                       const MemRegion * const *begin,
3508                                       const MemRegion * const *end) const;
3509                                           
3510     bool wantsRegionChangeUpdate(const GRState *state) const {
3511       return wantsRegionUpdate;
3512     }
3513 };
3514 } // end anonymous namespace
3515
3516 const GRState *
3517 RetainReleaseChecker::checkRegionChanges(const GRState *state,
3518                             const StoreManager::InvalidatedSymbols *invalidated,
3519                                          const MemRegion * const *begin,
3520                                          const MemRegion * const *end) const {
3521   if (!invalidated)
3522     return state;
3523
3524   for (StoreManager::InvalidatedSymbols::const_iterator I=invalidated->begin(),
3525        E = invalidated->end(); I!=E; ++I) {
3526     SymbolRef sym = *I;
3527     if (WhitelistedSymbols.count(sym))
3528       continue;
3529     // Remove any existing reference-count binding.
3530     state = state->remove<RefBindings>(sym);
3531   }
3532   return state;
3533 }
3534
3535 void RetainReleaseChecker::checkPostStmt(const BlockExpr *BE,
3536                                          CheckerContext &C) const {
3537
3538   // Scan the BlockDecRefExprs for any object the retain/release checker
3539   // may be tracking.
3540   if (!BE->getBlockDecl()->hasCaptures())
3541     return;
3542
3543   const GRState *state = C.getState();
3544   const BlockDataRegion *R =
3545     cast<BlockDataRegion>(state->getSVal(BE).getAsRegion());
3546
3547   BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
3548                                             E = R->referenced_vars_end();
3549
3550   if (I == E)
3551     return;
3552
3553   // FIXME: For now we invalidate the tracking of all symbols passed to blocks
3554   // via captured variables, even though captured variables result in a copy
3555   // and in implicit increment/decrement of a retain count.
3556   llvm::SmallVector<const MemRegion*, 10> Regions;
3557   const LocationContext *LC = C.getPredecessor()->getLocationContext();
3558   MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager();
3559
3560   for ( ; I != E; ++I) {
3561     const VarRegion *VR = *I;
3562     if (VR->getSuperRegion() == R) {
3563       VR = MemMgr.getVarRegion(VR->getDecl(), LC);
3564     }
3565     Regions.push_back(VR);
3566   }
3567
3568   state =
3569     state->scanReachableSymbols<StopTrackingCallback>(Regions.data(),
3570                                     Regions.data() + Regions.size()).getState();
3571   C.addTransition(state);
3572 }
3573
3574 void RetainReleaseChecker::checkPostStmt(const CastExpr *CE,
3575                                          CheckerContext &C) const {
3576   const ObjCBridgedCastExpr *BE = dyn_cast<ObjCBridgedCastExpr>(CE);
3577   if (!BE)
3578     return;
3579   
3580   ArgEffect AE = IncRef;
3581   
3582   switch (BE->getBridgeKind()) {
3583     case clang::OBC_Bridge:
3584       // Do nothing.
3585       return;
3586     case clang::OBC_BridgeRetained:
3587       AE = IncRef;
3588       break;      
3589     case clang::OBC_BridgeTransfer:
3590       AE = DecRefBridgedTransfered;
3591       break;
3592   }
3593   
3594   const GRState *state = C.getState();
3595   SymbolRef Sym = state->getSVal(CE).getAsLocSymbol();
3596   if (!Sym)
3597     return;
3598   const RefVal* T = state->get<RefBindings>(Sym);
3599   if (!T)
3600     return;
3601
3602   // This is gross.  Once the checker and CFRefCount are unified,
3603   // this will go away.
3604   CFRefCount &cf = static_cast<CFRefCount&>(C.getEngine().getTF());
3605   RefVal::Kind hasErr = (RefVal::Kind) 0;
3606   state = cf.Update(state, Sym, *T, AE, hasErr);
3607   
3608   if (hasErr) {
3609     
3610     return;
3611   }
3612
3613   C.generateNode(state);
3614 }
3615
3616 //===----------------------------------------------------------------------===//
3617 // Transfer function creation for external clients.
3618 //===----------------------------------------------------------------------===//
3619
3620 void CFRefCount::RegisterChecks(ExprEngine& Eng) {
3621   BugReporter &BR = Eng.getBugReporter();
3622
3623   useAfterRelease = new UseAfterRelease(this);
3624   BR.Register(useAfterRelease);
3625
3626   releaseNotOwned = new BadRelease(this);
3627   BR.Register(releaseNotOwned);
3628
3629   deallocGC = new DeallocGC(this);
3630   BR.Register(deallocGC);
3631
3632   deallocNotOwned = new DeallocNotOwned(this);
3633   BR.Register(deallocNotOwned);
3634
3635   overAutorelease = new OverAutorelease(this);
3636   BR.Register(overAutorelease);
3637
3638   returnNotOwnedForOwned = new ReturnedNotOwnedForOwned(this);
3639   BR.Register(returnNotOwnedForOwned);
3640
3641   // First register "return" leaks.
3642   const char* name = 0;
3643
3644   if (isGCEnabled())
3645     name = "Leak of returned object when using garbage collection";
3646   else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
3647     name = "Leak of returned object when not using garbage collection (GC) in "
3648     "dual GC/non-GC code";
3649   else {
3650     assert(getLangOptions().getGCMode() == LangOptions::NonGC);
3651     name = "Leak of returned object";
3652   }
3653
3654   // Leaks should not be reported if they are post-dominated by a sink.
3655   leakAtReturn = new LeakAtReturn(this, name);
3656   leakAtReturn->setSuppressOnSink(true);
3657   BR.Register(leakAtReturn);
3658
3659   // Second, register leaks within a function/method.
3660   if (isGCEnabled())
3661     name = "Leak of object when using garbage collection";
3662   else if (getLangOptions().getGCMode() == LangOptions::HybridGC)
3663     name = "Leak of object when not using garbage collection (GC) in "
3664     "dual GC/non-GC code";
3665   else {
3666     assert(getLangOptions().getGCMode() == LangOptions::NonGC);
3667     name = "Leak";
3668   }
3669
3670   // Leaks should not be reported if they are post-dominated by sinks.
3671   leakWithinFunction = new LeakWithinFunction(this, name);
3672   leakWithinFunction->setSuppressOnSink(true);
3673   BR.Register(leakWithinFunction);
3674
3675   // Save the reference to the BugReporter.
3676   this->BR = &BR;
3677
3678   // Register the RetainReleaseChecker with the ExprEngine object.
3679   // Functionality in CFRefCount will be migrated to RetainReleaseChecker
3680   // over time.
3681   // FIXME: HACK! Remove TransferFuncs and turn all of CFRefCount into fully
3682   // using the checker mechanism.
3683   Eng.getCheckerManager().registerChecker<RetainReleaseChecker>();
3684 }
3685
3686 TransferFuncs* ento::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
3687                                          const LangOptions& lopts) {
3688   return new CFRefCount(Ctx, GCEnabled, lopts);
3689 }