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