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