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