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/. */
5 #include "KungFuDeathGripChecker.h"
6 #include "CustomMatchers.h"
8 void KungFuDeathGripChecker::registerMatchers(MatchFinder
*AstMatcher
) {
9 AstMatcher
->addMatcher(varDecl(hasType(isRefPtr())).bind("decl"), this);
12 void KungFuDeathGripChecker::check(const MatchFinder::MatchResult
&Result
) {
13 const char *Error
= "Unused \"kungFuDeathGrip\" %0 objects constructed from "
15 const char *Note
= "Please switch all accesses to this %0 to go through "
16 "'%1', or explicitly pass '%1' to `mozilla::Unused`";
18 const VarDecl
*D
= Result
.Nodes
.getNodeAs
<VarDecl
>("decl");
19 if (D
->isReferenced() || !D
->hasLocalStorage() || !D
->hasInit()) {
23 // Not interested in parameters.
24 if (isa
<ImplicitParamDecl
>(D
) || isa
<ParmVarDecl
>(D
)) {
28 const Expr
*E
= IgnoreTrivials(D
->getInit());
29 const CXXConstructExpr
*CE
= dyn_cast
<CXXConstructExpr
>(E
);
30 if (CE
&& CE
->getNumArgs() == 0) {
31 // We don't report an error when we construct and don't use a nsCOMPtr /
32 // nsRefPtr with no arguments. We don't report it because the error is not
33 // related to the current check. In the future it may be reported through a
34 // more generic mechanism.
38 // We don't want to look at the single argument conversion constructors
39 // which are inbetween the declaration and the actual object which we are
40 // assigning into the nsCOMPtr/RefPtr. To do this, we repeatedly
41 // IgnoreTrivials, then look at the expression. If it is one of these
42 // conversion constructors, we ignore it and continue to dig.
43 while ((CE
= dyn_cast
<CXXConstructExpr
>(E
)) && CE
->getNumArgs() == 1) {
44 E
= IgnoreTrivials(CE
->getArg(0));
47 // We allow taking a kungFuDeathGrip of `this` because it cannot change
48 // beneath us, so calling directly through `this` is OK. This is the same
49 // for local variable declarations.
51 // We also don't complain about unused RefPtrs which are constructed from
52 // the return value of a new expression, as these are required in order to
53 // immediately destroy the value created (which was presumably created for
54 // its side effects), and are not used as a death grip.
55 if (isa
<CXXThisExpr
>(E
) || isa
<DeclRefExpr
>(E
) || isa
<CXXNewExpr
>(E
)) {
59 // These types are assigned into nsCOMPtr and RefPtr for their side effects,
60 // and not as a kungFuDeathGrip. We don't want to consider RefPtr and nsCOMPtr
61 // types which are initialized with these types as errors.
62 const TagDecl
*TD
= E
->getType()->getAsTagDecl();
63 if (TD
&& TD
->getIdentifier()) {
64 static const char *IgnoreTypes
[] = {
67 "nsGetServiceByCIDWithError",
68 "nsGetServiceByContractID",
69 "nsGetServiceByContractIDWithError",
70 "nsCreateInstanceByCID",
71 "nsCreateInstanceByContractID",
72 "nsCreateInstanceFromFactory",
75 for (uint32_t i
= 0; i
< sizeof(IgnoreTypes
) / sizeof(IgnoreTypes
[0]);
77 if (TD
->getName() == IgnoreTypes
[i
]) {
85 const char *NoteThing
;
86 if (isa
<MemberExpr
>(E
)) {
90 ErrThing
= "temporary values";
94 // We cannot provide the note if we don't have an initializer
95 diag(D
->getLocStart(), Error
, DiagnosticIDs::Error
)
96 << D
->getType() << ErrThing
;
97 diag(E
->getLocStart(), Note
, DiagnosticIDs::Note
)
98 << NoteThing
<< getNameChecked(D
);