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 | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 15 | #include "clang/Basic/LangOptions.h" |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRExprEngineBuilders.h" |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/PathDiagnostic.h" |
| 22 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/PathSensitive/SymbolManager.h" |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +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 | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 33 | #include <stdarg.h> |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace clang; |
Ted Kremenek | 5c74d50 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | // Utility functions. |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Ted Kremenek | 5c74d50 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 41 | // The "fundamental rule" for naming conventions of methods: |
| 42 | // (url broken into two lines) |
| 43 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ |
| 44 | // MemoryMgmt/Tasks/MemoryManagementRules.html |
| 45 | // |
| 46 | // "You take ownership of an object if you create it using a method whose name |
| 47 | // begins with “alloc” or “new” or contains “copy” (for example, alloc, |
| 48 | // newObject, or mutableCopy), or if you send it a retain message. You are |
| 49 | // responsible for relinquishing ownership of objects you own using release |
| 50 | // or autorelease. Any other time you receive an object, you must |
| 51 | // not release it." |
| 52 | // |
Ted Kremenek | b80976c | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 53 | |
| 54 | using llvm::CStrInCStrNoCase; |
Ted Kremenek | 39868cd | 2009-02-21 18:26:02 +0000 | [diff] [blame] | 55 | using llvm::StringsEqualNoCase; |
Ted Kremenek | b80976c | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 56 | |
| 57 | enum NamingConvention { NoConvention, CreateRule, InitRule }; |
| 58 | |
| 59 | static inline bool isWordEnd(char ch, char prev, char next) { |
| 60 | return ch == '\0' |
| 61 | || (islower(prev) && isupper(ch)) // xxxC |
| 62 | || (isupper(prev) && isupper(ch) && islower(next)) // XXCreate |
| 63 | || !isalpha(ch); |
| 64 | } |
| 65 | |
| 66 | static inline const char* parseWord(const char* s) { |
| 67 | char ch = *s, prev = '\0'; |
| 68 | assert(ch != '\0'); |
| 69 | char next = *(s+1); |
| 70 | while (!isWordEnd(ch, prev, next)) { |
| 71 | prev = ch; |
| 72 | ch = next; |
| 73 | next = *((++s)+1); |
| 74 | } |
| 75 | return s; |
| 76 | } |
| 77 | |
Ted Kremenek | 7db1604 | 2009-05-15 15:49:00 +0000 | [diff] [blame] | 78 | static NamingConvention deriveNamingConvention(Selector S) { |
| 79 | IdentifierInfo *II = S.getIdentifierInfoForSlot(0); |
| 80 | |
| 81 | if (!II) |
| 82 | return NoConvention; |
| 83 | |
| 84 | const char *s = II->getName(); |
| 85 | |
Ted Kremenek | b80976c | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 86 | // A method/function name may contain a prefix. We don't know it is there, |
| 87 | // however, until we encounter the first '_'. |
| 88 | bool InPossiblePrefix = true; |
| 89 | bool AtBeginning = true; |
| 90 | NamingConvention C = NoConvention; |
| 91 | |
| 92 | while (*s != '\0') { |
| 93 | // Skip '_'. |
| 94 | if (*s == '_') { |
| 95 | if (InPossiblePrefix) { |
| 96 | InPossiblePrefix = false; |
| 97 | AtBeginning = true; |
| 98 | // Discard whatever 'convention' we |
| 99 | // had already derived since it occurs |
| 100 | // in the prefix. |
| 101 | C = NoConvention; |
| 102 | } |
| 103 | ++s; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | // Skip numbers, ':', etc. |
| 108 | if (!isalpha(*s)) { |
| 109 | ++s; |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | const char *wordEnd = parseWord(s); |
| 114 | assert(wordEnd > s); |
| 115 | unsigned len = wordEnd - s; |
| 116 | |
| 117 | switch (len) { |
| 118 | default: |
| 119 | break; |
| 120 | case 3: |
| 121 | // Methods starting with 'new' follow the create rule. |
Ted Kremenek | 39868cd | 2009-02-21 18:26:02 +0000 | [diff] [blame] | 122 | if (AtBeginning && StringsEqualNoCase("new", s, len)) |
Ted Kremenek | b80976c | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 123 | C = CreateRule; |
| 124 | break; |
| 125 | case 4: |
| 126 | // Methods starting with 'alloc' or contain 'copy' follow the |
| 127 | // create rule |
Ted Kremenek | 8be2a67 | 2009-03-13 20:27:06 +0000 | [diff] [blame] | 128 | if (C == NoConvention && StringsEqualNoCase("copy", s, len)) |
Ted Kremenek | b80976c | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 129 | C = CreateRule; |
| 130 | else // Methods starting with 'init' follow the init rule. |
Ted Kremenek | 39868cd | 2009-02-21 18:26:02 +0000 | [diff] [blame] | 131 | if (AtBeginning && StringsEqualNoCase("init", s, len)) |
Ted Kremenek | 8be2a67 | 2009-03-13 20:27:06 +0000 | [diff] [blame] | 132 | C = InitRule; |
| 133 | break; |
| 134 | case 5: |
| 135 | if (AtBeginning && StringsEqualNoCase("alloc", s, len)) |
| 136 | C = CreateRule; |
Ted Kremenek | b80976c | 2009-02-21 05:13:43 +0000 | [diff] [blame] | 137 | break; |
| 138 | } |
| 139 | |
| 140 | // If we aren't in the prefix and have a derived convention then just |
| 141 | // return it now. |
| 142 | if (!InPossiblePrefix && C != NoConvention) |
| 143 | return C; |
| 144 | |
| 145 | AtBeginning = false; |
| 146 | s = wordEnd; |
| 147 | } |
| 148 | |
| 149 | // We will get here if there wasn't more than one word |
| 150 | // after the prefix. |
| 151 | return C; |
| 152 | } |
| 153 | |
Ted Kremenek | 7db1604 | 2009-05-15 15:49:00 +0000 | [diff] [blame] | 154 | static bool followsFundamentalRule(Selector S) { |
| 155 | return deriveNamingConvention(S) == CreateRule; |
Ted Kremenek | 4c79e55 | 2008-11-05 16:54:44 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 158 | static const ObjCMethodDecl* |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 159 | ResolveToInterfaceMethodDecl(const ObjCMethodDecl *MD) { |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 160 | ObjCInterfaceDecl *ID = |
| 161 | const_cast<ObjCInterfaceDecl*>(MD->getClassInterface()); |
| 162 | |
| 163 | return MD->isInstanceMethod() |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 164 | ? ID->lookupInstanceMethod(MD->getSelector()) |
| 165 | : ID->lookupClassMethod(MD->getSelector()); |
Ted Kremenek | 4c79e55 | 2008-11-05 16:54:44 +0000 | [diff] [blame] | 166 | } |
Ted Kremenek | 5c74d50 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 168 | namespace { |
| 169 | class VISIBILITY_HIDDEN GenericNodeBuilder { |
| 170 | GRStmtNodeBuilder<GRState> *SNB; |
| 171 | Stmt *S; |
| 172 | const void *tag; |
| 173 | GREndPathNodeBuilder<GRState> *ENB; |
| 174 | public: |
| 175 | GenericNodeBuilder(GRStmtNodeBuilder<GRState> &snb, Stmt *s, |
| 176 | const void *t) |
| 177 | : SNB(&snb), S(s), tag(t), ENB(0) {} |
| 178 | GenericNodeBuilder(GREndPathNodeBuilder<GRState> &enb) |
| 179 | : SNB(0), S(0), tag(0), ENB(&enb) {} |
| 180 | |
| 181 | ExplodedNode<GRState> *MakeNode(const GRState *state, |
| 182 | ExplodedNode<GRState> *Pred) { |
| 183 | if (SNB) |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 184 | return SNB->generateNode(PostStmt(S, tag), state, Pred); |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 185 | |
| 186 | assert(ENB); |
Ted Kremenek | 80c2418 | 2009-05-09 00:44:07 +0000 | [diff] [blame] | 187 | return ENB->generateNode(state, Pred); |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 188 | } |
| 189 | }; |
| 190 | } // end anonymous namespace |
| 191 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 192 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 193 | // Selector creation functions. |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 194 | //===----------------------------------------------------------------------===// |
| 195 | |
Ted Kremenek | b83e02e | 2008-05-01 18:31:44 +0000 | [diff] [blame] | 196 | static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 197 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 198 | return Ctx.Selectors.getSelector(0, &II); |
| 199 | } |
| 200 | |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 201 | static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { |
| 202 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 203 | return Ctx.Selectors.getSelector(1, &II); |
| 204 | } |
| 205 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
| 207 | // Type querying functions. |
| 208 | //===----------------------------------------------------------------------===// |
| 209 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 210 | static bool hasPrefix(const char* s, const char* prefix) { |
| 211 | if (!prefix) |
| 212 | return true; |
Ted Kremenek | 0fcbf8e | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 213 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 214 | char c = *s; |
| 215 | char cP = *prefix; |
Ted Kremenek | 0fcbf8e | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 217 | while (c != '\0' && cP != '\0') { |
| 218 | if (c != cP) break; |
| 219 | c = *(++s); |
| 220 | cP = *(++prefix); |
| 221 | } |
Ted Kremenek | 0fcbf8e | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 223 | return cP == '\0'; |
Ted Kremenek | 0fcbf8e | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 226 | static bool hasSuffix(const char* s, const char* suffix) { |
| 227 | const char* loc = strstr(s, suffix); |
| 228 | return loc && strcmp(suffix, loc) == 0; |
| 229 | } |
| 230 | |
| 231 | static bool isRefType(QualType RetTy, const char* prefix, |
| 232 | ASTContext* Ctx = 0, const char* name = 0) { |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 233 | |
Ted Kremenek | 6738b73 | 2009-05-12 04:53:03 +0000 | [diff] [blame] | 234 | // Recursively walk the typedef stack, allowing typedefs of reference types. |
| 235 | while (1) { |
| 236 | if (TypedefType* TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) { |
| 237 | const char* TDName = TD->getDecl()->getIdentifier()->getName(); |
| 238 | if (hasPrefix(TDName, prefix) && hasSuffix(TDName, "Ref")) |
| 239 | return true; |
| 240 | |
| 241 | RetTy = TD->getDecl()->getUnderlyingType(); |
| 242 | continue; |
| 243 | } |
| 244 | break; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | if (!Ctx || !name) |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 248 | return false; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 249 | |
| 250 | // Is the type void*? |
| 251 | const PointerType* PT = RetTy->getAsPointerType(); |
| 252 | if (!(PT->getPointeeType().getUnqualifiedType() == Ctx->VoidTy)) |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 253 | return false; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 254 | |
| 255 | // Does the name start with the prefix? |
| 256 | return hasPrefix(name, prefix); |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 259 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 260 | // Primitives used for constructing summaries for function/method calls. |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 261 | //===----------------------------------------------------------------------===// |
| 262 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 263 | /// ArgEffect is used to summarize a function/method call's effect on a |
| 264 | /// particular argument. |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 265 | enum ArgEffect { Autorelease, Dealloc, DecRef, DecRefMsg, DoNothing, |
| 266 | DoNothingByRef, IncRefMsg, IncRef, MakeCollectable, MayEscape, |
| 267 | NewAutoreleasePool, SelfOwn, StopTracking }; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 269 | namespace llvm { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 270 | template <> struct FoldingSetTrait<ArgEffect> { |
| 271 | static inline void Profile(const ArgEffect X, FoldingSetNodeID& ID) { |
| 272 | ID.AddInteger((unsigned) X); |
| 273 | } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 274 | }; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 275 | } // end llvm namespace |
| 276 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 277 | /// ArgEffects summarizes the effects of a function/method call on all of |
| 278 | /// its arguments. |
| 279 | typedef llvm::ImmutableMap<unsigned,ArgEffect> ArgEffects; |
| 280 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 281 | namespace { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 282 | |
| 283 | /// RetEffect is used to summarize a function/method call's behavior with |
| 284 | /// respect to its return value. |
| 285 | class VISIBILITY_HIDDEN RetEffect { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 286 | public: |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 287 | enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol, |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 288 | NotOwnedSymbol, GCNotOwnedSymbol, ReceiverAlias, |
| 289 | OwnedWhenTrackedReceiver }; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 290 | |
| 291 | enum ObjKind { CF, ObjC, AnyObj }; |
| 292 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 293 | private: |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 294 | Kind K; |
| 295 | ObjKind O; |
| 296 | unsigned index; |
| 297 | |
| 298 | RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {} |
| 299 | RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {} |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 300 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 301 | public: |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 302 | Kind getKind() const { return K; } |
| 303 | |
| 304 | ObjKind getObjKind() const { return O; } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 305 | |
| 306 | unsigned getIndex() const { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 307 | assert(getKind() == Alias); |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 308 | return index; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 309 | } |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 311 | bool isOwned() const { |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 312 | return K == OwnedSymbol || K == OwnedAllocatedSymbol || |
| 313 | K == OwnedWhenTrackedReceiver; |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 314 | } |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 315 | |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 316 | static RetEffect MakeOwnedWhenTrackedReceiver() { |
| 317 | return RetEffect(OwnedWhenTrackedReceiver, ObjC); |
| 318 | } |
| 319 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 320 | static RetEffect MakeAlias(unsigned Idx) { |
| 321 | return RetEffect(Alias, Idx); |
| 322 | } |
| 323 | static RetEffect MakeReceiverAlias() { |
| 324 | return RetEffect(ReceiverAlias); |
| 325 | } |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 326 | static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) { |
| 327 | return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 328 | } |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 329 | static RetEffect MakeNotOwned(ObjKind o) { |
| 330 | return RetEffect(NotOwnedSymbol, o); |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 331 | } |
| 332 | static RetEffect MakeGCNotOwned() { |
| 333 | return RetEffect(GCNotOwnedSymbol, ObjC); |
| 334 | } |
| 335 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 336 | static RetEffect MakeNoRet() { |
| 337 | return RetEffect(NoRet); |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 338 | } |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 339 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 340 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 341 | ID.AddInteger((unsigned)K); |
| 342 | ID.AddInteger((unsigned)O); |
| 343 | ID.AddInteger(index); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 344 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 345 | }; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 346 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 348 | class VISIBILITY_HIDDEN RetainSummary { |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 349 | /// Args - an ordered vector of (index, ArgEffect) pairs, where index |
| 350 | /// specifies the argument (starting from 0). This can be sparsely |
| 351 | /// populated; arguments with no entry in Args use 'DefaultArgEffect'. |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 352 | ArgEffects Args; |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 353 | |
| 354 | /// DefaultArgEffect - The default ArgEffect to apply to arguments that |
| 355 | /// do not have an entry in Args. |
| 356 | ArgEffect DefaultArgEffect; |
| 357 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 358 | /// Receiver - If this summary applies to an Objective-C message expression, |
| 359 | /// this is the effect applied to the state of the receiver. |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 360 | ArgEffect Receiver; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 361 | |
| 362 | /// Ret - The effect on the return value. Used to indicate if the |
| 363 | /// function/method call returns a new tracked symbol, returns an |
| 364 | /// alias of one of the arguments in the call, and so on. |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 365 | RetEffect Ret; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 367 | /// EndPath - Indicates that execution of this method/function should |
| 368 | /// terminate the simulation of a path. |
| 369 | bool EndPath; |
| 370 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 371 | public: |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 372 | RetainSummary(ArgEffects A, RetEffect R, ArgEffect defaultEff, |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 373 | ArgEffect ReceiverEff, bool endpath = false) |
| 374 | : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R), |
| 375 | EndPath(endpath) {} |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 376 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 377 | /// getArg - Return the argument effect on the argument specified by |
| 378 | /// idx (starting from 0). |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 379 | ArgEffect getArg(unsigned idx) const { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 380 | if (const ArgEffect *AE = Args.lookup(idx)) |
| 381 | return *AE; |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 382 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 383 | return DefaultArgEffect; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 386 | /// setDefaultArgEffect - Set the default argument effect. |
| 387 | void setDefaultArgEffect(ArgEffect E) { |
| 388 | DefaultArgEffect = E; |
| 389 | } |
| 390 | |
| 391 | /// setArg - Set the argument effect on the argument specified by idx. |
| 392 | void setArgEffect(ArgEffects::Factory& AF, unsigned idx, ArgEffect E) { |
| 393 | Args = AF.Add(Args, idx, E); |
| 394 | } |
| 395 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 396 | /// getRetEffect - Returns the effect on the return value of the call. |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 397 | RetEffect getRetEffect() const { return Ret; } |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 398 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 399 | /// setRetEffect - Set the effect of the return value of the call. |
| 400 | void setRetEffect(RetEffect E) { Ret = E; } |
| 401 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 402 | /// isEndPath - Returns true if executing the given method/function should |
| 403 | /// terminate the path. |
| 404 | bool isEndPath() const { return EndPath; } |
| 405 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 406 | /// getReceiverEffect - Returns the effect on the receiver of the call. |
| 407 | /// This is only meaningful if the summary applies to an ObjCMessageExpr*. |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 408 | ArgEffect getReceiverEffect() const { return Receiver; } |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 409 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 410 | /// setReceiverEffect - Set the effect on the receiver of the call. |
| 411 | void setReceiverEffect(ArgEffect E) { Receiver = E; } |
| 412 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 413 | typedef ArgEffects::iterator ExprIterator; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 414 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 415 | ExprIterator begin_args() const { return Args.begin(); } |
| 416 | ExprIterator end_args() const { return Args.end(); } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 417 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 418 | static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects A, |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 419 | RetEffect RetEff, ArgEffect DefaultEff, |
Ted Kremenek | 2d1086c | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 420 | ArgEffect ReceiverEff, bool EndPath) { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 421 | ID.Add(A); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 422 | ID.Add(RetEff); |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 423 | ID.AddInteger((unsigned) DefaultEff); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 424 | ID.AddInteger((unsigned) ReceiverEff); |
Ted Kremenek | 2d1086c | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 425 | ID.AddInteger((unsigned) EndPath); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 2d1086c | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 429 | Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 430 | } |
| 431 | }; |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 432 | } // end anonymous namespace |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 434 | //===----------------------------------------------------------------------===// |
| 435 | // Data structures for constructing summaries. |
| 436 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 53301ba | 2008-06-24 03:49:48 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 438 | namespace { |
| 439 | class VISIBILITY_HIDDEN ObjCSummaryKey { |
| 440 | IdentifierInfo* II; |
| 441 | Selector S; |
| 442 | public: |
| 443 | ObjCSummaryKey(IdentifierInfo* ii, Selector s) |
| 444 | : II(ii), S(s) {} |
| 445 | |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 446 | ObjCSummaryKey(const ObjCInterfaceDecl* d, Selector s) |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 447 | : II(d ? d->getIdentifier() : 0), S(s) {} |
Ted Kremenek | 70b6a83 | 2009-05-13 18:16:01 +0000 | [diff] [blame] | 448 | |
| 449 | ObjCSummaryKey(const ObjCInterfaceDecl* d, IdentifierInfo *ii, Selector s) |
| 450 | : II(d ? d->getIdentifier() : ii), S(s) {} |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 451 | |
| 452 | ObjCSummaryKey(Selector s) |
| 453 | : II(0), S(s) {} |
| 454 | |
| 455 | IdentifierInfo* getIdentifier() const { return II; } |
| 456 | Selector getSelector() const { return S; } |
| 457 | }; |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | namespace llvm { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 461 | template <> struct DenseMapInfo<ObjCSummaryKey> { |
| 462 | static inline ObjCSummaryKey getEmptyKey() { |
| 463 | return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(), |
| 464 | DenseMapInfo<Selector>::getEmptyKey()); |
| 465 | } |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 466 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 467 | static inline ObjCSummaryKey getTombstoneKey() { |
| 468 | return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(), |
| 469 | DenseMapInfo<Selector>::getTombstoneKey()); |
| 470 | } |
| 471 | |
| 472 | static unsigned getHashValue(const ObjCSummaryKey &V) { |
| 473 | return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier()) |
| 474 | & 0x88888888) |
| 475 | | (DenseMapInfo<Selector>::getHashValue(V.getSelector()) |
| 476 | & 0x55555555); |
| 477 | } |
| 478 | |
| 479 | static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) { |
| 480 | return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(), |
| 481 | RHS.getIdentifier()) && |
| 482 | DenseMapInfo<Selector>::isEqual(LHS.getSelector(), |
| 483 | RHS.getSelector()); |
| 484 | } |
| 485 | |
| 486 | static bool isPod() { |
| 487 | return DenseMapInfo<ObjCInterfaceDecl*>::isPod() && |
| 488 | DenseMapInfo<Selector>::isPod(); |
| 489 | } |
| 490 | }; |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 491 | } // end llvm namespace |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 492 | |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 493 | namespace { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 494 | class VISIBILITY_HIDDEN ObjCSummaryCache { |
| 495 | typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy; |
| 496 | MapTy M; |
| 497 | public: |
| 498 | ObjCSummaryCache() {} |
| 499 | |
| 500 | typedef MapTy::iterator iterator; |
| 501 | |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 502 | iterator find(const ObjCInterfaceDecl* D, IdentifierInfo *ClsName, |
| 503 | Selector S) { |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 504 | // Lookup the method using the decl for the class @interface. If we |
| 505 | // have no decl, lookup using the class name. |
| 506 | return D ? find(D, S) : find(ClsName, S); |
| 507 | } |
| 508 | |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 509 | iterator find(const ObjCInterfaceDecl* D, Selector S) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 510 | // Do a lookup with the (D,S) pair. If we find a match return |
| 511 | // the iterator. |
| 512 | ObjCSummaryKey K(D, S); |
| 513 | MapTy::iterator I = M.find(K); |
| 514 | |
| 515 | if (I != M.end() || !D) |
| 516 | return I; |
| 517 | |
| 518 | // Walk the super chain. If we find a hit with a parent, we'll end |
| 519 | // up returning that summary. We actually allow that key (null,S), as |
| 520 | // we cache summaries for the null ObjCInterfaceDecl* to allow us to |
| 521 | // generate initial summaries without having to worry about NSObject |
| 522 | // being declared. |
| 523 | // FIXME: We may change this at some point. |
| 524 | for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) { |
| 525 | if ((I = M.find(ObjCSummaryKey(C, S))) != M.end()) |
| 526 | break; |
| 527 | |
| 528 | if (!C) |
| 529 | return I; |
| 530 | } |
| 531 | |
| 532 | // Cache the summary with original key to make the next lookup faster |
| 533 | // and return the iterator. |
| 534 | M[K] = I->second; |
| 535 | return I; |
| 536 | } |
| 537 | |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 538 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 539 | iterator find(Expr* Receiver, Selector S) { |
| 540 | return find(getReceiverDecl(Receiver), S); |
| 541 | } |
| 542 | |
| 543 | iterator find(IdentifierInfo* II, Selector S) { |
| 544 | // FIXME: Class method lookup. Right now we dont' have a good way |
| 545 | // of going between IdentifierInfo* and the class hierarchy. |
| 546 | iterator I = M.find(ObjCSummaryKey(II, S)); |
| 547 | return I == M.end() ? M.find(ObjCSummaryKey(S)) : I; |
| 548 | } |
| 549 | |
| 550 | ObjCInterfaceDecl* getReceiverDecl(Expr* E) { |
| 551 | |
| 552 | const PointerType* PT = E->getType()->getAsPointerType(); |
| 553 | if (!PT) return 0; |
| 554 | |
| 555 | ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 556 | if (!OI) return 0; |
| 557 | |
| 558 | return OI ? OI->getDecl() : 0; |
| 559 | } |
| 560 | |
| 561 | iterator end() { return M.end(); } |
| 562 | |
| 563 | RetainSummary*& operator[](ObjCMessageExpr* ME) { |
| 564 | |
| 565 | Selector S = ME->getSelector(); |
| 566 | |
| 567 | if (Expr* Receiver = ME->getReceiver()) { |
| 568 | ObjCInterfaceDecl* OD = getReceiverDecl(Receiver); |
| 569 | return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S]; |
| 570 | } |
| 571 | |
| 572 | return M[ObjCSummaryKey(ME->getClassName(), S)]; |
| 573 | } |
| 574 | |
| 575 | RetainSummary*& operator[](ObjCSummaryKey K) { |
| 576 | return M[K]; |
| 577 | } |
| 578 | |
| 579 | RetainSummary*& operator[](Selector S) { |
| 580 | return M[ ObjCSummaryKey(S) ]; |
| 581 | } |
| 582 | }; |
| 583 | } // end anonymous namespace |
| 584 | |
| 585 | //===----------------------------------------------------------------------===// |
| 586 | // Data structures for managing collections of summaries. |
| 587 | //===----------------------------------------------------------------------===// |
| 588 | |
| 589 | namespace { |
| 590 | class VISIBILITY_HIDDEN RetainSummaryManager { |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 591 | |
| 592 | //==-----------------------------------------------------------------==// |
| 593 | // Typedefs. |
| 594 | //==-----------------------------------------------------------------==// |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 595 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 596 | typedef llvm::DenseMap<FunctionDecl*, RetainSummary*> |
| 597 | FuncSummariesTy; |
| 598 | |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 599 | typedef ObjCSummaryCache ObjCMethodSummariesTy; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 600 | |
| 601 | //==-----------------------------------------------------------------==// |
| 602 | // Data. |
| 603 | //==-----------------------------------------------------------------==// |
| 604 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 605 | /// Ctx - The ASTContext object for the analyzed ASTs. |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 606 | ASTContext& Ctx; |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 607 | |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 608 | /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier |
| 609 | /// "CFDictionaryCreate". |
| 610 | IdentifierInfo* CFDictionaryCreateII; |
| 611 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 612 | /// GCEnabled - Records whether or not the analyzed code runs in GC mode. |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 613 | const bool GCEnabled; |
Ted Kremenek | 22fe248 | 2009-05-04 04:30:18 +0000 | [diff] [blame] | 614 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 615 | /// FuncSummaries - A map from FunctionDecls to summaries. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 616 | FuncSummariesTy FuncSummaries; |
| 617 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 618 | /// ObjCClassMethodSummaries - A map from selectors (for instance methods) |
| 619 | /// to summaries. |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 620 | ObjCMethodSummariesTy ObjCClassMethodSummaries; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 621 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 622 | /// ObjCMethodSummaries - A map from selectors to summaries. |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 623 | ObjCMethodSummariesTy ObjCMethodSummaries; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 624 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 625 | /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects, |
| 626 | /// and all other data used by the checker. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 627 | llvm::BumpPtrAllocator BPAlloc; |
| 628 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 629 | /// AF - A factory for ArgEffects objects. |
| 630 | ArgEffects::Factory AF; |
| 631 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 632 | /// ScratchArgs - A holding buffer for construct ArgEffects. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 633 | ArgEffects ScratchArgs; |
| 634 | |
Ted Kremenek | ec31533 | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 635 | /// ObjCAllocRetE - Default return effect for methods returning Objective-C |
| 636 | /// objects. |
| 637 | RetEffect ObjCAllocRetE; |
Ted Kremenek | 547d495 | 2009-06-05 23:18:01 +0000 | [diff] [blame] | 638 | |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 639 | /// ObjCInitRetE - Default return effect for init methods returning Objective-C |
Ted Kremenek | 547d495 | 2009-06-05 23:18:01 +0000 | [diff] [blame] | 640 | /// objects. |
| 641 | RetEffect ObjCInitRetE; |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 642 | |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 643 | RetainSummary DefaultSummary; |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 644 | RetainSummary* StopSummary; |
| 645 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 646 | //==-----------------------------------------------------------------==// |
| 647 | // Methods. |
| 648 | //==-----------------------------------------------------------------==// |
| 649 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 650 | /// getArgEffects - Returns a persistent ArgEffects object based on the |
| 651 | /// data in ScratchArgs. |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 652 | ArgEffects getArgEffects(); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 653 | |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 654 | enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable }; |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 655 | |
| 656 | public: |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 657 | RetEffect getObjAllocRetEffect() const { return ObjCAllocRetE; } |
| 658 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 659 | RetainSummary *getDefaultSummary() { |
| 660 | RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>(); |
| 661 | return new (Summ) RetainSummary(DefaultSummary); |
| 662 | } |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 663 | |
Ted Kremenek | 6ad315a | 2009-02-23 16:51:39 +0000 | [diff] [blame] | 664 | RetainSummary* getUnarySummary(const FunctionType* FT, UnaryFuncKind func); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 665 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 666 | RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD); |
| 667 | RetainSummary* getCFSummaryGetRule(FunctionDecl* FD); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 668 | RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 669 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 670 | RetainSummary* getPersistentSummary(ArgEffects AE, RetEffect RetEff, |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 671 | ArgEffect ReceiverEff = DoNothing, |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 672 | ArgEffect DefaultEff = MayEscape, |
| 673 | bool isEndPath = false); |
Ted Kremenek | 706522f | 2008-10-29 04:07:07 +0000 | [diff] [blame] | 674 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 675 | RetainSummary* getPersistentSummary(RetEffect RE, |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 676 | ArgEffect ReceiverEff = DoNothing, |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 677 | ArgEffect DefaultEff = MayEscape) { |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 678 | return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff); |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 679 | } |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 680 | |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 681 | RetainSummary *getPersistentStopSummary() { |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 682 | if (StopSummary) |
| 683 | return StopSummary; |
| 684 | |
| 685 | StopSummary = getPersistentSummary(RetEffect::MakeNoRet(), |
| 686 | StopTracking, StopTracking); |
Ted Kremenek | 706522f | 2008-10-29 04:07:07 +0000 | [diff] [blame] | 687 | |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 688 | return StopSummary; |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 689 | } |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 690 | |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 691 | RetainSummary *getInitMethodSummary(QualType RetTy); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 692 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 693 | void InitializeClassMethodSummaries(); |
| 694 | void InitializeMethodSummaries(); |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 695 | |
Ted Kremenek | eff4b3c | 2009-05-03 04:42:10 +0000 | [diff] [blame] | 696 | bool isTrackedObjCObjectType(QualType T); |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 697 | bool isTrackedCFObjectType(QualType T); |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 698 | |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 699 | private: |
| 700 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 701 | void addClsMethSummary(IdentifierInfo* ClsII, Selector S, |
| 702 | RetainSummary* Summ) { |
| 703 | ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 704 | } |
| 705 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 706 | void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) { |
| 707 | ObjCClassMethodSummaries[S] = Summ; |
| 708 | } |
| 709 | |
| 710 | void addNSObjectMethSummary(Selector S, RetainSummary *Summ) { |
| 711 | ObjCMethodSummaries[S] = Summ; |
| 712 | } |
Ted Kremenek | 3aa7ecd | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 713 | |
| 714 | void addClassMethSummary(const char* Cls, const char* nullaryName, |
| 715 | RetainSummary *Summ) { |
| 716 | IdentifierInfo* ClsII = &Ctx.Idents.get(Cls); |
| 717 | Selector S = GetNullarySelector(nullaryName, Ctx); |
| 718 | ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 719 | } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 720 | |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 721 | void addInstMethSummary(const char* Cls, const char* nullaryName, |
| 722 | RetainSummary *Summ) { |
| 723 | IdentifierInfo* ClsII = &Ctx.Idents.get(Cls); |
| 724 | Selector S = GetNullarySelector(nullaryName, Ctx); |
| 725 | ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 726 | } |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 727 | |
| 728 | Selector generateSelector(va_list argp) { |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 729 | llvm::SmallVector<IdentifierInfo*, 10> II; |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 730 | |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 731 | while (const char* s = va_arg(argp, const char*)) |
| 732 | II.push_back(&Ctx.Idents.get(s)); |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 733 | |
| 734 | return Ctx.Selectors.getSelector(II.size(), &II[0]); |
| 735 | } |
| 736 | |
| 737 | void addMethodSummary(IdentifierInfo *ClsII, ObjCMethodSummariesTy& Summaries, |
| 738 | RetainSummary* Summ, va_list argp) { |
| 739 | Selector S = generateSelector(argp); |
| 740 | Summaries[ObjCSummaryKey(ClsII, S)] = Summ; |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 741 | } |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 742 | |
| 743 | void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) { |
| 744 | va_list argp; |
| 745 | va_start(argp, Summ); |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 746 | addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp); |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 747 | va_end(argp); |
| 748 | } |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 749 | |
| 750 | void addClsMethSummary(const char* Cls, RetainSummary* Summ, ...) { |
| 751 | va_list argp; |
| 752 | va_start(argp, Summ); |
| 753 | addMethodSummary(&Ctx.Idents.get(Cls),ObjCClassMethodSummaries, Summ, argp); |
| 754 | va_end(argp); |
| 755 | } |
| 756 | |
| 757 | void addClsMethSummary(IdentifierInfo *II, RetainSummary* Summ, ...) { |
| 758 | va_list argp; |
| 759 | va_start(argp, Summ); |
| 760 | addMethodSummary(II, ObjCClassMethodSummaries, Summ, argp); |
| 761 | va_end(argp); |
| 762 | } |
| 763 | |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 764 | void addPanicSummary(const char* Cls, ...) { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 765 | RetainSummary* Summ = getPersistentSummary(AF.GetEmptyMap(), |
| 766 | RetEffect::MakeNoRet(), |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 767 | DoNothing, DoNothing, true); |
| 768 | va_list argp; |
| 769 | va_start (argp, Cls); |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 770 | addMethodSummary(&Ctx.Idents.get(Cls), ObjCMethodSummaries, Summ, argp); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 771 | va_end(argp); |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 772 | } |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 773 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 774 | public: |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 775 | |
| 776 | RetainSummaryManager(ASTContext& ctx, bool gcenabled) |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 777 | : Ctx(ctx), |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 778 | CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")), |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 779 | GCEnabled(gcenabled), AF(BPAlloc), ScratchArgs(AF.GetEmptyMap()), |
Ted Kremenek | ec31533 | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 780 | ObjCAllocRetE(gcenabled ? RetEffect::MakeGCNotOwned() |
| 781 | : RetEffect::MakeOwned(RetEffect::ObjC, true)), |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 782 | ObjCInitRetE(gcenabled ? RetEffect::MakeGCNotOwned() |
| 783 | : RetEffect::MakeOwnedWhenTrackedReceiver()), |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 784 | DefaultSummary(AF.GetEmptyMap() /* per-argument effects (none) */, |
| 785 | RetEffect::MakeNoRet() /* return effect */, |
Ted Kremenek | ebd5a2d | 2009-05-11 18:30:24 +0000 | [diff] [blame] | 786 | MayEscape, /* default argument effect */ |
| 787 | DoNothing /* receiver effect */), |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 788 | StopSummary(0) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 789 | |
| 790 | InitializeClassMethodSummaries(); |
| 791 | InitializeMethodSummaries(); |
| 792 | } |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 793 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 794 | ~RetainSummaryManager(); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 795 | |
Ted Kremenek | ab59227 | 2008-06-24 03:56:45 +0000 | [diff] [blame] | 796 | RetainSummary* getSummary(FunctionDecl* FD); |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 797 | |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 798 | RetainSummary* getInstanceMethodSummary(ObjCMessageExpr* ME, |
| 799 | const ObjCInterfaceDecl* ID) { |
Ted Kremenek | ce8a41d | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 800 | return getInstanceMethodSummary(ME->getSelector(), ME->getClassName(), |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 801 | ID, ME->getMethodDecl(), ME->getType()); |
| 802 | } |
| 803 | |
Ted Kremenek | ce8a41d | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 804 | RetainSummary* getInstanceMethodSummary(Selector S, IdentifierInfo *ClsName, |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 805 | const ObjCInterfaceDecl* ID, |
| 806 | const ObjCMethodDecl *MD, |
| 807 | QualType RetTy); |
Ted Kremenek | fcd7c6f | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 808 | |
| 809 | RetainSummary *getClassMethodSummary(Selector S, IdentifierInfo *ClsName, |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 810 | const ObjCInterfaceDecl *ID, |
| 811 | const ObjCMethodDecl *MD, |
| 812 | QualType RetTy); |
Ted Kremenek | fcd7c6f | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 813 | |
| 814 | RetainSummary *getClassMethodSummary(ObjCMessageExpr *ME) { |
| 815 | return getClassMethodSummary(ME->getSelector(), ME->getClassName(), |
| 816 | ME->getClassInfo().first, |
| 817 | ME->getMethodDecl(), ME->getType()); |
| 818 | } |
Ted Kremenek | 552333c | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 819 | |
| 820 | /// getMethodSummary - This version of getMethodSummary is used to query |
| 821 | /// the summary for the current method being analyzed. |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 822 | RetainSummary *getMethodSummary(const ObjCMethodDecl *MD) { |
| 823 | // FIXME: Eventually this should be unneeded. |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 824 | const ObjCInterfaceDecl *ID = MD->getClassInterface(); |
Ted Kremenek | 70a6576 | 2009-04-30 05:41:14 +0000 | [diff] [blame] | 825 | Selector S = MD->getSelector(); |
Ted Kremenek | 552333c | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 826 | IdentifierInfo *ClsName = ID->getIdentifier(); |
| 827 | QualType ResultTy = MD->getResultType(); |
| 828 | |
Ted Kremenek | 76a50e3 | 2009-04-30 05:47:23 +0000 | [diff] [blame] | 829 | // Resolve the method decl last. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 830 | if (const ObjCMethodDecl *InterfaceMD = ResolveToInterfaceMethodDecl(MD)) |
Ted Kremenek | 76a50e3 | 2009-04-30 05:47:23 +0000 | [diff] [blame] | 831 | MD = InterfaceMD; |
Ted Kremenek | 70a6576 | 2009-04-30 05:41:14 +0000 | [diff] [blame] | 832 | |
Ted Kremenek | 552333c | 2009-04-29 17:17:48 +0000 | [diff] [blame] | 833 | if (MD->isInstanceMethod()) |
| 834 | return getInstanceMethodSummary(S, ClsName, ID, MD, ResultTy); |
| 835 | else |
| 836 | return getClassMethodSummary(S, ClsName, ID, MD, ResultTy); |
| 837 | } |
Ted Kremenek | fcd7c6f | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 838 | |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 839 | RetainSummary* getCommonMethodSummary(const ObjCMethodDecl* MD, |
| 840 | Selector S, QualType RetTy); |
| 841 | |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 842 | void updateSummaryFromAnnotations(RetainSummary &Summ, |
| 843 | const ObjCMethodDecl *MD); |
| 844 | |
| 845 | void updateSummaryFromAnnotations(RetainSummary &Summ, |
| 846 | const FunctionDecl *FD); |
| 847 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 848 | bool isGCEnabled() const { return GCEnabled; } |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 849 | |
| 850 | RetainSummary *copySummary(RetainSummary *OldSumm) { |
| 851 | RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>(); |
| 852 | new (Summ) RetainSummary(*OldSumm); |
| 853 | return Summ; |
| 854 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 855 | }; |
| 856 | |
| 857 | } // end anonymous namespace |
| 858 | |
| 859 | //===----------------------------------------------------------------------===// |
| 860 | // Implementation of checker data structures. |
| 861 | //===----------------------------------------------------------------------===// |
| 862 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 863 | RetainSummaryManager::~RetainSummaryManager() {} |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 864 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 865 | ArgEffects RetainSummaryManager::getArgEffects() { |
| 866 | ArgEffects AE = ScratchArgs; |
| 867 | ScratchArgs = AF.GetEmptyMap(); |
| 868 | return AE; |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 871 | RetainSummary* |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 872 | RetainSummaryManager::getPersistentSummary(ArgEffects AE, RetEffect RetEff, |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 873 | ArgEffect ReceiverEff, |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 874 | ArgEffect DefaultEff, |
Ted Kremenek | 22fe248 | 2009-05-04 04:30:18 +0000 | [diff] [blame] | 875 | bool isEndPath) { |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 876 | // Create the summary and return it. |
Ted Kremenek | 22fe248 | 2009-05-04 04:30:18 +0000 | [diff] [blame] | 877 | RetainSummary *Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>(); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 878 | new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 879 | return Summ; |
| 880 | } |
| 881 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 882 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 883 | // Predicates. |
| 884 | //===----------------------------------------------------------------------===// |
| 885 | |
Ted Kremenek | eff4b3c | 2009-05-03 04:42:10 +0000 | [diff] [blame] | 886 | bool RetainSummaryManager::isTrackedObjCObjectType(QualType Ty) { |
Ted Kremenek | 97d095f | 2009-04-23 22:11:07 +0000 | [diff] [blame] | 887 | if (!Ctx.isObjCObjectPointerType(Ty)) |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 888 | return false; |
| 889 | |
Ted Kremenek | 97d095f | 2009-04-23 22:11:07 +0000 | [diff] [blame] | 890 | // We assume that id<..>, id, and "Class" all represent tracked objects. |
| 891 | const PointerType *PT = Ty->getAsPointerType(); |
| 892 | if (PT == 0) |
| 893 | return true; |
| 894 | |
| 895 | const ObjCInterfaceType *OT = PT->getPointeeType()->getAsObjCInterfaceType(); |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 896 | |
| 897 | // We assume that id<..>, id, and "Class" all represent tracked objects. |
| 898 | if (!OT) |
| 899 | return true; |
Ted Kremenek | 97d095f | 2009-04-23 22:11:07 +0000 | [diff] [blame] | 900 | |
Ted Kremenek | fae664a | 2009-05-16 01:38:01 +0000 | [diff] [blame] | 901 | // Does the interface subclass NSObject? |
| 902 | // FIXME: We can memoize here if this gets too expensive. |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 903 | ObjCInterfaceDecl* ID = OT->getDecl(); |
| 904 | |
Ted Kremenek | fae664a | 2009-05-16 01:38:01 +0000 | [diff] [blame] | 905 | // Assume that anything declared with a forward declaration and no |
| 906 | // @interface subclasses NSObject. |
| 907 | if (ID->isForwardDecl()) |
| 908 | return true; |
| 909 | |
| 910 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
| 911 | |
| 912 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 913 | for ( ; ID ; ID = ID->getSuperClass()) |
| 914 | if (ID->getIdentifier() == NSObjectII) |
| 915 | return true; |
| 916 | |
| 917 | return false; |
| 918 | } |
| 919 | |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 920 | bool RetainSummaryManager::isTrackedCFObjectType(QualType T) { |
| 921 | return isRefType(T, "CF") || // Core Foundation. |
| 922 | isRefType(T, "CG") || // Core Graphics. |
| 923 | isRefType(T, "DADisk") || // Disk Arbitration API. |
| 924 | isRefType(T, "DADissenter") || |
| 925 | isRefType(T, "DASessionRef"); |
| 926 | } |
| 927 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 928 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 929 | // Summary creation for functions (largely uses of Core Foundation). |
| 930 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 931 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 932 | static bool isRetain(FunctionDecl* FD, const char* FName) { |
| 933 | const char* loc = strstr(FName, "Retain"); |
| 934 | return loc && loc[sizeof("Retain")-1] == '\0'; |
| 935 | } |
| 936 | |
| 937 | static bool isRelease(FunctionDecl* FD, const char* FName) { |
| 938 | const char* loc = strstr(FName, "Release"); |
| 939 | return loc && loc[sizeof("Release")-1] == '\0'; |
| 940 | } |
| 941 | |
Ted Kremenek | ab59227 | 2008-06-24 03:56:45 +0000 | [diff] [blame] | 942 | RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) { |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 943 | // Look up a summary in our cache of FunctionDecls -> Summaries. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 944 | FuncSummariesTy::iterator I = FuncSummaries.find(FD); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 945 | if (I != FuncSummaries.end()) |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 946 | return I->second; |
| 947 | |
Ted Kremenek | e401a0c | 2009-05-04 15:34:07 +0000 | [diff] [blame] | 948 | // No summary? Generate one. |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 949 | RetainSummary *S = 0; |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 950 | |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 951 | do { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 952 | // We generate "stop" summaries for implicitly defined functions. |
| 953 | if (FD->isImplicit()) { |
| 954 | S = getPersistentStopSummary(); |
| 955 | break; |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 956 | } |
Ted Kremenek | 6ca3191 | 2008-11-04 00:36:12 +0000 | [diff] [blame] | 957 | |
Ted Kremenek | 6ad315a | 2009-02-23 16:51:39 +0000 | [diff] [blame] | 958 | // [PR 3337] Use 'getAsFunctionType' to strip away any typedefs on the |
Ted Kremenek | 9989065 | 2009-01-16 18:40:33 +0000 | [diff] [blame] | 959 | // function's type. |
Ted Kremenek | 6ad315a | 2009-02-23 16:51:39 +0000 | [diff] [blame] | 960 | const FunctionType* FT = FD->getType()->getAsFunctionType(); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 961 | const char* FName = FD->getIdentifier()->getName(); |
| 962 | |
Ted Kremenek | bf0a4dd | 2009-03-05 22:11:14 +0000 | [diff] [blame] | 963 | // Strip away preceding '_'. Doing this here will effect all the checks |
| 964 | // down below. |
| 965 | while (*FName == '_') ++FName; |
| 966 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 967 | // Inspect the result type. |
| 968 | QualType RetTy = FT->getResultType(); |
| 969 | |
| 970 | // FIXME: This should all be refactored into a chain of "summary lookup" |
| 971 | // filters. |
Ted Kremenek | 39d88b0 | 2009-06-15 20:36:07 +0000 | [diff] [blame] | 972 | assert (ScratchArgs.isEmpty()); |
| 973 | |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 974 | switch (strlen(FName)) { |
| 975 | default: break; |
Ted Kremenek | 39d88b0 | 2009-06-15 20:36:07 +0000 | [diff] [blame] | 976 | |
| 977 | |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 978 | case 17: |
| 979 | // Handle: id NSMakeCollectable(CFTypeRef) |
| 980 | if (!memcmp(FName, "NSMakeCollectable", 17)) { |
| 981 | S = (RetTy == Ctx.getObjCIdType()) |
| 982 | ? getUnarySummary(FT, cfmakecollectable) |
| 983 | : getPersistentStopSummary(); |
| 984 | } |
Ted Kremenek | 39d88b0 | 2009-06-15 20:36:07 +0000 | [diff] [blame] | 985 | else if (!memcmp(FName, "IOBSDNameMatching", 17) || |
| 986 | !memcmp(FName, "IOServiceMatching", 17)) { |
| 987 | // Part of <rdar://problem/6961230>. (IOKit) |
| 988 | // This should be addressed using a API table. |
| 989 | S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true), |
| 990 | DoNothing, DoNothing); |
| 991 | } |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 992 | break; |
Ted Kremenek | 39d88b0 | 2009-06-15 20:36:07 +0000 | [diff] [blame] | 993 | |
| 994 | case 21: |
| 995 | if (!memcmp(FName, "IOServiceNameMatching", 21)) { |
| 996 | // Part of <rdar://problem/6961230>. (IOKit) |
| 997 | // This should be addressed using a API table. |
| 998 | S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true), |
| 999 | DoNothing, DoNothing); |
| 1000 | } |
| 1001 | break; |
| 1002 | |
| 1003 | case 24: |
| 1004 | if (!memcmp(FName, "IOServiceAddNotification", 24)) { |
| 1005 | // Part of <rdar://problem/6961230>. (IOKit) |
| 1006 | // This should be addressed using a API table. |
| 1007 | ScratchArgs = AF.Add(ScratchArgs, 2, DecRef); |
| 1008 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 1009 | } |
| 1010 | break; |
| 1011 | |
| 1012 | case 25: |
| 1013 | if (!memcmp(FName, "IORegistryEntryIDMatching", 25)) { |
| 1014 | // Part of <rdar://problem/6961230>. (IOKit) |
| 1015 | // This should be addressed using a API table. |
| 1016 | S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true), |
| 1017 | DoNothing, DoNothing); |
| 1018 | } |
| 1019 | break; |
| 1020 | |
| 1021 | case 26: |
| 1022 | if (!memcmp(FName, "IOOpenFirmwarePathMatching", 26)) { |
| 1023 | // Part of <rdar://problem/6961230>. (IOKit) |
| 1024 | // This should be addressed using a API table. |
| 1025 | S = getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true), |
| 1026 | DoNothing, DoNothing); |
| 1027 | } |
| 1028 | break; |
| 1029 | |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1030 | case 27: |
| 1031 | if (!memcmp(FName, "IOServiceGetMatchingService", 27)) { |
| 1032 | // Part of <rdar://problem/6961230>. |
| 1033 | // This should be addressed using a API table. |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1034 | ScratchArgs = AF.Add(ScratchArgs, 1, DecRef); |
| 1035 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 1036 | } |
| 1037 | break; |
| 1038 | |
| 1039 | case 28: |
| 1040 | if (!memcmp(FName, "IOServiceGetMatchingServices", 28)) { |
| 1041 | // FIXES: <rdar://problem/6326900> |
| 1042 | // This should be addressed using a API table. This strcmp is also |
| 1043 | // a little gross, but there is no need to super optimize here. |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1044 | ScratchArgs = AF.Add(ScratchArgs, 1, DecRef); |
| 1045 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 1046 | } |
| 1047 | break; |
Ted Kremenek | 39d88b0 | 2009-06-15 20:36:07 +0000 | [diff] [blame] | 1048 | |
| 1049 | case 32: |
| 1050 | if (!memcmp(FName, "IOServiceAddMatchingNotification", 32)) { |
| 1051 | // Part of <rdar://problem/6961230>. |
| 1052 | // This should be addressed using a API table. |
| 1053 | ScratchArgs = AF.Add(ScratchArgs, 2, DecRef); |
| 1054 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 1055 | } |
| 1056 | break; |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | // Did we get a summary? |
| 1060 | if (S) |
| 1061 | break; |
Ted Kremenek | 6199190 | 2009-03-17 22:43:44 +0000 | [diff] [blame] | 1062 | |
| 1063 | // Enable this code once the semantics of NSDeallocateObject are resolved |
| 1064 | // for GC. <rdar://problem/6619988> |
| 1065 | #if 0 |
| 1066 | // Handle: NSDeallocateObject(id anObject); |
| 1067 | // This method does allow 'nil' (although we don't check it now). |
| 1068 | if (strcmp(FName, "NSDeallocateObject") == 0) { |
| 1069 | return RetTy == Ctx.VoidTy |
| 1070 | ? getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, Dealloc) |
| 1071 | : getPersistentStopSummary(); |
| 1072 | } |
| 1073 | #endif |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1074 | |
| 1075 | if (RetTy->isPointerType()) { |
| 1076 | // For CoreFoundation ('CF') types. |
| 1077 | if (isRefType(RetTy, "CF", &Ctx, FName)) { |
| 1078 | if (isRetain(FD, FName)) |
| 1079 | S = getUnarySummary(FT, cfretain); |
| 1080 | else if (strstr(FName, "MakeCollectable")) |
| 1081 | S = getUnarySummary(FT, cfmakecollectable); |
| 1082 | else |
| 1083 | S = getCFCreateGetRuleSummary(FD, FName); |
| 1084 | |
| 1085 | break; |
| 1086 | } |
| 1087 | |
| 1088 | // For CoreGraphics ('CG') types. |
| 1089 | if (isRefType(RetTy, "CG", &Ctx, FName)) { |
| 1090 | if (isRetain(FD, FName)) |
| 1091 | S = getUnarySummary(FT, cfretain); |
| 1092 | else |
| 1093 | S = getCFCreateGetRuleSummary(FD, FName); |
| 1094 | |
| 1095 | break; |
| 1096 | } |
| 1097 | |
| 1098 | // For the Disk Arbitration API (DiskArbitration/DADisk.h) |
| 1099 | if (isRefType(RetTy, "DADisk") || |
| 1100 | isRefType(RetTy, "DADissenter") || |
| 1101 | isRefType(RetTy, "DASessionRef")) { |
| 1102 | S = getCFCreateGetRuleSummary(FD, FName); |
| 1103 | break; |
| 1104 | } |
| 1105 | |
| 1106 | break; |
| 1107 | } |
| 1108 | |
| 1109 | // Check for release functions, the only kind of functions that we care |
| 1110 | // about that don't return a pointer type. |
| 1111 | if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) { |
Ted Kremenek | bf0a4dd | 2009-03-05 22:11:14 +0000 | [diff] [blame] | 1112 | // Test for 'CGCF'. |
| 1113 | if (FName[1] == 'G' && FName[2] == 'C' && FName[3] == 'F') |
| 1114 | FName += 4; |
| 1115 | else |
| 1116 | FName += 2; |
| 1117 | |
| 1118 | if (isRelease(FD, FName)) |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1119 | S = getUnarySummary(FT, cfrelease); |
| 1120 | else { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1121 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | 6818928 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 1122 | // Remaining CoreFoundation and CoreGraphics functions. |
| 1123 | // We use to assume that they all strictly followed the ownership idiom |
| 1124 | // and that ownership cannot be transferred. While this is technically |
| 1125 | // correct, many methods allow a tracked object to escape. For example: |
| 1126 | // |
| 1127 | // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...); |
| 1128 | // CFDictionaryAddValue(y, key, x); |
| 1129 | // CFRelease(x); |
| 1130 | // ... it is okay to use 'x' since 'y' has a reference to it |
| 1131 | // |
| 1132 | // We handle this and similar cases with the follow heuristic. If the |
| 1133 | // function name contains "InsertValue", "SetValue" or "AddValue" then |
| 1134 | // we assume that arguments may "escape." |
| 1135 | // |
| 1136 | ArgEffect E = (CStrInCStrNoCase(FName, "InsertValue") || |
| 1137 | CStrInCStrNoCase(FName, "AddValue") || |
Ted Kremenek | a92206e | 2009-02-05 22:34:53 +0000 | [diff] [blame] | 1138 | CStrInCStrNoCase(FName, "SetValue") || |
| 1139 | CStrInCStrNoCase(FName, "AppendValue")) |
Ted Kremenek | 6818928 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 1140 | ? MayEscape : DoNothing; |
| 1141 | |
| 1142 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1143 | } |
| 1144 | } |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 1145 | } |
| 1146 | while (0); |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1147 | |
| 1148 | if (!S) |
| 1149 | S = getDefaultSummary(); |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 1150 | |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1151 | // Annotations override defaults. |
| 1152 | assert(S); |
| 1153 | updateSummaryFromAnnotations(*S, FD); |
| 1154 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1155 | FuncSummaries[FD] = S; |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1156 | return S; |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 1159 | RetainSummary* |
| 1160 | RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD, |
| 1161 | const char* FName) { |
| 1162 | |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1163 | if (strstr(FName, "Create") || strstr(FName, "Copy")) |
| 1164 | return getCFSummaryCreateRule(FD); |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 1165 | |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1166 | if (strstr(FName, "Get")) |
| 1167 | return getCFSummaryGetRule(FD); |
| 1168 | |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 1169 | return getDefaultSummary(); |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1172 | RetainSummary* |
Ted Kremenek | 6ad315a | 2009-02-23 16:51:39 +0000 | [diff] [blame] | 1173 | RetainSummaryManager::getUnarySummary(const FunctionType* FT, |
| 1174 | UnaryFuncKind func) { |
| 1175 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1176 | // Sanity check that this is *really* a unary function. This can |
| 1177 | // happen if people do weird things. |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1178 | const FunctionProtoType* FTP = dyn_cast<FunctionProtoType>(FT); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 1179 | if (!FTP || FTP->getNumArgs() != 1) |
| 1180 | return getPersistentStopSummary(); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1181 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1182 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1183 | |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1184 | switch (func) { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1185 | case cfretain: { |
| 1186 | ScratchArgs = AF.Add(ScratchArgs, 0, IncRef); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1187 | return getPersistentSummary(RetEffect::MakeAlias(0), |
| 1188 | DoNothing, DoNothing); |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | case cfrelease: { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1192 | ScratchArgs = AF.Add(ScratchArgs, 0, DecRef); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1193 | return getPersistentSummary(RetEffect::MakeNoRet(), |
| 1194 | DoNothing, DoNothing); |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | case cfmakecollectable: { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1198 | ScratchArgs = AF.Add(ScratchArgs, 0, MakeCollectable); |
Ted Kremenek | 2701900 | 2009-02-18 21:57:45 +0000 | [diff] [blame] | 1199 | return getPersistentSummary(RetEffect::MakeAlias(0),DoNothing, DoNothing); |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | default: |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 1203 | assert (false && "Not a supported unary function."); |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 1204 | return getDefaultSummary(); |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1205 | } |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1208 | RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1209 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1210 | |
| 1211 | if (FD->getIdentifier() == CFDictionaryCreateII) { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1212 | ScratchArgs = AF.Add(ScratchArgs, 1, DoNothingByRef); |
| 1213 | ScratchArgs = AF.Add(ScratchArgs, 2, DoNothingByRef); |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1216 | return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1219 | RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) { |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1220 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1221 | return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF), |
| 1222 | DoNothing, DoNothing); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1225 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1226 | // Summary creation for Selectors. |
| 1227 | //===----------------------------------------------------------------------===// |
| 1228 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 1229 | RetainSummary* |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 1230 | RetainSummaryManager::getInitMethodSummary(QualType RetTy) { |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1231 | assert(ScratchArgs.isEmpty()); |
| 1232 | // 'init' methods conceptually return a newly allocated object and claim |
| 1233 | // the receiver. |
| 1234 | if (isTrackedObjCObjectType(RetTy) || isTrackedCFObjectType(RetTy)) |
Ted Kremenek | 547d495 | 2009-06-05 23:18:01 +0000 | [diff] [blame] | 1235 | return getPersistentSummary(ObjCInitRetE, DecRefMsg); |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1236 | |
| 1237 | return getDefaultSummary(); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1238 | } |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1239 | |
| 1240 | void |
| 1241 | RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ, |
| 1242 | const FunctionDecl *FD) { |
| 1243 | if (!FD) |
| 1244 | return; |
| 1245 | |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1246 | QualType RetTy = FD->getResultType(); |
| 1247 | |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1248 | // Determine if there is a special return effect for this method. |
Ted Kremenek | b9d8db8 | 2009-06-05 23:00:33 +0000 | [diff] [blame] | 1249 | if (isTrackedObjCObjectType(RetTy)) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1250 | if (FD->getAttr<NSReturnsRetainedAttr>()) { |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1251 | Summ.setRetEffect(ObjCAllocRetE); |
| 1252 | } |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1253 | else if (FD->getAttr<CFReturnsRetainedAttr>()) { |
Ted Kremenek | b9d8db8 | 2009-06-05 23:00:33 +0000 | [diff] [blame] | 1254 | Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true)); |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | else if (RetTy->getAsPointerType()) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1258 | if (FD->getAttr<CFReturnsRetainedAttr>()) { |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1259 | Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true)); |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | void |
| 1265 | RetainSummaryManager::updateSummaryFromAnnotations(RetainSummary &Summ, |
| 1266 | const ObjCMethodDecl *MD) { |
| 1267 | if (!MD) |
| 1268 | return; |
| 1269 | |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 1270 | bool isTrackedLoc = false; |
| 1271 | |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1272 | // Determine if there is a special return effect for this method. |
| 1273 | if (isTrackedObjCObjectType(MD->getResultType())) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1274 | if (MD->getAttr<NSReturnsRetainedAttr>()) { |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1275 | Summ.setRetEffect(ObjCAllocRetE); |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 1276 | return; |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1277 | } |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 1278 | |
| 1279 | isTrackedLoc = true; |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1280 | } |
Ted Kremenek | 6d4b76d | 2009-07-06 18:30:43 +0000 | [diff] [blame] | 1281 | |
| 1282 | if (!isTrackedLoc) |
| 1283 | isTrackedLoc = MD->getResultType()->getAsPointerType() != NULL; |
| 1284 | |
| 1285 | if (isTrackedLoc && MD->getAttr<CFReturnsRetainedAttr>()) |
| 1286 | Summ.setRetEffect(RetEffect::MakeOwned(RetEffect::CF, true)); |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 1289 | RetainSummary* |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 1290 | RetainSummaryManager::getCommonMethodSummary(const ObjCMethodDecl* MD, |
| 1291 | Selector S, QualType RetTy) { |
Ted Kremenek | 8ee885b | 2009-04-24 21:56:17 +0000 | [diff] [blame] | 1292 | |
Ted Kremenek | fcd7c6f | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 1293 | if (MD) { |
Ted Kremenek | 376d1e7 | 2009-04-24 18:00:17 +0000 | [diff] [blame] | 1294 | // Scan the method decl for 'void*' arguments. These should be treated |
| 1295 | // as 'StopTracking' because they are often used with delegates. |
| 1296 | // Delegates are a frequent form of false positives with the retain |
| 1297 | // count checker. |
| 1298 | unsigned i = 0; |
| 1299 | for (ObjCMethodDecl::param_iterator I = MD->param_begin(), |
| 1300 | E = MD->param_end(); I != E; ++I, ++i) |
| 1301 | if (ParmVarDecl *PD = *I) { |
| 1302 | QualType Ty = Ctx.getCanonicalType(PD->getType()); |
| 1303 | if (Ty.getUnqualifiedType() == Ctx.VoidPtrTy) |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1304 | ScratchArgs = AF.Add(ScratchArgs, i, StopTracking); |
Ted Kremenek | 376d1e7 | 2009-04-24 18:00:17 +0000 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | |
Ted Kremenek | 8ee885b | 2009-04-24 21:56:17 +0000 | [diff] [blame] | 1308 | // Any special effect for the receiver? |
| 1309 | ArgEffect ReceiverEff = DoNothing; |
| 1310 | |
| 1311 | // If one of the arguments in the selector has the keyword 'delegate' we |
| 1312 | // should stop tracking the reference count for the receiver. This is |
| 1313 | // because the reference count is quite possibly handled by a delegate |
| 1314 | // method. |
| 1315 | if (S.isKeywordSelector()) { |
| 1316 | const std::string &str = S.getAsString(); |
| 1317 | assert(!str.empty()); |
| 1318 | if (CStrInCStrNoCase(&str[0], "delegate:")) ReceiverEff = StopTracking; |
| 1319 | } |
| 1320 | |
Ted Kremenek | 250b1fa | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1321 | // Look for methods that return an owned object. |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1322 | if (isTrackedObjCObjectType(RetTy)) { |
| 1323 | // EXPERIMENTAL: Assume the Cocoa conventions for all objects returned |
| 1324 | // by instance methods. |
Ted Kremenek | 7db1604 | 2009-05-15 15:49:00 +0000 | [diff] [blame] | 1325 | RetEffect E = followsFundamentalRule(S) |
| 1326 | ? ObjCAllocRetE : RetEffect::MakeNotOwned(RetEffect::ObjC); |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1327 | |
| 1328 | return getPersistentSummary(E, ReceiverEff, MayEscape); |
Ted Kremenek | 376d1e7 | 2009-04-24 18:00:17 +0000 | [diff] [blame] | 1329 | } |
Ted Kremenek | 250b1fa | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1330 | |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1331 | // Look for methods that return an owned core foundation object. |
| 1332 | if (isTrackedCFObjectType(RetTy)) { |
Ted Kremenek | 7db1604 | 2009-05-15 15:49:00 +0000 | [diff] [blame] | 1333 | RetEffect E = followsFundamentalRule(S) |
| 1334 | ? RetEffect::MakeOwned(RetEffect::CF, true) |
| 1335 | : RetEffect::MakeNotOwned(RetEffect::CF); |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1336 | |
| 1337 | return getPersistentSummary(E, ReceiverEff, MayEscape); |
| 1338 | } |
Ted Kremenek | 250b1fa | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1339 | |
Ted Kremenek | 9251143 | 2009-05-03 06:08:32 +0000 | [diff] [blame] | 1340 | if (ScratchArgs.isEmpty() && ReceiverEff == DoNothing) |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 1341 | return getDefaultSummary(); |
Ted Kremenek | 250b1fa | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1342 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1343 | return getPersistentSummary(RetEffect::MakeNoRet(), ReceiverEff, MayEscape); |
Ted Kremenek | 250b1fa | 2009-04-23 23:08:22 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | RetainSummary* |
Ted Kremenek | ce8a41d | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 1347 | RetainSummaryManager::getInstanceMethodSummary(Selector S, |
| 1348 | IdentifierInfo *ClsName, |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 1349 | const ObjCInterfaceDecl* ID, |
| 1350 | const ObjCMethodDecl *MD, |
Ted Kremenek | ce8a41d | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 1351 | QualType RetTy) { |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 1352 | |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 1353 | // Look up a summary in our summary cache. |
| 1354 | ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, ClsName, S); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1355 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1356 | if (I != ObjCMethodSummaries.end()) |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1357 | return I->second; |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1358 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1359 | assert(ScratchArgs.isEmpty()); |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1360 | RetainSummary *Summ = 0; |
Ted Kremenek | aee9e57 | 2008-05-06 06:09:09 +0000 | [diff] [blame] | 1361 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1362 | // "initXXX": pass-through for receiver. |
Ted Kremenek | 7db1604 | 2009-05-15 15:49:00 +0000 | [diff] [blame] | 1363 | if (deriveNamingConvention(S) == InitRule) |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1364 | Summ = getInitMethodSummary(RetTy); |
| 1365 | else |
| 1366 | Summ = getCommonMethodSummary(MD, S, RetTy); |
| 1367 | |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1368 | // Annotations override defaults. |
| 1369 | updateSummaryFromAnnotations(*Summ, MD); |
| 1370 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1371 | // Memoize the summary. |
Ted Kremenek | 70b6a83 | 2009-05-13 18:16:01 +0000 | [diff] [blame] | 1372 | ObjCMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ; |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 1373 | return Summ; |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1376 | RetainSummary* |
Ted Kremenek | fcd7c6f | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 1377 | RetainSummaryManager::getClassMethodSummary(Selector S, IdentifierInfo *ClsName, |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 1378 | const ObjCInterfaceDecl *ID, |
| 1379 | const ObjCMethodDecl *MD, |
| 1380 | QualType RetTy) { |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 1381 | |
Ted Kremenek | fcd7c6f | 2009-04-29 00:42:39 +0000 | [diff] [blame] | 1382 | assert(ClsName && "Class name must be specified."); |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 1383 | ObjCMethodSummariesTy::iterator I = |
| 1384 | ObjCClassMethodSummaries.find(ID, ClsName, S); |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1385 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1386 | if (I != ObjCClassMethodSummaries.end()) |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1387 | return I->second; |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1388 | |
| 1389 | RetainSummary *Summ = getCommonMethodSummary(MD, S, RetTy); |
Ted Kremenek | 4dd8fb4 | 2009-05-09 02:58:13 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Annotations override defaults. |
| 1392 | updateSummaryFromAnnotations(*Summ, MD); |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1393 | |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 1394 | // Memoize the summary. |
Ted Kremenek | 70b6a83 | 2009-05-13 18:16:01 +0000 | [diff] [blame] | 1395 | ObjCClassMethodSummaries[ObjCSummaryKey(ID, ClsName, S)] = Summ; |
Ted Kremenek | e87450e | 2009-04-23 19:11:35 +0000 | [diff] [blame] | 1396 | return Summ; |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Ted Kremenek | ec31533 | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 1399 | void RetainSummaryManager::InitializeClassMethodSummaries() { |
| 1400 | assert(ScratchArgs.isEmpty()); |
| 1401 | RetainSummary* Summ = getPersistentSummary(ObjCAllocRetE); |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 1402 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1403 | // Create the summaries for "alloc", "new", and "allocWithZone:" for |
| 1404 | // NSObject and its derivatives. |
| 1405 | addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ); |
| 1406 | addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ); |
| 1407 | addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1408 | |
| 1409 | // Create the [NSAssertionHandler currentHander] summary. |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1410 | addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"), |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1411 | GetNullarySelector("currentHandler", Ctx), |
| 1412 | getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC))); |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1413 | |
| 1414 | // Create the [NSAutoreleasePool addObject:] summary. |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1415 | ScratchArgs = AF.Add(ScratchArgs, 0, Autorelease); |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 1416 | addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"), |
| 1417 | GetUnarySelector("addObject", Ctx), |
| 1418 | getPersistentSummary(RetEffect::MakeNoRet(), |
Ted Kremenek | 022a3c4 | 2009-02-23 02:31:16 +0000 | [diff] [blame] | 1419 | DoNothing, Autorelease)); |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 1420 | |
| 1421 | // Create the summaries for [NSObject performSelector...]. We treat |
| 1422 | // these as 'stop tracking' for the arguments because they are often |
| 1423 | // used for delegates that can release the object. When we have better |
| 1424 | // inter-procedural analysis we can potentially do something better. This |
| 1425 | // workaround is to remove false positives. |
| 1426 | Summ = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, StopTracking); |
| 1427 | IdentifierInfo *NSObjectII = &Ctx.Idents.get("NSObject"); |
| 1428 | addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject", |
| 1429 | "afterDelay", NULL); |
| 1430 | addClsMethSummary(NSObjectII, Summ, "performSelector", "withObject", |
| 1431 | "afterDelay", "inModes", NULL); |
| 1432 | addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread", |
| 1433 | "withObject", "waitUntilDone", NULL); |
| 1434 | addClsMethSummary(NSObjectII, Summ, "performSelectorOnMainThread", |
| 1435 | "withObject", "waitUntilDone", "modes", NULL); |
| 1436 | addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread", |
| 1437 | "withObject", "waitUntilDone", NULL); |
| 1438 | addClsMethSummary(NSObjectII, Summ, "performSelector", "onThread", |
| 1439 | "withObject", "waitUntilDone", "modes", NULL); |
| 1440 | addClsMethSummary(NSObjectII, Summ, "performSelectorInBackground", |
| 1441 | "withObject", NULL); |
Ted Kremenek | 3043766 | 2009-05-14 21:29:16 +0000 | [diff] [blame] | 1442 | |
| 1443 | // Specially handle NSData. |
| 1444 | RetainSummary *dataWithBytesNoCopySumm = |
| 1445 | getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC), DoNothing, |
| 1446 | DoNothing); |
| 1447 | addClsMethSummary("NSData", dataWithBytesNoCopySumm, |
| 1448 | "dataWithBytesNoCopy", "length", NULL); |
| 1449 | addClsMethSummary("NSData", dataWithBytesNoCopySumm, |
| 1450 | "dataWithBytesNoCopy", "length", "freeWhenDone", NULL); |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1453 | void RetainSummaryManager::InitializeMethodSummaries() { |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1454 | |
Ted Kremenek | b77449c | 2009-05-03 05:20:50 +0000 | [diff] [blame] | 1455 | assert (ScratchArgs.isEmpty()); |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1456 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1457 | // Create the "init" selector. It just acts as a pass-through for the |
| 1458 | // receiver. |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1459 | addNSObjectMethSummary(GetNullarySelector("init", Ctx), |
Ted Kremenek | b04cb59 | 2009-06-11 18:17:24 +0000 | [diff] [blame] | 1460 | getPersistentSummary(ObjCInitRetE, DecRefMsg)); |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1461 | |
| 1462 | // The next methods are allocators. |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 1463 | RetainSummary *AllocSumm = getPersistentSummary(ObjCAllocRetE); |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1464 | |
| 1465 | // Create the "copy" selector. |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 1466 | addNSObjectMethSummary(GetNullarySelector("copy", Ctx), AllocSumm); |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1467 | |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1468 | // Create the "mutableCopy" selector. |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 1469 | addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), AllocSumm); |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1470 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1471 | // Create the "retain" selector. |
Ted Kremenek | ec31533 | 2009-05-07 23:40:42 +0000 | [diff] [blame] | 1472 | RetEffect E = RetEffect::MakeReceiverAlias(); |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 1473 | RetainSummary *Summ = getPersistentSummary(E, IncRefMsg); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1474 | addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Create the "release" selector. |
Ted Kremenek | 1c512f5 | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 1477 | Summ = getPersistentSummary(E, DecRefMsg); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1478 | addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ); |
Ted Kremenek | 299e815 | 2008-05-07 21:17:39 +0000 | [diff] [blame] | 1479 | |
| 1480 | // Create the "drain" selector. |
| 1481 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1482 | addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ); |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1483 | |
| 1484 | // Create the -dealloc summary. |
| 1485 | Summ = getPersistentSummary(RetEffect::MakeNoRet(), Dealloc); |
| 1486 | addNSObjectMethSummary(GetNullarySelector("dealloc", Ctx), Summ); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1487 | |
| 1488 | // Create the "autorelease" selector. |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 1489 | Summ = getPersistentSummary(E, Autorelease); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1490 | addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ); |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1491 | |
Ted Kremenek | f9a8e2e | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 1492 | // Specially handle NSAutoreleasePool. |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1493 | addInstMethSummary("NSAutoreleasePool", "init", |
Ted Kremenek | f9a8e2e | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 1494 | getPersistentSummary(RetEffect::MakeReceiverAlias(), |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1495 | NewAutoreleasePool)); |
Ted Kremenek | f9a8e2e | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 1496 | |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1497 | // For NSWindow, allocated objects are (initially) self-owned. |
Ted Kremenek | 89e202d | 2009-02-23 02:51:29 +0000 | [diff] [blame] | 1498 | // FIXME: For now we opt for false negatives with NSWindow, as these objects |
| 1499 | // self-own themselves. However, they only do this once they are displayed. |
| 1500 | // Thus, we need to track an NSWindow's display status. |
| 1501 | // This is tracked in <rdar://problem/6062711>. |
Ted Kremenek | 3aa7ecd | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 1502 | // See also http://llvm.org/bugs/show_bug.cgi?id=3714. |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1503 | RetainSummary *NoTrackYet = getPersistentSummary(RetEffect::MakeNoRet(), |
| 1504 | StopTracking, |
| 1505 | StopTracking); |
Ted Kremenek | 99d0269 | 2009-04-03 19:02:51 +0000 | [diff] [blame] | 1506 | |
| 1507 | addClassMethSummary("NSWindow", "alloc", NoTrackYet); |
| 1508 | |
Ted Kremenek | 3aa7ecd | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 1509 | #if 0 |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1510 | addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect", |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1511 | "styleMask", "backing", "defer", NULL); |
| 1512 | |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1513 | addInstMethSummary("NSWindow", NoTrackYet, "initWithContentRect", |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1514 | "styleMask", "backing", "defer", "screen", NULL); |
Ted Kremenek | 3aa7ecd | 2009-03-04 23:30:42 +0000 | [diff] [blame] | 1515 | #endif |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1516 | |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1517 | // For NSPanel (which subclasses NSWindow), allocated objects are not |
| 1518 | // self-owned. |
Ted Kremenek | 99d0269 | 2009-04-03 19:02:51 +0000 | [diff] [blame] | 1519 | // FIXME: For now we don't track NSPanels. object for the same reason |
| 1520 | // as for NSWindow objects. |
| 1521 | addClassMethSummary("NSPanel", "alloc", NoTrackYet); |
| 1522 | |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1523 | #if 0 |
| 1524 | addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect", |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1525 | "styleMask", "backing", "defer", NULL); |
| 1526 | |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1527 | addInstMethSummary("NSPanel", NoTrackYet, "initWithContentRect", |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1528 | "styleMask", "backing", "defer", "screen", NULL); |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 1529 | #endif |
Ted Kremenek | ba67f6a | 2009-05-18 23:14:34 +0000 | [diff] [blame] | 1530 | |
| 1531 | // Don't track allocated autorelease pools yet, as it is okay to prematurely |
| 1532 | // exit a method. |
| 1533 | addClassMethSummary("NSAutoreleasePool", "alloc", NoTrackYet); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1534 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1535 | // Create NSAssertionHandler summaries. |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1536 | addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file", |
| 1537 | "lineNumber", "description", NULL); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1538 | |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1539 | addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object", |
| 1540 | "file", "lineNumber", "description", NULL); |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 1541 | |
| 1542 | // Create summaries QCRenderer/QCView -createSnapShotImageOfType: |
| 1543 | addInstMethSummary("QCRenderer", AllocSumm, |
| 1544 | "createSnapshotImageOfType", NULL); |
| 1545 | addInstMethSummary("QCView", AllocSumm, |
| 1546 | "createSnapshotImageOfType", NULL); |
| 1547 | |
Ted Kremenek | 211a9c6 | 2009-06-15 20:58:58 +0000 | [diff] [blame] | 1548 | // Create summaries for CIContext, 'createCGImage' and |
| 1549 | // 'createCGLayerWithSize'. |
Ted Kremenek | 767d649 | 2009-05-20 22:39:57 +0000 | [diff] [blame] | 1550 | addInstMethSummary("CIContext", AllocSumm, |
| 1551 | "createCGImage", "fromRect", NULL); |
| 1552 | addInstMethSummary("CIContext", AllocSumm, |
Ted Kremenek | 211a9c6 | 2009-06-15 20:58:58 +0000 | [diff] [blame] | 1553 | "createCGImage", "fromRect", "format", "colorSpace", NULL); |
| 1554 | addInstMethSummary("CIContext", AllocSumm, "createCGLayerWithSize", |
| 1555 | "info", NULL); |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1558 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1559 | // Reference-counting logic (typestate + counts). |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1560 | //===----------------------------------------------------------------------===// |
| 1561 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1562 | namespace { |
| 1563 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1564 | class VISIBILITY_HIDDEN RefVal { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1565 | public: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1566 | enum Kind { |
| 1567 | Owned = 0, // Owning reference. |
| 1568 | NotOwned, // Reference is not owned by still valid (not freed). |
| 1569 | Released, // Object has been released. |
| 1570 | ReturnedOwned, // Returned object passes ownership to caller. |
| 1571 | ReturnedNotOwned, // Return object does not pass ownership to caller. |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1572 | ERROR_START, |
| 1573 | ErrorDeallocNotOwned, // -dealloc called on non-owned object. |
| 1574 | ErrorDeallocGC, // Calling -dealloc with GC enabled. |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1575 | ErrorUseAfterRelease, // Object used after released. |
| 1576 | ErrorReleaseNotOwned, // Release of an object that was not owned. |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1577 | ERROR_LEAK_START, |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1578 | ErrorLeak, // A memory leak due to excessive reference counts. |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 1579 | ErrorLeakReturned, // A memory leak due to the returning method not having |
| 1580 | // the correct naming conventions. |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 1581 | ErrorGCLeakReturned, |
| 1582 | ErrorOverAutorelease, |
| 1583 | ErrorReturnedNotOwned |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1584 | }; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1585 | |
| 1586 | private: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1587 | Kind kind; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1588 | RetEffect::ObjKind okind; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1589 | unsigned Cnt; |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1590 | unsigned ACnt; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1591 | QualType T; |
| 1592 | |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1593 | RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, unsigned acnt, QualType t) |
| 1594 | : kind(k), okind(o), Cnt(cnt), ACnt(acnt), T(t) {} |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1595 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1596 | RefVal(Kind k, unsigned cnt = 0) |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1597 | : kind(k), okind(RetEffect::AnyObj), Cnt(cnt), ACnt(0) {} |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1598 | |
| 1599 | public: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1600 | Kind getKind() const { return kind; } |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1601 | |
| 1602 | RetEffect::ObjKind getObjKind() const { return okind; } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1603 | |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1604 | unsigned getCount() const { return Cnt; } |
| 1605 | unsigned getAutoreleaseCount() const { return ACnt; } |
| 1606 | unsigned getCombinedCounts() const { return Cnt + ACnt; } |
| 1607 | void clearCounts() { Cnt = 0; ACnt = 0; } |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 1608 | void setCount(unsigned i) { Cnt = i; } |
| 1609 | void setAutoreleaseCount(unsigned i) { ACnt = i; } |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1610 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1611 | QualType getType() const { return T; } |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1612 | |
| 1613 | // Useful predicates. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1614 | |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1615 | static bool isError(Kind k) { return k >= ERROR_START; } |
Ted Kremenek | 73c750b | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 1616 | |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1617 | static bool isLeak(Kind k) { return k >= ERROR_LEAK_START; } |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1618 | |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1619 | bool isOwned() const { |
| 1620 | return getKind() == Owned; |
| 1621 | } |
| 1622 | |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1623 | bool isNotOwned() const { |
| 1624 | return getKind() == NotOwned; |
| 1625 | } |
| 1626 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1627 | bool isReturnedOwned() const { |
| 1628 | return getKind() == ReturnedOwned; |
| 1629 | } |
| 1630 | |
| 1631 | bool isReturnedNotOwned() const { |
| 1632 | return getKind() == ReturnedNotOwned; |
| 1633 | } |
| 1634 | |
| 1635 | bool isNonLeakError() const { |
| 1636 | Kind k = getKind(); |
| 1637 | return isError(k) && !isLeak(k); |
| 1638 | } |
| 1639 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1640 | static RefVal makeOwned(RetEffect::ObjKind o, QualType t, |
| 1641 | unsigned Count = 1) { |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1642 | return RefVal(Owned, o, Count, 0, t); |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1645 | static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t, |
| 1646 | unsigned Count = 0) { |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1647 | return RefVal(NotOwned, o, Count, 0, t); |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1648 | } |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1649 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1650 | // Comparison, profiling, and pretty-printing. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1651 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1652 | bool operator==(const RefVal& X) const { |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 1653 | return kind == X.kind && Cnt == X.Cnt && T == X.T && ACnt == X.ACnt; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1654 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1655 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1656 | RefVal operator-(size_t i) const { |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1657 | return RefVal(getKind(), getObjKind(), getCount() - i, |
| 1658 | getAutoreleaseCount(), getType()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | RefVal operator+(size_t i) const { |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1662 | return RefVal(getKind(), getObjKind(), getCount() + i, |
| 1663 | getAutoreleaseCount(), getType()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | RefVal operator^(Kind k) const { |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1667 | return RefVal(k, getObjKind(), getCount(), getAutoreleaseCount(), |
| 1668 | getType()); |
| 1669 | } |
| 1670 | |
| 1671 | RefVal autorelease() const { |
| 1672 | return RefVal(getKind(), getObjKind(), getCount(), getAutoreleaseCount()+1, |
| 1673 | getType()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1674 | } |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1675 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1676 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 1677 | ID.AddInteger((unsigned) kind); |
| 1678 | ID.AddInteger(Cnt); |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1679 | ID.AddInteger(ACnt); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1680 | ID.Add(T); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1681 | } |
| 1682 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1683 | void print(llvm::raw_ostream& Out) const; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1684 | }; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1685 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1686 | void RefVal::print(llvm::raw_ostream& Out) const { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1687 | if (!T.isNull()) |
| 1688 | Out << "Tracked Type:" << T.getAsString() << '\n'; |
| 1689 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1690 | switch (getKind()) { |
| 1691 | default: assert(false); |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1692 | case Owned: { |
| 1693 | Out << "Owned"; |
| 1694 | unsigned cnt = getCount(); |
| 1695 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1696 | break; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1697 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1698 | |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1699 | case NotOwned: { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1700 | Out << "NotOwned"; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1701 | unsigned cnt = getCount(); |
| 1702 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1703 | break; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1704 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1705 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1706 | case ReturnedOwned: { |
| 1707 | Out << "ReturnedOwned"; |
| 1708 | unsigned cnt = getCount(); |
| 1709 | if (cnt) Out << " (+ " << cnt << ")"; |
| 1710 | break; |
| 1711 | } |
| 1712 | |
| 1713 | case ReturnedNotOwned: { |
| 1714 | Out << "ReturnedNotOwned"; |
| 1715 | unsigned cnt = getCount(); |
| 1716 | if (cnt) Out << " (+ " << cnt << ")"; |
| 1717 | break; |
| 1718 | } |
| 1719 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1720 | case Released: |
| 1721 | Out << "Released"; |
| 1722 | break; |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1723 | |
| 1724 | case ErrorDeallocGC: |
| 1725 | Out << "-dealloc (GC)"; |
| 1726 | break; |
| 1727 | |
| 1728 | case ErrorDeallocNotOwned: |
| 1729 | Out << "-dealloc (not-owned)"; |
| 1730 | break; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1731 | |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1732 | case ErrorLeak: |
| 1733 | Out << "Leaked"; |
| 1734 | break; |
| 1735 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1736 | case ErrorLeakReturned: |
| 1737 | Out << "Leaked (Bad naming)"; |
| 1738 | break; |
| 1739 | |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 1740 | case ErrorGCLeakReturned: |
| 1741 | Out << "Leaked (GC-ed at return)"; |
| 1742 | break; |
| 1743 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1744 | case ErrorUseAfterRelease: |
| 1745 | Out << "Use-After-Release [ERROR]"; |
| 1746 | break; |
| 1747 | |
| 1748 | case ErrorReleaseNotOwned: |
| 1749 | Out << "Release of Not-Owned [ERROR]"; |
| 1750 | break; |
Ted Kremenek | 80c2418 | 2009-05-09 00:44:07 +0000 | [diff] [blame] | 1751 | |
| 1752 | case RefVal::ErrorOverAutorelease: |
| 1753 | Out << "Over autoreleased"; |
| 1754 | break; |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 1755 | |
| 1756 | case RefVal::ErrorReturnedNotOwned: |
| 1757 | Out << "Non-owned object returned instead of owned"; |
| 1758 | break; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1759 | } |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 1760 | |
| 1761 | if (ACnt) { |
| 1762 | Out << " [ARC +" << ACnt << ']'; |
| 1763 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1764 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1765 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1766 | } // end anonymous namespace |
| 1767 | |
| 1768 | //===----------------------------------------------------------------------===// |
| 1769 | // RefBindings - State used to track object reference counts. |
| 1770 | //===----------------------------------------------------------------------===// |
| 1771 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1772 | typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings; |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1773 | static int RefBIndex = 0; |
| 1774 | |
| 1775 | namespace clang { |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1776 | template<> |
| 1777 | struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> { |
| 1778 | static inline void* GDMIndex() { return &RefBIndex; } |
| 1779 | }; |
| 1780 | } |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1781 | |
| 1782 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1783 | // AutoreleaseBindings - State used to track objects in autorelease pools. |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1784 | //===----------------------------------------------------------------------===// |
| 1785 | |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1786 | typedef llvm::ImmutableMap<SymbolRef, unsigned> ARCounts; |
| 1787 | typedef llvm::ImmutableMap<SymbolRef, ARCounts> ARPoolContents; |
| 1788 | typedef llvm::ImmutableList<SymbolRef> ARStack; |
Ted Kremenek | f9a8e2e | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 1789 | |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1790 | static int AutoRCIndex = 0; |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1791 | static int AutoRBIndex = 0; |
| 1792 | |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1793 | namespace { class VISIBILITY_HIDDEN AutoreleasePoolContents {}; } |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1794 | namespace { class VISIBILITY_HIDDEN AutoreleaseStack {}; } |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1795 | |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1796 | namespace clang { |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1797 | template<> struct GRStateTrait<AutoreleaseStack> |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1798 | : public GRStatePartialTrait<ARStack> { |
| 1799 | static inline void* GDMIndex() { return &AutoRBIndex; } |
| 1800 | }; |
| 1801 | |
| 1802 | template<> struct GRStateTrait<AutoreleasePoolContents> |
| 1803 | : public GRStatePartialTrait<ARPoolContents> { |
| 1804 | static inline void* GDMIndex() { return &AutoRCIndex; } |
| 1805 | }; |
| 1806 | } // end clang namespace |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1807 | |
Ted Kremenek | 7037ab8 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1808 | static SymbolRef GetCurrentAutoreleasePool(const GRState* state) { |
| 1809 | ARStack stack = state->get<AutoreleaseStack>(); |
| 1810 | return stack.isEmpty() ? SymbolRef() : stack.getHead(); |
| 1811 | } |
| 1812 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1813 | static const GRState * SendAutorelease(const GRState *state, |
| 1814 | ARCounts::Factory &F, SymbolRef sym) { |
Ted Kremenek | 7037ab8 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1815 | |
| 1816 | SymbolRef pool = GetCurrentAutoreleasePool(state); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1817 | const ARCounts *cnts = state->get<AutoreleasePoolContents>(pool); |
Ted Kremenek | 7037ab8 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1818 | ARCounts newCnts(0); |
| 1819 | |
| 1820 | if (cnts) { |
| 1821 | const unsigned *cnt = (*cnts).lookup(sym); |
| 1822 | newCnts = F.Add(*cnts, sym, cnt ? *cnt + 1 : 1); |
| 1823 | } |
| 1824 | else |
| 1825 | newCnts = F.Add(F.GetEmptyMap(), sym, 1); |
| 1826 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1827 | return state->set<AutoreleasePoolContents>(pool, newCnts); |
Ted Kremenek | 7037ab8 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1830 | //===----------------------------------------------------------------------===// |
| 1831 | // Transfer functions. |
| 1832 | //===----------------------------------------------------------------------===// |
| 1833 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1834 | namespace { |
| 1835 | |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 1836 | class VISIBILITY_HIDDEN CFRefCount : public GRTransferFuncs { |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1837 | public: |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1838 | class BindingsPrinter : public GRState::Printer { |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1839 | public: |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1840 | virtual void Print(llvm::raw_ostream& Out, const GRState* state, |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1841 | const char* nl, const char* sep); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1842 | }; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1843 | |
| 1844 | private: |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1845 | typedef llvm::DenseMap<const GRExprEngine::NodeTy*, const RetainSummary*> |
| 1846 | SummaryLogTy; |
| 1847 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1848 | RetainSummaryManager Summaries; |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1849 | SummaryLogTy SummaryLog; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1850 | const LangOptions& LOpts; |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1851 | ARCounts::Factory ARCountFactory; |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1852 | |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1853 | BugType *useAfterRelease, *releaseNotOwned; |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1854 | BugType *deallocGC, *deallocNotOwned; |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1855 | BugType *leakWithinFunction, *leakAtReturn; |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 1856 | BugType *overAutorelease; |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 1857 | BugType *returnNotOwnedForOwned; |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1858 | BugReporter *BR; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1859 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1860 | const GRState * Update(const GRState * state, SymbolRef sym, RefVal V, ArgEffect E, |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1861 | RefVal::Kind& hasErr); |
| 1862 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1863 | void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst, |
| 1864 | GRStmtNodeBuilder<GRState>& Builder, |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 1865 | Expr* NodeExpr, Expr* ErrorExpr, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1866 | ExplodedNode<GRState>* Pred, |
| 1867 | const GRState* St, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1868 | RefVal::Kind hasErr, SymbolRef Sym); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1869 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1870 | const GRState * HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V, |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 1871 | llvm::SmallVectorImpl<SymbolRef> &Leaked); |
| 1872 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1873 | ExplodedNode<GRState>* ProcessLeaks(const GRState * state, |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 1874 | llvm::SmallVectorImpl<SymbolRef> &Leaked, |
| 1875 | GenericNodeBuilder &Builder, |
| 1876 | GRExprEngine &Eng, |
| 1877 | ExplodedNode<GRState> *Pred = 0); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1878 | |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 1879 | public: |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 1880 | CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts) |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1881 | : Summaries(Ctx, gcenabled), |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 1882 | LOpts(lopts), useAfterRelease(0), releaseNotOwned(0), |
| 1883 | deallocGC(0), deallocNotOwned(0), |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 1884 | leakWithinFunction(0), leakAtReturn(0), overAutorelease(0), |
| 1885 | returnNotOwnedForOwned(0), BR(0) {} |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1886 | |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1887 | virtual ~CFRefCount() {} |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1888 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1889 | void RegisterChecks(BugReporter &BR); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1890 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 1891 | virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) { |
| 1892 | Printers.push_back(new BindingsPrinter()); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1893 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1894 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1895 | bool isGCEnabled() const { return Summaries.isGCEnabled(); } |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1896 | const LangOptions& getLangOptions() const { return LOpts; } |
| 1897 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 1898 | const RetainSummary *getSummaryOfNode(const ExplodedNode<GRState> *N) const { |
| 1899 | SummaryLogTy::const_iterator I = SummaryLog.find(N); |
| 1900 | return I == SummaryLog.end() ? 0 : I->second; |
| 1901 | } |
| 1902 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1903 | // Calls. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1904 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1905 | void EvalSummary(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1906 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1907 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1908 | Expr* Ex, |
| 1909 | Expr* Receiver, |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 1910 | const RetainSummary& Summ, |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 1911 | ExprIterator arg_beg, ExprIterator arg_end, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1912 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1913 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1914 | virtual void EvalCall(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 199e1a0 | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 1915 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1916 | GRStmtNodeBuilder<GRState>& Builder, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1917 | CallExpr* CE, SVal L, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1918 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 1919 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1920 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1921 | virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1922 | GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1923 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1924 | ObjCMessageExpr* ME, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1925 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1926 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1927 | bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1928 | GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1929 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1930 | ObjCMessageExpr* ME, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1931 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1932 | |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 1933 | // Stores. |
| 1934 | virtual void EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val); |
| 1935 | |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1936 | // End-of-path. |
| 1937 | |
| 1938 | virtual void EvalEndPath(GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1939 | GREndPathNodeBuilder<GRState>& Builder); |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1940 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1941 | virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1942 | GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1943 | GRStmtNodeBuilder<GRState>& Builder, |
| 1944 | ExplodedNode<GRState>* Pred, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1945 | Stmt* S, const GRState* state, |
| 1946 | SymbolReaper& SymReaper); |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 1947 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 1948 | std::pair<ExplodedNode<GRState>*, const GRState *> |
| 1949 | HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd, |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 1950 | ExplodedNode<GRState>* Pred, GRExprEngine &Eng, |
| 1951 | SymbolRef Sym, RefVal V, bool &stop); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1952 | // Return statements. |
| 1953 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1954 | virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1955 | GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1956 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1957 | ReturnStmt* S, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1958 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1959 | |
| 1960 | // Assumptions. |
| 1961 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 1962 | virtual const GRState *EvalAssume(const GRState* state, SVal condition, |
| 1963 | bool assumption); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1964 | }; |
| 1965 | |
| 1966 | } // end anonymous namespace |
| 1967 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1968 | static void PrintPool(llvm::raw_ostream &Out, SymbolRef Sym, |
| 1969 | const GRState *state) { |
Ted Kremenek | 7037ab8 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1970 | Out << ' '; |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 1971 | if (Sym) |
| 1972 | Out << Sym->getSymbolID(); |
Ted Kremenek | 7037ab8 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 1973 | else |
| 1974 | Out << "<pool>"; |
| 1975 | Out << ":{"; |
| 1976 | |
| 1977 | // Get the contents of the pool. |
| 1978 | if (const ARCounts *cnts = state->get<AutoreleasePoolContents>(Sym)) |
| 1979 | for (ARCounts::iterator J=cnts->begin(), EJ=cnts->end(); J != EJ; ++J) |
| 1980 | Out << '(' << J.getKey() << ',' << J.getData() << ')'; |
| 1981 | |
| 1982 | Out << '}'; |
| 1983 | } |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1984 | |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1985 | void CFRefCount::BindingsPrinter::Print(llvm::raw_ostream& Out, |
| 1986 | const GRState* state, |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1987 | const char* nl, const char* sep) { |
| 1988 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1989 | RefBindings B = state->get<RefBindings>(); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1990 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1991 | if (!B.isEmpty()) |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1992 | Out << sep << nl; |
| 1993 | |
| 1994 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 1995 | Out << (*I).first << " : "; |
| 1996 | (*I).second.print(Out); |
| 1997 | Out << nl; |
| 1998 | } |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 1999 | |
| 2000 | // Print the autorelease stack. |
Ted Kremenek | 7037ab8 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 2001 | Out << sep << nl << "AR pool stack:"; |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 2002 | ARStack stack = state->get<AutoreleaseStack>(); |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 2003 | |
Ted Kremenek | 7037ab8 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 2004 | PrintPool(Out, SymbolRef(), state); // Print the caller's pool. |
| 2005 | for (ARStack::iterator I=stack.begin(), E=stack.end(); I!=E; ++I) |
| 2006 | PrintPool(Out, *I, state); |
| 2007 | |
| 2008 | Out << nl; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2011 | //===----------------------------------------------------------------------===// |
| 2012 | // Error reporting. |
| 2013 | //===----------------------------------------------------------------------===// |
| 2014 | |
| 2015 | namespace { |
| 2016 | |
| 2017 | //===-------------===// |
| 2018 | // Bug Descriptions. // |
| 2019 | //===-------------===// |
| 2020 | |
| 2021 | class VISIBILITY_HIDDEN CFRefBug : public BugType { |
| 2022 | protected: |
| 2023 | CFRefCount& TF; |
| 2024 | |
| 2025 | CFRefBug(CFRefCount* tf, const char* name) |
| 2026 | : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {} |
| 2027 | public: |
| 2028 | |
| 2029 | CFRefCount& getTF() { return TF; } |
| 2030 | const CFRefCount& getTF() const { return TF; } |
| 2031 | |
| 2032 | // FIXME: Eventually remove. |
| 2033 | virtual const char* getDescription() const = 0; |
| 2034 | |
| 2035 | virtual bool isLeak() const { return false; } |
| 2036 | }; |
| 2037 | |
| 2038 | class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug { |
| 2039 | public: |
| 2040 | UseAfterRelease(CFRefCount* tf) |
| 2041 | : CFRefBug(tf, "Use-after-release") {} |
| 2042 | |
| 2043 | const char* getDescription() const { |
| 2044 | return "Reference-counted object is used after it is released"; |
| 2045 | } |
| 2046 | }; |
| 2047 | |
| 2048 | class VISIBILITY_HIDDEN BadRelease : public CFRefBug { |
| 2049 | public: |
| 2050 | BadRelease(CFRefCount* tf) : CFRefBug(tf, "Bad release") {} |
| 2051 | |
| 2052 | const char* getDescription() const { |
| 2053 | return "Incorrect decrement of the reference count of an " |
| 2054 | "object is not owned at this point by the caller"; |
| 2055 | } |
| 2056 | }; |
| 2057 | |
| 2058 | class VISIBILITY_HIDDEN DeallocGC : public CFRefBug { |
| 2059 | public: |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 2060 | DeallocGC(CFRefCount *tf) |
| 2061 | : CFRefBug(tf, "-dealloc called while using garbage collection") {} |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2062 | |
| 2063 | const char *getDescription() const { |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 2064 | return "-dealloc called while using garbage collection"; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2065 | } |
| 2066 | }; |
| 2067 | |
| 2068 | class VISIBILITY_HIDDEN DeallocNotOwned : public CFRefBug { |
| 2069 | public: |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 2070 | DeallocNotOwned(CFRefCount *tf) |
| 2071 | : CFRefBug(tf, "-dealloc sent to non-exclusively owned object") {} |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2072 | |
| 2073 | const char *getDescription() const { |
| 2074 | return "-dealloc sent to object that may be referenced elsewhere"; |
| 2075 | } |
| 2076 | }; |
| 2077 | |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 2078 | class VISIBILITY_HIDDEN OverAutorelease : public CFRefBug { |
| 2079 | public: |
| 2080 | OverAutorelease(CFRefCount *tf) : |
| 2081 | CFRefBug(tf, "Object sent -autorelease too many times") {} |
| 2082 | |
| 2083 | const char *getDescription() const { |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 2084 | return "Object sent -autorelease too many times"; |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 2085 | } |
| 2086 | }; |
| 2087 | |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 2088 | class VISIBILITY_HIDDEN ReturnedNotOwnedForOwned : public CFRefBug { |
| 2089 | public: |
| 2090 | ReturnedNotOwnedForOwned(CFRefCount *tf) : |
| 2091 | CFRefBug(tf, "Method should return an owned object") {} |
| 2092 | |
| 2093 | const char *getDescription() const { |
| 2094 | return "Object with +0 retain counts returned to caller where a +1 " |
| 2095 | "(owning) retain count is expected"; |
| 2096 | } |
| 2097 | }; |
| 2098 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2099 | class VISIBILITY_HIDDEN Leak : public CFRefBug { |
| 2100 | const bool isReturn; |
| 2101 | protected: |
| 2102 | Leak(CFRefCount* tf, const char* name, bool isRet) |
| 2103 | : CFRefBug(tf, name), isReturn(isRet) {} |
| 2104 | public: |
| 2105 | |
| 2106 | const char* getDescription() const { return ""; } |
| 2107 | |
| 2108 | bool isLeak() const { return true; } |
| 2109 | }; |
| 2110 | |
| 2111 | class VISIBILITY_HIDDEN LeakAtReturn : public Leak { |
| 2112 | public: |
| 2113 | LeakAtReturn(CFRefCount* tf, const char* name) |
| 2114 | : Leak(tf, name, true) {} |
| 2115 | }; |
| 2116 | |
| 2117 | class VISIBILITY_HIDDEN LeakWithinFunction : public Leak { |
| 2118 | public: |
| 2119 | LeakWithinFunction(CFRefCount* tf, const char* name) |
| 2120 | : Leak(tf, name, false) {} |
| 2121 | }; |
| 2122 | |
| 2123 | //===---------===// |
| 2124 | // Bug Reports. // |
| 2125 | //===---------===// |
| 2126 | |
| 2127 | class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport { |
| 2128 | protected: |
| 2129 | SymbolRef Sym; |
| 2130 | const CFRefCount &TF; |
| 2131 | public: |
| 2132 | CFRefReport(CFRefBug& D, const CFRefCount &tf, |
| 2133 | ExplodedNode<GRState> *n, SymbolRef sym) |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 2134 | : RangedBugReport(D, D.getDescription(), n), Sym(sym), TF(tf) {} |
| 2135 | |
| 2136 | CFRefReport(CFRefBug& D, const CFRefCount &tf, |
| 2137 | ExplodedNode<GRState> *n, SymbolRef sym, const char* endText) |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 2138 | : RangedBugReport(D, D.getDescription(), endText, n), Sym(sym), TF(tf) {} |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2139 | |
| 2140 | virtual ~CFRefReport() {} |
| 2141 | |
| 2142 | CFRefBug& getBugType() { |
| 2143 | return (CFRefBug&) RangedBugReport::getBugType(); |
| 2144 | } |
| 2145 | const CFRefBug& getBugType() const { |
| 2146 | return (const CFRefBug&) RangedBugReport::getBugType(); |
| 2147 | } |
| 2148 | |
| 2149 | virtual void getRanges(BugReporter& BR, const SourceRange*& beg, |
| 2150 | const SourceRange*& end) { |
| 2151 | |
| 2152 | if (!getBugType().isLeak()) |
| 2153 | RangedBugReport::getRanges(BR, beg, end); |
| 2154 | else |
| 2155 | beg = end = 0; |
| 2156 | } |
| 2157 | |
| 2158 | SymbolRef getSymbol() const { return Sym; } |
| 2159 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2160 | PathDiagnosticPiece* getEndPath(BugReporterContext& BRC, |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2161 | const ExplodedNode<GRState>* N); |
| 2162 | |
| 2163 | std::pair<const char**,const char**> getExtraDescriptiveText(); |
| 2164 | |
| 2165 | PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState>* N, |
| 2166 | const ExplodedNode<GRState>* PrevN, |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2167 | BugReporterContext& BRC); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2168 | }; |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 2169 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2170 | class VISIBILITY_HIDDEN CFRefLeakReport : public CFRefReport { |
| 2171 | SourceLocation AllocSite; |
| 2172 | const MemRegion* AllocBinding; |
| 2173 | public: |
| 2174 | CFRefLeakReport(CFRefBug& D, const CFRefCount &tf, |
| 2175 | ExplodedNode<GRState> *n, SymbolRef sym, |
| 2176 | GRExprEngine& Eng); |
| 2177 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2178 | PathDiagnosticPiece* getEndPath(BugReporterContext& BRC, |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2179 | const ExplodedNode<GRState>* N); |
| 2180 | |
| 2181 | SourceLocation getLocation() const { return AllocSite; } |
| 2182 | }; |
| 2183 | } // end anonymous namespace |
| 2184 | |
| 2185 | void CFRefCount::RegisterChecks(BugReporter& BR) { |
| 2186 | useAfterRelease = new UseAfterRelease(this); |
| 2187 | BR.Register(useAfterRelease); |
| 2188 | |
| 2189 | releaseNotOwned = new BadRelease(this); |
| 2190 | BR.Register(releaseNotOwned); |
| 2191 | |
| 2192 | deallocGC = new DeallocGC(this); |
| 2193 | BR.Register(deallocGC); |
| 2194 | |
| 2195 | deallocNotOwned = new DeallocNotOwned(this); |
| 2196 | BR.Register(deallocNotOwned); |
| 2197 | |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 2198 | overAutorelease = new OverAutorelease(this); |
| 2199 | BR.Register(overAutorelease); |
| 2200 | |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 2201 | returnNotOwnedForOwned = new ReturnedNotOwnedForOwned(this); |
| 2202 | BR.Register(returnNotOwnedForOwned); |
| 2203 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2204 | // First register "return" leaks. |
| 2205 | const char* name = 0; |
| 2206 | |
| 2207 | if (isGCEnabled()) |
| 2208 | name = "Leak of returned object when using garbage collection"; |
| 2209 | else if (getLangOptions().getGCMode() == LangOptions::HybridGC) |
| 2210 | name = "Leak of returned object when not using garbage collection (GC) in " |
| 2211 | "dual GC/non-GC code"; |
| 2212 | else { |
| 2213 | assert(getLangOptions().getGCMode() == LangOptions::NonGC); |
| 2214 | name = "Leak of returned object"; |
| 2215 | } |
| 2216 | |
| 2217 | leakAtReturn = new LeakAtReturn(this, name); |
| 2218 | BR.Register(leakAtReturn); |
| 2219 | |
| 2220 | // Second, register leaks within a function/method. |
| 2221 | if (isGCEnabled()) |
| 2222 | name = "Leak of object when using garbage collection"; |
| 2223 | else if (getLangOptions().getGCMode() == LangOptions::HybridGC) |
| 2224 | name = "Leak of object when not using garbage collection (GC) in " |
| 2225 | "dual GC/non-GC code"; |
| 2226 | else { |
| 2227 | assert(getLangOptions().getGCMode() == LangOptions::NonGC); |
| 2228 | name = "Leak"; |
| 2229 | } |
| 2230 | |
| 2231 | leakWithinFunction = new LeakWithinFunction(this, name); |
| 2232 | BR.Register(leakWithinFunction); |
| 2233 | |
| 2234 | // Save the reference to the BugReporter. |
| 2235 | this->BR = &BR; |
| 2236 | } |
| 2237 | |
| 2238 | static const char* Msgs[] = { |
| 2239 | // GC only |
| 2240 | "Code is compiled to only use garbage collection", |
| 2241 | // No GC. |
| 2242 | "Code is compiled to use reference counts", |
| 2243 | // Hybrid, with GC. |
| 2244 | "Code is compiled to use either garbage collection (GC) or reference counts" |
| 2245 | " (non-GC). The bug occurs with GC enabled", |
| 2246 | // Hybrid, without GC |
| 2247 | "Code is compiled to use either garbage collection (GC) or reference counts" |
| 2248 | " (non-GC). The bug occurs in non-GC mode" |
| 2249 | }; |
| 2250 | |
| 2251 | std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() { |
| 2252 | CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF(); |
| 2253 | |
| 2254 | switch (TF.getLangOptions().getGCMode()) { |
| 2255 | default: |
| 2256 | assert(false); |
| 2257 | |
| 2258 | case LangOptions::GCOnly: |
| 2259 | assert (TF.isGCEnabled()); |
| 2260 | return std::make_pair(&Msgs[0], &Msgs[0]+1); |
| 2261 | |
| 2262 | case LangOptions::NonGC: |
| 2263 | assert (!TF.isGCEnabled()); |
| 2264 | return std::make_pair(&Msgs[1], &Msgs[1]+1); |
| 2265 | |
| 2266 | case LangOptions::HybridGC: |
| 2267 | if (TF.isGCEnabled()) |
| 2268 | return std::make_pair(&Msgs[2], &Msgs[2]+1); |
| 2269 | else |
| 2270 | return std::make_pair(&Msgs[3], &Msgs[3]+1); |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | static inline bool contains(const llvm::SmallVectorImpl<ArgEffect>& V, |
| 2275 | ArgEffect X) { |
| 2276 | for (llvm::SmallVectorImpl<ArgEffect>::const_iterator I=V.begin(), E=V.end(); |
| 2277 | I!=E; ++I) |
| 2278 | if (*I == X) return true; |
| 2279 | |
| 2280 | return false; |
| 2281 | } |
| 2282 | |
| 2283 | PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode<GRState>* N, |
| 2284 | const ExplodedNode<GRState>* PrevN, |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2285 | BugReporterContext& BRC) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2286 | |
Ted Kremenek | 2033a95 | 2009-05-13 07:12:33 +0000 | [diff] [blame] | 2287 | if (!isa<PostStmt>(N->getLocation())) |
| 2288 | return NULL; |
| 2289 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2290 | // Check if the type state has changed. |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2291 | const GRState *PrevSt = PrevN->getState(); |
| 2292 | const GRState *CurrSt = N->getState(); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2293 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2294 | const RefVal* CurrT = CurrSt->get<RefBindings>(Sym); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2295 | if (!CurrT) return NULL; |
| 2296 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2297 | const RefVal &CurrV = *CurrT; |
| 2298 | const RefVal *PrevT = PrevSt->get<RefBindings>(Sym); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2299 | |
| 2300 | // Create a string buffer to constain all the useful things we want |
| 2301 | // to tell the user. |
| 2302 | std::string sbuf; |
| 2303 | llvm::raw_string_ostream os(sbuf); |
| 2304 | |
| 2305 | // This is the allocation site since the previous node had no bindings |
| 2306 | // for this symbol. |
| 2307 | if (!PrevT) { |
| 2308 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 2309 | |
| 2310 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 2311 | // Get the name of the callee (if it is available). |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2312 | SVal X = CurrSt->getSValAsScalarOrLoc(CE->getCallee()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2313 | if (const FunctionDecl* FD = X.getAsFunctionDecl()) |
| 2314 | os << "Call to function '" << FD->getNameAsString() <<'\''; |
| 2315 | else |
| 2316 | os << "function call"; |
| 2317 | } |
| 2318 | else { |
| 2319 | assert (isa<ObjCMessageExpr>(S)); |
| 2320 | os << "Method"; |
| 2321 | } |
| 2322 | |
| 2323 | if (CurrV.getObjKind() == RetEffect::CF) { |
| 2324 | os << " returns a Core Foundation object with a "; |
| 2325 | } |
| 2326 | else { |
| 2327 | assert (CurrV.getObjKind() == RetEffect::ObjC); |
| 2328 | os << " returns an Objective-C object with a "; |
| 2329 | } |
| 2330 | |
| 2331 | if (CurrV.isOwned()) { |
| 2332 | os << "+1 retain count (owning reference)."; |
| 2333 | |
| 2334 | if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) { |
| 2335 | assert(CurrV.getObjKind() == RetEffect::CF); |
| 2336 | os << " " |
| 2337 | "Core Foundation objects are not automatically garbage collected."; |
| 2338 | } |
| 2339 | } |
| 2340 | else { |
| 2341 | assert (CurrV.isNotOwned()); |
| 2342 | os << "+0 retain count (non-owning reference)."; |
| 2343 | } |
| 2344 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2345 | PathDiagnosticLocation Pos(S, BRC.getSourceManager()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2346 | return new PathDiagnosticEventPiece(Pos, os.str()); |
| 2347 | } |
| 2348 | |
| 2349 | // Gather up the effects that were performed on the object at this |
| 2350 | // program point |
| 2351 | llvm::SmallVector<ArgEffect, 2> AEffects; |
| 2352 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2353 | if (const RetainSummary *Summ = |
| 2354 | TF.getSummaryOfNode(BRC.getNodeResolver().getOriginalNode(N))) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2355 | // We only have summaries attached to nodes after evaluating CallExpr and |
| 2356 | // ObjCMessageExprs. |
| 2357 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 2358 | |
| 2359 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 2360 | // Iterate through the parameter expressions and see if the symbol |
| 2361 | // was ever passed as an argument. |
| 2362 | unsigned i = 0; |
| 2363 | |
| 2364 | for (CallExpr::arg_iterator AI=CE->arg_begin(), AE=CE->arg_end(); |
| 2365 | AI!=AE; ++AI, ++i) { |
| 2366 | |
| 2367 | // Retrieve the value of the argument. Is it the symbol |
| 2368 | // we are interested in? |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2369 | if (CurrSt->getSValAsScalarOrLoc(*AI).getAsLocSymbol() != Sym) |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2370 | continue; |
| 2371 | |
| 2372 | // We have an argument. Get the effect! |
| 2373 | AEffects.push_back(Summ->getArg(i)); |
| 2374 | } |
| 2375 | } |
| 2376 | else if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(S)) { |
| 2377 | if (Expr *receiver = ME->getReceiver()) |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2378 | if (CurrSt->getSValAsScalarOrLoc(receiver).getAsLocSymbol() == Sym) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2379 | // The symbol we are tracking is the receiver. |
| 2380 | AEffects.push_back(Summ->getReceiverEffect()); |
| 2381 | } |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | do { |
| 2386 | // Get the previous type state. |
| 2387 | RefVal PrevV = *PrevT; |
| 2388 | |
| 2389 | // Specially handle -dealloc. |
| 2390 | if (!TF.isGCEnabled() && contains(AEffects, Dealloc)) { |
| 2391 | // Determine if the object's reference count was pushed to zero. |
| 2392 | assert(!(PrevV == CurrV) && "The typestate *must* have changed."); |
| 2393 | // We may not have transitioned to 'release' if we hit an error. |
| 2394 | // This case is handled elsewhere. |
| 2395 | if (CurrV.getKind() == RefVal::Released) { |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 2396 | assert(CurrV.getCombinedCounts() == 0); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2397 | os << "Object released by directly sending the '-dealloc' message"; |
| 2398 | break; |
| 2399 | } |
| 2400 | } |
| 2401 | |
| 2402 | // Specially handle CFMakeCollectable and friends. |
| 2403 | if (contains(AEffects, MakeCollectable)) { |
| 2404 | // Get the name of the function. |
| 2405 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2406 | SVal X = CurrSt->getSValAsScalarOrLoc(cast<CallExpr>(S)->getCallee()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2407 | const FunctionDecl* FD = X.getAsFunctionDecl(); |
| 2408 | const std::string& FName = FD->getNameAsString(); |
| 2409 | |
| 2410 | if (TF.isGCEnabled()) { |
| 2411 | // Determine if the object's reference count was pushed to zero. |
| 2412 | assert(!(PrevV == CurrV) && "The typestate *must* have changed."); |
| 2413 | |
| 2414 | os << "In GC mode a call to '" << FName |
| 2415 | << "' decrements an object's retain count and registers the " |
| 2416 | "object with the garbage collector. "; |
| 2417 | |
| 2418 | if (CurrV.getKind() == RefVal::Released) { |
| 2419 | assert(CurrV.getCount() == 0); |
| 2420 | os << "Since it now has a 0 retain count the object can be " |
| 2421 | "automatically collected by the garbage collector."; |
| 2422 | } |
| 2423 | else |
| 2424 | os << "An object must have a 0 retain count to be garbage collected. " |
| 2425 | "After this call its retain count is +" << CurrV.getCount() |
| 2426 | << '.'; |
| 2427 | } |
| 2428 | else |
| 2429 | os << "When GC is not enabled a call to '" << FName |
| 2430 | << "' has no effect on its argument."; |
| 2431 | |
| 2432 | // Nothing more to say. |
| 2433 | break; |
| 2434 | } |
| 2435 | |
| 2436 | // Determine if the typestate has changed. |
| 2437 | if (!(PrevV == CurrV)) |
| 2438 | switch (CurrV.getKind()) { |
| 2439 | case RefVal::Owned: |
| 2440 | case RefVal::NotOwned: |
| 2441 | |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 2442 | if (PrevV.getCount() == CurrV.getCount()) { |
| 2443 | // Did an autorelease message get sent? |
| 2444 | if (PrevV.getAutoreleaseCount() == CurrV.getAutoreleaseCount()) |
| 2445 | return 0; |
| 2446 | |
Zhongxing Xu | 264e937 | 2009-05-12 10:10:00 +0000 | [diff] [blame] | 2447 | assert(PrevV.getAutoreleaseCount() < CurrV.getAutoreleaseCount()); |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 2448 | os << "Object sent -autorelease message"; |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 2449 | break; |
| 2450 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2451 | |
| 2452 | if (PrevV.getCount() > CurrV.getCount()) |
| 2453 | os << "Reference count decremented."; |
| 2454 | else |
| 2455 | os << "Reference count incremented."; |
| 2456 | |
| 2457 | if (unsigned Count = CurrV.getCount()) |
| 2458 | os << " The object now has a +" << Count << " retain count."; |
| 2459 | |
| 2460 | if (PrevV.getKind() == RefVal::Released) { |
| 2461 | assert(TF.isGCEnabled() && CurrV.getCount() > 0); |
| 2462 | os << " The object is not eligible for garbage collection until the " |
| 2463 | "retain count reaches 0 again."; |
| 2464 | } |
| 2465 | |
| 2466 | break; |
| 2467 | |
| 2468 | case RefVal::Released: |
| 2469 | os << "Object released."; |
| 2470 | break; |
| 2471 | |
| 2472 | case RefVal::ReturnedOwned: |
| 2473 | os << "Object returned to caller as an owning reference (single retain " |
| 2474 | "count transferred to caller)."; |
| 2475 | break; |
| 2476 | |
| 2477 | case RefVal::ReturnedNotOwned: |
| 2478 | os << "Object returned to caller with a +0 (non-owning) retain count."; |
| 2479 | break; |
| 2480 | |
| 2481 | default: |
| 2482 | return NULL; |
| 2483 | } |
| 2484 | |
| 2485 | // Emit any remaining diagnostics for the argument effects (if any). |
| 2486 | for (llvm::SmallVectorImpl<ArgEffect>::iterator I=AEffects.begin(), |
| 2487 | E=AEffects.end(); I != E; ++I) { |
| 2488 | |
| 2489 | // A bunch of things have alternate behavior under GC. |
| 2490 | if (TF.isGCEnabled()) |
| 2491 | switch (*I) { |
| 2492 | default: break; |
| 2493 | case Autorelease: |
| 2494 | os << "In GC mode an 'autorelease' has no effect."; |
| 2495 | continue; |
| 2496 | case IncRefMsg: |
| 2497 | os << "In GC mode the 'retain' message has no effect."; |
| 2498 | continue; |
| 2499 | case DecRefMsg: |
| 2500 | os << "In GC mode the 'release' message has no effect."; |
| 2501 | continue; |
| 2502 | } |
| 2503 | } |
| 2504 | } while(0); |
| 2505 | |
| 2506 | if (os.str().empty()) |
| 2507 | return 0; // We have nothing to say! |
Ted Kremenek | 2033a95 | 2009-05-13 07:12:33 +0000 | [diff] [blame] | 2508 | |
| 2509 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2510 | PathDiagnosticLocation Pos(S, BRC.getSourceManager()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2511 | PathDiagnosticPiece* P = new PathDiagnosticEventPiece(Pos, os.str()); |
| 2512 | |
| 2513 | // Add the range by scanning the children of the statement for any bindings |
| 2514 | // to Sym. |
| 2515 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 2516 | if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2517 | if (CurrSt->getSValAsScalarOrLoc(Exp).getAsLocSymbol() == Sym) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2518 | P->addRange(Exp->getSourceRange()); |
| 2519 | break; |
| 2520 | } |
| 2521 | |
| 2522 | return P; |
| 2523 | } |
| 2524 | |
| 2525 | namespace { |
| 2526 | class VISIBILITY_HIDDEN FindUniqueBinding : |
| 2527 | public StoreManager::BindingsHandler { |
| 2528 | SymbolRef Sym; |
| 2529 | const MemRegion* Binding; |
| 2530 | bool First; |
| 2531 | |
| 2532 | public: |
| 2533 | FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {} |
| 2534 | |
| 2535 | bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R, |
| 2536 | SVal val) { |
| 2537 | |
| 2538 | SymbolRef SymV = val.getAsSymbol(); |
| 2539 | if (!SymV || SymV != Sym) |
| 2540 | return true; |
| 2541 | |
| 2542 | if (Binding) { |
| 2543 | First = false; |
| 2544 | return false; |
| 2545 | } |
| 2546 | else |
| 2547 | Binding = R; |
| 2548 | |
| 2549 | return true; |
| 2550 | } |
| 2551 | |
| 2552 | operator bool() { return First && Binding; } |
| 2553 | const MemRegion* getRegion() { return Binding; } |
| 2554 | }; |
| 2555 | } |
| 2556 | |
| 2557 | static std::pair<const ExplodedNode<GRState>*,const MemRegion*> |
| 2558 | GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode<GRState>* N, |
| 2559 | SymbolRef Sym) { |
| 2560 | |
| 2561 | // Find both first node that referred to the tracked symbol and the |
| 2562 | // memory location that value was store to. |
| 2563 | const ExplodedNode<GRState>* Last = N; |
| 2564 | const MemRegion* FirstBinding = 0; |
| 2565 | |
| 2566 | while (N) { |
| 2567 | const GRState* St = N->getState(); |
| 2568 | RefBindings B = St->get<RefBindings>(); |
| 2569 | |
| 2570 | if (!B.lookup(Sym)) |
| 2571 | break; |
| 2572 | |
| 2573 | FindUniqueBinding FB(Sym); |
| 2574 | StateMgr.iterBindings(St, FB); |
| 2575 | if (FB) FirstBinding = FB.getRegion(); |
| 2576 | |
| 2577 | Last = N; |
| 2578 | N = N->pred_empty() ? NULL : *(N->pred_begin()); |
| 2579 | } |
| 2580 | |
| 2581 | return std::make_pair(Last, FirstBinding); |
| 2582 | } |
| 2583 | |
| 2584 | PathDiagnosticPiece* |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2585 | CFRefReport::getEndPath(BugReporterContext& BRC, |
| 2586 | const ExplodedNode<GRState>* EndN) { |
| 2587 | // Tell the BugReporterContext to report cases when the tracked symbol is |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2588 | // assigned to different variables, etc. |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2589 | BRC.addNotableSymbol(Sym); |
| 2590 | return RangedBugReport::getEndPath(BRC, EndN); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | PathDiagnosticPiece* |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2594 | CFRefLeakReport::getEndPath(BugReporterContext& BRC, |
| 2595 | const ExplodedNode<GRState>* EndN){ |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2596 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2597 | // Tell the BugReporterContext to report cases when the tracked symbol is |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2598 | // assigned to different variables, etc. |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2599 | BRC.addNotableSymbol(Sym); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2600 | |
| 2601 | // We are reporting a leak. Walk up the graph to get to the first node where |
| 2602 | // the symbol appeared, and also get the first VarDecl that tracked object |
| 2603 | // is stored to. |
| 2604 | const ExplodedNode<GRState>* AllocNode = 0; |
| 2605 | const MemRegion* FirstBinding = 0; |
| 2606 | |
| 2607 | llvm::tie(AllocNode, FirstBinding) = |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 2608 | GetAllocationSite(BRC.getStateManager(), EndN, Sym); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2609 | |
| 2610 | // Get the allocate site. |
| 2611 | assert(AllocNode); |
| 2612 | Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt(); |
| 2613 | |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2614 | SourceManager& SMgr = BRC.getSourceManager(); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2615 | unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart()); |
| 2616 | |
| 2617 | // Compute an actual location for the leak. Sometimes a leak doesn't |
| 2618 | // occur at an actual statement (e.g., transition between blocks; end |
| 2619 | // of function) so we need to walk the graph and compute a real location. |
| 2620 | const ExplodedNode<GRState>* LeakN = EndN; |
| 2621 | PathDiagnosticLocation L; |
| 2622 | |
| 2623 | while (LeakN) { |
| 2624 | ProgramPoint P = LeakN->getLocation(); |
| 2625 | |
| 2626 | if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) { |
| 2627 | L = PathDiagnosticLocation(PS->getStmt()->getLocStart(), SMgr); |
| 2628 | break; |
| 2629 | } |
| 2630 | else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { |
| 2631 | if (const Stmt* Term = BE->getSrc()->getTerminator()) { |
| 2632 | L = PathDiagnosticLocation(Term->getLocStart(), SMgr); |
| 2633 | break; |
| 2634 | } |
| 2635 | } |
| 2636 | |
| 2637 | LeakN = LeakN->succ_empty() ? 0 : *(LeakN->succ_begin()); |
| 2638 | } |
| 2639 | |
| 2640 | if (!L.isValid()) { |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2641 | const Decl &D = BRC.getCodeDecl(); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2642 | L = PathDiagnosticLocation(D.getBodyRBrace(), SMgr); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | std::string sbuf; |
| 2646 | llvm::raw_string_ostream os(sbuf); |
| 2647 | |
| 2648 | os << "Object allocated on line " << AllocLine; |
| 2649 | |
| 2650 | if (FirstBinding) |
| 2651 | os << " and stored into '" << FirstBinding->getString() << '\''; |
| 2652 | |
| 2653 | // Get the retain count. |
| 2654 | const RefVal* RV = EndN->getState()->get<RefBindings>(Sym); |
| 2655 | |
| 2656 | if (RV->getKind() == RefVal::ErrorLeakReturned) { |
| 2657 | // FIXME: Per comments in rdar://6320065, "create" only applies to CF |
| 2658 | // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership |
| 2659 | // to the caller for NS objects. |
Ted Kremenek | 8966bc1 | 2009-05-06 21:39:49 +0000 | [diff] [blame] | 2660 | ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BRC.getCodeDecl()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2661 | os << " is returned from a method whose name ('" |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 2662 | << MD.getSelector().getAsString() |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2663 | << "') does not contain 'copy' or otherwise starts with" |
| 2664 | " 'new' or 'alloc'. This violates the naming convention rules given" |
Ted Kremenek | 8987a02 | 2009-04-29 22:25:52 +0000 | [diff] [blame] | 2665 | " in the Memory Management Guide for Cocoa (object leaked)"; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2666 | } |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 2667 | else if (RV->getKind() == RefVal::ErrorGCLeakReturned) { |
| 2668 | ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BRC.getCodeDecl()); |
| 2669 | os << " and returned from method '" << MD.getSelector().getAsString() |
Ted Kremenek | 82f2be5 | 2009-05-10 16:52:15 +0000 | [diff] [blame] | 2670 | << "' is potentially leaked when using garbage collection. Callers " |
| 2671 | "of this method do not expect a returned object with a +1 retain " |
| 2672 | "count since they expect the object to be managed by the garbage " |
| 2673 | "collector"; |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 2674 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2675 | else |
| 2676 | os << " is no longer referenced after this point and has a retain count of" |
Ted Kremenek | 8987a02 | 2009-04-29 22:25:52 +0000 | [diff] [blame] | 2677 | " +" << RV->getCount() << " (object leaked)"; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2678 | |
| 2679 | return new PathDiagnosticEventPiece(L, os.str()); |
| 2680 | } |
| 2681 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2682 | CFRefLeakReport::CFRefLeakReport(CFRefBug& D, const CFRefCount &tf, |
| 2683 | ExplodedNode<GRState> *n, |
| 2684 | SymbolRef sym, GRExprEngine& Eng) |
| 2685 | : CFRefReport(D, tf, n, sym) |
| 2686 | { |
| 2687 | |
| 2688 | // Most bug reports are cached at the location where they occured. |
| 2689 | // With leaks, we want to unique them by the location where they were |
| 2690 | // allocated, and only report a single path. To do this, we need to find |
| 2691 | // the allocation site of a piece of tracked memory, which we do via a |
| 2692 | // call to GetAllocationSite. This will walk the ExplodedGraph backwards. |
| 2693 | // Note that this is *not* the trimmed graph; we are guaranteed, however, |
| 2694 | // that all ancestor nodes that represent the allocation site have the |
| 2695 | // same SourceLocation. |
| 2696 | const ExplodedNode<GRState>* AllocNode = 0; |
| 2697 | |
| 2698 | llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding. |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 2699 | GetAllocationSite(Eng.getStateManager(), getEndNode(), getSymbol()); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2700 | |
| 2701 | // Get the SourceLocation for the allocation site. |
| 2702 | ProgramPoint P = AllocNode->getLocation(); |
| 2703 | AllocSite = cast<PostStmt>(P).getStmt()->getLocStart(); |
| 2704 | |
| 2705 | // Fill in the description of the bug. |
| 2706 | Description.clear(); |
| 2707 | llvm::raw_string_ostream os(Description); |
| 2708 | SourceManager& SMgr = Eng.getContext().getSourceManager(); |
| 2709 | unsigned AllocLine = SMgr.getInstantiationLineNumber(AllocSite); |
Ted Kremenek | dd924e2 | 2009-05-02 19:05:19 +0000 | [diff] [blame] | 2710 | os << "Potential leak "; |
| 2711 | if (tf.isGCEnabled()) { |
| 2712 | os << "(when using garbage collection) "; |
| 2713 | } |
| 2714 | os << "of an object allocated on line " << AllocLine; |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 2715 | |
| 2716 | // FIXME: AllocBinding doesn't get populated for RegionStore yet. |
| 2717 | if (AllocBinding) |
| 2718 | os << " and stored into '" << AllocBinding->getString() << '\''; |
| 2719 | } |
| 2720 | |
| 2721 | //===----------------------------------------------------------------------===// |
| 2722 | // Main checker logic. |
| 2723 | //===----------------------------------------------------------------------===// |
| 2724 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2725 | /// GetReturnType - Used to get the return type of a message expression or |
| 2726 | /// function call with the intention of affixing that type to a tracked symbol. |
| 2727 | /// While the the return type can be queried directly from RetEx, when |
| 2728 | /// invoking class methods we augment to the return type to be that of |
| 2729 | /// a pointer to the class (as opposed it just being id). |
| 2730 | static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) { |
| 2731 | |
| 2732 | QualType RetTy = RetE->getType(); |
| 2733 | |
| 2734 | // FIXME: We aren't handling id<...>. |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 2735 | const PointerType* PT = RetTy->getAsPointerType(); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2736 | if (!PT) |
| 2737 | return RetTy; |
| 2738 | |
| 2739 | // If RetEx is not a message expression just return its type. |
| 2740 | // If RetEx is a message expression, return its types if it is something |
| 2741 | /// more specific than id. |
| 2742 | |
| 2743 | ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE); |
| 2744 | |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 2745 | if (!ME || !Ctx.isObjCIdStructType(PT->getPointeeType())) |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2746 | return RetTy; |
| 2747 | |
| 2748 | ObjCInterfaceDecl* D = ME->getClassInfo().first; |
| 2749 | |
| 2750 | // At this point we know the return type of the message expression is id. |
| 2751 | // If we have an ObjCInterceDecl, we know this is a call to a class method |
| 2752 | // whose type we can resolve. In such cases, promote the return type to |
| 2753 | // Class*. |
| 2754 | return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D)); |
| 2755 | } |
| 2756 | |
| 2757 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2758 | void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2759 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2760 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2761 | Expr* Ex, |
| 2762 | Expr* Receiver, |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2763 | const RetainSummary& Summ, |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 2764 | ExprIterator arg_beg, ExprIterator arg_end, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2765 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2766 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2767 | // Get the state. |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2768 | const GRState *state = Builder.GetState(Pred); |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2769 | |
| 2770 | // Evaluate the effect of the arguments. |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2771 | RefVal::Kind hasErr = (RefVal::Kind) 0; |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2772 | unsigned idx = 0; |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 2773 | Expr* ErrorExpr = NULL; |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2774 | SymbolRef ErrorSym = 0; |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 2775 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2776 | for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) { |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2777 | SVal V = state->getSValAsScalarOrLoc(*I); |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2778 | SymbolRef Sym = V.getAsLocSymbol(); |
Ted Kremenek | 3f4d5ab | 2009-03-04 00:13:50 +0000 | [diff] [blame] | 2779 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 2780 | if (Sym) |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2781 | if (RefBindings::data_type* T = state->get<RefBindings>(Sym)) { |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2782 | state = Update(state, Sym, *T, Summ.getArg(idx), hasErr); |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2783 | if (hasErr) { |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 2784 | ErrorExpr = *I; |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2785 | ErrorSym = Sym; |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 2786 | break; |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2787 | } |
| 2788 | continue; |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2789 | } |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 2790 | |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 2791 | if (isa<Loc>(V)) { |
| 2792 | if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) { |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2793 | if (Summ.getArg(idx) == DoNothingByRef) |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 2794 | continue; |
| 2795 | |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2796 | // Invalidate the value of the variable passed by reference. |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 2797 | |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 2798 | // FIXME: We can have collisions on the conjured symbol if the |
| 2799 | // expression *I also creates conjured symbols. We probably want |
| 2800 | // to identify conjured symbols by an expression pair: the enclosing |
| 2801 | // expression (the context) and the expression itself. This should |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 2802 | // disambiguate conjured symbols. |
Zhongxing Xu | a03f157 | 2009-06-29 06:43:40 +0000 | [diff] [blame] | 2803 | unsigned Count = Builder.getCurrentBlockCount(); |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 2804 | StoreManager& StoreMgr = Eng.getStateManager().getStoreManager(); |
Ted Kremenek | 109bf47 | 2009-05-11 22:55:17 +0000 | [diff] [blame] | 2805 | |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 2806 | const MemRegion *R = MR->getRegion(); |
| 2807 | // Are we dealing with an ElementRegion? If the element type is |
| 2808 | // a basic integer type (e.g., char, int) and the underying region |
| 2809 | // is a variable region then strip off the ElementRegion. |
| 2810 | // FIXME: We really need to think about this for the general case |
| 2811 | // as sometimes we are reasoning about arrays and other times |
| 2812 | // about (char*), etc., is just a form of passing raw bytes. |
| 2813 | // e.g., void *p = alloca(); foo((char*)p); |
| 2814 | if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { |
| 2815 | // Checking for 'integral type' is probably too promiscuous, but |
| 2816 | // we'll leave it in for now until we have a systematic way of |
| 2817 | // handling all of these cases. Eventually we need to come up |
| 2818 | // with an interface to StoreManager so that this logic can be |
| 2819 | // approriately delegated to the respective StoreManagers while |
| 2820 | // still allowing us to do checker-specific logic (e.g., |
| 2821 | // invalidating reference counts), probably via callbacks. |
| 2822 | if (ER->getElementType()->isIntegralType()) { |
| 2823 | const MemRegion *superReg = ER->getSuperRegion(); |
| 2824 | if (isa<VarRegion>(superReg) || isa<FieldRegion>(superReg) || |
| 2825 | isa<ObjCIvarRegion>(superReg)) |
| 2826 | R = cast<TypedRegion>(superReg); |
Ted Kremenek | 4253051 | 2009-05-06 18:19:24 +0000 | [diff] [blame] | 2827 | } |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 2828 | // FIXME: What about layers of ElementRegions? |
| 2829 | } |
Zhongxing Xu | a03f157 | 2009-06-29 06:43:40 +0000 | [diff] [blame] | 2830 | |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 2831 | // Is the invalidated variable something that we were tracking? |
| 2832 | SymbolRef Sym = state->getSValAsScalarOrLoc(R).getAsLocSymbol(); |
| 2833 | |
| 2834 | // Remove any existing reference-count binding. |
| 2835 | if (Sym) |
| 2836 | state = state->remove<RefBindings>(Sym); |
| 2837 | |
| 2838 | state = StoreMgr.InvalidateRegion(state, R, *I, Count); |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 2839 | } |
| 2840 | else { |
| 2841 | // Nuke all other arguments passed by reference. |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 2842 | // FIXME: is this necessary or correct? unbind only removes the binding. |
| 2843 | // We should bind it to UnknownVal explicitly. Otherwise default value |
| 2844 | // may be loaded. |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2845 | state = state->unbindLoc(cast<Loc>(V)); |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 2846 | } |
Ted Kremenek | b887355 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 2847 | } |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2848 | else if (isa<nonloc::LocAsInteger>(V)) |
Zhongxing Xu | 313b6da | 2009-07-06 06:01:24 +0000 | [diff] [blame] | 2849 | // FIXME: is this necessary or correct? unbind only removes the binding. |
| 2850 | // We should bind it to UnknownVal explicitly. Otherwise default value |
| 2851 | // may be loaded. |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2852 | state = state->unbindLoc(cast<nonloc::LocAsInteger>(V).getLoc()); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2853 | } |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2854 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2855 | // Evaluate the effect on the message receiver. |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2856 | if (!ErrorExpr && Receiver) { |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2857 | SymbolRef Sym = state->getSValAsScalarOrLoc(Receiver).getAsLocSymbol(); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 2858 | if (Sym) { |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2859 | if (const RefVal* T = state->get<RefBindings>(Sym)) { |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2860 | state = Update(state, Sym, *T, Summ.getReceiverEffect(), hasErr); |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2861 | if (hasErr) { |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2862 | ErrorExpr = Receiver; |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2863 | ErrorSym = Sym; |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2864 | } |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 2865 | } |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2866 | } |
| 2867 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2868 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2869 | // Process any errors. |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2870 | if (hasErr) { |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2871 | ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state, |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2872 | hasErr, ErrorSym); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2873 | return; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2874 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2875 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 2876 | // Consult the summary for the return value. |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2877 | RetEffect RE = Summ.getRetEffect(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2878 | |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 2879 | if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) { |
| 2880 | assert(Receiver); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2881 | SVal V = state->getSValAsScalarOrLoc(Receiver); |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 2882 | bool found = false; |
| 2883 | if (SymbolRef Sym = V.getAsLocSymbol()) |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2884 | if (state->get<RefBindings>(Sym)) { |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 2885 | found = true; |
| 2886 | RE = Summaries.getObjAllocRetEffect(); |
| 2887 | } |
| 2888 | |
| 2889 | if (!found) |
| 2890 | RE = RetEffect::MakeNoRet(); |
| 2891 | } |
| 2892 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2893 | switch (RE.getKind()) { |
| 2894 | default: |
| 2895 | assert (false && "Unhandled RetEffect."); break; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2896 | |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2897 | case RetEffect::NoRet: { |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 2898 | // Make up a symbol for the return value (not reference counted). |
Ted Kremenek | 6c07bdb | 2009-06-26 00:05:51 +0000 | [diff] [blame] | 2899 | // FIXME: Most of this logic is not specific to the retain/release |
| 2900 | // checker. |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 2901 | |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 2902 | // FIXME: We eventually should handle structs and other compound types |
| 2903 | // that are returned by value. |
| 2904 | |
| 2905 | QualType T = Ex->getType(); |
| 2906 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 2907 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) { |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 2908 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 8d7f548 | 2009-04-09 22:22:44 +0000 | [diff] [blame] | 2909 | ValueManager &ValMgr = Eng.getValueManager(); |
| 2910 | SVal X = ValMgr.getConjuredSymbolVal(Ex, T, Count); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2911 | state = state->bindExpr(Ex, X, false); |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 2914 | break; |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 2915 | } |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 2916 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2917 | case RetEffect::Alias: { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2918 | unsigned idx = RE.getIndex(); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2919 | assert (arg_end >= arg_beg); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2920 | assert (idx < (unsigned) (arg_end - arg_beg)); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2921 | SVal V = state->getSValAsScalarOrLoc(*(arg_beg+idx)); |
| 2922 | state = state->bindExpr(Ex, V, false); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2923 | break; |
| 2924 | } |
| 2925 | |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2926 | case RetEffect::ReceiverAlias: { |
| 2927 | assert (Receiver); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2928 | SVal V = state->getSValAsScalarOrLoc(Receiver); |
| 2929 | state = state->bindExpr(Ex, V, false); |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2930 | break; |
| 2931 | } |
| 2932 | |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 2933 | case RetEffect::OwnedAllocatedSymbol: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2934 | case RetEffect::OwnedSymbol: { |
| 2935 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 044b6f0 | 2009-04-09 16:13:17 +0000 | [diff] [blame] | 2936 | ValueManager &ValMgr = Eng.getValueManager(); |
| 2937 | SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count); |
| 2938 | QualType RetT = GetReturnType(Ex, ValMgr.getContext()); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2939 | state = state->set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(), |
Ted Kremenek | 044b6f0 | 2009-04-09 16:13:17 +0000 | [diff] [blame] | 2940 | RetT)); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2941 | state = state->bindExpr(Ex, ValMgr.makeLoc(Sym), false); |
Ted Kremenek | 25d01ba | 2009-03-09 22:46:49 +0000 | [diff] [blame] | 2942 | |
| 2943 | // FIXME: Add a flag to the checker where allocations are assumed to |
| 2944 | // *not fail. |
| 2945 | #if 0 |
Ted Kremenek | b2bf7cd | 2009-01-28 22:27:59 +0000 | [diff] [blame] | 2946 | if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) { |
| 2947 | bool isFeasible; |
| 2948 | state = state.Assume(loc::SymbolVal(Sym), true, isFeasible); |
| 2949 | assert(isFeasible && "Cannot assume fresh symbol is non-null."); |
| 2950 | } |
Ted Kremenek | 25d01ba | 2009-03-09 22:46:49 +0000 | [diff] [blame] | 2951 | #endif |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 2952 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2953 | break; |
| 2954 | } |
Ted Kremenek | e798e7c | 2009-04-27 19:14:45 +0000 | [diff] [blame] | 2955 | |
| 2956 | case RetEffect::GCNotOwnedSymbol: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2957 | case RetEffect::NotOwnedSymbol: { |
| 2958 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 044b6f0 | 2009-04-09 16:13:17 +0000 | [diff] [blame] | 2959 | ValueManager &ValMgr = Eng.getValueManager(); |
| 2960 | SymbolRef Sym = ValMgr.getConjuredSymbol(Ex, Count); |
| 2961 | QualType RetT = GetReturnType(Ex, ValMgr.getContext()); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 2962 | state = state->set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(), |
Ted Kremenek | 044b6f0 | 2009-04-09 16:13:17 +0000 | [diff] [blame] | 2963 | RetT)); |
Zhongxing Xu | d91ee27 | 2009-06-23 09:02:15 +0000 | [diff] [blame] | 2964 | state = state->bindExpr(Ex, ValMgr.makeLoc(Sym), false); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2965 | break; |
| 2966 | } |
| 2967 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2968 | |
Ted Kremenek | f5b34b1 | 2009-02-18 02:00:25 +0000 | [diff] [blame] | 2969 | // Generate a sink node if we are at the end of a path. |
| 2970 | GRExprEngine::NodeTy *NewNode = |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2971 | Summ.isEndPath() ? Builder.MakeSinkNode(Dst, Ex, Pred, state) |
| 2972 | : Builder.MakeNode(Dst, Ex, Pred, state); |
Ted Kremenek | f5b34b1 | 2009-02-18 02:00:25 +0000 | [diff] [blame] | 2973 | |
| 2974 | // Annotate the edge with summary we used. |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2975 | if (NewNode) SummaryLog[NewNode] = &Summ; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2979 | void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2980 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2981 | GRStmtNodeBuilder<GRState>& Builder, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2982 | CallExpr* CE, SVal L, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2983 | ExplodedNode<GRState>* Pred) { |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 2984 | const FunctionDecl* FD = L.getAsFunctionDecl(); |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2985 | RetainSummary* Summ = !FD ? Summaries.getDefaultSummary() |
Zhongxing Xu | 369f447 | 2009-04-20 05:24:46 +0000 | [diff] [blame] | 2986 | : Summaries.getSummary(const_cast<FunctionDecl*>(FD)); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2987 | |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2988 | assert(Summ); |
| 2989 | EvalSummary(Dst, Eng, Builder, CE, 0, *Summ, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2990 | CE->arg_begin(), CE->arg_end(), Pred); |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 2991 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2992 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2993 | void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 2994 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2995 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 2996 | ObjCMessageExpr* ME, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2997 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 2998 | RetainSummary* Summ = 0; |
Ted Kremenek | 9040c65 | 2008-05-01 21:31:50 +0000 | [diff] [blame] | 2999 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3000 | if (Expr* Receiver = ME->getReceiver()) { |
| 3001 | // We need the type-information of the tracked receiver object |
| 3002 | // Retrieve it from the state. |
Ted Kremenek | 70b6a83 | 2009-05-13 18:16:01 +0000 | [diff] [blame] | 3003 | const ObjCInterfaceDecl* ID = 0; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3004 | |
| 3005 | // FIXME: Wouldn't it be great if this code could be reduced? It's just |
| 3006 | // a chain of lookups. |
Ted Kremenek | 8711c03 | 2009-04-29 05:04:30 +0000 | [diff] [blame] | 3007 | // FIXME: Is this really working as expected? There are cases where |
| 3008 | // we just use the 'ID' from the message expression. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 3009 | const GRState* St = Builder.GetState(Pred); |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 3010 | SVal V = St->getSValAsScalarOrLoc(Receiver); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3011 | |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 3012 | SymbolRef Sym = V.getAsLocSymbol(); |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 3013 | if (Sym) { |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 3014 | if (const RefVal* T = St->get<RefBindings>(Sym)) { |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 3015 | QualType Ty = T->getType(); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3016 | |
| 3017 | if (const PointerType* PT = Ty->getAsPointerType()) { |
| 3018 | QualType PointeeTy = PT->getPointeeType(); |
| 3019 | |
| 3020 | if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy)) |
| 3021 | ID = IT->getDecl(); |
| 3022 | } |
| 3023 | } |
| 3024 | } |
Ted Kremenek | 70b6a83 | 2009-05-13 18:16:01 +0000 | [diff] [blame] | 3025 | |
| 3026 | // FIXME: this is a hack. This may or may not be the actual method |
| 3027 | // that is called. |
| 3028 | if (!ID) { |
| 3029 | if (const PointerType *PT = Receiver->getType()->getAsPointerType()) |
| 3030 | if (const ObjCInterfaceType *p = |
| 3031 | PT->getPointeeType()->getAsObjCInterfaceType()) |
| 3032 | ID = p->getDecl(); |
| 3033 | } |
| 3034 | |
Ted Kremenek | ce8a41d | 2009-04-29 17:09:14 +0000 | [diff] [blame] | 3035 | // FIXME: The receiver could be a reference to a class, meaning that |
| 3036 | // we should use the class method. |
| 3037 | Summ = Summaries.getInstanceMethodSummary(ME, ID); |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 3038 | |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 3039 | // Special-case: are we sending a mesage to "self"? |
| 3040 | // This is a hack. When we have full-IP this should be removed. |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 3041 | if (isa<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl())) { |
| 3042 | if (Expr* Receiver = ME->getReceiver()) { |
Ted Kremenek | 23ec48c | 2009-06-18 23:58:37 +0000 | [diff] [blame] | 3043 | SVal X = St->getSValAsScalarOrLoc(Receiver); |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 3044 | if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X)) |
Ted Kremenek | 25e751a | 2009-06-23 21:37:46 +0000 | [diff] [blame] | 3045 | if (L->getRegion() == St->getSelfRegion()) { |
Ted Kremenek | 885c27b | 2009-05-04 05:31:22 +0000 | [diff] [blame] | 3046 | // Update the summary to make the default argument effect |
| 3047 | // 'StopTracking'. |
| 3048 | Summ = Summaries.copySummary(Summ); |
| 3049 | Summ->setDefaultArgEffect(StopTracking); |
| 3050 | } |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 3051 | } |
| 3052 | } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3053 | } |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 3054 | else |
Ted Kremenek | f9df136 | 2009-04-23 21:25:57 +0000 | [diff] [blame] | 3055 | Summ = Summaries.getClassMethodSummary(ME); |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 3056 | |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 3057 | if (!Summ) |
| 3058 | Summ = Summaries.getDefaultSummary(); |
Ted Kremenek | de4d533 | 2009-04-24 17:50:11 +0000 | [diff] [blame] | 3059 | |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 3060 | EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), *Summ, |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 3061 | ME->arg_begin(), ME->arg_end(), Pred); |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 3062 | } |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 3063 | |
| 3064 | namespace { |
| 3065 | class VISIBILITY_HIDDEN StopTrackingCallback : public SymbolVisitor { |
Ted Kremenek | 3a77203 | 2009-06-18 00:49:02 +0000 | [diff] [blame] | 3066 | const GRState *state; |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 3067 | public: |
Ted Kremenek | 3a77203 | 2009-06-18 00:49:02 +0000 | [diff] [blame] | 3068 | StopTrackingCallback(const GRState *st) : state(st) {} |
| 3069 | const GRState *getState() const { return state; } |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 3070 | |
| 3071 | bool VisitSymbol(SymbolRef sym) { |
Ted Kremenek | 3a77203 | 2009-06-18 00:49:02 +0000 | [diff] [blame] | 3072 | state = state->remove<RefBindings>(sym); |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 3073 | return true; |
| 3074 | } |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 3075 | }; |
| 3076 | } // end anonymous namespace |
| 3077 | |
| 3078 | |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 3079 | void CFRefCount::EvalBind(GRStmtNodeBuilderRef& B, SVal location, SVal val) { |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 3080 | // Are we storing to something that causes the value to "escape"? |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 3081 | bool escapes = false; |
| 3082 | |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 3083 | // A value escapes in three possible cases (this may change): |
| 3084 | // |
| 3085 | // (1) we are binding to something that is not a memory region. |
| 3086 | // (2) we are binding to a memregion that does not have stack storage |
| 3087 | // (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] | 3088 | // does not understand. |
Ted Kremenek | 3a77203 | 2009-06-18 00:49:02 +0000 | [diff] [blame] | 3089 | const GRState *state = B.getState(); |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 3090 | |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 3091 | if (!isa<loc::MemRegionVal>(location)) |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 3092 | escapes = true; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 3093 | else { |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 3094 | const MemRegion* R = cast<loc::MemRegionVal>(location).getRegion(); |
Ted Kremenek | ea20cd7 | 2009-06-23 18:05:21 +0000 | [diff] [blame] | 3095 | escapes = !R->hasStackStorage(); |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 3096 | |
| 3097 | if (!escapes) { |
| 3098 | // To test (3), generate a new state with the binding removed. If it is |
| 3099 | // the same state, then it escapes (since the store cannot represent |
| 3100 | // the binding). |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3101 | escapes = (state == (state->bindLoc(cast<Loc>(location), UnknownVal()))); |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 3102 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 3103 | } |
Ted Kremenek | 41573eb | 2009-02-14 01:43:44 +0000 | [diff] [blame] | 3104 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 3105 | // If our store can represent the binding and we aren't storing to something |
| 3106 | // that doesn't have local storage then just return and have the simulation |
| 3107 | // state continue as is. |
| 3108 | if (!escapes) |
| 3109 | return; |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 3110 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 3111 | // Otherwise, find all symbols referenced by 'val' that we are tracking |
| 3112 | // and stop tracking them. |
Ted Kremenek | 3a77203 | 2009-06-18 00:49:02 +0000 | [diff] [blame] | 3113 | B.MakeNode(state->scanReachableSymbols<StopTrackingCallback>(val).getState()); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3116 | // Return statements. |
| 3117 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 3118 | void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3119 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 3120 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3121 | ReturnStmt* S, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 3122 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3123 | |
| 3124 | Expr* RetE = S->getRetValue(); |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 3125 | if (!RetE) |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3126 | return; |
| 3127 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3128 | const GRState *state = Builder.GetState(Pred); |
| 3129 | SymbolRef Sym = state->getSValAsScalarOrLoc(RetE).getAsLocSymbol(); |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 3130 | |
Ted Kremenek | e0e4ebf | 2009-03-26 03:35:11 +0000 | [diff] [blame] | 3131 | if (!Sym) |
Ted Kremenek | 94c9698 | 2009-03-03 22:06:47 +0000 | [diff] [blame] | 3132 | return; |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3133 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3134 | // Get the reference count binding (if any). |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3135 | const RefVal* T = state->get<RefBindings>(Sym); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3136 | |
| 3137 | if (!T) |
| 3138 | return; |
| 3139 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 3140 | // Change the reference count. |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 3141 | RefVal X = *T; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3142 | |
Ted Kremenek | 78a35a3 | 2009-05-12 20:06:54 +0000 | [diff] [blame] | 3143 | switch (X.getKind()) { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3144 | case RefVal::Owned: { |
| 3145 | unsigned cnt = X.getCount(); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 3146 | assert (cnt > 0); |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 3147 | X.setCount(cnt - 1); |
| 3148 | X = X ^ RefVal::ReturnedOwned; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3149 | break; |
| 3150 | } |
| 3151 | |
| 3152 | case RefVal::NotOwned: { |
| 3153 | unsigned cnt = X.getCount(); |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 3154 | if (cnt) { |
| 3155 | X.setCount(cnt - 1); |
| 3156 | X = X ^ RefVal::ReturnedOwned; |
| 3157 | } |
| 3158 | else { |
| 3159 | X = X ^ RefVal::ReturnedNotOwned; |
| 3160 | } |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3161 | break; |
| 3162 | } |
| 3163 | |
| 3164 | default: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3165 | return; |
| 3166 | } |
| 3167 | |
| 3168 | // Update the binding. |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3169 | state = state->set<RefBindings>(Sym, X); |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 3170 | Pred = Builder.MakeNode(Dst, S, Pred, state); |
| 3171 | |
Ted Kremenek | 9f246b6 | 2009-04-30 05:51:50 +0000 | [diff] [blame] | 3172 | // Did we cache out? |
| 3173 | if (!Pred) |
| 3174 | return; |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 3175 | |
| 3176 | // Update the autorelease counts. |
| 3177 | static unsigned autoreleasetag = 0; |
| 3178 | GenericNodeBuilder Bd(Builder, S, &autoreleasetag); |
| 3179 | bool stop = false; |
| 3180 | llvm::tie(Pred, state) = HandleAutoreleaseCounts(state , Bd, Pred, Eng, Sym, |
| 3181 | X, stop); |
| 3182 | |
| 3183 | // Did we cache out? |
| 3184 | if (!Pred || stop) |
| 3185 | return; |
| 3186 | |
| 3187 | // Get the updated binding. |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3188 | T = state->get<RefBindings>(Sym); |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 3189 | assert(T); |
| 3190 | X = *T; |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3191 | |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 3192 | // Any leaks or other errors? |
| 3193 | if (X.isReturnedOwned() && X.getCount() == 0) { |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 3194 | const Decl *CD = &Eng.getStateManager().getCodeDecl(); |
Ted Kremenek | a883355 | 2009-04-29 23:03:22 +0000 | [diff] [blame] | 3195 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) { |
Ted Kremenek | 7faca82 | 2009-05-04 04:57:00 +0000 | [diff] [blame] | 3196 | const RetainSummary &Summ = *Summaries.getMethodSummary(MD); |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 3197 | RetEffect RE = Summ.getRetEffect(); |
| 3198 | bool hasError = false; |
| 3199 | |
Ted Kremenek | fae664a | 2009-05-16 01:38:01 +0000 | [diff] [blame] | 3200 | if (RE.getKind() != RetEffect::NoRet) { |
| 3201 | if (isGCEnabled() && RE.getObjKind() == RetEffect::ObjC) { |
| 3202 | // Things are more complicated with garbage collection. If the |
| 3203 | // returned object is suppose to be an Objective-C object, we have |
| 3204 | // a leak (as the caller expects a GC'ed object) because no |
| 3205 | // method should return ownership unless it returns a CF object. |
| 3206 | X = X ^ RefVal::ErrorGCLeakReturned; |
| 3207 | |
| 3208 | // Keep this false until this is properly tested. |
| 3209 | hasError = true; |
| 3210 | } |
| 3211 | else if (!RE.isOwned()) { |
| 3212 | // Either we are using GC and the returned object is a CF type |
| 3213 | // or we aren't using GC. In either case, we expect that the |
| 3214 | // enclosing method is expected to return ownership. |
| 3215 | hasError = true; |
| 3216 | X = X ^ RefVal::ErrorLeakReturned; |
| 3217 | } |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | if (hasError) { |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 3221 | // Generate an error node. |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 3222 | static int ReturnOwnLeakTag = 0; |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3223 | state = state->set<RefBindings>(Sym, X); |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 3224 | ExplodedNode<GRState> *N = |
| 3225 | Builder.generateNode(PostStmt(S, &ReturnOwnLeakTag), state, Pred); |
| 3226 | if (N) { |
| 3227 | CFRefReport *report = |
Ted Kremenek | 9f246b6 | 2009-04-30 05:51:50 +0000 | [diff] [blame] | 3228 | new CFRefLeakReport(*static_cast<CFRefBug*>(leakAtReturn), *this, |
| 3229 | N, Sym, Eng); |
| 3230 | BR->EmitReport(report); |
| 3231 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 3232 | } |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 3233 | } |
| 3234 | } |
| 3235 | else if (X.isReturnedNotOwned()) { |
| 3236 | const Decl *CD = &Eng.getStateManager().getCodeDecl(); |
| 3237 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) { |
| 3238 | const RetainSummary &Summ = *Summaries.getMethodSummary(MD); |
| 3239 | if (Summ.getRetEffect().isOwned()) { |
| 3240 | // Trying to return a not owned object to a caller expecting an |
| 3241 | // owned object. |
| 3242 | |
| 3243 | static int ReturnNotOwnedForOwnedTag = 0; |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3244 | state = state->set<RefBindings>(Sym, X ^ RefVal::ErrorReturnedNotOwned); |
Ted Kremenek | e8720ce | 2009-05-10 06:25:57 +0000 | [diff] [blame] | 3245 | if (ExplodedNode<GRState> *N = |
| 3246 | Builder.generateNode(PostStmt(S, &ReturnNotOwnedForOwnedTag), |
| 3247 | state, Pred)) { |
| 3248 | CFRefReport *report = |
| 3249 | new CFRefReport(*static_cast<CFRefBug*>(returnNotOwnedForOwned), |
| 3250 | *this, N, Sym); |
| 3251 | BR->EmitReport(report); |
| 3252 | } |
| 3253 | } |
Ted Kremenek | c887d13 | 2009-04-29 18:50:19 +0000 | [diff] [blame] | 3254 | } |
| 3255 | } |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3258 | // Assumptions. |
| 3259 | |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 3260 | const GRState* CFRefCount::EvalAssume(const GRState *state, |
| 3261 | SVal Cond, bool Assumption) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3262 | |
| 3263 | // FIXME: We may add to the interface of EvalAssume the list of symbols |
| 3264 | // whose assumptions have changed. For now we just iterate through the |
| 3265 | // bindings and check if any of the tracked symbols are NULL. This isn't |
| 3266 | // too bad since the number of symbols we will track in practice are |
| 3267 | // probably small and EvalAssume is only called at branches and a few |
| 3268 | // other places. |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3269 | RefBindings B = state->get<RefBindings>(); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3270 | |
| 3271 | if (B.isEmpty()) |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3272 | return state; |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3273 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3274 | bool changed = false; |
| 3275 | RefBindings::Factory& RefBFactory = state->get_context<RefBindings>(); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3276 | |
| 3277 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3278 | // Check if the symbol is null (or equal to any constant). |
| 3279 | // If this is the case, stop tracking the symbol. |
Ted Kremenek | a591bc0 | 2009-06-18 22:57:13 +0000 | [diff] [blame] | 3280 | if (state->getSymVal(I.getKey())) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3281 | changed = true; |
| 3282 | B = RefBFactory.Remove(B, I.getKey()); |
| 3283 | } |
| 3284 | } |
| 3285 | |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 3286 | if (changed) |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3287 | state = state->set<RefBindings>(B); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3288 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 3289 | return state; |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 3290 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 3291 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3292 | const GRState * CFRefCount::Update(const GRState * state, SymbolRef sym, |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 3293 | RefVal V, ArgEffect E, |
| 3294 | RefVal::Kind& hasErr) { |
Ted Kremenek | 1c512f5 | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 3295 | |
| 3296 | // In GC mode [... release] and [... retain] do nothing. |
| 3297 | switch (E) { |
| 3298 | default: break; |
| 3299 | case IncRefMsg: E = isGCEnabled() ? DoNothing : IncRef; break; |
| 3300 | case DecRefMsg: E = isGCEnabled() ? DoNothing : DecRef; break; |
Ted Kremenek | 2701900 | 2009-02-18 21:57:45 +0000 | [diff] [blame] | 3301 | case MakeCollectable: E = isGCEnabled() ? DecRef : DoNothing; break; |
Ted Kremenek | f9a8e2e | 2009-02-23 17:45:03 +0000 | [diff] [blame] | 3302 | case NewAutoreleasePool: E = isGCEnabled() ? DoNothing : |
| 3303 | NewAutoreleasePool; break; |
Ted Kremenek | 1c512f5 | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 3304 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 3305 | |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3306 | // Handle all use-after-releases. |
| 3307 | if (!isGCEnabled() && V.getKind() == RefVal::Released) { |
| 3308 | V = V ^ RefVal::ErrorUseAfterRelease; |
| 3309 | hasErr = V.getKind(); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3310 | return state->set<RefBindings>(sym, V); |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3313 | switch (E) { |
| 3314 | default: |
| 3315 | assert (false && "Unhandled CFRef transition."); |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3316 | |
| 3317 | case Dealloc: |
| 3318 | // Any use of -dealloc in GC is *bad*. |
| 3319 | if (isGCEnabled()) { |
| 3320 | V = V ^ RefVal::ErrorDeallocGC; |
| 3321 | hasErr = V.getKind(); |
| 3322 | break; |
| 3323 | } |
| 3324 | |
| 3325 | switch (V.getKind()) { |
| 3326 | default: |
| 3327 | assert(false && "Invalid case."); |
| 3328 | case RefVal::Owned: |
| 3329 | // The object immediately transitions to the released state. |
| 3330 | V = V ^ RefVal::Released; |
| 3331 | V.clearCounts(); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3332 | return state->set<RefBindings>(sym, V); |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3333 | case RefVal::NotOwned: |
| 3334 | V = V ^ RefVal::ErrorDeallocNotOwned; |
| 3335 | hasErr = V.getKind(); |
| 3336 | break; |
| 3337 | } |
| 3338 | break; |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 3339 | |
Ted Kremenek | 3579073 | 2009-02-25 23:11:49 +0000 | [diff] [blame] | 3340 | case NewAutoreleasePool: |
| 3341 | assert(!isGCEnabled()); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3342 | return state->add<AutoreleaseStack>(sym); |
Ted Kremenek | 3579073 | 2009-02-25 23:11:49 +0000 | [diff] [blame] | 3343 | |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 3344 | case MayEscape: |
| 3345 | if (V.getKind() == RefVal::Owned) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3346 | V = V ^ RefVal::NotOwned; |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 3347 | break; |
| 3348 | } |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3349 | |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 3350 | // Fall-through. |
Ted Kremenek | 6c4becb | 2009-02-25 02:54:57 +0000 | [diff] [blame] | 3351 | |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 3352 | case DoNothingByRef: |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3353 | case DoNothing: |
Ted Kremenek | 4d3957d | 2009-02-24 19:15:11 +0000 | [diff] [blame] | 3354 | return state; |
Ted Kremenek | e19f449 | 2008-06-30 16:57:41 +0000 | [diff] [blame] | 3355 | |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 3356 | case Autorelease: |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3357 | if (isGCEnabled()) |
| 3358 | return state; |
Ted Kremenek | 7037ab8 | 2009-03-20 17:34:15 +0000 | [diff] [blame] | 3359 | |
| 3360 | // Update the autorelease counts. |
| 3361 | state = SendAutorelease(state, ARCountFactory, sym); |
Ted Kremenek | f21332e | 2009-05-08 20:01:42 +0000 | [diff] [blame] | 3362 | V = V.autorelease(); |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 3363 | break; |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3364 | |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 3365 | case StopTracking: |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3366 | return state->remove<RefBindings>(sym); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3367 | |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3368 | case IncRef: |
| 3369 | switch (V.getKind()) { |
| 3370 | default: |
| 3371 | assert(false); |
| 3372 | |
| 3373 | case RefVal::Owned: |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3374 | case RefVal::NotOwned: |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3375 | V = V + 1; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3376 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3377 | case RefVal::Released: |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3378 | // Non-GC cases are handled above. |
| 3379 | assert(isGCEnabled()); |
| 3380 | V = (V ^ RefVal::Owned) + 1; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3381 | break; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3382 | } |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 3383 | break; |
| 3384 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3385 | case SelfOwn: |
| 3386 | V = V ^ RefVal::NotOwned; |
Ted Kremenek | 1c512f5 | 2009-02-18 18:54:33 +0000 | [diff] [blame] | 3387 | // Fall-through. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3388 | case DecRef: |
| 3389 | switch (V.getKind()) { |
| 3390 | default: |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3391 | // case 'RefVal::Released' handled above. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3392 | assert (false); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3393 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3394 | case RefVal::Owned: |
Ted Kremenek | bb8c5aa | 2009-02-18 22:57:22 +0000 | [diff] [blame] | 3395 | assert(V.getCount() > 0); |
| 3396 | if (V.getCount() == 1) V = V ^ RefVal::Released; |
| 3397 | V = V - 1; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3398 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3399 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3400 | case RefVal::NotOwned: |
| 3401 | if (V.getCount() > 0) |
| 3402 | V = V - 1; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 3403 | else { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3404 | V = V ^ RefVal::ErrorReleaseNotOwned; |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 3405 | hasErr = V.getKind(); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3406 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3407 | break; |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3408 | |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3409 | case RefVal::Released: |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3410 | // Non-GC cases are handled above. |
| 3411 | assert(isGCEnabled()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 3412 | V = V ^ RefVal::ErrorUseAfterRelease; |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 3413 | hasErr = V.getKind(); |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3414 | break; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 3415 | } |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 3416 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 3417 | } |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3418 | return state->set<RefBindings>(sym, V); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 3421 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3422 | // Handle dead symbols and end-of-path. |
| 3423 | //===----------------------------------------------------------------------===// |
| 3424 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3425 | std::pair<ExplodedNode<GRState>*, const GRState *> |
| 3426 | CFRefCount::HandleAutoreleaseCounts(const GRState * state, GenericNodeBuilder Bd, |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3427 | ExplodedNode<GRState>* Pred, |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3428 | GRExprEngine &Eng, |
| 3429 | SymbolRef Sym, RefVal V, bool &stop) { |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3430 | |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3431 | unsigned ACnt = V.getAutoreleaseCount(); |
| 3432 | stop = false; |
| 3433 | |
| 3434 | // No autorelease counts? Nothing to be done. |
| 3435 | if (!ACnt) |
| 3436 | return std::make_pair(Pred, state); |
| 3437 | |
| 3438 | assert(!isGCEnabled() && "Autorelease counts in GC mode?"); |
| 3439 | unsigned Cnt = V.getCount(); |
| 3440 | |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 3441 | // FIXME: Handle sending 'autorelease' to already released object. |
| 3442 | |
| 3443 | if (V.getKind() == RefVal::ReturnedOwned) |
| 3444 | ++Cnt; |
| 3445 | |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3446 | if (ACnt <= Cnt) { |
Ted Kremenek | 80c2418 | 2009-05-09 00:44:07 +0000 | [diff] [blame] | 3447 | if (ACnt == Cnt) { |
| 3448 | V.clearCounts(); |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 3449 | if (V.getKind() == RefVal::ReturnedOwned) |
| 3450 | V = V ^ RefVal::ReturnedNotOwned; |
| 3451 | else |
| 3452 | V = V ^ RefVal::NotOwned; |
Ted Kremenek | 80c2418 | 2009-05-09 00:44:07 +0000 | [diff] [blame] | 3453 | } |
Ted Kremenek | 95d3b90 | 2009-05-11 15:26:06 +0000 | [diff] [blame] | 3454 | else { |
Ted Kremenek | 80c2418 | 2009-05-09 00:44:07 +0000 | [diff] [blame] | 3455 | V.setCount(Cnt - ACnt); |
| 3456 | V.setAutoreleaseCount(0); |
| 3457 | } |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3458 | state = state->set<RefBindings>(Sym, V); |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3459 | ExplodedNode<GRState> *N = Bd.MakeNode(state, Pred); |
| 3460 | stop = (N == 0); |
| 3461 | return std::make_pair(N, state); |
| 3462 | } |
| 3463 | |
| 3464 | // Woah! More autorelease counts then retain counts left. |
| 3465 | // Emit hard error. |
| 3466 | stop = true; |
| 3467 | V = V ^ RefVal::ErrorOverAutorelease; |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3468 | state = state->set<RefBindings>(Sym, V); |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3469 | |
| 3470 | if (ExplodedNode<GRState> *N = Bd.MakeNode(state, Pred)) { |
Ted Kremenek | 80c2418 | 2009-05-09 00:44:07 +0000 | [diff] [blame] | 3471 | N->markAsSink(); |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 3472 | |
| 3473 | std::string sbuf; |
| 3474 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | daec145 | 2009-05-15 06:02:08 +0000 | [diff] [blame] | 3475 | os << "Object over-autoreleased: object was sent -autorelease"; |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 3476 | if (V.getAutoreleaseCount() > 1) |
| 3477 | os << V.getAutoreleaseCount() << " times"; |
| 3478 | os << " but the object has "; |
| 3479 | if (V.getCount() == 0) |
| 3480 | os << "zero (locally visible)"; |
| 3481 | else |
| 3482 | os << "+" << V.getCount(); |
| 3483 | os << " retain counts"; |
| 3484 | |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3485 | CFRefReport *report = |
| 3486 | new CFRefReport(*static_cast<CFRefBug*>(overAutorelease), |
Ted Kremenek | eaedfea | 2009-05-10 05:11:21 +0000 | [diff] [blame] | 3487 | *this, N, Sym, os.str().c_str()); |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3488 | BR->EmitReport(report); |
| 3489 | } |
| 3490 | |
| 3491 | return std::make_pair((ExplodedNode<GRState>*)0, state); |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3492 | } |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3493 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3494 | const GRState * |
| 3495 | CFRefCount::HandleSymbolDeath(const GRState * state, SymbolRef sid, RefVal V, |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3496 | llvm::SmallVectorImpl<SymbolRef> &Leaked) { |
| 3497 | |
| 3498 | bool hasLeak = V.isOwned() || |
| 3499 | ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0); |
| 3500 | |
| 3501 | if (!hasLeak) |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3502 | return state->remove<RefBindings>(sid); |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3503 | |
| 3504 | Leaked.push_back(sid); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3505 | return state->set<RefBindings>(sid, V ^ RefVal::ErrorLeak); |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | ExplodedNode<GRState>* |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3509 | CFRefCount::ProcessLeaks(const GRState * state, |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3510 | llvm::SmallVectorImpl<SymbolRef> &Leaked, |
| 3511 | GenericNodeBuilder &Builder, |
| 3512 | GRExprEngine& Eng, |
| 3513 | ExplodedNode<GRState> *Pred) { |
| 3514 | |
| 3515 | if (Leaked.empty()) |
| 3516 | return Pred; |
| 3517 | |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3518 | // Generate an intermediate node representing the leak point. |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 3519 | ExplodedNode<GRState> *N = Builder.MakeNode(state, Pred); |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3520 | |
| 3521 | if (N) { |
| 3522 | for (llvm::SmallVectorImpl<SymbolRef>::iterator |
| 3523 | I = Leaked.begin(), E = Leaked.end(); I != E; ++I) { |
| 3524 | |
| 3525 | CFRefBug *BT = static_cast<CFRefBug*>(Pred ? leakWithinFunction |
| 3526 | : leakAtReturn); |
| 3527 | assert(BT && "BugType not initialized."); |
| 3528 | CFRefLeakReport* report = new CFRefLeakReport(*BT, *this, N, *I, Eng); |
| 3529 | BR->EmitReport(report); |
| 3530 | } |
| 3531 | } |
| 3532 | |
| 3533 | return N; |
| 3534 | } |
| 3535 | |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3536 | void CFRefCount::EvalEndPath(GRExprEngine& Eng, |
| 3537 | GREndPathNodeBuilder<GRState>& Builder) { |
| 3538 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3539 | const GRState *state = Builder.getState(); |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3540 | GenericNodeBuilder Bd(Builder); |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3541 | RefBindings B = state->get<RefBindings>(); |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3542 | ExplodedNode<GRState> *Pred = 0; |
| 3543 | |
| 3544 | for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3545 | bool stop = false; |
| 3546 | llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng, |
| 3547 | (*I).first, |
| 3548 | (*I).second, stop); |
| 3549 | |
| 3550 | if (stop) |
| 3551 | return; |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3554 | B = state->get<RefBindings>(); |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3555 | llvm::SmallVector<SymbolRef, 10> Leaked; |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3556 | |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3557 | for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) |
| 3558 | state = HandleSymbolDeath(state, (*I).first, (*I).second, Leaked); |
| 3559 | |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3560 | ProcessLeaks(state, Leaked, Bd, Eng, Pred); |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3561 | } |
| 3562 | |
| 3563 | void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst, |
| 3564 | GRExprEngine& Eng, |
| 3565 | GRStmtNodeBuilder<GRState>& Builder, |
| 3566 | ExplodedNode<GRState>* Pred, |
| 3567 | Stmt* S, |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3568 | const GRState* state, |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3569 | SymbolReaper& SymReaper) { |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3570 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3571 | RefBindings B = state->get<RefBindings>(); |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3572 | |
| 3573 | // Update counts from autorelease pools |
| 3574 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
| 3575 | E = SymReaper.dead_end(); I != E; ++I) { |
| 3576 | SymbolRef Sym = *I; |
| 3577 | if (const RefVal* T = B.lookup(Sym)){ |
| 3578 | // Use the symbol as the tag. |
| 3579 | // FIXME: This might not be as unique as we would like. |
| 3580 | GenericNodeBuilder Bd(Builder, S, Sym); |
Ted Kremenek | 369de56 | 2009-05-09 00:10:05 +0000 | [diff] [blame] | 3581 | bool stop = false; |
| 3582 | llvm::tie(Pred, state) = HandleAutoreleaseCounts(state, Bd, Pred, Eng, |
| 3583 | Sym, *T, stop); |
| 3584 | if (stop) |
| 3585 | return; |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3586 | } |
| 3587 | } |
| 3588 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3589 | B = state->get<RefBindings>(); |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3590 | llvm::SmallVector<SymbolRef, 10> Leaked; |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3591 | |
| 3592 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3593 | E = SymReaper.dead_end(); I != E; ++I) { |
| 3594 | if (const RefVal* T = B.lookup(*I)) |
| 3595 | state = HandleSymbolDeath(state, *I, *T, Leaked); |
| 3596 | } |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3597 | |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3598 | static unsigned LeakPPTag = 0; |
Ted Kremenek | f04dced | 2009-05-08 23:32:51 +0000 | [diff] [blame] | 3599 | { |
| 3600 | GenericNodeBuilder Bd(Builder, S, &LeakPPTag); |
| 3601 | Pred = ProcessLeaks(state, Leaked, Bd, Eng, Pred); |
| 3602 | } |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3603 | |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3604 | // Did we cache out? |
| 3605 | if (!Pred) |
| 3606 | return; |
Ted Kremenek | 33b6f63 | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3607 | |
| 3608 | // Now generate a new node that nukes the old bindings. |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3609 | RefBindings::Factory& F = state->get_context<RefBindings>(); |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3610 | |
Ted Kremenek | 33b6f63 | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3611 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
Ted Kremenek | 9d9d3a6 | 2009-05-08 23:09:42 +0000 | [diff] [blame] | 3612 | E = SymReaper.dead_end(); I!=E; ++I) B = F.Remove(B, *I); |
| 3613 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 3614 | state = state->set<RefBindings>(B); |
Ted Kremenek | 33b6f63 | 2009-02-19 23:47:02 +0000 | [diff] [blame] | 3615 | Builder.MakeNode(Dst, S, Pred, state); |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
| 3618 | void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst, |
| 3619 | GRStmtNodeBuilder<GRState>& Builder, |
| 3620 | Expr* NodeExpr, Expr* ErrorExpr, |
| 3621 | ExplodedNode<GRState>* Pred, |
| 3622 | const GRState* St, |
| 3623 | RefVal::Kind hasErr, SymbolRef Sym) { |
| 3624 | Builder.BuildSinks = true; |
| 3625 | GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St); |
| 3626 | |
Ted Kremenek | 6b62ec9 | 2009-05-09 01:50:57 +0000 | [diff] [blame] | 3627 | if (!N) |
| 3628 | return; |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3629 | |
| 3630 | CFRefBug *BT = 0; |
| 3631 | |
Ted Kremenek | f95e9fc | 2009-03-17 19:42:23 +0000 | [diff] [blame] | 3632 | switch (hasErr) { |
| 3633 | default: |
| 3634 | assert(false && "Unhandled error."); |
| 3635 | return; |
| 3636 | case RefVal::ErrorUseAfterRelease: |
| 3637 | BT = static_cast<CFRefBug*>(useAfterRelease); |
| 3638 | break; |
| 3639 | case RefVal::ErrorReleaseNotOwned: |
| 3640 | BT = static_cast<CFRefBug*>(releaseNotOwned); |
| 3641 | break; |
| 3642 | case RefVal::ErrorDeallocGC: |
| 3643 | BT = static_cast<CFRefBug*>(deallocGC); |
| 3644 | break; |
| 3645 | case RefVal::ErrorDeallocNotOwned: |
| 3646 | BT = static_cast<CFRefBug*>(deallocNotOwned); |
| 3647 | break; |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 3650 | CFRefReport *report = new CFRefReport(*BT, *this, N, Sym); |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 3651 | report->addRange(ErrorExpr->getSourceRange()); |
| 3652 | BR->EmitReport(report); |
| 3653 | } |
| 3654 | |
| 3655 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 3656 | // Transfer function creation for external clients. |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 3657 | //===----------------------------------------------------------------------===// |
| 3658 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 3659 | GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled, |
| 3660 | const LangOptions& lopts) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 3661 | return new CFRefCount(Ctx, GCEnabled, lopts); |
Ted Kremenek | 3ea0b6a | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 3662 | } |