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