Expand the CF retain checker to allow the Create/Get rule to apply to any

function that returns a CFxxxRef, not just functions whose name begins with
CF.  This implements <rdar://problem/5917879>.

Added test case for this feature.

Updated calls to CStrInCStrNoCase to swap their arguments, per compatibility
with strcasestr.

llvm-svn: 50829
This commit is contained in:
Ted Kremenek 2008-05-07 20:06:41 +00:00
parent dac0391218
commit f958ec50c0
2 changed files with 70 additions and 52 deletions

View File

@ -45,6 +45,49 @@ static inline Selector GetUnarySelector(const char* name, ASTContext& Ctx) {
return Ctx.Selectors.getSelector(1, &II);
}
static bool isCFRefType(QualType T) {
if (!T->isPointerType())
return false;
// Check the typedef for the name "CF" 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] != 'F')
return false;
if (strstr(TDName, "Ref") == 0)
return false;
return true;
}
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;
}
//===----------------------------------------------------------------------===//
// Symbolic Evaluation of Reference Counting Logic
//===----------------------------------------------------------------------===//
@ -383,7 +426,11 @@ RetainSummary* RetainSummaryManager::getSummary(FunctionDecl* FD,
RetainSummary *S = 0;
if (FName[0] == 'C' && FName[1] == 'F')
FunctionType* FT = dyn_cast<FunctionType>(FD->getType());
if (FT && isCFRefType(FT->getResultType()))
S = getCFSummary(FD, FName);
else if (FName[0] == 'C' && FName[1] == 'F')
S = getCFSummary(FD, FName);
else if (FName[0] == 'N' && FName[1] == 'S')
S = getNSSummary(FD, FName);
@ -405,7 +452,8 @@ RetainSummary* RetainSummaryManager::getNSSummary(FunctionDecl* FD,
RetainSummary* RetainSummaryManager::getCFSummary(FunctionDecl* FD,
const char* FName) {
FName += 2;
if (FName[0] == 'C' && FName[1] == 'F')
FName += 2;
if (strcmp(FName, "Retain") == 0)
return getUnarySummary(FD, cfretain);
@ -470,53 +518,10 @@ RetainSummaryManager::getUnarySummary(FunctionDecl* FD, UnaryFuncKind func) {
}
}
static bool isCFRefType(QualType T) {
if (!T->isPointerType())
return false;
// Check the typedef for the name "CF" 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] != 'F')
return false;
if (strstr(TDName, "Ref") == 0)
return false;
return true;
}
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;
}
RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
FunctionTypeProto* FT =
dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
FunctionType* FT =
dyn_cast<FunctionType>(FD->getType().getTypePtr());
if (FT && !isCFRefType(FT->getResultType()))
return 0;
@ -530,8 +535,8 @@ RetainSummary* RetainSummaryManager::getCFSummaryCreateRule(FunctionDecl* FD) {
RetainSummary* RetainSummaryManager::getCFSummaryGetRule(FunctionDecl* FD) {
FunctionTypeProto* FT =
dyn_cast<FunctionTypeProto>(FD->getType().getTypePtr());
FunctionType* FT =
dyn_cast<FunctionType>(FD->getType().getTypePtr());
if (FT) {
QualType RetTy = FT->getResultType();
@ -606,8 +611,8 @@ RetainSummaryManager::getMethodSummary(ObjCMessageExpr* ME) {
if (!isNSType(ME->getReceiver()->getType()))
return 0;
if (CStrInCStrNoCase("create", s) || CStrInCStrNoCase("copy", s) ||
CStrInCStrNoCase("new", s)) {
if (CStrInCStrNoCase(s, "create") || CStrInCStrNoCase(s, "copy") ||
CStrInCStrNoCase(s, "new")) {
RetEffect E = isGCEnabled() ? RetEffect::MakeNoRet()
: RetEffect::MakeOwned();

View File

@ -94,3 +94,16 @@ CFDateRef f7() {
date = CFDateCreate(NULL, CFAbsoluteTimeGetCurrent());
return date;
}
// Generalization of Create rule. MyDateCreate returns a CFXXXTypeRef, and
// has the word create.
CFDateRef MyDateCreate();
CFDateRef f8() {
CFDateRef date = MyDateCreate();
CFRetain(date);
return date; // expected-warning{{leak}}
}