blob: 428032bee547079c1e317651f98893ce357076f0 [file] [log] [blame]
Ted Kremeneka64e89b2010-01-27 06:13:48 +00001//===- 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//
Francois Pichet69701552011-06-16 23:19:36 +000010// This file implements cocoa naming convention analysis.
Ted Kremeneka64e89b2010-01-27 06:13:48 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekbb8fef32010-12-17 05:21:58 +000014#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
Ted Kremenek78acdbf2010-01-27 18:00:17 +000015#include "clang/AST/Type.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclObjC.h"
Ted Kremeneka64e89b2010-01-27 06:13:48 +000018#include "llvm/ADT/StringExtras.h"
John McCall85f3d762011-03-02 01:50:55 +000019#include "llvm/Support/ErrorHandling.h"
Ted Kremeneka64e89b2010-01-27 06:13:48 +000020
21using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000022using namespace ento;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000023
24using llvm::StringRef;
25
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
30//
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
36// not release it."
37//
38
Douglas Gregor786dcd92011-07-06 16:00:34 +000039cocoa::NamingConvention cocoa::deriveNamingConvention(Selector S,
40 const ObjCMethodDecl *MD) {
41 switch (MD && MD->hasAttr<ObjCMethodFamilyAttr>()? MD->getMethodFamily()
42 : S.getMethodFamily()) {
John McCall85f3d762011-03-02 01:50:55 +000043 case OMF_None:
44 case OMF_autorelease:
45 case OMF_dealloc:
46 case OMF_release:
47 case OMF_retain:
48 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +000049 case OMF_self:
Fariborz Jahanian170579f2011-07-06 00:29:51 +000050 case OMF_performSelector:
Ted Kremeneka64e89b2010-01-27 06:13:48 +000051 return NoConvention;
52
John McCall85f3d762011-03-02 01:50:55 +000053 case OMF_init:
54 return InitRule;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000055
John McCall85f3d762011-03-02 01:50:55 +000056 case OMF_alloc:
57 case OMF_copy:
58 case OMF_mutableCopy:
59 case OMF_new:
60 return CreateRule;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000061 }
John McCall85f3d762011-03-02 01:50:55 +000062 llvm_unreachable("unexpected naming convention");
63 return NoConvention;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000064}
Ted Kremenek78acdbf2010-01-27 18:00:17 +000065
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000066bool cocoa::isRefType(QualType RetTy, llvm::StringRef Prefix,
67 llvm::StringRef Name) {
Ted Kremenek78acdbf2010-01-27 18:00:17 +000068 // Recursively walk the typedef stack, allowing typedefs of reference types.
John McCallf4c73712011-01-19 06:33:43 +000069 while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
Ted Kremenek78acdbf2010-01-27 18:00:17 +000070 llvm::StringRef TDName = TD->getDecl()->getIdentifier()->getName();
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000071 if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
Ted Kremenek78acdbf2010-01-27 18:00:17 +000072 return true;
73
74 RetTy = TD->getDecl()->getUnderlyingType();
75 }
76
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000077 if (Name.empty())
Ted Kremenek78acdbf2010-01-27 18:00:17 +000078 return false;
79
80 // Is the type void*?
81 const PointerType* PT = RetTy->getAs<PointerType>();
82 if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
83 return false;
84
85 // Does the name start with the prefix?
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000086 return Name.startswith(Prefix);
Ted Kremenek78acdbf2010-01-27 18:00:17 +000087}
88
Ted Kremenek05560482011-07-16 19:50:32 +000089bool 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");
Ted Kremenek78acdbf2010-01-27 18:00:17 +000095}
96
97
98bool cocoa::isCocoaObjectRef(QualType Ty) {
99 if (!Ty->isObjCObjectPointerType())
100 return false;
101
102 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
103
104 // Can be true for objects with the 'NSObject' attribute.
105 if (!PT)
106 return true;
107
Ted Kremenek4019c4f2010-08-05 00:19:24 +0000108 // We assume that id<..>, id, Class, and Class<..> all represent tracked
109 // objects.
Ted Kremenek78acdbf2010-01-27 18:00:17 +0000110 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
Ted Kremenek4019c4f2010-08-05 00:19:24 +0000111 PT->isObjCClassType() || PT->isObjCQualifiedClassType())
Ted Kremenek78acdbf2010-01-27 18:00:17 +0000112 return true;
113
114 // Does the interface subclass NSObject?
115 // FIXME: We can memoize here if this gets too expensive.
116 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
117
118 // Assume that anything declared with a forward declaration and no
119 // @interface subclasses NSObject.
120 if (ID->isForwardDecl())
121 return true;
122
123 for ( ; ID ; ID = ID->getSuperClass())
124 if (ID->getIdentifier()->getName() == "NSObject")
125 return true;
126
127 return false;
128}
Ted Kremenek05560482011-07-16 19:50:32 +0000129
130bool coreFoundation::followsCreateRule(llvm::StringRef functionName) {
131 return functionName.find("Create") != StringRef::npos ||
132 functionName.find("Copy") != StringRef::npos;
133}