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