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