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 | // |
| 10 | // This file defines |
| 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" |
| 19 | |
| 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 | |
| 23 | using llvm::StringRef; |
| 24 | |
| 25 | // The "fundamental rule" for naming conventions of methods: |
| 26 | // (url broken into two lines) |
| 27 | // http://developer.apple.com/documentation/Cocoa/Conceptual/ |
| 28 | // MemoryMgmt/Tasks/MemoryManagementRules.html |
| 29 | // |
| 30 | // "You take ownership of an object if you create it using a method whose name |
| 31 | // begins with "alloc" or "new" or contains "copy" (for example, alloc, |
| 32 | // newObject, or mutableCopy), or if you send it a retain message. You are |
| 33 | // responsible for relinquishing ownership of objects you own using release |
| 34 | // or autorelease. Any other time you receive an object, you must |
| 35 | // not release it." |
| 36 | // |
| 37 | |
| 38 | static bool isWordEnd(char ch, char prev, char next) { |
| 39 | return ch == '\0' |
| 40 | || (islower(prev) && isupper(ch)) // xxxC |
| 41 | || (isupper(prev) && isupper(ch) && islower(next)) // XXCreate |
| 42 | || !isalpha(ch); |
| 43 | } |
| 44 | |
| 45 | static const char* parseWord(const char* s) { |
| 46 | char ch = *s, prev = '\0'; |
| 47 | assert(ch != '\0'); |
| 48 | char next = *(s+1); |
| 49 | while (!isWordEnd(ch, prev, next)) { |
| 50 | prev = ch; |
| 51 | ch = next; |
| 52 | next = *((++s)+1); |
| 53 | } |
| 54 | return s; |
| 55 | } |
| 56 | |
Argyrios Kyrtzidis | 5078d46 | 2011-01-11 19:45:16 +0000 | [diff] [blame] | 57 | cocoa::NamingConvention cocoa::deriveNamingConvention(Selector S, |
| 58 | bool ignorePrefix) { |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 59 | IdentifierInfo *II = S.getIdentifierInfoForSlot(0); |
| 60 | |
| 61 | if (!II) |
| 62 | return NoConvention; |
| 63 | |
| 64 | const char *s = II->getNameStart(); |
| 65 | |
Argyrios Kyrtzidis | 5078d46 | 2011-01-11 19:45:16 +0000 | [diff] [blame] | 66 | const char *orig = s; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 67 | // A method/function name may contain a prefix. We don't know it is there, |
| 68 | // however, until we encounter the first '_'. |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 69 | while (*s != '\0') { |
Ted Kremenek | af86b0c | 2010-12-17 04:44:43 +0000 | [diff] [blame] | 70 | // Skip '_', numbers, ':', etc. |
| 71 | if (*s == '_' || !isalpha(*s)) { |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 72 | ++s; |
| 73 | continue; |
| 74 | } |
Ted Kremenek | af86b0c | 2010-12-17 04:44:43 +0000 | [diff] [blame] | 75 | break; |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Argyrios Kyrtzidis | 5078d46 | 2011-01-11 19:45:16 +0000 | [diff] [blame] | 78 | if (!ignorePrefix && s != orig) |
| 79 | return NoConvention; |
| 80 | |
Ted Kremenek | af86b0c | 2010-12-17 04:44:43 +0000 | [diff] [blame] | 81 | // Parse the first word, and look for specific keywords. |
| 82 | const char *wordEnd = parseWord(s); |
| 83 | assert(wordEnd > s); |
| 84 | unsigned len = wordEnd - s; |
| 85 | |
| 86 | switch (len) { |
| 87 | default: |
| 88 | return NoConvention; |
| 89 | case 3: |
| 90 | // Methods starting with 'new' follow the create rule. |
| 91 | return (memcmp(s, "new", 3) == 0) ? CreateRule : NoConvention; |
| 92 | case 4: |
| 93 | // Methods starting with 'copy' follow the create rule. |
| 94 | if (memcmp(s, "copy", 4) == 0) |
| 95 | return CreateRule; |
| 96 | // Methods starting with 'init' follow the init rule. |
| 97 | if (memcmp(s, "init", 4) == 0) |
| 98 | return InitRule; |
| 99 | return NoConvention; |
| 100 | case 5: |
| 101 | return (memcmp(s, "alloc", 5) == 0) ? CreateRule : NoConvention; |
| 102 | case 7: |
| 103 | // Methods starting with 'mutableCopy' follow the create rule. |
| 104 | if (memcmp(s, "mutable", 7) == 0) { |
| 105 | // Look at the next word to see if it is "Copy". |
| 106 | s = wordEnd; |
Ted Kremenek | 5eef59e | 2010-12-17 07:11:57 +0000 | [diff] [blame] | 107 | if (*s != '\0') { |
| 108 | wordEnd = parseWord(s); |
| 109 | len = wordEnd - s; |
| 110 | if (len == 4 && memcmp(s, "Copy", 4) == 0) |
| 111 | return CreateRule; |
| 112 | } |
Ted Kremenek | af86b0c | 2010-12-17 04:44:43 +0000 | [diff] [blame] | 113 | } |
| 114 | return NoConvention; |
| 115 | } |
Ted Kremenek | a64e89b | 2010-01-27 06:13:48 +0000 | [diff] [blame] | 116 | } |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 117 | |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 118 | bool cocoa::isRefType(QualType RetTy, llvm::StringRef Prefix, |
| 119 | llvm::StringRef Name) { |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 120 | // Recursively walk the typedef stack, allowing typedefs of reference types. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame^] | 121 | while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) { |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 122 | llvm::StringRef TDName = TD->getDecl()->getIdentifier()->getName(); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 123 | if (TDName.startswith(Prefix) && TDName.endswith("Ref")) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 124 | return true; |
| 125 | |
| 126 | RetTy = TD->getDecl()->getUnderlyingType(); |
| 127 | } |
| 128 | |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 129 | if (Name.empty()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 130 | return false; |
| 131 | |
| 132 | // Is the type void*? |
| 133 | const PointerType* PT = RetTy->getAs<PointerType>(); |
| 134 | if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType())) |
| 135 | return false; |
| 136 | |
| 137 | // Does the name start with the prefix? |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 138 | return Name.startswith(Prefix); |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | bool cocoa::isCFObjectRef(QualType T) { |
| 142 | return isRefType(T, "CF") || // Core Foundation. |
| 143 | isRefType(T, "CG") || // Core Graphics. |
| 144 | isRefType(T, "DADisk") || // Disk Arbitration API. |
| 145 | isRefType(T, "DADissenter") || |
| 146 | isRefType(T, "DASessionRef"); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | bool cocoa::isCocoaObjectRef(QualType Ty) { |
| 151 | if (!Ty->isObjCObjectPointerType()) |
| 152 | return false; |
| 153 | |
| 154 | const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>(); |
| 155 | |
| 156 | // Can be true for objects with the 'NSObject' attribute. |
| 157 | if (!PT) |
| 158 | return true; |
| 159 | |
Ted Kremenek | 4019c4f | 2010-08-05 00:19:24 +0000 | [diff] [blame] | 160 | // We assume that id<..>, id, Class, and Class<..> all represent tracked |
| 161 | // objects. |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 162 | if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() || |
Ted Kremenek | 4019c4f | 2010-08-05 00:19:24 +0000 | [diff] [blame] | 163 | PT->isObjCClassType() || PT->isObjCQualifiedClassType()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 164 | return true; |
| 165 | |
| 166 | // Does the interface subclass NSObject? |
| 167 | // FIXME: We can memoize here if this gets too expensive. |
| 168 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 169 | |
| 170 | // Assume that anything declared with a forward declaration and no |
| 171 | // @interface subclasses NSObject. |
| 172 | if (ID->isForwardDecl()) |
| 173 | return true; |
| 174 | |
| 175 | for ( ; ID ; ID = ID->getSuperClass()) |
| 176 | if (ID->getIdentifier()->getName() == "NSObject") |
| 177 | return true; |
| 178 | |
| 179 | return false; |
| 180 | } |