blob: 3926ce55aa256e38f93717f9d7bf705c5592e0da [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 +000020using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000021using namespace ento;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000022
Ted Kremeneka64e89b2010-01-27 06:13:48 +000023// 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
Douglas Gregor786dcd92011-07-06 16:00:34 +000036cocoa::NamingConvention cocoa::deriveNamingConvention(Selector S,
37 const ObjCMethodDecl *MD) {
38 switch (MD && MD->hasAttr<ObjCMethodFamilyAttr>()? MD->getMethodFamily()
39 : S.getMethodFamily()) {
John McCall85f3d762011-03-02 01:50:55 +000040 case OMF_None:
41 case OMF_autorelease:
42 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +000043 case OMF_finalize:
John McCall85f3d762011-03-02 01:50:55 +000044 case OMF_release:
45 case OMF_retain:
46 case OMF_retainCount:
Douglas Gregor926df6c2011-06-11 01:09:30 +000047 case OMF_self:
Fariborz Jahanian170579f2011-07-06 00:29:51 +000048 case OMF_performSelector:
Ted Kremeneka64e89b2010-01-27 06:13:48 +000049 return NoConvention;
50
John McCall85f3d762011-03-02 01:50:55 +000051 case OMF_init:
52 return InitRule;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000053
John McCall85f3d762011-03-02 01:50:55 +000054 case OMF_alloc:
55 case OMF_copy:
56 case OMF_mutableCopy:
57 case OMF_new:
58 return CreateRule;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000059 }
John McCall85f3d762011-03-02 01:50:55 +000060 llvm_unreachable("unexpected naming convention");
61 return NoConvention;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000062}
Ted Kremenek78acdbf2010-01-27 18:00:17 +000063
Chris Lattner5f9e2722011-07-23 10:55:15 +000064bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
65 StringRef Name) {
Ted Kremenek78acdbf2010-01-27 18:00:17 +000066 // Recursively walk the typedef stack, allowing typedefs of reference types.
John McCallf4c73712011-01-19 06:33:43 +000067 while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +000068 StringRef TDName = TD->getDecl()->getIdentifier()->getName();
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000069 if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
Ted Kremenek78acdbf2010-01-27 18:00:17 +000070 return true;
71
72 RetTy = TD->getDecl()->getUnderlyingType();
73 }
74
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000075 if (Name.empty())
Ted Kremenek78acdbf2010-01-27 18:00:17 +000076 return false;
77
78 // Is the type void*?
79 const PointerType* PT = RetTy->getAs<PointerType>();
80 if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
81 return false;
82
83 // Does the name start with the prefix?
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000084 return Name.startswith(Prefix);
Ted Kremenek78acdbf2010-01-27 18:00:17 +000085}
86
Ted Kremenek05560482011-07-16 19:50:32 +000087bool coreFoundation::isCFObjectRef(QualType T) {
88 return cocoa::isRefType(T, "CF") || // Core Foundation.
89 cocoa::isRefType(T, "CG") || // Core Graphics.
90 cocoa::isRefType(T, "DADisk") || // Disk Arbitration API.
91 cocoa::isRefType(T, "DADissenter") ||
92 cocoa::isRefType(T, "DASessionRef");
Ted Kremenek78acdbf2010-01-27 18:00:17 +000093}
94
95
96bool cocoa::isCocoaObjectRef(QualType Ty) {
97 if (!Ty->isObjCObjectPointerType())
98 return false;
99
100 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
101
102 // Can be true for objects with the 'NSObject' attribute.
103 if (!PT)
104 return true;
105
Ted Kremenek4019c4f2010-08-05 00:19:24 +0000106 // We assume that id<..>, id, Class, and Class<..> all represent tracked
107 // objects.
Ted Kremenek78acdbf2010-01-27 18:00:17 +0000108 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
Ted Kremenek4019c4f2010-08-05 00:19:24 +0000109 PT->isObjCClassType() || PT->isObjCQualifiedClassType())
Ted Kremenek78acdbf2010-01-27 18:00:17 +0000110 return true;
111
112 // Does the interface subclass NSObject?
113 // FIXME: We can memoize here if this gets too expensive.
114 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
115
116 // Assume that anything declared with a forward declaration and no
117 // @interface subclasses NSObject.
118 if (ID->isForwardDecl())
119 return true;
120
121 for ( ; ID ; ID = ID->getSuperClass())
122 if (ID->getIdentifier()->getName() == "NSObject")
123 return true;
124
125 return false;
126}
Ted Kremenek05560482011-07-16 19:50:32 +0000127
Chris Lattner5f9e2722011-07-23 10:55:15 +0000128bool coreFoundation::followsCreateRule(StringRef functionName) {
129 StringRef::iterator it = functionName.begin();
130 StringRef::iterator start = it;
131 StringRef::iterator endI = functionName.end();
Ted Kremenek797a7be2011-07-16 19:50:36 +0000132
133 while (true) {
134 // Scan for the start of 'create' or 'copy'.
135 for ( ; it != endI ; ++it) {
136 // Search for the first character. It can either be 'C' or 'c'.
137 char ch = *it;
138 if (ch == 'C' || ch == 'c') {
139 ++it;
140 break;
141 }
142 }
143
144 // Did we hit the end of the string? If so, we didn't find a match.
145 if (it == endI)
146 return false;
147
148 // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
149 // character.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000150 StringRef suffix = functionName.substr(it - start);
Ted Kremenek797a7be2011-07-16 19:50:36 +0000151 if (suffix.startswith("reate")) {
152 it += 5;
153 }
154 else if (suffix.startswith("opy")) {
155 it += 3;
Chad Rosier30601782011-08-17 23:08:45 +0000156 } else {
Ted Kremenek797a7be2011-07-16 19:50:36 +0000157 // Keep scanning.
158 continue;
159 }
160
161 if (it == endI || !islower(*it))
162 return true;
163
164 // If we matched a lowercase character, it isn't the end of the
165 // word. Keep scanning.
166 }
167
168 return false;
Ted Kremenek05560482011-07-16 19:50:32 +0000169}