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