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