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