Coverage Report

Created: 2020-09-22 08:39

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Analysis/CocoaConventions.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CocoaConventions.h - Special handling of Cocoa conventions -*- C++ -*--//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file implements cocoa naming convention analysis.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Analysis/DomainSpecific/CocoaConventions.h"
14
#include "clang/AST/Decl.h"
15
#include "clang/AST/DeclObjC.h"
16
#include "clang/AST/Type.h"
17
#include "clang/Basic/CharInfo.h"
18
#include "llvm/ADT/StringExtras.h"
19
#include "llvm/Support/ErrorHandling.h"
20
21
using namespace clang;
22
using namespace ento;
23
24
bool cocoa::isRefType(QualType RetTy, StringRef Prefix,
25
19.0k
                      StringRef Name) {
26
  // Recursively walk the typedef stack, allowing typedefs of reference types.
27
27.7k
  while (const TypedefType *TD = RetTy->getAs<TypedefType>()) {
28
10.0k
    StringRef TDName = TD->getDecl()->getIdentifier()->getName();
29
10.0k
    if (TDName.startswith(Prefix) && 
TDName.endswith("Ref")1.61k
)
30
1.19k
      return true;
31
    // XPC unfortunately uses CF-style function names, but aren't CF types.
32
8.86k
    if (TDName.startswith("xpc_"))
33
183
      return false;
34
8.68k
    RetTy = TD->getDecl()->getUnderlyingType();
35
8.68k
  }
36
37
17.6k
  if (Name.empty())
38
17.1k
    return false;
39
40
  // Is the type void*?
41
553
  const PointerType* PT = RetTy->castAs<PointerType>();
42
553
  if (!PT || !PT->getPointeeType().getUnqualifiedType()->isVoidType())
43
443
    return false;
44
45
  // Does the name start with the prefix?
46
110
  return Name.startswith(Prefix);
47
110
}
48
49
/// Returns true when the passed-in type is a CF-style reference-counted
50
/// type from the DiskArbitration framework.
51
2.88k
static bool isDiskArbitrationAPIRefType(QualType T) {
52
2.88k
  return cocoa::isRefType(T, "DADisk") ||
53
2.85k
      cocoa::isRefType(T, "DADissenter") ||
54
2.85k
      cocoa::isRefType(T, "DASessionRef");
55
2.88k
}
56
57
3.59k
bool coreFoundation::isCFObjectRef(QualType T) {
58
3.59k
  return cocoa::isRefType(T, "CF") || // Core Foundation.
59
2.95k
         cocoa::isRefType(T, "CG") || // Core Graphics.
60
2.90k
         cocoa::isRefType(T, "CM") || // Core Media.
61
2.88k
         isDiskArbitrationAPIRefType(T);
62
3.59k
}
63
64
65
11.2k
bool cocoa::isCocoaObjectRef(QualType Ty) {
66
11.2k
  if (!Ty->isObjCObjectPointerType())
67
9.00k
    return false;
68
69
2.27k
  const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
70
71
  // Can be true for objects with the 'NSObject' attribute.
72
2.27k
  if (!PT)
73
0
    return true;
74
75
  // We assume that id<..>, id, Class, and Class<..> all represent tracked
76
  // objects.
77
2.27k
  if (PT->isObjCIdType() || 
PT->isObjCQualifiedIdType()1.11k
||
78
1.08k
      PT->isObjCClassType() || 
PT->isObjCQualifiedClassType()1.06k
)
79
1.22k
    return true;
80
81
  // Does the interface subclass NSObject?
82
  // FIXME: We can memoize here if this gets too expensive.
83
1.04k
  const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
84
85
  // Assume that anything declared with a forward declaration and no
86
  // @interface subclasses NSObject.
87
1.04k
  if (!ID->hasDefinition())
88
61
    return true;
89
90
1.88k
  
for ( ; 987
ID ;
ID = ID->getSuperClass()896
)
91
1.83k
    if (ID->getIdentifier()->getName() == "NSObject")
92
939
      return true;
93
94
48
  return false;
95
987
}
96
97
288
bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
98
  // For now, *just* base this on the function name, not on anything else.
99
100
288
  const IdentifierInfo *ident = fn->getIdentifier();
101
288
  if (!ident) 
return false0
;
102
288
  StringRef functionName = ident->getName();
103
104
288
  StringRef::iterator it = functionName.begin();
105
288
  StringRef::iterator start = it;
106
288
  StringRef::iterator endI = functionName.end();
107
108
489
  while (true) {
109
    // Scan for the start of 'create' or 'copy'.
110
2.91k
    for ( ; it != endI ; 
++it2.42k
) {
111
      // Search for the first character.  It can either be 'C' or 'c'.
112
2.82k
      char ch = *it;
113
2.82k
      if (ch == 'C' || 
ch == 'c'2.49k
) {
114
        // Make sure this isn't something like 'recreate' or 'Scopy'.
115
446
        if (ch == 'c' && 
it != start113
&&
isLetter(*(it - 1))80
)
116
44
          continue;
117
118
402
        ++it;
119
402
        break;
120
402
      }
121
2.82k
    }
122
123
    // Did we hit the end of the string?  If so, we didn't find a match.
124
489
    if (it == endI)
125
90
      return false;
126
127
    // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
128
    // character.
129
399
    StringRef suffix = functionName.substr(it - start);
130
399
    if (suffix.startswith("reate")) {
131
171
      it += 5;
132
171
    }
133
228
    else if (suffix.startswith("opy")) {
134
36
      it += 3;
135
192
    } else {
136
      // Keep scanning.
137
192
      continue;
138
192
    }
139
140
207
    if (it == endI || 
!isLowercase(*it)155
)
141
198
      return true;
142
143
    // If we matched a lowercase character, it isn't the end of the
144
    // word.  Keep scanning.
145
207
  }
146
288
}