Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 1 | //===- 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 Pichet | 6970155 | 2011-06-16 23:19:36 +0000 | [diff] [blame] | 10 | // This file implements cocoa naming convention analysis. |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | bb8fef3 | 2010-12-17 05:21:58 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 15 | #include "clang/AST/Type.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclObjC.h" |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 20 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 21 | using namespace ento; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 22 | |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 23 | // The "fundamental rule" for naming conventions of methods: |
| 24 | // (url broken into two lines) |
| 25 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ |
| 26 | // MemoryMgmt/Tasks/MemoryManagementRules.html |
| 27 | // |
| 28 | // "You take ownership of an object if you create it using a method whose name |
| 29 | // begins with "alloc" or "new" or contains "copy" (for example, alloc, |
| 30 | // newObject, or mutableCopy), or if you send it a retain message. You are |
| 31 | // responsible for relinquishing ownership of objects you own using release |
| 32 | // or autorelease. Any other time you receive an object, you must |
| 33 | // not release it." |
| 34 | // |
| 35 | |
Douglas Gregor | 786dcd9 | 2011-07-06 16:00:34 +0000 | [diff] [blame] | 36 | cocoa::NamingConvention cocoa::deriveNamingConvention(Selector S, |
| 37 | const ObjCMethodDecl *MD) { |
| 38 | switch (MD && MD->hasAttr<ObjCMethodFamilyAttr>()? MD->getMethodFamily() |
| 39 | : S.getMethodFamily()) { |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 40 | case OMF_None: |
| 41 | case OMF_autorelease: |
| 42 | case OMF_dealloc: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 43 | case OMF_finalize: |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 44 | case OMF_release: |
| 45 | case OMF_retain: |
| 46 | case OMF_retainCount: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 47 | case OMF_self: |
Fariborz Jahanian | 170579f | 2011-07-06 00:29:51 +0000 | [diff] [blame] | 48 | case OMF_performSelector: |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 49 | return NoConvention; |
| 50 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 51 | case OMF_init: |
| 52 | return InitRule; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 53 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 54 | case OMF_alloc: |
| 55 | case OMF_copy: |
| 56 | case OMF_mutableCopy: |
| 57 | case OMF_new: |
| 58 | return CreateRule; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 59 | } |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 60 | llvm_unreachable("unexpected naming convention"); |
| 61 | return NoConvention; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 62 | } |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 64 | bool cocoa::isRefType(QualType RetTy, StringRef Prefix, |
| 65 | StringRef Name) { |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 66 | // Recursively walk the typedef stack, allowing typedefs of reference types. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 67 | while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | StringRef TDName = TD->getDecl()->getIdentifier()->getName(); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 69 | if (TDName.startswith(Prefix) && TDName.endswith("Ref")) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 70 | return true; |
Ted Kremenek | 1c87980 | 2012-01-04 00:35:48 +0000 | [diff] [blame^] | 71 | // XPC unfortunately uses CF-style function names, but aren't CF types. |
| 72 | if (TDName.startswith("xpc_")) |
| 73 | return false; |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 74 | RetTy = TD->getDecl()->getUnderlyingType(); |
| 75 | } |
| 76 | |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 77 | if (Name.empty()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 78 | return false; |
| 79 | |
| 80 | // Is the type void*? |
| 81 | const PointerType* PT = RetTy->getAs<PointerType>(); |
| 82 | if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType())) |
| 83 | return false; |
| 84 | |
| 85 | // Does the name start with the prefix? |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 86 | return Name.startswith(Prefix); |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Ted Kremenek | 0556048 | 2011-07-16 19:50:32 +0000 | [diff] [blame] | 89 | bool coreFoundation::isCFObjectRef(QualType T) { |
| 90 | return cocoa::isRefType(T, "CF") || // Core Foundation. |
| 91 | cocoa::isRefType(T, "CG") || // Core Graphics. |
| 92 | cocoa::isRefType(T, "DADisk") || // Disk Arbitration API. |
| 93 | cocoa::isRefType(T, "DADissenter") || |
| 94 | cocoa::isRefType(T, "DASessionRef"); |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
| 98 | bool cocoa::isCocoaObjectRef(QualType Ty) { |
| 99 | if (!Ty->isObjCObjectPointerType()) |
| 100 | return false; |
| 101 | |
| 102 | const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>(); |
| 103 | |
| 104 | // Can be true for objects with the 'NSObject' attribute. |
| 105 | if (!PT) |
| 106 | return true; |
| 107 | |
Ted Kremenek | 4019c4f | 2010-08-05 00:19:24 +0000 | [diff] [blame] | 108 | // We assume that id<..>, id, Class, and Class<..> all represent tracked |
| 109 | // objects. |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 110 | if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() || |
Ted Kremenek | 4019c4f | 2010-08-05 00:19:24 +0000 | [diff] [blame] | 111 | PT->isObjCClassType() || PT->isObjCQualifiedClassType()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 112 | return true; |
| 113 | |
| 114 | // Does the interface subclass NSObject? |
| 115 | // FIXME: We can memoize here if this gets too expensive. |
| 116 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 117 | |
| 118 | // Assume that anything declared with a forward declaration and no |
| 119 | // @interface subclasses NSObject. |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 120 | if (!ID->hasDefinition()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 121 | return true; |
| 122 | |
| 123 | for ( ; ID ; ID = ID->getSuperClass()) |
| 124 | if (ID->getIdentifier()->getName() == "NSObject") |
| 125 | return true; |
| 126 | |
| 127 | return false; |
| 128 | } |
Ted Kremenek | 0556048 | 2011-07-16 19:50:32 +0000 | [diff] [blame] | 129 | |
John McCall | 7df2ff4 | 2011-10-01 00:48:56 +0000 | [diff] [blame] | 130 | bool coreFoundation::followsCreateRule(const FunctionDecl *fn) { |
| 131 | // For now, *just* base this on the function name, not on anything else. |
| 132 | |
| 133 | const IdentifierInfo *ident = fn->getIdentifier(); |
| 134 | if (!ident) return false; |
| 135 | StringRef functionName = ident->getName(); |
| 136 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 137 | StringRef::iterator it = functionName.begin(); |
| 138 | StringRef::iterator start = it; |
| 139 | StringRef::iterator endI = functionName.end(); |
Ted Kremenek | 797a7be | 2011-07-16 19:50:36 +0000 | [diff] [blame] | 140 | |
| 141 | while (true) { |
| 142 | // Scan for the start of 'create' or 'copy'. |
| 143 | for ( ; it != endI ; ++it) { |
| 144 | // Search for the first character. It can either be 'C' or 'c'. |
| 145 | char ch = *it; |
| 146 | if (ch == 'C' || ch == 'c') { |
John McCall | 7df2ff4 | 2011-10-01 00:48:56 +0000 | [diff] [blame] | 147 | // Make sure this isn't something like 'recreate' or 'Scopy'. |
| 148 | if (ch == 'c' && it != start && isalpha(*(it - 1))) |
| 149 | continue; |
| 150 | |
Ted Kremenek | 797a7be | 2011-07-16 19:50:36 +0000 | [diff] [blame] | 151 | ++it; |
| 152 | break; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Did we hit the end of the string? If so, we didn't find a match. |
| 157 | if (it == endI) |
| 158 | return false; |
| 159 | |
| 160 | // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase |
| 161 | // character. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 162 | StringRef suffix = functionName.substr(it - start); |
Ted Kremenek | 797a7be | 2011-07-16 19:50:36 +0000 | [diff] [blame] | 163 | if (suffix.startswith("reate")) { |
| 164 | it += 5; |
| 165 | } |
| 166 | else if (suffix.startswith("opy")) { |
| 167 | it += 3; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 168 | } else { |
Ted Kremenek | 797a7be | 2011-07-16 19:50:36 +0000 | [diff] [blame] | 169 | // Keep scanning. |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | if (it == endI || !islower(*it)) |
| 174 | return true; |
| 175 | |
| 176 | // If we matched a lowercase character, it isn't the end of the |
| 177 | // word. Keep scanning. |
| 178 | } |
| 179 | |
| 180 | return false; |
Ted Kremenek | 0556048 | 2011-07-16 19:50:32 +0000 | [diff] [blame] | 181 | } |