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