CocoaConventions.cpp revision 3060178ad9df29789505c1e6debcfc80a3a13587
1//===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements cocoa naming convention analysis. 11// 12//===----------------------------------------------------------------------===// 13 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" 20using namespace clang; 21using namespace ento; 22 23// The "fundamental rule" for naming conventions of methods: 24// (url broken into two lines) 25// http://developer.apple.com/documentation/Cocoa/Conceptual/ 26// MemoryMgmt/Tasks/MemoryManagementRules.html 27// 28// "You take ownership of an object if you create it using a method whose name 29// begins with "alloc" or "new" or contains "copy" (for example, alloc, 30// newObject, or mutableCopy), or if you send it a retain message. You are 31// responsible for relinquishing ownership of objects you own using release 32// or autorelease. Any other time you receive an object, you must 33// not release it." 34// 35 36cocoa::NamingConvention cocoa::deriveNamingConvention(Selector S, 37 const ObjCMethodDecl *MD) { 38 switch (MD && MD->hasAttr<ObjCMethodFamilyAttr>()? MD->getMethodFamily() 39 : S.getMethodFamily()) { 40 case OMF_None: 41 case OMF_autorelease: 42 case OMF_dealloc: 43 case OMF_release: 44 case OMF_retain: 45 case OMF_retainCount: 46 case OMF_self: 47 case OMF_performSelector: 48 return NoConvention; 49 50 case OMF_init: 51 return InitRule; 52 53 case OMF_alloc: 54 case OMF_copy: 55 case OMF_mutableCopy: 56 case OMF_new: 57 return CreateRule; 58 } 59 llvm_unreachable("unexpected naming convention"); 60 return NoConvention; 61} 62 63bool cocoa::isRefType(QualType RetTy, StringRef Prefix, 64 StringRef Name) { 65 // Recursively walk the typedef stack, allowing typedefs of reference types. 66 while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) { 67 StringRef TDName = TD->getDecl()->getIdentifier()->getName(); 68 if (TDName.startswith(Prefix) && TDName.endswith("Ref")) 69 return true; 70 71 RetTy = TD->getDecl()->getUnderlyingType(); 72 } 73 74 if (Name.empty()) 75 return false; 76 77 // Is the type void*? 78 const PointerType* PT = RetTy->getAs<PointerType>(); 79 if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType())) 80 return false; 81 82 // Does the name start with the prefix? 83 return Name.startswith(Prefix); 84} 85 86bool coreFoundation::isCFObjectRef(QualType T) { 87 return cocoa::isRefType(T, "CF") || // Core Foundation. 88 cocoa::isRefType(T, "CG") || // Core Graphics. 89 cocoa::isRefType(T, "DADisk") || // Disk Arbitration API. 90 cocoa::isRefType(T, "DADissenter") || 91 cocoa::isRefType(T, "DASessionRef"); 92} 93 94 95bool cocoa::isCocoaObjectRef(QualType Ty) { 96 if (!Ty->isObjCObjectPointerType()) 97 return false; 98 99 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>(); 100 101 // Can be true for objects with the 'NSObject' attribute. 102 if (!PT) 103 return true; 104 105 // We assume that id<..>, id, Class, and Class<..> all represent tracked 106 // objects. 107 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() || 108 PT->isObjCClassType() || PT->isObjCQualifiedClassType()) 109 return true; 110 111 // Does the interface subclass NSObject? 112 // FIXME: We can memoize here if this gets too expensive. 113 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); 114 115 // Assume that anything declared with a forward declaration and no 116 // @interface subclasses NSObject. 117 if (ID->isForwardDecl()) 118 return true; 119 120 for ( ; ID ; ID = ID->getSuperClass()) 121 if (ID->getIdentifier()->getName() == "NSObject") 122 return true; 123 124 return false; 125} 126 127bool coreFoundation::followsCreateRule(StringRef functionName) { 128 StringRef::iterator it = functionName.begin(); 129 StringRef::iterator start = it; 130 StringRef::iterator endI = functionName.end(); 131 132 while (true) { 133 // Scan for the start of 'create' or 'copy'. 134 for ( ; it != endI ; ++it) { 135 // Search for the first character. It can either be 'C' or 'c'. 136 char ch = *it; 137 if (ch == 'C' || ch == 'c') { 138 ++it; 139 break; 140 } 141 } 142 143 // Did we hit the end of the string? If so, we didn't find a match. 144 if (it == endI) 145 return false; 146 147 // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase 148 // character. 149 StringRef suffix = functionName.substr(it - start); 150 if (suffix.startswith("reate")) { 151 it += 5; 152 } 153 else if (suffix.startswith("opy")) { 154 it += 3; 155 } else { 156 // Keep scanning. 157 continue; 158 } 159 160 if (it == endI || !islower(*it)) 161 return true; 162 163 // If we matched a lowercase character, it isn't the end of the 164 // word. Keep scanning. 165 } 166 167 return false; 168} 169