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