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 "NoAddRefReleaseOnReturnChecker.h"
6
#include "CustomMatchers.h"
7
8
void NoAddRefReleaseOnReturnChecker::registerMatchers(MatchFinder *AstMatcher) {
9
// Look for all of the calls to AddRef() or Release()
10
AstMatcher->addMatcher(
11
memberExpr(isAddRefOrRelease(), hasParent(callExpr())).bind("member"),
12
this);
13
}
14
15
void NoAddRefReleaseOnReturnChecker::check(
16
const MatchFinder::MatchResult &Result) {
17
const MemberExpr *Member = Result.Nodes.getNodeAs<MemberExpr>("member");
18
const Expr *Base = IgnoreTrivials(Member->getBase());
19
20
// Check if the call to AddRef() or Release() was made on the result of a call
21
// to a MOZ_NO_ADDREF_RELEASE_ON_RETURN function or method.
22
if (auto *Call = dyn_cast<CallExpr>(Base)) {
23
if (auto *Callee = Call->getDirectCallee()) {
24
if (hasCustomAttribute<moz_no_addref_release_on_return>(Callee)) {
25
diag(Call->getBeginLoc(),
26
"%1 cannot be called on the return value of %0",
27
DiagnosticIDs::Error)
28
<< Callee << dyn_cast<CXXMethodDecl>(Member->getMemberDecl());
29
}
30
}
31
}
32
}