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 "RefCountedCopyConstructorChecker.h"
6 #include "CustomMatchers.h"
7 
registerMatchers(MatchFinder * AstMatcher)8 void RefCountedCopyConstructorChecker::registerMatchers(
9     MatchFinder *AstMatcher) {
10   AstMatcher->addMatcher(
11       cxxConstructExpr(
12           hasDeclaration(cxxConstructorDecl(isCompilerProvidedCopyConstructor(),
13                                             ofClass(hasRefCntMember()))))
14           .bind("node"),
15       this);
16 }
17 
check(const MatchFinder::MatchResult & Result)18 void RefCountedCopyConstructorChecker::check(
19     const MatchFinder::MatchResult &Result) {
20   const char *Error =
21       "Invalid use of compiler-provided copy constructor on refcounted type";
22   const char *Note = "The default copy constructor also copies the "
23                      "default mRefCnt property, leading to reference "
24                      "count imbalance issues. Please provide your own "
25                      "copy constructor which only copies the fields which "
26                      "need to be copied";
27 
28   // Everything we needed to know was checked in the matcher - we just report
29   // the error here
30   const CXXConstructExpr *E = Result.Nodes.getNodeAs<CXXConstructExpr>("node");
31 
32   diag(E->getLocation(), Error, DiagnosticIDs::Error);
33   diag(E->getLocation(), Note, DiagnosticIDs::Note);
34 }
35