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 "ExplicitOperatorBoolChecker.h"
6
#include "CustomMatchers.h"
7
8
void ExplicitOperatorBoolChecker::registerMatchers(MatchFinder *AstMatcher) {
9
// Older clang versions such as the ones used on the infra recognize these
10
// conversions as 'operator _Bool', but newer clang versions recognize these
11
// as 'operator bool'.
12
AstMatcher->addMatcher(
13
cxxMethodDecl(anyOf(hasName("operator bool"), hasName("operator _Bool")))
14
.bind("node"),
15
this);
16
}
17
18
void ExplicitOperatorBoolChecker::check(
19
const MatchFinder::MatchResult &Result) {
20
const CXXConversionDecl *Method =
21
Result.Nodes.getNodeAs<CXXConversionDecl>("node");
22
const CXXRecordDecl *Clazz = Method->getParent();
23
24
if (!Method->isExplicit() &&
25
!hasCustomAttribute<moz_implicit>(Method) &&
26
!ASTIsInSystemHeader(Method->getASTContext(), *Method) &&
27
isInterestingDeclForImplicitConversion(Method)) {
28
diag(Method->getBeginLoc(), "bad implicit conversion operator for %0",
29
DiagnosticIDs::Error)
30
<< Clazz;
31
diag(Method->getBeginLoc(), "consider adding the explicit keyword to %0",
32
DiagnosticIDs::Note)
33
<< "'operator bool'";
34
}
35
}