Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
18.5k
                      StringRef Name) {
26
18.5k
  // Recursively walk the typedef stack, allowing typedefs of reference types.
27
27.2k
  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
8.84k
    // XPC unfortunately uses CF-style function names, but aren't CF types.
32
8.84k
    if (TDName.startswith("xpc_"))
33
183
      return false;
34
8.66k
    RetTy = TD->getDecl()->getUnderlyingType();
35
8.66k
  }
36
18.5k
37
18.5k
  
if (17.1k
Name.empty()17.1k
)
38
16.6k
    return false;
39
544
40
544
  // Is the type void*?
41
544
  const PointerType* PT = RetTy->getAs<PointerType>();
42
544
  if (!(PT->getPointeeType().getUnqualifiedType()->isVoidType()))
43
434
    return false;
44
110
45
110
  // 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.80k
static bool isDiskArbitrationAPIRefType(QualType T) {
52
2.80k
  return cocoa::isRefType(T, "DADisk") ||
53
2.80k
      
cocoa::isRefType(T, "DADissenter")2.77k
||
54
2.80k
      
cocoa::isRefType(T, "DASessionRef")2.76k
;
55
2.80k
}
56
57
3.51k
bool coreFoundation::isCFObjectRef(QualType T) {
58
3.51k
  return cocoa::isRefType(T, "CF") || // Core Foundation.
59
3.51k
         
cocoa::isRefType(T, "CG")2.87k
|| // Core Graphics.
60
3.51k
         
cocoa::isRefType(T, "CM")2.82k
|| // Core Media.
61
3.51k
         
isDiskArbitrationAPIRefType(T)2.80k
;
62
3.51k
}
63
64
65
10.9k
bool cocoa::isCocoaObjectRef(QualType Ty) {
66
10.9k
  if (!Ty->isObjCObjectPointerType())
67
8.76k
    return false;
68
2.19k
69
2.19k
  const ObjCObjectPointerType *PT = Ty->getAs<ObjCObjectPointerType>();
70
2.19k
71
2.19k
  // Can be true for objects with the 'NSObject' attribute.
72
2.19k
  if (!PT)
73
0
    return true;
74
2.19k
75
2.19k
  // We assume that id<..>, id, Class, and Class<..> all represent tracked
76
2.19k
  // objects.
77
2.19k
  if (PT->isObjCIdType() || 
PT->isObjCQualifiedIdType()1.04k
||
78
2.19k
      
PT->isObjCClassType()1.01k
||
PT->isObjCQualifiedClassType()998
)
79
1.21k
    return true;
80
986
81
986
  // Does the interface subclass NSObject?
82
986
  // FIXME: We can memoize here if this gets too expensive.
83
986
  const ObjCInterfaceDecl *ID = PT->getInterfaceDecl();
84
986
85
986
  // Assume that anything declared with a forward declaration and no
86
986
  // @interface subclasses NSObject.
87
986
  if (!ID->hasDefinition())
88
61
    return true;
89
925
90
1.80k
  
for ( ; 925
ID ;
ID = ID->getSuperClass()884
)
91
1.76k
    if (ID->getIdentifier()->getName() == "NSObject")
92
878
      return true;
93
925
94
925
  
return false47
;
95
925
}
96
97
288
bool coreFoundation::followsCreateRule(const FunctionDecl *fn) {
98
288
  // For now, *just* base this on the function name, not on anything else.
99
288
100
288
  const IdentifierInfo *ident = fn->getIdentifier();
101
288
  if (!ident) 
return false0
;
102
288
  StringRef functionName = ident->getName();
103
288
104
288
  StringRef::iterator it = functionName.begin();
105
288
  StringRef::iterator start = it;
106
288
  StringRef::iterator endI = functionName.end();
107
288
108
489
  while (true) {
109
489
    // Scan for the start of 'create' or 'copy'.
110
2.91k
    for ( ; it != endI ; 
++it2.42k
) {
111
2.82k
      // 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
446
        // 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
402
118
402
        ++it;
119
402
        break;
120
402
      }
121
2.82k
    }
122
489
123
489
    // 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
399
127
399
    // Scan for *lowercase* 'reate' or 'opy', followed by no lowercase
128
399
    // 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
192
      // Keep scanning.
137
192
      continue;
138
192
    }
139
207
140
207
    if (it == endI || 
!isLowercase(*it)155
)
141
198
      return true;
142
207
143
207
    // If we matched a lowercase character, it isn't the end of the
144
207
    // word.  Keep scanning.
145
207
  }
146
288
}