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