1 //===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements cocoa naming convention analysis.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/Type.h"
18 #include "clang/Basic/CharInfo.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/ErrorHandling.h"
22 using namespace clang;
25 bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
27 // Recursively walk the typedef stack, allowing typedefs of reference types.
28 while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
29 StringRef TDName = TD->getDecl()->getIdentifier()->getName();
30 if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
32 // XPC unfortunately uses CF-style function names, but aren't CF types.
33 if (TDName.startswith("xpc_"))
35 RetTy = TD->getDecl()->getUnderlyingType();
42 const PointerType* PT = RetTy->getAs<PointerType>();
43 if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
46 // Does the name start with the prefix?
47 return Name.startswith(Prefix);
50 /// Returns true when the passed-in type is a CF-style reference-counted
51 /// type from the DiskArbitration framework.
52 static bool isDiskArbitrationAPIRefType(QualType T) {
53 return cocoa::isRefType(T, "DADisk") ||
54 cocoa::isRefType(T, "DADissenter") ||
55 cocoa::isRefType(T, "DASessionRef");
58 bool coreFoundation::isCFObjectRef(QualType T) {
59 return cocoa::isRefType(T, "CF") || // Core Foundation.
60 cocoa::isRefType(T, "CG") || // Core Graphics.
61 cocoa::isRefType(T, "CM") || // Core Media.
62 isDiskArbitrationAPIRefType(T);
66 bool cocoa::isCocoaObjectRef(QualType Ty) {
67 if (!Ty->isObjCObjectPointerType())
70 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
72 // Can be true for objects with the 'NSObject' attribute.
76 // We assume that id<..>, id, Class, and Class<..> all represent tracked
78 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
79 PT->isObjCClassType() || PT->isObjCQualifiedClassType())
82 // Does the interface subclass NSObject?
83 // FIXME: We can memoize here if this gets too expensive.
84 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
86 // Assume that anything declared with a forward declaration and no
87 // @interface subclasses NSObject.
88 if (!ID->hasDefinition())
91 for ( ; ID ; ID = ID->getSuperClass())
92 if (ID->getIdentifier()->getName() == "NSObject")
98 bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
99 // For now, *just* base this on the function name, not on anything else.
101 const IdentifierInfo *ident = fn->getIdentifier();
102 if (!ident) return false;
103 StringRef functionName = ident->getName();
105 StringRef::iterator it = functionName.begin();
106 StringRef::iterator start = it;
107 StringRef::iterator endI = functionName.end();
110 // Scan for the start of 'create' or 'copy'.
111 for ( ; it != endI ; ++it) {
112 // Search for the first character. It can either be 'C' or 'c'.
114 if (ch == 'C' || ch == 'c') {
115 // Make sure this isn't something like 'recreate' or 'Scopy'.
116 if (ch == 'c' && it != start && isLetter(*(it - 1)))
124 // Did we hit the end of the string? If so, we didn't find a match.
128 // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
130 StringRef suffix = functionName.substr(it - start);
131 if (suffix.startswith("reate")) {
134 else if (suffix.startswith("opy")) {
141 if (it == endI || !isLowercase(*it))
144 // If we matched a lowercase character, it isn't the end of the
145 // word. Keep scanning.