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