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