]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Analysis/CocoaConventions.cpp
Merge clang trunk r321017 to contrib/llvm/tools/clang.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Analysis / CocoaConventions.cpp
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 implements cocoa naming convention analysis. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/Type.h"
18 #include "clang/Basic/CharInfo.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/ErrorHandling.h"
21
22 using namespace clang;
23 using namespace ento;
24
25 bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
26                       StringRef Name) {
27   // Recursively walk the typedef stack, allowing typedefs of reference types.
28   while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
29     StringRef TDName = TD->getDecl()->getIdentifier()->getName();
30     if (TDName.startswith(Prefix) && TDName.endswith("Ref"))
31       return true;
32     // XPC unfortunately uses CF-style function names, but aren't CF types.
33     if (TDName.startswith("xpc_"))
34       return false;
35     RetTy = TD->getDecl()->getUnderlyingType();
36   }
37   
38   if (Name.empty())
39     return false;
40   
41   // Is the type void*?
42   const PointerType* PT = RetTy->getAs<PointerType>();
43   if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
44     return false;
45   
46   // Does the name start with the prefix?
47   return Name.startswith(Prefix);
48 }
49
50 /// Returns true when the passed-in type is a CF-style reference-counted
51 /// type from the DiskArbitration framework.
52 static bool isDiskArbitrationAPIRefType(QualType T) {
53   return cocoa::isRefType(T, "DADisk") ||
54       cocoa::isRefType(T, "DADissenter") ||
55       cocoa::isRefType(T, "DASessionRef");
56 }
57
58 bool coreFoundation::isCFObjectRef(QualType T) {
59   return cocoa::isRefType(T, "CF") || // Core Foundation.
60          cocoa::isRefType(T, "CG") || // Core Graphics.
61          cocoa::isRefType(T, "CM") || // Core Media.
62          isDiskArbitrationAPIRefType(T);
63 }
64
65
66 bool cocoa::isCocoaObjectRef(QualType Ty) {
67   if (!Ty->isObjCObjectPointerType())
68     return false;
69   
70   const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
71   
72   // Can be true for objects with the 'NSObject' attribute.
73   if (!PT)
74     return true;
75   
76   // We assume that id<..>, id, Class, and Class<..> all represent tracked
77   // objects.
78   if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
79       PT->isObjCClassType() || PT->isObjCQualifiedClassType())
80     return true;
81   
82   // Does the interface subclass NSObject?
83   // FIXME: We can memoize here if this gets too expensive.
84   const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
85   
86   // Assume that anything declared with a forward declaration and no
87   // @interface subclasses NSObject.
88   if (!ID->hasDefinition())
89     return true;
90   
91   for ( ; ID ; ID = ID->getSuperClass())
92     if (ID->getIdentifier()->getName() == "NSObject")
93       return true;
94   
95   return false;
96 }
97
98 bool 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();
104   
105   StringRef::iterator it = functionName.begin();
106   StringRef::iterator start = it;
107   StringRef::iterator endI = functionName.end();
108     
109   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') {
115         // Make sure this isn't something like 'recreate' or 'Scopy'.
116         if (ch == 'c' && it != start && isLetter(*(it - 1)))
117           continue;
118
119         ++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;
127     
128     // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
129     // character.
130     StringRef suffix = functionName.substr(it - start);
131     if (suffix.startswith("reate")) {
132       it += 5;
133     }
134     else if (suffix.startswith("opy")) {
135       it += 3;
136     } else {
137       // Keep scanning.
138       continue;
139     }
140     
141     if (it == endI || !isLowercase(*it))
142       return true;
143   
144     // If we matched a lowercase character, it isn't the end of the
145     // word.  Keep scanning.
146   }
147 }