2008-03-16 02:59:48 +03:00
|
|
|
// CFRefCount.cpp - Transfer functions for tracking simple values -*- C++ -*--//
|
2008-03-06 03:08:09 +03:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-06 13:40:09 +03:00
|
|
|
// This file defines the methods for CFRefCount, which implements
|
2008-03-06 03:08:09 +03:00
|
|
|
// a reference count checker for Core Foundation (Mac OS X).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
#include "GRSimpleVals.h"
|
2008-05-01 03:47:44 +04:00
|
|
|
#include "clang/Basic/LangOptions.h"
|
2008-05-02 03:13:35 +04:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2008-03-06 03:08:09 +03:00
|
|
|
#include "clang/Analysis/PathSensitive/ValueState.h"
|
2008-03-31 22:26:32 +04:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
2008-03-06 03:08:09 +03:00
|
|
|
#include "clang/Analysis/LocalCheckers.h"
|
2008-04-09 05:10:13 +04:00
|
|
|
#include "clang/Analysis/PathDiagnostic.h"
|
|
|
|
#include "clang/Analysis/PathSensitive/BugReporter.h"
|
2008-08-11 09:35:13 +04:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-03-11 09:39:11 +03:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
|
|
|
#include "llvm/ADT/ImmutableMap.h"
|
2008-05-07 22:36:45 +04:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-04-09 05:10:13 +04:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-05-16 22:33:44 +04:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-03-11 22:44:10 +03:00
|
|
|
#include <ostream>
|
2008-04-18 08:55:01 +04:00
|
|
|
#include <sstream>
|
2008-03-06 03:08:09 +03:00
|
|
|
|
|
|
|
using namespace clang;
|
2008-05-07 22:36:45 +04:00
|
|
|
using llvm::CStrInCStrNoCase;
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-06-26 01:21:56 +04:00
|
|
|
// Selector creation functions.
|
2008-04-17 22:12:53 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-01 22:31:44 +04:00
|
|
|
static inline Selector GetNullarySelector(const char* name, ASTContext& Ctx) {
|
2008-04-17 22:12:53 +04:00
|
|
|
IdentifierInfo* II = &Ctx.Idents.get(name);
|
|
|
|
return Ctx.Selectors.getSelector(0, &II);
|
|
|
|
}
|
|
|
|
|
2008-05-06 04:30:21 +04:00
|
|
|
static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
|
|
|
|
IdentifierInfo* II = &Ctx.Idents.get(name);
|
|
|
|
return Ctx.Selectors.getSelector(1, &II);
|
|
|
|
}
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Type querying functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-08 00:06:41 +04:00
|
|
|
static bool isCFRefType(QualType T) {
|
|
|
|
|
|
|
|
if (!T->isPointerType())
|
|
|
|
return false;
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
// Check the typedef for the name "CF" and the substring "Ref".
|
2008-05-08 00:06:41 +04:00
|
|
|
TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
|
|
|
|
|
|
|
|
if (!TD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char* TDName = TD->getDecl()->getIdentifier()->getName();
|
|
|
|
assert (TDName);
|
|
|
|
|
|
|
|
if (TDName[0] != 'C' || TDName[1] != 'F')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (strstr(TDName, "Ref") == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-07-15 20:50:12 +04:00
|
|
|
static bool isCGRefType(QualType T) {
|
|
|
|
|
|
|
|
if (!T->isPointerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check the typedef for the name "CG" and the substring "Ref".
|
|
|
|
TypedefType* TD = dyn_cast<TypedefType>(T.getTypePtr());
|
|
|
|
|
|
|
|
if (!TD)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char* TDName = TD->getDecl()->getIdentifier()->getName();
|
|
|
|
assert (TDName);
|
|
|
|
|
|
|
|
if (TDName[0] != 'C' || TDName[1] != 'G')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (strstr(TDName, "Ref") == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-05-08 00:06:41 +04:00
|
|
|
static bool isNSType(QualType T) {
|
|
|
|
|
|
|
|
if (!T->isPointerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ObjCInterfaceType* OT = dyn_cast<ObjCInterfaceType>(T.getTypePtr());
|
|
|
|
|
|
|
|
if (!OT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const char* ClsName = OT->getDecl()->getIdentifier()->getName();
|
|
|
|
assert (ClsName);
|
|
|
|
|
|
|
|
if (ClsName[0] != 'N' || ClsName[1] != 'S')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-06-26 01:21:56 +04:00
|
|
|
// Primitives used for constructing summaries for function/method calls.
|
2008-04-10 03:49:11 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
namespace {
|
|
|
|
/// ArgEffect is used to summarize a function/method call's effect on a
|
|
|
|
/// particular argument.
|
2008-07-09 22:11:16 +04:00
|
|
|
enum ArgEffect { IncRef, DecRef, DoNothing, DoNothingByRef,
|
|
|
|
StopTracking, MayEscape, SelfOwn, Autorelease };
|
2008-06-26 01:21:56 +04:00
|
|
|
|
|
|
|
/// ArgEffects summarizes the effects of a function/method call on all of
|
|
|
|
/// its arguments.
|
|
|
|
typedef std::vector<std::pair<unsigned,ArgEffect> > ArgEffects;
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
namespace llvm {
|
2008-06-26 01:21:56 +04:00
|
|
|
template <> struct FoldingSetTrait<ArgEffects> {
|
|
|
|
static void Profile(const ArgEffects& X, FoldingSetNodeID& ID) {
|
|
|
|
for (ArgEffects::const_iterator I = X.begin(), E = X.end(); I!= E; ++I) {
|
|
|
|
ID.AddInteger(I->first);
|
|
|
|
ID.AddInteger((unsigned) I->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2008-03-11 09:39:11 +03:00
|
|
|
} // end llvm namespace
|
|
|
|
|
|
|
|
namespace {
|
2008-06-26 01:21:56 +04:00
|
|
|
|
|
|
|
/// RetEffect is used to summarize a function/method call's behavior with
|
|
|
|
/// respect to its return value.
|
|
|
|
class VISIBILITY_HIDDEN RetEffect {
|
2008-03-11 09:39:11 +03:00
|
|
|
public:
|
2008-06-23 22:02:52 +04:00
|
|
|
enum Kind { NoRet, Alias, OwnedSymbol, OwnedAllocatedSymbol,
|
|
|
|
NotOwnedSymbol, ReceiverAlias };
|
2008-06-26 01:21:56 +04:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
private:
|
|
|
|
unsigned Data;
|
2008-06-26 01:21:56 +04:00
|
|
|
RetEffect(Kind k, unsigned D = 0) { Data = (D << 3) | (unsigned) k; }
|
2008-03-11 09:39:11 +03:00
|
|
|
|
|
|
|
public:
|
2008-06-26 01:21:56 +04:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
Kind getKind() const { return (Kind) (Data & 0x7); }
|
2008-06-26 01:21:56 +04:00
|
|
|
|
|
|
|
unsigned getIndex() const {
|
2008-03-11 09:39:11 +03:00
|
|
|
assert(getKind() == Alias);
|
2008-05-06 02:11:16 +04:00
|
|
|
return Data >> 3;
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
static RetEffect MakeAlias(unsigned Idx) {
|
|
|
|
return RetEffect(Alias, Idx);
|
|
|
|
}
|
|
|
|
static RetEffect MakeReceiverAlias() {
|
|
|
|
return RetEffect(ReceiverAlias);
|
|
|
|
}
|
2008-06-23 22:02:52 +04:00
|
|
|
static RetEffect MakeOwned(bool isAllocated = false) {
|
2008-06-26 01:21:56 +04:00
|
|
|
return RetEffect(isAllocated ? OwnedAllocatedSymbol : OwnedSymbol);
|
|
|
|
}
|
|
|
|
static RetEffect MakeNotOwned() {
|
|
|
|
return RetEffect(NotOwnedSymbol);
|
|
|
|
}
|
|
|
|
static RetEffect MakeNoRet() {
|
|
|
|
return RetEffect(NoRet);
|
2008-06-23 22:02:52 +04:00
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
operator Kind() const {
|
|
|
|
return getKind();
|
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
ID.AddInteger(Data);
|
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
};
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN RetainSummary : public llvm::FoldingSetNode {
|
2008-05-06 19:44:25 +04:00
|
|
|
/// Args - an ordered vector of (index, ArgEffect) pairs, where index
|
|
|
|
/// specifies the argument (starting from 0). This can be sparsely
|
|
|
|
/// populated; arguments with no entry in Args use 'DefaultArgEffect'.
|
2008-03-11 09:39:11 +03:00
|
|
|
ArgEffects* Args;
|
2008-05-06 19:44:25 +04:00
|
|
|
|
|
|
|
/// DefaultArgEffect - The default ArgEffect to apply to arguments that
|
|
|
|
/// do not have an entry in Args.
|
|
|
|
ArgEffect DefaultArgEffect;
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// Receiver - If this summary applies to an Objective-C message expression,
|
|
|
|
/// this is the effect applied to the state of the receiver.
|
2008-05-06 06:26:56 +04:00
|
|
|
ArgEffect Receiver;
|
2008-06-26 01:21:56 +04:00
|
|
|
|
|
|
|
/// Ret - The effect on the return value. Used to indicate if the
|
|
|
|
/// function/method call returns a new tracked symbol, returns an
|
|
|
|
/// alias of one of the arguments in the call, and so on.
|
2008-03-11 09:39:11 +03:00
|
|
|
RetEffect Ret;
|
2008-06-26 01:21:56 +04:00
|
|
|
|
2008-07-18 21:24:20 +04:00
|
|
|
/// EndPath - Indicates that execution of this method/function should
|
|
|
|
/// terminate the simulation of a path.
|
|
|
|
bool EndPath;
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
public:
|
|
|
|
|
2008-05-06 19:44:25 +04:00
|
|
|
RetainSummary(ArgEffects* A, RetEffect R, ArgEffect defaultEff,
|
2008-07-18 21:24:20 +04:00
|
|
|
ArgEffect ReceiverEff, bool endpath = false)
|
|
|
|
: Args(A), DefaultArgEffect(defaultEff), Receiver(ReceiverEff), Ret(R),
|
|
|
|
EndPath(endpath) {}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// getArg - Return the argument effect on the argument specified by
|
|
|
|
/// idx (starting from 0).
|
2008-03-11 20:48:22 +03:00
|
|
|
ArgEffect getArg(unsigned idx) const {
|
2008-05-06 19:44:25 +04:00
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
if (!Args)
|
2008-05-06 19:44:25 +04:00
|
|
|
return DefaultArgEffect;
|
2008-04-24 21:22:33 +04:00
|
|
|
|
|
|
|
// If Args is present, it is likely to contain only 1 element.
|
|
|
|
// Just do a linear search. Do it from the back because functions with
|
|
|
|
// large numbers of arguments will be tail heavy with respect to which
|
2008-06-26 01:21:56 +04:00
|
|
|
// argument they actually modify with respect to the reference count.
|
2008-04-24 21:22:33 +04:00
|
|
|
for (ArgEffects::reverse_iterator I=Args->rbegin(), E=Args->rend();
|
|
|
|
I!=E; ++I) {
|
|
|
|
|
|
|
|
if (idx > I->first)
|
2008-05-06 19:44:25 +04:00
|
|
|
return DefaultArgEffect;
|
2008-04-24 21:22:33 +04:00
|
|
|
|
|
|
|
if (idx == I->first)
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2008-05-06 19:44:25 +04:00
|
|
|
return DefaultArgEffect;
|
2008-03-11 20:48:22 +03:00
|
|
|
}
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// getRetEffect - Returns the effect on the return value of the call.
|
2008-05-06 06:26:56 +04:00
|
|
|
RetEffect getRetEffect() const {
|
2008-03-12 04:21:45 +03:00
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2008-07-18 21:24:20 +04:00
|
|
|
/// isEndPath - Returns true if executing the given method/function should
|
|
|
|
/// terminate the path.
|
|
|
|
bool isEndPath() const { return EndPath; }
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// getReceiverEffect - Returns the effect on the receiver of the call.
|
|
|
|
/// This is only meaningful if the summary applies to an ObjCMessageExpr*.
|
2008-05-06 06:26:56 +04:00
|
|
|
ArgEffect getReceiverEffect() const {
|
|
|
|
return Receiver;
|
|
|
|
}
|
|
|
|
|
This patch is motivated by numerous strict-aliasing warnings when compiling
clang as a Release build.
The big change is that all AST nodes (subclasses of Stmt) whose children are
Expr* store their children as Stmt* or arrays of Stmt*. This is to remove
strict-aliasing warnings when using StmtIterator. None of the interfaces of any
of the classes have changed (except those with arg_iterators, see below), as the
accessor methods introduce the needed casts (via cast<>). While this extra
casting may seem cumbersome, it actually adds some important sanity checks
throughout the codebase, as clients using StmtIterator can potentially overwrite
children that are expected to be Expr* with Stmt* (that aren't Expr*). The casts
provide extra sanity checks that are operational in debug builds to catch
invariant violations such as these.
For classes that have arg_iterators (e.g., CallExpr), the definition of
arg_iterator has been replaced. Instead of it being Expr**, it is an actual
class (called ExprIterator) that wraps a Stmt**, and provides the necessary
operators for iteration. The nice thing about this class is that it also uses
cast<> to type-checking, which introduces extra sanity checks throughout the
codebase that are useful for debugging.
A few of the CodeGen functions that use arg_iterator (especially from
OverloadExpr) have been modified to take begin and end iterators instead of a
base Expr** and the number of arguments. This matches more with the abstraction
of iteration. This still needs to be cleaned up a little bit, as clients expect
that ExprIterator is a RandomAccessIterator (which we may or may not wish to
allow for efficiency of representation).
This is a fairly large patch. It passes the tests (except CodeGen/bitfield.c,
which was already broken) on both a Debug and Release build, but it should
obviously be reviewed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52378 91177308-0d34-0410-b5e6-96231b3b80d8
2008-06-17 06:43:46 +04:00
|
|
|
typedef ArgEffects::const_iterator ExprIterator;
|
2008-03-11 09:39:11 +03:00
|
|
|
|
This patch is motivated by numerous strict-aliasing warnings when compiling
clang as a Release build.
The big change is that all AST nodes (subclasses of Stmt) whose children are
Expr* store their children as Stmt* or arrays of Stmt*. This is to remove
strict-aliasing warnings when using StmtIterator. None of the interfaces of any
of the classes have changed (except those with arg_iterators, see below), as the
accessor methods introduce the needed casts (via cast<>). While this extra
casting may seem cumbersome, it actually adds some important sanity checks
throughout the codebase, as clients using StmtIterator can potentially overwrite
children that are expected to be Expr* with Stmt* (that aren't Expr*). The casts
provide extra sanity checks that are operational in debug builds to catch
invariant violations such as these.
For classes that have arg_iterators (e.g., CallExpr), the definition of
arg_iterator has been replaced. Instead of it being Expr**, it is an actual
class (called ExprIterator) that wraps a Stmt**, and provides the necessary
operators for iteration. The nice thing about this class is that it also uses
cast<> to type-checking, which introduces extra sanity checks throughout the
codebase that are useful for debugging.
A few of the CodeGen functions that use arg_iterator (especially from
OverloadExpr) have been modified to take begin and end iterators instead of a
base Expr** and the number of arguments. This matches more with the abstraction
of iteration. This still needs to be cleaned up a little bit, as clients expect
that ExprIterator is a RandomAccessIterator (which we may or may not wish to
allow for efficiency of representation).
This is a fairly large patch. It passes the tests (except CodeGen/bitfield.c,
which was already broken) on both a Debug and Release build, but it should
obviously be reviewed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52378 91177308-0d34-0410-b5e6-96231b3b80d8
2008-06-17 06:43:46 +04:00
|
|
|
ExprIterator begin_args() const { return Args->begin(); }
|
|
|
|
ExprIterator end_args() const { return Args->end(); }
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-05-06 06:26:56 +04:00
|
|
|
static void Profile(llvm::FoldingSetNodeID& ID, ArgEffects* A,
|
2008-05-06 19:44:25 +04:00
|
|
|
RetEffect RetEff, ArgEffect DefaultEff,
|
2008-07-18 21:39:56 +04:00
|
|
|
ArgEffect ReceiverEff, bool EndPath) {
|
2008-03-11 09:39:11 +03:00
|
|
|
ID.AddPointer(A);
|
2008-05-06 06:26:56 +04:00
|
|
|
ID.Add(RetEff);
|
2008-05-06 19:44:25 +04:00
|
|
|
ID.AddInteger((unsigned) DefaultEff);
|
2008-05-06 06:26:56 +04:00
|
|
|
ID.AddInteger((unsigned) ReceiverEff);
|
2008-07-18 21:39:56 +04:00
|
|
|
ID.AddInteger((unsigned) EndPath);
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) const {
|
2008-07-18 21:39:56 +04:00
|
|
|
Profile(ID, Args, Ret, DefaultArgEffect, Receiver, EndPath);
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
|
|
|
};
|
2008-06-24 03:30:29 +04:00
|
|
|
} // end anonymous namespace
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Data structures for constructing summaries.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-06-24 03:30:29 +04:00
|
|
|
namespace {
|
2008-06-26 01:21:56 +04:00
|
|
|
class VISIBILITY_HIDDEN ObjCSummaryKey {
|
|
|
|
IdentifierInfo* II;
|
|
|
|
Selector S;
|
|
|
|
public:
|
|
|
|
ObjCSummaryKey(IdentifierInfo* ii, Selector s)
|
|
|
|
: II(ii), S(s) {}
|
|
|
|
|
|
|
|
ObjCSummaryKey(ObjCInterfaceDecl* d, Selector s)
|
|
|
|
: II(d ? d->getIdentifier() : 0), S(s) {}
|
|
|
|
|
|
|
|
ObjCSummaryKey(Selector s)
|
|
|
|
: II(0), S(s) {}
|
|
|
|
|
|
|
|
IdentifierInfo* getIdentifier() const { return II; }
|
|
|
|
Selector getSelector() const { return S; }
|
|
|
|
};
|
2008-06-24 03:30:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
2008-06-26 01:21:56 +04:00
|
|
|
template <> struct DenseMapInfo<ObjCSummaryKey> {
|
|
|
|
static inline ObjCSummaryKey getEmptyKey() {
|
|
|
|
return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getEmptyKey(),
|
|
|
|
DenseMapInfo<Selector>::getEmptyKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ObjCSummaryKey getTombstoneKey() {
|
|
|
|
return ObjCSummaryKey(DenseMapInfo<IdentifierInfo*>::getTombstoneKey(),
|
|
|
|
DenseMapInfo<Selector>::getTombstoneKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getHashValue(const ObjCSummaryKey &V) {
|
|
|
|
return (DenseMapInfo<IdentifierInfo*>::getHashValue(V.getIdentifier())
|
|
|
|
& 0x88888888)
|
|
|
|
| (DenseMapInfo<Selector>::getHashValue(V.getSelector())
|
|
|
|
& 0x55555555);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isEqual(const ObjCSummaryKey& LHS, const ObjCSummaryKey& RHS) {
|
|
|
|
return DenseMapInfo<IdentifierInfo*>::isEqual(LHS.getIdentifier(),
|
|
|
|
RHS.getIdentifier()) &&
|
|
|
|
DenseMapInfo<Selector>::isEqual(LHS.getSelector(),
|
|
|
|
RHS.getSelector());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isPod() {
|
|
|
|
return DenseMapInfo<ObjCInterfaceDecl*>::isPod() &&
|
|
|
|
DenseMapInfo<Selector>::isPod();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end llvm namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class VISIBILITY_HIDDEN ObjCSummaryCache {
|
|
|
|
typedef llvm::DenseMap<ObjCSummaryKey, RetainSummary*> MapTy;
|
|
|
|
MapTy M;
|
|
|
|
public:
|
|
|
|
ObjCSummaryCache() {}
|
|
|
|
|
|
|
|
typedef MapTy::iterator iterator;
|
|
|
|
|
|
|
|
iterator find(ObjCInterfaceDecl* D, Selector S) {
|
|
|
|
|
|
|
|
// Do a lookup with the (D,S) pair. If we find a match return
|
|
|
|
// the iterator.
|
|
|
|
ObjCSummaryKey K(D, S);
|
|
|
|
MapTy::iterator I = M.find(K);
|
|
|
|
|
|
|
|
if (I != M.end() || !D)
|
|
|
|
return I;
|
|
|
|
|
|
|
|
// Walk the super chain. If we find a hit with a parent, we'll end
|
|
|
|
// up returning that summary. We actually allow that key (null,S), as
|
|
|
|
// we cache summaries for the null ObjCInterfaceDecl* to allow us to
|
|
|
|
// generate initial summaries without having to worry about NSObject
|
|
|
|
// being declared.
|
|
|
|
// FIXME: We may change this at some point.
|
|
|
|
for (ObjCInterfaceDecl* C=D->getSuperClass() ;; C=C->getSuperClass()) {
|
|
|
|
if ((I = M.find(ObjCSummaryKey(C, S))) != M.end())
|
|
|
|
break;
|
2008-06-24 03:30:29 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
if (!C)
|
|
|
|
return I;
|
2008-06-24 03:30:29 +04:00
|
|
|
}
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
// Cache the summary with original key to make the next lookup faster
|
|
|
|
// and return the iterator.
|
|
|
|
M[K] = I->second;
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
iterator find(Expr* Receiver, Selector S) {
|
|
|
|
return find(getReceiverDecl(Receiver), S);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator find(IdentifierInfo* II, Selector S) {
|
|
|
|
// FIXME: Class method lookup. Right now we dont' have a good way
|
|
|
|
// of going between IdentifierInfo* and the class hierarchy.
|
|
|
|
iterator I = M.find(ObjCSummaryKey(II, S));
|
|
|
|
return I == M.end() ? M.find(ObjCSummaryKey(S)) : I;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCInterfaceDecl* getReceiverDecl(Expr* E) {
|
2008-06-24 03:30:29 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
const PointerType* PT = E->getType()->getAsPointerType();
|
|
|
|
if (!PT) return 0;
|
|
|
|
|
|
|
|
ObjCInterfaceType* OI = dyn_cast<ObjCInterfaceType>(PT->getPointeeType());
|
|
|
|
if (!OI) return 0;
|
2008-06-24 03:30:29 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
return OI ? OI->getDecl() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() { return M.end(); }
|
|
|
|
|
|
|
|
RetainSummary*& operator[](ObjCMessageExpr* ME) {
|
|
|
|
|
|
|
|
Selector S = ME->getSelector();
|
|
|
|
|
|
|
|
if (Expr* Receiver = ME->getReceiver()) {
|
|
|
|
ObjCInterfaceDecl* OD = getReceiverDecl(Receiver);
|
|
|
|
return OD ? M[ObjCSummaryKey(OD->getIdentifier(), S)] : M[S];
|
2008-06-24 03:30:29 +04:00
|
|
|
}
|
2008-06-26 01:21:56 +04:00
|
|
|
|
|
|
|
return M[ObjCSummaryKey(ME->getClassName(), S)];
|
|
|
|
}
|
|
|
|
|
|
|
|
RetainSummary*& operator[](ObjCSummaryKey K) {
|
|
|
|
return M[K];
|
|
|
|
}
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
RetainSummary*& operator[](Selector S) {
|
|
|
|
return M[ ObjCSummaryKey(S) ];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Data structures for managing collections of summaries.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-06-24 03:30:29 +04:00
|
|
|
namespace {
|
2008-06-26 01:21:56 +04:00
|
|
|
class VISIBILITY_HIDDEN RetainSummaryManager {
|
2008-05-06 02:11:16 +04:00
|
|
|
|
|
|
|
//==-----------------------------------------------------------------==//
|
|
|
|
// Typedefs.
|
|
|
|
//==-----------------------------------------------------------------==//
|
|
|
|
|
|
|
|
typedef llvm::FoldingSet<llvm::FoldingSetNodeWrapper<ArgEffects> >
|
|
|
|
ArgEffectsSetTy;
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
typedef llvm::FoldingSet<RetainSummary>
|
|
|
|
SummarySetTy;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<FunctionDecl*, RetainSummary*>
|
|
|
|
FuncSummariesTy;
|
|
|
|
|
2008-06-24 03:30:29 +04:00
|
|
|
typedef ObjCSummaryCache ObjCMethodSummariesTy;
|
2008-05-06 02:11:16 +04:00
|
|
|
|
|
|
|
//==-----------------------------------------------------------------==//
|
|
|
|
// Data.
|
|
|
|
//==-----------------------------------------------------------------==//
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// Ctx - The ASTContext object for the analyzed ASTs.
|
2008-04-29 09:33:51 +04:00
|
|
|
ASTContext& Ctx;
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// NSWindowII - An IdentifierInfo* representing the identifier "NSWindow."
|
|
|
|
IdentifierInfo* NSWindowII;
|
2008-07-01 21:21:27 +04:00
|
|
|
|
|
|
|
/// NSPanelII - An IdentifierInfo* representing the identifier "NSPanel."
|
|
|
|
IdentifierInfo* NSPanelII;
|
2008-06-26 01:21:56 +04:00
|
|
|
|
2008-07-09 22:11:16 +04:00
|
|
|
/// CFDictionaryCreateII - An IdentifierInfo* representing the indentifier
|
|
|
|
/// "CFDictionaryCreate".
|
|
|
|
IdentifierInfo* CFDictionaryCreateII;
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// GCEnabled - Records whether or not the analyzed code runs in GC mode.
|
2008-04-29 09:33:51 +04:00
|
|
|
const bool GCEnabled;
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// SummarySet - A FoldingSet of uniqued summaries.
|
2008-04-11 02:58:08 +04:00
|
|
|
SummarySetTy SummarySet;
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// FuncSummaries - A map from FunctionDecls to summaries.
|
2008-05-06 02:11:16 +04:00
|
|
|
FuncSummariesTy FuncSummaries;
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// ObjCClassMethodSummaries - A map from selectors (for instance methods)
|
|
|
|
/// to summaries.
|
2008-06-24 02:21:20 +04:00
|
|
|
ObjCMethodSummariesTy ObjCClassMethodSummaries;
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// ObjCMethodSummaries - A map from selectors to summaries.
|
2008-06-24 02:21:20 +04:00
|
|
|
ObjCMethodSummariesTy ObjCMethodSummaries;
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// ArgEffectsSet - A FoldingSet of uniqued ArgEffects.
|
2008-05-06 02:11:16 +04:00
|
|
|
ArgEffectsSetTy ArgEffectsSet;
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// BPAlloc - A BumpPtrAllocator used for allocating summaries, ArgEffects,
|
|
|
|
/// and all other data used by the checker.
|
2008-05-06 02:11:16 +04:00
|
|
|
llvm::BumpPtrAllocator BPAlloc;
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// ScratchArgs - A holding buffer for construct ArgEffects.
|
2008-05-06 02:11:16 +04:00
|
|
|
ArgEffects ScratchArgs;
|
|
|
|
|
2008-05-06 22:11:36 +04:00
|
|
|
RetainSummary* StopSummary;
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
//==-----------------------------------------------------------------==//
|
|
|
|
// Methods.
|
|
|
|
//==-----------------------------------------------------------------==//
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// getArgEffects - Returns a persistent ArgEffects object based on the
|
|
|
|
/// data in ScratchArgs.
|
2008-03-12 04:21:45 +03:00
|
|
|
ArgEffects* getArgEffects();
|
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
enum UnaryFuncKind { cfretain, cfrelease, cfmakecollectable };
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary* getUnarySummary(FunctionDecl* FD, UnaryFuncKind func);
|
2008-04-29 09:33:51 +04:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary* getNSSummary(FunctionDecl* FD, const char* FName);
|
|
|
|
RetainSummary* getCFSummary(FunctionDecl* FD, const char* FName);
|
2008-07-15 20:50:12 +04:00
|
|
|
RetainSummary* getCGSummary(FunctionDecl* FD, const char* FName);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary* getCFSummaryCreateRule(FunctionDecl* FD);
|
|
|
|
RetainSummary* getCFSummaryGetRule(FunctionDecl* FD);
|
2008-07-15 20:50:12 +04:00
|
|
|
RetainSummary* getCFCreateGetRuleSummary(FunctionDecl* FD, const char* FName);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-06 06:26:56 +04:00
|
|
|
RetainSummary* getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
|
2008-05-06 19:44:25 +04:00
|
|
|
ArgEffect ReceiverEff = DoNothing,
|
2008-07-18 21:24:20 +04:00
|
|
|
ArgEffect DefaultEff = MayEscape,
|
|
|
|
bool isEndPath = false);
|
2008-05-06 19:44:25 +04:00
|
|
|
|
2008-05-06 04:30:21 +04:00
|
|
|
|
2008-05-06 06:26:56 +04:00
|
|
|
RetainSummary* getPersistentSummary(RetEffect RE,
|
2008-05-06 19:44:25 +04:00
|
|
|
ArgEffect ReceiverEff = DoNothing,
|
2008-05-22 21:31:13 +04:00
|
|
|
ArgEffect DefaultEff = MayEscape) {
|
2008-05-06 19:44:25 +04:00
|
|
|
return getPersistentSummary(getArgEffects(), RE, ReceiverEff, DefaultEff);
|
2008-05-06 04:30:21 +04:00
|
|
|
}
|
2008-05-06 03:55:01 +04:00
|
|
|
|
2008-05-06 22:11:36 +04:00
|
|
|
|
2008-05-06 19:44:25 +04:00
|
|
|
RetainSummary* getPersistentStopSummary() {
|
2008-05-06 22:11:36 +04:00
|
|
|
if (StopSummary)
|
|
|
|
return StopSummary;
|
|
|
|
|
|
|
|
StopSummary = getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
StopTracking, StopTracking);
|
|
|
|
|
|
|
|
return StopSummary;
|
2008-05-06 19:44:25 +04:00
|
|
|
}
|
2008-05-06 08:20:12 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
RetainSummary* getInitMethodSummary(ObjCMessageExpr* ME);
|
2008-05-06 03:55:01 +04:00
|
|
|
|
2008-06-24 02:21:20 +04:00
|
|
|
void InitializeClassMethodSummaries();
|
|
|
|
void InitializeMethodSummaries();
|
2008-07-18 21:24:20 +04:00
|
|
|
|
|
|
|
void addClsMethSummary(IdentifierInfo* ClsII, Selector S,
|
|
|
|
RetainSummary* Summ) {
|
|
|
|
ObjCClassMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
|
|
|
|
}
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
void addNSObjectClsMethSummary(Selector S, RetainSummary *Summ) {
|
|
|
|
ObjCClassMethodSummaries[S] = Summ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addNSObjectMethSummary(Selector S, RetainSummary *Summ) {
|
|
|
|
ObjCMethodSummaries[S] = Summ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addNSWindowMethSummary(Selector S, RetainSummary *Summ) {
|
|
|
|
ObjCMethodSummaries[ObjCSummaryKey(NSWindowII, S)] = Summ;
|
|
|
|
}
|
|
|
|
|
2008-07-01 21:21:27 +04:00
|
|
|
void addNSPanelMethSummary(Selector S, RetainSummary *Summ) {
|
|
|
|
ObjCMethodSummaries[ObjCSummaryKey(NSPanelII, S)] = Summ;
|
|
|
|
}
|
|
|
|
|
2008-08-12 22:48:50 +04:00
|
|
|
void addInstMethSummary(const char* Cls, RetainSummary* Summ, va_list argp) {
|
2008-08-12 22:30:56 +04:00
|
|
|
|
|
|
|
IdentifierInfo* ClsII = &Ctx.Idents.get(Cls);
|
|
|
|
llvm::SmallVector<IdentifierInfo*, 10> II;
|
2008-07-18 21:24:20 +04:00
|
|
|
|
2008-08-12 22:30:56 +04:00
|
|
|
while (const char* s = va_arg(argp, const char*))
|
|
|
|
II.push_back(&Ctx.Idents.get(s));
|
|
|
|
|
|
|
|
Selector S = Ctx.Selectors.getSelector(II.size(), &II[0]);
|
2008-07-18 21:24:20 +04:00
|
|
|
ObjCMethodSummaries[ObjCSummaryKey(ClsII, S)] = Summ;
|
|
|
|
}
|
2008-08-12 22:48:50 +04:00
|
|
|
|
|
|
|
void addInstMethSummary(const char* Cls, RetainSummary* Summ, ...) {
|
|
|
|
va_list argp;
|
|
|
|
va_start(argp, Summ);
|
|
|
|
addInstMethSummary(Cls, Summ, argp);
|
|
|
|
va_end(argp);
|
|
|
|
}
|
2008-08-12 22:30:56 +04:00
|
|
|
|
|
|
|
void addPanicSummary(const char* Cls, ...) {
|
|
|
|
RetainSummary* Summ = getPersistentSummary(0, RetEffect::MakeNoRet(),
|
|
|
|
DoNothing, DoNothing, true);
|
|
|
|
va_list argp;
|
|
|
|
va_start (argp, Cls);
|
2008-08-12 22:48:50 +04:00
|
|
|
addInstMethSummary(Cls, Summ, argp);
|
2008-08-12 22:30:56 +04:00
|
|
|
va_end(argp);
|
|
|
|
}
|
2008-07-18 21:24:20 +04:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
public:
|
2008-05-06 02:11:16 +04:00
|
|
|
|
|
|
|
RetainSummaryManager(ASTContext& ctx, bool gcenabled)
|
2008-07-01 21:21:27 +04:00
|
|
|
: Ctx(ctx),
|
|
|
|
NSWindowII(&ctx.Idents.get("NSWindow")),
|
|
|
|
NSPanelII(&ctx.Idents.get("NSPanel")),
|
2008-07-09 22:11:16 +04:00
|
|
|
CFDictionaryCreateII(&ctx.Idents.get("CFDictionaryCreate")),
|
2008-06-26 01:21:56 +04:00
|
|
|
GCEnabled(gcenabled), StopSummary(0) {
|
|
|
|
|
|
|
|
InitializeClassMethodSummaries();
|
|
|
|
InitializeMethodSummaries();
|
|
|
|
}
|
2008-04-29 09:33:51 +04:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
~RetainSummaryManager();
|
|
|
|
|
2008-06-24 07:56:45 +04:00
|
|
|
RetainSummary* getSummary(FunctionDecl* FD);
|
2008-06-26 01:21:56 +04:00
|
|
|
RetainSummary* getMethodSummary(ObjCMessageExpr* ME, ObjCInterfaceDecl* ID);
|
2008-06-24 02:21:20 +04:00
|
|
|
RetainSummary* getClassMethodSummary(IdentifierInfo* ClsName, Selector S);
|
2008-05-06 08:20:12 +04:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
bool isGCEnabled() const { return GCEnabled; }
|
2008-03-11 09:39:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Implementation of checker data structures.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummaryManager::~RetainSummaryManager() {
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
// FIXME: The ArgEffects could eventually be allocated from BPAlloc,
|
|
|
|
// mitigating the need to do explicit cleanup of the
|
|
|
|
// Argument-Effect summaries.
|
2008-03-06 03:08:09 +03:00
|
|
|
|
2008-05-06 03:55:01 +04:00
|
|
|
for (ArgEffectsSetTy::iterator I = ArgEffectsSet.begin(),
|
|
|
|
E = ArgEffectsSet.end(); I!=E; ++I)
|
2008-03-11 09:39:11 +03:00
|
|
|
I->getValue().~ArgEffects();
|
2008-03-06 03:08:09 +03:00
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
ArgEffects* RetainSummaryManager::getArgEffects() {
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
if (ScratchArgs.empty())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Compute a profile for a non-empty ScratchArgs.
|
2008-03-12 04:21:45 +03:00
|
|
|
llvm::FoldingSetNodeID profile;
|
|
|
|
profile.Add(ScratchArgs);
|
|
|
|
void* InsertPos;
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
// Look up the uniqued copy, or create a new one.
|
2008-03-12 04:21:45 +03:00
|
|
|
llvm::FoldingSetNodeWrapper<ArgEffects>* E =
|
2008-05-06 02:11:16 +04:00
|
|
|
ArgEffectsSet.FindNodeOrInsertPos(profile, InsertPos);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
if (E) {
|
2008-03-12 04:21:45 +03:00
|
|
|
ScratchArgs.clear();
|
|
|
|
return &E->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
E = (llvm::FoldingSetNodeWrapper<ArgEffects>*)
|
2008-06-26 01:21:56 +04:00
|
|
|
BPAlloc.Allocate<llvm::FoldingSetNodeWrapper<ArgEffects> >();
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
new (E) llvm::FoldingSetNodeWrapper<ArgEffects>(ScratchArgs);
|
2008-05-06 02:11:16 +04:00
|
|
|
ArgEffectsSet.InsertNode(E, InsertPos);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
ScratchArgs.clear();
|
|
|
|
return &E->getValue();
|
|
|
|
}
|
|
|
|
|
2008-05-06 06:26:56 +04:00
|
|
|
RetainSummary*
|
|
|
|
RetainSummaryManager::getPersistentSummary(ArgEffects* AE, RetEffect RetEff,
|
2008-05-06 19:44:25 +04:00
|
|
|
ArgEffect ReceiverEff,
|
2008-07-18 21:24:20 +04:00
|
|
|
ArgEffect DefaultEff,
|
|
|
|
bool isEndPath) {
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
// Generate a profile for the summary.
|
2008-03-12 04:21:45 +03:00
|
|
|
llvm::FoldingSetNodeID profile;
|
2008-07-18 21:39:56 +04:00
|
|
|
RetainSummary::Profile(profile, AE, RetEff, DefaultEff, ReceiverEff,
|
|
|
|
isEndPath);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
// Look up the uniqued summary, or create one if it doesn't exist.
|
|
|
|
void* InsertPos;
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary* Summ = SummarySet.FindNodeOrInsertPos(profile, InsertPos);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
if (Summ)
|
|
|
|
return Summ;
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
// Create the summary and return it.
|
2008-05-06 02:11:16 +04:00
|
|
|
Summ = (RetainSummary*) BPAlloc.Allocate<RetainSummary>();
|
2008-07-18 21:24:20 +04:00
|
|
|
new (Summ) RetainSummary(AE, RetEff, DefaultEff, ReceiverEff, isEndPath);
|
2008-03-12 04:21:45 +03:00
|
|
|
SummarySet.InsertNode(Summ, InsertPos);
|
|
|
|
|
|
|
|
return Summ;
|
|
|
|
}
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Summary creation for functions (largely uses of Core Foundation).
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-06-24 07:56:45 +04:00
|
|
|
RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD) {
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
SourceLocation Loc = FD->getLocation();
|
|
|
|
|
|
|
|
if (!Loc.isFileID())
|
|
|
|
return NULL;
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
// Look up a summary in our cache of FunctionDecls -> Summaries.
|
2008-05-06 02:11:16 +04:00
|
|
|
FuncSummariesTy::iterator I = FuncSummaries.find(FD);
|
2008-04-24 21:22:33 +04:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
if (I != FuncSummaries.end())
|
2008-04-24 21:22:33 +04:00
|
|
|
return I->second;
|
|
|
|
|
|
|
|
// No summary. Generate one.
|
2008-03-12 04:21:45 +03:00
|
|
|
const char* FName = FD->getIdentifier()->getName();
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary *S = 0;
|
2008-05-05 20:51:50 +04:00
|
|
|
|
2008-05-08 00:06:41 +04:00
|
|
|
FunctionType* FT = dyn_cast<FunctionType>(FD->getType());
|
2008-07-15 20:50:12 +04:00
|
|
|
|
|
|
|
do {
|
|
|
|
if (FT) {
|
|
|
|
|
|
|
|
QualType T = FT->getResultType();
|
|
|
|
|
|
|
|
if (isCFRefType(T)) {
|
|
|
|
S = getCFSummary(FD, FName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isCGRefType(T)) {
|
|
|
|
S = getCGSummary(FD, FName );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FName[0] == 'C' && FName[1] == 'F')
|
|
|
|
S = getCFSummary(FD, FName);
|
|
|
|
else if (FName[0] == 'N' && FName[1] == 'S')
|
|
|
|
S = getNSSummary(FD, FName);
|
|
|
|
}
|
|
|
|
while (0);
|
2008-04-24 21:22:33 +04:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
FuncSummaries[FD] = S;
|
2008-05-05 20:51:50 +04:00
|
|
|
return S;
|
2008-03-06 03:08:09 +03:00
|
|
|
}
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary* RetainSummaryManager::getNSSummary(FunctionDecl* FD,
|
2008-05-06 03:55:01 +04:00
|
|
|
const char* FName) {
|
2008-05-05 20:51:50 +04:00
|
|
|
FName += 2;
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
if (strcmp(FName, "MakeCollectable") == 0)
|
|
|
|
return getUnarySummary(FD, cfmakecollectable);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-15 20:50:12 +04:00
|
|
|
|
|
|
|
static bool isRetain(FunctionDecl* FD, const char* FName) {
|
2008-07-15 21:43:41 +04:00
|
|
|
const char* loc = strstr(FName, "Retain");
|
|
|
|
return loc && loc[sizeof("Retain")-1] == '\0';
|
2008-07-15 20:50:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isRelease(FunctionDecl* FD, const char* FName) {
|
2008-07-15 21:43:41 +04:00
|
|
|
const char* loc = strstr(FName, "Release");
|
|
|
|
return loc && loc[sizeof("Release")-1] == '\0';
|
2008-07-15 20:50:12 +04:00
|
|
|
}
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary* RetainSummaryManager::getCFSummary(FunctionDecl* FD,
|
2008-05-06 03:55:01 +04:00
|
|
|
const char* FName) {
|
2008-05-05 20:51:50 +04:00
|
|
|
|
2008-05-08 00:06:41 +04:00
|
|
|
if (FName[0] == 'C' && FName[1] == 'F')
|
|
|
|
FName += 2;
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-07-15 20:50:12 +04:00
|
|
|
if (isRetain(FD, FName))
|
2008-05-05 20:51:50 +04:00
|
|
|
return getUnarySummary(FD, cfretain);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-07-15 20:50:12 +04:00
|
|
|
if (isRelease(FD, FName))
|
2008-05-05 20:51:50 +04:00
|
|
|
return getUnarySummary(FD, cfrelease);
|
2008-07-09 22:11:16 +04:00
|
|
|
|
2008-04-29 09:33:51 +04:00
|
|
|
if (strcmp(FName, "MakeCollectable") == 0)
|
2008-05-05 20:51:50 +04:00
|
|
|
return getUnarySummary(FD, cfmakecollectable);
|
2008-07-15 20:50:12 +04:00
|
|
|
|
|
|
|
return getCFCreateGetRuleSummary(FD, FName);
|
|
|
|
}
|
|
|
|
|
|
|
|
RetainSummary* RetainSummaryManager::getCGSummary(FunctionDecl* FD,
|
|
|
|
const char* FName) {
|
|
|
|
|
|
|
|
if (FName[0] == 'C' && FName[1] == 'G')
|
|
|
|
FName += 2;
|
|
|
|
|
|
|
|
if (isRelease(FD, FName))
|
|
|
|
return getUnarySummary(FD, cfrelease);
|
|
|
|
|
|
|
|
if (isRetain(FD, FName))
|
|
|
|
return getUnarySummary(FD, cfretain);
|
|
|
|
|
|
|
|
return getCFCreateGetRuleSummary(FD, FName);
|
|
|
|
}
|
|
|
|
|
|
|
|
RetainSummary*
|
|
|
|
RetainSummaryManager::getCFCreateGetRuleSummary(FunctionDecl* FD,
|
|
|
|
const char* FName) {
|
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
if (strstr(FName, "Create") || strstr(FName, "Copy"))
|
|
|
|
return getCFSummaryCreateRule(FD);
|
2008-07-15 20:50:12 +04:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
if (strstr(FName, "Get"))
|
2008-05-05 20:51:50 +04:00
|
|
|
return getCFSummaryGetRule(FD);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
return 0;
|
2008-03-12 04:21:45 +03:00
|
|
|
}
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary*
|
|
|
|
RetainSummaryManager::getUnarySummary(FunctionDecl* FD, UnaryFuncKind func) {
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
FunctionTypeProto* FT =
|
|
|
|
dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
if (FT) {
|
|
|
|
|
|
|
|
if (FT->getNumArgs() != 1)
|
|
|
|
return 0;
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
TypedefType* ArgT = dyn_cast<TypedefType>(FT->getArgType(0).getTypePtr());
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
if (!ArgT)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!ArgT->isPointerType())
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
assert (ScratchArgs.empty());
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-04-29 09:33:51 +04:00
|
|
|
switch (func) {
|
|
|
|
case cfretain: {
|
|
|
|
ScratchArgs.push_back(std::make_pair(0, IncRef));
|
2008-05-22 21:31:13 +04:00
|
|
|
return getPersistentSummary(RetEffect::MakeAlias(0),
|
|
|
|
DoNothing, DoNothing);
|
2008-04-29 09:33:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
case cfrelease: {
|
|
|
|
ScratchArgs.push_back(std::make_pair(0, DecRef));
|
2008-05-22 21:31:13 +04:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(),
|
|
|
|
DoNothing, DoNothing);
|
2008-04-29 09:33:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
case cfmakecollectable: {
|
|
|
|
if (GCEnabled)
|
|
|
|
ScratchArgs.push_back(std::make_pair(0, DecRef));
|
|
|
|
|
2008-05-22 21:31:13 +04:00
|
|
|
return getPersistentSummary(RetEffect::MakeAlias(0),
|
|
|
|
DoNothing, DoNothing);
|
2008-04-29 09:33:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2008-05-05 20:51:50 +04:00
|
|
|
assert (false && "Not a supported unary function.");
|
2008-04-11 03:44:06 +04:00
|
|
|
}
|
2008-03-12 04:21:45 +03:00
|
|
|
}
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-08 00:06:41 +04:00
|
|
|
FunctionType* FT =
|
|
|
|
dyn_cast<FunctionType>(FD->getType().getTypePtr());
|
2008-05-05 20:51:50 +04:00
|
|
|
|
|
|
|
if (FT && !isCFRefType(FT->getResultType()))
|
2008-05-22 21:31:13 +04:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet());
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
assert (ScratchArgs.empty());
|
2008-07-09 22:11:16 +04:00
|
|
|
|
|
|
|
if (FD->getIdentifier() == CFDictionaryCreateII) {
|
|
|
|
ScratchArgs.push_back(std::make_pair(1, DoNothingByRef));
|
|
|
|
ScratchArgs.push_back(std::make_pair(2, DoNothingByRef));
|
|
|
|
}
|
|
|
|
|
2008-06-23 22:02:52 +04:00
|
|
|
return getPersistentSummary(RetEffect::MakeOwned(true));
|
2008-03-12 04:21:45 +03:00
|
|
|
}
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-05-08 00:06:41 +04:00
|
|
|
FunctionType* FT =
|
|
|
|
dyn_cast<FunctionType>(FD->getType().getTypePtr());
|
2008-04-12 00:11:19 +04:00
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
if (FT) {
|
|
|
|
QualType RetTy = FT->getResultType();
|
2008-04-12 00:11:19 +04:00
|
|
|
|
2008-05-05 20:51:50 +04:00
|
|
|
// FIXME: For now we assume that all pointer types returned are referenced
|
|
|
|
// counted. Since this is the "Get" rule, we assume non-ownership, which
|
|
|
|
// works fine for things that are not reference counted. We do this because
|
|
|
|
// some generic data structures return "void*". We need something better
|
|
|
|
// in the future.
|
|
|
|
|
|
|
|
if (!isCFRefType(RetTy) && !RetTy->isPointerType())
|
2008-05-22 21:31:13 +04:00
|
|
|
return getPersistentSummary(RetEffect::MakeNoRet(), DoNothing, DoNothing);
|
2008-05-05 20:51:50 +04:00
|
|
|
}
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
// FIXME: Add special-cases for functions that retain/release. For now
|
|
|
|
// just handle the default case.
|
|
|
|
|
2008-04-24 21:22:33 +04:00
|
|
|
assert (ScratchArgs.empty());
|
2008-05-22 21:31:13 +04:00
|
|
|
return getPersistentSummary(RetEffect::MakeNotOwned(), DoNothing, DoNothing);
|
2008-03-12 04:21:45 +03:00
|
|
|
}
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Summary creation for Selectors.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-06 19:44:25 +04:00
|
|
|
RetainSummary*
|
2008-06-26 01:21:56 +04:00
|
|
|
RetainSummaryManager::getInitMethodSummary(ObjCMessageExpr* ME) {
|
2008-05-06 03:55:01 +04:00
|
|
|
assert(ScratchArgs.empty());
|
|
|
|
|
|
|
|
RetainSummary* Summ =
|
2008-05-06 04:30:21 +04:00
|
|
|
getPersistentSummary(RetEffect::MakeReceiverAlias());
|
2008-05-06 03:55:01 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
ObjCMethodSummaries[ME] = Summ;
|
2008-05-06 03:55:01 +04:00
|
|
|
return Summ;
|
|
|
|
}
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
|
2008-05-06 19:44:25 +04:00
|
|
|
RetainSummary*
|
2008-06-26 01:21:56 +04:00
|
|
|
RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME,
|
|
|
|
ObjCInterfaceDecl* ID) {
|
2008-05-06 19:44:25 +04:00
|
|
|
|
|
|
|
Selector S = ME->getSelector();
|
2008-05-06 03:55:01 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
// Look up a summary in our summary cache.
|
|
|
|
ObjCMethodSummariesTy::iterator I = ObjCMethodSummaries.find(ID, S);
|
2008-05-06 03:55:01 +04:00
|
|
|
|
2008-06-24 02:21:20 +04:00
|
|
|
if (I != ObjCMethodSummaries.end())
|
2008-05-06 03:55:01 +04:00
|
|
|
return I->second;
|
2008-06-26 01:21:56 +04:00
|
|
|
|
2008-05-07 07:45:05 +04:00
|
|
|
if (!ME->getType()->isPointerType())
|
|
|
|
return 0;
|
|
|
|
|
2008-05-06 03:55:01 +04:00
|
|
|
// "initXXX": pass-through for receiver.
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-05-06 03:55:01 +04:00
|
|
|
const char* s = S.getIdentifierInfoForSlot(0)->getName();
|
2008-05-07 07:45:05 +04:00
|
|
|
assert (ScratchArgs.empty());
|
2008-05-06 10:09:09 +04:00
|
|
|
|
2008-06-02 21:14:13 +04:00
|
|
|
if (strncmp(s, "init", 4) == 0 || strncmp(s, "_init", 5) == 0)
|
2008-06-26 01:21:56 +04:00
|
|
|
return getInitMethodSummary(ME);
|
2008-05-06 19:44:25 +04:00
|
|
|
|
2008-05-07 07:45:05 +04:00
|
|
|
// "copyXXX", "createXXX", "newXXX": allocators.
|
2008-05-06 03:55:01 +04:00
|
|
|
|
2008-05-07 08:25:59 +04:00
|
|
|
if (!isNSType(ME->getReceiver()->getType()))
|
|
|
|
return 0;
|
|
|
|
|
2008-05-08 00:06:41 +04:00
|
|
|
if (CStrInCStrNoCase(s, "create") || CStrInCStrNoCase(s, "copy") ||
|
|
|
|
CStrInCStrNoCase(s, "new")) {
|
2008-05-06 19:44:25 +04:00
|
|
|
|
2008-05-07 07:45:05 +04:00
|
|
|
RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
|
2008-06-23 22:02:52 +04:00
|
|
|
: RetEffect::MakeOwned(true);
|
2008-05-06 19:44:25 +04:00
|
|
|
|
2008-05-07 07:45:05 +04:00
|
|
|
RetainSummary* Summ = getPersistentSummary(E);
|
2008-06-26 01:21:56 +04:00
|
|
|
ObjCMethodSummaries[ME] = Summ;
|
2008-05-07 07:45:05 +04:00
|
|
|
return Summ;
|
2008-05-06 19:44:25 +04:00
|
|
|
}
|
|
|
|
|
2008-05-06 03:55:01 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-05-07 01:26:51 +04:00
|
|
|
RetainSummary*
|
2008-06-24 02:21:20 +04:00
|
|
|
RetainSummaryManager::getClassMethodSummary(IdentifierInfo* ClsName,
|
|
|
|
Selector S) {
|
2008-05-07 01:26:51 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
// FIXME: Eventually we should properly do class method summaries, but
|
|
|
|
// it requires us being able to walk the type hierarchy. Unfortunately,
|
|
|
|
// we cannot do this with just an IdentifierInfo* for the class name.
|
|
|
|
|
2008-05-07 01:26:51 +04:00
|
|
|
// Look up a summary in our cache of Selectors -> Summaries.
|
2008-06-26 01:21:56 +04:00
|
|
|
ObjCMethodSummariesTy::iterator I = ObjCClassMethodSummaries.find(ClsName, S);
|
2008-05-07 01:26:51 +04:00
|
|
|
|
2008-06-24 02:21:20 +04:00
|
|
|
if (I != ObjCClassMethodSummaries.end())
|
2008-05-07 01:26:51 +04:00
|
|
|
return I->second;
|
|
|
|
|
2008-05-07 03:07:13 +04:00
|
|
|
return 0;
|
2008-05-07 01:26:51 +04:00
|
|
|
}
|
|
|
|
|
2008-06-24 02:21:20 +04:00
|
|
|
void RetainSummaryManager::InitializeClassMethodSummaries() {
|
2008-05-06 04:30:21 +04:00
|
|
|
|
|
|
|
assert (ScratchArgs.empty());
|
|
|
|
|
2008-06-23 22:02:52 +04:00
|
|
|
RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
|
|
|
|
: RetEffect::MakeOwned(true);
|
|
|
|
|
2008-05-06 04:30:21 +04:00
|
|
|
RetainSummary* Summ = getPersistentSummary(E);
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
// Create the summaries for "alloc", "new", and "allocWithZone:" for
|
|
|
|
// NSObject and its derivatives.
|
|
|
|
addNSObjectClsMethSummary(GetNullarySelector("alloc", Ctx), Summ);
|
|
|
|
addNSObjectClsMethSummary(GetNullarySelector("new", Ctx), Summ);
|
|
|
|
addNSObjectClsMethSummary(GetUnarySelector("allocWithZone", Ctx), Summ);
|
2008-07-18 21:24:20 +04:00
|
|
|
|
|
|
|
// Create the [NSAssertionHandler currentHander] summary.
|
2008-08-12 22:30:56 +04:00
|
|
|
addClsMethSummary(&Ctx.Idents.get("NSAssertionHandler"),
|
2008-07-18 22:14:26 +04:00
|
|
|
GetNullarySelector("currentHandler", Ctx),
|
2008-07-18 21:24:20 +04:00
|
|
|
getPersistentSummary(RetEffect::MakeNotOwned()));
|
2008-05-06 04:30:21 +04:00
|
|
|
}
|
|
|
|
|
2008-06-24 02:21:20 +04:00
|
|
|
void RetainSummaryManager::InitializeMethodSummaries() {
|
2008-05-06 04:38:54 +04:00
|
|
|
|
|
|
|
assert (ScratchArgs.empty());
|
|
|
|
|
2008-05-07 01:26:51 +04:00
|
|
|
// Create the "init" selector. It just acts as a pass-through for the
|
|
|
|
// receiver.
|
2008-07-01 21:21:27 +04:00
|
|
|
RetainSummary* InitSumm = getPersistentSummary(RetEffect::MakeReceiverAlias());
|
|
|
|
addNSObjectMethSummary(GetNullarySelector("init", Ctx), InitSumm);
|
2008-05-07 01:26:51 +04:00
|
|
|
|
|
|
|
// The next methods are allocators.
|
2008-06-23 22:02:52 +04:00
|
|
|
RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
|
|
|
|
: RetEffect::MakeOwned(true);
|
|
|
|
|
2008-07-01 21:21:27 +04:00
|
|
|
RetainSummary* Summ = getPersistentSummary(E);
|
2008-05-07 01:26:51 +04:00
|
|
|
|
|
|
|
// Create the "copy" selector.
|
2008-06-26 01:21:56 +04:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("copy", Ctx), Summ);
|
2008-05-06 04:38:54 +04:00
|
|
|
|
|
|
|
// Create the "mutableCopy" selector.
|
2008-06-26 01:21:56 +04:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("mutableCopy", Ctx), Summ);
|
2008-05-06 06:26:56 +04:00
|
|
|
|
|
|
|
// Create the "retain" selector.
|
|
|
|
E = RetEffect::MakeReceiverAlias();
|
|
|
|
Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : IncRef);
|
2008-06-26 01:21:56 +04:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("retain", Ctx), Summ);
|
2008-05-06 06:26:56 +04:00
|
|
|
|
|
|
|
// Create the "release" selector.
|
|
|
|
Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
|
2008-06-26 01:21:56 +04:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("release", Ctx), Summ);
|
2008-05-08 01:17:39 +04:00
|
|
|
|
|
|
|
// Create the "drain" selector.
|
|
|
|
Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : DecRef);
|
2008-06-26 01:21:56 +04:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("drain", Ctx), Summ);
|
2008-05-06 06:26:56 +04:00
|
|
|
|
|
|
|
// Create the "autorelease" selector.
|
2008-06-30 20:57:41 +04:00
|
|
|
Summ = getPersistentSummary(E, isGCEnabled() ? DoNothing : Autorelease);
|
2008-06-26 01:21:56 +04:00
|
|
|
addNSObjectMethSummary(GetNullarySelector("autorelease", Ctx), Summ);
|
|
|
|
|
2008-08-12 22:48:50 +04:00
|
|
|
// For NSWindow, allocated objects are (initially) self-owned.
|
|
|
|
RetainSummary *NSWindowSumm =
|
|
|
|
getPersistentSummary(RetEffect::MakeReceiverAlias(), SelfOwn);
|
|
|
|
|
|
|
|
addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
|
|
|
|
"styleMask", "backing", "defer", NULL);
|
|
|
|
|
|
|
|
addInstMethSummary("NSWindow", NSWindowSumm, "initWithContentRect",
|
|
|
|
"styleMask", "backing", "defer", "screen", NULL);
|
|
|
|
|
2008-07-01 21:21:27 +04:00
|
|
|
// For NSPanel (which subclasses NSWindow), allocated objects are not
|
|
|
|
// self-owned.
|
2008-08-12 22:48:50 +04:00
|
|
|
addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
|
|
|
|
"styleMask", "backing", "defer", NULL);
|
2008-07-01 21:21:27 +04:00
|
|
|
|
2008-08-12 22:48:50 +04:00
|
|
|
addInstMethSummary("NSPanel", InitSumm, "initWithContentRect",
|
|
|
|
"styleMask", "backing", "defer", "screen", NULL);
|
2008-06-26 01:21:56 +04:00
|
|
|
|
2008-07-18 21:24:20 +04:00
|
|
|
// Create NSAssertionHandler summaries.
|
2008-08-12 22:30:56 +04:00
|
|
|
addPanicSummary("NSAssertionHandler", "handleFailureInFunction", "file",
|
|
|
|
"lineNumber", "description", NULL);
|
|
|
|
|
|
|
|
addPanicSummary("NSAssertionHandler", "handleFailureInMethod", "object",
|
|
|
|
"file", "lineNumber", "description", NULL);
|
2008-05-06 04:38:54 +04:00
|
|
|
}
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-17 00:40:59 +04:00
|
|
|
// Reference-counting logic (typestate + counts).
|
2008-03-11 09:39:11 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
class VISIBILITY_HIDDEN RefVal {
|
2008-04-17 22:12:53 +04:00
|
|
|
public:
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
enum Kind {
|
|
|
|
Owned = 0, // Owning reference.
|
|
|
|
NotOwned, // Reference is not owned by still valid (not freed).
|
|
|
|
Released, // Object has been released.
|
|
|
|
ReturnedOwned, // Returned object passes ownership to caller.
|
|
|
|
ReturnedNotOwned, // Return object does not pass ownership to caller.
|
|
|
|
ErrorUseAfterRelease, // Object used after released.
|
|
|
|
ErrorReleaseNotOwned, // Release of an object that was not owned.
|
|
|
|
ErrorLeak // A memory leak due to excessive reference counts.
|
|
|
|
};
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
private:
|
|
|
|
|
|
|
|
Kind kind;
|
|
|
|
unsigned Cnt;
|
2008-06-26 01:21:56 +04:00
|
|
|
QualType T;
|
|
|
|
|
|
|
|
RefVal(Kind k, unsigned cnt, QualType t) : kind(k), Cnt(cnt), T(t) {}
|
|
|
|
RefVal(Kind k, unsigned cnt = 0) : kind(k), Cnt(cnt) {}
|
2008-03-11 20:48:22 +03:00
|
|
|
|
|
|
|
public:
|
2008-04-17 02:32:20 +04:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
Kind getKind() const { return kind; }
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
unsigned getCount() const { return Cnt; }
|
|
|
|
QualType getType() const { return T; }
|
2008-04-17 22:12:53 +04:00
|
|
|
|
|
|
|
// Useful predicates.
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-03-11 21:14:09 +03:00
|
|
|
static bool isError(Kind k) { return k >= ErrorUseAfterRelease; }
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
static bool isLeak(Kind k) { return k == ErrorLeak; }
|
|
|
|
|
2008-04-12 02:25:11 +04:00
|
|
|
bool isOwned() const {
|
|
|
|
return getKind() == Owned;
|
|
|
|
}
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
bool isNotOwned() const {
|
|
|
|
return getKind() == NotOwned;
|
|
|
|
}
|
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
bool isReturnedOwned() const {
|
|
|
|
return getKind() == ReturnedOwned;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isReturnedNotOwned() const {
|
|
|
|
return getKind() == ReturnedNotOwned;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isNonLeakError() const {
|
|
|
|
Kind k = getKind();
|
|
|
|
return isError(k) && !isLeak(k);
|
|
|
|
}
|
|
|
|
|
|
|
|
// State creation: normal state.
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
static RefVal makeOwned(QualType t, unsigned Count = 1) {
|
|
|
|
return RefVal(Owned, Count, t);
|
2008-04-11 03:09:18 +04:00
|
|
|
}
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
static RefVal makeNotOwned(QualType t, unsigned Count = 0) {
|
|
|
|
return RefVal(NotOwned, Count, t);
|
2008-04-11 03:09:18 +04:00
|
|
|
}
|
2008-04-17 22:12:53 +04:00
|
|
|
|
|
|
|
static RefVal makeReturnedOwned(unsigned Count) {
|
|
|
|
return RefVal(ReturnedOwned, Count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static RefVal makeReturnedNotOwned() {
|
|
|
|
return RefVal(ReturnedNotOwned);
|
|
|
|
}
|
|
|
|
|
|
|
|
// State creation: errors.
|
2008-06-26 01:21:56 +04:00
|
|
|
|
|
|
|
#if 0
|
2008-05-05 21:53:17 +04:00
|
|
|
static RefVal makeLeak(unsigned Count) { return RefVal(ErrorLeak, Count); }
|
2008-03-11 20:48:22 +03:00
|
|
|
static RefVal makeReleased() { return RefVal(Released); }
|
|
|
|
static RefVal makeUseAfterRelease() { return RefVal(ErrorUseAfterRelease); }
|
|
|
|
static RefVal makeReleaseNotOwned() { return RefVal(ErrorReleaseNotOwned); }
|
2008-06-26 01:21:56 +04:00
|
|
|
#endif
|
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
// Comparison, profiling, and pretty-printing.
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
bool operator==(const RefVal& X) const {
|
2008-06-26 01:21:56 +04:00
|
|
|
return kind == X.kind && Cnt == X.Cnt && T == X.T;
|
2008-04-17 22:12:53 +04:00
|
|
|
}
|
2008-03-11 22:44:10 +03:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
RefVal operator-(size_t i) const {
|
|
|
|
return RefVal(getKind(), getCount() - i, getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
RefVal operator+(size_t i) const {
|
|
|
|
return RefVal(getKind(), getCount() + i, getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
RefVal operator^(Kind k) const {
|
|
|
|
return RefVal(k, getCount(), getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
void Profile(llvm::FoldingSetNodeID& ID) const {
|
|
|
|
ID.AddInteger((unsigned) kind);
|
|
|
|
ID.AddInteger(Cnt);
|
2008-06-26 01:21:56 +04:00
|
|
|
ID.Add(T);
|
2008-04-17 22:12:53 +04:00
|
|
|
}
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
void print(std::ostream& Out) const;
|
2008-03-11 20:48:22 +03:00
|
|
|
};
|
2008-03-11 22:44:10 +03:00
|
|
|
|
|
|
|
void RefVal::print(std::ostream& Out) const {
|
2008-06-26 01:21:56 +04:00
|
|
|
if (!T.isNull())
|
|
|
|
Out << "Tracked Type:" << T.getAsString() << '\n';
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
switch (getKind()) {
|
|
|
|
default: assert(false);
|
2008-04-11 03:09:18 +04:00
|
|
|
case Owned: {
|
|
|
|
Out << "Owned";
|
|
|
|
unsigned cnt = getCount();
|
|
|
|
if (cnt) Out << " (+ " << cnt << ")";
|
2008-03-11 22:44:10 +03:00
|
|
|
break;
|
2008-04-11 03:09:18 +04:00
|
|
|
}
|
2008-03-11 22:44:10 +03:00
|
|
|
|
2008-04-11 03:09:18 +04:00
|
|
|
case NotOwned: {
|
2008-04-17 22:12:53 +04:00
|
|
|
Out << "NotOwned";
|
|
|
|
unsigned cnt = getCount();
|
|
|
|
if (cnt) Out << " (+ " << cnt << ")";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ReturnedOwned: {
|
|
|
|
Out << "ReturnedOwned";
|
2008-04-11 03:09:18 +04:00
|
|
|
unsigned cnt = getCount();
|
|
|
|
if (cnt) Out << " (+ " << cnt << ")";
|
2008-03-11 22:44:10 +03:00
|
|
|
break;
|
2008-04-11 03:09:18 +04:00
|
|
|
}
|
2008-03-11 22:44:10 +03:00
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
case ReturnedNotOwned: {
|
|
|
|
Out << "ReturnedNotOwned";
|
|
|
|
unsigned cnt = getCount();
|
|
|
|
if (cnt) Out << " (+ " << cnt << ")";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
case Released:
|
|
|
|
Out << "Released";
|
|
|
|
break;
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
case ErrorLeak:
|
|
|
|
Out << "Leaked";
|
|
|
|
break;
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
case ErrorUseAfterRelease:
|
|
|
|
Out << "Use-After-Release [ERROR]";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ErrorReleaseNotOwned:
|
|
|
|
Out << "Release of Not-Owned [ERROR]";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-11 20:48:22 +03:00
|
|
|
|
2008-04-17 00:40:59 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Transfer functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
class VISIBILITY_HIDDEN CFRefCount : public GRSimpleVals {
|
2008-04-18 07:39:05 +04:00
|
|
|
public:
|
2008-06-26 01:21:56 +04:00
|
|
|
// Type definitions.
|
2008-03-11 20:48:22 +03:00
|
|
|
typedef llvm::ImmutableMap<SymbolID, RefVal> RefBindings;
|
2008-06-26 01:21:56 +04:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
typedef RefBindings::Factory RefBFactoryTy;
|
2008-03-11 21:14:09 +03:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
typedef llvm::DenseMap<GRExprEngine::NodeTy*,std::pair<Expr*, SymbolID> >
|
|
|
|
ReleasesNotOwnedTy;
|
2008-03-11 21:14:09 +03:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
typedef ReleasesNotOwnedTy UseAfterReleasesTy;
|
|
|
|
|
|
|
|
typedef llvm::DenseMap<GRExprEngine::NodeTy*, std::vector<SymbolID>*>
|
|
|
|
LeaksTy;
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
class BindingsPrinter : public ValueState::CheckerStatePrinter {
|
|
|
|
public:
|
|
|
|
virtual void PrintCheckerState(std::ostream& Out, void* State,
|
|
|
|
const char* nl, const char* sep);
|
|
|
|
};
|
2008-04-18 07:39:05 +04:00
|
|
|
|
|
|
|
private:
|
2008-05-06 02:11:16 +04:00
|
|
|
RetainSummaryManager Summaries;
|
|
|
|
const LangOptions& LOpts;
|
2008-08-12 22:30:56 +04:00
|
|
|
RefBFactoryTy RefBFactory;
|
|
|
|
UseAfterReleasesTy UseAfterReleases;
|
|
|
|
ReleasesNotOwnedTy ReleasesNotOwned;
|
|
|
|
LeaksTy Leaks;
|
|
|
|
BindingsPrinter Printer;
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
public:
|
|
|
|
|
2008-07-11 02:03:41 +04:00
|
|
|
static RefBindings GetRefBindings(const ValueState& StImpl) {
|
|
|
|
return RefBindings((const RefBindings::TreeTy*) StImpl.CheckerState);
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
private:
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
static void SetRefBindings(ValueState& StImpl, RefBindings B) {
|
|
|
|
StImpl.CheckerState = B.getRoot();
|
|
|
|
}
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
RefBindings Remove(RefBindings B, SymbolID sym) {
|
|
|
|
return RefBFactory.Remove(B, sym);
|
|
|
|
}
|
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
RefBindings Update(RefBindings B, SymbolID sym, RefVal V, ArgEffect E,
|
2008-04-16 08:28:53 +04:00
|
|
|
RefVal::Kind& hasErr);
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
void ProcessNonLeakError(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* NodeExpr, Expr* ErrorExpr,
|
|
|
|
ExplodedNode<ValueState>* Pred,
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St,
|
2008-04-18 07:39:05 +04:00
|
|
|
RefVal::Kind hasErr, SymbolID Sym);
|
2008-04-17 02:32:20 +04:00
|
|
|
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* HandleSymbolDeath(ValueStateManager& VMgr,
|
|
|
|
const ValueState* St,
|
|
|
|
SymbolID sid, RefVal V, bool& hasLeak);
|
2008-04-17 02:32:20 +04:00
|
|
|
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* NukeBinding(ValueStateManager& VMgr, const ValueState* St,
|
|
|
|
SymbolID sid);
|
2008-03-11 09:39:11 +03:00
|
|
|
|
|
|
|
public:
|
2008-04-17 00:40:59 +04:00
|
|
|
|
2008-07-22 20:21:24 +04:00
|
|
|
CFRefCount(ASTContext& Ctx, bool gcenabled, const LangOptions& lopts)
|
2008-04-29 09:33:51 +04:00
|
|
|
: Summaries(Ctx, gcenabled),
|
2008-08-12 22:30:56 +04:00
|
|
|
LOpts(lopts) {}
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
virtual ~CFRefCount() {
|
|
|
|
for (LeaksTy::iterator I = Leaks.begin(), E = Leaks.end(); I!=E; ++I)
|
|
|
|
delete I->second;
|
|
|
|
}
|
2008-04-10 03:49:11 +04:00
|
|
|
|
|
|
|
virtual void RegisterChecks(GRExprEngine& Eng);
|
2008-03-11 22:44:10 +03:00
|
|
|
|
|
|
|
virtual ValueState::CheckerStatePrinter* getCheckerStatePrinter() {
|
|
|
|
return &Printer;
|
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
bool isGCEnabled() const { return Summaries.isGCEnabled(); }
|
2008-05-01 03:47:44 +04:00
|
|
|
const LangOptions& getLangOptions() const { return LOpts; }
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
// Calls.
|
2008-05-06 02:11:16 +04:00
|
|
|
|
|
|
|
void EvalSummary(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* Ex,
|
|
|
|
Expr* Receiver,
|
|
|
|
RetainSummary* Summ,
|
This patch is motivated by numerous strict-aliasing warnings when compiling
clang as a Release build.
The big change is that all AST nodes (subclasses of Stmt) whose children are
Expr* store their children as Stmt* or arrays of Stmt*. This is to remove
strict-aliasing warnings when using StmtIterator. None of the interfaces of any
of the classes have changed (except those with arg_iterators, see below), as the
accessor methods introduce the needed casts (via cast<>). While this extra
casting may seem cumbersome, it actually adds some important sanity checks
throughout the codebase, as clients using StmtIterator can potentially overwrite
children that are expected to be Expr* with Stmt* (that aren't Expr*). The casts
provide extra sanity checks that are operational in debug builds to catch
invariant violations such as these.
For classes that have arg_iterators (e.g., CallExpr), the definition of
arg_iterator has been replaced. Instead of it being Expr**, it is an actual
class (called ExprIterator) that wraps a Stmt**, and provides the necessary
operators for iteration. The nice thing about this class is that it also uses
cast<> to type-checking, which introduces extra sanity checks throughout the
codebase that are useful for debugging.
A few of the CodeGen functions that use arg_iterator (especially from
OverloadExpr) have been modified to take begin and end iterators instead of a
base Expr** and the number of arguments. This matches more with the abstraction
of iteration. This still needs to be cleaned up a little bit, as clients expect
that ExprIterator is a RandomAccessIterator (which we may or may not wish to
allow for efficiency of representation).
This is a fairly large patch. It passes the tests (except CodeGen/bitfield.c,
which was already broken) on both a Debug and Release build, but it should
obviously be reviewed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52378 91177308-0d34-0410-b5e6-96231b3b80d8
2008-06-17 06:43:46 +04:00
|
|
|
ExprIterator arg_beg, ExprIterator arg_end,
|
2008-05-06 02:11:16 +04:00
|
|
|
ExplodedNode<ValueState>* Pred);
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
virtual void EvalCall(ExplodedNodeSet<ValueState>& Dst,
|
2008-03-13 00:06:49 +03:00
|
|
|
GRExprEngine& Eng,
|
2008-03-11 09:39:11 +03:00
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
2008-04-24 00:12:28 +04:00
|
|
|
CallExpr* CE, RVal L,
|
2008-03-11 09:39:11 +03:00
|
|
|
ExplodedNode<ValueState>* Pred);
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-04-16 03:44:31 +04:00
|
|
|
virtual void EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ObjCMessageExpr* ME,
|
|
|
|
ExplodedNode<ValueState>* Pred);
|
|
|
|
|
|
|
|
bool EvalObjCMessageExprAux(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ObjCMessageExpr* ME,
|
|
|
|
ExplodedNode<ValueState>* Pred);
|
|
|
|
|
2008-04-17 00:40:59 +04:00
|
|
|
// Stores.
|
|
|
|
|
|
|
|
virtual void EvalStore(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* E, ExplodedNode<ValueState>* Pred,
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St, RVal TargetLV, RVal Val);
|
2008-04-12 02:25:11 +04:00
|
|
|
// End-of-path.
|
|
|
|
|
|
|
|
virtual void EvalEndPath(GRExprEngine& Engine,
|
|
|
|
GREndPathNodeBuilder<ValueState>& Builder);
|
|
|
|
|
2008-04-25 03:57:27 +04:00
|
|
|
virtual void EvalDeadSymbols(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
2008-04-25 05:25:15 +04:00
|
|
|
ExplodedNode<ValueState>* Pred,
|
|
|
|
Stmt* S,
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St,
|
2008-04-25 03:57:27 +04:00
|
|
|
const ValueStateManager::DeadSymbolsTy& Dead);
|
2008-04-17 22:12:53 +04:00
|
|
|
// Return statements.
|
|
|
|
|
|
|
|
virtual void EvalReturn(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Engine,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ReturnStmt* S,
|
|
|
|
ExplodedNode<ValueState>* Pred);
|
2008-04-18 23:23:43 +04:00
|
|
|
|
|
|
|
// Assumptions.
|
|
|
|
|
2008-07-18 03:33:10 +04:00
|
|
|
virtual const ValueState* EvalAssume(ValueStateManager& VMgr,
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St, RVal Cond,
|
|
|
|
bool Assumption, bool& isFeasible);
|
2008-04-18 23:23:43 +04:00
|
|
|
|
2008-04-09 05:10:13 +04:00
|
|
|
// Error iterators.
|
|
|
|
|
|
|
|
typedef UseAfterReleasesTy::iterator use_after_iterator;
|
|
|
|
typedef ReleasesNotOwnedTy::iterator bad_release_iterator;
|
2008-04-18 03:43:50 +04:00
|
|
|
typedef LeaksTy::iterator leaks_iterator;
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
use_after_iterator use_after_begin() { return UseAfterReleases.begin(); }
|
|
|
|
use_after_iterator use_after_end() { return UseAfterReleases.end(); }
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
bad_release_iterator bad_release_begin() { return ReleasesNotOwned.begin(); }
|
|
|
|
bad_release_iterator bad_release_end() { return ReleasesNotOwned.end(); }
|
2008-04-18 03:43:50 +04:00
|
|
|
|
|
|
|
leaks_iterator leaks_begin() { return Leaks.begin(); }
|
|
|
|
leaks_iterator leaks_end() { return Leaks.end(); }
|
2008-03-11 09:39:11 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
|
|
|
|
|
2008-03-11 22:44:10 +03:00
|
|
|
void CFRefCount::BindingsPrinter::PrintCheckerState(std::ostream& Out,
|
|
|
|
void* State, const char* nl,
|
|
|
|
const char* sep) {
|
|
|
|
RefBindings B((RefBindings::TreeTy*) State);
|
|
|
|
|
|
|
|
if (State)
|
|
|
|
Out << sep << nl;
|
|
|
|
|
|
|
|
for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
|
|
|
|
Out << (*I).first << " : ";
|
|
|
|
(*I).second.print(Out);
|
|
|
|
Out << nl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
static inline ArgEffect GetArgE(RetainSummary* Summ, unsigned idx) {
|
2008-05-22 21:31:13 +04:00
|
|
|
return Summ ? Summ->getArg(idx) : MayEscape;
|
2008-04-12 00:23:24 +04:00
|
|
|
}
|
|
|
|
|
2008-05-06 06:26:56 +04:00
|
|
|
static inline RetEffect GetRetEffect(RetainSummary* Summ) {
|
|
|
|
return Summ ? Summ->getRetEffect() : RetEffect::MakeNoRet();
|
2008-04-12 00:23:24 +04:00
|
|
|
}
|
|
|
|
|
2008-05-06 06:41:27 +04:00
|
|
|
static inline ArgEffect GetReceiverE(RetainSummary* Summ) {
|
|
|
|
return Summ ? Summ->getReceiverEffect() : DoNothing;
|
|
|
|
}
|
|
|
|
|
2008-07-18 21:24:20 +04:00
|
|
|
static inline bool IsEndPath(RetainSummary* Summ) {
|
|
|
|
return Summ ? Summ->isEndPath() : false;
|
|
|
|
}
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
void CFRefCount::ProcessNonLeakError(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* NodeExpr, Expr* ErrorExpr,
|
|
|
|
ExplodedNode<ValueState>* Pred,
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St,
|
2008-04-18 07:39:05 +04:00
|
|
|
RefVal::Kind hasErr, SymbolID Sym) {
|
2008-04-16 08:28:53 +04:00
|
|
|
Builder.BuildSinks = true;
|
|
|
|
GRExprEngine::NodeTy* N = Builder.MakeNode(Dst, NodeExpr, Pred, St);
|
|
|
|
|
|
|
|
if (!N) return;
|
|
|
|
|
|
|
|
switch (hasErr) {
|
|
|
|
default: assert(false);
|
|
|
|
case RefVal::ErrorUseAfterRelease:
|
2008-04-18 07:39:05 +04:00
|
|
|
UseAfterReleases[N] = std::make_pair(ErrorExpr, Sym);
|
2008-04-16 08:28:53 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::ErrorReleaseNotOwned:
|
2008-04-18 07:39:05 +04:00
|
|
|
ReleasesNotOwned[N] = std::make_pair(ErrorExpr, Sym);
|
2008-04-16 08:28:53 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
/// GetReturnType - Used to get the return type of a message expression or
|
|
|
|
/// function call with the intention of affixing that type to a tracked symbol.
|
|
|
|
/// While the the return type can be queried directly from RetEx, when
|
|
|
|
/// invoking class methods we augment to the return type to be that of
|
|
|
|
/// a pointer to the class (as opposed it just being id).
|
|
|
|
static QualType GetReturnType(Expr* RetE, ASTContext& Ctx) {
|
|
|
|
|
|
|
|
QualType RetTy = RetE->getType();
|
|
|
|
|
|
|
|
// FIXME: We aren't handling id<...>.
|
2008-07-27 02:36:27 +04:00
|
|
|
const PointerType* PT = RetTy->getAsPointerType();
|
2008-06-26 01:21:56 +04:00
|
|
|
if (!PT)
|
|
|
|
return RetTy;
|
|
|
|
|
|
|
|
// If RetEx is not a message expression just return its type.
|
|
|
|
// If RetEx is a message expression, return its types if it is something
|
|
|
|
/// more specific than id.
|
|
|
|
|
|
|
|
ObjCMessageExpr* ME = dyn_cast<ObjCMessageExpr>(RetE);
|
|
|
|
|
|
|
|
if (!ME || !Ctx.isObjCIdType(PT->getPointeeType()))
|
|
|
|
return RetTy;
|
|
|
|
|
|
|
|
ObjCInterfaceDecl* D = ME->getClassInfo().first;
|
|
|
|
|
|
|
|
// At this point we know the return type of the message expression is id.
|
|
|
|
// If we have an ObjCInterceDecl, we know this is a call to a class method
|
|
|
|
// whose type we can resolve. In such cases, promote the return type to
|
|
|
|
// Class*.
|
|
|
|
return !D ? RetTy : Ctx.getPointerType(Ctx.getObjCInterfaceType(D));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
void CFRefCount::EvalSummary(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* Ex,
|
|
|
|
Expr* Receiver,
|
|
|
|
RetainSummary* Summ,
|
This patch is motivated by numerous strict-aliasing warnings when compiling
clang as a Release build.
The big change is that all AST nodes (subclasses of Stmt) whose children are
Expr* store their children as Stmt* or arrays of Stmt*. This is to remove
strict-aliasing warnings when using StmtIterator. None of the interfaces of any
of the classes have changed (except those with arg_iterators, see below), as the
accessor methods introduce the needed casts (via cast<>). While this extra
casting may seem cumbersome, it actually adds some important sanity checks
throughout the codebase, as clients using StmtIterator can potentially overwrite
children that are expected to be Expr* with Stmt* (that aren't Expr*). The casts
provide extra sanity checks that are operational in debug builds to catch
invariant violations such as these.
For classes that have arg_iterators (e.g., CallExpr), the definition of
arg_iterator has been replaced. Instead of it being Expr**, it is an actual
class (called ExprIterator) that wraps a Stmt**, and provides the necessary
operators for iteration. The nice thing about this class is that it also uses
cast<> to type-checking, which introduces extra sanity checks throughout the
codebase that are useful for debugging.
A few of the CodeGen functions that use arg_iterator (especially from
OverloadExpr) have been modified to take begin and end iterators instead of a
base Expr** and the number of arguments. This matches more with the abstraction
of iteration. This still needs to be cleaned up a little bit, as clients expect
that ExprIterator is a RandomAccessIterator (which we may or may not wish to
allow for efficiency of representation).
This is a fairly large patch. It passes the tests (except CodeGen/bitfield.c,
which was already broken) on both a Debug and Release build, but it should
obviously be reviewed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52378 91177308-0d34-0410-b5e6-96231b3b80d8
2008-06-17 06:43:46 +04:00
|
|
|
ExprIterator arg_beg, ExprIterator arg_end,
|
2008-05-06 02:11:16 +04:00
|
|
|
ExplodedNode<ValueState>* Pred) {
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
// Get the state.
|
2008-05-06 02:11:16 +04:00
|
|
|
ValueStateManager& StateMgr = Eng.getStateManager();
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St = Builder.GetState(Pred);
|
2008-05-06 06:41:27 +04:00
|
|
|
|
|
|
|
// Evaluate the effect of the arguments.
|
2008-03-11 09:39:11 +03:00
|
|
|
ValueState StVals = *St;
|
2008-04-16 08:28:53 +04:00
|
|
|
RefVal::Kind hasErr = (RefVal::Kind) 0;
|
2008-03-12 04:21:45 +03:00
|
|
|
unsigned idx = 0;
|
2008-04-11 22:40:51 +04:00
|
|
|
Expr* ErrorExpr = NULL;
|
2008-04-18 07:39:05 +04:00
|
|
|
SymbolID ErrorSym = 0;
|
2008-04-11 22:40:51 +04:00
|
|
|
|
This patch is motivated by numerous strict-aliasing warnings when compiling
clang as a Release build.
The big change is that all AST nodes (subclasses of Stmt) whose children are
Expr* store their children as Stmt* or arrays of Stmt*. This is to remove
strict-aliasing warnings when using StmtIterator. None of the interfaces of any
of the classes have changed (except those with arg_iterators, see below), as the
accessor methods introduce the needed casts (via cast<>). While this extra
casting may seem cumbersome, it actually adds some important sanity checks
throughout the codebase, as clients using StmtIterator can potentially overwrite
children that are expected to be Expr* with Stmt* (that aren't Expr*). The casts
provide extra sanity checks that are operational in debug builds to catch
invariant violations such as these.
For classes that have arg_iterators (e.g., CallExpr), the definition of
arg_iterator has been replaced. Instead of it being Expr**, it is an actual
class (called ExprIterator) that wraps a Stmt**, and provides the necessary
operators for iteration. The nice thing about this class is that it also uses
cast<> to type-checking, which introduces extra sanity checks throughout the
codebase that are useful for debugging.
A few of the CodeGen functions that use arg_iterator (especially from
OverloadExpr) have been modified to take begin and end iterators instead of a
base Expr** and the number of arguments. This matches more with the abstraction
of iteration. This still needs to be cleaned up a little bit, as clients expect
that ExprIterator is a RandomAccessIterator (which we may or may not wish to
allow for efficiency of representation).
This is a fairly large patch. It passes the tests (except CodeGen/bitfield.c,
which was already broken) on both a Debug and Release build, but it should
obviously be reviewed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52378 91177308-0d34-0410-b5e6-96231b3b80d8
2008-06-17 06:43:46 +04:00
|
|
|
for (ExprIterator I = arg_beg; I != arg_end; ++I, ++idx) {
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
RVal V = StateMgr.GetRVal(St, *I);
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
if (isa<lval::SymbolVal>(V)) {
|
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
|
2008-04-12 00:23:24 +04:00
|
|
|
RefBindings B = GetRefBindings(StVals);
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
if (RefBindings::data_type* T = B.lookup(Sym)) {
|
|
|
|
B = Update(B, Sym, *T, GetArgE(Summ, idx), hasErr);
|
2008-03-12 04:21:45 +03:00
|
|
|
SetRefBindings(StVals, B);
|
2008-04-11 22:40:51 +04:00
|
|
|
|
2008-04-16 08:28:53 +04:00
|
|
|
if (hasErr) {
|
2008-04-11 22:40:51 +04:00
|
|
|
ErrorExpr = *I;
|
2008-07-07 20:21:19 +04:00
|
|
|
ErrorSym = Sym;
|
2008-04-11 22:40:51 +04:00
|
|
|
break;
|
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
2008-04-12 00:51:02 +04:00
|
|
|
}
|
2008-05-06 02:11:16 +04:00
|
|
|
else if (isa<LVal>(V)) {
|
2008-07-04 03:26:32 +04:00
|
|
|
#if 0
|
2008-05-06 02:11:16 +04:00
|
|
|
// Nuke all arguments passed by reference.
|
2008-04-12 00:23:24 +04:00
|
|
|
StateMgr.Unbind(StVals, cast<LVal>(V));
|
2008-07-04 03:26:32 +04:00
|
|
|
#else
|
2008-07-09 22:11:16 +04:00
|
|
|
if (lval::DeclVal* DV = dyn_cast<lval::DeclVal>(&V)) {
|
|
|
|
|
|
|
|
if (GetArgE(Summ, idx) == DoNothingByRef)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Invalidate the value of the variable passed by reference.
|
2008-07-04 03:26:32 +04:00
|
|
|
|
|
|
|
// FIXME: Either this logic should also be replicated in GRSimpleVals
|
|
|
|
// or should be pulled into a separate "constraint engine."
|
2008-07-09 22:11:16 +04:00
|
|
|
|
2008-07-04 03:26:32 +04:00
|
|
|
// FIXME: We can have collisions on the conjured symbol if the
|
|
|
|
// expression *I also creates conjured symbols. We probably want
|
|
|
|
// to identify conjured symbols by an expression pair: the enclosing
|
|
|
|
// expression (the context) and the expression itself. This should
|
2008-07-09 22:11:16 +04:00
|
|
|
// disambiguate conjured symbols.
|
|
|
|
|
|
|
|
// Is the invalidated variable something that we were tracking?
|
|
|
|
RVal X = StateMgr.GetRVal(&StVals, *DV);
|
2008-07-04 03:26:32 +04:00
|
|
|
|
2008-07-09 22:11:16 +04:00
|
|
|
if (isa<lval::SymbolVal>(X)) {
|
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(X).getSymbol();
|
|
|
|
SetRefBindings(StVals,RefBFactory.Remove(GetRefBindings(StVals),Sym));
|
|
|
|
}
|
|
|
|
|
2008-07-04 03:26:32 +04:00
|
|
|
// Set the value of the variable to be a conjured symbol.
|
|
|
|
unsigned Count = Builder.getCurrentBlockCount();
|
|
|
|
SymbolID NewSym = Eng.getSymbolManager().getConjuredSymbol(*I, Count);
|
|
|
|
|
2008-07-11 02:03:41 +04:00
|
|
|
StateMgr.SetRVal(StVals, *DV,
|
2008-07-04 03:26:32 +04:00
|
|
|
LVal::IsLValType(DV->getDecl()->getType())
|
|
|
|
? cast<RVal>(lval::SymbolVal(NewSym))
|
|
|
|
: cast<RVal>(nonlval::SymbolVal(NewSym)));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Nuke all other arguments passed by reference.
|
|
|
|
StateMgr.Unbind(StVals, cast<LVal>(V));
|
|
|
|
}
|
|
|
|
#endif
|
2008-04-12 00:51:02 +04:00
|
|
|
}
|
2008-04-23 01:39:21 +04:00
|
|
|
else if (isa<nonlval::LValAsInteger>(V))
|
|
|
|
StateMgr.Unbind(StVals, cast<nonlval::LValAsInteger>(V).getLVal());
|
2008-05-06 02:11:16 +04:00
|
|
|
}
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
// Evaluate the effect on the message receiver.
|
2008-05-06 06:41:27 +04:00
|
|
|
if (!ErrorExpr && Receiver) {
|
|
|
|
RVal V = StateMgr.GetRVal(St, Receiver);
|
|
|
|
|
|
|
|
if (isa<lval::SymbolVal>(V)) {
|
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
|
|
|
|
RefBindings B = GetRefBindings(StVals);
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
if (const RefVal* T = B.lookup(Sym)) {
|
|
|
|
B = Update(B, Sym, *T, GetReceiverE(Summ), hasErr);
|
2008-05-06 06:41:27 +04:00
|
|
|
SetRefBindings(StVals, B);
|
|
|
|
|
|
|
|
if (hasErr) {
|
|
|
|
ErrorExpr = Receiver;
|
2008-07-07 20:21:19 +04:00
|
|
|
ErrorSym = Sym;
|
2008-05-06 06:41:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
// Get the persistent state.
|
2008-04-16 08:28:53 +04:00
|
|
|
St = StateMgr.getPersistentState(StVals);
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
// Process any errors.
|
2008-04-16 08:28:53 +04:00
|
|
|
if (hasErr) {
|
2008-05-06 02:11:16 +04:00
|
|
|
ProcessNonLeakError(Dst, Builder, Ex, ErrorExpr, Pred, St,
|
2008-04-18 07:39:05 +04:00
|
|
|
hasErr, ErrorSym);
|
2008-03-12 04:21:45 +03:00
|
|
|
return;
|
2008-03-11 20:48:22 +03:00
|
|
|
}
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-07-18 21:24:20 +04:00
|
|
|
// Consult the summary for the return value.
|
2008-05-06 06:26:56 +04:00
|
|
|
RetEffect RE = GetRetEffect(Summ);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
switch (RE.getKind()) {
|
|
|
|
default:
|
|
|
|
assert (false && "Unhandled RetEffect."); break;
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-04-11 03:44:06 +04:00
|
|
|
case RetEffect::NoRet:
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-04-12 00:23:24 +04:00
|
|
|
// Make up a symbol for the return value (not reference counted).
|
2008-04-12 00:51:02 +04:00
|
|
|
// FIXME: This is basically copy-and-paste from GRSimpleVals. We
|
|
|
|
// should compose behavior, not copy it.
|
2008-04-12 00:23:24 +04:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
if (Ex->getType() != Eng.getContext().VoidTy) {
|
2008-04-12 00:23:24 +04:00
|
|
|
unsigned Count = Builder.getCurrentBlockCount();
|
2008-05-06 02:11:16 +04:00
|
|
|
SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
|
2008-04-12 00:23:24 +04:00
|
|
|
|
2008-05-10 03:45:33 +04:00
|
|
|
RVal X = LVal::IsLValType(Ex->getType())
|
|
|
|
? cast<RVal>(lval::SymbolVal(Sym))
|
|
|
|
: cast<RVal>(nonlval::SymbolVal(Sym));
|
2008-04-12 00:23:24 +04:00
|
|
|
|
2008-05-06 02:11:16 +04:00
|
|
|
St = StateMgr.SetRVal(St, Ex, X, Eng.getCFG().isBlkExpr(Ex), false);
|
2008-04-12 00:23:24 +04:00
|
|
|
}
|
|
|
|
|
2008-04-11 03:44:06 +04:00
|
|
|
break;
|
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
case RetEffect::Alias: {
|
2008-06-26 01:21:56 +04:00
|
|
|
unsigned idx = RE.getIndex();
|
This patch is motivated by numerous strict-aliasing warnings when compiling
clang as a Release build.
The big change is that all AST nodes (subclasses of Stmt) whose children are
Expr* store their children as Stmt* or arrays of Stmt*. This is to remove
strict-aliasing warnings when using StmtIterator. None of the interfaces of any
of the classes have changed (except those with arg_iterators, see below), as the
accessor methods introduce the needed casts (via cast<>). While this extra
casting may seem cumbersome, it actually adds some important sanity checks
throughout the codebase, as clients using StmtIterator can potentially overwrite
children that are expected to be Expr* with Stmt* (that aren't Expr*). The casts
provide extra sanity checks that are operational in debug builds to catch
invariant violations such as these.
For classes that have arg_iterators (e.g., CallExpr), the definition of
arg_iterator has been replaced. Instead of it being Expr**, it is an actual
class (called ExprIterator) that wraps a Stmt**, and provides the necessary
operators for iteration. The nice thing about this class is that it also uses
cast<> to type-checking, which introduces extra sanity checks throughout the
codebase that are useful for debugging.
A few of the CodeGen functions that use arg_iterator (especially from
OverloadExpr) have been modified to take begin and end iterators instead of a
base Expr** and the number of arguments. This matches more with the abstraction
of iteration. This still needs to be cleaned up a little bit, as clients expect
that ExprIterator is a RandomAccessIterator (which we may or may not wish to
allow for efficiency of representation).
This is a fairly large patch. It passes the tests (except CodeGen/bitfield.c,
which was already broken) on both a Debug and Release build, but it should
obviously be reviewed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52378 91177308-0d34-0410-b5e6-96231b3b80d8
2008-06-17 06:43:46 +04:00
|
|
|
assert (arg_end >= arg_beg);
|
2008-05-06 02:11:16 +04:00
|
|
|
assert (idx < (unsigned) (arg_end - arg_beg));
|
This patch is motivated by numerous strict-aliasing warnings when compiling
clang as a Release build.
The big change is that all AST nodes (subclasses of Stmt) whose children are
Expr* store their children as Stmt* or arrays of Stmt*. This is to remove
strict-aliasing warnings when using StmtIterator. None of the interfaces of any
of the classes have changed (except those with arg_iterators, see below), as the
accessor methods introduce the needed casts (via cast<>). While this extra
casting may seem cumbersome, it actually adds some important sanity checks
throughout the codebase, as clients using StmtIterator can potentially overwrite
children that are expected to be Expr* with Stmt* (that aren't Expr*). The casts
provide extra sanity checks that are operational in debug builds to catch
invariant violations such as these.
For classes that have arg_iterators (e.g., CallExpr), the definition of
arg_iterator has been replaced. Instead of it being Expr**, it is an actual
class (called ExprIterator) that wraps a Stmt**, and provides the necessary
operators for iteration. The nice thing about this class is that it also uses
cast<> to type-checking, which introduces extra sanity checks throughout the
codebase that are useful for debugging.
A few of the CodeGen functions that use arg_iterator (especially from
OverloadExpr) have been modified to take begin and end iterators instead of a
base Expr** and the number of arguments. This matches more with the abstraction
of iteration. This still needs to be cleaned up a little bit, as clients expect
that ExprIterator is a RandomAccessIterator (which we may or may not wish to
allow for efficiency of representation).
This is a fairly large patch. It passes the tests (except CodeGen/bitfield.c,
which was already broken) on both a Debug and Release build, but it should
obviously be reviewed.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52378 91177308-0d34-0410-b5e6-96231b3b80d8
2008-06-17 06:43:46 +04:00
|
|
|
RVal V = StateMgr.GetRVal(St, *(arg_beg+idx));
|
2008-05-06 02:11:16 +04:00
|
|
|
St = StateMgr.SetRVal(St, Ex, V, Eng.getCFG().isBlkExpr(Ex), false);
|
2008-03-12 04:21:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-05-06 06:41:27 +04:00
|
|
|
case RetEffect::ReceiverAlias: {
|
|
|
|
assert (Receiver);
|
|
|
|
RVal V = StateMgr.GetRVal(St, Receiver);
|
|
|
|
St = StateMgr.SetRVal(St, Ex, V, Eng.getCFG().isBlkExpr(Ex), false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-06-23 22:02:52 +04:00
|
|
|
case RetEffect::OwnedAllocatedSymbol:
|
2008-03-12 04:21:45 +03:00
|
|
|
case RetEffect::OwnedSymbol: {
|
|
|
|
unsigned Count = Builder.getCurrentBlockCount();
|
2008-05-06 02:11:16 +04:00
|
|
|
SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
|
2008-06-26 01:21:56 +04:00
|
|
|
QualType RetT = GetReturnType(Ex, Eng.getContext());
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
ValueState StImpl = *St;
|
|
|
|
RefBindings B = GetRefBindings(StImpl);
|
2008-06-26 01:21:56 +04:00
|
|
|
SetRefBindings(StImpl, RefBFactory.Add(B, Sym, RefVal::makeOwned(RetT)));
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl),
|
2008-05-06 02:11:16 +04:00
|
|
|
Ex, lval::SymbolVal(Sym),
|
|
|
|
Eng.getCFG().isBlkExpr(Ex), false);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
2008-06-23 22:02:52 +04:00
|
|
|
// FIXME: Add a flag to the checker where allocations are allowed to fail.
|
|
|
|
if (RE.getKind() == RetEffect::OwnedAllocatedSymbol)
|
|
|
|
St = StateMgr.AddNE(St, Sym, Eng.getBasicVals().getZeroWithPtrWidth());
|
|
|
|
|
2008-03-12 04:21:45 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RetEffect::NotOwnedSymbol: {
|
|
|
|
unsigned Count = Builder.getCurrentBlockCount();
|
2008-05-06 02:11:16 +04:00
|
|
|
SymbolID Sym = Eng.getSymbolManager().getConjuredSymbol(Ex, Count);
|
2008-06-26 01:21:56 +04:00
|
|
|
QualType RetT = GetReturnType(Ex, Eng.getContext());
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
ValueState StImpl = *St;
|
|
|
|
RefBindings B = GetRefBindings(StImpl);
|
2008-06-26 01:21:56 +04:00
|
|
|
SetRefBindings(StImpl, RefBFactory.Add(B, Sym,
|
|
|
|
RefVal::makeNotOwned(RetT)));
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
St = StateMgr.SetRVal(StateMgr.getPersistentState(StImpl),
|
2008-05-06 02:11:16 +04:00
|
|
|
Ex, lval::SymbolVal(Sym),
|
|
|
|
Eng.getCFG().isBlkExpr(Ex), false);
|
2008-03-12 04:21:45 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-05-06 02:11:16 +04:00
|
|
|
|
2008-07-18 21:24:20 +04:00
|
|
|
// Is this a sink?
|
|
|
|
if (IsEndPath(Summ))
|
|
|
|
Builder.MakeSinkNode(Dst, Ex, Pred, St);
|
|
|
|
else
|
|
|
|
Builder.MakeNode(Dst, Ex, Pred, St);
|
2008-05-06 02:11:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CFRefCount::EvalCall(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
CallExpr* CE, RVal L,
|
|
|
|
ExplodedNode<ValueState>* Pred) {
|
|
|
|
|
2008-08-12 22:30:56 +04:00
|
|
|
RetainSummary* Summ = !isa<lval::FuncVal>(L) ? 0
|
|
|
|
: Summaries.getSummary(cast<lval::FuncVal>(L).getDecl());
|
2008-05-06 02:11:16 +04:00
|
|
|
|
|
|
|
EvalSummary(Dst, Eng, Builder, CE, 0, Summ,
|
|
|
|
CE->arg_begin(), CE->arg_end(), Pred);
|
2008-03-06 03:08:09 +03:00
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-04-16 03:44:31 +04:00
|
|
|
void CFRefCount::EvalObjCMessageExpr(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ObjCMessageExpr* ME,
|
2008-08-12 22:30:56 +04:00
|
|
|
ExplodedNode<ValueState>* Pred) {
|
2008-05-06 08:20:12 +04:00
|
|
|
RetainSummary* Summ;
|
2008-05-02 01:31:50 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
if (Expr* Receiver = ME->getReceiver()) {
|
|
|
|
// We need the type-information of the tracked receiver object
|
|
|
|
// Retrieve it from the state.
|
|
|
|
ObjCInterfaceDecl* ID = 0;
|
|
|
|
|
|
|
|
// FIXME: Wouldn't it be great if this code could be reduced? It's just
|
|
|
|
// a chain of lookups.
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St = Builder.GetState(Pred);
|
2008-06-26 01:21:56 +04:00
|
|
|
RVal V = Eng.getStateManager().GetRVal(St, Receiver );
|
|
|
|
|
|
|
|
if (isa<lval::SymbolVal>(V)) {
|
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
if (const RefVal* T = GetRefBindings(*St).lookup(Sym)) {
|
|
|
|
QualType Ty = T->getType();
|
2008-06-26 01:21:56 +04:00
|
|
|
|
|
|
|
if (const PointerType* PT = Ty->getAsPointerType()) {
|
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
|
|
|
|
|
|
|
if (ObjCInterfaceType* IT = dyn_cast<ObjCInterfaceType>(PointeeTy))
|
|
|
|
ID = IT->getDecl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Summ = Summaries.getMethodSummary(ME, ID);
|
|
|
|
}
|
2008-04-16 08:28:53 +04:00
|
|
|
else
|
2008-06-24 02:21:20 +04:00
|
|
|
Summ = Summaries.getClassMethodSummary(ME->getClassName(),
|
|
|
|
ME->getSelector());
|
2008-04-16 08:28:53 +04:00
|
|
|
|
2008-05-06 08:20:12 +04:00
|
|
|
EvalSummary(Dst, Eng, Builder, ME, ME->getReceiver(), Summ,
|
|
|
|
ME->arg_begin(), ME->arg_end(), Pred);
|
2008-04-16 03:44:31 +04:00
|
|
|
}
|
2008-05-06 08:20:12 +04:00
|
|
|
|
2008-04-17 00:40:59 +04:00
|
|
|
// Stores.
|
|
|
|
|
|
|
|
void CFRefCount::EvalStore(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
Expr* E, ExplodedNode<ValueState>* Pred,
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St, RVal TargetLV, RVal Val) {
|
2008-04-17 00:40:59 +04:00
|
|
|
|
|
|
|
// Check if we have a binding for "Val" and if we are storing it to something
|
|
|
|
// we don't understand or otherwise the value "escapes" the function.
|
|
|
|
|
|
|
|
if (!isa<lval::SymbolVal>(Val))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Are we storing to something that causes the value to "escape"?
|
|
|
|
|
|
|
|
bool escapes = false;
|
|
|
|
|
|
|
|
if (!isa<lval::DeclVal>(TargetLV))
|
|
|
|
escapes = true;
|
|
|
|
else
|
|
|
|
escapes = cast<lval::DeclVal>(TargetLV).getDecl()->hasGlobalStorage();
|
|
|
|
|
|
|
|
if (!escapes)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(Val).getSymbol();
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
if (!GetRefBindings(*St).lookup(Sym))
|
2008-04-17 00:40:59 +04:00
|
|
|
return;
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
// Nuke the binding.
|
|
|
|
St = NukeBinding(Eng.getStateManager(), St, Sym);
|
2008-04-17 00:40:59 +04:00
|
|
|
|
|
|
|
// Hand of the remaining logic to the parent implementation.
|
|
|
|
GRSimpleVals::EvalStore(Dst, Eng, Builder, E, Pred, St, TargetLV, Val);
|
|
|
|
}
|
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* CFRefCount::NukeBinding(ValueStateManager& VMgr,
|
|
|
|
const ValueState* St,
|
|
|
|
SymbolID sid) {
|
2008-04-17 02:32:20 +04:00
|
|
|
ValueState StImpl = *St;
|
|
|
|
RefBindings B = GetRefBindings(StImpl);
|
|
|
|
StImpl.CheckerState = RefBFactory.Remove(B, sid).getRoot();
|
|
|
|
return VMgr.getPersistentState(StImpl);
|
|
|
|
}
|
|
|
|
|
2008-04-12 02:25:11 +04:00
|
|
|
// End-of-path.
|
|
|
|
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* CFRefCount::HandleSymbolDeath(ValueStateManager& VMgr,
|
|
|
|
const ValueState* St, SymbolID sid,
|
2008-04-17 02:32:20 +04:00
|
|
|
RefVal V, bool& hasLeak) {
|
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
hasLeak = V.isOwned() ||
|
|
|
|
((V.isNotOwned() || V.isReturnedOwned()) && V.getCount() > 0);
|
2008-04-17 02:32:20 +04:00
|
|
|
|
|
|
|
if (!hasLeak)
|
|
|
|
return NukeBinding(VMgr, St, sid);
|
|
|
|
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
2008-06-26 01:21:56 +04:00
|
|
|
ValueState StImpl = *St;
|
|
|
|
StImpl.CheckerState = RefBFactory.Add(B, sid, V^RefVal::ErrorLeak).getRoot();
|
2008-05-05 21:53:17 +04:00
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
return VMgr.getPersistentState(StImpl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CFRefCount::EvalEndPath(GRExprEngine& Eng,
|
2008-04-12 02:25:11 +04:00
|
|
|
GREndPathNodeBuilder<ValueState>& Builder) {
|
|
|
|
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St = Builder.getState();
|
2008-04-17 02:32:20 +04:00
|
|
|
RefBindings B = GetRefBindings(*St);
|
2008-04-12 02:25:11 +04:00
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
llvm::SmallVector<SymbolID, 10> Leaked;
|
2008-04-12 02:25:11 +04:00
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
for (RefBindings::iterator I = B.begin(), E = B.end(); I != E; ++I) {
|
|
|
|
bool hasLeak = false;
|
2008-04-12 02:25:11 +04:00
|
|
|
|
2008-04-17 02:32:20 +04:00
|
|
|
St = HandleSymbolDeath(Eng.getStateManager(), St,
|
|
|
|
(*I).first, (*I).second, hasLeak);
|
|
|
|
|
|
|
|
if (hasLeak) Leaked.push_back((*I).first);
|
|
|
|
}
|
2008-04-25 03:57:27 +04:00
|
|
|
|
|
|
|
if (Leaked.empty())
|
|
|
|
return;
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
ExplodedNode<ValueState>* N = Builder.MakeNode(St);
|
2008-04-18 20:30:14 +04:00
|
|
|
|
2008-04-25 03:57:27 +04:00
|
|
|
if (!N)
|
2008-04-18 20:30:14 +04:00
|
|
|
return;
|
2008-04-18 23:23:43 +04:00
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
std::vector<SymbolID>*& LeaksAtNode = Leaks[N];
|
|
|
|
assert (!LeaksAtNode);
|
|
|
|
LeaksAtNode = new std::vector<SymbolID>();
|
2008-04-17 02:32:20 +04:00
|
|
|
|
|
|
|
for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(),
|
|
|
|
E = Leaked.end(); I != E; ++I)
|
2008-04-18 07:39:05 +04:00
|
|
|
(*LeaksAtNode).push_back(*I);
|
2008-04-12 02:25:11 +04:00
|
|
|
}
|
|
|
|
|
2008-04-25 03:57:27 +04:00
|
|
|
// Dead symbols.
|
|
|
|
|
|
|
|
void CFRefCount::EvalDeadSymbols(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
2008-04-25 05:25:15 +04:00
|
|
|
ExplodedNode<ValueState>* Pred,
|
|
|
|
Stmt* S,
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St,
|
2008-04-25 03:57:27 +04:00
|
|
|
const ValueStateManager::DeadSymbolsTy& Dead) {
|
2008-04-25 05:25:15 +04:00
|
|
|
|
2008-04-25 03:57:27 +04:00
|
|
|
// FIXME: a lot of copy-and-paste from EvalEndPath. Refactor.
|
|
|
|
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
|
|
|
llvm::SmallVector<SymbolID, 10> Leaked;
|
|
|
|
|
|
|
|
for (ValueStateManager::DeadSymbolsTy::const_iterator
|
|
|
|
I=Dead.begin(), E=Dead.end(); I!=E; ++I) {
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
const RefVal* T = B.lookup(*I);
|
2008-04-25 03:57:27 +04:00
|
|
|
|
|
|
|
if (!T)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool hasLeak = false;
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
St = HandleSymbolDeath(Eng.getStateManager(), St, *I, *T, hasLeak);
|
2008-04-25 03:57:27 +04:00
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
if (hasLeak)
|
|
|
|
Leaked.push_back(*I);
|
2008-04-25 03:57:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Leaked.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
ExplodedNode<ValueState>* N = Builder.MakeNode(Dst, S, Pred, St);
|
|
|
|
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::vector<SymbolID>*& LeaksAtNode = Leaks[N];
|
|
|
|
assert (!LeaksAtNode);
|
|
|
|
LeaksAtNode = new std::vector<SymbolID>();
|
|
|
|
|
|
|
|
for (llvm::SmallVector<SymbolID, 10>::iterator I=Leaked.begin(),
|
|
|
|
E = Leaked.end(); I != E; ++I)
|
|
|
|
(*LeaksAtNode).push_back(*I);
|
|
|
|
}
|
|
|
|
|
2008-04-17 22:12:53 +04:00
|
|
|
// Return statements.
|
|
|
|
|
|
|
|
void CFRefCount::EvalReturn(ExplodedNodeSet<ValueState>& Dst,
|
|
|
|
GRExprEngine& Eng,
|
|
|
|
GRStmtNodeBuilder<ValueState>& Builder,
|
|
|
|
ReturnStmt* S,
|
|
|
|
ExplodedNode<ValueState>* Pred) {
|
|
|
|
|
|
|
|
Expr* RetE = S->getRetValue();
|
|
|
|
if (!RetE) return;
|
|
|
|
|
|
|
|
ValueStateManager& StateMgr = Eng.getStateManager();
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St = Builder.GetState(Pred);
|
2008-04-17 22:12:53 +04:00
|
|
|
RVal V = StateMgr.GetRVal(St, RetE);
|
|
|
|
|
|
|
|
if (!isa<lval::SymbolVal>(V))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get the reference count binding (if any).
|
|
|
|
SymbolID Sym = cast<lval::SymbolVal>(V).getSymbol();
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
2008-07-07 20:21:19 +04:00
|
|
|
const RefVal* T = B.lookup(Sym);
|
2008-04-17 22:12:53 +04:00
|
|
|
|
|
|
|
if (!T)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Change the reference count.
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
RefVal X = *T;
|
2008-04-17 22:12:53 +04:00
|
|
|
|
|
|
|
switch (X.getKind()) {
|
|
|
|
|
|
|
|
case RefVal::Owned: {
|
|
|
|
unsigned cnt = X.getCount();
|
2008-05-22 21:31:13 +04:00
|
|
|
assert (cnt > 0);
|
|
|
|
X = RefVal::makeReturnedOwned(cnt - 1);
|
2008-04-17 22:12:53 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case RefVal::NotOwned: {
|
|
|
|
unsigned cnt = X.getCount();
|
|
|
|
X = cnt ? RefVal::makeReturnedOwned(cnt - 1)
|
|
|
|
: RefVal::makeReturnedNotOwned();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the binding.
|
|
|
|
|
|
|
|
ValueState StImpl = *St;
|
|
|
|
StImpl.CheckerState = RefBFactory.Add(B, Sym, X).getRoot();
|
|
|
|
Builder.MakeNode(Dst, S, Pred, StateMgr.getPersistentState(StImpl));
|
|
|
|
}
|
|
|
|
|
2008-04-18 23:23:43 +04:00
|
|
|
// Assumptions.
|
|
|
|
|
2008-07-18 03:33:10 +04:00
|
|
|
const ValueState* CFRefCount::EvalAssume(ValueStateManager& VMgr,
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St,
|
|
|
|
RVal Cond, bool Assumption,
|
|
|
|
bool& isFeasible) {
|
2008-04-18 23:23:43 +04:00
|
|
|
|
|
|
|
// FIXME: We may add to the interface of EvalAssume the list of symbols
|
|
|
|
// whose assumptions have changed. For now we just iterate through the
|
|
|
|
// bindings and check if any of the tracked symbols are NULL. This isn't
|
|
|
|
// too bad since the number of symbols we will track in practice are
|
|
|
|
// probably small and EvalAssume is only called at branches and a few
|
|
|
|
// other places.
|
|
|
|
RefBindings B = GetRefBindings(*St);
|
|
|
|
|
|
|
|
if (B.isEmpty())
|
|
|
|
return St;
|
|
|
|
|
|
|
|
bool changed = false;
|
|
|
|
|
|
|
|
for (RefBindings::iterator I=B.begin(), E=B.end(); I!=E; ++I) {
|
|
|
|
// Check if the symbol is null (or equal to any constant).
|
|
|
|
// If this is the case, stop tracking the symbol.
|
|
|
|
if (St->getSymVal(I.getKey())) {
|
|
|
|
changed = true;
|
|
|
|
B = RefBFactory.Remove(B, I.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!changed)
|
|
|
|
return St;
|
|
|
|
|
|
|
|
ValueState StImpl = *St;
|
|
|
|
StImpl.CheckerState = B.getRoot();
|
2008-07-18 03:33:10 +04:00
|
|
|
return VMgr.getPersistentState(StImpl);
|
2008-04-18 23:23:43 +04:00
|
|
|
}
|
2008-03-11 09:39:11 +03:00
|
|
|
|
|
|
|
CFRefCount::RefBindings CFRefCount::Update(RefBindings B, SymbolID sym,
|
2008-03-11 20:48:22 +03:00
|
|
|
RefVal V, ArgEffect E,
|
2008-04-16 08:28:53 +04:00
|
|
|
RefVal::Kind& hasErr) {
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
// FIXME: This dispatch can potentially be sped up by unifiying it into
|
|
|
|
// a single switch statement. Opt for simplicity for now.
|
2008-03-11 09:39:11 +03:00
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
switch (E) {
|
|
|
|
default:
|
|
|
|
assert (false && "Unhandled CFRef transition.");
|
2008-05-22 21:31:13 +04:00
|
|
|
|
|
|
|
case MayEscape:
|
|
|
|
if (V.getKind() == RefVal::Owned) {
|
2008-06-26 01:21:56 +04:00
|
|
|
V = V ^ RefVal::NotOwned;
|
2008-05-22 21:31:13 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Fall-through.
|
2008-07-09 22:11:16 +04:00
|
|
|
case DoNothingByRef:
|
2008-03-11 20:48:22 +03:00
|
|
|
case DoNothing:
|
2008-05-06 02:11:16 +04:00
|
|
|
if (!isGCEnabled() && V.getKind() == RefVal::Released) {
|
2008-06-26 01:21:56 +04:00
|
|
|
V = V ^ RefVal::ErrorUseAfterRelease;
|
2008-04-16 08:28:53 +04:00
|
|
|
hasErr = V.getKind();
|
2008-03-12 04:21:45 +03:00
|
|
|
break;
|
2008-08-12 22:30:56 +04:00
|
|
|
}
|
2008-03-11 20:48:22 +03:00
|
|
|
return B;
|
2008-06-30 20:57:41 +04:00
|
|
|
|
2008-07-01 04:01:02 +04:00
|
|
|
case Autorelease:
|
2008-05-06 06:41:27 +04:00
|
|
|
case StopTracking:
|
|
|
|
return RefBFactory.Remove(B, sym);
|
2008-08-12 22:30:56 +04:00
|
|
|
|
2008-03-11 20:48:22 +03:00
|
|
|
case IncRef:
|
|
|
|
switch (V.getKind()) {
|
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
|
|
|
|
case RefVal::Owned:
|
|
|
|
case RefVal::NotOwned:
|
2008-06-26 01:21:56 +04:00
|
|
|
V = V + 1;
|
2008-08-12 22:30:56 +04:00
|
|
|
break;
|
2008-03-11 20:48:22 +03:00
|
|
|
case RefVal::Released:
|
2008-05-06 02:11:16 +04:00
|
|
|
if (isGCEnabled())
|
2008-06-26 01:21:56 +04:00
|
|
|
V = V ^ RefVal::Owned;
|
2008-04-29 09:44:10 +04:00
|
|
|
else {
|
2008-06-26 01:21:56 +04:00
|
|
|
V = V ^ RefVal::ErrorUseAfterRelease;
|
2008-04-29 09:44:10 +04:00
|
|
|
hasErr = V.getKind();
|
|
|
|
}
|
2008-03-11 20:48:22 +03:00
|
|
|
break;
|
2008-08-12 22:30:56 +04:00
|
|
|
}
|
2008-04-11 03:44:06 +04:00
|
|
|
break;
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
case SelfOwn:
|
|
|
|
V = V ^ RefVal::NotOwned;
|
2008-08-12 22:30:56 +04:00
|
|
|
// Fall-through.
|
2008-03-11 20:48:22 +03:00
|
|
|
case DecRef:
|
|
|
|
switch (V.getKind()) {
|
|
|
|
default:
|
|
|
|
assert (false);
|
2008-08-12 22:30:56 +04:00
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
case RefVal::Owned:
|
|
|
|
V = V.getCount() > 1 ? V - 1 : V ^ RefVal::Released;
|
2008-03-11 20:48:22 +03:00
|
|
|
break;
|
|
|
|
|
2008-06-26 01:21:56 +04:00
|
|
|
case RefVal::NotOwned:
|
|
|
|
if (V.getCount() > 0)
|
|
|
|
V = V - 1;
|
2008-04-11 03:09:18 +04:00
|
|
|
else {
|
2008-06-26 01:21:56 +04:00
|
|
|
V = V ^ RefVal::ErrorReleaseNotOwned;
|
2008-04-16 08:28:53 +04:00
|
|
|
hasErr = V.getKind();
|
2008-08-12 22:30:56 +04:00
|
|
|
}
|
2008-03-11 20:48:22 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::Released:
|
2008-06-26 01:21:56 +04:00
|
|
|
V = V ^ RefVal::ErrorUseAfterRelease;
|
2008-04-16 08:28:53 +04:00
|
|
|
hasErr = V.getKind();
|
2008-03-11 20:48:22 +03:00
|
|
|
break;
|
2008-08-12 22:30:56 +04:00
|
|
|
}
|
2008-04-11 03:44:06 +04:00
|
|
|
break;
|
2008-03-11 20:48:22 +03:00
|
|
|
}
|
|
|
|
return RefBFactory.Add(B, sym, V);
|
2008-03-11 09:39:11 +03:00
|
|
|
}
|
|
|
|
|
2008-04-09 05:10:13 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-10 03:49:11 +04:00
|
|
|
// Error reporting.
|
2008-04-09 05:10:13 +04:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
//===-------------===//
|
|
|
|
// Bug Descriptions. //
|
|
|
|
//===-------------===//
|
|
|
|
|
2008-04-19 00:54:29 +04:00
|
|
|
class VISIBILITY_HIDDEN CFRefBug : public BugTypeCacheLocation {
|
2008-04-18 07:39:05 +04:00
|
|
|
protected:
|
|
|
|
CFRefCount& TF;
|
|
|
|
|
|
|
|
public:
|
|
|
|
CFRefBug(CFRefCount& tf) : TF(tf) {}
|
2008-05-01 03:47:44 +04:00
|
|
|
|
2008-05-02 02:50:36 +04:00
|
|
|
CFRefCount& getTF() { return TF; }
|
2008-05-06 03:16:31 +04:00
|
|
|
const CFRefCount& getTF() const { return TF; }
|
|
|
|
|
2008-05-02 03:13:35 +04:00
|
|
|
virtual bool isLeak() const { return false; }
|
2008-04-18 07:39:05 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN UseAfterRelease : public CFRefBug {
|
|
|
|
public:
|
|
|
|
UseAfterRelease(CFRefCount& tf) : CFRefBug(tf) {}
|
|
|
|
|
|
|
|
virtual const char* getName() const {
|
2008-05-06 03:16:31 +04:00
|
|
|
return "Use-After-Release";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
virtual const char* getDescription() const {
|
2008-08-12 22:30:56 +04:00
|
|
|
return "Reference-counted object is used after it is released.";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EmitWarnings(BugReporter& BR);
|
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN BadRelease : public CFRefBug {
|
|
|
|
public:
|
|
|
|
BadRelease(CFRefCount& tf) : CFRefBug(tf) {}
|
|
|
|
|
|
|
|
virtual const char* getName() const {
|
2008-05-06 03:16:31 +04:00
|
|
|
return "Bad Release";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
virtual const char* getDescription() const {
|
|
|
|
return "Incorrect decrement of the reference count of a "
|
2008-04-18 08:55:01 +04:00
|
|
|
"CoreFoundation object: "
|
2008-04-18 07:39:05 +04:00
|
|
|
"The object is not owned at this point by the caller.";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EmitWarnings(BugReporter& BR);
|
|
|
|
};
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN Leak : public CFRefBug {
|
|
|
|
public:
|
|
|
|
Leak(CFRefCount& tf) : CFRefBug(tf) {}
|
|
|
|
|
|
|
|
virtual const char* getName() const {
|
2008-05-06 22:11:36 +04:00
|
|
|
|
|
|
|
if (getTF().isGCEnabled())
|
|
|
|
return "Memory Leak (GC)";
|
|
|
|
|
|
|
|
if (getTF().getLangOptions().getGCMode() == LangOptions::HybridGC)
|
|
|
|
return "Memory Leak (Hybrid MM, non-GC)";
|
|
|
|
|
|
|
|
assert (getTF().getLangOptions().getGCMode() == LangOptions::NonGC);
|
|
|
|
return "Memory Leak";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual const char* getDescription() const {
|
2008-08-12 22:30:56 +04:00
|
|
|
return "Object leaked";
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EmitWarnings(BugReporter& BR);
|
2008-05-02 02:50:36 +04:00
|
|
|
virtual void GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes);
|
2008-05-02 03:13:35 +04:00
|
|
|
virtual bool isLeak() const { return true; }
|
2008-05-16 22:33:44 +04:00
|
|
|
virtual bool isCached(BugReport& R);
|
2008-04-18 07:39:05 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
//===---------===//
|
|
|
|
// Bug Reports. //
|
|
|
|
//===---------===//
|
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN CFRefReport : public RangedBugReport {
|
|
|
|
SymbolID Sym;
|
|
|
|
public:
|
2008-05-01 03:47:44 +04:00
|
|
|
CFRefReport(CFRefBug& D, ExplodedNode<ValueState> *n, SymbolID sym)
|
2008-04-18 07:39:05 +04:00
|
|
|
: RangedBugReport(D, n), Sym(sym) {}
|
|
|
|
|
|
|
|
virtual ~CFRefReport() {}
|
|
|
|
|
2008-05-02 02:50:36 +04:00
|
|
|
CFRefBug& getBugType() {
|
|
|
|
return (CFRefBug&) RangedBugReport::getBugType();
|
|
|
|
}
|
|
|
|
const CFRefBug& getBugType() const {
|
|
|
|
return (const CFRefBug&) RangedBugReport::getBugType();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getRanges(BugReporter& BR, const SourceRange*& beg,
|
|
|
|
const SourceRange*& end) {
|
|
|
|
|
2008-05-03 00:53:50 +04:00
|
|
|
if (!getBugType().isLeak())
|
2008-05-02 02:50:36 +04:00
|
|
|
RangedBugReport::getRanges(BR, beg, end);
|
2008-08-12 22:30:56 +04:00
|
|
|
else
|
|
|
|
beg = end = 0;
|
2008-05-02 02:50:36 +04:00
|
|
|
}
|
|
|
|
|
2008-05-16 22:33:44 +04:00
|
|
|
SymbolID getSymbol() const { return Sym; }
|
|
|
|
|
2008-05-02 03:13:35 +04:00
|
|
|
virtual PathDiagnosticPiece* getEndPath(BugReporter& BR,
|
|
|
|
ExplodedNode<ValueState>* N);
|
|
|
|
|
2008-05-01 03:47:44 +04:00
|
|
|
virtual std::pair<const char**,const char**> getExtraDescriptiveText();
|
2008-04-18 07:39:05 +04:00
|
|
|
|
|
|
|
virtual PathDiagnosticPiece* VisitNode(ExplodedNode<ValueState>* N,
|
|
|
|
ExplodedNode<ValueState>* PrevN,
|
|
|
|
ExplodedGraph<ValueState>& G,
|
|
|
|
BugReporter& BR);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
void CFRefCount::RegisterChecks(GRExprEngine& Eng) {
|
|
|
|
Eng.Register(new UseAfterRelease(*this));
|
|
|
|
Eng.Register(new BadRelease(*this));
|
|
|
|
Eng.Register(new Leak(*this));
|
|
|
|
}
|
|
|
|
|
2008-05-01 03:47:44 +04:00
|
|
|
|
|
|
|
static const char* Msgs[] = {
|
|
|
|
"Code is compiled in garbage collection only mode" // GC only
|
|
|
|
" (the bug occurs with garbage collection enabled).",
|
|
|
|
|
|
|
|
"Code is compiled without garbage collection.", // No GC.
|
|
|
|
|
|
|
|
"Code is compiled for use with and without garbage collection (GC)."
|
|
|
|
" The bug occurs with GC enabled.", // Hybrid, with GC.
|
|
|
|
|
|
|
|
"Code is compiled for use with and without garbage collection (GC)."
|
|
|
|
" The bug occurs in non-GC mode." // Hyrbird, without GC/
|
|
|
|
};
|
|
|
|
|
|
|
|
std::pair<const char**,const char**> CFRefReport::getExtraDescriptiveText() {
|
|
|
|
CFRefCount& TF = static_cast<CFRefBug&>(getBugType()).getTF();
|
|
|
|
|
|
|
|
switch (TF.getLangOptions().getGCMode()) {
|
|
|
|
default:
|
|
|
|
assert(false);
|
2008-05-01 08:02:04 +04:00
|
|
|
|
|
|
|
case LangOptions::GCOnly:
|
|
|
|
assert (TF.isGCEnabled());
|
2008-08-12 22:30:56 +04:00
|
|
|
return std::make_pair(&Msgs[0], &Msgs[0]+1);
|
|
|
|
|
2008-05-01 03:47:44 +04:00
|
|
|
case LangOptions::NonGC:
|
|
|
|
assert (!TF.isGCEnabled());
|
|
|
|
return std::make_pair(&Msgs[1], &Msgs[1]+1);
|
|
|
|
|
|
|
|
case LangOptions::HybridGC:
|
|
|
|
if (TF.isGCEnabled())
|
|
|
|
return std::make_pair(&Msgs[2], &Msgs[2]+1);
|
|
|
|
else
|
|
|
|
return std::make_pair(&Msgs[3], &Msgs[3]+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
PathDiagnosticPiece* CFRefReport::VisitNode(ExplodedNode<ValueState>* N,
|
|
|
|
ExplodedNode<ValueState>* PrevN,
|
|
|
|
ExplodedGraph<ValueState>& G,
|
|
|
|
BugReporter& BR) {
|
|
|
|
|
|
|
|
// Check if the type state has changed.
|
|
|
|
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* PrevSt = PrevN->getState();
|
|
|
|
const ValueState* CurrSt = N->getState();
|
2008-04-18 07:39:05 +04:00
|
|
|
|
|
|
|
CFRefCount::RefBindings PrevB = CFRefCount::GetRefBindings(*PrevSt);
|
|
|
|
CFRefCount::RefBindings CurrB = CFRefCount::GetRefBindings(*CurrSt);
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
const RefVal* PrevT = PrevB.lookup(Sym);
|
|
|
|
const RefVal* CurrT = CurrB.lookup(Sym);
|
2008-04-18 07:39:05 +04:00
|
|
|
|
2008-04-18 08:55:01 +04:00
|
|
|
if (!CurrT)
|
|
|
|
return NULL;
|
2008-04-18 07:39:05 +04:00
|
|
|
|
2008-04-18 08:55:01 +04:00
|
|
|
const char* Msg = NULL;
|
2008-07-07 20:21:19 +04:00
|
|
|
const RefVal& CurrV = *CurrB.lookup(Sym);
|
2008-05-05 21:53:17 +04:00
|
|
|
|
2008-04-18 08:55:01 +04:00
|
|
|
if (!PrevT) {
|
|
|
|
|
2008-05-05 21:53:17 +04:00
|
|
|
Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
|
|
|
|
|
|
|
|
if (CurrV.isOwned()) {
|
|
|
|
|
|
|
|
if (isa<CallExpr>(S))
|
|
|
|
Msg = "Function call returns an object with a +1 retain count"
|
|
|
|
" (owning reference).";
|
|
|
|
else {
|
|
|
|
assert (isa<ObjCMessageExpr>(S));
|
|
|
|
Msg = "Method returns an object with a +1 retain count"
|
|
|
|
" (owning reference).";
|
|
|
|
}
|
|
|
|
}
|
2008-04-18 08:55:01 +04:00
|
|
|
else {
|
|
|
|
assert (CurrV.isNotOwned());
|
2008-05-05 21:53:17 +04:00
|
|
|
|
|
|
|
if (isa<CallExpr>(S))
|
|
|
|
Msg = "Function call returns an object with a +0 retain count"
|
|
|
|
" (non-owning reference).";
|
|
|
|
else {
|
|
|
|
assert (isa<ObjCMessageExpr>(S));
|
|
|
|
Msg = "Method returns an object with a +0 retain count"
|
|
|
|
" (non-owning reference).";
|
|
|
|
}
|
2008-04-18 08:55:01 +04:00
|
|
|
}
|
2008-05-05 21:53:17 +04:00
|
|
|
|
2008-04-18 08:55:01 +04:00
|
|
|
FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
|
|
|
|
PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
|
|
|
|
|
|
|
|
if (Expr* Exp = dyn_cast<Expr>(S))
|
|
|
|
P->addRange(Exp->getSourceRange());
|
|
|
|
|
|
|
|
return P;
|
|
|
|
}
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
// Determine if the typestate has changed.
|
|
|
|
RefVal PrevV = *PrevB.lookup(Sym);
|
2008-04-18 08:55:01 +04:00
|
|
|
|
|
|
|
if (PrevV == CurrV)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// The typestate has changed.
|
|
|
|
|
|
|
|
std::ostringstream os;
|
|
|
|
|
|
|
|
switch (CurrV.getKind()) {
|
|
|
|
case RefVal::Owned:
|
|
|
|
case RefVal::NotOwned:
|
2008-05-22 21:31:13 +04:00
|
|
|
|
|
|
|
if (PrevV.getCount() == CurrV.getCount())
|
|
|
|
return 0;
|
2008-04-18 08:55:01 +04:00
|
|
|
|
|
|
|
if (PrevV.getCount() > CurrV.getCount())
|
|
|
|
os << "Reference count decremented.";
|
|
|
|
else
|
|
|
|
os << "Reference count incremented.";
|
|
|
|
|
2008-05-22 21:31:13 +04:00
|
|
|
if (unsigned Count = CurrV.getCount()) {
|
2008-05-05 21:53:17 +04:00
|
|
|
|
|
|
|
os << " Object has +" << Count;
|
2008-04-18 09:32:44 +04:00
|
|
|
|
2008-05-05 21:53:17 +04:00
|
|
|
if (Count > 1)
|
|
|
|
os << " retain counts.";
|
2008-04-18 09:32:44 +04:00
|
|
|
else
|
2008-05-05 21:53:17 +04:00
|
|
|
os << " retain count.";
|
2008-04-18 09:32:44 +04:00
|
|
|
}
|
2008-04-18 08:55:01 +04:00
|
|
|
|
|
|
|
Msg = os.str().c_str();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::Released:
|
|
|
|
Msg = "Object released.";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::ReturnedOwned:
|
2008-05-05 21:53:17 +04:00
|
|
|
Msg = "Object returned to caller as owning reference (single retain count"
|
|
|
|
" transferred to caller).";
|
2008-04-18 08:55:01 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RefVal::ReturnedNotOwned:
|
2008-05-05 21:53:17 +04:00
|
|
|
Msg = "Object returned to caller with a +0 (non-owning) retain count.";
|
2008-04-18 08:55:01 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stmt* S = cast<PostStmt>(N->getLocation()).getStmt();
|
|
|
|
FullSourceLoc Pos(S->getLocStart(), BR.getContext().getSourceManager());
|
|
|
|
PathDiagnosticPiece* P = new PathDiagnosticPiece(Pos, Msg);
|
|
|
|
|
|
|
|
// Add the range by scanning the children of the statement for any bindings
|
|
|
|
// to Sym.
|
|
|
|
|
2008-07-03 01:24:01 +04:00
|
|
|
ValueStateManager& VSM = cast<GRBugReporter>(BR).getStateManager();
|
2008-04-18 08:55:01 +04:00
|
|
|
|
|
|
|
for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
|
|
|
|
if (Expr* Exp = dyn_cast_or_null<Expr>(*I)) {
|
|
|
|
RVal X = VSM.GetRVal(CurrSt, Exp);
|
|
|
|
|
|
|
|
if (lval::SymbolVal* SV = dyn_cast<lval::SymbolVal>(&X))
|
|
|
|
if (SV->getSymbol() == Sym) {
|
|
|
|
P->addRange(Exp->getSourceRange()); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return P;
|
2008-04-18 07:39:05 +04:00
|
|
|
}
|
|
|
|
|
2008-05-16 22:33:44 +04:00
|
|
|
static std::pair<ExplodedNode<ValueState>*,VarDecl*>
|
|
|
|
GetAllocationSite(ExplodedNode<ValueState>* N, SymbolID Sym) {
|
2008-05-02 03:13:35 +04:00
|
|
|
|
2008-05-05 21:53:17 +04:00
|
|
|
typedef CFRefCount::RefBindings RefBindings;
|
2008-05-02 03:13:35 +04:00
|
|
|
ExplodedNode<ValueState>* Last = N;
|
2008-05-05 22:50:19 +04:00
|
|
|
|
2008-05-03 00:53:50 +04:00
|
|
|
// Find the first node that referred to the tracked symbol. We also
|
|
|
|
// try and find the first VarDecl the value was stored to.
|
|
|
|
|
|
|
|
VarDecl* FirstDecl = 0;
|
2008-05-16 22:33:44 +04:00
|
|
|
|
2008-05-02 03:13:35 +04:00
|
|
|
while (N) {
|
2008-07-11 02:03:41 +04:00
|
|
|
const ValueState* St = N->getState();
|
2008-05-02 03:13:35 +04:00
|
|
|
RefBindings B = RefBindings((RefBindings::TreeTy*) St->CheckerState);
|
2008-05-03 00:53:50 +04:00
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
if (!B.lookup(Sym))
|
2008-05-02 03:13:35 +04:00
|
|
|
break;
|
2008-05-16 22:33:44 +04:00
|
|
|
|
2008-05-03 00:53:50 +04:00
|
|
|
VarDecl* VD = 0;
|
|
|
|
|
|
|
|
// Determine if there is an LVal binding to the symbol.
|
|
|
|
for (ValueState::vb_iterator I=St->vb_begin(), E=St->vb_end(); I!=E; ++I) {
|
|
|
|
if (!isa<lval::SymbolVal>(I->second) // Is the value a symbol?
|
|
|
|
|| cast<lval::SymbolVal>(I->second).getSymbol() != Sym)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (VD) { // Multiple decls map to this symbol.
|
|
|
|
VD = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
VD = I->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VD) FirstDecl = VD;
|
2008-05-16 22:33:44 +04:00
|
|
|
|
2008-05-02 03:13:35 +04:00
|
|
|
Last = N;
|
|
|
|
N = N->pred_empty() ? NULL : *(N->pred_begin());
|
|
|
|
}
|
|
|
|
|
2008-05-16 22:33:44 +04:00
|
|
|
return std::make_pair(Last, FirstDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
PathDiagnosticPiece* CFRefReport::getEndPath(BugReporter& BR,
|
|
|
|
ExplodedNode<ValueState>* EndN) {
|
2008-05-23 03:45:19 +04:00
|
|
|
|
|
|
|
// Tell the BugReporter to report cases when the tracked symbol is
|
|
|
|
// assigned to different variables, etc.
|
2008-07-03 01:24:01 +04:00
|
|
|
cast<GRBugReporter>(BR).addNotableSymbol(Sym);
|
2008-05-02 03:13:35 +04:00
|
|
|
|
2008-05-16 22:33:44 +04:00
|
|
|
if (!getBugType().isLeak())
|
|
|
|
return RangedBugReport::getEndPath(BR, EndN);
|
|
|
|
|
|
|
|
typedef CFRefCount::RefBindings RefBindings;
|
|
|
|
|
|
|
|
// Get the retain count.
|
|
|
|
|
2008-07-07 20:21:19 +04:00
|
|
|
unsigned long RetCount =
|
|
|
|
CFRefCount::GetRefBindings(*EndN->getState()).lookup(Sym)->getCount();
|
|
|
|
|
2008-05-16 22:33:44 +04:00
|
|
|
// We are a leak. Walk up the graph to get to the first node where the
|
|
|
|
// symbol appeared, and also get the first VarDecl that tracked object
|
|
|
|
// is stored to.
|
|
|
|
|
|
|
|
ExplodedNode<ValueState>* AllocNode = 0;
|
|
|
|
VarDecl* FirstDecl = 0;
|
|
|
|
llvm::tie(AllocNode, FirstDecl) = GetAllocationSite(EndN, Sym);
|
|
|
|
|
|
|
|
// Get the allocate site.
|
|
|
|
assert (AllocNode);
|
|
|
|
Stmt* FirstStmt = cast<PostStmt>(AllocNode->getLocation()).getStmt();
|
2008-05-02 03:13:35 +04:00
|
|
|
|
2008-05-05 22:50:19 +04:00
|
|
|
SourceManager& SMgr = BR.getContext().getSourceManager();
|
|
|
|
unsigned AllocLine = SMgr.getLogicalLineNumber(FirstStmt->getLocStart());
|
|
|
|
|
|
|
|
// Get the leak site. We may have multiple ExplodedNodes (one with the
|
|
|
|
// leak) that occur on the same line number; if the node with the leak
|
|
|
|
// has any immediate predecessor nodes with the same line number, find
|
|
|
|
// any transitive-successors that have a different statement and use that
|
|
|
|
// line number instead. This avoids emiting a diagnostic like:
|
|
|
|
//
|
|
|
|
// // 'y' is leaked.
|
|
|
|
// int x = foo(y);
|
|
|
|
//
|
|
|
|
// instead we want:
|
|
|
|
//
|
|
|
|
// int x = foo(y);
|
|
|
|
// // 'y' is leaked.
|
|
|
|
|
|
|
|
Stmt* S = getStmt(BR); // This is the statement where the leak occured.
|
|
|
|
assert (S);
|
|
|
|
unsigned EndLine = SMgr.getLogicalLineNumber(S->getLocStart());
|
|
|
|
|
|
|
|
// Look in the *trimmed* graph at the immediate predecessor of EndN. Does
|
|
|
|
// it occur on the same line?
|
2008-05-07 03:07:13 +04:00
|
|
|
|
|
|
|
PathDiagnosticPiece::DisplayHint Hint = PathDiagnosticPiece::Above;
|
2008-05-05 22:50:19 +04:00
|
|
|
|
|
|
|
assert (!EndN->pred_empty()); // Not possible to have 0 predecessors.
|
2008-05-07 03:07:13 +04:00
|
|
|
ExplodedNode<ValueState> *Pred = *(EndN->pred_begin());
|
|
|
|
ProgramPoint PredPos = Pred->getLocation();
|
2008-05-05 22:50:19 +04:00
|
|
|
|
2008-05-07 03:07:13 +04:00
|
|
|
if (PostStmt* PredPS = dyn_cast<PostStmt>(&PredPos)) {
|
2008-05-05 22:50:19 +04:00
|
|
|
|
2008-05-07 03:07:13 +04:00
|
|
|
Stmt* SPred = PredPS->getStmt();
|
2008-05-05 22:50:19 +04:00
|
|
|
|
|
|
|
// Predecessor at same line?
|
2008-05-07 03:07:13 +04:00
|
|
|
if (SMgr.getLogicalLineNumber(SPred->getLocStart()) != EndLine) {
|
|
|
|
Hint = PathDiagnosticPiece::Below;
|
|
|
|
S = SPred;
|
|
|
|
}
|
2008-05-05 22:50:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the diagnostic.
|
2008-05-07 03:07:13 +04:00
|
|
|
FullSourceLoc L( S->getLocStart(), SMgr);
|
2008-05-02 03:13:35 +04:00
|
|
|
std::ostringstream os;
|
2008-05-03 00:53:50 +04:00
|
|
|
|
2008-05-05 22:50:19 +04:00
|
|
|
os << "Object allocated on line " << AllocLine;
|
2008-05-03 00:53:50 +04:00
|
|
|
|
|
|
|
if (FirstDecl)
|
|
|
|
os << " and stored into '" << FirstDecl->getName() << '\'';
|
|
|
|
|
2008-05-05 21:53:17 +04:00
|
|
|
os << " is no longer referenced after this point and has a retain count of +"
|
|
|
|
<< RetCount << " (object leaked).";
|
2008-05-02 03:13:35 +04:00
|
|
|
|
2008-05-07 03:07:13 +04:00
|
|
|
return new PathDiagnosticPiece(L, os.str(), Hint);
|
2008-05-02 03:13:35 +04:00
|
|
|
}
|
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
void UseAfterRelease::EmitWarnings(BugReporter& BR) {
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
for (CFRefCount::use_after_iterator I = TF.use_after_begin(),
|
|
|
|
E = TF.use_after_end(); I != E; ++I) {
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
CFRefReport report(*this, I->first, I->second.second);
|
|
|
|
report.addRange(I->second.first->getSourceRange());
|
2008-04-18 05:56:37 +04:00
|
|
|
BR.EmitWarning(report);
|
2008-04-09 05:10:13 +04:00
|
|
|
}
|
2008-04-10 03:49:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void BadRelease::EmitWarnings(BugReporter& BR) {
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-10 03:49:11 +04:00
|
|
|
for (CFRefCount::bad_release_iterator I = TF.bad_release_begin(),
|
|
|
|
E = TF.bad_release_end(); I != E; ++I) {
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
CFRefReport report(*this, I->first, I->second.second);
|
|
|
|
report.addRange(I->second.first->getSourceRange());
|
|
|
|
BR.EmitWarning(report);
|
2008-04-10 03:49:11 +04:00
|
|
|
}
|
|
|
|
}
|
2008-04-09 05:10:13 +04:00
|
|
|
|
2008-04-18 03:43:50 +04:00
|
|
|
void Leak::EmitWarnings(BugReporter& BR) {
|
|
|
|
|
|
|
|
for (CFRefCount::leaks_iterator I = TF.leaks_begin(),
|
|
|
|
E = TF.leaks_end(); I != E; ++I) {
|
|
|
|
|
2008-04-18 07:39:05 +04:00
|
|
|
std::vector<SymbolID>& SymV = *(I->second);
|
|
|
|
unsigned n = SymV.size();
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < n; ++i) {
|
|
|
|
CFRefReport report(*this, I->first, SymV[i]);
|
|
|
|
BR.EmitWarning(report);
|
|
|
|
}
|
2008-04-18 03:43:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-18 23:23:43 +04:00
|
|
|
void Leak::GetErrorNodes(std::vector<ExplodedNode<ValueState>*>& Nodes) {
|
|
|
|
for (CFRefCount::leaks_iterator I=TF.leaks_begin(), E=TF.leaks_end();
|
|
|
|
I!=E; ++I)
|
|
|
|
Nodes.push_back(I->first);
|
|
|
|
}
|
|
|
|
|
2008-05-16 22:33:44 +04:00
|
|
|
bool Leak::isCached(BugReport& R) {
|
|
|
|
|
|
|
|
// Most bug reports are cached at the location where they occured.
|
|
|
|
// With leaks, we want to unique them by the location where they were
|
|
|
|
// allocated, and only report only a single path.
|
|
|
|
|
|
|
|
SymbolID Sym = static_cast<CFRefReport&>(R).getSymbol();
|
|
|
|
|
|
|
|
ExplodedNode<ValueState>* AllocNode =
|
|
|
|
GetAllocationSite(R.getEndNode(), Sym).first;
|
|
|
|
|
|
|
|
if (!AllocNode)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return BugTypeCacheLocation::isCached(AllocNode->getLocation());
|
|
|
|
}
|
|
|
|
|
2008-03-11 09:39:11 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-04-11 02:16:52 +04:00
|
|
|
// Transfer function creation for external clients.
|
2008-03-11 09:39:11 +03:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-05-01 03:47:44 +04:00
|
|
|
GRTransferFuncs* clang::MakeCFRefCountTF(ASTContext& Ctx, bool GCEnabled,
|
|
|
|
const LangOptions& lopts) {
|
2008-07-22 20:21:24 +04:00
|
|
|
return new CFRefCount(Ctx, GCEnabled, lopts);
|
2008-04-11 02:58:08 +04:00
|
|
|
}
|