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