Bug 1814798 - pt 1. Add bool to enable/disable PHC at runtime r=glandium
[gecko.git] / build / clang-plugin / NaNExprChecker.cpp
blobbacebc91559788d8369e7fa17fba6d718cd447f1
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 "NaNExprChecker.h"
6 #include "CustomMatchers.h"
8 void NaNExprChecker::registerMatchers(MatchFinder *AstMatcher) {
9 AstMatcher->addMatcher(
10 binaryOperator(
11 allOf(binaryEqualityOperator(),
12 hasLHS(has(ignoringParenImpCasts(
13 declRefExpr(hasType(qualType((isFloat())))).bind("lhs")))),
14 hasRHS(has(ignoringParenImpCasts(
15 declRefExpr(hasType(qualType((isFloat())))).bind("rhs")))),
16 unless(anyOf(isInSystemHeader(), isInWhitelistForNaNExpr()))))
17 .bind("node"),
18 this);
21 void NaNExprChecker::check(const MatchFinder::MatchResult &Result) {
22 if (!Result.Context->getLangOpts().CPlusPlus) {
23 return;
26 const BinaryOperator *Expression =
27 Result.Nodes.getNodeAs<BinaryOperator>("node");
28 const DeclRefExpr *LHS = Result.Nodes.getNodeAs<DeclRefExpr>("lhs");
29 const DeclRefExpr *RHS = Result.Nodes.getNodeAs<DeclRefExpr>("rhs");
30 const ImplicitCastExpr *LHSExpr =
31 dyn_cast<ImplicitCastExpr>(Expression->getLHS());
32 const ImplicitCastExpr *RHSExpr =
33 dyn_cast<ImplicitCastExpr>(Expression->getRHS());
34 // The AST subtree that we are looking for will look like this:
35 // -BinaryOperator ==/!=
36 // |-ImplicitCastExpr LValueToRValue
37 // | |-DeclRefExpr
38 // |-ImplicitCastExpr LValueToRValue
39 // |-DeclRefExpr
40 // The check below ensures that we are dealing with the correct AST subtree
41 // shape, and
42 // also that both of the found DeclRefExpr's point to the same declaration.
43 if (LHS->getFoundDecl() == RHS->getFoundDecl() && LHSExpr && RHSExpr &&
44 std::distance(LHSExpr->child_begin(), LHSExpr->child_end()) == 1 &&
45 std::distance(RHSExpr->child_begin(), RHSExpr->child_end()) == 1 &&
46 *LHSExpr->child_begin() == LHS && *RHSExpr->child_begin() == RHS) {
47 diag(Expression->getBeginLoc(),
48 "comparing a floating point value to itself for "
49 "NaN checking can lead to incorrect results",
50 DiagnosticIDs::Error);
51 diag(Expression->getBeginLoc(), "consider using std::isnan instead",
52 DiagnosticIDs::Note);