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