/Users/buildslave/jenkins/workspace/coverage/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 | 19.5k | StringRef Name) { |
26 | | // Recursively walk the typedef stack, allowing typedefs of reference types. |
27 | 28.1k | while (const TypedefType *TD = RetTy->getAs<TypedefType>()) { |
28 | 9.99k | StringRef TDName = TD->getDecl()->getIdentifier()->getName(); |
29 | 9.99k | if (TDName.startswith(Prefix) && TDName.endswith("Ref")1.60k ) |
30 | 1.18k | return true; |
31 | | // XPC unfortunately uses CF-style function names, but aren't CF types. |
32 | 8.81k | if (TDName.startswith("xpc_")) |
33 | 183 | return false; |
34 | 8.62k | RetTy = TD->getDecl()->getUnderlyingType(); |
35 | 8.62k | } |
36 | | |
37 | 18.1k | if (Name.empty()) |
38 | 17.5k | return false; |
39 | | |
40 | | // Is the type void*? |
41 | 577 | const PointerType* PT = RetTy->castAs<PointerType>(); |
42 | 577 | if (!PT || !PT->getPointeeType().getUnqualifiedType()->isVoidType()) |
43 | 467 | return false; |
44 | | |
45 | | // Does the name start with the prefix? |
46 | 110 | return Name.startswith(Prefix); |
47 | 577 | } |
48 | | |
49 | | /// Returns true when the passed-in type is a CF-style reference-counted |
50 | | /// type from the DiskArbitration framework. |
51 | 2.96k | static bool isDiskArbitrationAPIRefType(QualType T) { |
52 | 2.96k | return cocoa::isRefType(T, "DADisk") || |
53 | 2.96k | cocoa::isRefType(T, "DADissenter")2.92k || |
54 | 2.96k | cocoa::isRefType(T, "DASessionRef")2.92k ; |
55 | 2.96k | } |
56 | | |
57 | 3.66k | bool coreFoundation::isCFObjectRef(QualType T) { |
58 | 3.66k | return cocoa::isRefType(T, "CF") || // Core Foundation. |
59 | 3.66k | cocoa::isRefType(T, "CG")3.03k || // Core Graphics. |
60 | 3.66k | cocoa::isRefType(T, "CM")2.97k || // Core Media. |
61 | 3.66k | isDiskArbitrationAPIRefType(T)2.96k ; |
62 | 3.66k | } |
63 | | |
64 | | |
65 | 11.4k | bool cocoa::isCocoaObjectRef(QualType Ty) { |
66 | 11.4k | if (!Ty->isObjCObjectPointerType()) |
67 | 9.18k | return false; |
68 | | |
69 | 2.28k | const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>(); |
70 | | |
71 | | // Can be true for objects with the 'NSObject' attribute. |
72 | 2.28k | if (!PT) |
73 | 0 | return true; |
74 | | |
75 | | // We assume that id<..>, id, Class, and Class<..> all represent tracked |
76 | | // objects. |
77 | 2.28k | if (PT->isObjCIdType() || PT->isObjCQualifiedIdType()1.11k || |
78 | 2.28k | PT->isObjCClassType()1.08k || PT->isObjCQualifiedClassType()1.06k ) |
79 | 1.23k | return true; |
80 | | |
81 | | // Does the interface subclass NSObject? |
82 | | // FIXME: We can memoize here if this gets too expensive. |
83 | 1.05k | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
84 | | |
85 | | // Assume that anything declared with a forward declaration and no |
86 | | // @interface subclasses NSObject. |
87 | 1.05k | if (!ID->hasDefinition()) |
88 | 61 | return true; |
89 | | |
90 | 1.88k | for ( ; 989 ID ; ID = ID->getSuperClass()898 ) |
91 | 1.83k | if (ID->getIdentifier()->getName() == "NSObject") |
92 | 939 | return true; |
93 | | |
94 | 50 | return false; |
95 | 989 | } |
96 | | |
97 | 288 | bool coreFoundation::followsCreateRule(const FunctionDecl *fn) { |
98 | | // For now, *just* base this on the function name, not on anything else. |
99 | | |
100 | 288 | const IdentifierInfo *ident = fn->getIdentifier(); |
101 | 288 | if (!ident) return false0 ; |
102 | 288 | StringRef functionName = ident->getName(); |
103 | | |
104 | 288 | StringRef::iterator it = functionName.begin(); |
105 | 288 | StringRef::iterator start = it; |
106 | 288 | StringRef::iterator endI = functionName.end(); |
107 | | |
108 | 489 | while (true) { |
109 | | // Scan for the start of 'create' or 'copy'. |
110 | 2.91k | for ( ; it != endI ; ++it2.42k ) { |
111 | | // Search for the first character. It can either be 'C' or 'c'. |
112 | 2.82k | char ch = *it; |
113 | 2.82k | if (ch == 'C' || ch == 'c'2.49k ) { |
114 | | // Make sure this isn't something like 'recreate' or 'Scopy'. |
115 | 446 | if (ch == 'c' && it != start113 && isLetter(*(it - 1))80 ) |
116 | 44 | continue; |
117 | | |
118 | 402 | ++it; |
119 | 402 | break; |
120 | 446 | } |
121 | 2.82k | } |
122 | | |
123 | | // Did we hit the end of the string? If so, we didn't find a match. |
124 | 489 | if (it == endI) |
125 | 90 | return false; |
126 | | |
127 | | // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase |
128 | | // character. |
129 | 399 | StringRef suffix = functionName.substr(it - start); |
130 | 399 | if (suffix.startswith("reate")) { |
131 | 171 | it += 5; |
132 | 171 | } |
133 | 228 | else if (suffix.startswith("opy")) { |
134 | 36 | it += 3; |
135 | 192 | } else { |
136 | | // Keep scanning. |
137 | 192 | continue; |
138 | 192 | } |
139 | | |
140 | 207 | if (it == endI || !isLowercase(*it)155 ) |
141 | 198 | return true; |
142 | | |
143 | | // If we matched a lowercase character, it isn't the end of the |
144 | | // word. Keep scanning. |
145 | 207 | } |
146 | 288 | } |