2010-01-27 14:13:48 +08:00
|
|
|
//===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2011-06-17 07:19:36 +08:00
|
|
|
// This file implements cocoa naming convention analysis.
|
2010-01-27 14:13:48 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-12-17 13:21:58 +08:00
|
|
|
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
|
2010-01-28 02:00:17 +08:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2010-01-27 14:13:48 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2011-03-02 09:50:55 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-01-27 14:13:48 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-01-27 14:13:48 +08:00
|
|
|
|
|
|
|
using llvm::StringRef;
|
|
|
|
|
|
|
|
// The "fundamental rule" for naming conventions of methods:
|
|
|
|
// (url broken into two lines)
|
|
|
|
// http://developer.apple.com/documentation/Cocoa/Conceptual/
|
|
|
|
// MemoryMgmt/Tasks/MemoryManagementRules.html
|
|
|
|
//
|
|
|
|
// "You take ownership of an object if you create it using a method whose name
|
|
|
|
// begins with "alloc" or "new" or contains "copy" (for example, alloc,
|
|
|
|
// newObject, or mutableCopy), or if you send it a retain message. You are
|
|
|
|
// responsible for relinquishing ownership of objects you own using release
|
|
|
|
// or autorelease. Any other time you receive an object, you must
|
|
|
|
// not release it."
|
|
|
|
//
|
|
|
|
|
2011-07-07 00:00:34 +08:00
|
|
|
cocoa::NamingConvention cocoa::deriveNamingConvention(Selector S,
|
|
|
|
const ObjCMethodDecl *MD) {
|
|
|
|
switch (MD && MD->hasAttr<ObjCMethodFamilyAttr>()? MD->getMethodFamily()
|
|
|
|
: S.getMethodFamily()) {
|
2011-03-02 09:50:55 +08:00
|
|
|
case OMF_None:
|
|
|
|
case OMF_autorelease:
|
|
|
|
case OMF_dealloc:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_retain:
|
|
|
|
case OMF_retainCount:
|
2011-06-11 09:09:30 +08:00
|
|
|
case OMF_self:
|
2011-07-06 08:29:51 +08:00
|
|
|
case OMF_performSelector:
|
2011-01-12 03:45:16 +08:00
|
|
|
return NoConvention;
|
|
|
|
|
2011-03-02 09:50:55 +08:00
|
|
|
case OMF_init:
|
|
|
|
return InitRule;
|
2010-01-27 14:13:48 +08:00
|
|
|
|
2011-03-02 09:50:55 +08:00
|
|
|
case OMF_alloc:
|
|
|
|
case OMF_copy:
|
|
|
|
case OMF_mutableCopy:
|
|
|
|
case OMF_new:
|
|
|
|
return CreateRule;
|
2010-01-27 14:13:48 +08:00
|
|
|
}
|
2011-03-02 09:50:55 +08:00
|
|
|
llvm_unreachable("unexpected naming convention");
|
|
|
|
return NoConvention;
|
2010-01-27 14:13:48 +08:00
|
|
|
}
|
2010-01-28 02:00:17 +08:00
|
|
|
|
2010-02-09 02:38:55 +08:00
|
|
|
bool cocoa::isRefType(QualType RetTy, llvm::StringRef Prefix,
|
|
|
|
llvm::StringRef Name) {
|
2010-01-28 02:00:17 +08:00
|
|
|
// Recursively walk the typedef stack, allowing typedefs of reference types.
|
2011-01-19 14:33:43 +08:00
|
|
|
while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
|
2010-01-28 02:00:17 +08:00
|
|
|
llvm::StringRef TDName = TD->getDecl()->getIdentifier()->getName();
|
2010-02-09 02:38:55 +08:00
|
|
|
if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
|
2010-01-28 02:00:17 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
RetTy = TD->getDecl()->getUnderlyingType();
|
|
|
|
}
|
|
|
|
|
2010-02-09 02:38:55 +08:00
|
|
|
if (Name.empty())
|
2010-01-28 02:00:17 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Is the type void*?
|
|
|
|
const PointerType* PT = RetTy->getAs<PointerType>();
|
|
|
|
if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Does the name start with the prefix?
|
2010-02-09 02:38:55 +08:00
|
|
|
return Name.startswith(Prefix);
|
2010-01-28 02:00:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cocoa::isCFObjectRef(QualType T) {
|
|
|
|
return isRefType(T, "CF") || // Core Foundation.
|
|
|
|
isRefType(T, "CG") || // Core Graphics.
|
|
|
|
isRefType(T, "DADisk") || // Disk Arbitration API.
|
|
|
|
isRefType(T, "DADissenter") ||
|
|
|
|
isRefType(T, "DASessionRef");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool cocoa::isCocoaObjectRef(QualType Ty) {
|
|
|
|
if (!Ty->isObjCObjectPointerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
|
|
|
|
|
|
|
|
// Can be true for objects with the 'NSObject' attribute.
|
|
|
|
if (!PT)
|
|
|
|
return true;
|
|
|
|
|
2010-08-05 08:19:24 +08:00
|
|
|
// We assume that id<..>, id, Class, and Class<..> all represent tracked
|
|
|
|
// objects.
|
2010-01-28 02:00:17 +08:00
|
|
|
if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
|
2010-08-05 08:19:24 +08:00
|
|
|
PT->isObjCClassType() || PT->isObjCQualifiedClassType())
|
2010-01-28 02:00:17 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Does the interface subclass NSObject?
|
|
|
|
// FIXME: We can memoize here if this gets too expensive.
|
|
|
|
const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
|
|
|
|
|
|
|
|
// Assume that anything declared with a forward declaration and no
|
|
|
|
// @interface subclasses NSObject.
|
|
|
|
if (ID->isForwardDecl())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for ( ; ID ; ID = ID->getSuperClass())
|
|
|
|
if (ID->getIdentifier()->getName() == "NSObject")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|