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 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 23 | bool cocoa::isRefType(QualType RetTy, StringRef Prefix, |
| 24 | StringRef Name) { |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 25 | // Recursively walk the typedef stack, allowing typedefs of reference types. |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 26 | while (const TypedefType *TD = dyn_cast<TypedefType>(RetTy.getTypePtr())) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 27 | StringRef TDName = TD->getDecl()->getIdentifier()->getName(); |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 28 | if (TDName.startswith(Prefix) && TDName.endswith("Ref")) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 29 | return true; |
Ted Kremenek | 1c87980 | 2012-01-04 00:35:48 +0000 | [diff] [blame] | 30 | // XPC unfortunately uses CF-style function names, but aren't CF types. |
| 31 | if (TDName.startswith("xpc_")) |
| 32 | return false; |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 33 | RetTy = TD->getDecl()->getUnderlyingType(); |
| 34 | } |
| 35 | |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 36 | if (Name.empty()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 37 | return false; |
| 38 | |
| 39 | // Is the type void*? |
| 40 | const PointerType* PT = RetTy->getAs<PointerType>(); |
| 41 | if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType())) |
| 42 | return false; |
| 43 | |
| 44 | // Does the name start with the prefix? |
Benjamin Kramer | b6f3c70 | 2010-02-08 18:38:55 +0000 | [diff] [blame] | 45 | return Name.startswith(Prefix); |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Ted Kremenek | 0556048 | 2011-07-16 19:50:32 +0000 | [diff] [blame] | 48 | bool coreFoundation::isCFObjectRef(QualType T) { |
| 49 | return cocoa::isRefType(T, "CF") || // Core Foundation. |
| 50 | cocoa::isRefType(T, "CG") || // Core Graphics. |
| 51 | cocoa::isRefType(T, "DADisk") || // Disk Arbitration API. |
| 52 | cocoa::isRefType(T, "DADissenter") || |
| 53 | cocoa::isRefType(T, "DASessionRef"); |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | |
| 57 | bool cocoa::isCocoaObjectRef(QualType Ty) { |
| 58 | if (!Ty->isObjCObjectPointerType()) |
| 59 | return false; |
| 60 | |
| 61 | const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>(); |
| 62 | |
| 63 | // Can be true for objects with the 'NSObject' attribute. |
| 64 | if (!PT) |
| 65 | return true; |
| 66 | |
Ted Kremenek | 4019c4f | 2010-08-05 00:19:24 +0000 | [diff] [blame] | 67 | // We assume that id<..>, id, Class, and Class<..> all represent tracked |
| 68 | // objects. |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 69 | if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() || |
Ted Kremenek | 4019c4f | 2010-08-05 00:19:24 +0000 | [diff] [blame] | 70 | PT->isObjCClassType() || PT->isObjCQualifiedClassType()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 71 | return true; |
| 72 | |
| 73 | // Does the interface subclass NSObject? |
| 74 | // FIXME: We can memoize here if this gets too expensive. |
| 75 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 76 | |
| 77 | // Assume that anything declared with a forward declaration and no |
| 78 | // @interface subclasses NSObject. |
Douglas Gregor | 7723fec | 2011-12-15 20:29:51 +0000 | [diff] [blame] | 79 | if (!ID->hasDefinition()) |
Ted Kremenek | 78acdbf | 2010-01-27 18:00:17 +0000 | [diff] [blame] | 80 | return true; |
| 81 | |
| 82 | for ( ; ID ; ID = ID->getSuperClass()) |
| 83 | if (ID->getIdentifier()->getName() == "NSObject") |
| 84 | return true; |
| 85 | |
| 86 | return false; |
| 87 | } |
Ted Kremenek | 0556048 | 2011-07-16 19:50:32 +0000 | [diff] [blame] | 88 | |
John McCall | 7df2ff4 | 2011-10-01 00:48:56 +0000 | [diff] [blame] | 89 | bool coreFoundation::followsCreateRule(const FunctionDecl *fn) { |
| 90 | // For now, *just* base this on the function name, not on anything else. |
| 91 | |
| 92 | const IdentifierInfo *ident = fn->getIdentifier(); |
| 93 | if (!ident) return false; |
| 94 | StringRef functionName = ident->getName(); |
| 95 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 96 | StringRef::iterator it = functionName.begin(); |
| 97 | StringRef::iterator start = it; |
| 98 | StringRef::iterator endI = functionName.end(); |
Ted Kremenek | 797a7be | 2011-07-16 19:50:36 +0000 | [diff] [blame] | 99 | |
| 100 | while (true) { |
| 101 | // Scan for the start of 'create' or 'copy'. |
| 102 | for ( ; it != endI ; ++it) { |
| 103 | // Search for the first character. It can either be 'C' or 'c'. |
| 104 | char ch = *it; |
| 105 | if (ch == 'C' || ch == 'c') { |
John McCall | 7df2ff4 | 2011-10-01 00:48:56 +0000 | [diff] [blame] | 106 | // Make sure this isn't something like 'recreate' or 'Scopy'. |
| 107 | if (ch == 'c' && it != start && isalpha(*(it - 1))) |
| 108 | continue; |
| 109 | |
Ted Kremenek | 797a7be | 2011-07-16 19:50:36 +0000 | [diff] [blame] | 110 | ++it; |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Did we hit the end of the string? If so, we didn't find a match. |
| 116 | if (it == endI) |
| 117 | return false; |
| 118 | |
| 119 | // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase |
| 120 | // character. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 121 | StringRef suffix = functionName.substr(it - start); |
Ted Kremenek | 797a7be | 2011-07-16 19:50:36 +0000 | [diff] [blame] | 122 | if (suffix.startswith("reate")) { |
| 123 | it += 5; |
| 124 | } |
| 125 | else if (suffix.startswith("opy")) { |
| 126 | it += 3; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 127 | } else { |
Ted Kremenek | 797a7be | 2011-07-16 19:50:36 +0000 | [diff] [blame] | 128 | // Keep scanning. |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | if (it == endI || !islower(*it)) |
| 133 | return true; |
| 134 | |
| 135 | // If we matched a lowercase character, it isn't the end of the |
| 136 | // word. Keep scanning. |
| 137 | } |
Ted Kremenek | 0556048 | 2011-07-16 19:50:32 +0000 | [diff] [blame] | 138 | } |