Chris Lattner | bda0b62 | 2008-03-15 23:59:48 +0000 | [diff] [blame] | 1 | // CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--// |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gabor Greif | 843e934 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines the methods for CFRefCount, which implements |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 11 | // a reference count checker for Core Foundation (Mac OS X). |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 15 | #include "GRSimpleVals.h" |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 16 | #include "clang/Basic/LangOptions.h" |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Ted Kremenek | 4dc41cc | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/PathDiagnostic.h" |
| 23 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/DenseMap.h" |
| 26 | #include "llvm/ADT/FoldingSet.h" |
| 27 | #include "llvm/ADT/ImmutableMap.h" |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/ImmutableList.h" |
Ted Kremenek | 900a2d7 | 2008-05-07 18:36:45 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 32 | #include <ostream> |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 33 | #include <stdarg.h> |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace clang; |
Ted Kremenek | 5c74d50 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | // Utility functions. |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Ted Kremenek | 900a2d7 | 2008-05-07 18:36:45 +0000 | [diff] [blame] | 41 | using llvm::CStrInCStrNoCase; |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | 5c74d50 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 43 | // The "fundamental rule" for naming conventions of methods: |
| 44 | // (url broken into two lines) |
| 45 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ |
| 46 | // MemoryMgmt/Tasks/MemoryManagementRules.html |
| 47 | // |
| 48 | // "You take ownership of an object if you create it using a method whose name |
| 49 | // begins with “alloc” or “new” or contains “copy” (for example, alloc, |
| 50 | // newObject, or mutableCopy), or if you send it a retain message. You are |
| 51 | // responsible for relinquishing ownership of objects you own using release |
| 52 | // or autorelease. Any other time you receive an object, you must |
| 53 | // not release it." |
| 54 | // |
| 55 | static bool followsFundamentalRule(const char* s) { |
Ted Kremenek | e1e91af | 2008-10-30 23:14:58 +0000 | [diff] [blame] | 56 | while (*s == '_') ++s; |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 57 | return CStrInCStrNoCase(s, "copy") |
| 58 | || CStrInCStrNoCase(s, "new") == s |
| 59 | || CStrInCStrNoCase(s, "alloc") == s; |
Ted Kremenek | 4c79e55 | 2008-11-05 16:54:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static bool followsReturnRule(const char* s) { |
| 63 | while (*s == '_') ++s; |
| 64 | return followsFundamentalRule(s) || CStrInCStrNoCase(s, "init") == s; |
| 65 | } |
Ted Kremenek | 5c74d50 | 2008-10-24 21:18:08 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 67 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 68 | // Selector creation functions. |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 69 | //===----------------------------------------------------------------------===// |
| 70 | |
Ted Kremenek | b83e02e | 2008-05-01 18:31:44 +0000 | [diff] [blame] | 71 | static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 72 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 73 | return Ctx.Selectors.getSelector(0, &II); |
| 74 | } |
| 75 | |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 76 | static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { |
| 77 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 78 | return Ctx.Selectors.getSelector(1, &II); |
| 79 | } |
| 80 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 81 | //===----------------------------------------------------------------------===// |
| 82 | // Type querying functions. |
| 83 | //===----------------------------------------------------------------------===// |
| 84 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 85 | static bool hasPrefix(const char* s, const char* prefix) { |
| 86 | if (!prefix) |
| 87 | return true; |
Ted Kremenek | 0fcbf8e | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 89 | char c = *s; |
| 90 | char cP = *prefix; |
Ted Kremenek | 0fcbf8e | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 92 | while (c != '\0' && cP != '\0') { |
| 93 | if (c != cP) break; |
| 94 | c = *(++s); |
| 95 | cP = *(++prefix); |
| 96 | } |
Ted Kremenek | 0fcbf8e | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 98 | return cP == '\0'; |
Ted Kremenek | 0fcbf8e | 2008-05-07 20:06:41 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 101 | static bool hasSuffix(const char* s, const char* suffix) { |
| 102 | const char* loc = strstr(s, suffix); |
| 103 | return loc && strcmp(suffix, loc) == 0; |
| 104 | } |
| 105 | |
| 106 | static bool isRefType(QualType RetTy, const char* prefix, |
| 107 | ASTContext* Ctx = 0, const char* name = 0) { |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 108 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 109 | if (TypedefType* TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) { |
| 110 | const char* TDName = TD->getDecl()->getIdentifier()->getName(); |
| 111 | return hasPrefix(TDName, prefix) && hasSuffix(TDName, "Ref"); |
| 112 | } |
| 113 | |
| 114 | if (!Ctx || !name) |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 115 | return false; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 116 | |
| 117 | // Is the type void*? |
| 118 | const PointerType* PT = RetTy->getAsPointerType(); |
| 119 | if (!(PT->getPointeeType().getUnqualifiedType() == Ctx->VoidTy)) |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 120 | return false; |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 121 | |
| 122 | // Does the name start with the prefix? |
| 123 | return hasPrefix(name, prefix); |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 126 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 127 | // Primitives used for constructing summaries for function/method calls. |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 128 | //===----------------------------------------------------------------------===// |
| 129 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 130 | namespace { |
| 131 | /// ArgEffect is used to summarize a function/method call's effect on a |
| 132 | /// particular argument. |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 133 | enum ArgEffect { IncRef, DecRef, DoNothing, DoNothingByRef, |
| 134 | StopTracking, MayEscape, SelfOwn, Autorelease }; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 135 | |
| 136 | /// ArgEffects summarizes the effects of a function/method call on all of |
| 137 | /// its arguments. |
| 138 | typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 139 | } |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 141 | namespace llvm { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 142 | template <> struct FoldingSetTrait<ArgEffects> { |
| 143 | static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) { |
| 144 | for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) { |
| 145 | ID.AddInteger(I->first); |
| 146 | ID.AddInteger((unsigned) I->second); |
| 147 | } |
| 148 | } |
| 149 | }; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 150 | } // end llvm namespace |
| 151 | |
| 152 | namespace { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 153 | |
| 154 | /// RetEffect is used to summarize a function/method call's behavior with |
| 155 | /// respect to its return value. |
| 156 | class VISIBILITY_HIDDEN RetEffect { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 157 | public: |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 158 | enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol, |
| 159 | NotOwnedSymbol, ReceiverAlias }; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 160 | |
| 161 | enum ObjKind { CF, ObjC, AnyObj }; |
| 162 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 163 | private: |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 164 | Kind K; |
| 165 | ObjKind O; |
| 166 | unsigned index; |
| 167 | |
| 168 | RetEffect(Kind k, unsigned idx = 0) : K(k), O(AnyObj), index(idx) {} |
| 169 | RetEffect(Kind k, ObjKind o) : K(k), O(o), index(0) {} |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 170 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 171 | public: |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 172 | Kind getKind() const { return K; } |
| 173 | |
| 174 | ObjKind getObjKind() const { return O; } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 175 | |
| 176 | unsigned getIndex() const { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 177 | assert(getKind() == Alias); |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 178 | return index; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 179 | } |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 181 | static RetEffect MakeAlias(unsigned Idx) { |
| 182 | return RetEffect(Alias, Idx); |
| 183 | } |
| 184 | static RetEffect MakeReceiverAlias() { |
| 185 | return RetEffect(ReceiverAlias); |
| 186 | } |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 187 | static RetEffect MakeOwned(ObjKind o, bool isAllocated = false) { |
| 188 | return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol, o); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 189 | } |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 190 | static RetEffect MakeNotOwned(ObjKind o) { |
| 191 | return RetEffect(NotOwnedSymbol, o); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 192 | } |
| 193 | static RetEffect MakeNoRet() { |
| 194 | return RetEffect(NoRet); |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 195 | } |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 196 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 197 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 198 | ID.AddInteger((unsigned)K); |
| 199 | ID.AddInteger((unsigned)O); |
| 200 | ID.AddInteger(index); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 201 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 202 | }; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 204 | |
| 205 | class VISIBILITY_HIDDEN RetainSummary : public llvm::FoldingSetNode { |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 206 | /// Args - an ordered vector of (index, ArgEffect) pairs, where index |
| 207 | /// specifies the argument (starting from 0). This can be sparsely |
| 208 | /// populated; arguments with no entry in Args use 'DefaultArgEffect'. |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 209 | ArgEffects* Args; |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 210 | |
| 211 | /// DefaultArgEffect - The default ArgEffect to apply to arguments that |
| 212 | /// do not have an entry in Args. |
| 213 | ArgEffect DefaultArgEffect; |
| 214 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 215 | /// Receiver - If this summary applies to an Objective-C message expression, |
| 216 | /// this is the effect applied to the state of the receiver. |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 217 | ArgEffect Receiver; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 218 | |
| 219 | /// Ret - The effect on the return value. Used to indicate if the |
| 220 | /// function/method call returns a new tracked symbol, returns an |
| 221 | /// alias of one of the arguments in the call, and so on. |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 222 | RetEffect Ret; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 224 | /// EndPath - Indicates that execution of this method/function should |
| 225 | /// terminate the simulation of a path. |
| 226 | bool EndPath; |
| 227 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 228 | public: |
| 229 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 230 | RetainSummary(ArgEffects* A, RetEffect R, ArgEffect defaultEff, |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 231 | ArgEffect ReceiverEff, bool endpath = false) |
| 232 | : Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R), |
| 233 | EndPath(endpath) {} |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 235 | /// getArg - Return the argument effect on the argument specified by |
| 236 | /// idx (starting from 0). |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 237 | ArgEffect getArg(unsigned idx) const { |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 239 | if (!Args) |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 240 | return DefaultArgEffect; |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 241 | |
| 242 | // If Args is present, it is likely to contain only 1 element. |
| 243 | // Just do a linear search. Do it from the back because functions with |
| 244 | // large numbers of arguments will be tail heavy with respect to which |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 245 | // argument they actually modify with respect to the reference count. |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 246 | for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend(); |
| 247 | I!=E; ++I) { |
| 248 | |
| 249 | if (idx > I->first) |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 250 | return DefaultArgEffect; |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 251 | |
| 252 | if (idx == I->first) |
| 253 | return I->second; |
| 254 | } |
| 255 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 256 | return DefaultArgEffect; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 259 | /// getRetEffect - Returns the effect on the return value of the call. |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 260 | RetEffect getRetEffect() const { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 261 | return Ret; |
| 262 | } |
| 263 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 264 | /// isEndPath - Returns true if executing the given method/function should |
| 265 | /// terminate the path. |
| 266 | bool isEndPath() const { return EndPath; } |
| 267 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 268 | /// getReceiverEffect - Returns the effect on the receiver of the call. |
| 269 | /// This is only meaningful if the summary applies to an ObjCMessageExpr*. |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 270 | ArgEffect getReceiverEffect() const { |
| 271 | return Receiver; |
| 272 | } |
| 273 | |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 274 | typedef ArgEffects::const_iterator ExprIterator; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 276 | ExprIterator begin_args() const { return Args->begin(); } |
| 277 | ExprIterator end_args() const { return Args->end(); } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 278 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 279 | static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A, |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 280 | RetEffect RetEff, ArgEffect DefaultEff, |
Ted Kremenek | 2d1086c | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 281 | ArgEffect ReceiverEff, bool EndPath) { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 282 | ID.AddPointer(A); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 283 | ID.Add(RetEff); |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 284 | ID.AddInteger((unsigned) DefaultEff); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 285 | ID.AddInteger((unsigned) ReceiverEff); |
Ted Kremenek | 2d1086c | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 286 | ID.AddInteger((unsigned) EndPath); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 2d1086c | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 290 | Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 291 | } |
| 292 | }; |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 293 | } // end anonymous namespace |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 295 | //===----------------------------------------------------------------------===// |
| 296 | // Data structures for constructing summaries. |
| 297 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 53301ba | 2008-06-24 03:49:48 +0000 | [diff] [blame] | 298 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 299 | namespace { |
| 300 | class VISIBILITY_HIDDEN ObjCSummaryKey { |
| 301 | IdentifierInfo* II; |
| 302 | Selector S; |
| 303 | public: |
| 304 | ObjCSummaryKey(IdentifierInfo* ii, Selector s) |
| 305 | : II(ii), S(s) {} |
| 306 | |
| 307 | ObjCSummaryKey(ObjCInterfaceDecl* d, Selector s) |
| 308 | : II(d ? d->getIdentifier() : 0), S(s) {} |
| 309 | |
| 310 | ObjCSummaryKey(Selector s) |
| 311 | : II(0), S(s) {} |
| 312 | |
| 313 | IdentifierInfo* getIdentifier() const { return II; } |
| 314 | Selector getSelector() const { return S; } |
| 315 | }; |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | namespace llvm { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 319 | template <> struct DenseMapInfo<ObjCSummaryKey> { |
| 320 | static inline ObjCSummaryKey getEmptyKey() { |
| 321 | return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(), |
| 322 | DenseMapInfo<Selector>::getEmptyKey()); |
| 323 | } |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 324 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 325 | static inline ObjCSummaryKey getTombstoneKey() { |
| 326 | return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(), |
| 327 | DenseMapInfo<Selector>::getTombstoneKey()); |
| 328 | } |
| 329 | |
| 330 | static unsigned getHashValue(const ObjCSummaryKey &V) { |
| 331 | return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier()) |
| 332 | & 0x88888888) |
| 333 | | (DenseMapInfo<Selector>::getHashValue(V.getSelector()) |
| 334 | & 0x55555555); |
| 335 | } |
| 336 | |
| 337 | static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) { |
| 338 | return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(), |
| 339 | RHS.getIdentifier()) && |
| 340 | DenseMapInfo<Selector>::isEqual(LHS.getSelector(), |
| 341 | RHS.getSelector()); |
| 342 | } |
| 343 | |
| 344 | static bool isPod() { |
| 345 | return DenseMapInfo<ObjCInterfaceDecl*>::isPod() && |
| 346 | DenseMapInfo<Selector>::isPod(); |
| 347 | } |
| 348 | }; |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 349 | } // end llvm namespace |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 351 | namespace { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 352 | class VISIBILITY_HIDDEN ObjCSummaryCache { |
| 353 | typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy; |
| 354 | MapTy M; |
| 355 | public: |
| 356 | ObjCSummaryCache() {} |
| 357 | |
| 358 | typedef MapTy::iterator iterator; |
| 359 | |
| 360 | iterator find(ObjCInterfaceDecl* D, Selector S) { |
| 361 | |
| 362 | // Do a lookup with the (D,S) pair. If we find a match return |
| 363 | // the iterator. |
| 364 | ObjCSummaryKey K(D, S); |
| 365 | MapTy::iterator I = M.find(K); |
| 366 | |
| 367 | if (I != M.end() || !D) |
| 368 | return I; |
| 369 | |
| 370 | // Walk the super chain. If we find a hit with a parent, we'll end |
| 371 | // up returning that summary. We actually allow that key (null,S), as |
| 372 | // we cache summaries for the null ObjCInterfaceDecl* to allow us to |
| 373 | // generate initial summaries without having to worry about NSObject |
| 374 | // being declared. |
| 375 | // FIXME: We may change this at some point. |
| 376 | for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) { |
| 377 | if ((I = M.find(ObjCSummaryKey(C, S))) != M.end()) |
| 378 | break; |
| 379 | |
| 380 | if (!C) |
| 381 | return I; |
| 382 | } |
| 383 | |
| 384 | // Cache the summary with original key to make the next lookup faster |
| 385 | // and return the iterator. |
| 386 | M[K] = I->second; |
| 387 | return I; |
| 388 | } |
| 389 | |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 390 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 391 | iterator find(Expr* Receiver, Selector S) { |
| 392 | return find(getReceiverDecl(Receiver), S); |
| 393 | } |
| 394 | |
| 395 | iterator find(IdentifierInfo* II, Selector S) { |
| 396 | // FIXME: Class method lookup. Right now we dont' have a good way |
| 397 | // of going between IdentifierInfo* and the class hierarchy. |
| 398 | iterator I = M.find(ObjCSummaryKey(II, S)); |
| 399 | return I == M.end() ? M.find(ObjCSummaryKey(S)) : I; |
| 400 | } |
| 401 | |
| 402 | ObjCInterfaceDecl* getReceiverDecl(Expr* E) { |
| 403 | |
| 404 | const PointerType* PT = E->getType()->getAsPointerType(); |
| 405 | if (!PT) return 0; |
| 406 | |
| 407 | ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType()); |
| 408 | if (!OI) return 0; |
| 409 | |
| 410 | return OI ? OI->getDecl() : 0; |
| 411 | } |
| 412 | |
| 413 | iterator end() { return M.end(); } |
| 414 | |
| 415 | RetainSummary*& operator[](ObjCMessageExpr* ME) { |
| 416 | |
| 417 | Selector S = ME->getSelector(); |
| 418 | |
| 419 | if (Expr* Receiver = ME->getReceiver()) { |
| 420 | ObjCInterfaceDecl* OD = getReceiverDecl(Receiver); |
| 421 | return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S]; |
| 422 | } |
| 423 | |
| 424 | return M[ObjCSummaryKey(ME->getClassName(), S)]; |
| 425 | } |
| 426 | |
| 427 | RetainSummary*& operator[](ObjCSummaryKey K) { |
| 428 | return M[K]; |
| 429 | } |
| 430 | |
| 431 | RetainSummary*& operator[](Selector S) { |
| 432 | return M[ ObjCSummaryKey(S) ]; |
| 433 | } |
| 434 | }; |
| 435 | } // end anonymous namespace |
| 436 | |
| 437 | //===----------------------------------------------------------------------===// |
| 438 | // Data structures for managing collections of summaries. |
| 439 | //===----------------------------------------------------------------------===// |
| 440 | |
| 441 | namespace { |
| 442 | class VISIBILITY_HIDDEN RetainSummaryManager { |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 443 | |
| 444 | //==-----------------------------------------------------------------==// |
| 445 | // Typedefs. |
| 446 | //==-----------------------------------------------------------------==// |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 447 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 448 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> > |
| 449 | ArgEffectsSetTy; |
| 450 | |
| 451 | typedef llvm::FoldingSet<RetainSummary> |
| 452 | SummarySetTy; |
| 453 | |
| 454 | typedef llvm::DenseMap<FunctionDecl*, RetainSummary*> |
| 455 | FuncSummariesTy; |
| 456 | |
Ted Kremenek | 4f22a78 | 2008-06-23 23:30:29 +0000 | [diff] [blame] | 457 | typedef ObjCSummaryCache ObjCMethodSummariesTy; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 458 | |
| 459 | //==-----------------------------------------------------------------==// |
| 460 | // Data. |
| 461 | //==-----------------------------------------------------------------==// |
| 462 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 463 | /// Ctx - The ASTContext object for the analyzed ASTs. |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 464 | ASTContext& Ctx; |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 465 | |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 466 | /// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier |
| 467 | /// "CFDictionaryCreate". |
| 468 | IdentifierInfo* CFDictionaryCreateII; |
| 469 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 470 | /// GCEnabled - Records whether or not the analyzed code runs in GC mode. |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 471 | const bool GCEnabled; |
| 472 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 473 | /// SummarySet - A FoldingSet of uniqued summaries. |
Ted Kremenek | 3ea0b6a | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 474 | SummarySetTy SummarySet; |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 475 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 476 | /// FuncSummaries - A map from FunctionDecls to summaries. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 477 | FuncSummariesTy FuncSummaries; |
| 478 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 479 | /// ObjCClassMethodSummaries - A map from selectors (for instance methods) |
| 480 | /// to summaries. |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 481 | ObjCMethodSummariesTy ObjCClassMethodSummaries; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 482 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 483 | /// ObjCMethodSummaries - A map from selectors to summaries. |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 484 | ObjCMethodSummariesTy ObjCMethodSummaries; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 485 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 486 | /// ArgEffectsSet - A FoldingSet of uniqued ArgEffects. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 487 | ArgEffectsSetTy ArgEffectsSet; |
| 488 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 489 | /// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects, |
| 490 | /// and all other data used by the checker. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 491 | llvm::BumpPtrAllocator BPAlloc; |
| 492 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 493 | /// ScratchArgs - A holding buffer for construct ArgEffects. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 494 | ArgEffects ScratchArgs; |
| 495 | |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 496 | RetainSummary* StopSummary; |
| 497 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 498 | //==-----------------------------------------------------------------==// |
| 499 | // Methods. |
| 500 | //==-----------------------------------------------------------------==// |
| 501 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 502 | /// getArgEffects - Returns a persistent ArgEffects object based on the |
| 503 | /// data in ScratchArgs. |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 504 | ArgEffects* getArgEffects(); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 505 | |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 506 | enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable }; |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 507 | |
| 508 | public: |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 509 | RetainSummary* getUnarySummary(FunctionType* FT, UnaryFuncKind func); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 510 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 511 | RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD); |
| 512 | RetainSummary* getCFSummaryGetRule(FunctionDecl* FD); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 513 | RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 514 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 515 | RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff, |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 516 | ArgEffect ReceiverEff = DoNothing, |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 517 | ArgEffect DefaultEff = MayEscape, |
| 518 | bool isEndPath = false); |
Ted Kremenek | 706522f | 2008-10-29 04:07:07 +0000 | [diff] [blame] | 519 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 520 | RetainSummary* getPersistentSummary(RetEffect RE, |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 521 | ArgEffect ReceiverEff = DoNothing, |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 522 | ArgEffect DefaultEff = MayEscape) { |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 523 | return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff); |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 524 | } |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 525 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 526 | RetainSummary* getPersistentStopSummary() { |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 527 | if (StopSummary) |
| 528 | return StopSummary; |
| 529 | |
| 530 | StopSummary = getPersistentSummary(RetEffect::MakeNoRet(), |
| 531 | StopTracking, StopTracking); |
Ted Kremenek | 706522f | 2008-10-29 04:07:07 +0000 | [diff] [blame] | 532 | |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 533 | return StopSummary; |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 534 | } |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 535 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 536 | RetainSummary* getInitMethodSummary(ObjCMessageExpr* ME); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 537 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 538 | void InitializeClassMethodSummaries(); |
| 539 | void InitializeMethodSummaries(); |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 540 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 541 | bool isTrackedObjectType(QualType T); |
| 542 | |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 543 | private: |
| 544 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 545 | void addClsMethSummary(IdentifierInfo* ClsII, Selector S, |
| 546 | RetainSummary* Summ) { |
| 547 | ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 548 | } |
| 549 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 550 | void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) { |
| 551 | ObjCClassMethodSummaries[S] = Summ; |
| 552 | } |
| 553 | |
| 554 | void addNSObjectMethSummary(Selector S, RetainSummary *Summ) { |
| 555 | ObjCMethodSummaries[S] = Summ; |
| 556 | } |
| 557 | |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 558 | void addInstMethSummary(const char* Cls, RetainSummary* Summ, va_list argp) { |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 559 | |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 560 | IdentifierInfo* ClsII = &Ctx.Idents.get(Cls); |
| 561 | llvm::SmallVector<IdentifierInfo*, 10> II; |
| 562 | |
| 563 | while (const char* s = va_arg(argp, const char*)) |
| 564 | II.push_back(&Ctx.Idents.get(s)); |
| 565 | |
| 566 | Selector S = Ctx.Selectors.getSelector(II.size(), &II[0]); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 567 | ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ; |
| 568 | } |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 569 | |
| 570 | void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) { |
| 571 | va_list argp; |
| 572 | va_start(argp, Summ); |
| 573 | addInstMethSummary(Cls, Summ, argp); |
| 574 | va_end(argp); |
| 575 | } |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 576 | |
| 577 | void addPanicSummary(const char* Cls, ...) { |
| 578 | RetainSummary* Summ = getPersistentSummary(0, RetEffect::MakeNoRet(), |
| 579 | DoNothing, DoNothing, true); |
| 580 | va_list argp; |
| 581 | va_start (argp, Cls); |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 582 | addInstMethSummary(Cls, Summ, argp); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 583 | va_end(argp); |
| 584 | } |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 585 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 586 | public: |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 587 | |
| 588 | RetainSummaryManager(ASTContext& ctx, bool gcenabled) |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 589 | : Ctx(ctx), |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 590 | CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")), |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 591 | GCEnabled(gcenabled), StopSummary(0) { |
| 592 | |
| 593 | InitializeClassMethodSummaries(); |
| 594 | InitializeMethodSummaries(); |
| 595 | } |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 596 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 597 | ~RetainSummaryManager(); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 598 | |
Ted Kremenek | ab59227 | 2008-06-24 03:56:45 +0000 | [diff] [blame] | 599 | RetainSummary* getSummary(FunctionDecl* FD); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 600 | RetainSummary* getMethodSummary(ObjCMessageExpr* ME, ObjCInterfaceDecl* ID); |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 601 | RetainSummary* getClassMethodSummary(IdentifierInfo* ClsName, Selector S); |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 602 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 603 | bool isGCEnabled() const { return GCEnabled; } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 604 | }; |
| 605 | |
| 606 | } // end anonymous namespace |
| 607 | |
| 608 | //===----------------------------------------------------------------------===// |
| 609 | // Implementation of checker data structures. |
| 610 | //===----------------------------------------------------------------------===// |
| 611 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 612 | RetainSummaryManager::~RetainSummaryManager() { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 613 | |
| 614 | // FIXME: The ArgEffects could eventually be allocated from BPAlloc, |
| 615 | // mitigating the need to do explicit cleanup of the |
| 616 | // Argument-Effect summaries. |
| 617 | |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 618 | for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(), |
| 619 | E = ArgEffectsSet.end(); I!=E; ++I) |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 620 | I->getValue().~ArgEffects(); |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 621 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 622 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 623 | ArgEffects* RetainSummaryManager::getArgEffects() { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 624 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 625 | if (ScratchArgs.empty()) |
| 626 | return NULL; |
| 627 | |
| 628 | // Compute a profile for a non-empty ScratchArgs. |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 629 | llvm::FoldingSetNodeID profile; |
| 630 | profile.Add(ScratchArgs); |
| 631 | void* InsertPos; |
| 632 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 633 | // Look up the uniqued copy, or create a new one. |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 634 | llvm::FoldingSetNodeWrapper<ArgEffects>* E = |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 635 | ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 636 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 637 | if (E) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 638 | ScratchArgs.clear(); |
| 639 | return &E->getValue(); |
| 640 | } |
| 641 | |
| 642 | E = (llvm::FoldingSetNodeWrapper<ArgEffects>*) |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 643 | BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 644 | |
| 645 | new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 646 | ArgEffectsSet.InsertNode(E, InsertPos); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 647 | |
| 648 | ScratchArgs.clear(); |
| 649 | return &E->getValue(); |
| 650 | } |
| 651 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 652 | RetainSummary* |
| 653 | RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff, |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 654 | ArgEffect ReceiverEff, |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 655 | ArgEffect DefaultEff, |
| 656 | bool isEndPath) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 657 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 658 | // Generate a profile for the summary. |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 659 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | 2d1086c | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 660 | RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff, |
| 661 | isEndPath); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 662 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 663 | // Look up the uniqued summary, or create one if it doesn't exist. |
| 664 | void* InsertPos; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 665 | RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 666 | |
| 667 | if (Summ) |
| 668 | return Summ; |
| 669 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 670 | // Create the summary and return it. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 671 | Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>(); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 672 | new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 673 | SummarySet.InsertNode(Summ, InsertPos); |
| 674 | |
| 675 | return Summ; |
| 676 | } |
| 677 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 678 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 679 | // Predicates. |
| 680 | //===----------------------------------------------------------------------===// |
| 681 | |
| 682 | bool RetainSummaryManager::isTrackedObjectType(QualType T) { |
| 683 | if (!Ctx.isObjCObjectPointerType(T)) |
| 684 | return false; |
| 685 | |
| 686 | // Does it subclass NSObject? |
| 687 | ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr()); |
| 688 | |
| 689 | // We assume that id<..>, id, and "Class" all represent tracked objects. |
| 690 | if (!OT) |
| 691 | return true; |
| 692 | |
| 693 | // Does the object type subclass NSObject? |
| 694 | // FIXME: We can memoize here if this gets too expensive. |
| 695 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
| 696 | ObjCInterfaceDecl* ID = OT->getDecl(); |
| 697 | |
| 698 | for ( ; ID ; ID = ID->getSuperClass()) |
| 699 | if (ID->getIdentifier() == NSObjectII) |
| 700 | return true; |
| 701 | |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 706 | // Summary creation for functions (largely uses of Core Foundation). |
| 707 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 708 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 709 | static bool isRetain(FunctionDecl* FD, const char* FName) { |
| 710 | const char* loc = strstr(FName, "Retain"); |
| 711 | return loc && loc[sizeof("Retain")-1] == '\0'; |
| 712 | } |
| 713 | |
| 714 | static bool isRelease(FunctionDecl* FD, const char* FName) { |
| 715 | const char* loc = strstr(FName, "Release"); |
| 716 | return loc && loc[sizeof("Release")-1] == '\0'; |
| 717 | } |
| 718 | |
Ted Kremenek | ab59227 | 2008-06-24 03:56:45 +0000 | [diff] [blame] | 719 | RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 720 | |
| 721 | SourceLocation Loc = FD->getLocation(); |
| 722 | |
| 723 | if (!Loc.isFileID()) |
| 724 | return NULL; |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 725 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 726 | // Look up a summary in our cache of FunctionDecls -> Summaries. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 727 | FuncSummariesTy::iterator I = FuncSummaries.find(FD); |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 728 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 729 | if (I != FuncSummaries.end()) |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 730 | return I->second; |
| 731 | |
| 732 | // No summary. Generate one. |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 733 | RetainSummary *S = 0; |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 734 | |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 735 | do { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 736 | // We generate "stop" summaries for implicitly defined functions. |
| 737 | if (FD->isImplicit()) { |
| 738 | S = getPersistentStopSummary(); |
| 739 | break; |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 740 | } |
Ted Kremenek | 6ca3191 | 2008-11-04 00:36:12 +0000 | [diff] [blame] | 741 | |
Ted Kremenek | 9989065 | 2009-01-16 18:40:33 +0000 | [diff] [blame] | 742 | // [PR 3337] Use 'getDesugaredType' to strip away any typedefs on the |
| 743 | // function's type. |
| 744 | FunctionType* FT = cast<FunctionType>(FD->getType()->getDesugaredType()); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 745 | const char* FName = FD->getIdentifier()->getName(); |
| 746 | |
| 747 | // Inspect the result type. |
| 748 | QualType RetTy = FT->getResultType(); |
| 749 | |
| 750 | // FIXME: This should all be refactored into a chain of "summary lookup" |
| 751 | // filters. |
| 752 | if (strcmp(FName, "IOServiceGetMatchingServices") == 0) { |
| 753 | // FIXES: <rdar://problem/6326900> |
| 754 | // This should be addressed using a API table. This strcmp is also |
| 755 | // a little gross, but there is no need to super optimize here. |
| 756 | assert (ScratchArgs.empty()); |
| 757 | ScratchArgs.push_back(std::make_pair(1, DecRef)); |
| 758 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 759 | break; |
Ted Kremenek | 64e859a | 2008-10-22 20:54:52 +0000 | [diff] [blame] | 760 | } |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 761 | |
| 762 | // Handle: id NSMakeCollectable(CFTypeRef) |
| 763 | if (strcmp(FName, "NSMakeCollectable") == 0) { |
| 764 | S = (RetTy == Ctx.getObjCIdType()) |
| 765 | ? getUnarySummary(FT, cfmakecollectable) |
| 766 | : getPersistentStopSummary(); |
| 767 | |
| 768 | break; |
| 769 | } |
| 770 | |
| 771 | if (RetTy->isPointerType()) { |
| 772 | // For CoreFoundation ('CF') types. |
| 773 | if (isRefType(RetTy, "CF", &Ctx, FName)) { |
| 774 | if (isRetain(FD, FName)) |
| 775 | S = getUnarySummary(FT, cfretain); |
| 776 | else if (strstr(FName, "MakeCollectable")) |
| 777 | S = getUnarySummary(FT, cfmakecollectable); |
| 778 | else |
| 779 | S = getCFCreateGetRuleSummary(FD, FName); |
| 780 | |
| 781 | break; |
| 782 | } |
| 783 | |
| 784 | // For CoreGraphics ('CG') types. |
| 785 | if (isRefType(RetTy, "CG", &Ctx, FName)) { |
| 786 | if (isRetain(FD, FName)) |
| 787 | S = getUnarySummary(FT, cfretain); |
| 788 | else |
| 789 | S = getCFCreateGetRuleSummary(FD, FName); |
| 790 | |
| 791 | break; |
| 792 | } |
| 793 | |
| 794 | // For the Disk Arbitration API (DiskArbitration/DADisk.h) |
| 795 | if (isRefType(RetTy, "DADisk") || |
| 796 | isRefType(RetTy, "DADissenter") || |
| 797 | isRefType(RetTy, "DASessionRef")) { |
| 798 | S = getCFCreateGetRuleSummary(FD, FName); |
| 799 | break; |
| 800 | } |
| 801 | |
| 802 | break; |
| 803 | } |
| 804 | |
| 805 | // Check for release functions, the only kind of functions that we care |
| 806 | // about that don't return a pointer type. |
| 807 | if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) { |
| 808 | if (isRelease(FD, FName+2)) |
| 809 | S = getUnarySummary(FT, cfrelease); |
| 810 | else { |
Ted Kremenek | 6818928 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 811 | assert (ScratchArgs.empty()); |
| 812 | // Remaining CoreFoundation and CoreGraphics functions. |
| 813 | // We use to assume that they all strictly followed the ownership idiom |
| 814 | // and that ownership cannot be transferred. While this is technically |
| 815 | // correct, many methods allow a tracked object to escape. For example: |
| 816 | // |
| 817 | // CFMutableDictionaryRef x = CFDictionaryCreateMutable(...); |
| 818 | // CFDictionaryAddValue(y, key, x); |
| 819 | // CFRelease(x); |
| 820 | // ... it is okay to use 'x' since 'y' has a reference to it |
| 821 | // |
| 822 | // We handle this and similar cases with the follow heuristic. If the |
| 823 | // function name contains "InsertValue", "SetValue" or "AddValue" then |
| 824 | // we assume that arguments may "escape." |
| 825 | // |
| 826 | ArgEffect E = (CStrInCStrNoCase(FName, "InsertValue") || |
| 827 | CStrInCStrNoCase(FName, "AddValue") || |
Ted Kremenek | a92206e | 2009-02-05 22:34:53 +0000 | [diff] [blame] | 828 | CStrInCStrNoCase(FName, "SetValue") || |
| 829 | CStrInCStrNoCase(FName, "AppendValue")) |
Ted Kremenek | 6818928 | 2009-01-29 22:45:13 +0000 | [diff] [blame] | 830 | ? MayEscape : DoNothing; |
| 831 | |
| 832 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, E); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 833 | } |
| 834 | } |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 835 | } |
| 836 | while (0); |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 837 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 838 | FuncSummaries[FD] = S; |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 839 | return S; |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 842 | RetainSummary* |
| 843 | RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD, |
| 844 | const char* FName) { |
| 845 | |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 846 | if (strstr(FName, "Create") || strstr(FName, "Copy")) |
| 847 | return getCFSummaryCreateRule(FD); |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 848 | |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 849 | if (strstr(FName, "Get")) |
| 850 | return getCFSummaryGetRule(FD); |
| 851 | |
| 852 | return 0; |
| 853 | } |
| 854 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 855 | RetainSummary* |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 856 | RetainSummaryManager::getUnarySummary(FunctionType* FT, UnaryFuncKind func) { |
| 857 | // Sanity check that this is *really* a unary function. This can |
| 858 | // happen if people do weird things. |
| 859 | FunctionTypeProto* FTP = dyn_cast<FunctionTypeProto>(FT); |
| 860 | if (!FTP || FTP->getNumArgs() != 1) |
| 861 | return getPersistentStopSummary(); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 862 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 863 | assert (ScratchArgs.empty()); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 864 | |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 865 | switch (func) { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 866 | case cfretain: { |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 867 | ScratchArgs.push_back(std::make_pair(0, IncRef)); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 868 | return getPersistentSummary(RetEffect::MakeAlias(0), |
| 869 | DoNothing, DoNothing); |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | case cfrelease: { |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 873 | ScratchArgs.push_back(std::make_pair(0, DecRef)); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 874 | return getPersistentSummary(RetEffect::MakeNoRet(), |
| 875 | DoNothing, DoNothing); |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | case cfmakecollectable: { |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 879 | if (GCEnabled) |
| 880 | ScratchArgs.push_back(std::make_pair(0, DecRef)); |
| 881 | |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 882 | return getPersistentSummary(RetEffect::MakeAlias(0), |
| 883 | DoNothing, DoNothing); |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | default: |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 887 | assert (false && "Not a supported unary function."); |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 888 | return 0; |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 889 | } |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 892 | RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) { |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 893 | assert (ScratchArgs.empty()); |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 894 | |
| 895 | if (FD->getIdentifier() == CFDictionaryCreateII) { |
| 896 | ScratchArgs.push_back(std::make_pair(1, DoNothingByRef)); |
| 897 | ScratchArgs.push_back(std::make_pair(2, DoNothingByRef)); |
| 898 | } |
| 899 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 900 | return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 903 | RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) { |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 904 | assert (ScratchArgs.empty()); |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 905 | return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF), |
| 906 | DoNothing, DoNothing); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 909 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 910 | // Summary creation for Selectors. |
| 911 | //===----------------------------------------------------------------------===// |
| 912 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 913 | RetainSummary* |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 914 | RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) { |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 915 | assert(ScratchArgs.empty()); |
| 916 | |
| 917 | RetainSummary* Summ = |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 918 | getPersistentSummary(RetEffect::MakeReceiverAlias()); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 919 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 920 | ObjCMethodSummaries[ME] = Summ; |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 921 | return Summ; |
| 922 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 923 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 924 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 925 | RetainSummary* |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 926 | RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME, |
| 927 | ObjCInterfaceDecl* ID) { |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 928 | |
| 929 | Selector S = ME->getSelector(); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 930 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 931 | // Look up a summary in our summary cache. |
| 932 | ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 933 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 934 | if (I != ObjCMethodSummaries.end()) |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 935 | return I->second; |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 936 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 937 | // "initXXX": pass-through for receiver. |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 938 | const char* s = S.getIdentifierInfoForSlot(0)->getName(); |
Ted Kremenek | a4b695a | 2008-05-07 03:45:05 +0000 | [diff] [blame] | 939 | assert (ScratchArgs.empty()); |
Ted Kremenek | aee9e57 | 2008-05-06 06:09:09 +0000 | [diff] [blame] | 940 | |
Ted Kremenek | 0327f77 | 2008-06-02 17:14:13 +0000 | [diff] [blame] | 941 | if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0) |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 942 | return getInitMethodSummary(ME); |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 943 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 944 | // Look for methods that return an owned object. |
| 945 | if (!isTrackedObjectType(Ctx.getCanonicalType(ME->getType()))) |
Ted Kremenek | 84060db | 2008-05-07 04:25:59 +0000 | [diff] [blame] | 946 | return 0; |
Ted Kremenek | a4b695a | 2008-05-07 03:45:05 +0000 | [diff] [blame] | 947 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 948 | if (followsFundamentalRule(s)) { |
| 949 | RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet() |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 950 | : RetEffect::MakeOwned(RetEffect::ObjC, true); |
Ted Kremenek | a4b695a | 2008-05-07 03:45:05 +0000 | [diff] [blame] | 951 | RetainSummary* Summ = getPersistentSummary(E); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 952 | ObjCMethodSummaries[ME] = Summ; |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 953 | return Summ; |
| 954 | } |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 955 | |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 956 | return 0; |
| 957 | } |
| 958 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 959 | RetainSummary* |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 960 | RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName, |
| 961 | Selector S) { |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 962 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 963 | // FIXME: Eventually we should properly do class method summaries, but |
| 964 | // it requires us being able to walk the type hierarchy. Unfortunately, |
| 965 | // we cannot do this with just an IdentifierInfo* for the class name. |
| 966 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 967 | // Look up a summary in our cache of Selectors -> Summaries. |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 968 | ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S); |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 969 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 970 | if (I != ObjCClassMethodSummaries.end()) |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 971 | return I->second; |
| 972 | |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 973 | return 0; |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 976 | void RetainSummaryManager::InitializeClassMethodSummaries() { |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 977 | |
| 978 | assert (ScratchArgs.empty()); |
| 979 | |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 980 | RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet() |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 981 | : RetEffect::MakeOwned(RetEffect::ObjC, true); |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 982 | |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 983 | RetainSummary* Summ = getPersistentSummary(E); |
| 984 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 985 | // Create the summaries for "alloc", "new", and "allocWithZone:" for |
| 986 | // NSObject and its derivatives. |
| 987 | addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ); |
| 988 | addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ); |
| 989 | addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 990 | |
| 991 | // Create the [NSAssertionHandler currentHander] summary. |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 992 | addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"), |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 993 | GetNullarySelector("currentHandler", Ctx), |
| 994 | getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC))); |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 995 | |
| 996 | // Create the [NSAutoreleasePool addObject:] summary. |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 997 | ScratchArgs.push_back(std::make_pair(0, Autorelease)); |
| 998 | addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"), |
| 999 | GetUnarySelector("addObject", Ctx), |
| 1000 | getPersistentSummary(RetEffect::MakeNoRet(), |
| 1001 | DoNothing, DoNothing)); |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1004 | void RetainSummaryManager::InitializeMethodSummaries() { |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1005 | |
| 1006 | assert (ScratchArgs.empty()); |
| 1007 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1008 | // Create the "init" selector. It just acts as a pass-through for the |
| 1009 | // receiver. |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 1010 | RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias()); |
| 1011 | addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm); |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1012 | |
| 1013 | // The next methods are allocators. |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1014 | RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet() |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1015 | : RetEffect::MakeOwned(RetEffect::ObjC, true); |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1016 | |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 1017 | RetainSummary* Summ = getPersistentSummary(E); |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1018 | |
| 1019 | // Create the "copy" selector. |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1020 | addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ); |
| 1021 | |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1022 | // Create the "mutableCopy" selector. |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1023 | addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ); |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1024 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1025 | // Create the "retain" selector. |
| 1026 | E = RetEffect::MakeReceiverAlias(); |
| 1027 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1028 | addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1029 | |
| 1030 | // Create the "release" selector. |
| 1031 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1032 | addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ); |
Ted Kremenek | 299e815 | 2008-05-07 21:17:39 +0000 | [diff] [blame] | 1033 | |
| 1034 | // Create the "drain" selector. |
| 1035 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1036 | addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Create the "autorelease" selector. |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 1039 | Summ = getPersistentSummary(E, Autorelease); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1040 | addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ); |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1041 | |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1042 | // For NSWindow, allocated objects are (initially) self-owned. |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 1043 | RetainSummary *NSWindowSumm = |
| 1044 | getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn); |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1045 | |
| 1046 | addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect", |
| 1047 | "styleMask", "backing", "defer", NULL); |
| 1048 | |
| 1049 | addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect", |
| 1050 | "styleMask", "backing", "defer", "screen", NULL); |
| 1051 | |
| 1052 | // For NSPanel (which subclasses NSWindow), allocated objects are not |
| 1053 | // self-owned. |
| 1054 | addInstMethSummary("NSPanel", InitSumm, "initWithContentRect", |
| 1055 | "styleMask", "backing", "defer", NULL); |
| 1056 | |
| 1057 | addInstMethSummary("NSPanel", InitSumm, "initWithContentRect", |
| 1058 | "styleMask", "backing", "defer", "screen", NULL); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1059 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1060 | // Create NSAssertionHandler summaries. |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1061 | addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file", |
| 1062 | "lineNumber", "description", NULL); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1063 | |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1064 | addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object", |
| 1065 | "file", "lineNumber", "description", NULL); |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1068 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1069 | // Reference-counting logic (typestate + counts). |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1070 | //===----------------------------------------------------------------------===// |
| 1071 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1072 | namespace { |
| 1073 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1074 | class VISIBILITY_HIDDEN RefVal { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1075 | public: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1076 | enum Kind { |
| 1077 | Owned = 0, // Owning reference. |
| 1078 | NotOwned, // Reference is not owned by still valid (not freed). |
| 1079 | Released, // Object has been released. |
| 1080 | ReturnedOwned, // Returned object passes ownership to caller. |
| 1081 | ReturnedNotOwned, // Return object does not pass ownership to caller. |
| 1082 | ErrorUseAfterRelease, // Object used after released. |
| 1083 | ErrorReleaseNotOwned, // Release of an object that was not owned. |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1084 | ErrorLeak, // A memory leak due to excessive reference counts. |
| 1085 | ErrorLeakReturned // A memory leak due to the returning method not having |
| 1086 | // the correct naming conventions. |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1087 | }; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1088 | |
| 1089 | private: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1090 | Kind kind; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1091 | RetEffect::ObjKind okind; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1092 | unsigned Cnt; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1093 | QualType T; |
| 1094 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1095 | RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, QualType t) |
| 1096 | : kind(k), okind(o), Cnt(cnt), T(t) {} |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1097 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1098 | RefVal(Kind k, unsigned cnt = 0) |
| 1099 | : kind(k), okind(RetEffect::AnyObj), Cnt(cnt) {} |
| 1100 | |
| 1101 | public: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1102 | Kind getKind() const { return kind; } |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1103 | |
| 1104 | RetEffect::ObjKind getObjKind() const { return okind; } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1105 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1106 | unsigned getCount() const { return Cnt; } |
| 1107 | QualType getType() const { return T; } |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1108 | |
| 1109 | // Useful predicates. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1110 | |
Ted Kremenek | 73c750b | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 1111 | static bool isError(Kind k) { return k >= ErrorUseAfterRelease; } |
| 1112 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1113 | static bool isLeak(Kind k) { return k >= ErrorLeak; } |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1114 | |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1115 | bool isOwned() const { |
| 1116 | return getKind() == Owned; |
| 1117 | } |
| 1118 | |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1119 | bool isNotOwned() const { |
| 1120 | return getKind() == NotOwned; |
| 1121 | } |
| 1122 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1123 | bool isReturnedOwned() const { |
| 1124 | return getKind() == ReturnedOwned; |
| 1125 | } |
| 1126 | |
| 1127 | bool isReturnedNotOwned() const { |
| 1128 | return getKind() == ReturnedNotOwned; |
| 1129 | } |
| 1130 | |
| 1131 | bool isNonLeakError() const { |
| 1132 | Kind k = getKind(); |
| 1133 | return isError(k) && !isLeak(k); |
| 1134 | } |
| 1135 | |
| 1136 | // State creation: normal state. |
| 1137 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1138 | static RefVal makeOwned(RetEffect::ObjKind o, QualType t, |
| 1139 | unsigned Count = 1) { |
| 1140 | return RefVal(Owned, o, Count, t); |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1143 | static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t, |
| 1144 | unsigned Count = 0) { |
| 1145 | return RefVal(NotOwned, o, Count, t); |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1146 | } |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1147 | |
| 1148 | static RefVal makeReturnedOwned(unsigned Count) { |
| 1149 | return RefVal(ReturnedOwned, Count); |
| 1150 | } |
| 1151 | |
| 1152 | static RefVal makeReturnedNotOwned() { |
| 1153 | return RefVal(ReturnedNotOwned); |
| 1154 | } |
| 1155 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1156 | // Comparison, profiling, and pretty-printing. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1157 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1158 | bool operator==(const RefVal& X) const { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1159 | return kind == X.kind && Cnt == X.Cnt && T == X.T; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1160 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1161 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1162 | RefVal operator-(size_t i) const { |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1163 | return RefVal(getKind(), getObjKind(), getCount() - i, getType()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | RefVal operator+(size_t i) const { |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1167 | return RefVal(getKind(), getObjKind(), getCount() + i, getType()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | RefVal operator^(Kind k) const { |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1171 | return RefVal(k, getObjKind(), getCount(), getType()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1172 | } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1173 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1174 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 1175 | ID.AddInteger((unsigned) kind); |
| 1176 | ID.AddInteger(Cnt); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1177 | ID.Add(T); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1180 | void print(std::ostream& Out) const; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1181 | }; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1182 | |
| 1183 | void RefVal::print(std::ostream& Out) const { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1184 | if (!T.isNull()) |
| 1185 | Out << "Tracked Type:" << T.getAsString() << '\n'; |
| 1186 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1187 | switch (getKind()) { |
| 1188 | default: assert(false); |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1189 | case Owned: { |
| 1190 | Out << "Owned"; |
| 1191 | unsigned cnt = getCount(); |
| 1192 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1193 | break; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1194 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1195 | |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1196 | case NotOwned: { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1197 | Out << "NotOwned"; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1198 | unsigned cnt = getCount(); |
| 1199 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1200 | break; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1201 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1202 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1203 | case ReturnedOwned: { |
| 1204 | Out << "ReturnedOwned"; |
| 1205 | unsigned cnt = getCount(); |
| 1206 | if (cnt) Out << " (+ " << cnt << ")"; |
| 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | case ReturnedNotOwned: { |
| 1211 | Out << "ReturnedNotOwned"; |
| 1212 | unsigned cnt = getCount(); |
| 1213 | if (cnt) Out << " (+ " << cnt << ")"; |
| 1214 | break; |
| 1215 | } |
| 1216 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1217 | case Released: |
| 1218 | Out << "Released"; |
| 1219 | break; |
| 1220 | |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1221 | case ErrorLeak: |
| 1222 | Out << "Leaked"; |
| 1223 | break; |
| 1224 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1225 | case ErrorLeakReturned: |
| 1226 | Out << "Leaked (Bad naming)"; |
| 1227 | break; |
| 1228 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1229 | case ErrorUseAfterRelease: |
| 1230 | Out << "Use-After-Release [ERROR]"; |
| 1231 | break; |
| 1232 | |
| 1233 | case ErrorReleaseNotOwned: |
| 1234 | Out << "Release of Not-Owned [ERROR]"; |
| 1235 | break; |
| 1236 | } |
| 1237 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1238 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1239 | } // end anonymous namespace |
| 1240 | |
| 1241 | //===----------------------------------------------------------------------===// |
| 1242 | // RefBindings - State used to track object reference counts. |
| 1243 | //===----------------------------------------------------------------------===// |
| 1244 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1245 | typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings; |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1246 | static int RefBIndex = 0; |
| 1247 | |
| 1248 | namespace clang { |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1249 | template<> |
| 1250 | struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> { |
| 1251 | static inline void* GDMIndex() { return &RefBIndex; } |
| 1252 | }; |
| 1253 | } |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1254 | |
| 1255 | //===----------------------------------------------------------------------===// |
| 1256 | // ARBindings - State used to track objects in autorelease pools. |
| 1257 | //===----------------------------------------------------------------------===// |
| 1258 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1259 | typedef llvm::ImmutableSet<SymbolRef> ARPoolContents; |
| 1260 | typedef llvm::ImmutableList< std::pair<SymbolRef, ARPoolContents*> > ARBindings; |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1261 | static int AutoRBIndex = 0; |
| 1262 | |
| 1263 | namespace clang { |
| 1264 | template<> |
| 1265 | struct GRStateTrait<ARBindings> : public GRStatePartialTrait<ARBindings> { |
| 1266 | static inline void* GDMIndex() { return &AutoRBIndex; } |
| 1267 | }; |
| 1268 | } |
| 1269 | |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1270 | //===----------------------------------------------------------------------===// |
| 1271 | // Transfer functions. |
| 1272 | //===----------------------------------------------------------------------===// |
| 1273 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1274 | namespace { |
| 1275 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1276 | class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals { |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1277 | public: |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1278 | class BindingsPrinter : public GRState::Printer { |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1279 | public: |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1280 | virtual void Print(std::ostream& Out, const GRState* state, |
| 1281 | const char* nl, const char* sep); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1282 | }; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1283 | |
| 1284 | private: |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1285 | RetainSummaryManager Summaries; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1286 | const LangOptions& LOpts; |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1287 | |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1288 | BugType *useAfterRelease, *releaseNotOwned; |
| 1289 | BugType *leakWithinFunction, *leakAtReturn; |
| 1290 | BugReporter *BR; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1291 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1292 | RefBindings Update(RefBindings B, SymbolRef sym, RefVal V, ArgEffect E, |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1293 | RefVal::Kind& hasErr, RefBindings::Factory& RefBFactory); |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1294 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1295 | RefVal::Kind& Update(GRStateRef& state, SymbolRef sym, RefVal V, |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1296 | ArgEffect E, RefVal::Kind& hasErr) { |
| 1297 | |
| 1298 | state = state.set<RefBindings>(Update(state.get<RefBindings>(), sym, V, |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1299 | E, hasErr, |
| 1300 | state.get_context<RefBindings>())); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1301 | return hasErr; |
| 1302 | } |
| 1303 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1304 | void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst, |
| 1305 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1306 | Expr* NodeExpr, Expr* ErrorExpr, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1307 | ExplodedNode<GRState>* Pred, |
| 1308 | const GRState* St, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1309 | RefVal::Kind hasErr, SymbolRef Sym); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1310 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1311 | std::pair<GRStateRef, bool> |
| 1312 | HandleSymbolDeath(GRStateManager& VMgr, const GRState* St, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1313 | const Decl* CD, SymbolRef sid, RefVal V, bool& hasLeak); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1314 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1315 | public: |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1316 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 1317 | CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts) |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1318 | : Summaries(Ctx, gcenabled), |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1319 | LOpts(lopts), useAfterRelease(0), releaseNotOwned(0), |
| 1320 | leakWithinFunction(0), leakAtReturn(0), BR(0) {} |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1321 | |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1322 | virtual ~CFRefCount() {} |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1323 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 1324 | void RegisterChecks(BugReporter &BR); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1325 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 1326 | virtual void RegisterPrinters(std::vector<GRState::Printer*>& Printers) { |
| 1327 | Printers.push_back(new BindingsPrinter()); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1328 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1329 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1330 | bool isGCEnabled() const { return Summaries.isGCEnabled(); } |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1331 | const LangOptions& getLangOptions() const { return LOpts; } |
| 1332 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1333 | // Calls. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1334 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1335 | void EvalSummary(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1336 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1337 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1338 | Expr* Ex, |
| 1339 | Expr* Receiver, |
| 1340 | RetainSummary* Summ, |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1341 | ExprIterator arg_beg, ExprIterator arg_end, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1342 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1343 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1344 | virtual void EvalCall(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 199e1a0 | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 1345 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1346 | GRStmtNodeBuilder<GRState>& Builder, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1347 | CallExpr* CE, SVal L, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1348 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 1349 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1350 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1351 | virtual void EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1352 | GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1353 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1354 | ObjCMessageExpr* ME, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1355 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1356 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1357 | bool EvalObjCMessageExprAux(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1358 | GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1359 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1360 | ObjCMessageExpr* ME, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1361 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1362 | |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1363 | // Stores. |
| 1364 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1365 | virtual void EvalStore(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1366 | GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1367 | GRStmtNodeBuilder<GRState>& Builder, |
| 1368 | Expr* E, ExplodedNode<GRState>* Pred, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1369 | const GRState* St, SVal TargetLV, SVal Val); |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1370 | // End-of-path. |
| 1371 | |
| 1372 | virtual void EvalEndPath(GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1373 | GREndPathNodeBuilder<GRState>& Builder); |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1374 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1375 | virtual void EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1376 | GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1377 | GRStmtNodeBuilder<GRState>& Builder, |
| 1378 | ExplodedNode<GRState>* Pred, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1379 | Stmt* S, const GRState* state, |
| 1380 | SymbolReaper& SymReaper); |
| 1381 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1382 | // Return statements. |
| 1383 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1384 | virtual void EvalReturn(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1385 | GRExprEngine& Engine, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1386 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1387 | ReturnStmt* S, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1388 | ExplodedNode<GRState>* Pred); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1389 | |
| 1390 | // Assumptions. |
| 1391 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1392 | virtual const GRState* EvalAssume(GRStateManager& VMgr, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1393 | const GRState* St, SVal Cond, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 1394 | bool Assumption, bool& isFeasible); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1395 | }; |
| 1396 | |
| 1397 | } // end anonymous namespace |
| 1398 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1399 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1400 | void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state, |
| 1401 | const char* nl, const char* sep) { |
| 1402 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1403 | RefBindings B = state->get<RefBindings>(); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1404 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1405 | if (!B.isEmpty()) |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1406 | Out << sep << nl; |
| 1407 | |
| 1408 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 1409 | Out << (*I).first << " : "; |
| 1410 | (*I).second.print(Out); |
| 1411 | Out << nl; |
| 1412 | } |
| 1413 | } |
| 1414 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1415 | static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) { |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1416 | return Summ ? Summ->getArg(idx) : MayEscape; |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1419 | static inline RetEffect GetRetEffect(RetainSummary* Summ) { |
| 1420 | return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet(); |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1423 | static inline ArgEffect GetReceiverE(RetainSummary* Summ) { |
| 1424 | return Summ ? Summ->getReceiverEffect() : DoNothing; |
| 1425 | } |
| 1426 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1427 | static inline bool IsEndPath(RetainSummary* Summ) { |
| 1428 | return Summ ? Summ->isEndPath() : false; |
| 1429 | } |
| 1430 | |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1431 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1432 | /// GetReturnType - Used to get the return type of a message expression or |
| 1433 | /// function call with the intention of affixing that type to a tracked symbol. |
| 1434 | /// While the the return type can be queried directly from RetEx, when |
| 1435 | /// invoking class methods we augment to the return type to be that of |
| 1436 | /// a pointer to the class (as opposed it just being id). |
| 1437 | static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) { |
| 1438 | |
| 1439 | QualType RetTy = RetE->getType(); |
| 1440 | |
| 1441 | // FIXME: We aren't handling id<...>. |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 1442 | const PointerType* PT = RetTy->getAsPointerType(); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1443 | if (!PT) |
| 1444 | return RetTy; |
| 1445 | |
| 1446 | // If RetEx is not a message expression just return its type. |
| 1447 | // If RetEx is a message expression, return its types if it is something |
| 1448 | /// more specific than id. |
| 1449 | |
| 1450 | ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE); |
| 1451 | |
| 1452 | if (!ME || !Ctx.isObjCIdType(PT->getPointeeType())) |
| 1453 | return RetTy; |
| 1454 | |
| 1455 | ObjCInterfaceDecl* D = ME->getClassInfo().first; |
| 1456 | |
| 1457 | // At this point we know the return type of the message expression is id. |
| 1458 | // If we have an ObjCInterceDecl, we know this is a call to a class method |
| 1459 | // whose type we can resolve. In such cases, promote the return type to |
| 1460 | // Class*. |
| 1461 | return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D)); |
| 1462 | } |
| 1463 | |
| 1464 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1465 | void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1466 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1467 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1468 | Expr* Ex, |
| 1469 | Expr* Receiver, |
| 1470 | RetainSummary* Summ, |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1471 | ExprIterator arg_beg, ExprIterator arg_end, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1472 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1473 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1474 | // Get the state. |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1475 | GRStateRef state(Builder.GetState(Pred), Eng.getStateManager()); |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1476 | ASTContext& Ctx = Eng.getStateManager().getContext(); |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1477 | |
| 1478 | // Evaluate the effect of the arguments. |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1479 | RefVal::Kind hasErr = (RefVal::Kind) 0; |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1480 | unsigned idx = 0; |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1481 | Expr* ErrorExpr = NULL; |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1482 | SymbolRef ErrorSym = 0; |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1483 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1484 | for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1485 | SVal V = state.GetSVal(*I); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1486 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1487 | if (isa<loc::SymbolVal>(V)) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1488 | SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol(); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1489 | if (RefBindings::data_type* T = state.get<RefBindings>(Sym)) |
| 1490 | if (Update(state, Sym, *T, GetArgE(Summ, idx), hasErr)) { |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1491 | ErrorExpr = *I; |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1492 | ErrorSym = Sym; |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1493 | break; |
| 1494 | } |
Ted Kremenek | b887355 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 1495 | } |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1496 | else if (isa<Loc>(V)) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1497 | if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) { |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1498 | |
| 1499 | if (GetArgE(Summ, idx) == DoNothingByRef) |
| 1500 | continue; |
| 1501 | |
| 1502 | // Invalidate the value of the variable passed by reference. |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 1503 | |
| 1504 | // FIXME: Either this logic should also be replicated in GRSimpleVals |
| 1505 | // or should be pulled into a separate "constraint engine." |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1506 | |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 1507 | // FIXME: We can have collisions on the conjured symbol if the |
| 1508 | // expression *I also creates conjured symbols. We probably want |
| 1509 | // to identify conjured symbols by an expression pair: the enclosing |
| 1510 | // expression (the context) and the expression itself. This should |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1511 | // disambiguate conjured symbols. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1512 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 1513 | const TypedRegion* R = dyn_cast<TypedRegion>(MR->getRegion()); |
Ted Kremenek | 90b3236 | 2008-12-17 19:42:34 +0000 | [diff] [blame] | 1514 | |
| 1515 | // Blast through AnonTypedRegions to get the original region type. |
| 1516 | while (R) { |
| 1517 | const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R); |
| 1518 | if (!ATR) break; |
| 1519 | R = dyn_cast<TypedRegion>(ATR->getSuperRegion()); |
| 1520 | } |
| 1521 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1522 | if (R) { |
Ted Kremenek | 40e86d9 | 2008-12-18 23:34:57 +0000 | [diff] [blame] | 1523 | |
| 1524 | // Is the invalidated variable something that we were tracking? |
| 1525 | SVal X = state.GetSVal(Loc::MakeVal(R)); |
| 1526 | |
| 1527 | if (isa<loc::SymbolVal>(X)) { |
| 1528 | SymbolRef Sym = cast<loc::SymbolVal>(X).getSymbol(); |
| 1529 | state = state.remove<RefBindings>(Sym); |
| 1530 | } |
| 1531 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1532 | // Set the value of the variable to be a conjured symbol. |
| 1533 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 1534 | QualType T = R->getRValueType(Ctx); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1535 | |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1536 | // FIXME: handle structs. |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 1537 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1538 | SymbolRef NewSym = |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1539 | Eng.getSymbolManager().getConjuredSymbol(*I, T, Count); |
| 1540 | |
Ted Kremenek | 90b3236 | 2008-12-17 19:42:34 +0000 | [diff] [blame] | 1541 | state = state.BindLoc(Loc::MakeVal(R), |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1542 | Loc::IsLocType(T) |
| 1543 | ? cast<SVal>(loc::SymbolVal(NewSym)) |
| 1544 | : cast<SVal>(nonloc::SymbolVal(NewSym))); |
| 1545 | } |
| 1546 | else { |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1547 | state = state.BindLoc(*MR, UnknownVal()); |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1548 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1549 | } |
| 1550 | else |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1551 | state = state.BindLoc(*MR, UnknownVal()); |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 1552 | } |
| 1553 | else { |
| 1554 | // Nuke all other arguments passed by reference. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1555 | state = state.Unbind(cast<Loc>(V)); |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 1556 | } |
Ted Kremenek | b887355 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 1557 | } |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1558 | else if (isa<nonloc::LocAsInteger>(V)) |
| 1559 | state = state.Unbind(cast<nonloc::LocAsInteger>(V).getLoc()); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1560 | } |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1561 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1562 | // Evaluate the effect on the message receiver. |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1563 | if (!ErrorExpr && Receiver) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1564 | SVal V = state.GetSVal(Receiver); |
| 1565 | if (isa<loc::SymbolVal>(V)) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1566 | SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol(); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1567 | if (const RefVal* T = state.get<RefBindings>(Sym)) |
| 1568 | if (Update(state, Sym, *T, GetReceiverE(Summ), hasErr)) { |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1569 | ErrorExpr = Receiver; |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1570 | ErrorSym = Sym; |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1571 | } |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1574 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1575 | // Process any errors. |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1576 | if (hasErr) { |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1577 | ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state, |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1578 | hasErr, ErrorSym); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1579 | return; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1580 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1581 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1582 | // Consult the summary for the return value. |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1583 | RetEffect RE = GetRetEffect(Summ); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1584 | |
| 1585 | switch (RE.getKind()) { |
| 1586 | default: |
| 1587 | assert (false && "Unhandled RetEffect."); break; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1588 | |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1589 | case RetEffect::NoRet: { |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1590 | |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1591 | // Make up a symbol for the return value (not reference counted). |
Ted Kremenek | b887355 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 1592 | // FIXME: This is basically copy-and-paste from GRSimpleVals. We |
| 1593 | // should compose behavior, not copy it. |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1594 | |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1595 | // FIXME: We eventually should handle structs and other compound types |
| 1596 | // that are returned by value. |
| 1597 | |
| 1598 | QualType T = Ex->getType(); |
| 1599 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 1600 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) { |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1601 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1602 | SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count); |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1603 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1604 | SVal X = Loc::IsLocType(Ex->getType()) |
| 1605 | ? cast<SVal>(loc::SymbolVal(Sym)) |
| 1606 | : cast<SVal>(nonloc::SymbolVal(Sym)); |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1607 | |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1608 | state = state.BindExpr(Ex, X, false); |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1611 | break; |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1612 | } |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1613 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1614 | case RetEffect::Alias: { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1615 | unsigned idx = RE.getIndex(); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1616 | assert (arg_end >= arg_beg); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1617 | assert (idx < (unsigned) (arg_end - arg_beg)); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1618 | SVal V = state.GetSVal(*(arg_beg+idx)); |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1619 | state = state.BindExpr(Ex, V, false); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1620 | break; |
| 1621 | } |
| 1622 | |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1623 | case RetEffect::ReceiverAlias: { |
| 1624 | assert (Receiver); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1625 | SVal V = state.GetSVal(Receiver); |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1626 | state = state.BindExpr(Ex, V, false); |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1627 | break; |
| 1628 | } |
| 1629 | |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1630 | case RetEffect::OwnedAllocatedSymbol: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1631 | case RetEffect::OwnedSymbol: { |
| 1632 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1633 | SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count); |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1634 | QualType RetT = GetReturnType(Ex, Eng.getContext()); |
| 1635 | state = |
| 1636 | state.set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(), RetT)); |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1637 | state = state.BindExpr(Ex, loc::SymbolVal(Sym), false); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1638 | |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1639 | // FIXME: Add a flag to the checker where allocations are allowed to fail. |
Ted Kremenek | b2bf7cd | 2009-01-28 22:27:59 +0000 | [diff] [blame] | 1640 | if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) { |
| 1641 | bool isFeasible; |
| 1642 | state = state.Assume(loc::SymbolVal(Sym), true, isFeasible); |
| 1643 | assert(isFeasible && "Cannot assume fresh symbol is non-null."); |
| 1644 | } |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1645 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1646 | break; |
| 1647 | } |
| 1648 | |
| 1649 | case RetEffect::NotOwnedSymbol: { |
| 1650 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1651 | SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1652 | QualType RetT = GetReturnType(Ex, Eng.getContext()); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1653 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame] | 1654 | state = |
| 1655 | state.set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),RetT)); |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1656 | state = state.BindExpr(Ex, loc::SymbolVal(Sym), false); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1657 | break; |
| 1658 | } |
| 1659 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1660 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1661 | // Is this a sink? |
| 1662 | if (IsEndPath(Summ)) |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1663 | Builder.MakeSinkNode(Dst, Ex, Pred, state); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1664 | else |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1665 | Builder.MakeNode(Dst, Ex, Pred, state); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1669 | void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1670 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1671 | GRStmtNodeBuilder<GRState>& Builder, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1672 | CallExpr* CE, SVal L, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1673 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1674 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1675 | RetainSummary* Summ = !isa<loc::FuncVal>(L) ? 0 |
| 1676 | : Summaries.getSummary(cast<loc::FuncVal>(L).getDecl()); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1677 | |
| 1678 | EvalSummary(Dst, Eng, Builder, CE, 0, Summ, |
| 1679 | CE->arg_begin(), CE->arg_end(), Pred); |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 1680 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1681 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1682 | void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1683 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1684 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1685 | ObjCMessageExpr* ME, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1686 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 1687 | RetainSummary* Summ; |
Ted Kremenek | 9040c65 | 2008-05-01 21:31:50 +0000 | [diff] [blame] | 1688 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1689 | if (Expr* Receiver = ME->getReceiver()) { |
| 1690 | // We need the type-information of the tracked receiver object |
| 1691 | // Retrieve it from the state. |
| 1692 | ObjCInterfaceDecl* ID = 0; |
| 1693 | |
| 1694 | // FIXME: Wouldn't it be great if this code could be reduced? It's just |
| 1695 | // a chain of lookups. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1696 | const GRState* St = Builder.GetState(Pred); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1697 | SVal V = Eng.getStateManager().GetSVal(St, Receiver ); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1698 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1699 | if (isa<loc::SymbolVal>(V)) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1700 | SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol(); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1701 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1702 | if (const RefVal* T = St->get<RefBindings>(Sym)) { |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1703 | QualType Ty = T->getType(); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1704 | |
| 1705 | if (const PointerType* PT = Ty->getAsPointerType()) { |
| 1706 | QualType PointeeTy = PT->getPointeeType(); |
| 1707 | |
| 1708 | if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy)) |
| 1709 | ID = IT->getDecl(); |
| 1710 | } |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | Summ = Summaries.getMethodSummary(ME, ID); |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1715 | |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 1716 | // Special-case: are we sending a mesage to "self"? |
| 1717 | // This is a hack. When we have full-IP this should be removed. |
| 1718 | if (!Summ) { |
| 1719 | ObjCMethodDecl* MD = |
| 1720 | dyn_cast<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl()); |
| 1721 | |
| 1722 | if (MD) { |
| 1723 | if (Expr* Receiver = ME->getReceiver()) { |
| 1724 | SVal X = Eng.getStateManager().GetSVal(St, Receiver); |
| 1725 | if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X)) |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1726 | if (L->getRegion() == Eng.getStateManager().getSelfRegion(St)) { |
| 1727 | // Create a summmary where all of the arguments "StopTracking". |
| 1728 | Summ = Summaries.getPersistentSummary(RetEffect::MakeNoRet(), |
| 1729 | DoNothing, |
| 1730 | StopTracking); |
| 1731 | } |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1735 | } |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1736 | else |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1737 | Summ = Summaries.getClassMethodSummary(ME->getClassName(), |
| 1738 | ME->getSelector()); |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1739 | |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 1740 | EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ, |
| 1741 | ME->arg_begin(), ME->arg_end(), Pred); |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1742 | } |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 1743 | |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1744 | // Stores. |
| 1745 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1746 | void CFRefCount::EvalStore(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1747 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1748 | GRStmtNodeBuilder<GRState>& Builder, |
| 1749 | Expr* E, ExplodedNode<GRState>* Pred, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1750 | const GRState* St, SVal TargetLV, SVal Val) { |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1751 | |
| 1752 | // Check if we have a binding for "Val" and if we are storing it to something |
| 1753 | // we don't understand or otherwise the value "escapes" the function. |
| 1754 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1755 | if (!isa<loc::SymbolVal>(Val)) |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1756 | return; |
| 1757 | |
| 1758 | // Are we storing to something that causes the value to "escape"? |
| 1759 | |
| 1760 | bool escapes = false; |
| 1761 | |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1762 | // A value escapes in three possible cases (this may change): |
| 1763 | // |
| 1764 | // (1) we are binding to something that is not a memory region. |
| 1765 | // (2) we are binding to a memregion that does not have stack storage |
| 1766 | // (3) we are binding to a memregion with stack storage that the store |
| 1767 | // does not understand. |
| 1768 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1769 | SymbolRef Sym = cast<loc::SymbolVal>(Val).getSymbol(); |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1770 | GRStateRef state(St, Eng.getStateManager()); |
| 1771 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1772 | if (!isa<loc::MemRegionVal>(TargetLV)) |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1773 | escapes = true; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1774 | else { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 1775 | const MemRegion* R = cast<loc::MemRegionVal>(TargetLV).getRegion(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1776 | escapes = !Eng.getStateManager().hasStackStorage(R); |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1777 | |
| 1778 | if (!escapes) { |
| 1779 | // To test (3), generate a new state with the binding removed. If it is |
| 1780 | // the same state, then it escapes (since the store cannot represent |
| 1781 | // the binding). |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1782 | GRStateRef stateNew = state.BindLoc(cast<Loc>(TargetLV), Val); |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1783 | escapes = (stateNew == state); |
| 1784 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1785 | } |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1786 | |
| 1787 | if (!escapes) |
| 1788 | return; |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1789 | |
| 1790 | // Do we have a reference count binding? |
| 1791 | // FIXME: Is this step even needed? We do blow away the binding anyway. |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1792 | if (!state.get<RefBindings>(Sym)) |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1793 | return; |
| 1794 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1795 | // Nuke the binding. |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1796 | state = state.remove<RefBindings>(Sym); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1797 | |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1798 | // Hand of the remaining logic to the parent implementation. |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1799 | GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, state, TargetLV, Val); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1802 | // End-of-path. |
| 1803 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1804 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1805 | std::pair<GRStateRef,bool> |
| 1806 | CFRefCount::HandleSymbolDeath(GRStateManager& VMgr, |
| 1807 | const GRState* St, const Decl* CD, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1808 | SymbolRef sid, |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1809 | RefVal V, bool& hasLeak) { |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1810 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1811 | GRStateRef state(St, VMgr); |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1812 | assert ((!V.isReturnedOwned() || CD) && |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1813 | "CodeDecl must be available for reporting ReturnOwned errors."); |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 1814 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1815 | if (V.isReturnedOwned() && V.getCount() == 0) |
| 1816 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1817 | std::string s = MD->getSelector().getAsString(); |
Ted Kremenek | 4c79e55 | 2008-11-05 16:54:44 +0000 | [diff] [blame] | 1818 | if (!followsReturnRule(s.c_str())) { |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1819 | hasLeak = true; |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1820 | state = state.set<RefBindings>(sid, V ^ RefVal::ErrorLeakReturned); |
| 1821 | return std::make_pair(state, true); |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1822 | } |
| 1823 | } |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 1824 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1825 | // All other cases. |
| 1826 | |
| 1827 | hasLeak = V.isOwned() || |
| 1828 | ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1829 | |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1830 | if (!hasLeak) |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1831 | return std::make_pair(state.remove<RefBindings>(sid), false); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1832 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1833 | return std::make_pair(state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak), |
| 1834 | false); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1837 | |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1838 | |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1839 | // Dead symbols. |
| 1840 | |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 1841 | |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1842 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1843 | // Return statements. |
| 1844 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1845 | void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1846 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1847 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1848 | ReturnStmt* S, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1849 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1850 | |
| 1851 | Expr* RetE = S->getRetValue(); |
| 1852 | if (!RetE) return; |
| 1853 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1854 | GRStateRef state(Builder.GetState(Pred), Eng.getStateManager()); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1855 | SVal V = state.GetSVal(RetE); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1856 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1857 | if (!isa<loc::SymbolVal>(V)) |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1858 | return; |
| 1859 | |
| 1860 | // Get the reference count binding (if any). |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1861 | SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol(); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1862 | const RefVal* T = state.get<RefBindings>(Sym); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1863 | |
| 1864 | if (!T) |
| 1865 | return; |
| 1866 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1867 | // Change the reference count. |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1868 | RefVal X = *T; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1869 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1870 | switch (X.getKind()) { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1871 | case RefVal::Owned: { |
| 1872 | unsigned cnt = X.getCount(); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1873 | assert (cnt > 0); |
| 1874 | X = RefVal::makeReturnedOwned(cnt - 1); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1875 | break; |
| 1876 | } |
| 1877 | |
| 1878 | case RefVal::NotOwned: { |
| 1879 | unsigned cnt = X.getCount(); |
| 1880 | X = cnt ? RefVal::makeReturnedOwned(cnt - 1) |
| 1881 | : RefVal::makeReturnedNotOwned(); |
| 1882 | break; |
| 1883 | } |
| 1884 | |
| 1885 | default: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1886 | return; |
| 1887 | } |
| 1888 | |
| 1889 | // Update the binding. |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1890 | state = state.set<RefBindings>(Sym, X); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1891 | Builder.MakeNode(Dst, S, Pred, state); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1894 | // Assumptions. |
| 1895 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1896 | const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr, |
| 1897 | const GRState* St, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1898 | SVal Cond, bool Assumption, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 1899 | bool& isFeasible) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1900 | |
| 1901 | // FIXME: We may add to the interface of EvalAssume the list of symbols |
| 1902 | // whose assumptions have changed. For now we just iterate through the |
| 1903 | // bindings and check if any of the tracked symbols are NULL. This isn't |
| 1904 | // too bad since the number of symbols we will track in practice are |
| 1905 | // probably small and EvalAssume is only called at branches and a few |
| 1906 | // other places. |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1907 | RefBindings B = St->get<RefBindings>(); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1908 | |
| 1909 | if (B.isEmpty()) |
| 1910 | return St; |
| 1911 | |
| 1912 | bool changed = false; |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1913 | |
| 1914 | GRStateRef state(St, VMgr); |
| 1915 | RefBindings::Factory& RefBFactory = state.get_context<RefBindings>(); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1916 | |
| 1917 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1918 | // Check if the symbol is null (or equal to any constant). |
| 1919 | // If this is the case, stop tracking the symbol. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 1920 | if (VMgr.getSymVal(St, I.getKey())) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1921 | changed = true; |
| 1922 | B = RefBFactory.Remove(B, I.getKey()); |
| 1923 | } |
| 1924 | } |
| 1925 | |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1926 | if (changed) |
| 1927 | state = state.set<RefBindings>(B); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1928 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1929 | return state; |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1930 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1931 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1932 | RefBindings CFRefCount::Update(RefBindings B, SymbolRef sym, |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1933 | RefVal V, ArgEffect E, |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1934 | RefVal::Kind& hasErr, |
| 1935 | RefBindings::Factory& RefBFactory) { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1936 | |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1937 | // FIXME: This dispatch can potentially be sped up by unifiying it into |
| 1938 | // a single switch statement. Opt for simplicity for now. |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1939 | |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1940 | switch (E) { |
| 1941 | default: |
| 1942 | assert (false && "Unhandled CFRef transition."); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1943 | |
| 1944 | case MayEscape: |
| 1945 | if (V.getKind() == RefVal::Owned) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1946 | V = V ^ RefVal::NotOwned; |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1947 | break; |
| 1948 | } |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1949 | // Fall-through. |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1950 | case DoNothingByRef: |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1951 | case DoNothing: |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1952 | if (!isGCEnabled() && V.getKind() == RefVal::Released) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1953 | V = V ^ RefVal::ErrorUseAfterRelease; |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1954 | hasErr = V.getKind(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1955 | break; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1956 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1957 | return B; |
Ted Kremenek | e19f449 | 2008-06-30 16:57:41 +0000 | [diff] [blame] | 1958 | |
Ted Kremenek | abf4397 | 2009-01-28 21:44:40 +0000 | [diff] [blame] | 1959 | case Autorelease: |
| 1960 | if (isGCEnabled()) return B; |
| 1961 | // Fall-through. |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1962 | case StopTracking: |
| 1963 | return RefBFactory.Remove(B, sym); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1964 | |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1965 | case IncRef: |
| 1966 | switch (V.getKind()) { |
| 1967 | default: |
| 1968 | assert(false); |
| 1969 | |
| 1970 | case RefVal::Owned: |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1971 | case RefVal::NotOwned: |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1972 | V = V + 1; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1973 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1974 | case RefVal::Released: |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1975 | if (isGCEnabled()) |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1976 | V = V ^ RefVal::Owned; |
Ted Kremenek | 65c9165 | 2008-04-29 05:44:10 +0000 | [diff] [blame] | 1977 | else { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1978 | V = V ^ RefVal::ErrorUseAfterRelease; |
Ted Kremenek | 65c9165 | 2008-04-29 05:44:10 +0000 | [diff] [blame] | 1979 | hasErr = V.getKind(); |
| 1980 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1981 | break; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1982 | } |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1983 | break; |
| 1984 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1985 | case SelfOwn: |
| 1986 | V = V ^ RefVal::NotOwned; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1987 | // Fall-through. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1988 | case DecRef: |
| 1989 | switch (V.getKind()) { |
| 1990 | default: |
| 1991 | assert (false); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1992 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1993 | case RefVal::Owned: |
| 1994 | V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1995 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1996 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1997 | case RefVal::NotOwned: |
| 1998 | if (V.getCount() > 0) |
| 1999 | V = V - 1; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 2000 | else { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2001 | V = V ^ RefVal::ErrorReleaseNotOwned; |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2002 | hasErr = V.getKind(); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2003 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2004 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2005 | |
| 2006 | case RefVal::Released: |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2007 | V = V ^ RefVal::ErrorUseAfterRelease; |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2008 | hasErr = V.getKind(); |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2009 | break; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2010 | } |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 2011 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2012 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2013 | return RefBFactory.Add(B, sym, V); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2016 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2017 | // Error reporting. |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2018 | //===----------------------------------------------------------------------===// |
| 2019 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2020 | namespace { |
| 2021 | |
| 2022 | //===-------------===// |
| 2023 | // Bug Descriptions. // |
| 2024 | //===-------------===// |
| 2025 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2026 | class VISIBILITY_HIDDEN CFRefBug : public BugType { |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2027 | protected: |
| 2028 | CFRefCount& TF; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2029 | |
| 2030 | CFRefBug(CFRefCount* tf, const char* name) |
| 2031 | : BugType(name, "Memory (Core Foundation/Objective-C)"), TF(*tf) {} |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2032 | public: |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2033 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 2034 | CFRefCount& getTF() { return TF; } |
Ted Kremenek | 789deac | 2008-05-05 23:16:31 +0000 | [diff] [blame] | 2035 | const CFRefCount& getTF() const { return TF; } |
| 2036 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2037 | // FIXME: Eventually remove. |
| 2038 | virtual const char* getDescription() const = 0; |
| 2039 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2040 | virtual bool isLeak() const { return false; } |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2041 | }; |
| 2042 | |
| 2043 | class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug { |
| 2044 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2045 | UseAfterRelease(CFRefCount* tf) |
| 2046 | : CFRefBug(tf, "use-after-release") {} |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2047 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2048 | const char* getDescription() const { |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2049 | return "Reference-counted object is used after it is released."; |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 2050 | } |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2051 | }; |
| 2052 | |
| 2053 | class VISIBILITY_HIDDEN BadRelease : public CFRefBug { |
| 2054 | public: |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2055 | BadRelease(CFRefCount* tf) : CFRefBug(tf, "bad release") {} |
| 2056 | |
| 2057 | const char* getDescription() const { |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2058 | return "Incorrect decrement of the reference count of a " |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2059 | "CoreFoundation object: " |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2060 | "The object is not owned at this point by the caller."; |
| 2061 | } |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2062 | }; |
| 2063 | |
| 2064 | class VISIBILITY_HIDDEN Leak : public CFRefBug { |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2065 | const bool isReturn; |
| 2066 | protected: |
| 2067 | Leak(CFRefCount* tf, const char* name, bool isRet) |
| 2068 | : CFRefBug(tf, name), isReturn(isRet) {} |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2069 | public: |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2070 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2071 | // FIXME: Remove once reports have better descriptions. |
| 2072 | const char* getDescription() const { return "leak"; } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2073 | |
Ted Kremenek | e45e57f | 2009-02-05 00:38:00 +0000 | [diff] [blame] | 2074 | bool isLeak() const { return true; } |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2075 | }; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2076 | |
| 2077 | class VISIBILITY_HIDDEN LeakAtReturn : public Leak { |
| 2078 | public: |
| 2079 | LeakAtReturn(CFRefCount* tf, const char* name) |
| 2080 | : Leak(tf, name, true) {} |
| 2081 | }; |
| 2082 | |
| 2083 | class VISIBILITY_HIDDEN LeakWithinFunction : public Leak { |
| 2084 | public: |
| 2085 | LeakWithinFunction(CFRefCount* tf, const char* name) |
| 2086 | : Leak(tf, name, false) {} |
| 2087 | }; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2088 | |
| 2089 | //===---------===// |
| 2090 | // Bug Reports. // |
| 2091 | //===---------===// |
| 2092 | |
| 2093 | class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport { |
Ted Kremenek | 66d9706 | 2009-02-07 22:04:05 +0000 | [diff] [blame] | 2094 | protected: |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2095 | SymbolRef Sym; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2096 | public: |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2097 | CFRefReport(CFRefBug& D, ExplodedNode<GRState> *n, SymbolRef sym) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2098 | : RangedBugReport(D, D.getDescription(), n), Sym(sym) {} |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2099 | |
| 2100 | virtual ~CFRefReport() {} |
| 2101 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 2102 | CFRefBug& getBugType() { |
| 2103 | return (CFRefBug&) RangedBugReport::getBugType(); |
| 2104 | } |
| 2105 | const CFRefBug& getBugType() const { |
| 2106 | return (const CFRefBug&) RangedBugReport::getBugType(); |
| 2107 | } |
| 2108 | |
| 2109 | virtual void getRanges(BugReporter& BR, const SourceRange*& beg, |
| 2110 | const SourceRange*& end) { |
| 2111 | |
Ted Kremenek | e92c1b2 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 2112 | if (!getBugType().isLeak()) |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 2113 | RangedBugReport::getRanges(BR, beg, end); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2114 | else |
| 2115 | beg = end = 0; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2118 | SymbolRef getSymbol() const { return Sym; } |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2119 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2120 | PathDiagnosticPiece* getEndPath(BugReporter& BR, |
| 2121 | const ExplodedNode<GRState>* N); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2122 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2123 | std::pair<const char**,const char**> getExtraDescriptiveText(); |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2124 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2125 | PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState>* N, |
| 2126 | const ExplodedNode<GRState>* PrevN, |
| 2127 | const ExplodedGraph<GRState>& G, |
| 2128 | BugReporter& BR); |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2129 | }; |
| 2130 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2131 | class VISIBILITY_HIDDEN CFRefLeakReport : public CFRefReport { |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2132 | SourceLocation AllocSite; |
| 2133 | const MemRegion* AllocBinding; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2134 | public: |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2135 | CFRefLeakReport(CFRefBug& D, ExplodedNode<GRState> *n, SymbolRef sym, |
| 2136 | GRStateManager& StateMgr); |
Ted Kremenek | 66d9706 | 2009-02-07 22:04:05 +0000 | [diff] [blame] | 2137 | |
| 2138 | PathDiagnosticPiece* getEndPath(BugReporter& BR, |
| 2139 | const ExplodedNode<GRState>* N); |
| 2140 | |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2141 | SourceLocation getLocation() const { return AllocSite; } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2142 | }; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2143 | } // end anonymous namespace |
| 2144 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2145 | void CFRefCount::RegisterChecks(BugReporter& BR) { |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 2146 | useAfterRelease = new UseAfterRelease(this); |
| 2147 | BR.Register(useAfterRelease); |
| 2148 | |
| 2149 | releaseNotOwned = new BadRelease(this); |
| 2150 | BR.Register(releaseNotOwned); |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2151 | |
| 2152 | // First register "return" leaks. |
| 2153 | const char* name = 0; |
| 2154 | |
| 2155 | if (isGCEnabled()) |
| 2156 | name = "[naming convention] leak of returned object (GC)"; |
| 2157 | else if (getLangOptions().getGCMode() == LangOptions::HybridGC) |
| 2158 | name = "[naming convention] leak of returned object (hybrid MM, " |
| 2159 | "non-GC)"; |
| 2160 | else { |
| 2161 | assert(getLangOptions().getGCMode() == LangOptions::NonGC); |
| 2162 | name = "[naming convention] leak of returned object"; |
| 2163 | } |
| 2164 | |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 2165 | leakAtReturn = new LeakAtReturn(this, name); |
| 2166 | BR.Register(leakAtReturn); |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2167 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2168 | // Second, register leaks within a function/method. |
| 2169 | if (isGCEnabled()) |
| 2170 | name = "leak (GC)"; |
| 2171 | else if (getLangOptions().getGCMode() == LangOptions::HybridGC) |
| 2172 | name = "leak (hybrid MM, non-GC)"; |
| 2173 | else { |
| 2174 | assert(getLangOptions().getGCMode() == LangOptions::NonGC); |
| 2175 | name = "leak"; |
| 2176 | } |
| 2177 | |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 2178 | leakWithinFunction = new LeakWithinFunction(this, name); |
| 2179 | BR.Register(leakWithinFunction); |
| 2180 | |
| 2181 | // Save the reference to the BugReporter. |
| 2182 | this->BR = &BR; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2183 | } |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2184 | |
| 2185 | static const char* Msgs[] = { |
| 2186 | "Code is compiled in garbage collection only mode" // GC only |
| 2187 | " (the bug occurs with garbage collection enabled).", |
| 2188 | |
| 2189 | "Code is compiled without garbage collection.", // No GC. |
| 2190 | |
| 2191 | "Code is compiled for use with and without garbage collection (GC)." |
| 2192 | " The bug occurs with GC enabled.", // Hybrid, with GC. |
| 2193 | |
| 2194 | "Code is compiled for use with and without garbage collection (GC)." |
| 2195 | " The bug occurs in non-GC mode." // Hyrbird, without GC/ |
| 2196 | }; |
| 2197 | |
| 2198 | std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() { |
| 2199 | CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF(); |
| 2200 | |
| 2201 | switch (TF.getLangOptions().getGCMode()) { |
| 2202 | default: |
| 2203 | assert(false); |
Ted Kremenek | 31593ac | 2008-05-01 04:02:04 +0000 | [diff] [blame] | 2204 | |
| 2205 | case LangOptions::GCOnly: |
| 2206 | assert (TF.isGCEnabled()); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2207 | return std::make_pair(&Msgs[0], &Msgs[0]+1); |
| 2208 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2209 | case LangOptions::NonGC: |
| 2210 | assert (!TF.isGCEnabled()); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2211 | return std::make_pair(&Msgs[1], &Msgs[1]+1); |
| 2212 | |
| 2213 | case LangOptions::HybridGC: |
| 2214 | if (TF.isGCEnabled()) |
| 2215 | return std::make_pair(&Msgs[2], &Msgs[2]+1); |
| 2216 | else |
| 2217 | return std::make_pair(&Msgs[3], &Msgs[3]+1); |
| 2218 | } |
| 2219 | } |
| 2220 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2221 | PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode<GRState>* N, |
| 2222 | const ExplodedNode<GRState>* PrevN, |
| 2223 | const ExplodedGraph<GRState>& G, |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2224 | BugReporter& BR) { |
| 2225 | |
Ted Kremenek | 611a15a | 2009-01-28 05:29:13 +0000 | [diff] [blame] | 2226 | // Check if the type state has changed. |
| 2227 | GRStateManager &StMgr = cast<GRBugReporter>(BR).getStateManager(); |
| 2228 | GRStateRef PrevSt(PrevN->getState(), StMgr); |
| 2229 | GRStateRef CurrSt(N->getState(), StMgr); |
Ted Kremenek | 2098280 | 2009-01-28 05:06:46 +0000 | [diff] [blame] | 2230 | |
Ted Kremenek | 611a15a | 2009-01-28 05:29:13 +0000 | [diff] [blame] | 2231 | const RefVal* CurrT = CurrSt.get<RefBindings>(Sym); |
| 2232 | if (!CurrT) return NULL; |
| 2233 | |
| 2234 | const RefVal& CurrV = *CurrT; |
| 2235 | const RefVal* PrevT = PrevSt.get<RefBindings>(Sym); |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2236 | |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2237 | if (!PrevT) { |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2238 | std::string sbuf; |
| 2239 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2240 | |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2241 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 2242 | |
Ted Kremenek | 5c1cd52 | 2009-01-28 05:15:02 +0000 | [diff] [blame] | 2243 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 2244 | // Get the name of the callee (if it is available). |
| 2245 | SVal X = CurrSt.GetSVal(CE->getCallee()); |
| 2246 | if (loc::FuncVal* FV = dyn_cast<loc::FuncVal>(&X)) |
| 2247 | os << "Call to function '" << FV->getDecl()->getNameAsString() <<'\''; |
| 2248 | else |
Ted Kremenek | a102c0c | 2009-01-28 06:01:42 +0000 | [diff] [blame] | 2249 | os << "function call"; |
Ted Kremenek | 5c1cd52 | 2009-01-28 05:15:02 +0000 | [diff] [blame] | 2250 | } |
| 2251 | else { |
| 2252 | assert (isa<ObjCMessageExpr>(S)); |
Ted Kremenek | a102c0c | 2009-01-28 06:01:42 +0000 | [diff] [blame] | 2253 | os << "Method"; |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2254 | } |
Ted Kremenek | 5c1cd52 | 2009-01-28 05:15:02 +0000 | [diff] [blame] | 2255 | |
Ted Kremenek | 961b61d | 2009-01-28 06:06:36 +0000 | [diff] [blame] | 2256 | if (CurrV.getObjKind() == RetEffect::CF) { |
| 2257 | os << " returns a Core Foundation object with a "; |
| 2258 | } |
| 2259 | else { |
| 2260 | assert (CurrV.getObjKind() == RetEffect::ObjC); |
| 2261 | os << " returns an Objective-C object with a "; |
| 2262 | } |
Ted Kremenek | a102c0c | 2009-01-28 06:01:42 +0000 | [diff] [blame] | 2263 | |
Ted Kremenek | 23b8eaa | 2009-01-28 06:25:48 +0000 | [diff] [blame] | 2264 | if (CurrV.isOwned()) { |
| 2265 | os << "+1 retain count (owning reference)."; |
| 2266 | |
| 2267 | if (static_cast<CFRefBug&>(getBugType()).getTF().isGCEnabled()) { |
| 2268 | assert(CurrV.getObjKind() == RetEffect::CF); |
| 2269 | os << " " |
| 2270 | "Core Foundation objects are not automatically garbage collected."; |
| 2271 | } |
| 2272 | } |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2273 | else { |
| 2274 | assert (CurrV.isNotOwned()); |
Ted Kremenek | 5c1cd52 | 2009-01-28 05:15:02 +0000 | [diff] [blame] | 2275 | os << "+0 retain count (non-owning reference)."; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2276 | } |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2277 | |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2278 | FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2279 | PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str()); |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2280 | |
| 2281 | if (Expr* Exp = dyn_cast<Expr>(S)) |
| 2282 | P->addRange(Exp->getSourceRange()); |
| 2283 | |
| 2284 | return P; |
| 2285 | } |
| 2286 | |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2287 | // Determine if the typestate has changed. |
Ted Kremenek | 611a15a | 2009-01-28 05:29:13 +0000 | [diff] [blame] | 2288 | RefVal PrevV = *PrevT; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2289 | |
| 2290 | if (PrevV == CurrV) |
| 2291 | return NULL; |
| 2292 | |
| 2293 | // The typestate has changed. |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2294 | std::string sbuf; |
| 2295 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2296 | |
| 2297 | switch (CurrV.getKind()) { |
| 2298 | case RefVal::Owned: |
| 2299 | case RefVal::NotOwned: |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 2300 | |
| 2301 | if (PrevV.getCount() == CurrV.getCount()) |
| 2302 | return 0; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2303 | |
| 2304 | if (PrevV.getCount() > CurrV.getCount()) |
| 2305 | os << "Reference count decremented."; |
| 2306 | else |
| 2307 | os << "Reference count incremented."; |
| 2308 | |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 2309 | if (unsigned Count = CurrV.getCount()) { |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2310 | os << " Object has +" << Count; |
Ted Kremenek | 79c140b | 2008-04-18 05:32:44 +0000 | [diff] [blame] | 2311 | |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2312 | if (Count > 1) |
| 2313 | os << " retain counts."; |
Ted Kremenek | 79c140b | 2008-04-18 05:32:44 +0000 | [diff] [blame] | 2314 | else |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2315 | os << " retain count."; |
Ted Kremenek | 79c140b | 2008-04-18 05:32:44 +0000 | [diff] [blame] | 2316 | } |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2317 | |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2318 | break; |
| 2319 | |
| 2320 | case RefVal::Released: |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2321 | os << "Object released."; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2322 | break; |
| 2323 | |
| 2324 | case RefVal::ReturnedOwned: |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2325 | os << "Object returned to caller as an owning reference (single retain " |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2326 | "count transferred to caller)."; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2327 | break; |
| 2328 | |
| 2329 | case RefVal::ReturnedNotOwned: |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2330 | os << "Object returned to caller with a +0 (non-owning) retain count."; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2331 | break; |
| 2332 | |
| 2333 | default: |
| 2334 | return NULL; |
| 2335 | } |
| 2336 | |
| 2337 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 2338 | FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2339 | PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str()); |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2340 | |
| 2341 | // Add the range by scanning the children of the statement for any bindings |
| 2342 | // to Sym. |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2343 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 2344 | if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) { |
Ted Kremenek | 2098280 | 2009-01-28 05:06:46 +0000 | [diff] [blame] | 2345 | SVal X = CurrSt.GetSVal(Exp); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2346 | if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&X)) |
Ted Kremenek | 2098280 | 2009-01-28 05:06:46 +0000 | [diff] [blame] | 2347 | if (SV->getSymbol() == Sym) P->addRange(Exp->getSourceRange()); break; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | return P; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2353 | namespace { |
| 2354 | class VISIBILITY_HIDDEN FindUniqueBinding : |
| 2355 | public StoreManager::BindingsHandler { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2356 | SymbolRef Sym; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2357 | MemRegion* Binding; |
| 2358 | bool First; |
| 2359 | |
| 2360 | public: |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2361 | FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {} |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2362 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2363 | bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal val) { |
| 2364 | if (const loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&val)) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2365 | if (SV->getSymbol() != Sym) |
| 2366 | return true; |
| 2367 | } |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2368 | else if (const nonloc::SymbolVal* SV=dyn_cast<nonloc::SymbolVal>(&val)) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2369 | if (SV->getSymbol() != Sym) |
| 2370 | return true; |
| 2371 | } |
| 2372 | else |
| 2373 | return true; |
| 2374 | |
| 2375 | if (Binding) { |
| 2376 | First = false; |
| 2377 | return false; |
| 2378 | } |
| 2379 | else |
| 2380 | Binding = R; |
| 2381 | |
| 2382 | return true; |
| 2383 | } |
| 2384 | |
| 2385 | operator bool() { return First && Binding; } |
| 2386 | MemRegion* getRegion() { return Binding; } |
| 2387 | }; |
| 2388 | } |
| 2389 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2390 | static std::pair<const ExplodedNode<GRState>*,const MemRegion*> |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2391 | GetAllocationSite(GRStateManager& StateMgr, const ExplodedNode<GRState>* N, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2392 | SymbolRef Sym) { |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2393 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2394 | // Find both first node that referred to the tracked symbol and the |
| 2395 | // memory location that value was store to. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2396 | const ExplodedNode<GRState>* Last = N; |
| 2397 | const MemRegion* FirstBinding = 0; |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2398 | |
| 2399 | while (N) { |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2400 | const GRState* St = N->getState(); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2401 | RefBindings B = St->get<RefBindings>(); |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2402 | |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2403 | if (!B.lookup(Sym)) |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2404 | break; |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2405 | |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2406 | FindUniqueBinding FB(Sym); |
| 2407 | StateMgr.iterBindings(St, FB); |
| 2408 | if (FB) FirstBinding = FB.getRegion(); |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2409 | |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2410 | Last = N; |
| 2411 | N = N->pred_empty() ? NULL : *(N->pred_begin()); |
| 2412 | } |
| 2413 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2414 | return std::make_pair(Last, FirstBinding); |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2415 | } |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2416 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2417 | PathDiagnosticPiece* |
| 2418 | CFRefReport::getEndPath(BugReporter& br, const ExplodedNode<GRState>* EndN) { |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 2419 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2420 | GRBugReporter& BR = cast<GRBugReporter>(br); |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 2421 | // Tell the BugReporter to report cases when the tracked symbol is |
| 2422 | // assigned to different variables, etc. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 2423 | cast<GRBugReporter>(BR).addNotableSymbol(Sym); |
Ted Kremenek | 66d9706 | 2009-02-07 22:04:05 +0000 | [diff] [blame] | 2424 | return RangedBugReport::getEndPath(BR, EndN); |
| 2425 | } |
| 2426 | |
| 2427 | PathDiagnosticPiece* |
| 2428 | CFRefLeakReport::getEndPath(BugReporter& br, const ExplodedNode<GRState>* EndN){ |
| 2429 | |
| 2430 | GRBugReporter& BR = cast<GRBugReporter>(br); |
| 2431 | // Tell the BugReporter to report cases when the tracked symbol is |
| 2432 | // assigned to different variables, etc. |
| 2433 | cast<GRBugReporter>(BR).addNotableSymbol(Sym); |
| 2434 | |
| 2435 | // We are reporting a leak. Walk up the graph to get to the first node where |
| 2436 | // the symbol appeared, and also get the first VarDecl that tracked object |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2437 | // is stored to. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2438 | const ExplodedNode<GRState>* AllocNode = 0; |
| 2439 | const MemRegion* FirstBinding = 0; |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2440 | |
| 2441 | llvm::tie(AllocNode, FirstBinding) = |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2442 | GetAllocationSite(BR.getStateManager(), EndN, Sym); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2443 | |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2444 | // Get the allocate site. |
| 2445 | assert (AllocNode); |
| 2446 | Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt(); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2447 | |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2448 | SourceManager& SMgr = BR.getContext().getSourceManager(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 2449 | unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart()); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2450 | |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2451 | // Get the leak site. We may have multiple ExplodedNodes (one with the |
| 2452 | // leak) that occur on the same line number; if the node with the leak |
| 2453 | // has any immediate predecessor nodes with the same line number, find |
| 2454 | // any transitive-successors that have a different statement and use that |
| 2455 | // line number instead. This avoids emiting a diagnostic like: |
| 2456 | // |
| 2457 | // // 'y' is leaked. |
| 2458 | // int x = foo(y); |
| 2459 | // |
| 2460 | // instead we want: |
| 2461 | // |
| 2462 | // int x = foo(y); |
| 2463 | // // 'y' is leaked. |
| 2464 | |
| 2465 | Stmt* S = getStmt(BR); // This is the statement where the leak occured. |
| 2466 | assert (S); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 2467 | unsigned EndLine = SMgr.getInstantiationLineNumber(S->getLocStart()); |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2468 | |
| 2469 | // Look in the *trimmed* graph at the immediate predecessor of EndN. Does |
| 2470 | // it occur on the same line? |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2471 | PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above; |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2472 | |
| 2473 | assert (!EndN->pred_empty()); // Not possible to have 0 predecessors. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2474 | const ExplodedNode<GRState> *Pred = *(EndN->pred_begin()); |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2475 | ProgramPoint PredPos = Pred->getLocation(); |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2476 | |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2477 | if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) { |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2478 | |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2479 | Stmt* SPred = PredPS->getStmt(); |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2480 | |
| 2481 | // Predecessor at same line? |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 2482 | if (SMgr.getInstantiationLineNumber(SPred->getLocStart()) != EndLine) { |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2483 | Hint = PathDiagnosticPiece::Below; |
| 2484 | S = SPred; |
| 2485 | } |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2486 | } |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2487 | |
| 2488 | // Generate the diagnostic. |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2489 | FullSourceLoc L( S->getLocStart(), SMgr); |
Ted Kremenek | c9e3d86 | 2009-02-07 21:59:45 +0000 | [diff] [blame] | 2490 | std::string sbuf; |
| 2491 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | e92c1b2 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 2492 | |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2493 | os << "Object allocated on line " << AllocLine; |
Ted Kremenek | e92c1b2 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 2494 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2495 | if (FirstBinding) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2496 | os << " and stored into '" << FirstBinding->getString() << '\''; |
| 2497 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2498 | // Get the retain count. |
| 2499 | const RefVal* RV = EndN->getState()->get<RefBindings>(Sym); |
| 2500 | |
| 2501 | if (RV->getKind() == RefVal::ErrorLeakReturned) { |
Ted Kremenek | 04f9d46 | 2008-12-02 01:26:07 +0000 | [diff] [blame] | 2502 | // FIXME: Per comments in rdar://6320065, "create" only applies to CF |
| 2503 | // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership |
| 2504 | // to the caller for NS objects. |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2505 | ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BR.getGraph().getCodeDecl()); |
| 2506 | os << " is returned from a method whose name ('" |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2507 | << MD.getSelector().getAsString() |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 2508 | << "') does not contain 'copy' or otherwise starts with" |
Ted Kremenek | 9d1d570 | 2008-10-24 21:22:44 +0000 | [diff] [blame] | 2509 | " 'new' or 'alloc'. This violates the naming convention rules given" |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2510 | " in the Memory Management Guide for Cocoa (object leaked)."; |
| 2511 | } |
| 2512 | else |
Ted Kremenek | 9d1d570 | 2008-10-24 21:22:44 +0000 | [diff] [blame] | 2513 | os << " is no longer referenced after this point and has a retain count of" |
| 2514 | " +" |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2515 | << RV->getCount() << " (object leaked)."; |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2516 | |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2517 | return new PathDiagnosticPiece(L, os.str(), Hint); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Ted Kremenek | 989d519 | 2008-04-17 23:43:50 +0000 | [diff] [blame] | 2520 | |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2521 | CFRefLeakReport::CFRefLeakReport(CFRefBug& D, ExplodedNode<GRState> *n, |
| 2522 | SymbolRef sym, GRStateManager& StateMgr) |
| 2523 | : CFRefReport(D, n, sym) |
| 2524 | { |
| 2525 | |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2526 | // Most bug reports are cached at the location where they occured. |
| 2527 | // With leaks, we want to unique them by the location where they were |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2528 | // allocated, and only report a single path. To do this, we need to find |
| 2529 | // the allocation site of a piece of tracked memory, which we do via a |
| 2530 | // call to GetAllocationSite. This will walk the ExplodedGraph backwards. |
| 2531 | // Note that this is *not* the trimmed graph; we are guaranteed, however, |
| 2532 | // that all ancestor nodes that represent the allocation site have the |
| 2533 | // same SourceLocation. |
| 2534 | const ExplodedNode<GRState>* AllocNode = 0; |
| 2535 | |
| 2536 | llvm::tie(AllocNode, AllocBinding) = // Set AllocBinding. |
| 2537 | GetAllocationSite(StateMgr, getEndNode(), getSymbol()); |
| 2538 | |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 2539 | ProgramPoint P = |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2540 | GetAllocationSite(StateMgr, getEndNode(), getSymbol()).first->getLocation(); |
| 2541 | |
| 2542 | // Get the SourceLocation for the allocation site. |
| 2543 | AllocSite = cast<PostStmt>(P).getStmt()->getLocStart(); |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2544 | } |
| 2545 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2546 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 2547 | // Handle dead symbols and end-of-path. |
| 2548 | //===----------------------------------------------------------------------===// |
| 2549 | |
| 2550 | void CFRefCount::EvalEndPath(GRExprEngine& Eng, |
| 2551 | GREndPathNodeBuilder<GRState>& Builder) { |
| 2552 | |
| 2553 | const GRState* St = Builder.getState(); |
| 2554 | RefBindings B = St->get<RefBindings>(); |
| 2555 | |
| 2556 | llvm::SmallVector<std::pair<SymbolRef, bool>, 10> Leaked; |
| 2557 | const Decl* CodeDecl = &Eng.getGraph().getCodeDecl(); |
| 2558 | |
| 2559 | for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 2560 | bool hasLeak = false; |
| 2561 | |
| 2562 | std::pair<GRStateRef, bool> X = |
| 2563 | HandleSymbolDeath(Eng.getStateManager(), St, CodeDecl, |
| 2564 | (*I).first, (*I).second, hasLeak); |
| 2565 | |
| 2566 | St = X.first; |
| 2567 | if (hasLeak) Leaked.push_back(std::make_pair((*I).first, X.second)); |
| 2568 | } |
| 2569 | |
| 2570 | if (Leaked.empty()) |
| 2571 | return; |
| 2572 | |
| 2573 | ExplodedNode<GRState>* N = Builder.MakeNode(St); |
| 2574 | |
| 2575 | if (!N) |
| 2576 | return; |
| 2577 | |
| 2578 | for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator |
| 2579 | I = Leaked.begin(), E = Leaked.end(); I != E; ++I) { |
| 2580 | |
| 2581 | CFRefBug *BT = static_cast<CFRefBug*>(I->second ? leakAtReturn |
| 2582 | : leakWithinFunction); |
| 2583 | assert(BT && "BugType not initialized."); |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2584 | CFRefLeakReport* report = new CFRefLeakReport(*BT, N, I->first, |
| 2585 | Eng.getStateManager()); |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 2586 | BR->EmitReport(report); |
| 2587 | } |
| 2588 | } |
| 2589 | |
| 2590 | void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst, |
| 2591 | GRExprEngine& Eng, |
| 2592 | GRStmtNodeBuilder<GRState>& Builder, |
| 2593 | ExplodedNode<GRState>* Pred, |
| 2594 | Stmt* S, |
| 2595 | const GRState* St, |
| 2596 | SymbolReaper& SymReaper) { |
| 2597 | |
| 2598 | // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor. |
| 2599 | |
| 2600 | RefBindings B = St->get<RefBindings>(); |
| 2601 | llvm::SmallVector<std::pair<SymbolRef,bool>, 10> Leaked; |
| 2602 | |
| 2603 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
| 2604 | E = SymReaper.dead_end(); I != E; ++I) { |
| 2605 | |
| 2606 | const RefVal* T = B.lookup(*I); |
| 2607 | if (!T) continue; |
| 2608 | |
| 2609 | bool hasLeak = false; |
| 2610 | |
| 2611 | std::pair<GRStateRef, bool> X |
| 2612 | = HandleSymbolDeath(Eng.getStateManager(), St, 0, *I, *T, hasLeak); |
| 2613 | |
| 2614 | St = X.first; |
| 2615 | |
| 2616 | if (hasLeak) |
| 2617 | Leaked.push_back(std::make_pair(*I,X.second)); |
| 2618 | } |
| 2619 | |
| 2620 | if (Leaked.empty()) |
| 2621 | return; |
| 2622 | |
| 2623 | ExplodedNode<GRState>* N = Builder.MakeNode(Dst, S, Pred, St); |
| 2624 | |
| 2625 | if (!N) |
| 2626 | return; |
| 2627 | |
| 2628 | for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator |
| 2629 | I = Leaked.begin(), E = Leaked.end(); I != E; ++I) { |
| 2630 | |
| 2631 | CFRefBug *BT = static_cast<CFRefBug*>(I->second ? leakAtReturn |
| 2632 | : leakWithinFunction); |
| 2633 | assert(BT && "BugType not initialized."); |
Ted Kremenek | e469fa0 | 2009-02-07 22:19:59 +0000 | [diff] [blame^] | 2634 | CFRefLeakReport* report = new CFRefLeakReport(*BT, N, I->first, |
| 2635 | Eng.getStateManager()); |
Ted Kremenek | cf70177 | 2009-02-05 06:50:21 +0000 | [diff] [blame] | 2636 | BR->EmitReport(report); |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst, |
| 2641 | GRStmtNodeBuilder<GRState>& Builder, |
| 2642 | Expr* NodeExpr, Expr* ErrorExpr, |
| 2643 | ExplodedNode<GRState>* Pred, |
| 2644 | const GRState* St, |
| 2645 | RefVal::Kind hasErr, SymbolRef Sym) { |
| 2646 | Builder.BuildSinks = true; |
| 2647 | GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St); |
| 2648 | |
| 2649 | if (!N) return; |
| 2650 | |
| 2651 | CFRefBug *BT = 0; |
| 2652 | |
| 2653 | if (hasErr == RefVal::ErrorUseAfterRelease) |
| 2654 | BT = static_cast<CFRefBug*>(useAfterRelease); |
| 2655 | else { |
| 2656 | assert(hasErr == RefVal::ErrorReleaseNotOwned); |
| 2657 | BT = static_cast<CFRefBug*>(releaseNotOwned); |
| 2658 | } |
| 2659 | |
| 2660 | CFRefReport *report = new CFRefReport(*BT, N, Sym); |
| 2661 | report->addRange(ErrorExpr->getSourceRange()); |
| 2662 | BR->EmitReport(report); |
| 2663 | } |
| 2664 | |
| 2665 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 2666 | // Transfer function creation for external clients. |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2667 | //===----------------------------------------------------------------------===// |
| 2668 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2669 | GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled, |
| 2670 | const LangOptions& lopts) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 2671 | return new CFRefCount(Ctx, GCEnabled, lopts); |
Ted Kremenek | 3ea0b6a | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 2672 | } |