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