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