1*67e74705SXin Li //===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This file implements cocoa naming convention analysis.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li
14*67e74705SXin Li #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
15*67e74705SXin Li #include "clang/AST/Decl.h"
16*67e74705SXin Li #include "clang/AST/DeclObjC.h"
17*67e74705SXin Li #include "clang/AST/Type.h"
18*67e74705SXin Li #include "clang/Basic/CharInfo.h"
19*67e74705SXin Li #include "llvm/ADT/StringExtras.h"
20*67e74705SXin Li #include "llvm/Support/ErrorHandling.h"
21*67e74705SXin Li
22*67e74705SXin Li using namespace clang;
23*67e74705SXin Li using namespace ento;
24*67e74705SXin Li
isRefType(QualType RetTy,StringRef Prefix,StringRef Name)25*67e74705SXin Li bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
26*67e74705SXin Li StringRef Name) {
27*67e74705SXin Li // Recursively walk the typedef stack, allowing typedefs of reference types.
28*67e74705SXin Li while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
29*67e74705SXin Li StringRef TDName = TD->getDecl()->getIdentifier()->getName();
30*67e74705SXin Li if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
31*67e74705SXin Li return true;
32*67e74705SXin Li // XPC unfortunately uses CF-style function names, but aren't CF types.
33*67e74705SXin Li if (TDName.startswith("xpc_"))
34*67e74705SXin Li return false;
35*67e74705SXin Li RetTy = TD->getDecl()->getUnderlyingType();
36*67e74705SXin Li }
37*67e74705SXin Li
38*67e74705SXin Li if (Name.empty())
39*67e74705SXin Li return false;
40*67e74705SXin Li
41*67e74705SXin Li // Is the type void*?
42*67e74705SXin Li const PointerType* PT = RetTy->getAs<PointerType>();
43*67e74705SXin Li if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
44*67e74705SXin Li return false;
45*67e74705SXin Li
46*67e74705SXin Li // Does the name start with the prefix?
47*67e74705SXin Li return Name.startswith(Prefix);
48*67e74705SXin Li }
49*67e74705SXin Li
isCFObjectRef(QualType T)50*67e74705SXin Li bool coreFoundation::isCFObjectRef(QualType T) {
51*67e74705SXin Li return cocoa::isRefType(T, "CF") || // Core Foundation.
52*67e74705SXin Li cocoa::isRefType(T, "CG") || // Core Graphics.
53*67e74705SXin Li cocoa::isRefType(T, "DADisk") || // Disk Arbitration API.
54*67e74705SXin Li cocoa::isRefType(T, "DADissenter") ||
55*67e74705SXin Li cocoa::isRefType(T, "DASessionRef");
56*67e74705SXin Li }
57*67e74705SXin Li
58*67e74705SXin Li
isCocoaObjectRef(QualType Ty)59*67e74705SXin Li bool cocoa::isCocoaObjectRef(QualType Ty) {
60*67e74705SXin Li if (!Ty->isObjCObjectPointerType())
61*67e74705SXin Li return false;
62*67e74705SXin Li
63*67e74705SXin Li const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
64*67e74705SXin Li
65*67e74705SXin Li // Can be true for objects with the 'NSObject' attribute.
66*67e74705SXin Li if (!PT)
67*67e74705SXin Li return true;
68*67e74705SXin Li
69*67e74705SXin Li // We assume that id<..>, id, Class, and Class<..> all represent tracked
70*67e74705SXin Li // objects.
71*67e74705SXin Li if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
72*67e74705SXin Li PT->isObjCClassType() || PT->isObjCQualifiedClassType())
73*67e74705SXin Li return true;
74*67e74705SXin Li
75*67e74705SXin Li // Does the interface subclass NSObject?
76*67e74705SXin Li // FIXME: We can memoize here if this gets too expensive.
77*67e74705SXin Li const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
78*67e74705SXin Li
79*67e74705SXin Li // Assume that anything declared with a forward declaration and no
80*67e74705SXin Li // @interface subclasses NSObject.
81*67e74705SXin Li if (!ID->hasDefinition())
82*67e74705SXin Li return true;
83*67e74705SXin Li
84*67e74705SXin Li for ( ; ID ; ID = ID->getSuperClass())
85*67e74705SXin Li if (ID->getIdentifier()->getName() == "NSObject")
86*67e74705SXin Li return true;
87*67e74705SXin Li
88*67e74705SXin Li return false;
89*67e74705SXin Li }
90*67e74705SXin Li
followsCreateRule(const FunctionDecl * fn)91*67e74705SXin Li bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
92*67e74705SXin Li // For now, *just* base this on the function name, not on anything else.
93*67e74705SXin Li
94*67e74705SXin Li const IdentifierInfo *ident = fn->getIdentifier();
95*67e74705SXin Li if (!ident) return false;
96*67e74705SXin Li StringRef functionName = ident->getName();
97*67e74705SXin Li
98*67e74705SXin Li StringRef::iterator it = functionName.begin();
99*67e74705SXin Li StringRef::iterator start = it;
100*67e74705SXin Li StringRef::iterator endI = functionName.end();
101*67e74705SXin Li
102*67e74705SXin Li while (true) {
103*67e74705SXin Li // Scan for the start of 'create' or 'copy'.
104*67e74705SXin Li for ( ; it != endI ; ++it) {
105*67e74705SXin Li // Search for the first character. It can either be 'C' or 'c'.
106*67e74705SXin Li char ch = *it;
107*67e74705SXin Li if (ch == 'C' || ch == 'c') {
108*67e74705SXin Li // Make sure this isn't something like 'recreate' or 'Scopy'.
109*67e74705SXin Li if (ch == 'c' && it != start && isLetter(*(it - 1)))
110*67e74705SXin Li continue;
111*67e74705SXin Li
112*67e74705SXin Li ++it;
113*67e74705SXin Li break;
114*67e74705SXin Li }
115*67e74705SXin Li }
116*67e74705SXin Li
117*67e74705SXin Li // Did we hit the end of the string? If so, we didn't find a match.
118*67e74705SXin Li if (it == endI)
119*67e74705SXin Li return false;
120*67e74705SXin Li
121*67e74705SXin Li // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
122*67e74705SXin Li // character.
123*67e74705SXin Li StringRef suffix = functionName.substr(it - start);
124*67e74705SXin Li if (suffix.startswith("reate")) {
125*67e74705SXin Li it += 5;
126*67e74705SXin Li }
127*67e74705SXin Li else if (suffix.startswith("opy")) {
128*67e74705SXin Li it += 3;
129*67e74705SXin Li } else {
130*67e74705SXin Li // Keep scanning.
131*67e74705SXin Li continue;
132*67e74705SXin Li }
133*67e74705SXin Li
134*67e74705SXin Li if (it == endI || !isLowercase(*it))
135*67e74705SXin Li return true;
136*67e74705SXin Li
137*67e74705SXin Li // If we matched a lowercase character, it isn't the end of the
138*67e74705SXin Li // word. Keep scanning.
139*67e74705SXin Li }
140*67e74705SXin Li }
141