Chris Lattner | be1a7a0 | 2008-03-15 23:59:48 +0000 | [diff] [blame] | 1 | // CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--// |
Ted Kremenek | 827f93b | 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 | 2224fcb | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines the methods for CFRefCount, which implements |
Ted Kremenek | 827f93b | 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 | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 15 | #include "GRSimpleVals.h" |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 16 | #include "clang/Basic/LangOptions.h" |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 17 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 18 | #include "clang/Analysis/PathSensitive/ValueState.h" |
Ted Kremenek | dd0126b | 2008-03-31 18:26:32 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/PathDiagnostic.h" |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/LocalCheckers.h" |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/PathDiagnostic.h" |
| 22 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
Ted Kremenek | a7338b4 | 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 | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 27 | #include <ostream> |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 28 | #include <sstream> |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace clang; |
| 31 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 32 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 33 | // Utility functions. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
Ted Kremenek | 1bd6ddb | 2008-05-01 18:31:44 +0000 | [diff] [blame] | 36 | static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 37 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 38 | return Ctx.Selectors.getSelector(0, &II); |
| 39 | } |
| 40 | |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 41 | static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) { |
| 42 | IdentifierInfo* II = &Ctx.Idents.get(name); |
| 43 | return Ctx.Selectors.getSelector(1, &II); |
| 44 | } |
| 45 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 46 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 47 | // Symbolic Evaluation of Reference Counting Logic |
| 48 | //===----------------------------------------------------------------------===// |
| 49 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 50 | namespace { |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame^] | 51 | enum ArgEffect { IncRef, DecRef, DoNothing, StopTracking }; |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 52 | typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 53 | } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 55 | namespace llvm { |
| 56 | template <> struct FoldingSetTrait<ArgEffects> { |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 57 | static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) { |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 58 | for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) { |
| 59 | ID.AddInteger(I->first); |
| 60 | ID.AddInteger((unsigned) I->second); |
| 61 | } |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 62 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 63 | }; |
| 64 | } // end llvm namespace |
| 65 | |
| 66 | namespace { |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 68 | class RetEffect { |
| 69 | public: |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 70 | enum Kind { NoRet = 0x0, Alias = 0x1, OwnedSymbol = 0x2, |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 71 | NotOwnedSymbol = 0x3, ReceiverAlias=0x4 }; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | unsigned Data; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 75 | RetEffect(Kind k, unsigned D) { Data = (D << 3) | (unsigned) k; } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 76 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 77 | public: |
| 78 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 79 | Kind getKind() const { return (Kind) (Data & 0x7); } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 80 | |
| 81 | unsigned getValue() const { |
| 82 | assert(getKind() == Alias); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 83 | return Data >> 3; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 84 | } |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 85 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 86 | static RetEffect MakeAlias(unsigned Idx) { return RetEffect(Alias, Idx); } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 87 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 88 | static RetEffect MakeReceiverAlias() { return RetEffect(ReceiverAlias, 0); } |
| 89 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 90 | static RetEffect MakeOwned() { return RetEffect(OwnedSymbol, 0); } |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 91 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 92 | static RetEffect MakeNotOwned() { return RetEffect(NotOwnedSymbol, 0); } |
| 93 | |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 94 | static RetEffect MakeNoRet() { return RetEffect(NoRet, 0); } |
| 95 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 96 | operator Kind() const { return getKind(); } |
| 97 | |
| 98 | void Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger(Data); } |
| 99 | }; |
| 100 | |
| 101 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 102 | class RetainSummary : public llvm::FoldingSetNode { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 103 | ArgEffects* Args; |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 104 | ArgEffect Receiver; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 105 | RetEffect Ret; |
| 106 | public: |
| 107 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 108 | RetainSummary(ArgEffects* A, RetEffect R, ArgEffect ReceiverEff = DoNothing) |
| 109 | : Args(A), Receiver(ReceiverEff), Ret(R) {} |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 110 | |
| 111 | unsigned getNumArgs() const { return Args->size(); } |
| 112 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 113 | ArgEffect getArg(unsigned idx) const { |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 114 | if (!Args) |
| 115 | return DoNothing; |
| 116 | |
| 117 | // If Args is present, it is likely to contain only 1 element. |
| 118 | // Just do a linear search. Do it from the back because functions with |
| 119 | // large numbers of arguments will be tail heavy with respect to which |
| 120 | // argument they actually modify with respect to the reference count. |
| 121 | |
| 122 | for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend(); |
| 123 | I!=E; ++I) { |
| 124 | |
| 125 | if (idx > I->first) |
| 126 | return DoNothing; |
| 127 | |
| 128 | if (idx == I->first) |
| 129 | return I->second; |
| 130 | } |
| 131 | |
| 132 | return DoNothing; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 135 | RetEffect getRetEffect() const { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 136 | return Ret; |
| 137 | } |
| 138 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 139 | ArgEffect getReceiverEffect() const { |
| 140 | return Receiver; |
| 141 | } |
| 142 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 143 | typedef ArgEffects::const_iterator arg_iterator; |
| 144 | |
| 145 | arg_iterator begin_args() const { return Args->begin(); } |
| 146 | arg_iterator end_args() const { return Args->end(); } |
| 147 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 148 | static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A, |
| 149 | RetEffect RetEff, ArgEffect ReceiverEff) { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 150 | ID.AddPointer(A); |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 151 | ID.Add(RetEff); |
| 152 | ID.AddInteger((unsigned) ReceiverEff); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void Profile(llvm::FoldingSetNodeID& ID) const { |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 156 | Profile(ID, Args, Ret, Receiver); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 157 | } |
| 158 | }; |
| 159 | |
| 160 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 161 | class RetainSummaryManager { |
| 162 | |
| 163 | //==-----------------------------------------------------------------==// |
| 164 | // Typedefs. |
| 165 | //==-----------------------------------------------------------------==// |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 167 | typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> > |
| 168 | ArgEffectsSetTy; |
| 169 | |
| 170 | typedef llvm::FoldingSet<RetainSummary> |
| 171 | SummarySetTy; |
| 172 | |
| 173 | typedef llvm::DenseMap<FunctionDecl*, RetainSummary*> |
| 174 | FuncSummariesTy; |
| 175 | |
| 176 | typedef llvm::DenseMap<Selector, RetainSummary*> |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 177 | ObjCMethSummariesTy; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 178 | |
| 179 | //==-----------------------------------------------------------------==// |
| 180 | // Data. |
| 181 | //==-----------------------------------------------------------------==// |
| 182 | |
| 183 | // Ctx - The ASTContext object for the analyzed ASTs. |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 184 | ASTContext& Ctx; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 185 | |
| 186 | // GCEnabled - Records whether or not the analyzed code runs in GC mode. |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 187 | const bool GCEnabled; |
| 188 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 189 | // SummarySet - A FoldingSet of uniqued summaries. |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 190 | SummarySetTy SummarySet; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 191 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 192 | // FuncSummaries - A map from FunctionDecls to summaries. |
| 193 | FuncSummariesTy FuncSummaries; |
| 194 | |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 195 | // ObjCInstMethSummaries - A map from selectors (for instance methods) |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 196 | // to summaries. |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 197 | ObjCMethSummariesTy ObjCInstMethSummaries; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 199 | // ObjCMethSummaries - A map from selectors to summaries. |
| 200 | ObjCMethSummariesTy ObjCMethSummaries; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 201 | |
| 202 | // ArgEffectsSet - A FoldingSet of uniqued ArgEffects. |
| 203 | ArgEffectsSetTy ArgEffectsSet; |
| 204 | |
| 205 | // BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects, |
| 206 | // and all other data used by the checker. |
| 207 | llvm::BumpPtrAllocator BPAlloc; |
| 208 | |
| 209 | // ScratchArgs - A holding buffer for construct ArgEffects. |
| 210 | ArgEffects ScratchArgs; |
| 211 | |
| 212 | //==-----------------------------------------------------------------==// |
| 213 | // Methods. |
| 214 | //==-----------------------------------------------------------------==// |
| 215 | |
| 216 | // getArgEffects - Returns a persistent ArgEffects object based on the |
| 217 | // data in ScratchArgs. |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 218 | ArgEffects* getArgEffects(); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 220 | enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable }; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 221 | RetainSummary* getUnarySummary(FunctionDecl* FD, UnaryFuncKind func); |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 223 | RetainSummary* getNSSummary(FunctionDecl* FD, const char* FName); |
| 224 | RetainSummary* getCFSummary(FunctionDecl* FD, const char* FName); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 225 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 226 | RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD); |
| 227 | RetainSummary* getCFSummaryGetRule(FunctionDecl* FD); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 229 | RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff, |
| 230 | ArgEffect ReceiverEff = DoNothing); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 231 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 232 | RetainSummary* getPersistentSummary(RetEffect RE, |
| 233 | ArgEffect ReceiverEff = DoNothing) { |
| 234 | return getPersistentSummary(getArgEffects(), RE, ReceiverEff); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 235 | } |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 236 | |
| 237 | RetainSummary* getInstanceMethodSummary(Selector S); |
| 238 | |
| 239 | RetainSummary* getMethodSummary(Selector S); |
| 240 | RetainSummary* getInitMethodSummary(Selector S); |
| 241 | |
Ted Kremenek | 83b2cde | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 242 | void InitializeInstMethSummaries(); |
| 243 | void InitializeMethSummaries(); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 244 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 245 | public: |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 246 | |
| 247 | RetainSummaryManager(ASTContext& ctx, bool gcenabled) |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 248 | : Ctx(ctx), GCEnabled(gcenabled) { |
| 249 | |
Ted Kremenek | 83b2cde | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 250 | InitializeInstMethSummaries(); |
| 251 | InitializeMethSummaries(); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 252 | } |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 253 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 254 | ~RetainSummaryManager(); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 256 | RetainSummary* getSummary(FunctionDecl* FD, ASTContext& Ctx); |
| 257 | |
| 258 | bool isGCEnabled() const { return GCEnabled; } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | } // end anonymous namespace |
| 262 | |
| 263 | //===----------------------------------------------------------------------===// |
| 264 | // Implementation of checker data structures. |
| 265 | //===----------------------------------------------------------------------===// |
| 266 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 267 | RetainSummaryManager::~RetainSummaryManager() { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 268 | |
| 269 | // FIXME: The ArgEffects could eventually be allocated from BPAlloc, |
| 270 | // mitigating the need to do explicit cleanup of the |
| 271 | // Argument-Effect summaries. |
| 272 | |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 273 | for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(), |
| 274 | E = ArgEffectsSet.end(); I!=E; ++I) |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 275 | I->getValue().~ArgEffects(); |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 276 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 277 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 278 | ArgEffects* RetainSummaryManager::getArgEffects() { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 279 | |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 280 | if (ScratchArgs.empty()) |
| 281 | return NULL; |
| 282 | |
| 283 | // Compute a profile for a non-empty ScratchArgs. |
| 284 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 285 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 287 | profile.Add(ScratchArgs); |
| 288 | void* InsertPos; |
| 289 | |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 290 | // Look up the uniqued copy, or create a new one. |
| 291 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 292 | llvm::FoldingSetNodeWrapper<ArgEffects>* E = |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 293 | ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 295 | if (E) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 296 | ScratchArgs.clear(); |
| 297 | return &E->getValue(); |
| 298 | } |
| 299 | |
| 300 | E = (llvm::FoldingSetNodeWrapper<ArgEffects>*) |
| 301 | BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >(); |
| 302 | |
| 303 | new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 304 | ArgEffectsSet.InsertNode(E, InsertPos); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 305 | |
| 306 | ScratchArgs.clear(); |
| 307 | return &E->getValue(); |
| 308 | } |
| 309 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 310 | RetainSummary* |
| 311 | RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff, |
| 312 | ArgEffect ReceiverEff) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 314 | // Generate a profile for the summary. |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 315 | llvm::FoldingSetNodeID profile; |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 316 | RetainSummary::Profile(profile, AE, RetEff, ReceiverEff); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 317 | |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 318 | // Look up the uniqued summary, or create one if it doesn't exist. |
| 319 | void* InsertPos; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 320 | RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 321 | |
| 322 | if (Summ) |
| 323 | return Summ; |
| 324 | |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 325 | // Create the summary and return it. |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 326 | Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>(); |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 327 | new (Summ) RetainSummary(AE, RetEff, ReceiverEff); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 328 | SummarySet.InsertNode(Summ, InsertPos); |
| 329 | |
| 330 | return Summ; |
| 331 | } |
| 332 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 333 | //===----------------------------------------------------------------------===// |
| 334 | // Summary creation for functions (largely uses of Core Foundation). |
| 335 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 336 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 337 | RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD, |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 338 | ASTContext& Ctx) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 339 | |
| 340 | SourceLocation Loc = FD->getLocation(); |
| 341 | |
| 342 | if (!Loc.isFileID()) |
| 343 | return NULL; |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 344 | |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 345 | // Look up a summary in our cache of FunctionDecls -> Summaries. |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 346 | FuncSummariesTy::iterator I = FuncSummaries.find(FD); |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 347 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 348 | if (I != FuncSummaries.end()) |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 349 | return I->second; |
| 350 | |
| 351 | // No summary. Generate one. |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 352 | const char* FName = FD->getIdentifier()->getName(); |
| 353 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 354 | RetainSummary *S = 0; |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 355 | |
| 356 | if (FName[0] == 'C' && FName[1] == 'F') |
| 357 | S = getCFSummary(FD, FName); |
| 358 | else if (FName[0] == 'N' && FName[1] == 'S') |
| 359 | S = getNSSummary(FD, FName); |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 361 | FuncSummaries[FD] = S; |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 362 | return S; |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 365 | RetainSummary* RetainSummaryManager::getNSSummary(FunctionDecl* FD, |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 366 | const char* FName) { |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 367 | FName += 2; |
| 368 | |
| 369 | if (strcmp(FName, "MakeCollectable") == 0) |
| 370 | return getUnarySummary(FD, cfmakecollectable); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 375 | RetainSummary* RetainSummaryManager::getCFSummary(FunctionDecl* FD, |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 376 | const char* FName) { |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 377 | |
| 378 | FName += 2; |
| 379 | |
| 380 | if (strcmp(FName, "Retain") == 0) |
| 381 | return getUnarySummary(FD, cfretain); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 382 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 383 | if (strcmp(FName, "Release") == 0) |
| 384 | return getUnarySummary(FD, cfrelease); |
| 385 | |
| 386 | if (strcmp(FName, "MakeCollectable") == 0) |
| 387 | return getUnarySummary(FD, cfmakecollectable); |
| 388 | |
| 389 | if (strstr(FName, "Create") || strstr(FName, "Copy")) |
| 390 | return getCFSummaryCreateRule(FD); |
| 391 | |
| 392 | if (strstr(FName, "Get")) |
| 393 | return getCFSummaryGetRule(FD); |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 398 | RetainSummary* |
| 399 | RetainSummaryManager::getUnarySummary(FunctionDecl* FD, UnaryFuncKind func) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 400 | |
| 401 | FunctionTypeProto* FT = |
| 402 | dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr()); |
| 403 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 404 | if (FT) { |
| 405 | |
| 406 | if (FT->getNumArgs() != 1) |
| 407 | return 0; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 408 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 409 | TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr()); |
| 410 | |
| 411 | if (!ArgT) |
| 412 | return 0; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 414 | if (!ArgT->isPointerType()) |
| 415 | return NULL; |
| 416 | } |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 417 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 418 | assert (ScratchArgs.empty()); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 419 | |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 420 | switch (func) { |
| 421 | case cfretain: { |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 422 | ScratchArgs.push_back(std::make_pair(0, IncRef)); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 423 | return getPersistentSummary(RetEffect::MakeAlias(0)); |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | case cfrelease: { |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 427 | ScratchArgs.push_back(std::make_pair(0, DecRef)); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 428 | return getPersistentSummary(RetEffect::MakeNoRet()); |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | case cfmakecollectable: { |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 432 | if (GCEnabled) |
| 433 | ScratchArgs.push_back(std::make_pair(0, DecRef)); |
| 434 | |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 435 | return getPersistentSummary(RetEffect::MakeAlias(0)); |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | default: |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 439 | assert (false && "Not a supported unary function."); |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 440 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static bool isCFRefType(QualType T) { |
| 444 | |
| 445 | if (!T->isPointerType()) |
| 446 | return false; |
| 447 | |
| 448 | // Check the typedef for the name "CF" and the substring "Ref". |
| 449 | |
| 450 | TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr()); |
| 451 | |
| 452 | if (!TD) |
| 453 | return false; |
| 454 | |
| 455 | const char* TDName = TD->getDecl()->getIdentifier()->getName(); |
| 456 | assert (TDName); |
| 457 | |
| 458 | if (TDName[0] != 'C' || TDName[1] != 'F') |
| 459 | return false; |
| 460 | |
| 461 | if (strstr(TDName, "Ref") == 0) |
| 462 | return false; |
| 463 | |
| 464 | return true; |
| 465 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 466 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 467 | RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 468 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 469 | FunctionTypeProto* FT = |
| 470 | dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr()); |
| 471 | |
| 472 | if (FT && !isCFRefType(FT->getResultType())) |
| 473 | return 0; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 474 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 475 | // FIXME: Add special-cases for functions that retain/release. For now |
| 476 | // just handle the default case. |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 477 | |
| 478 | assert (ScratchArgs.empty()); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 479 | return getPersistentSummary(RetEffect::MakeOwned()); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 482 | RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 483 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 484 | FunctionTypeProto* FT = |
| 485 | dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr()); |
Ted Kremenek | d4244d4 | 2008-04-11 20:11:19 +0000 | [diff] [blame] | 486 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 487 | if (FT) { |
| 488 | QualType RetTy = FT->getResultType(); |
Ted Kremenek | d4244d4 | 2008-04-11 20:11:19 +0000 | [diff] [blame] | 489 | |
Ted Kremenek | 562c130 | 2008-05-05 16:51:50 +0000 | [diff] [blame] | 490 | // FIXME: For now we assume that all pointer types returned are referenced |
| 491 | // counted. Since this is the "Get" rule, we assume non-ownership, which |
| 492 | // works fine for things that are not reference counted. We do this because |
| 493 | // some generic data structures return "void*". We need something better |
| 494 | // in the future. |
| 495 | |
| 496 | if (!isCFRefType(RetTy) && !RetTy->isPointerType()) |
| 497 | return 0; |
| 498 | } |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 499 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 500 | // FIXME: Add special-cases for functions that retain/release. For now |
| 501 | // just handle the default case. |
| 502 | |
Ted Kremenek | ae855d4 | 2008-04-24 17:22:33 +0000 | [diff] [blame] | 503 | assert (ScratchArgs.empty()); |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 504 | return getPersistentSummary(RetEffect::MakeNotOwned()); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 507 | //===----------------------------------------------------------------------===// |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 508 | // Summary creation for Selectors. |
| 509 | //===----------------------------------------------------------------------===// |
| 510 | |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 511 | RetainSummary* RetainSummaryManager::getInitMethodSummary(Selector S) { |
| 512 | assert(ScratchArgs.empty()); |
| 513 | |
| 514 | RetainSummary* Summ = |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 515 | getPersistentSummary(RetEffect::MakeReceiverAlias()); |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 516 | |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 517 | ObjCMethSummaries[S] = Summ; |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 518 | return Summ; |
| 519 | } |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 520 | |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 521 | RetainSummary* RetainSummaryManager::getMethodSummary(Selector S) { |
| 522 | |
| 523 | // Look up a summary in our cache of Selectors -> Summaries. |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 524 | ObjCMethSummariesTy::iterator I = ObjCMethSummaries.find(S); |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 525 | |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 526 | if (I != ObjCMethSummaries.end()) |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 527 | return I->second; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 528 | |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 529 | // "initXXX": pass-through for receiver. |
| 530 | |
| 531 | const char* s = S.getIdentifierInfoForSlot(0)->getName(); |
| 532 | |
| 533 | if (s[0] == 'i' && s[10] == 'n' && s[2] == 'i' && s[3] == 't') |
| 534 | return getInitMethodSummary(S); |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
Ted Kremenek | 83b2cde | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 539 | void RetainSummaryManager::InitializeInstMethSummaries() { |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 540 | |
| 541 | assert (ScratchArgs.empty()); |
| 542 | |
| 543 | RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet() : RetEffect::MakeOwned(); |
| 544 | RetainSummary* Summ = getPersistentSummary(E); |
| 545 | |
| 546 | // Create the "alloc" selector. |
| 547 | ObjCInstMethSummaries[ GetNullarySelector("alloc", Ctx) ] = Summ; |
| 548 | |
| 549 | // Create the "new" selector. |
| 550 | ObjCInstMethSummaries[ GetNullarySelector("new", Ctx) ] = Summ; |
| 551 | |
| 552 | // Create the "allocWithZone:" selector. |
| 553 | ObjCInstMethSummaries[ GetUnarySelector("allocWithZone", Ctx) ] = Summ; |
| 554 | |
| 555 | // Create the "copyWithZone:" selector. |
| 556 | ObjCInstMethSummaries[ GetUnarySelector("copyWithZone", Ctx) ] = Summ; |
| 557 | |
| 558 | // Create the "mutableCopyWithZone:" selector. |
| 559 | ObjCInstMethSummaries[ GetUnarySelector("mutableCopyWithZone", Ctx) ] = Summ; |
| 560 | } |
| 561 | |
Ted Kremenek | 83b2cde | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 562 | void RetainSummaryManager::InitializeMethSummaries() { |
| 563 | |
| 564 | assert (ScratchArgs.empty()); |
| 565 | |
| 566 | // Create the "init" selector. |
| 567 | RetainSummary* Summ = getPersistentSummary(RetEffect::MakeReceiverAlias()); |
| 568 | ObjCMethSummaries[ GetNullarySelector("init", Ctx) ] = Summ; |
| 569 | |
| 570 | // Create the "copy" selector. |
| 571 | RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet() : RetEffect::MakeOwned(); |
| 572 | Summ = getPersistentSummary(E); |
| 573 | ObjCMethSummaries[ GetNullarySelector("copy", Ctx) ] = Summ; |
| 574 | |
| 575 | // Create the "mutableCopy" selector. |
| 576 | ObjCMethSummaries[ GetNullarySelector("mutableCopy", Ctx) ] = Summ; |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 577 | |
| 578 | // Create the "retain" selector. |
| 579 | E = RetEffect::MakeReceiverAlias(); |
| 580 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef); |
| 581 | ObjCMethSummaries[ GetNullarySelector("retain", Ctx) ] = Summ; |
| 582 | |
| 583 | // Create the "release" selector. |
| 584 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef); |
| 585 | ObjCMethSummaries[ GetNullarySelector("release", Ctx) ] = Summ; |
| 586 | |
| 587 | // Create the "autorelease" selector. |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame^] | 588 | Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : StopTracking); |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 589 | ObjCMethSummaries[ GetNullarySelector("autorelease", Ctx) ] = Summ; |
Ted Kremenek | 83b2cde | 2008-05-06 00:38:54 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 592 | |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 593 | RetainSummary* RetainSummaryManager::getInstanceMethodSummary(Selector S) { |
| 594 | |
| 595 | // Look up a summary in our cache of Selectors -> Summaries. |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 596 | ObjCMethSummariesTy::iterator I = ObjCInstMethSummaries.find(S); |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 597 | |
Ted Kremenek | 0e344d4 | 2008-05-06 00:30:21 +0000 | [diff] [blame] | 598 | if (I != ObjCInstMethSummaries.end()) |
Ted Kremenek | 42ea032 | 2008-05-05 23:55:01 +0000 | [diff] [blame] | 599 | return I->second; |
| 600 | |
| 601 | return 0; |
| 602 | } |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 603 | |
| 604 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 605 | // Reference-counting logic (typestate + counts). |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 606 | //===----------------------------------------------------------------------===// |
| 607 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 608 | namespace { |
| 609 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 610 | class VISIBILITY_HIDDEN RefVal { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 611 | public: |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 612 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 613 | enum Kind { |
| 614 | Owned = 0, // Owning reference. |
| 615 | NotOwned, // Reference is not owned by still valid (not freed). |
| 616 | Released, // Object has been released. |
| 617 | ReturnedOwned, // Returned object passes ownership to caller. |
| 618 | ReturnedNotOwned, // Return object does not pass ownership to caller. |
| 619 | ErrorUseAfterRelease, // Object used after released. |
| 620 | ErrorReleaseNotOwned, // Release of an object that was not owned. |
| 621 | ErrorLeak // A memory leak due to excessive reference counts. |
| 622 | }; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 623 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 624 | private: |
| 625 | |
| 626 | Kind kind; |
| 627 | unsigned Cnt; |
| 628 | |
| 629 | RefVal(Kind k, unsigned cnt) : kind(k), Cnt(cnt) {} |
| 630 | |
| 631 | RefVal(Kind k) : kind(k), Cnt(0) {} |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 632 | |
| 633 | public: |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 634 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 635 | Kind getKind() const { return kind; } |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 636 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 637 | unsigned getCount() const { return Cnt; } |
| 638 | |
| 639 | // Useful predicates. |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 640 | |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 641 | static bool isError(Kind k) { return k >= ErrorUseAfterRelease; } |
| 642 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 643 | static bool isLeak(Kind k) { return k == ErrorLeak; } |
| 644 | |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 645 | bool isOwned() const { |
| 646 | return getKind() == Owned; |
| 647 | } |
| 648 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 649 | bool isNotOwned() const { |
| 650 | return getKind() == NotOwned; |
| 651 | } |
| 652 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 653 | bool isReturnedOwned() const { |
| 654 | return getKind() == ReturnedOwned; |
| 655 | } |
| 656 | |
| 657 | bool isReturnedNotOwned() const { |
| 658 | return getKind() == ReturnedNotOwned; |
| 659 | } |
| 660 | |
| 661 | bool isNonLeakError() const { |
| 662 | Kind k = getKind(); |
| 663 | return isError(k) && !isLeak(k); |
| 664 | } |
| 665 | |
| 666 | // State creation: normal state. |
| 667 | |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 668 | static RefVal makeOwned(unsigned Count = 0) { |
| 669 | return RefVal(Owned, Count); |
| 670 | } |
| 671 | |
| 672 | static RefVal makeNotOwned(unsigned Count = 0) { |
| 673 | return RefVal(NotOwned, Count); |
| 674 | } |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 675 | |
| 676 | static RefVal makeReturnedOwned(unsigned Count) { |
| 677 | return RefVal(ReturnedOwned, Count); |
| 678 | } |
| 679 | |
| 680 | static RefVal makeReturnedNotOwned() { |
| 681 | return RefVal(ReturnedNotOwned); |
| 682 | } |
| 683 | |
| 684 | // State creation: errors. |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 685 | |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 686 | static RefVal makeLeak(unsigned Count) { return RefVal(ErrorLeak, Count); } |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 687 | static RefVal makeReleased() { return RefVal(Released); } |
| 688 | static RefVal makeUseAfterRelease() { return RefVal(ErrorUseAfterRelease); } |
| 689 | static RefVal makeReleaseNotOwned() { return RefVal(ErrorReleaseNotOwned); } |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 690 | |
| 691 | // Comparison, profiling, and pretty-printing. |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 692 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 693 | bool operator==(const RefVal& X) const { |
| 694 | return kind == X.kind && Cnt == X.Cnt; |
| 695 | } |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 696 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 697 | void Profile(llvm::FoldingSetNodeID& ID) const { |
| 698 | ID.AddInteger((unsigned) kind); |
| 699 | ID.AddInteger(Cnt); |
| 700 | } |
| 701 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 702 | void print(std::ostream& Out) const; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 703 | }; |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 704 | |
| 705 | void RefVal::print(std::ostream& Out) const { |
| 706 | switch (getKind()) { |
| 707 | default: assert(false); |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 708 | case Owned: { |
| 709 | Out << "Owned"; |
| 710 | unsigned cnt = getCount(); |
| 711 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 712 | break; |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 713 | } |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 714 | |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 715 | case NotOwned: { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 716 | Out << "NotOwned"; |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 717 | unsigned cnt = getCount(); |
| 718 | if (cnt) Out << " (+ " << cnt << ")"; |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 719 | break; |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 720 | } |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 721 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 722 | case ReturnedOwned: { |
| 723 | Out << "ReturnedOwned"; |
| 724 | unsigned cnt = getCount(); |
| 725 | if (cnt) Out << " (+ " << cnt << ")"; |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | case ReturnedNotOwned: { |
| 730 | Out << "ReturnedNotOwned"; |
| 731 | unsigned cnt = getCount(); |
| 732 | if (cnt) Out << " (+ " << cnt << ")"; |
| 733 | break; |
| 734 | } |
| 735 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 736 | case Released: |
| 737 | Out << "Released"; |
| 738 | break; |
| 739 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 740 | case ErrorLeak: |
| 741 | Out << "Leaked"; |
| 742 | break; |
| 743 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 744 | case ErrorUseAfterRelease: |
| 745 | Out << "Use-After-Release [ERROR]"; |
| 746 | break; |
| 747 | |
| 748 | case ErrorReleaseNotOwned: |
| 749 | Out << "Release of Not-Owned [ERROR]"; |
| 750 | break; |
| 751 | } |
| 752 | } |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 753 | |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 754 | static inline unsigned GetCount(RefVal V) { |
| 755 | switch (V.getKind()) { |
| 756 | default: |
| 757 | return V.getCount(); |
| 758 | |
| 759 | case RefVal::Owned: |
| 760 | return V.getCount()+1; |
| 761 | } |
| 762 | } |
| 763 | |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 764 | //===----------------------------------------------------------------------===// |
| 765 | // Transfer functions. |
| 766 | //===----------------------------------------------------------------------===// |
| 767 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 768 | class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals { |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 769 | public: |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 770 | // Type definitions. |
| 771 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 772 | typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings; |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 773 | typedef RefBindings::Factory RefBFactoryTy; |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 774 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 775 | typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolID> > |
| 776 | ReleasesNotOwnedTy; |
| 777 | |
| 778 | typedef ReleasesNotOwnedTy UseAfterReleasesTy; |
| 779 | |
| 780 | typedef llvm::DenseMap<GRExprEngine::NodeTy*, std::vector<SymbolID>*> |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 781 | LeaksTy; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 782 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 783 | class BindingsPrinter : public ValueState::CheckerStatePrinter { |
| 784 | public: |
| 785 | virtual void PrintCheckerState(std::ostream& Out, void* State, |
| 786 | const char* nl, const char* sep); |
| 787 | }; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 788 | |
| 789 | private: |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 790 | // Instance variables. |
| 791 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 792 | RetainSummaryManager Summaries; |
| 793 | const bool EmitStandardWarnings; |
| 794 | const LangOptions& LOpts; |
| 795 | RefBFactoryTy RefBFactory; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 796 | |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 797 | UseAfterReleasesTy UseAfterReleases; |
| 798 | ReleasesNotOwnedTy ReleasesNotOwned; |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 799 | LeaksTy Leaks; |
Ted Kremenek | 1daa16c | 2008-03-11 18:14:09 +0000 | [diff] [blame] | 800 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 801 | BindingsPrinter Printer; |
| 802 | |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 803 | Selector RetainSelector; |
| 804 | Selector ReleaseSelector; |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 805 | Selector AutoreleaseSelector; |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 806 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 807 | public: |
| 808 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 809 | static RefBindings GetRefBindings(ValueState& StImpl) { |
| 810 | return RefBindings((RefBindings::TreeTy*) StImpl.CheckerState); |
| 811 | } |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 812 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 813 | private: |
| 814 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 815 | static void SetRefBindings(ValueState& StImpl, RefBindings B) { |
| 816 | StImpl.CheckerState = B.getRoot(); |
| 817 | } |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 818 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 819 | RefBindings Remove(RefBindings B, SymbolID sym) { |
| 820 | return RefBFactory.Remove(B, sym); |
| 821 | } |
| 822 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 823 | RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E, |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 824 | RefVal::Kind& hasErr); |
| 825 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 826 | void ProcessNonLeakError(ExplodedNodeSet<ValueState>& Dst, |
| 827 | GRStmtNodeBuilder<ValueState>& Builder, |
| 828 | Expr* NodeExpr, Expr* ErrorExpr, |
| 829 | ExplodedNode<ValueState>* Pred, |
| 830 | ValueState* St, |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 831 | RefVal::Kind hasErr, SymbolID Sym); |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 832 | |
| 833 | ValueState* HandleSymbolDeath(ValueStateManager& VMgr, ValueState* St, |
| 834 | SymbolID sid, RefVal V, bool& hasLeak); |
| 835 | |
| 836 | ValueState* NukeBinding(ValueStateManager& VMgr, ValueState* St, |
| 837 | SymbolID sid); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 838 | |
| 839 | public: |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 840 | |
Ted Kremenek | 2f62f35 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 841 | CFRefCount(ASTContext& Ctx, bool gcenabled, bool StandardWarnings, |
| 842 | const LangOptions& lopts) |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 843 | : Summaries(Ctx, gcenabled), |
Ted Kremenek | 2f62f35 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 844 | EmitStandardWarnings(StandardWarnings), |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 845 | LOpts(lopts), |
Ted Kremenek | 1bd6ddb | 2008-05-01 18:31:44 +0000 | [diff] [blame] | 846 | RetainSelector(GetNullarySelector("retain", Ctx)), |
| 847 | ReleaseSelector(GetNullarySelector("release", Ctx)), |
| 848 | AutoreleaseSelector(GetNullarySelector("autorelease", Ctx)) {} |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 849 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 850 | virtual ~CFRefCount() { |
| 851 | for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I) |
| 852 | delete I->second; |
| 853 | } |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 854 | |
| 855 | virtual void RegisterChecks(GRExprEngine& Eng); |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 856 | |
| 857 | virtual ValueState::CheckerStatePrinter* getCheckerStatePrinter() { |
| 858 | return &Printer; |
| 859 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 860 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 861 | bool isGCEnabled() const { return Summaries.isGCEnabled(); } |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 862 | const LangOptions& getLangOptions() const { return LOpts; } |
| 863 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 864 | // Calls. |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 865 | |
| 866 | void EvalSummary(ExplodedNodeSet<ValueState>& Dst, |
| 867 | GRExprEngine& Eng, |
| 868 | GRStmtNodeBuilder<ValueState>& Builder, |
| 869 | Expr* Ex, |
| 870 | Expr* Receiver, |
| 871 | RetainSummary* Summ, |
| 872 | Expr** arg_beg, Expr** arg_end, |
| 873 | ExplodedNode<ValueState>* Pred); |
| 874 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 875 | virtual void EvalCall(ExplodedNodeSet<ValueState>& Dst, |
Ted Kremenek | ce0767f | 2008-03-12 21:06:49 +0000 | [diff] [blame] | 876 | GRExprEngine& Eng, |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 877 | GRStmtNodeBuilder<ValueState>& Builder, |
Ted Kremenek | 0a6a80b | 2008-04-23 20:12:28 +0000 | [diff] [blame] | 878 | CallExpr* CE, RVal L, |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 879 | ExplodedNode<ValueState>* Pred); |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 880 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 881 | |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 882 | virtual void EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst, |
| 883 | GRExprEngine& Engine, |
| 884 | GRStmtNodeBuilder<ValueState>& Builder, |
| 885 | ObjCMessageExpr* ME, |
| 886 | ExplodedNode<ValueState>* Pred); |
| 887 | |
| 888 | bool EvalObjCMessageExprAux(ExplodedNodeSet<ValueState>& Dst, |
| 889 | GRExprEngine& Engine, |
| 890 | GRStmtNodeBuilder<ValueState>& Builder, |
| 891 | ObjCMessageExpr* ME, |
| 892 | ExplodedNode<ValueState>* Pred); |
| 893 | |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 894 | // Stores. |
| 895 | |
| 896 | virtual void EvalStore(ExplodedNodeSet<ValueState>& Dst, |
| 897 | GRExprEngine& Engine, |
| 898 | GRStmtNodeBuilder<ValueState>& Builder, |
| 899 | Expr* E, ExplodedNode<ValueState>* Pred, |
| 900 | ValueState* St, RVal TargetLV, RVal Val); |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 901 | // End-of-path. |
| 902 | |
| 903 | virtual void EvalEndPath(GRExprEngine& Engine, |
| 904 | GREndPathNodeBuilder<ValueState>& Builder); |
| 905 | |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 906 | virtual void EvalDeadSymbols(ExplodedNodeSet<ValueState>& Dst, |
| 907 | GRExprEngine& Engine, |
| 908 | GRStmtNodeBuilder<ValueState>& Builder, |
Ted Kremenek | ac91ce9 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 909 | ExplodedNode<ValueState>* Pred, |
| 910 | Stmt* S, |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 911 | ValueState* St, |
| 912 | const ValueStateManager::DeadSymbolsTy& Dead); |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 913 | // Return statements. |
| 914 | |
| 915 | virtual void EvalReturn(ExplodedNodeSet<ValueState>& Dst, |
| 916 | GRExprEngine& Engine, |
| 917 | GRStmtNodeBuilder<ValueState>& Builder, |
| 918 | ReturnStmt* S, |
| 919 | ExplodedNode<ValueState>* Pred); |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 920 | |
| 921 | // Assumptions. |
| 922 | |
| 923 | virtual ValueState* EvalAssume(GRExprEngine& Engine, ValueState* St, |
| 924 | RVal Cond, bool Assumption, bool& isFeasible); |
| 925 | |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 926 | // Error iterators. |
| 927 | |
| 928 | typedef UseAfterReleasesTy::iterator use_after_iterator; |
| 929 | typedef ReleasesNotOwnedTy::iterator bad_release_iterator; |
Ted Kremenek | 7f3f41a | 2008-04-17 23:43:50 +0000 | [diff] [blame] | 930 | typedef LeaksTy::iterator leaks_iterator; |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 931 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 932 | use_after_iterator use_after_begin() { return UseAfterReleases.begin(); } |
| 933 | use_after_iterator use_after_end() { return UseAfterReleases.end(); } |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 934 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 935 | bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); } |
| 936 | bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); } |
Ted Kremenek | 7f3f41a | 2008-04-17 23:43:50 +0000 | [diff] [blame] | 937 | |
| 938 | leaks_iterator leaks_begin() { return Leaks.begin(); } |
| 939 | leaks_iterator leaks_end() { return Leaks.end(); } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 940 | }; |
| 941 | |
| 942 | } // end anonymous namespace |
| 943 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 944 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 945 | |
| 946 | |
Ted Kremenek | 3b11f7a | 2008-03-11 19:44:10 +0000 | [diff] [blame] | 947 | void CFRefCount::BindingsPrinter::PrintCheckerState(std::ostream& Out, |
| 948 | void* State, const char* nl, |
| 949 | const char* sep) { |
| 950 | RefBindings B((RefBindings::TreeTy*) State); |
| 951 | |
| 952 | if (State) |
| 953 | Out << sep << nl; |
| 954 | |
| 955 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 956 | Out << (*I).first << " : "; |
| 957 | (*I).second.print(Out); |
| 958 | Out << nl; |
| 959 | } |
| 960 | } |
| 961 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 962 | static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) { |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 963 | return Summ ? Summ->getArg(idx) : DoNothing; |
| 964 | } |
| 965 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 966 | static inline RetEffect GetRetEffect(RetainSummary* Summ) { |
| 967 | return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet(); |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame^] | 970 | static inline ArgEffect GetReceiverE(RetainSummary* Summ) { |
| 971 | return Summ ? Summ->getReceiverEffect() : DoNothing; |
| 972 | } |
| 973 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 974 | void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<ValueState>& Dst, |
| 975 | GRStmtNodeBuilder<ValueState>& Builder, |
| 976 | Expr* NodeExpr, Expr* ErrorExpr, |
| 977 | ExplodedNode<ValueState>* Pred, |
| 978 | ValueState* St, |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 979 | RefVal::Kind hasErr, SymbolID Sym) { |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 980 | Builder.BuildSinks = true; |
| 981 | GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St); |
| 982 | |
| 983 | if (!N) return; |
| 984 | |
| 985 | switch (hasErr) { |
| 986 | default: assert(false); |
| 987 | case RefVal::ErrorUseAfterRelease: |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 988 | UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym); |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 989 | break; |
| 990 | |
| 991 | case RefVal::ErrorReleaseNotOwned: |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 992 | ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym); |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 993 | break; |
| 994 | } |
| 995 | } |
| 996 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 997 | void CFRefCount::EvalSummary(ExplodedNodeSet<ValueState>& Dst, |
| 998 | GRExprEngine& Eng, |
| 999 | GRStmtNodeBuilder<ValueState>& Builder, |
| 1000 | Expr* Ex, |
| 1001 | Expr* Receiver, |
| 1002 | RetainSummary* Summ, |
| 1003 | Expr** arg_beg, Expr** arg_end, |
| 1004 | ExplodedNode<ValueState>* Pred) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1005 | |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 1006 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1007 | // Get the state. |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1008 | ValueStateManager& StateMgr = Eng.getStateManager(); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1009 | ValueState* St = Builder.GetState(Pred); |
| 1010 | |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame^] | 1011 | |
| 1012 | // Evaluate the effect of the arguments. |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1013 | |
| 1014 | ValueState StVals = *St; |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1015 | RefVal::Kind hasErr = (RefVal::Kind) 0; |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1016 | unsigned idx = 0; |
Ted Kremenek | 99b0ecb | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1017 | Expr* ErrorExpr = NULL; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1018 | SymbolID ErrorSym = 0; |
Ted Kremenek | 99b0ecb | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1019 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1020 | for (Expr **I = arg_beg, **E = arg_end; I != E; ++I, ++idx) { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1021 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1022 | RVal V = StateMgr.GetRVal(St, *I); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1023 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1024 | if (isa<lval::SymbolVal>(V)) { |
| 1025 | SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol(); |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1026 | RefBindings B = GetRefBindings(StVals); |
| 1027 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1028 | if (RefBindings::TreeTy* T = B.SlimFind(Sym)) { |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1029 | B = Update(B, Sym, T->getValue().second, GetArgE(Summ, idx), hasErr); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1030 | SetRefBindings(StVals, B); |
Ted Kremenek | 99b0ecb | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1031 | |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1032 | if (hasErr) { |
Ted Kremenek | 99b0ecb | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1033 | ErrorExpr = *I; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1034 | ErrorSym = T->getValue().first; |
Ted Kremenek | 99b0ecb | 2008-04-11 18:40:51 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1037 | } |
Ted Kremenek | e492420 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 1038 | } |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1039 | else if (isa<LVal>(V)) { |
| 1040 | // Nuke all arguments passed by reference. |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1041 | StateMgr.Unbind(StVals, cast<LVal>(V)); |
Ted Kremenek | e492420 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 1042 | } |
Ted Kremenek | be62129 | 2008-04-22 21:39:21 +0000 | [diff] [blame] | 1043 | else if (isa<nonlval::LValAsInteger>(V)) |
| 1044 | StateMgr.Unbind(StVals, cast<nonlval::LValAsInteger>(V).getLVal()); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1045 | } |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1046 | |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame^] | 1047 | // Evaluate the effect on the message receiver. |
| 1048 | |
| 1049 | if (!ErrorExpr && Receiver) { |
| 1050 | RVal V = StateMgr.GetRVal(St, Receiver); |
| 1051 | |
| 1052 | if (isa<lval::SymbolVal>(V)) { |
| 1053 | SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol(); |
| 1054 | RefBindings B = GetRefBindings(StVals); |
| 1055 | |
| 1056 | if (RefBindings::TreeTy* T = B.SlimFind(Sym)) { |
| 1057 | B = Update(B, Sym, T->getValue().second, GetReceiverE(Summ), hasErr); |
| 1058 | SetRefBindings(StVals, B); |
| 1059 | |
| 1060 | if (hasErr) { |
| 1061 | ErrorExpr = Receiver; |
| 1062 | ErrorSym = T->getValue().first; |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | // Get the persistent state. |
| 1069 | |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1070 | St = StateMgr.getPersistentState(StVals); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1071 | |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame^] | 1072 | // Process any errors. |
| 1073 | |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1074 | if (hasErr) { |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1075 | ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, St, |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1076 | hasErr, ErrorSym); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1077 | return; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1078 | } |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1079 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1080 | // Finally, consult the summary for the return value. |
| 1081 | |
Ted Kremenek | 266d8b6 | 2008-05-06 02:26:56 +0000 | [diff] [blame] | 1082 | RetEffect RE = GetRetEffect(Summ); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1083 | |
| 1084 | switch (RE.getKind()) { |
| 1085 | default: |
| 1086 | assert (false && "Unhandled RetEffect."); break; |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1087 | |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1088 | case RetEffect::NoRet: |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1089 | |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1090 | // Make up a symbol for the return value (not reference counted). |
Ted Kremenek | e492420 | 2008-04-11 20:51:02 +0000 | [diff] [blame] | 1091 | // FIXME: This is basically copy-and-paste from GRSimpleVals. We |
| 1092 | // should compose behavior, not copy it. |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1093 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1094 | if (Ex->getType() != Eng.getContext().VoidTy) { |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1095 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1096 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count); |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1097 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1098 | RVal X = Ex->getType()->isPointerType() |
| 1099 | ? cast<RVal>(lval::SymbolVal(Sym)) |
| 1100 | : cast<RVal>(nonlval::SymbolVal(Sym)); |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1101 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1102 | St = StateMgr.SetRVal(St, Ex, X, Eng.getCFG().isBlkExpr(Ex), false); |
Ted Kremenek | 455dd86 | 2008-04-11 20:23:24 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1105 | break; |
| 1106 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1107 | case RetEffect::Alias: { |
| 1108 | unsigned idx = RE.getValue(); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1109 | assert ((arg_end - arg_beg) >= 0); |
| 1110 | assert (idx < (unsigned) (arg_end - arg_beg)); |
| 1111 | RVal V = StateMgr.GetRVal(St, arg_beg[idx]); |
| 1112 | St = StateMgr.SetRVal(St, Ex, V, Eng.getCFG().isBlkExpr(Ex), false); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1113 | break; |
| 1114 | } |
| 1115 | |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame^] | 1116 | case RetEffect::ReceiverAlias: { |
| 1117 | assert (Receiver); |
| 1118 | RVal V = StateMgr.GetRVal(St, Receiver); |
| 1119 | St = StateMgr.SetRVal(St, Ex, V, Eng.getCFG().isBlkExpr(Ex), false); |
| 1120 | break; |
| 1121 | } |
| 1122 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1123 | case RetEffect::OwnedSymbol: { |
| 1124 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1125 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count); |
| 1126 | |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1127 | ValueState StImpl = *St; |
| 1128 | RefBindings B = GetRefBindings(StImpl); |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1129 | SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned())); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1130 | |
| 1131 | St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl), |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1132 | Ex, lval::SymbolVal(Sym), |
| 1133 | Eng.getCFG().isBlkExpr(Ex), false); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1134 | |
| 1135 | break; |
| 1136 | } |
| 1137 | |
| 1138 | case RetEffect::NotOwnedSymbol: { |
| 1139 | unsigned Count = Builder.getCurrentBlockCount(); |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1140 | SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1141 | |
| 1142 | ValueState StImpl = *St; |
| 1143 | RefBindings B = GetRefBindings(StImpl); |
| 1144 | SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeNotOwned())); |
| 1145 | |
| 1146 | St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl), |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1147 | Ex, lval::SymbolVal(Sym), |
| 1148 | Eng.getCFG().isBlkExpr(Ex), false); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1149 | |
| 1150 | break; |
| 1151 | } |
| 1152 | } |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1153 | |
| 1154 | Builder.MakeNode(Dst, Ex, Pred, St); |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | void CFRefCount::EvalCall(ExplodedNodeSet<ValueState>& Dst, |
| 1159 | GRExprEngine& Eng, |
| 1160 | GRStmtNodeBuilder<ValueState>& Builder, |
| 1161 | CallExpr* CE, RVal L, |
| 1162 | ExplodedNode<ValueState>* Pred) { |
| 1163 | |
| 1164 | |
| 1165 | RetainSummary* Summ = NULL; |
| 1166 | |
| 1167 | // Get the summary. |
| 1168 | |
| 1169 | if (isa<lval::FuncVal>(L)) { |
| 1170 | lval::FuncVal FV = cast<lval::FuncVal>(L); |
| 1171 | FunctionDecl* FD = FV.getDecl(); |
| 1172 | Summ = Summaries.getSummary(FD, Eng.getContext()); |
| 1173 | } |
| 1174 | |
| 1175 | EvalSummary(Dst, Eng, Builder, CE, 0, Summ, |
| 1176 | CE->arg_begin(), CE->arg_end(), Pred); |
Ted Kremenek | 827f93b | 2008-03-06 00:08:09 +0000 | [diff] [blame] | 1177 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1178 | |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1179 | |
| 1180 | void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst, |
| 1181 | GRExprEngine& Eng, |
| 1182 | GRStmtNodeBuilder<ValueState>& Builder, |
| 1183 | ObjCMessageExpr* ME, |
| 1184 | ExplodedNode<ValueState>* Pred) { |
| 1185 | |
Ted Kremenek | 3366180 | 2008-05-01 21:31:50 +0000 | [diff] [blame] | 1186 | if (!EvalObjCMessageExprAux(Dst, Eng, Builder, ME, Pred)) |
| 1187 | return; |
| 1188 | |
| 1189 | // The basic transfer function logic for message expressions does nothing. |
| 1190 | // We just invalidate all arguments passed in by references. |
| 1191 | |
| 1192 | ValueStateManager& StateMgr = Eng.getStateManager(); |
| 1193 | ValueState* St = Builder.GetState(Pred); |
| 1194 | RefBindings B = GetRefBindings(*St); |
| 1195 | |
| 1196 | for (ObjCMessageExpr::arg_iterator I = ME->arg_begin(), E = ME->arg_end(); |
| 1197 | I != E; ++I) { |
| 1198 | |
| 1199 | RVal V = StateMgr.GetRVal(St, *I); |
| 1200 | |
| 1201 | if (isa<LVal>(V)) { |
| 1202 | |
| 1203 | LVal lv = cast<LVal>(V); |
| 1204 | |
| 1205 | // Did the lval bind to a symbol? |
| 1206 | RVal X = StateMgr.GetRVal(St, lv); |
| 1207 | |
| 1208 | if (isa<lval::SymbolVal>(X)) { |
Ted Kremenek | 6a24ec0 | 2008-05-01 23:38:35 +0000 | [diff] [blame] | 1209 | SymbolID Sym = cast<lval::SymbolVal>(X).getSymbol(); |
Ted Kremenek | 3366180 | 2008-05-01 21:31:50 +0000 | [diff] [blame] | 1210 | B = Remove(B, Sym); |
| 1211 | |
| 1212 | // Create a new state with the updated bindings. |
| 1213 | ValueState StVals = *St; |
| 1214 | SetRefBindings(StVals, B); |
| 1215 | St = StateMgr.getPersistentState(StVals); |
| 1216 | } |
| 1217 | |
| 1218 | St = StateMgr.SetRVal(St, cast<LVal>(V), UnknownVal()); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | Builder.MakeNode(Dst, ME, Pred, St); |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | bool CFRefCount::EvalObjCMessageExprAux(ExplodedNodeSet<ValueState>& Dst, |
| 1226 | GRExprEngine& Eng, |
| 1227 | GRStmtNodeBuilder<ValueState>& Builder, |
| 1228 | ObjCMessageExpr* ME, |
| 1229 | ExplodedNode<ValueState>* Pred) { |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1230 | |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1231 | if (isGCEnabled()) |
Ted Kremenek | 9b0c09c | 2008-04-29 05:33:51 +0000 | [diff] [blame] | 1232 | return true; |
| 1233 | |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1234 | // Handle "toll-free bridging" of calls to "Release" and "Retain". |
| 1235 | |
| 1236 | // FIXME: track the underlying object type associated so that we can |
| 1237 | // flag illegal uses of toll-free bridging (or at least handle it |
| 1238 | // at casts). |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1239 | |
| 1240 | Selector S = ME->getSelector(); |
| 1241 | |
| 1242 | if (!S.isUnarySelector()) |
| 1243 | return true; |
| 1244 | |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1245 | Expr* Receiver = ME->getReceiver(); |
| 1246 | |
| 1247 | if (!Receiver) |
| 1248 | return true; |
| 1249 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1250 | // Check if we are calling "autorelease". |
| 1251 | |
| 1252 | enum { IsRelease, IsRetain, IsAutorelease, IsNone } mode = IsNone; |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1253 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1254 | if (S == AutoreleaseSelector) |
| 1255 | mode = IsAutorelease; |
| 1256 | else if (S == RetainSelector) |
| 1257 | mode = IsRetain; |
| 1258 | else if (S == ReleaseSelector) |
| 1259 | mode = IsRelease; |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1260 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1261 | if (mode == IsNone) |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1262 | return true; |
| 1263 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1264 | // We have "retain", "release", or "autorelease". |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1265 | ValueStateManager& StateMgr = Eng.getStateManager(); |
| 1266 | ValueState* St = Builder.GetState(Pred); |
| 1267 | RVal V = StateMgr.GetRVal(St, Receiver); |
| 1268 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1269 | // Was the argument something we are not tracking? |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1270 | if (!isa<lval::SymbolVal>(V)) |
| 1271 | return true; |
| 1272 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1273 | // Get the bindings. |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1274 | SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol(); |
| 1275 | RefBindings B = GetRefBindings(*St); |
| 1276 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1277 | // Find the tracked value. |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1278 | RefBindings::TreeTy* T = B.SlimFind(Sym); |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1279 | |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1280 | if (!T) |
| 1281 | return true; |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1282 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1283 | RefVal::Kind hasErr = (RefVal::Kind) 0; |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1284 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1285 | // Update the bindings. |
| 1286 | switch (mode) { |
| 1287 | case IsNone: |
| 1288 | assert(false); |
| 1289 | |
| 1290 | case IsRelease: |
| 1291 | B = Update(B, Sym, T->getValue().second, DecRef, hasErr); |
| 1292 | break; |
| 1293 | |
| 1294 | case IsRetain: |
| 1295 | B = Update(B, Sym, T->getValue().second, IncRef, hasErr); |
| 1296 | break; |
| 1297 | |
| 1298 | case IsAutorelease: |
| 1299 | // For now we just stop tracking a value if we see |
| 1300 | // it sent "autorelease." In the future we can potentially |
| 1301 | // track the associated pool. |
| 1302 | B = Remove(B, Sym); |
| 1303 | break; |
| 1304 | } |
| 1305 | |
| 1306 | // Create a new state with the updated bindings. |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1307 | ValueState StVals = *St; |
| 1308 | SetRefBindings(StVals, B); |
Ted Kremenek | cb470940 | 2008-05-01 04:02:04 +0000 | [diff] [blame] | 1309 | St = Eng.SetRVal(StateMgr.getPersistentState(StVals), ME, V); |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1310 | |
Ted Kremenek | 3281a1f | 2008-05-01 02:18:37 +0000 | [diff] [blame] | 1311 | // Create an error node if it exists. |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1312 | if (hasErr) |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1313 | ProcessNonLeakError(Dst, Builder, ME, Receiver, Pred, St, hasErr, Sym); |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1314 | else |
| 1315 | Builder.MakeNode(Dst, ME, Pred, St); |
| 1316 | |
| 1317 | return false; |
Ted Kremenek | 4b4738b | 2008-04-15 23:44:31 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1320 | // Stores. |
| 1321 | |
| 1322 | void CFRefCount::EvalStore(ExplodedNodeSet<ValueState>& Dst, |
| 1323 | GRExprEngine& Eng, |
| 1324 | GRStmtNodeBuilder<ValueState>& Builder, |
| 1325 | Expr* E, ExplodedNode<ValueState>* Pred, |
| 1326 | ValueState* St, RVal TargetLV, RVal Val) { |
| 1327 | |
| 1328 | // Check if we have a binding for "Val" and if we are storing it to something |
| 1329 | // we don't understand or otherwise the value "escapes" the function. |
| 1330 | |
| 1331 | if (!isa<lval::SymbolVal>(Val)) |
| 1332 | return; |
| 1333 | |
| 1334 | // Are we storing to something that causes the value to "escape"? |
| 1335 | |
| 1336 | bool escapes = false; |
| 1337 | |
| 1338 | if (!isa<lval::DeclVal>(TargetLV)) |
| 1339 | escapes = true; |
| 1340 | else |
| 1341 | escapes = cast<lval::DeclVal>(TargetLV).getDecl()->hasGlobalStorage(); |
| 1342 | |
| 1343 | if (!escapes) |
| 1344 | return; |
| 1345 | |
| 1346 | SymbolID Sym = cast<lval::SymbolVal>(Val).getSymbol(); |
| 1347 | RefBindings B = GetRefBindings(*St); |
| 1348 | RefBindings::TreeTy* T = B.SlimFind(Sym); |
| 1349 | |
| 1350 | if (!T) |
| 1351 | return; |
| 1352 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1353 | // Nuke the binding. |
| 1354 | St = NukeBinding(Eng.getStateManager(), St, Sym); |
Ted Kremenek | 7aef484 | 2008-04-16 20:40:59 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Hand of the remaining logic to the parent implementation. |
| 1357 | GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, St, TargetLV, Val); |
| 1358 | } |
| 1359 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1360 | |
| 1361 | ValueState* CFRefCount::NukeBinding(ValueStateManager& VMgr, ValueState* St, |
| 1362 | SymbolID sid) { |
| 1363 | ValueState StImpl = *St; |
| 1364 | RefBindings B = GetRefBindings(StImpl); |
| 1365 | StImpl.CheckerState = RefBFactory.Remove(B, sid).getRoot(); |
| 1366 | return VMgr.getPersistentState(StImpl); |
| 1367 | } |
| 1368 | |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1369 | // End-of-path. |
| 1370 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1371 | ValueState* CFRefCount::HandleSymbolDeath(ValueStateManager& VMgr, |
| 1372 | ValueState* St, SymbolID sid, |
| 1373 | RefVal V, bool& hasLeak) { |
| 1374 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1375 | hasLeak = V.isOwned() || |
| 1376 | ((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0); |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1377 | |
| 1378 | if (!hasLeak) |
| 1379 | return NukeBinding(VMgr, St, sid); |
| 1380 | |
| 1381 | RefBindings B = GetRefBindings(*St); |
| 1382 | ValueState StImpl = *St; |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1383 | |
| 1384 | StImpl.CheckerState = |
| 1385 | RefBFactory.Add(B, sid, RefVal::makeLeak(GetCount(V))).getRoot(); |
| 1386 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1387 | return VMgr.getPersistentState(StImpl); |
| 1388 | } |
| 1389 | |
| 1390 | void CFRefCount::EvalEndPath(GRExprEngine& Eng, |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1391 | GREndPathNodeBuilder<ValueState>& Builder) { |
| 1392 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1393 | ValueState* St = Builder.getState(); |
| 1394 | RefBindings B = GetRefBindings(*St); |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1395 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1396 | llvm::SmallVector<SymbolID, 10> Leaked; |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1397 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1398 | for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) { |
| 1399 | bool hasLeak = false; |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1400 | |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1401 | St = HandleSymbolDeath(Eng.getStateManager(), St, |
| 1402 | (*I).first, (*I).second, hasLeak); |
| 1403 | |
| 1404 | if (hasLeak) Leaked.push_back((*I).first); |
| 1405 | } |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1406 | |
| 1407 | if (Leaked.empty()) |
| 1408 | return; |
| 1409 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1410 | ExplodedNode<ValueState>* N = Builder.MakeNode(St); |
Ted Kremenek | cfc909d | 2008-04-18 16:30:14 +0000 | [diff] [blame] | 1411 | |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1412 | if (!N) |
Ted Kremenek | cfc909d | 2008-04-18 16:30:14 +0000 | [diff] [blame] | 1413 | return; |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1414 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1415 | std::vector<SymbolID>*& LeaksAtNode = Leaks[N]; |
| 1416 | assert (!LeaksAtNode); |
| 1417 | LeaksAtNode = new std::vector<SymbolID>(); |
Ted Kremenek | 3f3c9c8 | 2008-04-16 22:32:20 +0000 | [diff] [blame] | 1418 | |
| 1419 | for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(), |
| 1420 | E = Leaked.end(); I != E; ++I) |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1421 | (*LeaksAtNode).push_back(*I); |
Ted Kremenek | ffefc35 | 2008-04-11 22:25:11 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1424 | // Dead symbols. |
| 1425 | |
| 1426 | void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<ValueState>& Dst, |
| 1427 | GRExprEngine& Eng, |
| 1428 | GRStmtNodeBuilder<ValueState>& Builder, |
Ted Kremenek | ac91ce9 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 1429 | ExplodedNode<ValueState>* Pred, |
| 1430 | Stmt* S, |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1431 | ValueState* St, |
| 1432 | const ValueStateManager::DeadSymbolsTy& Dead) { |
Ted Kremenek | ac91ce9 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 1433 | |
Ted Kremenek | 541db37 | 2008-04-24 23:57:27 +0000 | [diff] [blame] | 1434 | // FIXME: a lot of copy-and-paste from EvalEndPath. Refactor. |
| 1435 | |
| 1436 | RefBindings B = GetRefBindings(*St); |
| 1437 | llvm::SmallVector<SymbolID, 10> Leaked; |
| 1438 | |
| 1439 | for (ValueStateManager::DeadSymbolsTy::const_iterator |
| 1440 | I=Dead.begin(), E=Dead.end(); I!=E; ++I) { |
| 1441 | |
| 1442 | RefBindings::TreeTy* T = B.SlimFind(*I); |
| 1443 | |
| 1444 | if (!T) |
| 1445 | continue; |
| 1446 | |
| 1447 | bool hasLeak = false; |
| 1448 | |
| 1449 | St = HandleSymbolDeath(Eng.getStateManager(), St, |
| 1450 | *I, T->getValue().second, hasLeak); |
| 1451 | |
| 1452 | if (hasLeak) Leaked.push_back(*I); |
| 1453 | } |
| 1454 | |
| 1455 | if (Leaked.empty()) |
| 1456 | return; |
| 1457 | |
| 1458 | ExplodedNode<ValueState>* N = Builder.MakeNode(Dst, S, Pred, St); |
| 1459 | |
| 1460 | if (!N) |
| 1461 | return; |
| 1462 | |
| 1463 | std::vector<SymbolID>*& LeaksAtNode = Leaks[N]; |
| 1464 | assert (!LeaksAtNode); |
| 1465 | LeaksAtNode = new std::vector<SymbolID>(); |
| 1466 | |
| 1467 | for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(), |
| 1468 | E = Leaked.end(); I != E; ++I) |
| 1469 | (*LeaksAtNode).push_back(*I); |
| 1470 | } |
| 1471 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1472 | // Return statements. |
| 1473 | |
| 1474 | void CFRefCount::EvalReturn(ExplodedNodeSet<ValueState>& Dst, |
| 1475 | GRExprEngine& Eng, |
| 1476 | GRStmtNodeBuilder<ValueState>& Builder, |
| 1477 | ReturnStmt* S, |
| 1478 | ExplodedNode<ValueState>* Pred) { |
| 1479 | |
| 1480 | Expr* RetE = S->getRetValue(); |
| 1481 | if (!RetE) return; |
| 1482 | |
| 1483 | ValueStateManager& StateMgr = Eng.getStateManager(); |
| 1484 | ValueState* St = Builder.GetState(Pred); |
| 1485 | RVal V = StateMgr.GetRVal(St, RetE); |
| 1486 | |
| 1487 | if (!isa<lval::SymbolVal>(V)) |
| 1488 | return; |
| 1489 | |
| 1490 | // Get the reference count binding (if any). |
| 1491 | SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol(); |
| 1492 | RefBindings B = GetRefBindings(*St); |
| 1493 | RefBindings::TreeTy* T = B.SlimFind(Sym); |
| 1494 | |
| 1495 | if (!T) |
| 1496 | return; |
| 1497 | |
| 1498 | // Change the reference count. |
| 1499 | |
| 1500 | RefVal X = T->getValue().second; |
| 1501 | |
| 1502 | switch (X.getKind()) { |
| 1503 | |
| 1504 | case RefVal::Owned: { |
| 1505 | unsigned cnt = X.getCount(); |
| 1506 | X = RefVal::makeReturnedOwned(cnt); |
| 1507 | break; |
| 1508 | } |
| 1509 | |
| 1510 | case RefVal::NotOwned: { |
| 1511 | unsigned cnt = X.getCount(); |
| 1512 | X = cnt ? RefVal::makeReturnedOwned(cnt - 1) |
| 1513 | : RefVal::makeReturnedNotOwned(); |
| 1514 | break; |
| 1515 | } |
| 1516 | |
| 1517 | default: |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1518 | return; |
| 1519 | } |
| 1520 | |
| 1521 | // Update the binding. |
| 1522 | |
| 1523 | ValueState StImpl = *St; |
| 1524 | StImpl.CheckerState = RefBFactory.Add(B, Sym, X).getRoot(); |
| 1525 | Builder.MakeNode(Dst, S, Pred, StateMgr.getPersistentState(StImpl)); |
| 1526 | } |
| 1527 | |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 1528 | // Assumptions. |
| 1529 | |
| 1530 | ValueState* CFRefCount::EvalAssume(GRExprEngine& Eng, ValueState* St, |
| 1531 | RVal Cond, bool Assumption, |
| 1532 | bool& isFeasible) { |
| 1533 | |
| 1534 | // FIXME: We may add to the interface of EvalAssume the list of symbols |
| 1535 | // whose assumptions have changed. For now we just iterate through the |
| 1536 | // bindings and check if any of the tracked symbols are NULL. This isn't |
| 1537 | // too bad since the number of symbols we will track in practice are |
| 1538 | // probably small and EvalAssume is only called at branches and a few |
| 1539 | // other places. |
| 1540 | |
| 1541 | RefBindings B = GetRefBindings(*St); |
| 1542 | |
| 1543 | if (B.isEmpty()) |
| 1544 | return St; |
| 1545 | |
| 1546 | bool changed = false; |
| 1547 | |
| 1548 | for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) { |
| 1549 | |
| 1550 | // Check if the symbol is null (or equal to any constant). |
| 1551 | // If this is the case, stop tracking the symbol. |
| 1552 | |
| 1553 | if (St->getSymVal(I.getKey())) { |
| 1554 | changed = true; |
| 1555 | B = RefBFactory.Remove(B, I.getKey()); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | if (!changed) |
| 1560 | return St; |
| 1561 | |
| 1562 | ValueState StImpl = *St; |
| 1563 | StImpl.CheckerState = B.getRoot(); |
| 1564 | return Eng.getStateManager().getPersistentState(StImpl); |
| 1565 | } |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1566 | |
| 1567 | CFRefCount::RefBindings CFRefCount::Update(RefBindings B, SymbolID sym, |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1568 | RefVal V, ArgEffect E, |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1569 | RefVal::Kind& hasErr) { |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1570 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1571 | // FIXME: This dispatch can potentially be sped up by unifiying it into |
| 1572 | // a single switch statement. Opt for simplicity for now. |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1573 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1574 | switch (E) { |
| 1575 | default: |
| 1576 | assert (false && "Unhandled CFRef transition."); |
| 1577 | |
| 1578 | case DoNothing: |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1579 | if (!isGCEnabled() && V.getKind() == RefVal::Released) { |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1580 | V = RefVal::makeUseAfterRelease(); |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1581 | hasErr = V.getKind(); |
Ted Kremenek | ce3ed1e | 2008-03-12 01:21:45 +0000 | [diff] [blame] | 1582 | break; |
| 1583 | } |
| 1584 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1585 | return B; |
| 1586 | |
Ted Kremenek | 227c537 | 2008-05-06 02:41:27 +0000 | [diff] [blame^] | 1587 | case StopTracking: |
| 1588 | return RefBFactory.Remove(B, sym); |
| 1589 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1590 | case IncRef: |
| 1591 | switch (V.getKind()) { |
| 1592 | default: |
| 1593 | assert(false); |
| 1594 | |
| 1595 | case RefVal::Owned: |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1596 | V = RefVal::makeOwned(V.getCount()+1); |
| 1597 | break; |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1598 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1599 | case RefVal::NotOwned: |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1600 | V = RefVal::makeNotOwned(V.getCount()+1); |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1601 | break; |
| 1602 | |
| 1603 | case RefVal::Released: |
Ted Kremenek | a8c3c43 | 2008-05-05 22:11:16 +0000 | [diff] [blame] | 1604 | if (isGCEnabled()) |
Ted Kremenek | e2dd957 | 2008-04-29 05:44:10 +0000 | [diff] [blame] | 1605 | V = RefVal::makeOwned(); |
| 1606 | else { |
| 1607 | V = RefVal::makeUseAfterRelease(); |
| 1608 | hasErr = V.getKind(); |
| 1609 | } |
| 1610 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1611 | break; |
| 1612 | } |
| 1613 | |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1614 | break; |
| 1615 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1616 | case DecRef: |
| 1617 | switch (V.getKind()) { |
| 1618 | default: |
| 1619 | assert (false); |
| 1620 | |
| 1621 | case RefVal::Owned: { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1622 | unsigned Count = V.getCount(); |
| 1623 | V = Count > 0 ? RefVal::makeOwned(Count - 1) : RefVal::makeReleased(); |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1624 | break; |
| 1625 | } |
| 1626 | |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1627 | case RefVal::NotOwned: { |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1628 | unsigned Count = V.getCount(); |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1629 | |
Ted Kremenek | d9ccf68 | 2008-04-17 18:12:53 +0000 | [diff] [blame] | 1630 | if (Count > 0) |
| 1631 | V = RefVal::makeNotOwned(Count - 1); |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1632 | else { |
| 1633 | V = RefVal::makeReleaseNotOwned(); |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1634 | hasErr = V.getKind(); |
Ted Kremenek | c4f8102 | 2008-04-10 23:09:18 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1637 | break; |
| 1638 | } |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1639 | |
| 1640 | case RefVal::Released: |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1641 | V = RefVal::makeUseAfterRelease(); |
Ted Kremenek | 1feab29 | 2008-04-16 04:28:53 +0000 | [diff] [blame] | 1642 | hasErr = V.getKind(); |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1643 | break; |
| 1644 | } |
Ted Kremenek | ab2fa2a | 2008-04-10 23:44:06 +0000 | [diff] [blame] | 1645 | |
| 1646 | break; |
Ted Kremenek | 0d72157 | 2008-03-11 17:48:22 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | return RefBFactory.Add(B, sym, V); |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 1652 | |
| 1653 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 1654 | // Error reporting. |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 1655 | //===----------------------------------------------------------------------===// |
| 1656 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1657 | namespace { |
| 1658 | |
| 1659 | //===-------------===// |
| 1660 | // Bug Descriptions. // |
| 1661 | //===-------------===// |
| 1662 | |
Ted Kremenek | e376985 | 2008-04-18 20:54:29 +0000 | [diff] [blame] | 1663 | class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation { |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1664 | protected: |
| 1665 | CFRefCount& TF; |
| 1666 | |
| 1667 | public: |
| 1668 | CFRefBug(CFRefCount& tf) : TF(tf) {} |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1669 | |
Ted Kremenek | 5c3407a | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1670 | CFRefCount& getTF() { return TF; } |
Ted Kremenek | 0ff3f20 | 2008-05-05 23:16:31 +0000 | [diff] [blame] | 1671 | const CFRefCount& getTF() const { return TF; } |
| 1672 | |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1673 | virtual bool isLeak() const { return false; } |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1674 | }; |
| 1675 | |
| 1676 | class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug { |
| 1677 | public: |
| 1678 | UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {} |
| 1679 | |
| 1680 | virtual const char* getName() const { |
Ted Kremenek | 0ff3f20 | 2008-05-05 23:16:31 +0000 | [diff] [blame] | 1681 | return "Use-After-Release"; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1682 | } |
| 1683 | virtual const char* getDescription() const { |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1684 | return "Reference-counted object is used" |
| 1685 | " after it is released."; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | virtual void EmitWarnings(BugReporter& BR); |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1689 | }; |
| 1690 | |
| 1691 | class VISIBILITY_HIDDEN BadRelease : public CFRefBug { |
| 1692 | public: |
| 1693 | BadRelease(CFRefCount& tf) : CFRefBug(tf) {} |
| 1694 | |
| 1695 | virtual const char* getName() const { |
Ted Kremenek | 0ff3f20 | 2008-05-05 23:16:31 +0000 | [diff] [blame] | 1696 | return "Bad Release"; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1697 | } |
| 1698 | virtual const char* getDescription() const { |
| 1699 | return "Incorrect decrement of the reference count of a " |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1700 | "CoreFoundation object: " |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1701 | "The object is not owned at this point by the caller."; |
| 1702 | } |
| 1703 | |
| 1704 | virtual void EmitWarnings(BugReporter& BR); |
| 1705 | }; |
| 1706 | |
| 1707 | class VISIBILITY_HIDDEN Leak : public CFRefBug { |
| 1708 | public: |
| 1709 | Leak(CFRefCount& tf) : CFRefBug(tf) {} |
| 1710 | |
| 1711 | virtual const char* getName() const { |
Ted Kremenek | 0ff3f20 | 2008-05-05 23:16:31 +0000 | [diff] [blame] | 1712 | return getTF().isGCEnabled() ? "Memory Leak (GC)" : "Memory Leak"; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | virtual const char* getDescription() const { |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1716 | return "Object leaked."; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | virtual void EmitWarnings(BugReporter& BR); |
Ted Kremenek | 5c3407a | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1720 | virtual void GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes); |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1721 | virtual bool isLeak() const { return true; } |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1722 | }; |
| 1723 | |
| 1724 | //===---------===// |
| 1725 | // Bug Reports. // |
| 1726 | //===---------===// |
| 1727 | |
| 1728 | class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport { |
| 1729 | SymbolID Sym; |
| 1730 | public: |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1731 | CFRefReport(CFRefBug& D, ExplodedNode<ValueState> *n, SymbolID sym) |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1732 | : RangedBugReport(D, n), Sym(sym) {} |
| 1733 | |
| 1734 | virtual ~CFRefReport() {} |
| 1735 | |
Ted Kremenek | 5c3407a | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1736 | CFRefBug& getBugType() { |
| 1737 | return (CFRefBug&) RangedBugReport::getBugType(); |
| 1738 | } |
| 1739 | const CFRefBug& getBugType() const { |
| 1740 | return (const CFRefBug&) RangedBugReport::getBugType(); |
| 1741 | } |
| 1742 | |
| 1743 | virtual void getRanges(BugReporter& BR, const SourceRange*& beg, |
| 1744 | const SourceRange*& end) { |
| 1745 | |
Ted Kremenek | 198cae0 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 1746 | if (!getBugType().isLeak()) |
Ted Kremenek | 5c3407a | 2008-05-01 22:50:36 +0000 | [diff] [blame] | 1747 | RangedBugReport::getRanges(BR, beg, end); |
| 1748 | else { |
| 1749 | beg = 0; |
| 1750 | end = 0; |
| 1751 | } |
| 1752 | } |
| 1753 | |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1754 | virtual PathDiagnosticPiece* getEndPath(BugReporter& BR, |
| 1755 | ExplodedNode<ValueState>* N); |
| 1756 | |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1757 | virtual std::pair<const char**,const char**> getExtraDescriptiveText(); |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1758 | |
| 1759 | virtual PathDiagnosticPiece* VisitNode(ExplodedNode<ValueState>* N, |
| 1760 | ExplodedNode<ValueState>* PrevN, |
| 1761 | ExplodedGraph<ValueState>& G, |
| 1762 | BugReporter& BR); |
| 1763 | }; |
| 1764 | |
| 1765 | |
| 1766 | } // end anonymous namespace |
| 1767 | |
| 1768 | void CFRefCount::RegisterChecks(GRExprEngine& Eng) { |
Ted Kremenek | 2f62f35 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 1769 | if (EmitStandardWarnings) GRSimpleVals::RegisterChecks(Eng); |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1770 | Eng.Register(new UseAfterRelease(*this)); |
| 1771 | Eng.Register(new BadRelease(*this)); |
| 1772 | Eng.Register(new Leak(*this)); |
| 1773 | } |
| 1774 | |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1775 | |
| 1776 | static const char* Msgs[] = { |
| 1777 | "Code is compiled in garbage collection only mode" // GC only |
| 1778 | " (the bug occurs with garbage collection enabled).", |
| 1779 | |
| 1780 | "Code is compiled without garbage collection.", // No GC. |
| 1781 | |
| 1782 | "Code is compiled for use with and without garbage collection (GC)." |
| 1783 | " The bug occurs with GC enabled.", // Hybrid, with GC. |
| 1784 | |
| 1785 | "Code is compiled for use with and without garbage collection (GC)." |
| 1786 | " The bug occurs in non-GC mode." // Hyrbird, without GC/ |
| 1787 | }; |
| 1788 | |
| 1789 | std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() { |
| 1790 | CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF(); |
| 1791 | |
| 1792 | switch (TF.getLangOptions().getGCMode()) { |
| 1793 | default: |
| 1794 | assert(false); |
Ted Kremenek | cb470940 | 2008-05-01 04:02:04 +0000 | [diff] [blame] | 1795 | |
| 1796 | case LangOptions::GCOnly: |
| 1797 | assert (TF.isGCEnabled()); |
| 1798 | return std::make_pair(&Msgs[0], &Msgs[0]+1); |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1799 | |
| 1800 | case LangOptions::NonGC: |
| 1801 | assert (!TF.isGCEnabled()); |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 1802 | return std::make_pair(&Msgs[1], &Msgs[1]+1); |
| 1803 | |
| 1804 | case LangOptions::HybridGC: |
| 1805 | if (TF.isGCEnabled()) |
| 1806 | return std::make_pair(&Msgs[2], &Msgs[2]+1); |
| 1807 | else |
| 1808 | return std::make_pair(&Msgs[3], &Msgs[3]+1); |
| 1809 | } |
| 1810 | } |
| 1811 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1812 | PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<ValueState>* N, |
| 1813 | ExplodedNode<ValueState>* PrevN, |
| 1814 | ExplodedGraph<ValueState>& G, |
| 1815 | BugReporter& BR) { |
| 1816 | |
| 1817 | // Check if the type state has changed. |
| 1818 | |
| 1819 | ValueState* PrevSt = PrevN->getState(); |
| 1820 | ValueState* CurrSt = N->getState(); |
| 1821 | |
| 1822 | CFRefCount::RefBindings PrevB = CFRefCount::GetRefBindings(*PrevSt); |
| 1823 | CFRefCount::RefBindings CurrB = CFRefCount::GetRefBindings(*CurrSt); |
| 1824 | |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1825 | CFRefCount::RefBindings::TreeTy* PrevT = PrevB.SlimFind(Sym); |
| 1826 | CFRefCount::RefBindings::TreeTy* CurrT = CurrB.SlimFind(Sym); |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1827 | |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1828 | if (!CurrT) |
| 1829 | return NULL; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1830 | |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1831 | const char* Msg = NULL; |
| 1832 | RefVal CurrV = CurrB.SlimFind(Sym)->getValue().second; |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1833 | |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1834 | if (!PrevT) { |
| 1835 | |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1836 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 1837 | |
| 1838 | if (CurrV.isOwned()) { |
| 1839 | |
| 1840 | if (isa<CallExpr>(S)) |
| 1841 | Msg = "Function call returns an object with a +1 retain count" |
| 1842 | " (owning reference)."; |
| 1843 | else { |
| 1844 | assert (isa<ObjCMessageExpr>(S)); |
| 1845 | Msg = "Method returns an object with a +1 retain count" |
| 1846 | " (owning reference)."; |
| 1847 | } |
| 1848 | } |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1849 | else { |
| 1850 | assert (CurrV.isNotOwned()); |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (isa<CallExpr>(S)) |
| 1853 | Msg = "Function call returns an object with a +0 retain count" |
| 1854 | " (non-owning reference)."; |
| 1855 | else { |
| 1856 | assert (isa<ObjCMessageExpr>(S)); |
| 1857 | Msg = "Method returns an object with a +0 retain count" |
| 1858 | " (non-owning reference)."; |
| 1859 | } |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1860 | } |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1861 | |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1862 | FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager()); |
| 1863 | PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg); |
| 1864 | |
| 1865 | if (Expr* Exp = dyn_cast<Expr>(S)) |
| 1866 | P->addRange(Exp->getSourceRange()); |
| 1867 | |
| 1868 | return P; |
| 1869 | } |
| 1870 | |
| 1871 | // Determine if the typestate has changed. |
| 1872 | |
| 1873 | RefVal PrevV = PrevB.SlimFind(Sym)->getValue().second; |
| 1874 | |
| 1875 | if (PrevV == CurrV) |
| 1876 | return NULL; |
| 1877 | |
| 1878 | // The typestate has changed. |
| 1879 | |
| 1880 | std::ostringstream os; |
| 1881 | |
| 1882 | switch (CurrV.getKind()) { |
| 1883 | case RefVal::Owned: |
| 1884 | case RefVal::NotOwned: |
| 1885 | assert (PrevV.getKind() == CurrV.getKind()); |
| 1886 | |
| 1887 | if (PrevV.getCount() > CurrV.getCount()) |
| 1888 | os << "Reference count decremented."; |
| 1889 | else |
| 1890 | os << "Reference count incremented."; |
| 1891 | |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1892 | if (unsigned Count = GetCount(CurrV)) { |
| 1893 | |
| 1894 | os << " Object has +" << Count; |
Ted Kremenek | 752b584 | 2008-04-18 05:32:44 +0000 | [diff] [blame] | 1895 | |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1896 | if (Count > 1) |
| 1897 | os << " retain counts."; |
Ted Kremenek | 752b584 | 2008-04-18 05:32:44 +0000 | [diff] [blame] | 1898 | else |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1899 | os << " retain count."; |
Ted Kremenek | 752b584 | 2008-04-18 05:32:44 +0000 | [diff] [blame] | 1900 | } |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1901 | |
| 1902 | Msg = os.str().c_str(); |
| 1903 | |
| 1904 | break; |
| 1905 | |
| 1906 | case RefVal::Released: |
| 1907 | Msg = "Object released."; |
| 1908 | break; |
| 1909 | |
| 1910 | case RefVal::ReturnedOwned: |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1911 | Msg = "Object returned to caller as owning reference (single retain count" |
| 1912 | " transferred to caller)."; |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1913 | break; |
| 1914 | |
| 1915 | case RefVal::ReturnedNotOwned: |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1916 | Msg = "Object returned to caller with a +0 (non-owning) retain count."; |
Ted Kremenek | a850395 | 2008-04-18 04:55:01 +0000 | [diff] [blame] | 1917 | break; |
| 1918 | |
| 1919 | default: |
| 1920 | return NULL; |
| 1921 | } |
| 1922 | |
| 1923 | Stmt* S = cast<PostStmt>(N->getLocation()).getStmt(); |
| 1924 | FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager()); |
| 1925 | PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg); |
| 1926 | |
| 1927 | // Add the range by scanning the children of the statement for any bindings |
| 1928 | // to Sym. |
| 1929 | |
| 1930 | ValueStateManager& VSM = BR.getEngine().getStateManager(); |
| 1931 | |
| 1932 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 1933 | if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) { |
| 1934 | RVal X = VSM.GetRVal(CurrSt, Exp); |
| 1935 | |
| 1936 | if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&X)) |
| 1937 | if (SV->getSymbol() == Sym) { |
| 1938 | P->addRange(Exp->getSourceRange()); break; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | return P; |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1945 | PathDiagnosticPiece* CFRefReport::getEndPath(BugReporter& BR, |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 1946 | ExplodedNode<ValueState>* EndN) { |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1947 | |
| 1948 | if (!getBugType().isLeak()) |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 1949 | return RangedBugReport::getEndPath(BR, EndN); |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1950 | |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1951 | typedef CFRefCount::RefBindings RefBindings; |
| 1952 | |
| 1953 | // Get the retain count. |
| 1954 | unsigned long RetCount = 0; |
| 1955 | |
| 1956 | { |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 1957 | ValueState* St = EndN->getState(); |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 1958 | RefBindings B = RefBindings((RefBindings::TreeTy*) St->CheckerState); |
| 1959 | RefBindings::TreeTy* T = B.SlimFind(Sym); |
| 1960 | assert (T); |
| 1961 | RetCount = GetCount(T->getValue().second); |
| 1962 | } |
| 1963 | |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1964 | // We are a leak. Walk up the graph to get to the first node where the |
| 1965 | // symbol appeared. |
| 1966 | |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 1967 | ExplodedNode<ValueState>* N = EndN; |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1968 | ExplodedNode<ValueState>* Last = N; |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 1969 | |
Ted Kremenek | 198cae0 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 1970 | // Find the first node that referred to the tracked symbol. We also |
| 1971 | // try and find the first VarDecl the value was stored to. |
| 1972 | |
| 1973 | VarDecl* FirstDecl = 0; |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 1974 | |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1975 | while (N) { |
| 1976 | ValueState* St = N->getState(); |
| 1977 | RefBindings B = RefBindings((RefBindings::TreeTy*) St->CheckerState); |
Ted Kremenek | 198cae0 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 1978 | RefBindings::TreeTy* T = B.SlimFind(Sym); |
| 1979 | |
| 1980 | if (!T) |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 1981 | break; |
Ted Kremenek | 198cae0 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 1982 | |
| 1983 | VarDecl* VD = 0; |
| 1984 | |
| 1985 | // Determine if there is an LVal binding to the symbol. |
| 1986 | for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I) { |
| 1987 | if (!isa<lval::SymbolVal>(I->second) // Is the value a symbol? |
| 1988 | || cast<lval::SymbolVal>(I->second).getSymbol() != Sym) |
| 1989 | continue; |
| 1990 | |
| 1991 | if (VD) { // Multiple decls map to this symbol. |
| 1992 | VD = 0; |
| 1993 | break; |
| 1994 | } |
| 1995 | |
| 1996 | VD = I->first; |
| 1997 | } |
| 1998 | |
| 1999 | if (VD) FirstDecl = VD; |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2000 | |
| 2001 | Last = N; |
| 2002 | N = N->pred_empty() ? NULL : *(N->pred_begin()); |
| 2003 | } |
| 2004 | |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2005 | // Get the allocate site. |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2006 | |
| 2007 | assert (Last); |
| 2008 | Stmt* FirstStmt = cast<PostStmt>(Last->getLocation()).getStmt(); |
| 2009 | |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2010 | SourceManager& SMgr = BR.getContext().getSourceManager(); |
| 2011 | unsigned AllocLine = SMgr.getLogicalLineNumber(FirstStmt->getLocStart()); |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2012 | |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2013 | // Get the leak site. We may have multiple ExplodedNodes (one with the |
| 2014 | // leak) that occur on the same line number; if the node with the leak |
| 2015 | // has any immediate predecessor nodes with the same line number, find |
| 2016 | // any transitive-successors that have a different statement and use that |
| 2017 | // line number instead. This avoids emiting a diagnostic like: |
| 2018 | // |
| 2019 | // // 'y' is leaked. |
| 2020 | // int x = foo(y); |
| 2021 | // |
| 2022 | // instead we want: |
| 2023 | // |
| 2024 | // int x = foo(y); |
| 2025 | // // 'y' is leaked. |
| 2026 | |
| 2027 | Stmt* S = getStmt(BR); // This is the statement where the leak occured. |
| 2028 | assert (S); |
| 2029 | unsigned EndLine = SMgr.getLogicalLineNumber(S->getLocStart()); |
| 2030 | |
| 2031 | // Look in the *trimmed* graph at the immediate predecessor of EndN. Does |
| 2032 | // it occur on the same line? |
| 2033 | |
| 2034 | assert (!EndN->pred_empty()); // Not possible to have 0 predecessors. |
| 2035 | N = *(EndN->pred_begin()); |
| 2036 | |
| 2037 | do { |
| 2038 | ProgramPoint P = N->getLocation(); |
| 2039 | |
| 2040 | if (!isa<PostStmt>(P)) |
| 2041 | break; |
| 2042 | |
| 2043 | // Predecessor at same line? |
| 2044 | |
| 2045 | Stmt* SPred = cast<PostStmt>(P).getStmt(); |
| 2046 | |
| 2047 | if (SMgr.getLogicalLineNumber(SPred->getLocStart()) != EndLine) |
| 2048 | break; |
| 2049 | |
| 2050 | // The predecessor (where the object was not yet leaked) is a statement |
| 2051 | // on the same line. Get the first successor statement that appears |
| 2052 | // on a different line. For this operation, we can traverse the |
| 2053 | // non-trimmed graph. |
| 2054 | |
| 2055 | N = getEndNode(); // This is the node where the leak occured in the |
| 2056 | // original graph. |
| 2057 | |
| 2058 | while (!N->succ_empty()) { |
| 2059 | |
| 2060 | N = *(N->succ_begin()); |
| 2061 | ProgramPoint P = N->getLocation(); |
| 2062 | |
| 2063 | if (!isa<PostStmt>(P)) |
| 2064 | continue; |
| 2065 | |
| 2066 | Stmt* SSucc = cast<PostStmt>(P).getStmt(); |
| 2067 | |
| 2068 | if (SMgr.getLogicalLineNumber(SSucc->getLocStart()) != EndLine) { |
| 2069 | S = SSucc; |
| 2070 | break; |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | while (false); |
| 2075 | |
| 2076 | // Construct the location. |
| 2077 | |
| 2078 | FullSourceLoc L(S->getLocStart(), SMgr); |
| 2079 | |
| 2080 | // Generate the diagnostic. |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2081 | std::ostringstream os; |
Ted Kremenek | 198cae0 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 2082 | |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2083 | os << "Object allocated on line " << AllocLine; |
Ted Kremenek | 198cae0 | 2008-05-02 20:53:50 +0000 | [diff] [blame] | 2084 | |
| 2085 | if (FirstDecl) |
| 2086 | os << " and stored into '" << FirstDecl->getName() << '\''; |
| 2087 | |
Ted Kremenek | 9363fd9 | 2008-05-05 17:53:17 +0000 | [diff] [blame] | 2088 | os << " is no longer referenced after this point and has a retain count of +" |
| 2089 | << RetCount << " (object leaked)."; |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2090 | |
Ted Kremenek | ea794e9 | 2008-05-05 18:50:19 +0000 | [diff] [blame] | 2091 | return new PathDiagnosticPiece(L, os.str()); |
Ted Kremenek | fe4d231 | 2008-05-01 23:13:35 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2094 | void UseAfterRelease::EmitWarnings(BugReporter& BR) { |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2095 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2096 | for (CFRefCount::use_after_iterator I = TF.use_after_begin(), |
| 2097 | E = TF.use_after_end(); I != E; ++I) { |
| 2098 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2099 | CFRefReport report(*this, I->first, I->second.second); |
| 2100 | report.addRange(I->second.first->getSourceRange()); |
Ted Kremenek | 270ab7d | 2008-04-18 01:56:37 +0000 | [diff] [blame] | 2101 | BR.EmitWarning(report); |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2102 | } |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
| 2105 | void BadRelease::EmitWarnings(BugReporter& BR) { |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2106 | |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2107 | for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(), |
| 2108 | E = TF.bad_release_end(); I != E; ++I) { |
| 2109 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2110 | CFRefReport report(*this, I->first, I->second.second); |
| 2111 | report.addRange(I->second.first->getSourceRange()); |
| 2112 | BR.EmitWarning(report); |
Ted Kremenek | 7d421f3 | 2008-04-09 23:49:11 +0000 | [diff] [blame] | 2113 | } |
| 2114 | } |
Ted Kremenek | 10fe66d | 2008-04-09 01:10:13 +0000 | [diff] [blame] | 2115 | |
Ted Kremenek | 7f3f41a | 2008-04-17 23:43:50 +0000 | [diff] [blame] | 2116 | void Leak::EmitWarnings(BugReporter& BR) { |
| 2117 | |
| 2118 | for (CFRefCount::leaks_iterator I = TF.leaks_begin(), |
| 2119 | E = TF.leaks_end(); I != E; ++I) { |
| 2120 | |
Ted Kremenek | 2be7ddb | 2008-04-18 03:39:05 +0000 | [diff] [blame] | 2121 | std::vector<SymbolID>& SymV = *(I->second); |
| 2122 | unsigned n = SymV.size(); |
| 2123 | |
| 2124 | for (unsigned i = 0; i < n; ++i) { |
| 2125 | CFRefReport report(*this, I->first, SymV[i]); |
| 2126 | BR.EmitWarning(report); |
| 2127 | } |
Ted Kremenek | 7f3f41a | 2008-04-17 23:43:50 +0000 | [diff] [blame] | 2128 | } |
| 2129 | } |
| 2130 | |
Ted Kremenek | eef8f1e | 2008-04-18 19:23:43 +0000 | [diff] [blame] | 2131 | void Leak::GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes) { |
| 2132 | for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end(); |
| 2133 | I!=E; ++I) |
| 2134 | Nodes.push_back(I->first); |
| 2135 | } |
| 2136 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2137 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b1983ba | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 2138 | // Transfer function creation for external clients. |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 2139 | //===----------------------------------------------------------------------===// |
| 2140 | |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2141 | GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled, |
Ted Kremenek | 2f62f35 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 2142 | bool StandardWarnings, |
Ted Kremenek | fe30beb | 2008-04-30 23:47:44 +0000 | [diff] [blame] | 2143 | const LangOptions& lopts) { |
Ted Kremenek | 2f62f35 | 2008-05-02 18:01:49 +0000 | [diff] [blame] | 2144 | return new CFRefCount(Ctx, GCEnabled, StandardWarnings, lopts); |
Ted Kremenek | a4c7429 | 2008-04-10 22:58:08 +0000 | [diff] [blame] | 2145 | } |