Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "KungFuDeathGripChecker.h"
6
#include "CustomMatchers.h"
7
8
void KungFuDeathGripChecker::registerMatchers(MatchFinder *AstMatcher) {
9
AstMatcher->addMatcher(varDecl(allOf(hasType(isRefPtr()),
10
hasLocalStorage(),
11
hasInitializer(anything())))
12
.bind("decl"),
13
this);
14
}
15
16
void KungFuDeathGripChecker::check(const MatchFinder::MatchResult &Result) {
17
const char *Error = "Unused \"kungFuDeathGrip\" %0 objects constructed from "
18
"%1 are prohibited";
19
const char *Note = "Please switch all accesses to this %0 to go through "
20
"'%1', or explicitly pass '%1' to `mozilla::Unused`";
21
22
const VarDecl *D = Result.Nodes.getNodeAs<VarDecl>("decl");
23
if (D->isReferenced()) {
24
return;
25
}
26
27
// Not interested in parameters.
28
if (isa<ImplicitParamDecl>(D) || isa<ParmVarDecl>(D)) {
29
return;
30
}
31
32
const Expr *E = IgnoreTrivials(D->getInit());
33
const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E);
34
if (CE && CE->getNumArgs() == 0) {
35
// We don't report an error when we construct and don't use a nsCOMPtr /
36
// nsRefPtr with no arguments. We don't report it because the error is not
37
// related to the current check. In the future it may be reported through a
38
// more generic mechanism.
39
return;
40
}
41
42
// We don't want to look at the single argument conversion constructors
43
// which are inbetween the declaration and the actual object which we are
44
// assigning into the nsCOMPtr/RefPtr. To do this, we repeatedly
45
// IgnoreTrivials, then look at the expression. If it is one of these
46
// conversion constructors, we ignore it and continue to dig.
47
while ((CE = dyn_cast<CXXConstructExpr>(E)) && CE->getNumArgs() == 1) {
48
E = IgnoreTrivials(CE->getArg(0));
49
}
50
51
// It is possible that the QualType doesn't point to a type yet so we are
52
// not interested.
53
if (E->getType().isNull()) {
54
return;
55
}
56
57
// We allow taking a kungFuDeathGrip of `this` because it cannot change
58
// beneath us, so calling directly through `this` is OK. This is the same
59
// for local variable declarations.
60
//
61
// We also don't complain about unused RefPtrs which are constructed from
62
// the return value of a new expression, as these are required in order to
63
// immediately destroy the value created (which was presumably created for
64
// its side effects), and are not used as a death grip.
65
if (isa<CXXThisExpr>(E) || isa<DeclRefExpr>(E) || isa<CXXNewExpr>(E)) {
66
return;
67
}
68
69
// These types are assigned into nsCOMPtr and RefPtr for their side effects,
70
// and not as a kungFuDeathGrip. We don't want to consider RefPtr and nsCOMPtr
71
// types which are initialized with these types as errors.
72
const TagDecl *TD = E->getType()->getAsTagDecl();
73
if (TD && TD->getIdentifier()) {
74
static const char *IgnoreTypes[] = {
75
"already_AddRefed",
76
"nsGetServiceByCID",
77
"nsGetServiceByCIDWithError",
78
"nsGetServiceByContractID",
79
"nsGetServiceByContractIDWithError",
80
"nsCreateInstanceByCID",
81
"nsCreateInstanceByContractID",
82
"nsCreateInstanceFromFactory",
83
};
84
85
for (uint32_t i = 0; i < sizeof(IgnoreTypes) / sizeof(IgnoreTypes[0]);
86
++i) {
87
if (TD->getName() == IgnoreTypes[i]) {
88
return;
89
}
90
}
91
}
92
93
// Report the error
94
const char *ErrThing;
95
const char *NoteThing;
96
if (isa<MemberExpr>(E)) {
97
ErrThing = "members";
98
NoteThing = "member";
99
} else {
100
ErrThing = "temporary values";
101
NoteThing = "value";
102
}
103
104
// We cannot provide the note if we don't have an initializer
105
diag(D->getBeginLoc(), Error, DiagnosticIDs::Error)
106
<< D->getType() << ErrThing;
107
diag(E->getBeginLoc(), Note, DiagnosticIDs::Note)
108
<< NoteThing << getNameChecked(D);
109
}