blob: ce973af6d431b4074d9fd107b6f359c2ec57e1b4 [file] [log] [blame]
Ted Kremeneka64e89b2010-01-27 06:13:48 +00001//===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Francois Pichet69701552011-06-16 23:19:36 +000010// This file implements cocoa naming convention analysis.
Ted Kremeneka64e89b2010-01-27 06:13:48 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenekbb8fef32010-12-17 05:21:58 +000014#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
Ted Kremenek78acdbf2010-01-27 18:00:17 +000015#include "clang/AST/Type.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclObjC.h"
Ted Kremeneka64e89b2010-01-27 06:13:48 +000018#include "llvm/ADT/StringExtras.h"
John McCall85f3d762011-03-02 01:50:55 +000019#include "llvm/Support/ErrorHandling.h"
Joerg Sonnenberger7094dee2012-08-10 10:58:18 +000020#include <cctype>
21
Ted Kremeneka64e89b2010-01-27 06:13:48 +000022using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000023using namespace ento;
Ted Kremeneka64e89b2010-01-27 06:13:48 +000024
Chris Lattner5f9e2722011-07-23 10:55:15 +000025bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
26 StringRef Name) {
Ted Kremenek78acdbf2010-01-27 18:00:17 +000027 // Recursively walk the typedef stack, allowing typedefs of reference types.
John McCallf4c73712011-01-19 06:33:43 +000028 while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) {
Chris Lattner5f9e2722011-07-23 10:55:15 +000029 StringRef TDName = TD->getDecl()->getIdentifier()->getName();
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000030 if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
Ted Kremenek78acdbf2010-01-27 18:00:17 +000031 return true;
Ted Kremenek1c879802012-01-04 00:35:48 +000032 // XPC unfortunately uses CF-style function names, but aren't CF types.
33 if (TDName.startswith("xpc_"))
34 return false;
Ted Kremenek78acdbf2010-01-27 18:00:17 +000035 RetTy = TD->getDecl()->getUnderlyingType();
36 }
37
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000038 if (Name.empty())
Ted Kremenek78acdbf2010-01-27 18:00:17 +000039 return false;
40
41 // Is the type void*?
42 const PointerType* PT = RetTy->getAs<PointerType>();
43 if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
44 return false;
45
46 // Does the name start with the prefix?
Benjamin Kramerb6f3c702010-02-08 18:38:55 +000047 return Name.startswith(Prefix);
Ted Kremenek78acdbf2010-01-27 18:00:17 +000048}
49
Ted Kremenek05560482011-07-16 19:50:32 +000050bool 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");
Ted Kremenek78acdbf2010-01-27 18:00:17 +000056}
57
58
59bool cocoa::isCocoaObjectRef(QualType Ty) {
60 if (!Ty->isObjCObjectPointerType())
61 return false;
62
63 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
64
65 // Can be true for objects with the 'NSObject' attribute.
66 if (!PT)
67 return true;
68
Ted Kremenek4019c4f2010-08-05 00:19:24 +000069 // We assume that id<..>, id, Class, and Class<..> all represent tracked
70 // objects.
Ted Kremenek78acdbf2010-01-27 18:00:17 +000071 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
Ted Kremenek4019c4f2010-08-05 00:19:24 +000072 PT->isObjCClassType() || PT->isObjCQualifiedClassType())
Ted Kremenek78acdbf2010-01-27 18:00:17 +000073 return true;
74
75 // Does the interface subclass NSObject?
76 // FIXME: We can memoize here if this gets too expensive.
77 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
78
79 // Assume that anything declared with a forward declaration and no
80 // @interface subclasses NSObject.
Douglas Gregor7723fec2011-12-15 20:29:51 +000081 if (!ID->hasDefinition())
Ted Kremenek78acdbf2010-01-27 18:00:17 +000082 return true;
83
84 for ( ; ID ; ID = ID->getSuperClass())
85 if (ID->getIdentifier()->getName() == "NSObject")
86 return true;
87
88 return false;
89}
Ted Kremenek05560482011-07-16 19:50:32 +000090
John McCall7df2ff42011-10-01 00:48:56 +000091bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
92 // For now, *just* base this on the function name, not on anything else.
93
94 const IdentifierInfo *ident = fn->getIdentifier();
95 if (!ident) return false;
96 StringRef functionName = ident->getName();
97
Chris Lattner5f9e2722011-07-23 10:55:15 +000098 StringRef::iterator it = functionName.begin();
99 StringRef::iterator start = it;
100 StringRef::iterator endI = functionName.end();
Ted Kremenek797a7be2011-07-16 19:50:36 +0000101
102 while (true) {
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'.
106 char ch = *it;
107 if (ch == 'C' || ch == 'c') {
John McCall7df2ff42011-10-01 00:48:56 +0000108 // Make sure this isn't something like 'recreate' or 'Scopy'.
109 if (ch == 'c' && it != start && isalpha(*(it - 1)))
110 continue;
111
Ted Kremenek797a7be2011-07-16 19:50:36 +0000112 ++it;
113 break;
114 }
115 }
116
117 // Did we hit the end of the string? If so, we didn't find a match.
118 if (it == endI)
119 return false;
120
121 // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
122 // character.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 StringRef suffix = functionName.substr(it - start);
Ted Kremenek797a7be2011-07-16 19:50:36 +0000124 if (suffix.startswith("reate")) {
125 it += 5;
126 }
127 else if (suffix.startswith("opy")) {
128 it += 3;
Chad Rosier30601782011-08-17 23:08:45 +0000129 } else {
Ted Kremenek797a7be2011-07-16 19:50:36 +0000130 // Keep scanning.
131 continue;
132 }
133
134 if (it == endI || !islower(*it))
135 return true;
136
137 // If we matched a lowercase character, it isn't the end of the
138 // word. Keep scanning.
139 }
Ted Kremenek05560482011-07-16 19:50:32 +0000140}