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 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 609 | |
| 610 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 611 | //===----------------------------------------------------------------------===// |
| 612 | // Implementation of checker data structures. |
| 613 | //===----------------------------------------------------------------------===// |
| 614 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 615 | RetainSummaryManager::~RetainSummaryManager() { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 616 | |
| 617 | // FIXME: The ArgEffects could eventually be allocated from BPAlloc, |
| 618 | // mitigating the need to do explicit cleanup of the |
| 619 | // Argument-Effect summaries. |
| 620 | |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 621 | for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(), |
| 622 | E = ArgEffectsSet.end(); I!=E; ++I) |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 623 | I->getValue().~ArgEffects(); |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 624 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 625 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 626 | ArgEffects* RetainSummaryManager::getArgEffects() { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 627 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 628 | if (ScratchArgs.empty()) |
| 629 | return NULL; |
| 630 | |
| 631 | // Compute a profile for a non-empty ScratchArgs. |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 632 | llvm::FoldingSetNodeID profile; |
| 633 | profile.Add(ScratchArgs); |
| 634 | void* InsertPos; |
| 635 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 636 | // Look up the uniqued copy, or create a new one. |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 637 | llvm::FoldingSetNodeWrapper<ArgEffects>* E = |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 638 | ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 639 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 640 | if (E) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 641 | ScratchArgs.clear(); |
| 642 | return &E->getValue(); |
| 643 | } |
| 644 | |
| 645 | E = (llvm::FoldingSetNodeWrapper<ArgEffects>*) |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 646 | BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 647 | |
| 648 | new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 649 | ArgEffectsSet.InsertNode(E, InsertPos); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 650 | |
| 651 | ScratchArgs.clear(); |
| 652 | return &E->getValue(); |
| 653 | } |
| 654 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 655 | RetainSummary* |
| 656 | RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff, |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 657 | ArgEffect ReceiverEff, |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 658 | ArgEffect DefaultEff, |
| 659 | bool isEndPath) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 660 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 661 | // Generate a profile for the summary. |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 662 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | 2d1086c | 2008-07-18 17:39:56 +0000 | [diff] [blame] | 663 | RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff, |
| 664 | isEndPath); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 665 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 666 | // Look up the uniqued summary, or create one if it doesn't exist. |
| 667 | void* InsertPos; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 668 | RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 669 | |
| 670 | if (Summ) |
| 671 | return Summ; |
| 672 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 673 | // Create the summary and return it. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 674 | Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>(); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 675 | new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 676 | SummarySet.InsertNode(Summ, InsertPos); |
| 677 | |
| 678 | return Summ; |
| 679 | } |
| 680 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 681 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 682 | // Predicates. |
| 683 | //===----------------------------------------------------------------------===// |
| 684 | |
| 685 | bool RetainSummaryManager::isTrackedObjectType(QualType T) { |
| 686 | if (!Ctx.isObjCObjectPointerType(T)) |
| 687 | return false; |
| 688 | |
| 689 | // Does it subclass NSObject? |
| 690 | ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr()); |
| 691 | |
| 692 | // We assume that id<..>, id, and "Class" all represent tracked objects. |
| 693 | if (!OT) |
| 694 | return true; |
| 695 | |
| 696 | // Does the object type subclass NSObject? |
| 697 | // FIXME: We can memoize here if this gets too expensive. |
| 698 | IdentifierInfo* NSObjectII = &Ctx.Idents.get("NSObject"); |
| 699 | ObjCInterfaceDecl* ID = OT->getDecl(); |
| 700 | |
| 701 | for ( ; ID ; ID = ID->getSuperClass()) |
| 702 | if (ID->getIdentifier() == NSObjectII) |
| 703 | return true; |
| 704 | |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 709 | // Summary creation for functions (largely uses of Core Foundation). |
| 710 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 711 | |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 712 | static bool isRetain(FunctionDecl* FD, const char* FName) { |
| 713 | const char* loc = strstr(FName, "Retain"); |
| 714 | return loc && loc[sizeof("Retain")-1] == '\0'; |
| 715 | } |
| 716 | |
| 717 | static bool isRelease(FunctionDecl* FD, const char* FName) { |
| 718 | const char* loc = strstr(FName, "Release"); |
| 719 | return loc && loc[sizeof("Release")-1] == '\0'; |
| 720 | } |
| 721 | |
Ted Kremenek | ab59227 | 2008-06-24 03:56:45 +0000 | [diff] [blame] | 722 | RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 723 | |
| 724 | SourceLocation Loc = FD->getLocation(); |
| 725 | |
| 726 | if (!Loc.isFileID()) |
| 727 | return NULL; |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 728 | |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 729 | // Look up a summary in our cache of FunctionDecls -> Summaries. |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 730 | FuncSummariesTy::iterator I = FuncSummaries.find(FD); |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 731 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 732 | if (I != FuncSummaries.end()) |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 733 | return I->second; |
| 734 | |
| 735 | // No summary. Generate one. |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 736 | RetainSummary *S = 0; |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 737 | |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 738 | do { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 739 | // We generate "stop" summaries for implicitly defined functions. |
| 740 | if (FD->isImplicit()) { |
| 741 | S = getPersistentStopSummary(); |
| 742 | break; |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 743 | } |
Ted Kremenek | 6ca3191 | 2008-11-04 00:36:12 +0000 | [diff] [blame] | 744 | |
Ted Kremenek | 9989065 | 2009-01-16 18:40:33 +0000 | [diff] [blame] | 745 | // [PR 3337] Use 'getDesugaredType' to strip away any typedefs on the |
| 746 | // function's type. |
| 747 | FunctionType* FT = cast<FunctionType>(FD->getType()->getDesugaredType()); |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 748 | const char* FName = FD->getIdentifier()->getName(); |
| 749 | |
| 750 | // Inspect the result type. |
| 751 | QualType RetTy = FT->getResultType(); |
| 752 | |
| 753 | // FIXME: This should all be refactored into a chain of "summary lookup" |
| 754 | // filters. |
| 755 | if (strcmp(FName, "IOServiceGetMatchingServices") == 0) { |
| 756 | // FIXES: <rdar://problem/6326900> |
| 757 | // This should be addressed using a API table. This strcmp is also |
| 758 | // a little gross, but there is no need to super optimize here. |
| 759 | assert (ScratchArgs.empty()); |
| 760 | ScratchArgs.push_back(std::make_pair(1, DecRef)); |
| 761 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing); |
| 762 | break; |
Ted Kremenek | 64e859a | 2008-10-22 20:54:52 +0000 | [diff] [blame] | 763 | } |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 764 | |
| 765 | // Handle: id NSMakeCollectable(CFTypeRef) |
| 766 | if (strcmp(FName, "NSMakeCollectable") == 0) { |
| 767 | S = (RetTy == Ctx.getObjCIdType()) |
| 768 | ? getUnarySummary(FT, cfmakecollectable) |
| 769 | : getPersistentStopSummary(); |
| 770 | |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | if (RetTy->isPointerType()) { |
| 775 | // For CoreFoundation ('CF') types. |
| 776 | if (isRefType(RetTy, "CF", &Ctx, FName)) { |
| 777 | if (isRetain(FD, FName)) |
| 778 | S = getUnarySummary(FT, cfretain); |
| 779 | else if (strstr(FName, "MakeCollectable")) |
| 780 | S = getUnarySummary(FT, cfmakecollectable); |
| 781 | else |
| 782 | S = getCFCreateGetRuleSummary(FD, FName); |
| 783 | |
| 784 | break; |
| 785 | } |
| 786 | |
| 787 | // For CoreGraphics ('CG') types. |
| 788 | if (isRefType(RetTy, "CG", &Ctx, FName)) { |
| 789 | if (isRetain(FD, FName)) |
| 790 | S = getUnarySummary(FT, cfretain); |
| 791 | else |
| 792 | S = getCFCreateGetRuleSummary(FD, FName); |
| 793 | |
| 794 | break; |
| 795 | } |
| 796 | |
| 797 | // For the Disk Arbitration API (DiskArbitration/DADisk.h) |
| 798 | if (isRefType(RetTy, "DADisk") || |
| 799 | isRefType(RetTy, "DADissenter") || |
| 800 | isRefType(RetTy, "DASessionRef")) { |
| 801 | S = getCFCreateGetRuleSummary(FD, FName); |
| 802 | break; |
| 803 | } |
| 804 | |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | // Check for release functions, the only kind of functions that we care |
| 809 | // about that don't return a pointer type. |
| 810 | if (FName[0] == 'C' && (FName[1] == 'F' || FName[1] == 'G')) { |
| 811 | if (isRelease(FD, FName+2)) |
| 812 | S = getUnarySummary(FT, cfrelease); |
| 813 | else { |
| 814 | // For CoreFoundation and CoreGraphics functions we assume they |
| 815 | // follow the ownership idiom strictly and thus do not cause |
| 816 | // ownership to "escape". |
| 817 | assert (ScratchArgs.empty()); |
| 818 | S = getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, |
| 819 | DoNothing); |
| 820 | } |
| 821 | } |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 822 | } |
| 823 | while (0); |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 824 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 825 | FuncSummaries[FD] = S; |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 826 | return S; |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 829 | RetainSummary* |
| 830 | RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD, |
| 831 | const char* FName) { |
| 832 | |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 833 | if (strstr(FName, "Create") || strstr(FName, "Copy")) |
| 834 | return getCFSummaryCreateRule(FD); |
Ted Kremenek | 37d785b | 2008-07-15 16:50:12 +0000 | [diff] [blame] | 835 | |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 836 | if (strstr(FName, "Get")) |
| 837 | return getCFSummaryGetRule(FD); |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 842 | RetainSummary* |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 843 | RetainSummaryManager::getUnarySummary(FunctionType* FT, UnaryFuncKind func) { |
| 844 | // Sanity check that this is *really* a unary function. This can |
| 845 | // happen if people do weird things. |
| 846 | FunctionTypeProto* FTP = dyn_cast<FunctionTypeProto>(FT); |
| 847 | if (!FTP || FTP->getNumArgs() != 1) |
| 848 | return getPersistentStopSummary(); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 849 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 850 | assert (ScratchArgs.empty()); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 851 | |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 852 | switch (func) { |
Ted Kremenek | 1261938 | 2009-01-12 21:45:02 +0000 | [diff] [blame] | 853 | case cfretain: { |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 854 | ScratchArgs.push_back(std::make_pair(0, IncRef)); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 855 | return getPersistentSummary(RetEffect::MakeAlias(0), |
| 856 | DoNothing, DoNothing); |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | case cfrelease: { |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 860 | ScratchArgs.push_back(std::make_pair(0, DecRef)); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 861 | return getPersistentSummary(RetEffect::MakeNoRet(), |
| 862 | DoNothing, DoNothing); |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | case cfmakecollectable: { |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 866 | if (GCEnabled) |
| 867 | ScratchArgs.push_back(std::make_pair(0, DecRef)); |
| 868 | |
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 | default: |
Ted Kremenek | 86ad3bc | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 874 | assert (false && "Not a supported unary function."); |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 875 | return 0; |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 876 | } |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 879 | RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) { |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 880 | assert (ScratchArgs.empty()); |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 881 | |
| 882 | if (FD->getIdentifier() == CFDictionaryCreateII) { |
| 883 | ScratchArgs.push_back(std::make_pair(1, DoNothingByRef)); |
| 884 | ScratchArgs.push_back(std::make_pair(2, DoNothingByRef)); |
| 885 | } |
| 886 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 887 | return getPersistentSummary(RetEffect::MakeOwned(RetEffect::CF, true)); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 890 | RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) { |
Ted Kremenek | 891d5cc | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 891 | assert (ScratchArgs.empty()); |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 892 | return getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::CF), |
| 893 | DoNothing, DoNothing); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 896 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 897 | // Summary creation for Selectors. |
| 898 | //===----------------------------------------------------------------------===// |
| 899 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 900 | RetainSummary* |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 901 | RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) { |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 902 | assert(ScratchArgs.empty()); |
| 903 | |
| 904 | RetainSummary* Summ = |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 905 | getPersistentSummary(RetEffect::MakeReceiverAlias()); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 906 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 907 | ObjCMethodSummaries[ME] = Summ; |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 908 | return Summ; |
| 909 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 910 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 911 | |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 912 | RetainSummary* |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 913 | RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME, |
| 914 | ObjCInterfaceDecl* ID) { |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 915 | |
| 916 | Selector S = ME->getSelector(); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 917 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 918 | // Look up a summary in our summary cache. |
| 919 | ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S); |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 920 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 921 | if (I != ObjCMethodSummaries.end()) |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 922 | return I->second; |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 923 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 924 | // "initXXX": pass-through for receiver. |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 925 | const char* s = S.getIdentifierInfoForSlot(0)->getName(); |
Ted Kremenek | a4b695a | 2008-05-07 03:45:05 +0000 | [diff] [blame] | 926 | assert (ScratchArgs.empty()); |
Ted Kremenek | aee9e57 | 2008-05-06 06:09:09 +0000 | [diff] [blame] | 927 | |
Ted Kremenek | 0327f77 | 2008-06-02 17:14:13 +0000 | [diff] [blame] | 928 | if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0) |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 929 | return getInitMethodSummary(ME); |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 930 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 931 | // Look for methods that return an owned object. |
| 932 | if (!isTrackedObjectType(Ctx.getCanonicalType(ME->getType()))) |
Ted Kremenek | 84060db | 2008-05-07 04:25:59 +0000 | [diff] [blame] | 933 | return 0; |
Ted Kremenek | a4b695a | 2008-05-07 03:45:05 +0000 | [diff] [blame] | 934 | |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 935 | if (followsFundamentalRule(s)) { |
| 936 | RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet() |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 937 | : RetEffect::MakeOwned(RetEffect::ObjC, true); |
Ted Kremenek | a4b695a | 2008-05-07 03:45:05 +0000 | [diff] [blame] | 938 | RetainSummary* Summ = getPersistentSummary(E); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 939 | ObjCMethodSummaries[ME] = Summ; |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 940 | return Summ; |
| 941 | } |
Ted Kremenek | 1bffd74 | 2008-05-06 15:44:25 +0000 | [diff] [blame] | 942 | |
Ted Kremenek | 46e49ee | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 943 | return 0; |
| 944 | } |
| 945 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 946 | RetainSummary* |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 947 | RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName, |
| 948 | Selector S) { |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 949 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 950 | // FIXME: Eventually we should properly do class method summaries, but |
| 951 | // it requires us being able to walk the type hierarchy. Unfortunately, |
| 952 | // we cannot do this with just an IdentifierInfo* for the class name. |
| 953 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 954 | // Look up a summary in our cache of Selectors -> Summaries. |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 955 | ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S); |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 956 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 957 | if (I != ObjCClassMethodSummaries.end()) |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 958 | return I->second; |
| 959 | |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 960 | return 0; |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 963 | void RetainSummaryManager::InitializeClassMethodSummaries() { |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 964 | |
| 965 | assert (ScratchArgs.empty()); |
| 966 | |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 967 | RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet() |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 968 | : RetEffect::MakeOwned(RetEffect::ObjC, true); |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 969 | |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 970 | RetainSummary* Summ = getPersistentSummary(E); |
| 971 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 972 | // Create the summaries for "alloc", "new", and "allocWithZone:" for |
| 973 | // NSObject and its derivatives. |
| 974 | addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ); |
| 975 | addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ); |
| 976 | addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 977 | |
| 978 | // Create the [NSAssertionHandler currentHander] summary. |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 979 | addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"), |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 980 | GetNullarySelector("currentHandler", Ctx), |
| 981 | getPersistentSummary(RetEffect::MakeNotOwned(RetEffect::ObjC))); |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 982 | |
| 983 | // Create the [NSAutoreleasePool addObject:] summary. |
| 984 | if (!isGCEnabled()) { |
| 985 | ScratchArgs.push_back(std::make_pair(0, Autorelease)); |
| 986 | addClsMethSummary(&Ctx.Idents.get("NSAutoreleasePool"), |
| 987 | GetUnarySelector("addObject", Ctx), |
| 988 | getPersistentSummary(RetEffect::MakeNoRet(), |
| 989 | DoNothing, DoNothing)); |
| 990 | } |
Ted Kremenek | 9c32d08 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 993 | void RetainSummaryManager::InitializeMethodSummaries() { |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 994 | |
| 995 | assert (ScratchArgs.empty()); |
| 996 | |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 997 | // Create the "init" selector. It just acts as a pass-through for the |
| 998 | // receiver. |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 999 | RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias()); |
| 1000 | addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm); |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1001 | |
| 1002 | // The next methods are allocators. |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1003 | RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet() |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1004 | : RetEffect::MakeOwned(RetEffect::ObjC, true); |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1005 | |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 1006 | RetainSummary* Summ = getPersistentSummary(E); |
Ted Kremenek | c839560 | 2008-05-06 21:26:51 +0000 | [diff] [blame] | 1007 | |
| 1008 | // Create the "copy" selector. |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1009 | addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ); |
| 1010 | |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1011 | // Create the "mutableCopy" selector. |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1012 | addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ); |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1013 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1014 | // Create the "retain" selector. |
| 1015 | E = RetEffect::MakeReceiverAlias(); |
| 1016 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1017 | addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1018 | |
| 1019 | // Create the "release" selector. |
| 1020 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1021 | addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ); |
Ted Kremenek | 299e815 | 2008-05-07 21:17:39 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Create the "drain" selector. |
| 1024 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1025 | addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ); |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1026 | |
| 1027 | // Create the "autorelease" selector. |
Ted Kremenek | e19f449 | 2008-06-30 16:57:41 +0000 | [diff] [blame] | 1028 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : Autorelease); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1029 | addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ); |
Ted Kremenek | 9853045 | 2008-08-12 20:41:56 +0000 | [diff] [blame] | 1030 | |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1031 | // For NSWindow, allocated objects are (initially) self-owned. |
Ted Kremenek | 179064e | 2008-07-01 17:21:27 +0000 | [diff] [blame] | 1032 | RetainSummary *NSWindowSumm = |
| 1033 | getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn); |
Ted Kremenek | af9dc27 | 2008-08-12 18:48:50 +0000 | [diff] [blame] | 1034 | |
| 1035 | addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect", |
| 1036 | "styleMask", "backing", "defer", NULL); |
| 1037 | |
| 1038 | addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect", |
| 1039 | "styleMask", "backing", "defer", "screen", NULL); |
| 1040 | |
| 1041 | // For NSPanel (which subclasses NSWindow), allocated objects are not |
| 1042 | // self-owned. |
| 1043 | addInstMethSummary("NSPanel", InitSumm, "initWithContentRect", |
| 1044 | "styleMask", "backing", "defer", NULL); |
| 1045 | |
| 1046 | addInstMethSummary("NSPanel", InitSumm, "initWithContentRect", |
| 1047 | "styleMask", "backing", "defer", "screen", NULL); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1048 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1049 | // Create NSAssertionHandler summaries. |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1050 | addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file", |
| 1051 | "lineNumber", "description", NULL); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1052 | |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1053 | addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object", |
| 1054 | "file", "lineNumber", "description", NULL); |
Ted Kremenek | b3c3c28 | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1057 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1058 | // Reference-counting logic (typestate + counts). |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1059 | //===----------------------------------------------------------------------===// |
| 1060 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1061 | namespace { |
| 1062 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1063 | class VISIBILITY_HIDDEN RefVal { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1064 | public: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1065 | enum Kind { |
| 1066 | Owned = 0, // Owning reference. |
| 1067 | NotOwned, // Reference is not owned by still valid (not freed). |
| 1068 | Released, // Object has been released. |
| 1069 | ReturnedOwned, // Returned object passes ownership to caller. |
| 1070 | ReturnedNotOwned, // Return object does not pass ownership to caller. |
| 1071 | ErrorUseAfterRelease, // Object used after released. |
| 1072 | ErrorReleaseNotOwned, // Release of an object that was not owned. |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1073 | ErrorLeak, // A memory leak due to excessive reference counts. |
| 1074 | ErrorLeakReturned // A memory leak due to the returning method not having |
| 1075 | // the correct naming conventions. |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1076 | }; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1077 | |
| 1078 | private: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1079 | Kind kind; |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1080 | RetEffect::ObjKind okind; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1081 | unsigned Cnt; |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1082 | QualType T; |
| 1083 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1084 | RefVal(Kind k, RetEffect::ObjKind o, unsigned cnt, QualType t) |
| 1085 | : kind(k), okind(o), Cnt(cnt), T(t) {} |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1086 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1087 | RefVal(Kind k, unsigned cnt = 0) |
| 1088 | : kind(k), okind(RetEffect::AnyObj), Cnt(cnt) {} |
| 1089 | |
| 1090 | public: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1091 | Kind getKind() const { return kind; } |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1092 | |
| 1093 | RetEffect::ObjKind getObjKind() const { return okind; } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1094 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1095 | unsigned getCount() const { return Cnt; } |
| 1096 | QualType getType() const { return T; } |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1097 | |
| 1098 | // Useful predicates. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1099 | |
Ted Kremenek | 73c750b | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 1100 | static bool isError(Kind k) { return k >= ErrorUseAfterRelease; } |
| 1101 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1102 | static bool isLeak(Kind k) { return k >= ErrorLeak; } |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1103 | |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1104 | bool isOwned() const { |
| 1105 | return getKind() == Owned; |
| 1106 | } |
| 1107 | |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1108 | bool isNotOwned() const { |
| 1109 | return getKind() == NotOwned; |
| 1110 | } |
| 1111 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1112 | bool isReturnedOwned() const { |
| 1113 | return getKind() == ReturnedOwned; |
| 1114 | } |
| 1115 | |
| 1116 | bool isReturnedNotOwned() const { |
| 1117 | return getKind() == ReturnedNotOwned; |
| 1118 | } |
| 1119 | |
| 1120 | bool isNonLeakError() const { |
| 1121 | Kind k = getKind(); |
| 1122 | return isError(k) && !isLeak(k); |
| 1123 | } |
| 1124 | |
| 1125 | // State creation: normal state. |
| 1126 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1127 | static RefVal makeOwned(RetEffect::ObjKind o, QualType t, |
| 1128 | unsigned Count = 1) { |
| 1129 | return RefVal(Owned, o, Count, t); |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1132 | static RefVal makeNotOwned(RetEffect::ObjKind o, QualType t, |
| 1133 | unsigned Count = 0) { |
| 1134 | return RefVal(NotOwned, o, Count, t); |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1135 | } |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1136 | |
| 1137 | static RefVal makeReturnedOwned(unsigned Count) { |
| 1138 | return RefVal(ReturnedOwned, Count); |
| 1139 | } |
| 1140 | |
| 1141 | static RefVal makeReturnedNotOwned() { |
| 1142 | return RefVal(ReturnedNotOwned); |
| 1143 | } |
| 1144 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1145 | // Comparison, profiling, and pretty-printing. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1146 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1147 | bool operator==(const RefVal& X) const { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1148 | return kind == X.kind && Cnt == X.Cnt && T == X.T; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1149 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1150 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1151 | RefVal operator-(size_t i) const { |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1152 | return RefVal(getKind(), getObjKind(), getCount() - i, getType()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| 1155 | RefVal operator+(size_t i) const { |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1156 | return RefVal(getKind(), getObjKind(), getCount() + i, getType()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | RefVal operator^(Kind k) const { |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1160 | return RefVal(k, getObjKind(), getCount(), getType()); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1161 | } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1162 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1163 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 1164 | ID.AddInteger((unsigned) kind); |
| 1165 | ID.AddInteger(Cnt); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1166 | ID.Add(T); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1169 | void print(std::ostream& Out) const; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1170 | }; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1171 | |
| 1172 | void RefVal::print(std::ostream& Out) const { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1173 | if (!T.isNull()) |
| 1174 | Out << "Tracked Type:" << T.getAsString() << '\n'; |
| 1175 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1176 | switch (getKind()) { |
| 1177 | default: assert(false); |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1178 | case Owned: { |
| 1179 | Out << "Owned"; |
| 1180 | unsigned cnt = getCount(); |
| 1181 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1182 | break; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1183 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1184 | |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1185 | case NotOwned: { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1186 | Out << "NotOwned"; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1187 | unsigned cnt = getCount(); |
| 1188 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1189 | break; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1190 | } |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1191 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1192 | case ReturnedOwned: { |
| 1193 | Out << "ReturnedOwned"; |
| 1194 | unsigned cnt = getCount(); |
| 1195 | if (cnt) Out << " (+ " << cnt << ")"; |
| 1196 | break; |
| 1197 | } |
| 1198 | |
| 1199 | case ReturnedNotOwned: { |
| 1200 | Out << "ReturnedNotOwned"; |
| 1201 | unsigned cnt = getCount(); |
| 1202 | if (cnt) Out << " (+ " << cnt << ")"; |
| 1203 | break; |
| 1204 | } |
| 1205 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1206 | case Released: |
| 1207 | Out << "Released"; |
| 1208 | break; |
| 1209 | |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1210 | case ErrorLeak: |
| 1211 | Out << "Leaked"; |
| 1212 | break; |
| 1213 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1214 | case ErrorLeakReturned: |
| 1215 | Out << "Leaked (Bad naming)"; |
| 1216 | break; |
| 1217 | |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1218 | case ErrorUseAfterRelease: |
| 1219 | Out << "Use-After-Release [ERROR]"; |
| 1220 | break; |
| 1221 | |
| 1222 | case ErrorReleaseNotOwned: |
| 1223 | Out << "Release of Not-Owned [ERROR]"; |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1227 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1228 | } // end anonymous namespace |
| 1229 | |
| 1230 | //===----------------------------------------------------------------------===// |
| 1231 | // RefBindings - State used to track object reference counts. |
| 1232 | //===----------------------------------------------------------------------===// |
| 1233 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1234 | typedef llvm::ImmutableMap<SymbolRef, RefVal> RefBindings; |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1235 | static int RefBIndex = 0; |
| 1236 | |
| 1237 | namespace clang { |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1238 | template<> |
| 1239 | struct GRStateTrait<RefBindings> : public GRStatePartialTrait<RefBindings> { |
| 1240 | static inline void* GDMIndex() { return &RefBIndex; } |
| 1241 | }; |
| 1242 | } |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1243 | |
| 1244 | //===----------------------------------------------------------------------===// |
| 1245 | // ARBindings - State used to track objects in autorelease pools. |
| 1246 | //===----------------------------------------------------------------------===// |
| 1247 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1248 | typedef llvm::ImmutableSet<SymbolRef> ARPoolContents; |
| 1249 | typedef llvm::ImmutableList< std::pair<SymbolRef, ARPoolContents*> > ARBindings; |
Ted Kremenek | 6d34893 | 2008-10-21 15:53:15 +0000 | [diff] [blame] | 1250 | static int AutoRBIndex = 0; |
| 1251 | |
| 1252 | namespace clang { |
| 1253 | template<> |
| 1254 | struct GRStateTrait<ARBindings> : public GRStatePartialTrait<ARBindings> { |
| 1255 | static inline void* GDMIndex() { return &AutoRBIndex; } |
| 1256 | }; |
| 1257 | } |
| 1258 | |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1259 | //===----------------------------------------------------------------------===// |
| 1260 | // Transfer functions. |
| 1261 | //===----------------------------------------------------------------------===// |
| 1262 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1263 | namespace { |
| 1264 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1265 | class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals { |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1266 | public: |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1267 | // Type definitions. |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1268 | typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolRef> > |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1269 | ReleasesNotOwnedTy; |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1270 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1271 | typedef ReleasesNotOwnedTy UseAfterReleasesTy; |
| 1272 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1273 | typedef llvm::DenseMap<GRExprEngine::NodeTy*, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1274 | std::vector<std::pair<SymbolRef,bool> >*> |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1275 | LeaksTy; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1276 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1277 | class BindingsPrinter : public GRState::Printer { |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1278 | public: |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1279 | virtual void Print(std::ostream& Out, const GRState* state, |
| 1280 | const char* nl, const char* sep); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1281 | }; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1282 | |
| 1283 | private: |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1284 | RetainSummaryManager Summaries; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1285 | const LangOptions& LOpts; |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1286 | |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1287 | UseAfterReleasesTy UseAfterReleases; |
| 1288 | ReleasesNotOwnedTy ReleasesNotOwned; |
| 1289 | LeaksTy Leaks; |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1290 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1291 | RefBindings Update(RefBindings B, SymbolRef sym, RefVal V, ArgEffect E, |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1292 | RefVal::Kind& hasErr, RefBindings::Factory& RefBFactory); |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1293 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1294 | RefVal::Kind& Update(GRStateRef& state, SymbolRef sym, RefVal V, |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1295 | ArgEffect E, RefVal::Kind& hasErr) { |
| 1296 | |
| 1297 | state = state.set<RefBindings>(Update(state.get<RefBindings>(), sym, V, |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1298 | E, hasErr, |
| 1299 | state.get_context<RefBindings>())); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1300 | return hasErr; |
| 1301 | } |
| 1302 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1303 | void ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst, |
| 1304 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1305 | Expr* NodeExpr, Expr* ErrorExpr, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1306 | ExplodedNode<GRState>* Pred, |
| 1307 | const GRState* St, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1308 | RefVal::Kind hasErr, SymbolRef Sym); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1309 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1310 | std::pair<GRStateRef, bool> |
| 1311 | HandleSymbolDeath(GRStateManager& VMgr, const GRState* St, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1312 | const Decl* CD, SymbolRef sid, RefVal V, bool& hasLeak); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1313 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1314 | public: |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1315 | |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 1316 | CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts) |
Ted Kremenek | 377e230 | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1317 | : Summaries(Ctx, gcenabled), |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 1318 | LOpts(lopts) {} |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1319 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1320 | virtual ~CFRefCount() { |
| 1321 | for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I) |
| 1322 | delete I->second; |
| 1323 | } |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1324 | |
| 1325 | virtual void RegisterChecks(GRExprEngine& Eng); |
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 | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1396 | |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 1397 | // Error iterators. |
| 1398 | |
| 1399 | typedef UseAfterReleasesTy::iterator use_after_iterator; |
| 1400 | typedef ReleasesNotOwnedTy::iterator bad_release_iterator; |
Ted Kremenek | 989d519 | 2008-04-17 23:43:50 +0000 | [diff] [blame] | 1401 | typedef LeaksTy::iterator leaks_iterator; |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 1402 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1403 | use_after_iterator use_after_begin() { return UseAfterReleases.begin(); } |
| 1404 | use_after_iterator use_after_end() { return UseAfterReleases.end(); } |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 1405 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1406 | bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); } |
| 1407 | bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); } |
Ted Kremenek | 989d519 | 2008-04-17 23:43:50 +0000 | [diff] [blame] | 1408 | |
| 1409 | leaks_iterator leaks_begin() { return Leaks.begin(); } |
| 1410 | leaks_iterator leaks_end() { return Leaks.end(); } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1411 | }; |
| 1412 | |
| 1413 | } // end anonymous namespace |
| 1414 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1415 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1416 | |
| 1417 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1418 | void CFRefCount::BindingsPrinter::Print(std::ostream& Out, const GRState* state, |
| 1419 | const char* nl, const char* sep) { |
| 1420 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1421 | RefBindings B = state->get<RefBindings>(); |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1422 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 1423 | if (!B.isEmpty()) |
Ted Kremenek | f394804 | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 1424 | Out << sep << nl; |
| 1425 | |
| 1426 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 1427 | Out << (*I).first << " : "; |
| 1428 | (*I).second.print(Out); |
| 1429 | Out << nl; |
| 1430 | } |
| 1431 | } |
| 1432 | |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1433 | static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) { |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1434 | return Summ ? Summ->getArg(idx) : MayEscape; |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1437 | static inline RetEffect GetRetEffect(RetainSummary* Summ) { |
| 1438 | return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet(); |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1441 | static inline ArgEffect GetReceiverE(RetainSummary* Summ) { |
| 1442 | return Summ ? Summ->getReceiverEffect() : DoNothing; |
| 1443 | } |
| 1444 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1445 | static inline bool IsEndPath(RetainSummary* Summ) { |
| 1446 | return Summ ? Summ->isEndPath() : false; |
| 1447 | } |
| 1448 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1449 | void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<GRState>& Dst, |
| 1450 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1451 | Expr* NodeExpr, Expr* ErrorExpr, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1452 | ExplodedNode<GRState>* Pred, |
| 1453 | const GRState* St, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1454 | RefVal::Kind hasErr, SymbolRef Sym) { |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1455 | Builder.BuildSinks = true; |
| 1456 | GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St); |
| 1457 | |
| 1458 | if (!N) return; |
| 1459 | |
| 1460 | switch (hasErr) { |
| 1461 | default: assert(false); |
| 1462 | case RefVal::ErrorUseAfterRelease: |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1463 | UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym); |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1464 | break; |
| 1465 | |
| 1466 | case RefVal::ErrorReleaseNotOwned: |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1467 | ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym); |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1468 | break; |
| 1469 | } |
| 1470 | } |
| 1471 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1472 | /// GetReturnType - Used to get the return type of a message expression or |
| 1473 | /// function call with the intention of affixing that type to a tracked symbol. |
| 1474 | /// While the the return type can be queried directly from RetEx, when |
| 1475 | /// invoking class methods we augment to the return type to be that of |
| 1476 | /// a pointer to the class (as opposed it just being id). |
| 1477 | static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) { |
| 1478 | |
| 1479 | QualType RetTy = RetE->getType(); |
| 1480 | |
| 1481 | // FIXME: We aren't handling id<...>. |
Chris Lattner | 8b51fd7 | 2008-07-26 22:36:27 +0000 | [diff] [blame] | 1482 | const PointerType* PT = RetTy->getAsPointerType(); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1483 | if (!PT) |
| 1484 | return RetTy; |
| 1485 | |
| 1486 | // If RetEx is not a message expression just return its type. |
| 1487 | // If RetEx is a message expression, return its types if it is something |
| 1488 | /// more specific than id. |
| 1489 | |
| 1490 | ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE); |
| 1491 | |
| 1492 | if (!ME || !Ctx.isObjCIdType(PT->getPointeeType())) |
| 1493 | return RetTy; |
| 1494 | |
| 1495 | ObjCInterfaceDecl* D = ME->getClassInfo().first; |
| 1496 | |
| 1497 | // At this point we know the return type of the message expression is id. |
| 1498 | // If we have an ObjCInterceDecl, we know this is a call to a class method |
| 1499 | // whose type we can resolve. In such cases, promote the return type to |
| 1500 | // Class*. |
| 1501 | return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D)); |
| 1502 | } |
| 1503 | |
| 1504 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1505 | void CFRefCount::EvalSummary(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1506 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1507 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1508 | Expr* Ex, |
| 1509 | Expr* Receiver, |
| 1510 | RetainSummary* Summ, |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1511 | ExprIterator arg_beg, ExprIterator arg_end, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1512 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1513 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1514 | // Get the state. |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1515 | GRStateRef state(Builder.GetState(Pred), Eng.getStateManager()); |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1516 | ASTContext& Ctx = Eng.getStateManager().getContext(); |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1517 | |
| 1518 | // Evaluate the effect of the arguments. |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1519 | RefVal::Kind hasErr = (RefVal::Kind) 0; |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1520 | unsigned idx = 0; |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1521 | Expr* ErrorExpr = NULL; |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1522 | SymbolRef ErrorSym = 0; |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1523 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1524 | for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1525 | SVal V = state.GetSVal(*I); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1526 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1527 | if (isa<loc::SymbolVal>(V)) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1528 | SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol(); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1529 | if (RefBindings::data_type* T = state.get<RefBindings>(Sym)) |
| 1530 | if (Update(state, Sym, *T, GetArgE(Summ, idx), hasErr)) { |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1531 | ErrorExpr = *I; |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1532 | ErrorSym = Sym; |
Ted Kremenek | bcf50ad | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1533 | break; |
| 1534 | } |
Ted Kremenek | b887355 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 1535 | } |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1536 | else if (isa<Loc>(V)) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1537 | if (loc::MemRegionVal* MR = dyn_cast<loc::MemRegionVal>(&V)) { |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1538 | |
| 1539 | if (GetArgE(Summ, idx) == DoNothingByRef) |
| 1540 | continue; |
| 1541 | |
| 1542 | // Invalidate the value of the variable passed by reference. |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 1543 | |
| 1544 | // FIXME: Either this logic should also be replicated in GRSimpleVals |
| 1545 | // or should be pulled into a separate "constraint engine." |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1546 | |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 1547 | // FIXME: We can have collisions on the conjured symbol if the |
| 1548 | // expression *I also creates conjured symbols. We probably want |
| 1549 | // to identify conjured symbols by an expression pair: the enclosing |
| 1550 | // expression (the context) and the expression itself. This should |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 1551 | // disambiguate conjured symbols. |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1552 | |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 1553 | const TypedRegion* R = dyn_cast<TypedRegion>(MR->getRegion()); |
Ted Kremenek | 90b3236 | 2008-12-17 19:42:34 +0000 | [diff] [blame] | 1554 | |
| 1555 | // Blast through AnonTypedRegions to get the original region type. |
| 1556 | while (R) { |
| 1557 | const AnonTypedRegion* ATR = dyn_cast<AnonTypedRegion>(R); |
| 1558 | if (!ATR) break; |
| 1559 | R = dyn_cast<TypedRegion>(ATR->getSuperRegion()); |
| 1560 | } |
| 1561 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1562 | if (R) { |
Ted Kremenek | 40e86d9 | 2008-12-18 23:34:57 +0000 | [diff] [blame] | 1563 | |
| 1564 | // Is the invalidated variable something that we were tracking? |
| 1565 | SVal X = state.GetSVal(Loc::MakeVal(R)); |
| 1566 | |
| 1567 | if (isa<loc::SymbolVal>(X)) { |
| 1568 | SymbolRef Sym = cast<loc::SymbolVal>(X).getSymbol(); |
| 1569 | state = state.remove<RefBindings>(Sym); |
| 1570 | } |
| 1571 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1572 | // Set the value of the variable to be a conjured symbol. |
| 1573 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 6eddeb1 | 2008-12-13 21:49:13 +0000 | [diff] [blame] | 1574 | QualType T = R->getRValueType(Ctx); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1575 | |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1576 | // FIXME: handle structs. |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 1577 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1578 | SymbolRef NewSym = |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1579 | Eng.getSymbolManager().getConjuredSymbol(*I, T, Count); |
| 1580 | |
Ted Kremenek | 90b3236 | 2008-12-17 19:42:34 +0000 | [diff] [blame] | 1581 | state = state.BindLoc(Loc::MakeVal(R), |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1582 | Loc::IsLocType(T) |
| 1583 | ? cast<SVal>(loc::SymbolVal(NewSym)) |
| 1584 | : cast<SVal>(nonloc::SymbolVal(NewSym))); |
| 1585 | } |
| 1586 | else { |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1587 | state = state.BindLoc(*MR, UnknownVal()); |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1588 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1589 | } |
| 1590 | else |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1591 | state = state.BindLoc(*MR, UnknownVal()); |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 1592 | } |
| 1593 | else { |
| 1594 | // Nuke all other arguments passed by reference. |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1595 | state = state.Unbind(cast<Loc>(V)); |
Ted Kremenek | 8c5633e | 2008-07-03 23:26:32 +0000 | [diff] [blame] | 1596 | } |
Ted Kremenek | b887355 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 1597 | } |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1598 | else if (isa<nonloc::LocAsInteger>(V)) |
| 1599 | state = state.Unbind(cast<nonloc::LocAsInteger>(V).getLoc()); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1600 | } |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1601 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1602 | // Evaluate the effect on the message receiver. |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1603 | if (!ErrorExpr && Receiver) { |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1604 | SVal V = state.GetSVal(Receiver); |
| 1605 | if (isa<loc::SymbolVal>(V)) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1606 | SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol(); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1607 | if (const RefVal* T = state.get<RefBindings>(Sym)) |
| 1608 | if (Update(state, Sym, *T, GetReceiverE(Summ), hasErr)) { |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1609 | ErrorExpr = Receiver; |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1610 | ErrorSym = Sym; |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1611 | } |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1612 | } |
| 1613 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1614 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1615 | // Process any errors. |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1616 | if (hasErr) { |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1617 | ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, state, |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1618 | hasErr, ErrorSym); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1619 | return; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1620 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1621 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1622 | // Consult the summary for the return value. |
Ted Kremenek | 3c0cea3 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1623 | RetEffect RE = GetRetEffect(Summ); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1624 | |
| 1625 | switch (RE.getKind()) { |
| 1626 | default: |
| 1627 | assert (false && "Unhandled RetEffect."); break; |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1628 | |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1629 | case RetEffect::NoRet: { |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1630 | |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1631 | // Make up a symbol for the return value (not reference counted). |
Ted Kremenek | b887355 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 1632 | // FIXME: This is basically copy-and-paste from GRSimpleVals. We |
| 1633 | // should compose behavior, not copy it. |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1634 | |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1635 | // FIXME: We eventually should handle structs and other compound types |
| 1636 | // that are returned by value. |
| 1637 | |
| 1638 | QualType T = Ex->getType(); |
| 1639 | |
Ted Kremenek | 062e2f9 | 2008-11-13 06:10:40 +0000 | [diff] [blame] | 1640 | if (Loc::IsLocType(T) || (T->isIntegerType() && T->isScalarType())) { |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1641 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1642 | SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count); |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1643 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1644 | SVal X = Loc::IsLocType(Ex->getType()) |
| 1645 | ? cast<SVal>(loc::SymbolVal(Sym)) |
| 1646 | : cast<SVal>(nonloc::SymbolVal(Sym)); |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1647 | |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1648 | state = state.BindExpr(Ex, X, false); |
Ted Kremenek | f9561e5 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1651 | break; |
Ted Kremenek | fd30194 | 2008-10-17 22:23:12 +0000 | [diff] [blame] | 1652 | } |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1653 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1654 | case RetEffect::Alias: { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1655 | unsigned idx = RE.getIndex(); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1656 | assert (arg_end >= arg_beg); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1657 | assert (idx < (unsigned) (arg_end - arg_beg)); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1658 | SVal V = state.GetSVal(*(arg_beg+idx)); |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1659 | state = state.BindExpr(Ex, V, false); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1660 | break; |
| 1661 | } |
| 1662 | |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1663 | case RetEffect::ReceiverAlias: { |
| 1664 | assert (Receiver); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1665 | SVal V = state.GetSVal(Receiver); |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1666 | state = state.BindExpr(Ex, V, false); |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 1667 | break; |
| 1668 | } |
| 1669 | |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1670 | case RetEffect::OwnedAllocatedSymbol: |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1671 | case RetEffect::OwnedSymbol: { |
| 1672 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1673 | SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count); |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1674 | QualType RetT = GetReturnType(Ex, Eng.getContext()); |
| 1675 | state = |
| 1676 | state.set<RefBindings>(Sym, RefVal::makeOwned(RE.getObjKind(), RetT)); |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1677 | state = state.BindExpr(Ex, loc::SymbolVal(Sym), false); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1678 | |
| 1679 | #if 0 |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1680 | RefBindings B = GetRefBindings(StImpl); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1681 | SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(RetT))); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1682 | #endif |
| 1683 | |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1684 | // FIXME: Add a flag to the checker where allocations are allowed to fail. |
| 1685 | if (RE.getKind() == RetEffect::OwnedAllocatedSymbol) |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1686 | state = state.AddNE(Sym, Eng.getBasicVals().getZeroWithPtrWidth()); |
Ted Kremenek | a734470 | 2008-06-23 18:02:52 +0000 | [diff] [blame] | 1687 | |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1688 | break; |
| 1689 | } |
| 1690 | |
| 1691 | case RetEffect::NotOwnedSymbol: { |
| 1692 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1693 | SymbolRef Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1694 | QualType RetT = GetReturnType(Ex, Eng.getContext()); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1695 | |
Ted Kremenek | 2d1652e | 2009-01-28 05:56:51 +0000 | [diff] [blame^] | 1696 | state = |
| 1697 | state.set<RefBindings>(Sym, RefVal::makeNotOwned(RE.getObjKind(),RetT)); |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1698 | state = state.BindExpr(Ex, loc::SymbolVal(Sym), false); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1699 | break; |
| 1700 | } |
| 1701 | } |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1702 | |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1703 | // Is this a sink? |
| 1704 | if (IsEndPath(Summ)) |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1705 | Builder.MakeSinkNode(Dst, Ex, Pred, state); |
Ted Kremenek | 70a733e | 2008-07-18 17:24:20 +0000 | [diff] [blame] | 1706 | else |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1707 | Builder.MakeNode(Dst, Ex, Pred, state); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1711 | void CFRefCount::EvalCall(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1712 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1713 | GRStmtNodeBuilder<GRState>& Builder, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1714 | CallExpr* CE, SVal L, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1715 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1716 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1717 | RetainSummary* Summ = !isa<loc::FuncVal>(L) ? 0 |
| 1718 | : Summaries.getSummary(cast<loc::FuncVal>(L).getDecl()); |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1719 | |
| 1720 | EvalSummary(Dst, Eng, Builder, CE, 0, Summ, |
| 1721 | CE->arg_begin(), CE->arg_end(), Pred); |
Ted Kremenek | 2fff37e | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 1722 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1723 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1724 | void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1725 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1726 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1727 | ObjCMessageExpr* ME, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1728 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 1729 | RetainSummary* Summ; |
Ted Kremenek | 9040c65 | 2008-05-01 21:31:50 +0000 | [diff] [blame] | 1730 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1731 | if (Expr* Receiver = ME->getReceiver()) { |
| 1732 | // We need the type-information of the tracked receiver object |
| 1733 | // Retrieve it from the state. |
| 1734 | ObjCInterfaceDecl* ID = 0; |
| 1735 | |
| 1736 | // FIXME: Wouldn't it be great if this code could be reduced? It's just |
| 1737 | // a chain of lookups. |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1738 | const GRState* St = Builder.GetState(Pred); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1739 | SVal V = Eng.getStateManager().GetSVal(St, Receiver ); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1740 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1741 | if (isa<loc::SymbolVal>(V)) { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1742 | SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol(); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1743 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1744 | if (const RefVal* T = St->get<RefBindings>(Sym)) { |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1745 | QualType Ty = T->getType(); |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1746 | |
| 1747 | if (const PointerType* PT = Ty->getAsPointerType()) { |
| 1748 | QualType PointeeTy = PT->getPointeeType(); |
| 1749 | |
| 1750 | if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy)) |
| 1751 | ID = IT->getDecl(); |
| 1752 | } |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | Summ = Summaries.getMethodSummary(ME, ID); |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1757 | |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 1758 | // Special-case: are we sending a mesage to "self"? |
| 1759 | // This is a hack. When we have full-IP this should be removed. |
| 1760 | if (!Summ) { |
| 1761 | ObjCMethodDecl* MD = |
| 1762 | dyn_cast<ObjCMethodDecl>(&Eng.getGraph().getCodeDecl()); |
| 1763 | |
| 1764 | if (MD) { |
| 1765 | if (Expr* Receiver = ME->getReceiver()) { |
| 1766 | SVal X = Eng.getStateManager().GetSVal(St, Receiver); |
| 1767 | if (loc::MemRegionVal* L = dyn_cast<loc::MemRegionVal>(&X)) |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1768 | if (L->getRegion() == Eng.getStateManager().getSelfRegion(St)) { |
| 1769 | // Create a summmary where all of the arguments "StopTracking". |
| 1770 | Summ = Summaries.getPersistentSummary(RetEffect::MakeNoRet(), |
| 1771 | DoNothing, |
| 1772 | StopTracking); |
| 1773 | } |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 1774 | } |
| 1775 | } |
| 1776 | } |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 1777 | } |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1778 | else |
Ted Kremenek | 1f180c3 | 2008-06-23 22:21:20 +0000 | [diff] [blame] | 1779 | Summ = Summaries.getClassMethodSummary(ME->getClassName(), |
| 1780 | ME->getSelector()); |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1781 | |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 1782 | EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ, |
| 1783 | ME->arg_begin(), ME->arg_end(), Pred); |
Ted Kremenek | 8534820 | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1784 | } |
Ted Kremenek | b309525 | 2008-05-06 04:20:12 +0000 | [diff] [blame] | 1785 | |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1786 | // Stores. |
| 1787 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1788 | void CFRefCount::EvalStore(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1789 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1790 | GRStmtNodeBuilder<GRState>& Builder, |
| 1791 | Expr* E, ExplodedNode<GRState>* Pred, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1792 | const GRState* St, SVal TargetLV, SVal Val) { |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1793 | |
| 1794 | // Check if we have a binding for "Val" and if we are storing it to something |
| 1795 | // we don't understand or otherwise the value "escapes" the function. |
| 1796 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1797 | if (!isa<loc::SymbolVal>(Val)) |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1798 | return; |
| 1799 | |
| 1800 | // Are we storing to something that causes the value to "escape"? |
| 1801 | |
| 1802 | bool escapes = false; |
| 1803 | |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1804 | // A value escapes in three possible cases (this may change): |
| 1805 | // |
| 1806 | // (1) we are binding to something that is not a memory region. |
| 1807 | // (2) we are binding to a memregion that does not have stack storage |
| 1808 | // (3) we are binding to a memregion with stack storage that the store |
| 1809 | // does not understand. |
| 1810 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1811 | SymbolRef Sym = cast<loc::SymbolVal>(Val).getSymbol(); |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1812 | GRStateRef state(St, Eng.getStateManager()); |
| 1813 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1814 | if (!isa<loc::MemRegionVal>(TargetLV)) |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1815 | escapes = true; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1816 | else { |
Ted Kremenek | 993f1c7 | 2008-10-17 20:28:54 +0000 | [diff] [blame] | 1817 | const MemRegion* R = cast<loc::MemRegionVal>(TargetLV).getRegion(); |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1818 | escapes = !Eng.getStateManager().hasStackStorage(R); |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1819 | |
| 1820 | if (!escapes) { |
| 1821 | // To test (3), generate a new state with the binding removed. If it is |
| 1822 | // the same state, then it escapes (since the store cannot represent |
| 1823 | // the binding). |
Ted Kremenek | a441b7e | 2008-11-12 19:22:09 +0000 | [diff] [blame] | 1824 | GRStateRef stateNew = state.BindLoc(cast<Loc>(TargetLV), Val); |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1825 | escapes = (stateNew == state); |
| 1826 | } |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 1827 | } |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1828 | |
| 1829 | if (!escapes) |
| 1830 | return; |
Ted Kremenek | a496d16 | 2008-10-18 03:49:51 +0000 | [diff] [blame] | 1831 | |
| 1832 | // Do we have a reference count binding? |
| 1833 | // 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] | 1834 | if (!state.get<RefBindings>(Sym)) |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1835 | return; |
| 1836 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1837 | // Nuke the binding. |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 1838 | state = state.remove<RefBindings>(Sym); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1839 | |
Ted Kremenek | 1392261 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1840 | // Hand of the remaining logic to the parent implementation. |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1841 | GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, state, TargetLV, Val); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1844 | // End-of-path. |
| 1845 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1846 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1847 | std::pair<GRStateRef,bool> |
| 1848 | CFRefCount::HandleSymbolDeath(GRStateManager& VMgr, |
| 1849 | const GRState* St, const Decl* CD, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1850 | SymbolRef sid, |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1851 | RefVal V, bool& hasLeak) { |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1852 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1853 | GRStateRef state(St, VMgr); |
Sanjiv Gupta | 31fc07d | 2008-10-31 09:52:39 +0000 | [diff] [blame] | 1854 | assert ((!V.isReturnedOwned() || CD) && |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1855 | "CodeDecl must be available for reporting ReturnOwned errors."); |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 1856 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1857 | if (V.isReturnedOwned() && V.getCount() == 0) |
| 1858 | if (const ObjCMethodDecl* MD = dyn_cast<ObjCMethodDecl>(CD)) { |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 1859 | std::string s = MD->getSelector().getAsString(); |
Ted Kremenek | 4c79e55 | 2008-11-05 16:54:44 +0000 | [diff] [blame] | 1860 | if (!followsReturnRule(s.c_str())) { |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1861 | hasLeak = true; |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1862 | state = state.set<RefBindings>(sid, V ^ RefVal::ErrorLeakReturned); |
| 1863 | return std::make_pair(state, true); |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1864 | } |
| 1865 | } |
Ted Kremenek | 896cd9d | 2008-10-23 01:56:15 +0000 | [diff] [blame] | 1866 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1867 | // All other cases. |
| 1868 | |
| 1869 | hasLeak = V.isOwned() || |
| 1870 | ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1871 | |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1872 | if (!hasLeak) |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1873 | return std::make_pair(state.remove<RefBindings>(sid), false); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1874 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1875 | return std::make_pair(state.set<RefBindings>(sid, V ^ RefVal::ErrorLeak), |
| 1876 | false); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | void CFRefCount::EvalEndPath(GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1880 | GREndPathNodeBuilder<GRState>& Builder) { |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1881 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1882 | const GRState* St = Builder.getState(); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1883 | RefBindings B = St->get<RefBindings>(); |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1884 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1885 | llvm::SmallVector<std::pair<SymbolRef, bool>, 10> Leaked; |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 1886 | const Decl* CodeDecl = &Eng.getGraph().getCodeDecl(); |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1887 | |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1888 | for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1889 | bool hasLeak = false; |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1890 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1891 | std::pair<GRStateRef, bool> X = |
| 1892 | HandleSymbolDeath(Eng.getStateManager(), St, CodeDecl, |
| 1893 | (*I).first, (*I).second, hasLeak); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1894 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1895 | St = X.first; |
| 1896 | if (hasLeak) Leaked.push_back(std::make_pair((*I).first, X.second)); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1897 | } |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1898 | |
| 1899 | if (Leaked.empty()) |
| 1900 | return; |
| 1901 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1902 | ExplodedNode<GRState>* N = Builder.MakeNode(St); |
Ted Kremenek | 4f28515 | 2008-04-18 16:30:14 +0000 | [diff] [blame] | 1903 | |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1904 | if (!N) |
Ted Kremenek | 4f28515 | 2008-04-18 16:30:14 +0000 | [diff] [blame] | 1905 | return; |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1906 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1907 | std::vector<std::pair<SymbolRef,bool> >*& LeaksAtNode = Leaks[N]; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1908 | assert (!LeaksAtNode); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1909 | LeaksAtNode = new std::vector<std::pair<SymbolRef,bool> >(); |
Ted Kremenek | db86371 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1910 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1911 | for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1912 | I = Leaked.begin(), E = Leaked.end(); I != E; ++I) |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1913 | (*LeaksAtNode).push_back(*I); |
Ted Kremenek | e7bd9c2 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1916 | // Dead symbols. |
| 1917 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1918 | void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1919 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1920 | GRStmtNodeBuilder<GRState>& Builder, |
| 1921 | ExplodedNode<GRState>* Pred, |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 1922 | Stmt* S, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1923 | const GRState* St, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1924 | SymbolReaper& SymReaper) { |
Ted Kremenek | 910e999 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 1925 | |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1926 | // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor. |
| 1927 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1928 | RefBindings B = St->get<RefBindings>(); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1929 | llvm::SmallVector<std::pair<SymbolRef,bool>, 10> Leaked; |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1930 | |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1931 | for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(), |
| 1932 | E = SymReaper.dead_end(); I != E; ++I) { |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1933 | |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1934 | const RefVal* T = B.lookup(*I); |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 1935 | if (!T) continue; |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1936 | |
| 1937 | bool hasLeak = false; |
| 1938 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1939 | std::pair<GRStateRef, bool> X |
| 1940 | = HandleSymbolDeath(Eng.getStateManager(), St, 0, *I, *T, hasLeak); |
| 1941 | |
| 1942 | St = X.first; |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1943 | |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1944 | if (hasLeak) |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1945 | Leaked.push_back(std::make_pair(*I,X.second)); |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | if (Leaked.empty()) |
| 1949 | return; |
| 1950 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1951 | ExplodedNode<GRState>* N = Builder.MakeNode(Dst, S, Pred, St); |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1952 | |
| 1953 | if (!N) |
| 1954 | return; |
| 1955 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1956 | std::vector<std::pair<SymbolRef,bool> >*& LeaksAtNode = Leaks[N]; |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1957 | assert (!LeaksAtNode); |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1958 | LeaksAtNode = new std::vector<std::pair<SymbolRef,bool> >(); |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1959 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1960 | for (llvm::SmallVector<std::pair<SymbolRef,bool>, 10>::iterator |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 1961 | I = Leaked.begin(), E = Leaked.end(); I != E; ++I) |
Ted Kremenek | 652adc6 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1962 | (*LeaksAtNode).push_back(*I); |
| 1963 | } |
| 1964 | |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1965 | // Return statements. |
| 1966 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1967 | void CFRefCount::EvalReturn(ExplodedNodeSet<GRState>& Dst, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1968 | GRExprEngine& Eng, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1969 | GRStmtNodeBuilder<GRState>& Builder, |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1970 | ReturnStmt* S, |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1971 | ExplodedNode<GRState>* Pred) { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1972 | |
| 1973 | Expr* RetE = S->getRetValue(); |
| 1974 | if (!RetE) return; |
| 1975 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1976 | GRStateRef state(Builder.GetState(Pred), Eng.getStateManager()); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1977 | SVal V = state.GetSVal(RetE); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1978 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 1979 | if (!isa<loc::SymbolVal>(V)) |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1980 | return; |
| 1981 | |
| 1982 | // Get the reference count binding (if any). |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 1983 | SymbolRef Sym = cast<loc::SymbolVal>(V).getSymbol(); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1984 | const RefVal* T = state.get<RefBindings>(Sym); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1985 | |
| 1986 | if (!T) |
| 1987 | return; |
| 1988 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1989 | // Change the reference count. |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 1990 | RefVal X = *T; |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1991 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 1992 | switch (X.getKind()) { |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1993 | case RefVal::Owned: { |
| 1994 | unsigned cnt = X.getCount(); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 1995 | assert (cnt > 0); |
| 1996 | X = RefVal::makeReturnedOwned(cnt - 1); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1997 | break; |
| 1998 | } |
| 1999 | |
| 2000 | case RefVal::NotOwned: { |
| 2001 | unsigned cnt = X.getCount(); |
| 2002 | X = cnt ? RefVal::makeReturnedOwned(cnt - 1) |
| 2003 | : RefVal::makeReturnedNotOwned(); |
| 2004 | break; |
| 2005 | } |
| 2006 | |
| 2007 | default: |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2008 | return; |
| 2009 | } |
| 2010 | |
| 2011 | // Update the binding. |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 2012 | state = state.set<RefBindings>(Sym, X); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2013 | Builder.MakeNode(Dst, S, Pred, state); |
Ted Kremenek | 4fd8897 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2016 | // Assumptions. |
| 2017 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2018 | const GRState* CFRefCount::EvalAssume(GRStateManager& VMgr, |
| 2019 | const GRState* St, |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2020 | SVal Cond, bool Assumption, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 2021 | bool& isFeasible) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2022 | |
| 2023 | // FIXME: We may add to the interface of EvalAssume the list of symbols |
| 2024 | // whose assumptions have changed. For now we just iterate through the |
| 2025 | // bindings and check if any of the tracked symbols are NULL. This isn't |
| 2026 | // too bad since the number of symbols we will track in practice are |
| 2027 | // probably small and EvalAssume is only called at branches and a few |
| 2028 | // other places. |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2029 | RefBindings B = St->get<RefBindings>(); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2030 | |
| 2031 | if (B.isEmpty()) |
| 2032 | return St; |
| 2033 | |
| 2034 | bool changed = false; |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 2035 | |
| 2036 | GRStateRef state(St, VMgr); |
| 2037 | RefBindings::Factory& RefBFactory = state.get_context<RefBindings>(); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2038 | |
| 2039 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2040 | // Check if the symbol is null (or equal to any constant). |
| 2041 | // If this is the case, stop tracking the symbol. |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame] | 2042 | if (VMgr.getSymVal(St, I.getKey())) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2043 | changed = true; |
| 2044 | B = RefBFactory.Remove(B, I.getKey()); |
| 2045 | } |
| 2046 | } |
| 2047 | |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 2048 | if (changed) |
| 2049 | state = state.set<RefBindings>(B); |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2050 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2051 | return state; |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2052 | } |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2053 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2054 | RefBindings CFRefCount::Update(RefBindings B, SymbolRef sym, |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2055 | RefVal V, ArgEffect E, |
Ted Kremenek | b9d17f9 | 2008-08-17 03:20:02 +0000 | [diff] [blame] | 2056 | RefVal::Kind& hasErr, |
| 2057 | RefBindings::Factory& RefBFactory) { |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2058 | |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2059 | // FIXME: This dispatch can potentially be sped up by unifiying it into |
| 2060 | // a single switch statement. Opt for simplicity for now. |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2061 | |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2062 | switch (E) { |
| 2063 | default: |
| 2064 | assert (false && "Unhandled CFRef transition."); |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 2065 | |
| 2066 | case MayEscape: |
| 2067 | if (V.getKind() == RefVal::Owned) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2068 | V = V ^ RefVal::NotOwned; |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | } |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 2071 | // Fall-through. |
Ted Kremenek | 070a825 | 2008-07-09 18:11:16 +0000 | [diff] [blame] | 2072 | case DoNothingByRef: |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2073 | case DoNothing: |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2074 | if (!isGCEnabled() && V.getKind() == RefVal::Released) { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2075 | V = V ^ RefVal::ErrorUseAfterRelease; |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2076 | hasErr = V.getKind(); |
Ted Kremenek | 00a3a5f | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 2077 | break; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2078 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2079 | return B; |
Ted Kremenek | e19f449 | 2008-06-30 16:57:41 +0000 | [diff] [blame] | 2080 | |
Ted Kremenek | 80d753f | 2008-07-01 00:01:02 +0000 | [diff] [blame] | 2081 | case Autorelease: |
Ted Kremenek | 1499389 | 2008-05-06 02:41:27 +0000 | [diff] [blame] | 2082 | case StopTracking: |
| 2083 | return RefBFactory.Remove(B, sym); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2084 | |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2085 | case IncRef: |
| 2086 | switch (V.getKind()) { |
| 2087 | default: |
| 2088 | assert(false); |
| 2089 | |
| 2090 | case RefVal::Owned: |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2091 | case RefVal::NotOwned: |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2092 | V = V + 1; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2093 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2094 | case RefVal::Released: |
Ted Kremenek | d3dbcf4 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 2095 | if (isGCEnabled()) |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2096 | V = V ^ RefVal::Owned; |
Ted Kremenek | 65c9165 | 2008-04-29 05:44:10 +0000 | [diff] [blame] | 2097 | else { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2098 | V = V ^ RefVal::ErrorUseAfterRelease; |
Ted Kremenek | 65c9165 | 2008-04-29 05:44:10 +0000 | [diff] [blame] | 2099 | hasErr = V.getKind(); |
| 2100 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2101 | break; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2102 | } |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 2103 | break; |
| 2104 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2105 | case SelfOwn: |
| 2106 | V = V ^ RefVal::NotOwned; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2107 | // Fall-through. |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2108 | case DecRef: |
| 2109 | switch (V.getKind()) { |
| 2110 | default: |
| 2111 | assert (false); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2112 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2113 | case RefVal::Owned: |
| 2114 | V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2115 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2116 | |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2117 | case RefVal::NotOwned: |
| 2118 | if (V.getCount() > 0) |
| 2119 | V = V - 1; |
Ted Kremenek | 61b9f87 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 2120 | else { |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2121 | V = V ^ RefVal::ErrorReleaseNotOwned; |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2122 | hasErr = V.getKind(); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2123 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2124 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2125 | |
| 2126 | case RefVal::Released: |
Ted Kremenek | 553cf18 | 2008-06-25 21:21:56 +0000 | [diff] [blame] | 2127 | V = V ^ RefVal::ErrorUseAfterRelease; |
Ted Kremenek | 9ed18e6 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 2128 | hasErr = V.getKind(); |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2129 | break; |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2130 | } |
Ted Kremenek | 940b1d8 | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 2131 | break; |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2132 | } |
Ted Kremenek | 1ac08d6 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 2133 | return RefBFactory.Add(B, sym, V); |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2136 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2137 | // Error reporting. |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2138 | //===----------------------------------------------------------------------===// |
| 2139 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2140 | namespace { |
| 2141 | |
| 2142 | //===-------------===// |
| 2143 | // Bug Descriptions. // |
| 2144 | //===-------------===// |
| 2145 | |
Ted Kremenek | 95cc1ba | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 2146 | class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation { |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2147 | protected: |
| 2148 | CFRefCount& TF; |
| 2149 | |
| 2150 | public: |
| 2151 | CFRefBug(CFRefCount& tf) : TF(tf) {} |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2152 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 2153 | CFRefCount& getTF() { return TF; } |
Ted Kremenek | 789deac | 2008-05-05 23:16:31 +0000 | [diff] [blame] | 2154 | const CFRefCount& getTF() const { return TF; } |
| 2155 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2156 | virtual bool isLeak() const { return false; } |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 2157 | |
| 2158 | const char* getCategory() const { |
Ted Kremenek | 062bae0 | 2008-09-27 22:02:42 +0000 | [diff] [blame] | 2159 | return "Memory (Core Foundation/Objective-C)"; |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 2160 | } |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2161 | }; |
| 2162 | |
| 2163 | class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug { |
| 2164 | public: |
| 2165 | UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {} |
| 2166 | |
| 2167 | virtual const char* getName() const { |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 2168 | return "use-after-release"; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2169 | } |
| 2170 | virtual const char* getDescription() const { |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2171 | return "Reference-counted object is used after it is released."; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
| 2174 | virtual void EmitWarnings(BugReporter& BR); |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2175 | }; |
| 2176 | |
| 2177 | class VISIBILITY_HIDDEN BadRelease : public CFRefBug { |
| 2178 | public: |
| 2179 | BadRelease(CFRefCount& tf) : CFRefBug(tf) {} |
| 2180 | |
| 2181 | virtual const char* getName() const { |
Ted Kremenek | 8c036c7 | 2008-09-20 04:23:38 +0000 | [diff] [blame] | 2182 | return "bad release"; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2183 | } |
| 2184 | virtual const char* getDescription() const { |
| 2185 | return "Incorrect decrement of the reference count of a " |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2186 | "CoreFoundation object: " |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2187 | "The object is not owned at this point by the caller."; |
| 2188 | } |
| 2189 | |
| 2190 | virtual void EmitWarnings(BugReporter& BR); |
| 2191 | }; |
| 2192 | |
| 2193 | class VISIBILITY_HIDDEN Leak : public CFRefBug { |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2194 | bool isReturn; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2195 | public: |
| 2196 | Leak(CFRefCount& tf) : CFRefBug(tf) {} |
| 2197 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2198 | void setIsReturn(bool x) { isReturn = x; } |
| 2199 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2200 | virtual const char* getName() const { |
Ted Kremenek | 432af59 | 2008-05-06 18:11:36 +0000 | [diff] [blame] | 2201 | |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2202 | if (!isReturn) { |
| 2203 | if (getTF().isGCEnabled()) |
| 2204 | return "leak (GC)"; |
| 2205 | |
| 2206 | if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC) |
| 2207 | return "leak (hybrid MM, non-GC)"; |
| 2208 | |
| 2209 | assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC); |
| 2210 | return "leak"; |
| 2211 | } |
| 2212 | else { |
| 2213 | if (getTF().isGCEnabled()) |
Ted Kremenek | 9d1d570 | 2008-10-24 21:22:44 +0000 | [diff] [blame] | 2214 | return "[naming convention] leak of returned object (GC)"; |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2215 | |
| 2216 | if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC) |
Ted Kremenek | 9d1d570 | 2008-10-24 21:22:44 +0000 | [diff] [blame] | 2217 | return "[naming convention] leak of returned object (hybrid MM, " |
| 2218 | "non-GC)"; |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2219 | |
| 2220 | assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC); |
Ted Kremenek | 9d1d570 | 2008-10-24 21:22:44 +0000 | [diff] [blame] | 2221 | return "[naming convention] leak of returned object"; |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2222 | } |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2223 | } |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2224 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2225 | virtual void EmitWarnings(BugReporter& BR); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2226 | virtual void GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2227 | virtual bool isLeak() const { return true; } |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2228 | virtual bool isCached(BugReport& R); |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2229 | }; |
| 2230 | |
| 2231 | //===---------===// |
| 2232 | // Bug Reports. // |
| 2233 | //===---------===// |
| 2234 | |
| 2235 | class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2236 | SymbolRef Sym; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2237 | public: |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2238 | CFRefReport(CFRefBug& D, ExplodedNode<GRState> *n, SymbolRef sym) |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2239 | : RangedBugReport(D, n), Sym(sym) {} |
| 2240 | |
| 2241 | virtual ~CFRefReport() {} |
| 2242 | |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 2243 | CFRefBug& getBugType() { |
| 2244 | return (CFRefBug&) RangedBugReport::getBugType(); |
| 2245 | } |
| 2246 | const CFRefBug& getBugType() const { |
| 2247 | return (const CFRefBug&) RangedBugReport::getBugType(); |
| 2248 | } |
| 2249 | |
| 2250 | virtual void getRanges(BugReporter& BR, const SourceRange*& beg, |
| 2251 | const SourceRange*& end) { |
| 2252 | |
Ted Kremenek | e92c1b2 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 2253 | if (!getBugType().isLeak()) |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 2254 | RangedBugReport::getRanges(BR, beg, end); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2255 | else |
| 2256 | beg = end = 0; |
Ted Kremenek | bb77e9b | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2259 | SymbolRef getSymbol() const { return Sym; } |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2260 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2261 | PathDiagnosticPiece* getEndPath(BugReporter& BR, |
| 2262 | const ExplodedNode<GRState>* N); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2263 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2264 | std::pair<const char**,const char**> getExtraDescriptiveText(); |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2265 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2266 | PathDiagnosticPiece* VisitNode(const ExplodedNode<GRState>* N, |
| 2267 | const ExplodedNode<GRState>* PrevN, |
| 2268 | const ExplodedGraph<GRState>& G, |
| 2269 | BugReporter& BR); |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2270 | }; |
| 2271 | |
| 2272 | |
| 2273 | } // end anonymous namespace |
| 2274 | |
| 2275 | void CFRefCount::RegisterChecks(GRExprEngine& Eng) { |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2276 | Eng.Register(new UseAfterRelease(*this)); |
| 2277 | Eng.Register(new BadRelease(*this)); |
| 2278 | Eng.Register(new Leak(*this)); |
| 2279 | } |
| 2280 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2281 | |
| 2282 | static const char* Msgs[] = { |
| 2283 | "Code is compiled in garbage collection only mode" // GC only |
| 2284 | " (the bug occurs with garbage collection enabled).", |
| 2285 | |
| 2286 | "Code is compiled without garbage collection.", // No GC. |
| 2287 | |
| 2288 | "Code is compiled for use with and without garbage collection (GC)." |
| 2289 | " The bug occurs with GC enabled.", // Hybrid, with GC. |
| 2290 | |
| 2291 | "Code is compiled for use with and without garbage collection (GC)." |
| 2292 | " The bug occurs in non-GC mode." // Hyrbird, without GC/ |
| 2293 | }; |
| 2294 | |
| 2295 | std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() { |
| 2296 | CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF(); |
| 2297 | |
| 2298 | switch (TF.getLangOptions().getGCMode()) { |
| 2299 | default: |
| 2300 | assert(false); |
Ted Kremenek | 31593ac | 2008-05-01 04:02:04 +0000 | [diff] [blame] | 2301 | |
| 2302 | case LangOptions::GCOnly: |
| 2303 | assert (TF.isGCEnabled()); |
Ted Kremenek | 9e476de | 2008-08-12 18:30:56 +0000 | [diff] [blame] | 2304 | return std::make_pair(&Msgs[0], &Msgs[0]+1); |
| 2305 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2306 | case LangOptions::NonGC: |
| 2307 | assert (!TF.isGCEnabled()); |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2308 | return std::make_pair(&Msgs[1], &Msgs[1]+1); |
| 2309 | |
| 2310 | case LangOptions::HybridGC: |
| 2311 | if (TF.isGCEnabled()) |
| 2312 | return std::make_pair(&Msgs[2], &Msgs[2]+1); |
| 2313 | else |
| 2314 | return std::make_pair(&Msgs[3], &Msgs[3]+1); |
| 2315 | } |
| 2316 | } |
| 2317 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2318 | PathDiagnosticPiece* CFRefReport::VisitNode(const ExplodedNode<GRState>* N, |
| 2319 | const ExplodedNode<GRState>* PrevN, |
| 2320 | const ExplodedGraph<GRState>& G, |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2321 | BugReporter& BR) { |
| 2322 | |
Ted Kremenek | 611a15a | 2009-01-28 05:29:13 +0000 | [diff] [blame] | 2323 | // Check if the type state has changed. |
| 2324 | GRStateManager &StMgr = cast<GRBugReporter>(BR).getStateManager(); |
| 2325 | GRStateRef PrevSt(PrevN->getState(), StMgr); |
| 2326 | GRStateRef CurrSt(N->getState(), StMgr); |
Ted Kremenek | 2098280 | 2009-01-28 05:06:46 +0000 | [diff] [blame] | 2327 | |
Ted Kremenek | 611a15a | 2009-01-28 05:29:13 +0000 | [diff] [blame] | 2328 | const RefVal* CurrT = CurrSt.get<RefBindings>(Sym); |
| 2329 | if (!CurrT) return NULL; |
| 2330 | |
| 2331 | const RefVal& CurrV = *CurrT; |
| 2332 | const RefVal* PrevT = PrevSt.get<RefBindings>(Sym); |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2333 | |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2334 | if (!PrevT) { |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2335 | std::string sbuf; |
| 2336 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2337 | |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2338 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 2339 | |
Ted Kremenek | 5c1cd52 | 2009-01-28 05:15:02 +0000 | [diff] [blame] | 2340 | if (CallExpr *CE = dyn_cast<CallExpr>(S)) { |
| 2341 | // Get the name of the callee (if it is available). |
| 2342 | SVal X = CurrSt.GetSVal(CE->getCallee()); |
| 2343 | if (loc::FuncVal* FV = dyn_cast<loc::FuncVal>(&X)) |
| 2344 | os << "Call to function '" << FV->getDecl()->getNameAsString() <<'\''; |
| 2345 | else |
| 2346 | os << "function call"; |
Ted Kremenek | 103a185 | 2009-01-28 05:18:08 +0000 | [diff] [blame] | 2347 | |
| 2348 | os << " returns an object with a "; |
Ted Kremenek | 5c1cd52 | 2009-01-28 05:15:02 +0000 | [diff] [blame] | 2349 | } |
| 2350 | else { |
| 2351 | assert (isa<ObjCMessageExpr>(S)); |
| 2352 | os << "Method returns an object with a "; |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2353 | } |
Ted Kremenek | 5c1cd52 | 2009-01-28 05:15:02 +0000 | [diff] [blame] | 2354 | |
| 2355 | if (CurrV.isOwned()) |
| 2356 | os << "+1 retain count (owning reference)."; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2357 | else { |
| 2358 | assert (CurrV.isNotOwned()); |
Ted Kremenek | 5c1cd52 | 2009-01-28 05:15:02 +0000 | [diff] [blame] | 2359 | os << "+0 retain count (non-owning reference)."; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2360 | } |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2361 | |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2362 | FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2363 | PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str()); |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2364 | |
| 2365 | if (Expr* Exp = dyn_cast<Expr>(S)) |
| 2366 | P->addRange(Exp->getSourceRange()); |
| 2367 | |
| 2368 | return P; |
| 2369 | } |
| 2370 | |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2371 | // Determine if the typestate has changed. |
Ted Kremenek | 611a15a | 2009-01-28 05:29:13 +0000 | [diff] [blame] | 2372 | RefVal PrevV = *PrevT; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2373 | |
| 2374 | if (PrevV == CurrV) |
| 2375 | return NULL; |
| 2376 | |
| 2377 | // The typestate has changed. |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2378 | std::string sbuf; |
| 2379 | llvm::raw_string_ostream os(sbuf); |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2380 | |
| 2381 | switch (CurrV.getKind()) { |
| 2382 | case RefVal::Owned: |
| 2383 | case RefVal::NotOwned: |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 2384 | |
| 2385 | if (PrevV.getCount() == CurrV.getCount()) |
| 2386 | return 0; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2387 | |
| 2388 | if (PrevV.getCount() > CurrV.getCount()) |
| 2389 | os << "Reference count decremented."; |
| 2390 | else |
| 2391 | os << "Reference count incremented."; |
| 2392 | |
Ted Kremenek | 3eabf1c | 2008-05-22 17:31:13 +0000 | [diff] [blame] | 2393 | if (unsigned Count = CurrV.getCount()) { |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2394 | os << " Object has +" << Count; |
Ted Kremenek | 79c140b | 2008-04-18 05:32:44 +0000 | [diff] [blame] | 2395 | |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2396 | if (Count > 1) |
| 2397 | os << " retain counts."; |
Ted Kremenek | 79c140b | 2008-04-18 05:32:44 +0000 | [diff] [blame] | 2398 | else |
Ted Kremenek | ce48e00 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2399 | os << " retain count."; |
Ted Kremenek | 79c140b | 2008-04-18 05:32:44 +0000 | [diff] [blame] | 2400 | } |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2401 | |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2402 | break; |
| 2403 | |
| 2404 | case RefVal::Released: |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2405 | os << "Object released."; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2406 | break; |
| 2407 | |
| 2408 | case RefVal::ReturnedOwned: |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2409 | os << "Object returned to caller as an owning reference (single retain " |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2410 | "count transferred to caller)."; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | |
| 2413 | case RefVal::ReturnedNotOwned: |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2414 | os << "Object returned to caller with a +0 (non-owning) retain count."; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2415 | break; |
| 2416 | |
| 2417 | default: |
| 2418 | return NULL; |
| 2419 | } |
| 2420 | |
| 2421 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 2422 | FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager()); |
Ted Kremenek | a1f117e | 2009-01-28 04:47:13 +0000 | [diff] [blame] | 2423 | PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, os.str()); |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2424 | |
| 2425 | // Add the range by scanning the children of the statement for any bindings |
| 2426 | // to Sym. |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2427 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 2428 | if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) { |
Ted Kremenek | 2098280 | 2009-01-28 05:06:46 +0000 | [diff] [blame] | 2429 | SVal X = CurrSt.GetSVal(Exp); |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2430 | if (loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&X)) |
Ted Kremenek | 2098280 | 2009-01-28 05:06:46 +0000 | [diff] [blame] | 2431 | if (SV->getSymbol() == Sym) P->addRange(Exp->getSourceRange()); break; |
Ted Kremenek | 2cf943a | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | return P; |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2437 | namespace { |
| 2438 | class VISIBILITY_HIDDEN FindUniqueBinding : |
| 2439 | public StoreManager::BindingsHandler { |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2440 | SymbolRef Sym; |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2441 | MemRegion* Binding; |
| 2442 | bool First; |
| 2443 | |
| 2444 | public: |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2445 | FindUniqueBinding(SymbolRef sym) : Sym(sym), Binding(0), First(true) {} |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2446 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2447 | bool HandleBinding(StoreManager& SMgr, Store store, MemRegion* R, SVal val) { |
| 2448 | if (const loc::SymbolVal* SV = dyn_cast<loc::SymbolVal>(&val)) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2449 | if (SV->getSymbol() != Sym) |
| 2450 | return true; |
| 2451 | } |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 2452 | else if (const nonloc::SymbolVal* SV=dyn_cast<nonloc::SymbolVal>(&val)) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2453 | if (SV->getSymbol() != Sym) |
| 2454 | return true; |
| 2455 | } |
| 2456 | else |
| 2457 | return true; |
| 2458 | |
| 2459 | if (Binding) { |
| 2460 | First = false; |
| 2461 | return false; |
| 2462 | } |
| 2463 | else |
| 2464 | Binding = R; |
| 2465 | |
| 2466 | return true; |
| 2467 | } |
| 2468 | |
| 2469 | operator bool() { return First && Binding; } |
| 2470 | MemRegion* getRegion() { return Binding; } |
| 2471 | }; |
| 2472 | } |
| 2473 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2474 | static std::pair<const ExplodedNode<GRState>*,const MemRegion*> |
| 2475 | GetAllocationSite(GRStateManager* StateMgr, const ExplodedNode<GRState>* N, |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2476 | SymbolRef Sym) { |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2477 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2478 | // Find both first node that referred to the tracked symbol and the |
| 2479 | // memory location that value was store to. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2480 | const ExplodedNode<GRState>* Last = N; |
| 2481 | const MemRegion* FirstBinding = 0; |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2482 | |
| 2483 | while (N) { |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2484 | const GRState* St = N->getState(); |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 2485 | RefBindings B = St->get<RefBindings>(); |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2486 | |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2487 | if (!B.lookup(Sym)) |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2488 | break; |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2489 | |
| 2490 | if (StateMgr) { |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2491 | FindUniqueBinding FB(Sym); |
| 2492 | StateMgr->iterBindings(St, FB); |
| 2493 | if (FB) FirstBinding = FB.getRegion(); |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2496 | Last = N; |
| 2497 | N = N->pred_empty() ? NULL : *(N->pred_begin()); |
| 2498 | } |
| 2499 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2500 | return std::make_pair(Last, FirstBinding); |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2501 | } |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2502 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2503 | PathDiagnosticPiece* |
| 2504 | CFRefReport::getEndPath(BugReporter& br, const ExplodedNode<GRState>* EndN) { |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 2505 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2506 | GRBugReporter& BR = cast<GRBugReporter>(br); |
| 2507 | |
Ted Kremenek | 1aa44c7 | 2008-05-22 23:45:19 +0000 | [diff] [blame] | 2508 | // Tell the BugReporter to report cases when the tracked symbol is |
| 2509 | // assigned to different variables, etc. |
Ted Kremenek | c095997 | 2008-07-02 21:24:01 +0000 | [diff] [blame] | 2510 | cast<GRBugReporter>(BR).addNotableSymbol(Sym); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2511 | |
| 2512 | if (!getBugType().isLeak()) |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2513 | return RangedBugReport::getEndPath(BR, EndN); |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 2514 | |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2515 | // 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] | 2516 | // symbol appeared, and also get the first VarDecl that tracked object |
| 2517 | // is stored to. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2518 | const ExplodedNode<GRState>* AllocNode = 0; |
| 2519 | const MemRegion* FirstBinding = 0; |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2520 | |
| 2521 | llvm::tie(AllocNode, FirstBinding) = |
| 2522 | GetAllocationSite(&BR.getStateManager(), EndN, Sym); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2523 | |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2524 | // Get the allocate site. |
| 2525 | assert (AllocNode); |
| 2526 | Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt(); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2527 | |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2528 | SourceManager& SMgr = BR.getContext().getSourceManager(); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 2529 | unsigned AllocLine =SMgr.getInstantiationLineNumber(FirstStmt->getLocStart()); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2530 | |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2531 | // Get the leak site. We may have multiple ExplodedNodes (one with the |
| 2532 | // leak) that occur on the same line number; if the node with the leak |
| 2533 | // has any immediate predecessor nodes with the same line number, find |
| 2534 | // any transitive-successors that have a different statement and use that |
| 2535 | // line number instead. This avoids emiting a diagnostic like: |
| 2536 | // |
| 2537 | // // 'y' is leaked. |
| 2538 | // int x = foo(y); |
| 2539 | // |
| 2540 | // instead we want: |
| 2541 | // |
| 2542 | // int x = foo(y); |
| 2543 | // // 'y' is leaked. |
| 2544 | |
| 2545 | Stmt* S = getStmt(BR); // This is the statement where the leak occured. |
| 2546 | assert (S); |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 2547 | unsigned EndLine = SMgr.getInstantiationLineNumber(S->getLocStart()); |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2548 | |
| 2549 | // Look in the *trimmed* graph at the immediate predecessor of EndN. Does |
| 2550 | // it occur on the same line? |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2551 | PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above; |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2552 | |
| 2553 | assert (!EndN->pred_empty()); // Not possible to have 0 predecessors. |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2554 | const ExplodedNode<GRState> *Pred = *(EndN->pred_begin()); |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2555 | ProgramPoint PredPos = Pred->getLocation(); |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2556 | |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2557 | if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) { |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2558 | |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2559 | Stmt* SPred = PredPS->getStmt(); |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2560 | |
| 2561 | // Predecessor at same line? |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 2562 | if (SMgr.getInstantiationLineNumber(SPred->getLocStart()) != EndLine) { |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2563 | Hint = PathDiagnosticPiece::Below; |
| 2564 | S = SPred; |
| 2565 | } |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2566 | } |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2567 | |
| 2568 | // Generate the diagnostic. |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2569 | FullSourceLoc L( S->getLocStart(), SMgr); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2570 | std::ostringstream os; |
Ted Kremenek | e92c1b2 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 2571 | |
Ted Kremenek | e28565b | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2572 | os << "Object allocated on line " << AllocLine; |
Ted Kremenek | e92c1b2 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 2573 | |
Ted Kremenek | 2bc39c6 | 2008-08-29 00:47:32 +0000 | [diff] [blame] | 2574 | if (FirstBinding) |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2575 | os << " and stored into '" << FirstBinding->getString() << '\''; |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2576 | |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 2577 | |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2578 | // Get the retain count. |
| 2579 | const RefVal* RV = EndN->getState()->get<RefBindings>(Sym); |
| 2580 | |
| 2581 | if (RV->getKind() == RefVal::ErrorLeakReturned) { |
Ted Kremenek | 04f9d46 | 2008-12-02 01:26:07 +0000 | [diff] [blame] | 2582 | // FIXME: Per comments in rdar://6320065, "create" only applies to CF |
| 2583 | // ojbects. Only "copy", "alloc", "retain" and "new" transfer ownership |
| 2584 | // to the caller for NS objects. |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2585 | ObjCMethodDecl& MD = cast<ObjCMethodDecl>(BR.getGraph().getCodeDecl()); |
| 2586 | os << " is returned from a method whose name ('" |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 2587 | << MD.getSelector().getAsString() |
Ted Kremenek | 234a4c2 | 2009-01-07 00:39:56 +0000 | [diff] [blame] | 2588 | << "') does not contain 'copy' or otherwise starts with" |
Ted Kremenek | 9d1d570 | 2008-10-24 21:22:44 +0000 | [diff] [blame] | 2589 | " 'new' or 'alloc'. This violates the naming convention rules given" |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2590 | " in the Memory Management Guide for Cocoa (object leaked)."; |
| 2591 | } |
| 2592 | else |
Ted Kremenek | 9d1d570 | 2008-10-24 21:22:44 +0000 | [diff] [blame] | 2593 | os << " is no longer referenced after this point and has a retain count of" |
| 2594 | " +" |
Ted Kremenek | 3ad2cc8 | 2008-10-22 23:56:21 +0000 | [diff] [blame] | 2595 | << RV->getCount() << " (object leaked)."; |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2596 | |
Ted Kremenek | a22cc2f | 2008-05-06 23:07:13 +0000 | [diff] [blame] | 2597 | return new PathDiagnosticPiece(L, os.str(), Hint); |
Ted Kremenek | c9fa2f7 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2600 | void UseAfterRelease::EmitWarnings(BugReporter& BR) { |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2601 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2602 | for (CFRefCount::use_after_iterator I = TF.use_after_begin(), |
| 2603 | E = TF.use_after_end(); I != E; ++I) { |
| 2604 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2605 | CFRefReport report(*this, I->first, I->second.second); |
| 2606 | report.addRange(I->second.first->getSourceRange()); |
Ted Kremenek | 75840e1 | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 2607 | BR.EmitWarning(report); |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2608 | } |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | void BadRelease::EmitWarnings(BugReporter& BR) { |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2612 | |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2613 | for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(), |
| 2614 | E = TF.bad_release_end(); I != E; ++I) { |
| 2615 | |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2616 | CFRefReport report(*this, I->first, I->second.second); |
| 2617 | report.addRange(I->second.first->getSourceRange()); |
| 2618 | BR.EmitWarning(report); |
Ted Kremenek | 05cbe1a | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2619 | } |
| 2620 | } |
Ted Kremenek | fa34b33 | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2621 | |
Ted Kremenek | 989d519 | 2008-04-17 23:43:50 +0000 | [diff] [blame] | 2622 | void Leak::EmitWarnings(BugReporter& BR) { |
| 2623 | |
| 2624 | for (CFRefCount::leaks_iterator I = TF.leaks_begin(), |
| 2625 | E = TF.leaks_end(); I != E; ++I) { |
| 2626 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2627 | std::vector<std::pair<SymbolRef, bool> >& SymV = *(I->second); |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2628 | unsigned n = SymV.size(); |
| 2629 | |
| 2630 | for (unsigned i = 0; i < n; ++i) { |
Ted Kremenek | f9790ae | 2008-10-24 20:32:50 +0000 | [diff] [blame] | 2631 | setIsReturn(SymV[i].second); |
| 2632 | CFRefReport report(*this, I->first, SymV[i].first); |
Ted Kremenek | 8dd5646 | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2633 | BR.EmitWarning(report); |
| 2634 | } |
Ted Kremenek | 989d519 | 2008-04-17 23:43:50 +0000 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 2638 | void Leak::GetErrorNodes(std::vector<ExplodedNode<GRState>*>& Nodes) { |
Ted Kremenek | cb61292 | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2639 | for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end(); |
| 2640 | I!=E; ++I) |
| 2641 | Nodes.push_back(I->first); |
| 2642 | } |
| 2643 | |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2644 | bool Leak::isCached(BugReport& R) { |
| 2645 | |
| 2646 | // Most bug reports are cached at the location where they occured. |
| 2647 | // 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] | 2648 | // allocated, and only report a single path. |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2649 | |
Ted Kremenek | 2dabd43 | 2008-12-05 02:27:51 +0000 | [diff] [blame] | 2650 | SymbolRef Sym = static_cast<CFRefReport&>(R).getSymbol(); |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2651 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 2652 | const ExplodedNode<GRState>* AllocNode = |
| 2653 | GetAllocationSite(0, R.getEndNode(), Sym).first; |
Ted Kremenek | 6ed9afc | 2008-05-16 18:33:44 +0000 | [diff] [blame] | 2654 | |
| 2655 | if (!AllocNode) |
| 2656 | return false; |
| 2657 | |
| 2658 | return BugTypeCacheLocation::isCached(AllocNode->getLocation()); |
| 2659 | } |
| 2660 | |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2661 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 2662 | // Transfer function creation for external clients. |
Ted Kremenek | 6b3a0f7 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2663 | //===----------------------------------------------------------------------===// |
| 2664 | |
Ted Kremenek | 072192b | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2665 | GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled, |
| 2666 | const LangOptions& lopts) { |
Ted Kremenek | 78d4624 | 2008-07-22 16:21:24 +0000 | [diff] [blame] | 2667 | return new CFRefCount(Ctx, GCEnabled, lopts); |
Ted Kremenek | 3ea0b6a | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 2668 | } |