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 
51  return cocoa::isRefType(T, "CF") || // Core Foundation.
52  cocoa::isRefType(T, "CG") || // Core Graphics.
53  cocoa::isRefType(T, "DADisk") || // Disk Arbitration API.
54  cocoa::isRefType(T, "DADissenter") ||
55  cocoa::isRefType(T, "DASessionRef");
56 }
57 
58 
60  if (!Ty->isObjCObjectPointerType())
61  return false;
62 
64 
65  // Can be true for objects with the 'NSObject' attribute.
66  if (!PT)
67  return true;
68 
69  // We assume that id<..>, id, Class, and Class<..> all represent tracked
70  // objects.
71  if (PT->isObjCIdType() || PT->isObjCQualifiedIdType() ||
73  return true;
74 
75  // Does the interface subclass NSObject?
76  // FIXME: We can memoize here if this gets too expensive.
77  const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
78 
79  // Assume that anything declared with a forward declaration and no
80  // @interface subclasses NSObject.
81  if (!ID->hasDefinition())
82  return true;
83 
84  for ( ; ID ; ID = ID->getSuperClass())
85  if (ID->getIdentifier()->getName() == "NSObject")
86  return true;
87 
88  return false;
89 }
90 
92  // For now, *just* base this on the function name, not on anything else.
93 
94  const IdentifierInfo *ident = fn->getIdentifier();
95  if (!ident) return false;
96  StringRef functionName = ident->getName();
97 
98  StringRef::iterator it = functionName.begin();
99  StringRef::iterator start = it;
100  StringRef::iterator endI = functionName.end();
101 
102  while (true) {
103  // Scan for the start of 'create' or 'copy'.
104  for ( ; it != endI ; ++it) {
105  // Search for the first character. It can either be 'C' or 'c'.
106  char ch = *it;
107  if (ch == 'C' || ch == 'c') {
108  // Make sure this isn't something like 'recreate' or 'Scopy'.
109  if (ch == 'c' && it != start && isLetter(*(it - 1)))
110  continue;
111 
112  ++it;
113  break;
114  }
115  }
116 
117  // Did we hit the end of the string? If so, we didn't find a match.
118  if (it == endI)
119  return false;
120 
121  // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
122  // character.
123  StringRef suffix = functionName.substr(it - start);
124  if (suffix.startswith("reate")) {
125  it += 5;
126  }
127  else if (suffix.startswith("opy")) {
128  it += 3;
129  } else {
130  // Keep scanning.
131  continue;
132  }
133 
134  if (it == endI || !isLowercase(*it))
135  return true;
136 
137  // If we matched a lowercase character, it isn't the end of the
138  // word. Keep scanning.
139  }
140 }
bool hasDefinition() const
Determine whether this class has been defined.
Definition: DeclObjC.h:1457
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1631
bool isObjCQualifiedIdType() const
True if this is equivalent to &#39;id.
Definition: Type.h:5352
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition: Type.h:2222
QualType getPointeeType() const
Definition: Type.h:2236
A (possibly-)qualified type.
Definition: Type.h:614
C Language Family Type Representation.
static LLVM_READONLY bool isLetter(unsigned char c)
Return true if this character is an ASCII letter: [a-zA-Z].
Definition: CharInfo.h:112
bool isCocoaObjectRef(QualType T)
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6099
IdentifierInfo * getIdentifier() const
getIdentifier - Get the identifier that names this declaration, if there is one.
Definition: Decl.h:232
One of these records is kept for each identifier that is lexed.
bool followsCreateRule(const FunctionDecl *FD)
Represents an ObjC class declaration.
Definition: DeclObjC.h:1108
const FunctionProtoType * T
bool isObjCIdType() const
True if this is equivalent to the &#39;id&#39; type, i.e.
Definition: Type.h:5335
ObjCInterfaceDecl * getSuperClass() const
Definition: DeclObjC.cpp:314
bool isObjCQualifiedClassType() const
True if this is equivalent to &#39;Class.
Definition: Type.h:5358
bool isObjCObjectPointerType() const
Definition: Type.h:5841
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:5341
Represents a pointer to an Objective C object.
Definition: Type.h:5274
static LLVM_READONLY bool isLowercase(unsigned char c)
Return true if this character is a lowercase ASCII letter: [a-z].
Definition: CharInfo.h:100
ObjCInterfaceDecl * getInterfaceDecl() const
If this pointer points to an Objective @interface type, gets the declaration for that interface...
Definition: Type.h:5329
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:5623
bool isVoidType() const
Definition: Type.h:5963