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