Bug 1523562 [wpt PR 14965] - Sync Mozilla CSS tests as of 2019-01-20, a=testonly
[gecko.git] / build / clang-plugin / ExplicitOperatorBoolChecker.cpp
blobc748167975cc2ab85b33e4d27d6c109709c9d937
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/. */
5 #include "ExplicitOperatorBoolChecker.h"
6 #include "CustomMatchers.h"
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);
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();
24 if (!Method->isExplicitSpecified() &&
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'";