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