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/Type.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "llvm/ADT/StringExtras.h"
19 #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 = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
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 bool coreFoundation::isCFObjectRef(QualType T) {
51 return cocoa::isRefType(T, "CF") || // Core Foundation.
52 cocoa::isRefType(T, "CG") || // Core Graphics.
53 cocoa::isRefType(T, "DADisk") || // Disk Arbitration API.
54 cocoa::isRefType(T, "DADissenter") ||
55 cocoa::isRefType(T, "DASessionRef");
59 bool cocoa::isCocoaObjectRef(QualType Ty) {
60 if (!Ty->isObjCObjectPointerType())
63 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
65 // Can be true for objects with the 'NSObject' attribute.
69 // We assume that id<..>, id, Class, and Class<..> all represent tracked
71 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
72 PT->isObjCClassType() || PT->isObjCQualifiedClassType())
75 // Does the interface subclass NSObject?
76 // FIXME: We can memoize here if this gets too expensive.
77 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
79 // Assume that anything declared with a forward declaration and no
80 // @interface subclasses NSObject.
81 if (!ID->hasDefinition())
84 for ( ; ID ; ID = ID->getSuperClass())
85 if (ID->getIdentifier()->getName() == "NSObject")
91 bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
92 // For now, *just* base this on the function name, not on anything else.
94 const IdentifierInfo *ident = fn->getIdentifier();
95 if (!ident) return false;
96 StringRef functionName = ident->getName();
98 StringRef::iterator it = functionName.begin();
99 StringRef::iterator start = it;
100 StringRef::iterator endI = functionName.end();
103 // Scan for the start of 'create' or 'copy'.
104 for ( ; it != endI ; ++it) {
105 // Search for the first character. It can either be 'C' or 'c'.
107 if (ch == 'C' || ch == 'c') {
108 // Make sure this isn't something like 'recreate' or 'Scopy'.
109 if (ch == 'c' && it != start && isalpha(*(it - 1)))
117 // Did we hit the end of the string? If so, we didn't find a match.
121 // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
123 StringRef suffix = functionName.substr(it - start);
124 if (suffix.startswith("reate")) {
127 else if (suffix.startswith("opy")) {
134 if (it == endI || !islower(*it))
137 // If we matched a lowercase character, it isn't the end of the
138 // word. Keep scanning.