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