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