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