clang 17.0.0git
CocoaConventions.cpp
Go to the documentation of this file.
1//===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements cocoa naming convention analysis.
10//
11//===----------------------------------------------------------------------===//
12
14#include "clang/AST/Decl.h"
15#include "clang/AST/DeclObjC.h"
16#include "clang/AST/Type.h"
18#include "llvm/ADT/StringExtras.h"
19#include "llvm/Support/ErrorHandling.h"
20
21using namespace clang;
22using namespace ento;
23
24bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
25 StringRef Name) {
26 // Recursively walk the typedef stack, allowing typedefs of reference types.
27 while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
28 StringRef TDName = TD->getDecl()->getIdentifier()->getName();
29 if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
30 return true;
31 // XPC unfortunately uses CF-style function names, but aren't CF types.
32 if (TDName.startswith("xpc_"))
33 return false;
34 RetTy = TD->getDecl()->getUnderlyingType();
35 }
36
37 if (Name.empty())
38 return false;
39
40 // Is the type void*?
41 const PointerType* PT = RetTy->castAs<PointerType>();
42 if (!PT || !PT->getPointeeType().getUnqualifiedType()->isVoidType())
43 return false;
44
45 // Does the name start with the prefix?
46 return Name.startswith(Prefix);
47}
48
49/// Returns true when the passed-in type is a CF-style reference-counted
50/// type from the DiskArbitration framework.
52 return cocoa::isRefType(T, "DADisk") ||
53 cocoa::isRefType(T, "DADissenter") ||
54 cocoa::isRefType(T, "DASessionRef");
55}
56
58 return cocoa::isRefType(T, "CF") || // Core Foundation.
59 cocoa::isRefType(T, "CG") || // Core Graphics.
60 cocoa::isRefType(T, "CM") || // Core Media.
62}
63
64
66 if (!Ty->isObjCObjectPointerType())
67 return false;
68
70
71 // Can be true for objects with the 'NSObject' attribute.
72 if (!PT)
73 return true;
74
75 // We assume that id<..>, id, Class, and Class<..> all represent tracked
76 // objects.
77 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
79 return true;
80
81 // Does the interface subclass NSObject?
82 // FIXME: We can memoize here if this gets too expensive.
83 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
84
85 // Assume that anything declared with a forward declaration and no
86 // @interface subclasses NSObject.
87 if (!ID->hasDefinition())
88 return true;
89
90 for ( ; ID ; ID = ID->getSuperClass())
91 if (ID->getIdentifier()->getName() == "NSObject")
92 return true;
93
94 return false;
95}
96
98 // For now, *just* base this on the function name, not on anything else.
99
100 const IdentifierInfo *ident = fn->getIdentifier();
101 if (!ident) return false;
102 StringRef functionName = ident->getName();
103
104 StringRef::iterator it = functionName.begin();
105 StringRef::iterator start = it;
106 StringRef::iterator endI = functionName.end();
107
108 while (true) {
109 // Scan for the start of 'create' or 'copy'.
110 for ( ; it != endI ; ++it) {
111 // Search for the first character. It can either be 'C' or 'c'.
112 char ch = *it;
113 if (ch == 'C' || ch == 'c') {
114 // Make sure this isn't something like 'recreate' or 'Scopy'.
115 if (ch == 'c' && it != start && isLetter(*(it - 1)))
116 continue;
117
118 ++it;
119 break;
120 }
121 }
122
123 // Did we hit the end of the string? If so, we didn't find a match.
124 if (it == endI)
125 return false;
126
127 // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
128 // character.
129 StringRef suffix = functionName.substr(it - start);
130 if (suffix.startswith("reate")) {
131 it += 5;
132 }
133 else if (suffix.startswith("opy")) {
134 it += 3;
135 } else {
136 // Keep scanning.
137 continue;
138 }
139
140 if (it == endI || !isLowercase(*it))
141 return true;
142
143 // If we matched a lowercase character, it isn't the end of the
144 // word. Keep scanning.
145 }
146}
static bool isDiskArbitrationAPIRefType(QualType T)
Returns true when the passed-in type is a CF-style reference-counted type from the DiskArbitration fr...
C Language Family Type Representation.
Represents a function declaration or definition.
Definition: Decl.h:1917
One of these records is kept for each identifier that is lexed.
StringRef getName() const
Return the actual identifier string.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition: Decl.h:268
Represents an ObjC class declaration.
Definition: DeclObjC.h:1147
Represents a pointer to an Objective C object.
Definition: Type.h:6312
bool isObjCQualifiedClassType() const
True if this is equivalent to 'Class.
Definition: Type.h:6393
bool isObjCQualifiedIdType() const
True if this is equivalent to 'id.
Definition: Type.h:6387
bool isObjCIdType() const
True if this is equivalent to the 'id' type, i.e.
Definition: Type.h:6370
ObjCInterfaceDecl * getInterfaceDecl() const
If this pointer points to an Objective @interface type, gets the declaration for that interface.
Definition: Type.h:6364
bool isObjCClassType() const
True if this is equivalent to the 'Class' type, i.e.
Definition: Type.h:6376
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2800
QualType getPointeeType() const
Definition: Type.h:2810
A (possibly-)qualified type.
Definition: Type.h:736
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:6757
bool isVoidType() const
Definition: Type.h:7224
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7497
bool isObjCObjectPointerType() const
Definition: Type.h:7044
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7430
bool isCocoaObjectRef(QualType T)
bool isRefType(QualType RetTy, StringRef Prefix, StringRef Name=StringRef())
bool followsCreateRule(const FunctionDecl *FD)
LLVM_READONLY bool isLetter(unsigned char c)
Return true if this character is an ASCII letter: [a-zA-Z].
Definition: CharInfo.h:117
LLVM_READONLY bool isLowercase(unsigned char c)
Return true if this character is a lowercase ASCII letter: [a-z].
Definition: CharInfo.h:105