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