clang  6.0.0svn
CocoaConventions.cpp
Go to the documentation of this file.
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 
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>();
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.
53  return cocoa::isRefType(T, "DADisk") ||
54  cocoa::isRefType(T, "DADissenter") ||
55  cocoa::isRefType(T, "DASessionRef");
56 }
57 
59  return cocoa::isRefType(T, "CF") || // Core Foundation.
60  cocoa::isRefType(T, "CG") || // Core Graphics.
61  cocoa::isRefType(T, "CM") || // Core Media.
63 }
64 
65 
67  if (!Ty->isObjCObjectPointerType())
68  return false;
69 
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() ||
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 
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 }
bool hasDefinition() const
Determine whether this class has been defined.
Definition: DeclObjC.h:1554
An instance of this class is created to represent a function declaration or definition.
Definition: Decl.h:1697
bool isObjCQualifiedIdType() const
True if this is equivalent to &#39;id.
Definition: Type.h:5521
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2285
QualType getPointeeType() const
Definition: Type.h:2298
A (possibly-)qualified type.
Definition: Type.h:653
C Language Family Type Representation.
bool isCocoaObjectRef(QualType T)
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6307
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Definition: Decl.h:265
One of these records is kept for each identifier that is lexed.
LLVM_READONLY bool isLetter(unsigned char c)
Return true if this character is an ASCII letter: [a-zA-Z].
Definition: CharInfo.h:112
bool followsCreateRule(const FunctionDecl *FD)
Represents an ObjC class declaration.
Definition: DeclObjC.h:1191
const FunctionProtoType * T
bool isObjCIdType() const
True if this is equivalent to the &#39;id&#39; type, i.e.
Definition: Type.h:5504
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:330
bool isObjCQualifiedClassType() const
True if this is equivalent to &#39;Class.
Definition: Type.h:5527
bool isObjCObjectPointerType() const
Definition: Type.h:6041
LLVM_READONLY bool isLowercase(unsigned char c)
Return true if this character is a lowercase ASCII letter: [a-z].
Definition: CharInfo.h:100
StringRef getName() const
Return the actual identifier string.
Dataflow Directional Tag Classes.
bool isRefType(QualType RetTy, StringRef Prefix, StringRef Name=StringRef())
bool isObjCClassType() const
True if this is equivalent to the &#39;Class&#39; type, i.e.
Definition: Type.h:5510
Represents a pointer to an Objective C object.
Definition: Type.h:5442
static bool isDiskArbitrationAPIRefType(QualType T)
Returns true when the passed-in type is a CF-style reference-counted type from the DiskArbitration fr...
ObjCInterfaceDecl * getInterfaceDecl() const
If this pointer points to an Objective @interface type, gets the declaration for that interface...
Definition: Type.h:5498
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:5800
bool isVoidType() const
Definition: Type.h:6171