blob: b2d416c171a1084ab057ccdc3829603b532f1df9 [file] [log] [blame]
Ted Kremenekdb1832d2010-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//
Fangrui Song6907ce22018-07-30 19:24:48 +000010// This file implements cocoa naming convention analysis.
Ted Kremenekdb1832d2010-01-27 06:13:48 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek853da962010-12-17 05:21:58 +000014#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
Ted Kremeneke9918352010-01-27 18:00:17 +000015#include "clang/AST/Decl.h"
16#include "clang/AST/DeclObjC.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000017#include "clang/AST/Type.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000018#include "clang/Basic/CharInfo.h"
Ted Kremenekdb1832d2010-01-27 06:13:48 +000019#include "llvm/ADT/StringExtras.h"
John McCallb4526252011-03-02 01:50:55 +000020#include "llvm/Support/ErrorHandling.h"
Joerg Sonnenberger42cf2682012-08-10 10:58:18 +000021
Ted Kremenekdb1832d2010-01-27 06:13:48 +000022using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000023using namespace ento;
Ted Kremenekdb1832d2010-01-27 06:13:48 +000024
Chris Lattner0e62c1c2011-07-23 10:55:15 +000025bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
26 StringRef Name) {
Ted Kremeneke9918352010-01-27 18:00:17 +000027 // Recursively walk the typedef stack, allowing typedefs of reference types.
Douglas Gregoreb6e64c2015-06-19 23:17:46 +000028 while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +000029 StringRef TDName = TD->getDecl()->getIdentifier()->getName();
Benjamin Kramereaabbd82010-02-08 18:38:55 +000030 if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
Ted Kremeneke9918352010-01-27 18:00:17 +000031 return true;
Ted Kremenek990464c2012-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 Kremeneke9918352010-01-27 18:00:17 +000035 RetTy = TD->getDecl()->getUnderlyingType();
36 }
Fangrui Song6907ce22018-07-30 19:24:48 +000037
Benjamin Kramereaabbd82010-02-08 18:38:55 +000038 if (Name.empty())
Ted Kremeneke9918352010-01-27 18:00:17 +000039 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +000040
Ted Kremeneke9918352010-01-27 18:00:17 +000041 // Is the type void*?
42 const PointerType* PT = RetTy->getAs<PointerType>();
43 if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
44 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +000045
Ted Kremeneke9918352010-01-27 18:00:17 +000046 // Does the name start with the prefix?
Benjamin Kramereaabbd82010-02-08 18:38:55 +000047 return Name.startswith(Prefix);
Ted Kremeneke9918352010-01-27 18:00:17 +000048}
49
Devin Coughlincc5915a2017-11-25 14:57:42 +000050/// Returns true when the passed-in type is a CF-style reference-counted
51/// type from the DiskArbitration framework.
52static bool isDiskArbitrationAPIRefType(QualType T) {
53 return cocoa::isRefType(T, "DADisk") ||
54 cocoa::isRefType(T, "DADissenter") ||
55 cocoa::isRefType(T, "DASessionRef");
56}
57
Ted Kremenekc85964e2011-07-16 19:50:32 +000058bool coreFoundation::isCFObjectRef(QualType T) {
59 return cocoa::isRefType(T, "CF") || // Core Foundation.
60 cocoa::isRefType(T, "CG") || // Core Graphics.
Devin Coughlincc5915a2017-11-25 14:57:42 +000061 cocoa::isRefType(T, "CM") || // Core Media.
62 isDiskArbitrationAPIRefType(T);
Ted Kremeneke9918352010-01-27 18:00:17 +000063}
64
65
66bool cocoa::isCocoaObjectRef(QualType Ty) {
67 if (!Ty->isObjCObjectPointerType())
68 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +000069
Ted Kremeneke9918352010-01-27 18:00:17 +000070 const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
Fangrui Song6907ce22018-07-30 19:24:48 +000071
Ted Kremeneke9918352010-01-27 18:00:17 +000072 // Can be true for objects with the 'NSObject' attribute.
73 if (!PT)
74 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +000075
Ted Kremenek1d08fd92010-08-05 00:19:24 +000076 // We assume that id<..>, id, Class, and Class<..> all represent tracked
77 // objects.
Ted Kremeneke9918352010-01-27 18:00:17 +000078 if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
Ted Kremenek1d08fd92010-08-05 00:19:24 +000079 PT->isObjCClassType() || PT->isObjCQualifiedClassType())
Ted Kremeneke9918352010-01-27 18:00:17 +000080 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +000081
Ted Kremeneke9918352010-01-27 18:00:17 +000082 // Does the interface subclass NSObject?
83 // FIXME: We can memoize here if this gets too expensive.
84 const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
Fangrui Song6907ce22018-07-30 19:24:48 +000085
Ted Kremeneke9918352010-01-27 18:00:17 +000086 // Assume that anything declared with a forward declaration and no
87 // @interface subclasses NSObject.
Douglas Gregordc9166c2011-12-15 20:29:51 +000088 if (!ID->hasDefinition())
Ted Kremeneke9918352010-01-27 18:00:17 +000089 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +000090
Ted Kremeneke9918352010-01-27 18:00:17 +000091 for ( ; ID ; ID = ID->getSuperClass())
92 if (ID->getIdentifier()->getName() == "NSObject")
93 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +000094
Ted Kremeneke9918352010-01-27 18:00:17 +000095 return false;
96}
Ted Kremenekc85964e2011-07-16 19:50:32 +000097
John McCall525f0552011-10-01 00:48:56 +000098bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
99 // For now, *just* base this on the function name, not on anything else.
100
101 const IdentifierInfo *ident = fn->getIdentifier();
102 if (!ident) return false;
103 StringRef functionName = ident->getName();
Fangrui Song6907ce22018-07-30 19:24:48 +0000104
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000105 StringRef::iterator it = functionName.begin();
106 StringRef::iterator start = it;
107 StringRef::iterator endI = functionName.end();
Fangrui Song6907ce22018-07-30 19:24:48 +0000108
Ted Kremenek6dcbbe82011-07-16 19:50:36 +0000109 while (true) {
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'.
113 char ch = *it;
114 if (ch == 'C' || ch == 'c') {
John McCall525f0552011-10-01 00:48:56 +0000115 // Make sure this isn't something like 'recreate' or 'Scopy'.
Jordan Rosea7d03842013-02-08 22:30:41 +0000116 if (ch == 'c' && it != start && isLetter(*(it - 1)))
John McCall525f0552011-10-01 00:48:56 +0000117 continue;
118
Ted Kremenek6dcbbe82011-07-16 19:50:36 +0000119 ++it;
120 break;
121 }
122 }
123
124 // Did we hit the end of the string? If so, we didn't find a match.
125 if (it == endI)
126 return false;
Fangrui Song6907ce22018-07-30 19:24:48 +0000127
Ted Kremenek6dcbbe82011-07-16 19:50:36 +0000128 // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
129 // character.
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000130 StringRef suffix = functionName.substr(it - start);
Ted Kremenek6dcbbe82011-07-16 19:50:36 +0000131 if (suffix.startswith("reate")) {
132 it += 5;
133 }
134 else if (suffix.startswith("opy")) {
135 it += 3;
Chad Rosier6fdf38b2011-08-17 23:08:45 +0000136 } else {
Ted Kremenek6dcbbe82011-07-16 19:50:36 +0000137 // Keep scanning.
138 continue;
139 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000140
Jordan Rosea7d03842013-02-08 22:30:41 +0000141 if (it == endI || !isLowercase(*it))
Ted Kremenek6dcbbe82011-07-16 19:50:36 +0000142 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +0000143
Ted Kremenek6dcbbe82011-07-16 19:50:36 +0000144 // If we matched a lowercase character, it isn't the end of the
145 // word. Keep scanning.
146 }
Ted Kremenekc85964e2011-07-16 19:50:32 +0000147}