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 
registerMatchers(MatchFinder * AstMatcher)8 void KungFuDeathGripChecker::registerMatchers(MatchFinder *AstMatcher) {
9   AstMatcher->addMatcher(varDecl(hasType(isRefPtr())).bind("decl"), this);
10 }
11 
check(const MatchFinder::MatchResult & Result)12 void KungFuDeathGripChecker::check(const MatchFinder::MatchResult &Result) {
13   const char *Error = "Unused \"kungFuDeathGrip\" %0 objects constructed from "
14                       "%1 are prohibited";
15   const char *Note = "Please switch all accesses to this %0 to go through "
16                      "'%1', or explicitly pass '%1' to `mozilla::Unused`";
17 
18   const VarDecl *D = Result.Nodes.getNodeAs<VarDecl>("decl");
19   if (D->isReferenced() || !D->hasLocalStorage() || !D->hasInit()) {
20     return;
21   }
22 
23   // Not interested in parameters.
24   if (isa<ImplicitParamDecl>(D) || isa<ParmVarDecl>(D)) {
25     return;
26   }
27 
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.
35     return;
36   }
37 
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));
45   }
46 
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.
50   //
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)) {
56     return;
57   }
58 
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[] = {
65         "already_AddRefed",
66         "nsGetServiceByCID",
67         "nsGetServiceByCIDWithError",
68         "nsGetServiceByContractID",
69         "nsGetServiceByContractIDWithError",
70         "nsCreateInstanceByCID",
71         "nsCreateInstanceByContractID",
72         "nsCreateInstanceFromFactory",
73     };
74 
75     for (uint32_t i = 0; i < sizeof(IgnoreTypes) / sizeof(IgnoreTypes[0]);
76          ++i) {
77       if (TD->getName() == IgnoreTypes[i]) {
78         return;
79       }
80     }
81   }
82 
83   // Report the error
84   const char *ErrThing;
85   const char *NoteThing;
86   if (isa<MemberExpr>(E)) {
87     ErrThing = "members";
88     NoteThing = "member";
89   } else {
90     ErrThing = "temporary values";
91     NoteThing = "value";
92   }
93 
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);
99 }
100