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 | |
| 21 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 22 | using namespace ento; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 23 | |
| 24 | using llvm::StringRef; |
| 25 | |
| 26 | // The "fundamental rule" for naming conventions of methods: |
| 27 | // (url broken into two lines) |
| 28 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ |
| 29 | // MemoryMgmt/Tasks/MemoryManagementRules.html |
| 30 | // |
| 31 | // "You take ownership of an object if you create it using a method whose name |
| 32 | // begins with "alloc" or "new" or contains "copy" (for example, alloc, |
| 33 | // newObject, or mutableCopy), or if you send it a retain message. You are |
| 34 | // responsible for relinquishing ownership of objects you own using release |
| 35 | // or autorelease. Any other time you receive an object, you must |
| 36 | // not release it." |
| 37 | // |
| 38 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 39 | cocoa::NamingConvention cocoa::deriveNamingConvention(Selector S) { |
| 40 | switch (S.getMethodFamily()) { |
| 41 | case OMF_None: |
| 42 | case OMF_autorelease: |
| 43 | case OMF_dealloc: |
| 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: |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 48 | return NoConvention; |
| 49 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 50 | case OMF_init: |
| 51 | return InitRule; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 52 | |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 53 | case OMF_alloc: |
| 54 | case OMF_copy: |
| 55 | case OMF_mutableCopy: |
| 56 | case OMF_new: |
| 57 | return CreateRule; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 58 | } |
John McCall | 85f3d76 | 2011-03-02 01:50:55 +0000 | [diff] [blame] | 59 | llvm_unreachable("unexpected naming convention"); |
| 60 | return NoConvention; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 61 | } |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 62 | |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 63 | bool cocoa::isRefType(QualType RetTy, llvm::StringRef Prefix, |
| 64 | llvm::StringRef Name) { |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 65 | // Recursively walk the typedef stack, allowing typedefs of reference types. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 66 | while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) { |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 67 | llvm::StringRef TDName = TD->getDecl()->getIdentifier()->getName(); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 68 | if (TDName.startswith(Prefix) && TDName.endswith("Ref")) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 69 | return true; |
| 70 | |
| 71 | RetTy = TD->getDecl()->getUnderlyingType(); |
| 72 | } |
| 73 | |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 74 | if (Name.empty()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 75 | return false; |
| 76 | |
| 77 | // Is the type void*? |
| 78 | const PointerType* PT = RetTy->getAs<PointerType>(); |
| 79 | if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType())) |
| 80 | return false; |
| 81 | |
| 82 | // Does the name start with the prefix? |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 83 | return Name.startswith(Prefix); |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | bool cocoa::isCFObjectRef(QualType T) { |
| 87 | return isRefType(T, "CF") || // Core Foundation. |
| 88 | isRefType(T, "CG") || // Core Graphics. |
| 89 | isRefType(T, "DADisk") || // Disk Arbitration API. |
| 90 | isRefType(T, "DADissenter") || |
| 91 | isRefType(T, "DASessionRef"); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | bool cocoa::isCocoaObjectRef(QualType Ty) { |
| 96 | if (!Ty->isObjCObjectPointerType()) |
| 97 | return false; |
| 98 | |
| 99 | const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>(); |
| 100 | |
| 101 | // Can be true for objects with the 'NSObject' attribute. |
| 102 | if (!PT) |
| 103 | return true; |
| 104 | |
Ted Kremenek | 4019c4f | 2010-08-05 00:19:24 +0000 | [diff] [blame] | 105 | // We assume that id<..>, id, Class, and Class<..> all represent tracked |
| 106 | // objects. |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 107 | if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() || |
Ted Kremenek | 4019c4f | 2010-08-05 00:19:24 +0000 | [diff] [blame] | 108 | PT->isObjCClassType() || PT->isObjCQualifiedClassType()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 109 | return true; |
| 110 | |
| 111 | // Does the interface subclass NSObject? |
| 112 | // FIXME: We can memoize here if this gets too expensive. |
| 113 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 114 | |
| 115 | // Assume that anything declared with a forward declaration and no |
| 116 | // @interface subclasses NSObject. |
| 117 | if (ID->isForwardDecl()) |
| 118 | return true; |
| 119 | |
| 120 | for ( ; ID ; ID = ID->getSuperClass()) |
| 121 | if (ID->getIdentifier()->getName() == "NSObject") |
| 122 | return true; |
| 123 | |
| 124 | return false; |
| 125 | } |