1 //===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements cocoa naming convention analysis.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
15 #include "clang/AST/Type.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/ErrorHandling.h"
21 using namespace clang;
24 using llvm::StringRef;
26 // The "fundamental rule" for naming conventions of methods:
27 // (url broken into two lines)
28 // http://developer.apple.com/documentation/Cocoa/Conceptual/
29 // MemoryMgmt/Tasks/MemoryManagementRules.html
31 // "You take ownership of an object if you create it using a method whose name
32 // begins with "alloc" or "new" or contains "copy" (for example, alloc,
33 // newObject, or mutableCopy), or if you send it a retain message. You are
34 // responsible for relinquishing ownership of objects you own using release
35 // or autorelease. Any other time you receive an object, you must
39 cocoa::NamingConvention cocoa::deriveNamingConvention(Selector S,
40 const ObjCMethodDecl *MD) {
41 switch (MD && MD->hasAttr<ObjCMethodFamilyAttr>()? MD->getMethodFamily()
42 : S.getMethodFamily()) {
50 case OMF_performSelector:
62 llvm_unreachable("unexpected naming convention");
66 bool cocoa::isRefType(QualType RetTy, llvm::StringRef Prefix,
67 llvm::StringRef Name) {
68 // Recursively walk the typedef stack, allowing typedefs of reference types.
69 while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
70 llvm::StringRef TDName = TD->getDecl()->getIdentifier()->getName();
71 if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
74 RetTy = TD->getDecl()->getUnderlyingType();
81 const PointerType* PT = RetTy->getAs<PointerType>();
82 if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
85 // Does the name start with the prefix?
86 return Name.startswith(Prefix);
89 bool coreFoundation::isCFObjectRef(QualType T) {
90 return cocoa::isRefType(T, "CF") || // Core Foundation.
91 cocoa::isRefType(T, "CG") || // Core Graphics.
92 cocoa::isRefType(T, "DADisk") || // Disk Arbitration API.
93 cocoa::isRefType(T, "DADissenter") ||
94 cocoa::isRefType(T, "DASessionRef");
98 bool cocoa::isCocoaObjectRef(QualType Ty) {
99 if (!Ty->isObjCObjectPointerType())
102 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
104 // Can be true for objects with the 'NSObject' attribute.
108 // We assume that id<..>, id, Class, and Class<..> all represent tracked
110 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
111 PT->isObjCClassType() || PT->isObjCQualifiedClassType())
114 // Does the interface subclass NSObject?
115 // FIXME: We can memoize here if this gets too expensive.
116 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
118 // Assume that anything declared with a forward declaration and no
119 // @interface subclasses NSObject.
120 if (ID->isForwardDecl())
123 for ( ; ID ; ID = ID->getSuperClass())
124 if (ID->getIdentifier()->getName() == "NSObject")
130 bool coreFoundation::followsCreateRule(llvm::StringRef functionName) {
131 llvm::StringRef::iterator it = functionName.begin();
132 llvm::StringRef::iterator start = it;
133 llvm::StringRef::iterator endI = functionName.end();
136 // Scan for the start of 'create' or 'copy'.
137 for ( ; it != endI ; ++it) {
138 // Search for the first character. It can either be 'C' or 'c'.
140 if (ch == 'C' || ch == 'c') {
146 // Did we hit the end of the string? If so, we didn't find a match.
150 // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
152 llvm::StringRef suffix = functionName.substr(it - start);
153 if (suffix.startswith("reate")) {
156 else if (suffix.startswith("opy")) {
164 if (it == endI || !islower(*it))
167 // If we matched a lowercase character, it isn't the end of the
168 // word. Keep scanning.