blob: 4c62f36e6c929ad714def3bebac995c28261b8c1 [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//
10// This file defines
11//
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
John McCall85f3d762011-03-02 01:50:55 +000039cocoa::NamingConvention cocoa::deriveNamingConvention(Selector S) {
40 switch (S.getMethodFamily()) {
41 case OMF_None:
42 case OMF_autorelease:
43 case OMF_dealloc:
44 case OMF_release:
45 case OMF_retain:
46 case OMF_retainCount:
Ted Kremeneka64e89b2010-01-27 06:13:48 +000047 return NoConvention;
48
John McCall85f3d762011-03-02 01:50:55 +000049 case OMF_init:
50 return InitRule;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000051
John McCall85f3d762011-03-02 01:50:55 +000052 case OMF_alloc:
53 case OMF_copy:
54 case OMF_mutableCopy:
55 case OMF_new:
56 return CreateRule;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000057 }
John McCall85f3d762011-03-02 01:50:55 +000058 llvm_unreachable("unexpected naming convention");
59 return NoConvention;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000060}
Ted Kremenek78acdbf2010-01-27 18:00:17 +000061
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000062bool cocoa::isRefType(QualType RetTy, llvm::StringRef Prefix,
63 llvm::StringRef Name) {
Ted Kremenek78acdbf2010-01-27 18:00:17 +000064 // Recursively walk the typedef stack, allowing typedefs of reference types.
John McCallf4c73712011-01-19 06:33:43 +000065 while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
Ted Kremenek78acdbf2010-01-27 18:00:17 +000066 llvm::StringRef TDName = TD->getDecl()->getIdentifier()->getName();
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000067 if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
Ted Kremenek78acdbf2010-01-27 18:00:17 +000068 return true;
69
70 RetTy = TD->getDecl()->getUnderlyingType();
71 }
72
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000073 if (Name.empty())
Ted Kremenek78acdbf2010-01-27 18:00:17 +000074 return false;
75
76 // Is the type void*?
77 const PointerType* PT = RetTy->getAs<PointerType>();
78 if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
79 return false;
80
81 // Does the name start with the prefix?
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000082 return Name.startswith(Prefix);
Ted Kremenek78acdbf2010-01-27 18:00:17 +000083}
84
85bool cocoa::isCFObjectRef(QualType T) {
86 return isRefType(T, "CF") || // Core Foundation.
87 isRefType(T, "CG") || // Core Graphics.
88 isRefType(T, "DADisk") || // Disk Arbitration API.
89 isRefType(T, "DADissenter") ||
90 isRefType(T, "DASessionRef");
91}
92
93
94bool cocoa::isCocoaObjectRef(QualType Ty) {
95 if (!Ty->isObjCObjectPointerType())
96 return false;
97
98 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
99
100 // Can be true for objects with the 'NSObject' attribute.
101 if (!PT)
102 return true;
103
Ted Kremenek4019c4f2010-08-05 00:19:24 +0000104 // We assume that id<..>, id, Class, and Class<..> all represent tracked
105 // objects.
Ted Kremenek78acdbf2010-01-27 18:00:17 +0000106 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
Ted Kremenek4019c4f2010-08-05 00:19:24 +0000107 PT->isObjCClassType() || PT->isObjCQualifiedClassType())
Ted Kremenek78acdbf2010-01-27 18:00:17 +0000108 return true;
109
110 // Does the interface subclass NSObject?
111 // FIXME: We can memoize here if this gets too expensive.
112 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
113
114 // Assume that anything declared with a forward declaration and no
115 // @interface subclasses NSObject.
116 if (ID->isForwardDecl())
117 return true;
118
119 for ( ; ID ; ID = ID->getSuperClass())
120 if (ID->getIdentifier()->getName() == "NSObject")
121 return true;
122
123 return false;
124}