2016-12-17 21:35:53 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ExplicitOperatorBoolChecker.h"
|
|
|
|
#include "CustomMatchers.h"
|
|
|
|
|
2017-10-20 17:11:50 +00:00
|
|
|
void ExplicitOperatorBoolChecker::registerMatchers(MatchFinder *AstMatcher) {
|
2016-12-17 21:35:53 +00:00
|
|
|
// Older clang versions such as the ones used on the infra recognize these
|
|
|
|
// conversions as 'operator _Bool', but newer clang versions recognize these
|
|
|
|
// as 'operator bool'.
|
2016-12-18 02:14:37 +00:00
|
|
|
AstMatcher->addMatcher(
|
2016-12-17 21:35:53 +00:00
|
|
|
cxxMethodDecl(anyOf(hasName("operator bool"), hasName("operator _Bool")))
|
|
|
|
.bind("node"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2016-12-18 02:14:37 +00:00
|
|
|
void ExplicitOperatorBoolChecker::check(
|
2016-12-17 21:35:53 +00:00
|
|
|
const MatchFinder::MatchResult &Result) {
|
|
|
|
const CXXConversionDecl *Method =
|
|
|
|
Result.Nodes.getNodeAs<CXXConversionDecl>("node");
|
|
|
|
const CXXRecordDecl *Clazz = Method->getParent();
|
|
|
|
|
|
|
|
if (!Method->isExplicitSpecified() &&
|
2018-09-18 13:03:33 +00:00
|
|
|
!hasCustomAttribute<moz_implicit>(Method) &&
|
2016-12-17 21:35:53 +00:00
|
|
|
!ASTIsInSystemHeader(Method->getASTContext(), *Method) &&
|
|
|
|
isInterestingDeclForImplicitConversion(Method)) {
|
2018-11-03 13:41:36 +00:00
|
|
|
diag(Method->getBeginLoc(), "bad implicit conversion operator for %0",
|
2017-10-20 17:11:50 +00:00
|
|
|
DiagnosticIDs::Error)
|
|
|
|
<< Clazz;
|
2018-11-03 13:41:36 +00:00
|
|
|
diag(Method->getBeginLoc(), "consider adding the explicit keyword to %0",
|
2017-10-20 17:11:50 +00:00
|
|
|
DiagnosticIDs::Note)
|
|
|
|
<< "'operator bool'";
|
2016-12-17 21:35:53 +00:00
|
|
|
}
|
|
|
|
}
|