blob: 94a502a9b689c189f9ea4550202f08a9d5c681ba [file] [log] [blame]
Chris Lattnerbe1a7a02008-03-15 23:59:48 +00001// CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
Ted Kremenek827f93b2008-03-06 00:08:09 +00002//
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 Greif2224fcb2008-03-06 10:40:09 +000010// This file defines the methods for CFRefCount, which implements
Ted Kremenek827f93b2008-03-06 00:08:09 +000011// a reference count checker for Core Foundation (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremeneka7338b42008-03-11 06:39:11 +000015#include "GRSimpleVals.h"
Ted Kremenek827f93b2008-03-06 00:08:09 +000016#include "clang/Analysis/PathSensitive/ValueState.h"
Ted Kremenekdd0126b2008-03-31 18:26:32 +000017#include "clang/Analysis/PathDiagnostic.h"
Ted Kremenek827f93b2008-03-06 00:08:09 +000018#include "clang/Analysis/LocalCheckers.h"
Ted Kremenek10fe66d2008-04-09 01:10:13 +000019#include "clang/Analysis/PathDiagnostic.h"
20#include "clang/Analysis/PathSensitive/BugReporter.h"
Ted Kremeneka7338b42008-03-11 06:39:11 +000021#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/FoldingSet.h"
23#include "llvm/ADT/ImmutableMap.h"
Ted Kremenek10fe66d2008-04-09 01:10:13 +000024#include "llvm/Support/Compiler.h"
Ted Kremenek3b11f7a2008-03-11 19:44:10 +000025#include <ostream>
Ted Kremenek827f93b2008-03-06 00:08:09 +000026
27using namespace clang;
28
Ted Kremenek7d421f32008-04-09 23:49:11 +000029//===----------------------------------------------------------------------===//
30// Symbolic Evaluation of Reference Counting Logic
31//===----------------------------------------------------------------------===//
32
Ted Kremeneka7338b42008-03-11 06:39:11 +000033namespace {
34 enum ArgEffect { IncRef, DecRef, DoNothing };
35 typedef std::vector<ArgEffect> ArgEffects;
36}
Ted Kremenek827f93b2008-03-06 00:08:09 +000037
Ted Kremeneka7338b42008-03-11 06:39:11 +000038namespace llvm {
39 template <> struct FoldingSetTrait<ArgEffects> {
Ted Kremeneka4c74292008-04-10 22:58:08 +000040 static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
Ted Kremeneka7338b42008-03-11 06:39:11 +000041 for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I)
42 ID.AddInteger((unsigned) *I);
Ted Kremeneka4c74292008-04-10 22:58:08 +000043 }
Ted Kremeneka7338b42008-03-11 06:39:11 +000044 };
45} // end llvm namespace
46
47namespace {
Ted Kremenek827f93b2008-03-06 00:08:09 +000048
Ted Kremeneka7338b42008-03-11 06:39:11 +000049class RetEffect {
50public:
Ted Kremenekab2fa2a2008-04-10 23:44:06 +000051 enum Kind { NoRet = 0x0, Alias = 0x1, OwnedSymbol = 0x2,
52 NotOwnedSymbol = 0x3 };
Ted Kremeneka7338b42008-03-11 06:39:11 +000053
54private:
55 unsigned Data;
Ted Kremeneka4c74292008-04-10 22:58:08 +000056 RetEffect(Kind k, unsigned D) { Data = (D << 2) | (unsigned) k; }
Ted Kremenek827f93b2008-03-06 00:08:09 +000057
Ted Kremeneka7338b42008-03-11 06:39:11 +000058public:
59
60 Kind getKind() const { return (Kind) (Data & 0x3); }
61
62 unsigned getValue() const {
63 assert(getKind() == Alias);
Ted Kremeneka4c74292008-04-10 22:58:08 +000064 return Data >> 2;
Ted Kremeneka7338b42008-03-11 06:39:11 +000065 }
Ted Kremenekffefc352008-04-11 22:25:11 +000066
Ted Kremeneka7338b42008-03-11 06:39:11 +000067 static RetEffect MakeAlias(unsigned Idx) { return RetEffect(Alias, Idx); }
Ted Kremenek827f93b2008-03-06 00:08:09 +000068
Ted Kremeneka7338b42008-03-11 06:39:11 +000069 static RetEffect MakeOwned() { return RetEffect(OwnedSymbol, 0); }
Ted Kremenek827f93b2008-03-06 00:08:09 +000070
Ted Kremeneka7338b42008-03-11 06:39:11 +000071 static RetEffect MakeNotOwned() { return RetEffect(NotOwnedSymbol, 0); }
72
Ted Kremenekab2fa2a2008-04-10 23:44:06 +000073 static RetEffect MakeNoRet() { return RetEffect(NoRet, 0); }
74
Ted Kremeneka7338b42008-03-11 06:39:11 +000075 operator Kind() const { return getKind(); }
76
77 void Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger(Data); }
78};
79
80
81class CFRefSummary : public llvm::FoldingSetNode {
82 ArgEffects* Args;
83 RetEffect Ret;
84public:
85
86 CFRefSummary(ArgEffects* A, RetEffect R) : Args(A), Ret(R) {}
87
88 unsigned getNumArgs() const { return Args->size(); }
89
Ted Kremenek0d721572008-03-11 17:48:22 +000090 ArgEffect getArg(unsigned idx) const {
91 assert (idx < getNumArgs());
92 return (*Args)[idx];
93 }
94
Ted Kremenekce3ed1e2008-03-12 01:21:45 +000095 RetEffect getRet() const {
96 return Ret;
97 }
98
Ted Kremeneka7338b42008-03-11 06:39:11 +000099 typedef ArgEffects::const_iterator arg_iterator;
100
101 arg_iterator begin_args() const { return Args->begin(); }
102 arg_iterator end_args() const { return Args->end(); }
103
104 static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A, RetEffect R) {
105 ID.AddPointer(A);
106 ID.Add(R);
107 }
108
109 void Profile(llvm::FoldingSetNodeID& ID) const {
110 Profile(ID, Args, Ret);
111 }
112};
113
114
115class CFRefSummaryManager {
116 typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> > AESetTy;
117 typedef llvm::FoldingSet<CFRefSummary> SummarySetTy;
118 typedef llvm::DenseMap<FunctionDecl*, CFRefSummary*> SummaryMapTy;
119
Ted Kremeneka4c74292008-04-10 22:58:08 +0000120 ASTContext& Ctx;
121 SummarySetTy SummarySet;
122 SummaryMapTy SummaryMap;
123 AESetTy AESet;
124 llvm::BumpPtrAllocator BPAlloc;
125 ArgEffects ScratchArgs;
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000126
127
128 ArgEffects* getArgEffects();
Ted Kremeneka7338b42008-03-11 06:39:11 +0000129
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000130 CFRefSummary* getCannedCFSummary(FunctionTypeProto* FT, bool isRetain);
131
132 CFRefSummary* getCFSummary(FunctionDecl* FD, const char* FName);
133
134 CFRefSummary* getCFSummaryCreateRule(FunctionTypeProto* FT);
135 CFRefSummary* getCFSummaryGetRule(FunctionTypeProto* FT);
136
137 CFRefSummary* getPersistentSummary(ArgEffects* AE, RetEffect RE);
138
Ted Kremenekab2fa2a2008-04-10 23:44:06 +0000139 void FillDoNothing(unsigned Args);
140
141
Ted Kremeneka7338b42008-03-11 06:39:11 +0000142public:
Ted Kremeneka4c74292008-04-10 22:58:08 +0000143 CFRefSummaryManager(ASTContext& ctx) : Ctx(ctx) {}
Ted Kremeneka7338b42008-03-11 06:39:11 +0000144 ~CFRefSummaryManager();
145
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000146 CFRefSummary* getSummary(FunctionDecl* FD, ASTContext& Ctx);
Ted Kremeneka7338b42008-03-11 06:39:11 +0000147};
148
149} // end anonymous namespace
150
151//===----------------------------------------------------------------------===//
152// Implementation of checker data structures.
153//===----------------------------------------------------------------------===//
154
155CFRefSummaryManager::~CFRefSummaryManager() {
156
157 // FIXME: The ArgEffects could eventually be allocated from BPAlloc,
158 // mitigating the need to do explicit cleanup of the
159 // Argument-Effect summaries.
160
161 for (AESetTy::iterator I = AESet.begin(), E = AESet.end(); I!=E; ++I)
162 I->getValue().~ArgEffects();
Ted Kremenek827f93b2008-03-06 00:08:09 +0000163}
Ted Kremeneka7338b42008-03-11 06:39:11 +0000164
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000165ArgEffects* CFRefSummaryManager::getArgEffects() {
166
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000167 llvm::FoldingSetNodeID profile;
168 profile.Add(ScratchArgs);
169 void* InsertPos;
170
171 llvm::FoldingSetNodeWrapper<ArgEffects>* E =
172 AESet.FindNodeOrInsertPos(profile, InsertPos);
173
174 if (E) {
175 ScratchArgs.clear();
176 return &E->getValue();
177 }
178
179 E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
180 BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
181
182 new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
183 AESet.InsertNode(E, InsertPos);
184
185 ScratchArgs.clear();
186 return &E->getValue();
187}
188
189CFRefSummary* CFRefSummaryManager::getPersistentSummary(ArgEffects* AE,
190 RetEffect RE) {
191
192 llvm::FoldingSetNodeID profile;
193 CFRefSummary::Profile(profile, AE, RE);
194 void* InsertPos;
195
196 CFRefSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
197
198 if (Summ)
199 return Summ;
200
201 Summ = (CFRefSummary*) BPAlloc.Allocate<CFRefSummary>();
202 new (Summ) CFRefSummary(AE, RE);
203 SummarySet.InsertNode(Summ, InsertPos);
204
205 return Summ;
206}
207
208
209CFRefSummary* CFRefSummaryManager::getSummary(FunctionDecl* FD,
210 ASTContext& Ctx) {
211
212 SourceLocation Loc = FD->getLocation();
213
214 if (!Loc.isFileID())
215 return NULL;
Ted Kremenek827f93b2008-03-06 00:08:09 +0000216
Ted Kremeneka7338b42008-03-11 06:39:11 +0000217 { // Look into our cache of summaries to see if we have already computed
218 // a summary for this FunctionDecl.
219
220 SummaryMapTy::iterator I = SummaryMap.find(FD);
221
222 if (I != SummaryMap.end())
223 return I->second;
224 }
225
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000226#if 0
227 SourceManager& SrcMgr = Ctx.getSourceManager();
228 unsigned fid = Loc.getFileID();
229 const FileEntry* FE = SrcMgr.getFileEntryForID(fid);
Ted Kremeneka7338b42008-03-11 06:39:11 +0000230
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000231 if (!FE)
232 return NULL;
233
234 const char* DirName = FE->getDir()->getName();
235 assert (DirName);
236 assert (strlen(DirName) > 0);
237
238 if (!strstr(DirName, "CoreFoundation")) {
239 SummaryMap[FD] = NULL;
240 return NULL;
241 }
242#endif
243
244 const char* FName = FD->getIdentifier()->getName();
245
246 if (FName[0] == 'C' && FName[1] == 'F') {
247 CFRefSummary* S = getCFSummary(FD, FName);
248 SummaryMap[FD] = S;
249 return S;
250 }
Ted Kremeneka7338b42008-03-11 06:39:11 +0000251
252 return NULL;
Ted Kremenek827f93b2008-03-06 00:08:09 +0000253}
254
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000255CFRefSummary* CFRefSummaryManager::getCFSummary(FunctionDecl* FD,
256 const char* FName) {
257
258 // For now, only generate summaries for functions that have a prototype.
259
260 FunctionTypeProto* FT =
261 dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
262
263 if (!FT)
264 return NULL;
265
266 FName += 2;
267
268 if (strcmp(FName, "Retain") == 0)
269 return getCannedCFSummary(FT, true);
270
271 if (strcmp(FName, "Release") == 0)
272 return getCannedCFSummary(FT, false);
273
274 assert (ScratchArgs.empty());
275 bool usesCreateRule = false;
276
277 if (strstr(FName, "Create"))
278 usesCreateRule = true;
279
280 if (!usesCreateRule && strstr(FName, "Copy"))
281 usesCreateRule = true;
282
283 if (usesCreateRule)
284 return getCFSummaryCreateRule(FT);
285
286 if (strstr(FName, "Get"))
287 return getCFSummaryGetRule(FT);
288
289 return NULL;
290}
291
292CFRefSummary* CFRefSummaryManager::getCannedCFSummary(FunctionTypeProto* FT,
293 bool isRetain) {
294
295 if (FT->getNumArgs() != 1)
296 return NULL;
297
298 TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr());
299
300 if (!ArgT)
301 return NULL;
302
303 // For CFRetain/CFRelease, the first (and only) argument is of type
304 // "CFTypeRef".
305
306 const char* TDName = ArgT->getDecl()->getIdentifier()->getName();
307 assert (TDName);
308
Ted Kremeneka4c74292008-04-10 22:58:08 +0000309 if (strcmp("CFTypeRef", TDName) != 0)
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000310 return NULL;
311
312 if (!ArgT->isPointerType())
313 return NULL;
Ted Kremeneka4c74292008-04-10 22:58:08 +0000314
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000315 QualType RetTy = FT->getResultType();
316
Ted Kremeneka4c74292008-04-10 22:58:08 +0000317 if (isRetain) {
318 // CFRetain: the return type should also be "CFTypeRef".
319 if (RetTy.getTypePtr() != ArgT)
320 return NULL;
Ted Kremenekab2fa2a2008-04-10 23:44:06 +0000321
322 // The function's interface checks out. Generate a canned summary.
323 assert (ScratchArgs.empty());
324 ScratchArgs.push_back(IncRef);
325 return getPersistentSummary(getArgEffects(), RetEffect::MakeAlias(0));
Ted Kremeneka4c74292008-04-10 22:58:08 +0000326 }
327 else {
328 // CFRelease: the return type should be void.
329
330 if (RetTy != Ctx.VoidTy)
331 return NULL;
Ted Kremeneka4c74292008-04-10 22:58:08 +0000332
Ted Kremenekab2fa2a2008-04-10 23:44:06 +0000333 assert (ScratchArgs.empty());
334 ScratchArgs.push_back(DecRef);
335 return getPersistentSummary(getArgEffects(), RetEffect::MakeNoRet());
336 }
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000337}
338
339static bool isCFRefType(QualType T) {
340
341 if (!T->isPointerType())
342 return false;
343
344 // Check the typedef for the name "CF" and the substring "Ref".
345
346 TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
347
348 if (!TD)
349 return false;
350
351 const char* TDName = TD->getDecl()->getIdentifier()->getName();
352 assert (TDName);
353
354 if (TDName[0] != 'C' || TDName[1] != 'F')
355 return false;
356
357 if (strstr(TDName, "Ref") == 0)
358 return false;
359
360 return true;
361}
362
Ted Kremenekab2fa2a2008-04-10 23:44:06 +0000363void CFRefSummaryManager::FillDoNothing(unsigned Args) {
364 for (unsigned i = 0; i != Args; ++i)
365 ScratchArgs.push_back(DoNothing);
366}
367
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000368
369CFRefSummary*
370CFRefSummaryManager::getCFSummaryCreateRule(FunctionTypeProto* FT) {
371
372 if (!isCFRefType(FT->getResultType()))
Ted Kremenekd4244d42008-04-11 20:11:19 +0000373 return NULL;
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000374
375 assert (ScratchArgs.empty());
376
377 // FIXME: Add special-cases for functions that retain/release. For now
378 // just handle the default case.
379
Ted Kremenekab2fa2a2008-04-10 23:44:06 +0000380 FillDoNothing(FT->getNumArgs());
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000381 return getPersistentSummary(getArgEffects(), RetEffect::MakeOwned());
382}
383
384CFRefSummary*
385CFRefSummaryManager::getCFSummaryGetRule(FunctionTypeProto* FT) {
386
Ted Kremenekd4244d42008-04-11 20:11:19 +0000387 QualType RetTy = FT->getResultType();
388
389 // FIXME: For now we assume that all pointer types returned are referenced
390 // counted. Since this is the "Get" rule, we assume non-ownership, which
391 // works fine for things that are not reference counted. We do this because
392 // some generic data structures return "void*". We need something better
393 // in the future.
394
395 if (!isCFRefType(RetTy) && !RetTy->isPointerType())
396 return NULL;
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000397
398 assert (ScratchArgs.empty());
399
400 // FIXME: Add special-cases for functions that retain/release. For now
401 // just handle the default case.
402
Ted Kremenekab2fa2a2008-04-10 23:44:06 +0000403 FillDoNothing(FT->getNumArgs());
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000404 return getPersistentSummary(getArgEffects(), RetEffect::MakeNotOwned());
405}
406
Ted Kremeneka7338b42008-03-11 06:39:11 +0000407//===----------------------------------------------------------------------===//
Ted Kremenek7d421f32008-04-09 23:49:11 +0000408// Bug Descriptions.
409//===----------------------------------------------------------------------===//
410
411namespace {
412
413 class CFRefCount;
414
415 class VISIBILITY_HIDDEN CFRefBug : public BugType {
416 protected:
417 CFRefCount& TF;
418
419 public:
420 CFRefBug(CFRefCount& tf) : TF(tf) {}
421 };
422
423 class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
424 public:
425 UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
426
427 virtual const char* getName() const {
428 return "(CoreFoundation) use-after-release";
429 }
430 virtual const char* getDescription() const {
431 return "(CoreFoundation) Reference-counted object is used"
432 " after it is released.";
433 }
434
435 virtual void EmitWarnings(BugReporter& BR);
436
437 };
438
439 class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
440 public:
441 BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
442
443 virtual const char* getName() const {
444 return "(CoreFoundation) release of non-owned object";
445 }
446 virtual const char* getDescription() const {
447 return "Incorrect decrement of the reference count of a "
448 "CoreFoundation object:\n"
449 "The object is not owned at this point by the caller.";
450 }
451
452 virtual void EmitWarnings(BugReporter& BR);
453 };
454
455} // end anonymous namespace
456
457//===----------------------------------------------------------------------===//
Ted Kremenek7aef4842008-04-16 20:40:59 +0000458// Reference-counting logic (typestate + counts).
Ted Kremeneka7338b42008-03-11 06:39:11 +0000459//===----------------------------------------------------------------------===//
460
Ted Kremeneka7338b42008-03-11 06:39:11 +0000461namespace {
462
Ted Kremenek7d421f32008-04-09 23:49:11 +0000463class VISIBILITY_HIDDEN RefVal {
Ted Kremenek0d721572008-03-11 17:48:22 +0000464 unsigned Data;
465
466 RefVal(unsigned K, unsigned D) : Data((D << 3) | K) {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000467 assert ((K & ~0x7) == 0x0);
Ted Kremenek0d721572008-03-11 17:48:22 +0000468 }
469
470 RefVal(unsigned K) : Data(K) {
Ted Kremeneka4c74292008-04-10 22:58:08 +0000471 assert ((K & ~0x7) == 0x0);
Ted Kremenek0d721572008-03-11 17:48:22 +0000472 }
473
474public:
Ted Kremenekc4f81022008-04-10 23:09:18 +0000475 enum Kind { Owned = 0, NotOwned = 1, Released = 2,
476 ErrorUseAfterRelease = 3, ErrorReleaseNotOwned = 4 };
Ted Kremenek0d721572008-03-11 17:48:22 +0000477
478
Ted Kremenekc4f81022008-04-10 23:09:18 +0000479 Kind getKind() const { return (Kind) (Data & 0x7); }
Ted Kremenek0d721572008-03-11 17:48:22 +0000480
481 unsigned getCount() const {
Ted Kremenekc4f81022008-04-10 23:09:18 +0000482 assert (getKind() == Owned || getKind() == NotOwned);
Ted Kremenek0d721572008-03-11 17:48:22 +0000483 return Data >> 3;
484 }
485
Ted Kremenek1daa16c2008-03-11 18:14:09 +0000486 static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
487
Ted Kremenekffefc352008-04-11 22:25:11 +0000488 bool isOwned() const {
489 return getKind() == Owned;
490 }
491
Ted Kremenekc4f81022008-04-10 23:09:18 +0000492 static RefVal makeOwned(unsigned Count = 0) {
493 return RefVal(Owned, Count);
494 }
495
496 static RefVal makeNotOwned(unsigned Count = 0) {
497 return RefVal(NotOwned, Count);
498 }
499
Ted Kremenek0d721572008-03-11 17:48:22 +0000500 static RefVal makeReleased() { return RefVal(Released); }
501 static RefVal makeUseAfterRelease() { return RefVal(ErrorUseAfterRelease); }
502 static RefVal makeReleaseNotOwned() { return RefVal(ErrorReleaseNotOwned); }
503
504 bool operator==(const RefVal& X) const { return Data == X.Data; }
505 void Profile(llvm::FoldingSetNodeID& ID) const { ID.AddInteger(Data); }
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000506
507 void print(std::ostream& Out) const;
Ted Kremenek0d721572008-03-11 17:48:22 +0000508};
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000509
510void RefVal::print(std::ostream& Out) const {
511 switch (getKind()) {
512 default: assert(false);
Ted Kremenekc4f81022008-04-10 23:09:18 +0000513 case Owned: {
514 Out << "Owned";
515 unsigned cnt = getCount();
516 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000517 break;
Ted Kremenekc4f81022008-04-10 23:09:18 +0000518 }
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000519
Ted Kremenekc4f81022008-04-10 23:09:18 +0000520 case NotOwned: {
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000521 Out << "Not-Owned";
Ted Kremenekc4f81022008-04-10 23:09:18 +0000522 unsigned cnt = getCount();
523 if (cnt) Out << " (+ " << cnt << ")";
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000524 break;
Ted Kremenekc4f81022008-04-10 23:09:18 +0000525 }
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000526
527 case Released:
528 Out << "Released";
529 break;
530
531 case ErrorUseAfterRelease:
532 Out << "Use-After-Release [ERROR]";
533 break;
534
535 case ErrorReleaseNotOwned:
536 Out << "Release of Not-Owned [ERROR]";
537 break;
538 }
539}
Ted Kremenek0d721572008-03-11 17:48:22 +0000540
Ted Kremenek7aef4842008-04-16 20:40:59 +0000541//===----------------------------------------------------------------------===//
542// Transfer functions.
543//===----------------------------------------------------------------------===//
544
545static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
546 IdentifierInfo* II = &Ctx.Idents.get(name);
547 return Ctx.Selectors.getSelector(0, &II);
548}
549
Ted Kremenek7d421f32008-04-09 23:49:11 +0000550class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000551
552 // Type definitions.
553
Ted Kremenek0d721572008-03-11 17:48:22 +0000554 typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings;
Ted Kremeneka7338b42008-03-11 06:39:11 +0000555 typedef RefBindings::Factory RefBFactoryTy;
Ted Kremenek1daa16c2008-03-11 18:14:09 +0000556
Ted Kremenek99b0ecb2008-04-11 18:40:51 +0000557 typedef llvm::DenseMap<GRExprEngine::NodeTy*,Expr*> UseAfterReleasesTy;
558 typedef llvm::DenseMap<GRExprEngine::NodeTy*,Expr*> ReleasesNotOwnedTy;
Ted Kremenek1daa16c2008-03-11 18:14:09 +0000559
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000560 class BindingsPrinter : public ValueState::CheckerStatePrinter {
561 public:
562 virtual void PrintCheckerState(std::ostream& Out, void* State,
563 const char* nl, const char* sep);
564 };
565
566 // Instance variables.
567
Ted Kremeneka7338b42008-03-11 06:39:11 +0000568 CFRefSummaryManager Summaries;
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000569 RefBFactoryTy RefBFactory;
570
Ted Kremenek1daa16c2008-03-11 18:14:09 +0000571 UseAfterReleasesTy UseAfterReleases;
572 ReleasesNotOwnedTy ReleasesNotOwned;
573
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000574 BindingsPrinter Printer;
575
Ted Kremenek1feab292008-04-16 04:28:53 +0000576 Selector RetainSelector;
577 Selector ReleaseSelector;
578
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000579 // Private methods.
Ted Kremenek1feab292008-04-16 04:28:53 +0000580
Ted Kremeneka7338b42008-03-11 06:39:11 +0000581 static RefBindings GetRefBindings(ValueState& StImpl) {
582 return RefBindings((RefBindings::TreeTy*) StImpl.CheckerState);
583 }
Ted Kremenek1feab292008-04-16 04:28:53 +0000584
Ted Kremeneka7338b42008-03-11 06:39:11 +0000585 static void SetRefBindings(ValueState& StImpl, RefBindings B) {
586 StImpl.CheckerState = B.getRoot();
587 }
Ted Kremenek1feab292008-04-16 04:28:53 +0000588
Ted Kremeneka7338b42008-03-11 06:39:11 +0000589 RefBindings Remove(RefBindings B, SymbolID sym) {
590 return RefBFactory.Remove(B, sym);
591 }
592
Ted Kremenek0d721572008-03-11 17:48:22 +0000593 RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E,
Ted Kremenek1feab292008-04-16 04:28:53 +0000594 RefVal::Kind& hasErr);
595
596 void ProcessError(ExplodedNodeSet<ValueState>& Dst,
597 GRStmtNodeBuilder<ValueState>& Builder,
598 Expr* NodeExpr, Expr* ErrorExpr,
599 ExplodedNode<ValueState>* Pred,
600 ValueState* St,
601 RefVal::Kind hasErr);
Ted Kremeneka7338b42008-03-11 06:39:11 +0000602
603public:
Ted Kremenek7aef4842008-04-16 20:40:59 +0000604
Ted Kremenek1feab292008-04-16 04:28:53 +0000605 CFRefCount(ASTContext& Ctx)
606 : Summaries(Ctx),
607 RetainSelector(GetUnarySelector("retain", Ctx)),
608 ReleaseSelector(GetUnarySelector("release", Ctx)) {}
609
Ted Kremeneka7338b42008-03-11 06:39:11 +0000610 virtual ~CFRefCount() {}
Ted Kremenek7d421f32008-04-09 23:49:11 +0000611
612 virtual void RegisterChecks(GRExprEngine& Eng);
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000613
614 virtual ValueState::CheckerStatePrinter* getCheckerStatePrinter() {
615 return &Printer;
616 }
Ted Kremeneka7338b42008-03-11 06:39:11 +0000617
618 // Calls.
619
620 virtual void EvalCall(ExplodedNodeSet<ValueState>& Dst,
Ted Kremenekce0767f2008-03-12 21:06:49 +0000621 GRExprEngine& Eng,
Ted Kremeneka7338b42008-03-11 06:39:11 +0000622 GRStmtNodeBuilder<ValueState>& Builder,
Ted Kremeneka7338b42008-03-11 06:39:11 +0000623 CallExpr* CE, LVal L,
624 ExplodedNode<ValueState>* Pred);
Ted Kremenek10fe66d2008-04-09 01:10:13 +0000625
Ted Kremenek4b4738b2008-04-15 23:44:31 +0000626 virtual void EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst,
627 GRExprEngine& Engine,
628 GRStmtNodeBuilder<ValueState>& Builder,
629 ObjCMessageExpr* ME,
630 ExplodedNode<ValueState>* Pred);
631
632 bool EvalObjCMessageExprAux(ExplodedNodeSet<ValueState>& Dst,
633 GRExprEngine& Engine,
634 GRStmtNodeBuilder<ValueState>& Builder,
635 ObjCMessageExpr* ME,
636 ExplodedNode<ValueState>* Pred);
637
Ted Kremenek7aef4842008-04-16 20:40:59 +0000638 // Stores.
639
640 virtual void EvalStore(ExplodedNodeSet<ValueState>& Dst,
641 GRExprEngine& Engine,
642 GRStmtNodeBuilder<ValueState>& Builder,
643 Expr* E, ExplodedNode<ValueState>* Pred,
644 ValueState* St, RVal TargetLV, RVal Val);
Ted Kremenekffefc352008-04-11 22:25:11 +0000645 // End-of-path.
646
647 virtual void EvalEndPath(GRExprEngine& Engine,
648 GREndPathNodeBuilder<ValueState>& Builder);
649
Ted Kremenek10fe66d2008-04-09 01:10:13 +0000650 // Error iterators.
651
652 typedef UseAfterReleasesTy::iterator use_after_iterator;
653 typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
654
Ted Kremenek7d421f32008-04-09 23:49:11 +0000655 use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
656 use_after_iterator use_after_end() { return UseAfterReleases.end(); }
Ted Kremenek10fe66d2008-04-09 01:10:13 +0000657
Ted Kremenek7d421f32008-04-09 23:49:11 +0000658 bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
659 bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
Ted Kremeneka7338b42008-03-11 06:39:11 +0000660};
661
662} // end anonymous namespace
663
Ted Kremenek7d421f32008-04-09 23:49:11 +0000664void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
665 GRSimpleVals::RegisterChecks(Eng);
666 Eng.Register(new UseAfterRelease(*this));
667 Eng.Register(new BadRelease(*this));
668}
669
670
Ted Kremenek3b11f7a2008-03-11 19:44:10 +0000671void CFRefCount::BindingsPrinter::PrintCheckerState(std::ostream& Out,
672 void* State, const char* nl,
673 const char* sep) {
674 RefBindings B((RefBindings::TreeTy*) State);
675
676 if (State)
677 Out << sep << nl;
678
679 for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
680 Out << (*I).first << " : ";
681 (*I).second.print(Out);
682 Out << nl;
683 }
684}
685
Ted Kremenek455dd862008-04-11 20:23:24 +0000686static inline ArgEffect GetArgE(CFRefSummary* Summ, unsigned idx) {
687 return Summ ? Summ->getArg(idx) : DoNothing;
688}
689
690static inline RetEffect GetRetE(CFRefSummary* Summ) {
691 return Summ ? Summ->getRet() : RetEffect::MakeNoRet();
692}
693
Ted Kremenek1feab292008-04-16 04:28:53 +0000694void CFRefCount::ProcessError(ExplodedNodeSet<ValueState>& Dst,
695 GRStmtNodeBuilder<ValueState>& Builder,
696 Expr* NodeExpr, Expr* ErrorExpr,
697 ExplodedNode<ValueState>* Pred,
698 ValueState* St,
699 RefVal::Kind hasErr) {
700 Builder.BuildSinks = true;
701 GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
702
703 if (!N) return;
704
705 switch (hasErr) {
706 default: assert(false);
707 case RefVal::ErrorUseAfterRelease:
708 UseAfterReleases[N] = ErrorExpr;
709 break;
710
711 case RefVal::ErrorReleaseNotOwned:
712 ReleasesNotOwned[N] = ErrorExpr;
713 break;
714 }
715}
716
Ted Kremenek827f93b2008-03-06 00:08:09 +0000717void CFRefCount::EvalCall(ExplodedNodeSet<ValueState>& Dst,
Ted Kremenekce0767f2008-03-12 21:06:49 +0000718 GRExprEngine& Eng,
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000719 GRStmtNodeBuilder<ValueState>& Builder,
720 CallExpr* CE, LVal L,
721 ExplodedNode<ValueState>* Pred) {
722
Ted Kremenekce0767f2008-03-12 21:06:49 +0000723 ValueStateManager& StateMgr = Eng.getStateManager();
Ted Kremenek827f93b2008-03-06 00:08:09 +0000724
Ted Kremenek73ba0472008-04-14 17:45:13 +0000725 CFRefSummary* Summ = NULL;
Ted Kremeneka7338b42008-03-11 06:39:11 +0000726
727 // Get the summary.
Ted Kremenek827f93b2008-03-06 00:08:09 +0000728
Ted Kremenek73ba0472008-04-14 17:45:13 +0000729 if (isa<lval::FuncVal>(L)) {
730 lval::FuncVal FV = cast<lval::FuncVal>(L);
731 FunctionDecl* FD = FV.getDecl();
732 Summ = Summaries.getSummary(FD, Eng.getContext());
733 }
Ted Kremenek827f93b2008-03-06 00:08:09 +0000734
Ted Kremeneka7338b42008-03-11 06:39:11 +0000735 // Get the state.
736
737 ValueState* St = Builder.GetState(Pred);
738
739 // Evaluate the effects of the call.
740
741 ValueState StVals = *St;
Ted Kremenek1feab292008-04-16 04:28:53 +0000742 RefVal::Kind hasErr = (RefVal::Kind) 0;
Ted Kremenek455dd862008-04-11 20:23:24 +0000743
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000744 // This function has a summary. Evaluate the effect of the arguments.
745
746 unsigned idx = 0;
747
Ted Kremenek99b0ecb2008-04-11 18:40:51 +0000748 Expr* ErrorExpr = NULL;
749
750 for (CallExpr::arg_iterator I = CE->arg_begin(), E = CE->arg_end();
751 I != E; ++I, ++idx) {
Ted Kremeneka7338b42008-03-11 06:39:11 +0000752
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000753 RVal V = StateMgr.GetRVal(St, *I);
Ted Kremeneka7338b42008-03-11 06:39:11 +0000754
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000755 if (isa<lval::SymbolVal>(V)) {
756 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
Ted Kremenek455dd862008-04-11 20:23:24 +0000757 RefBindings B = GetRefBindings(StVals);
758
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000759 if (RefBindings::TreeTy* T = B.SlimFind(Sym)) {
Ted Kremenek1feab292008-04-16 04:28:53 +0000760 B = Update(B, Sym, T->getValue().second, GetArgE(Summ, idx), hasErr);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000761 SetRefBindings(StVals, B);
Ted Kremenek99b0ecb2008-04-11 18:40:51 +0000762
Ted Kremenek1feab292008-04-16 04:28:53 +0000763 if (hasErr) {
Ted Kremenek99b0ecb2008-04-11 18:40:51 +0000764 ErrorExpr = *I;
765 break;
766 }
Ted Kremeneka7338b42008-03-11 06:39:11 +0000767 }
Ted Kremeneke4924202008-04-11 20:51:02 +0000768 }
769 else if (isa<LVal>(V)) { // Nuke all arguments passed by reference.
770
771 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
772 // should compose behavior, not copy it.
Ted Kremenek455dd862008-04-11 20:23:24 +0000773 StateMgr.Unbind(StVals, cast<LVal>(V));
Ted Kremeneke4924202008-04-11 20:51:02 +0000774 }
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000775 }
Ted Kremenek1feab292008-04-16 04:28:53 +0000776
777 St = StateMgr.getPersistentState(StVals);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000778
Ted Kremenek1feab292008-04-16 04:28:53 +0000779 if (hasErr) {
780 ProcessError(Dst, Builder, CE, ErrorExpr, Pred, St, hasErr);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000781 return;
Ted Kremenek0d721572008-03-11 17:48:22 +0000782 }
Ted Kremenek1feab292008-04-16 04:28:53 +0000783
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000784 // Finally, consult the summary for the return value.
785
Ted Kremenek455dd862008-04-11 20:23:24 +0000786 RetEffect RE = GetRetE(Summ);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000787
788 switch (RE.getKind()) {
789 default:
790 assert (false && "Unhandled RetEffect."); break;
791
Ted Kremenekab2fa2a2008-04-10 23:44:06 +0000792 case RetEffect::NoRet:
Ted Kremenek455dd862008-04-11 20:23:24 +0000793
794 // Make up a symbol for the return value (not reference counted).
Ted Kremeneke4924202008-04-11 20:51:02 +0000795 // FIXME: This is basically copy-and-paste from GRSimpleVals. We
796 // should compose behavior, not copy it.
Ted Kremenek455dd862008-04-11 20:23:24 +0000797
798 if (CE->getType() != Eng.getContext().VoidTy) {
799 unsigned Count = Builder.getCurrentBlockCount();
800 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
801
802 RVal X = CE->getType()->isPointerType()
803 ? cast<RVal>(lval::SymbolVal(Sym))
804 : cast<RVal>(nonlval::SymbolVal(Sym));
805
806 St = StateMgr.SetRVal(St, CE, X, Eng.getCFG().isBlkExpr(CE), false);
807 }
808
Ted Kremenekab2fa2a2008-04-10 23:44:06 +0000809 break;
810
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000811 case RetEffect::Alias: {
812 unsigned idx = RE.getValue();
813 assert (idx < CE->getNumArgs());
814 RVal V = StateMgr.GetRVal(St, CE->getArg(idx));
Ted Kremenekce0767f2008-03-12 21:06:49 +0000815 St = StateMgr.SetRVal(St, CE, V, Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000816 break;
817 }
818
819 case RetEffect::OwnedSymbol: {
820 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd4676512008-03-12 21:45:47 +0000821 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000822
823 ValueState StImpl = *St;
824 RefBindings B = GetRefBindings(StImpl);
Ted Kremenekc4f81022008-04-10 23:09:18 +0000825 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned()));
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000826
827 St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl),
828 CE, lval::SymbolVal(Sym),
Ted Kremenekce0767f2008-03-12 21:06:49 +0000829 Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000830
831 break;
832 }
833
834 case RetEffect::NotOwnedSymbol: {
835 unsigned Count = Builder.getCurrentBlockCount();
Ted Kremenekd4676512008-03-12 21:45:47 +0000836 SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(CE, Count);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000837
838 ValueState StImpl = *St;
839 RefBindings B = GetRefBindings(StImpl);
840 SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeNotOwned()));
841
842 St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl),
843 CE, lval::SymbolVal(Sym),
Ted Kremenekce0767f2008-03-12 21:06:49 +0000844 Eng.getCFG().isBlkExpr(CE), false);
Ted Kremenekce3ed1e2008-03-12 01:21:45 +0000845
846 break;
847 }
848 }
849
Ted Kremenekf10f2882008-03-21 21:30:14 +0000850 Builder.MakeNode(Dst, CE, Pred, St);
Ted Kremenek827f93b2008-03-06 00:08:09 +0000851}
Ted Kremeneka7338b42008-03-11 06:39:11 +0000852
Ted Kremenek4b4738b2008-04-15 23:44:31 +0000853
854void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst,
855 GRExprEngine& Eng,
856 GRStmtNodeBuilder<ValueState>& Builder,
857 ObjCMessageExpr* ME,
858 ExplodedNode<ValueState>* Pred) {
859
860 if (EvalObjCMessageExprAux(Dst, Eng, Builder, ME, Pred))
861 GRSimpleVals::EvalObjCMessageExpr(Dst, Eng, Builder, ME, Pred);
862}
863
864bool CFRefCount::EvalObjCMessageExprAux(ExplodedNodeSet<ValueState>& Dst,
865 GRExprEngine& Eng,
866 GRStmtNodeBuilder<ValueState>& Builder,
867 ObjCMessageExpr* ME,
868 ExplodedNode<ValueState>* Pred) {
869
Ted Kremenek1feab292008-04-16 04:28:53 +0000870 // Handle "toll-free bridging" of calls to "Release" and "Retain".
871
872 // FIXME: track the underlying object type associated so that we can
873 // flag illegal uses of toll-free bridging (or at least handle it
874 // at casts).
Ted Kremenek4b4738b2008-04-15 23:44:31 +0000875
876 Selector S = ME->getSelector();
877
878 if (!S.isUnarySelector())
879 return true;
880
Ted Kremenek1feab292008-04-16 04:28:53 +0000881 Expr* Receiver = ME->getReceiver();
882
883 if (!Receiver)
884 return true;
885
886 // Check if we are calling "Retain" or "Release".
887
888 bool isRetain = false;
889
890 if (S == RetainSelector)
891 isRetain = true;
892 else if (S != ReleaseSelector)
893 return true;
894
895 // We have "Retain" or "Release". Get the reference binding.
896
897 ValueStateManager& StateMgr = Eng.getStateManager();
898 ValueState* St = Builder.GetState(Pred);
899 RVal V = StateMgr.GetRVal(St, Receiver);
900
901 if (!isa<lval::SymbolVal>(V))
902 return true;
903
904 SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
905 RefBindings B = GetRefBindings(*St);
906
907 RefBindings::TreeTy* T = B.SlimFind(Sym);
908
909 if (!T)
910 return true;
911
912 RefVal::Kind hasErr = (RefVal::Kind) 0;
913 B = Update(B, Sym, T->getValue().second, isRetain ? IncRef : DecRef, hasErr);
914
915 // Create a new state with the updated bindings.
916
917 ValueState StVals = *St;
918 SetRefBindings(StVals, B);
919 St = StateMgr.getPersistentState(StVals);
920
921 // Create an error node if it exists.
922
923 if (hasErr)
924 ProcessError(Dst, Builder, ME, Receiver, Pred, St, hasErr);
925 else
926 Builder.MakeNode(Dst, ME, Pred, St);
927
928 return false;
Ted Kremenek4b4738b2008-04-15 23:44:31 +0000929}
930
Ted Kremenek7aef4842008-04-16 20:40:59 +0000931// Stores.
932
933void CFRefCount::EvalStore(ExplodedNodeSet<ValueState>& Dst,
934 GRExprEngine& Eng,
935 GRStmtNodeBuilder<ValueState>& Builder,
936 Expr* E, ExplodedNode<ValueState>* Pred,
937 ValueState* St, RVal TargetLV, RVal Val) {
938
939 // Check if we have a binding for "Val" and if we are storing it to something
940 // we don't understand or otherwise the value "escapes" the function.
941
942 if (!isa<lval::SymbolVal>(Val))
943 return;
944
945 // Are we storing to something that causes the value to "escape"?
946
947 bool escapes = false;
948
949 if (!isa<lval::DeclVal>(TargetLV))
950 escapes = true;
951 else
952 escapes = cast<lval::DeclVal>(TargetLV).getDecl()->hasGlobalStorage();
953
954 if (!escapes)
955 return;
956
957 SymbolID Sym = cast<lval::SymbolVal>(Val).getSymbol();
958 RefBindings B = GetRefBindings(*St);
959 RefBindings::TreeTy* T = B.SlimFind(Sym);
960
961 if (!T)
962 return;
963
964 // Nuke the binding.
965
966 ValueState StImpl = *St;
967 StImpl.CheckerState = RefBFactory.Remove(B, Sym).getRoot();
968 St = Eng.getStateManager().getPersistentState(StImpl);
969
970 // Hand of the remaining logic to the parent implementation.
971 GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, St, TargetLV, Val);
972}
973
Ted Kremenekffefc352008-04-11 22:25:11 +0000974// End-of-path.
975
976void CFRefCount::EvalEndPath(GRExprEngine& Engine,
977 GREndPathNodeBuilder<ValueState>& Builder) {
978
979 RefBindings B = GetRefBindings(*Builder.getState());
980
981 // Scan the set of bindings for symbols that are in the Owned state.
982
983 for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I)
984 if ((*I).second.isOwned()) {
985
986 // FIXME: Register an error with the diagnostic engine. Since we
987 // don't have a Stmt* here, we need some extra machinery to get
988 // a sourcelocation.
989
990 }
991}
992
Ted Kremeneka7338b42008-03-11 06:39:11 +0000993
994CFRefCount::RefBindings CFRefCount::Update(RefBindings B, SymbolID sym,
Ted Kremenek0d721572008-03-11 17:48:22 +0000995 RefVal V, ArgEffect E,
Ted Kremenek1feab292008-04-16 04:28:53 +0000996 RefVal::Kind& hasErr) {
Ted Kremeneka7338b42008-03-11 06:39:11 +0000997
Ted Kremenek0d721572008-03-11 17:48:22 +0000998 // FIXME: This dispatch can potentially be sped up by unifiying it into
999 // a single switch statement. Opt for simplicity for now.
Ted Kremeneka7338b42008-03-11 06:39:11 +00001000
Ted Kremenek0d721572008-03-11 17:48:22 +00001001 switch (E) {
1002 default:
1003 assert (false && "Unhandled CFRef transition.");
1004
1005 case DoNothing:
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001006 if (V.getKind() == RefVal::Released) {
1007 V = RefVal::makeUseAfterRelease();
Ted Kremenek1feab292008-04-16 04:28:53 +00001008 hasErr = V.getKind();
Ted Kremenekce3ed1e2008-03-12 01:21:45 +00001009 break;
1010 }
1011
Ted Kremenek0d721572008-03-11 17:48:22 +00001012 return B;
1013
1014 case IncRef:
1015 switch (V.getKind()) {
1016 default:
1017 assert(false);
1018
1019 case RefVal::Owned:
Ted Kremenekab2fa2a2008-04-10 23:44:06 +00001020 V = RefVal::makeOwned(V.getCount()+1);
1021 break;
Ted Kremenekc4f81022008-04-10 23:09:18 +00001022
Ted Kremenek0d721572008-03-11 17:48:22 +00001023 case RefVal::NotOwned:
Ted Kremenekc4f81022008-04-10 23:09:18 +00001024 V = RefVal::makeNotOwned(V.getCount()+1);
Ted Kremenek0d721572008-03-11 17:48:22 +00001025 break;
1026
1027 case RefVal::Released:
Ted Kremenek0d721572008-03-11 17:48:22 +00001028 V = RefVal::makeUseAfterRelease();
Ted Kremenek1feab292008-04-16 04:28:53 +00001029 hasErr = V.getKind();
Ted Kremenek0d721572008-03-11 17:48:22 +00001030 break;
1031 }
1032
Ted Kremenekab2fa2a2008-04-10 23:44:06 +00001033 break;
1034
Ted Kremenek0d721572008-03-11 17:48:22 +00001035 case DecRef:
1036 switch (V.getKind()) {
1037 default:
1038 assert (false);
1039
1040 case RefVal::Owned: {
Ted Kremenekc4f81022008-04-10 23:09:18 +00001041 signed Count = ((signed) V.getCount()) - 1;
1042 V = Count >= 0 ? RefVal::makeOwned(Count) : RefVal::makeReleased();
Ted Kremenek0d721572008-03-11 17:48:22 +00001043 break;
1044 }
1045
Ted Kremenekc4f81022008-04-10 23:09:18 +00001046 case RefVal::NotOwned: {
1047 signed Count = ((signed) V.getCount()) - 1;
1048
1049 if (Count >= 0)
1050 V = RefVal::makeNotOwned(Count);
1051 else {
1052 V = RefVal::makeReleaseNotOwned();
Ted Kremenek1feab292008-04-16 04:28:53 +00001053 hasErr = V.getKind();
Ted Kremenekc4f81022008-04-10 23:09:18 +00001054 }
1055
Ted Kremenek0d721572008-03-11 17:48:22 +00001056 break;
1057 }
Ted Kremenek0d721572008-03-11 17:48:22 +00001058
1059 case RefVal::Released:
Ted Kremenek0d721572008-03-11 17:48:22 +00001060 V = RefVal::makeUseAfterRelease();
Ted Kremenek1feab292008-04-16 04:28:53 +00001061 hasErr = V.getKind();
Ted Kremenek0d721572008-03-11 17:48:22 +00001062 break;
1063 }
Ted Kremenekab2fa2a2008-04-10 23:44:06 +00001064
1065 break;
Ted Kremenek0d721572008-03-11 17:48:22 +00001066 }
1067
1068 return RefBFactory.Add(B, sym, V);
Ted Kremeneka7338b42008-03-11 06:39:11 +00001069}
1070
Ted Kremenek10fe66d2008-04-09 01:10:13 +00001071
1072//===----------------------------------------------------------------------===//
Ted Kremenek7d421f32008-04-09 23:49:11 +00001073// Error reporting.
Ted Kremenek10fe66d2008-04-09 01:10:13 +00001074//===----------------------------------------------------------------------===//
1075
Ted Kremenek7d421f32008-04-09 23:49:11 +00001076void UseAfterRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenek10fe66d2008-04-09 01:10:13 +00001077
Ted Kremenek7d421f32008-04-09 23:49:11 +00001078 for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
1079 E = TF.use_after_end(); I != E; ++I) {
1080
Ted Kremeneke3ef1c72008-04-14 17:39:48 +00001081 RangedBugReport report(*this, I->first);
Ted Kremenek99b0ecb2008-04-11 18:40:51 +00001082 report.addRange(I->second->getSourceRange());
Ted Kremeneke3ef1c72008-04-14 17:39:48 +00001083 BR.EmitPathWarning(report);
Ted Kremenek10fe66d2008-04-09 01:10:13 +00001084 }
Ted Kremenek7d421f32008-04-09 23:49:11 +00001085}
1086
1087void BadRelease::EmitWarnings(BugReporter& BR) {
Ted Kremenek10fe66d2008-04-09 01:10:13 +00001088
Ted Kremenek7d421f32008-04-09 23:49:11 +00001089 for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
1090 E = TF.bad_release_end(); I != E; ++I) {
1091
Ted Kremeneke3ef1c72008-04-14 17:39:48 +00001092 RangedBugReport report(*this, I->first);
Ted Kremenek99b0ecb2008-04-11 18:40:51 +00001093 report.addRange(I->second->getSourceRange());
Ted Kremeneke3ef1c72008-04-14 17:39:48 +00001094 BR.EmitPathWarning(report);
Ted Kremenek99b0ecb2008-04-11 18:40:51 +00001095
Ted Kremenek7d421f32008-04-09 23:49:11 +00001096 }
1097}
Ted Kremenek10fe66d2008-04-09 01:10:13 +00001098
Ted Kremeneka7338b42008-03-11 06:39:11 +00001099//===----------------------------------------------------------------------===//
Ted Kremenekb1983ba2008-04-10 22:16:52 +00001100// Transfer function creation for external clients.
Ted Kremeneka7338b42008-03-11 06:39:11 +00001101//===----------------------------------------------------------------------===//
1102
Ted Kremeneka4c74292008-04-10 22:58:08 +00001103GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx) {
1104 return new CFRefCount(Ctx);
1105}