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 "NaNExprChecker.h"
6
#include "CustomMatchers.h"
7
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);
19
}
20
21
void NaNExprChecker::check(const MatchFinder::MatchResult &Result) {
22
if (!Result.Context->getLangOpts().CPlusPlus) {
23
// mozilla::IsNaN is not usable in C, so there is no point in issuing these
24
// warnings.
25
return;
26
}
27
28
const BinaryOperator *Expression =
29
Result.Nodes.getNodeAs<BinaryOperator>("node");
30
const DeclRefExpr *LHS = Result.Nodes.getNodeAs<DeclRefExpr>("lhs");
31
const DeclRefExpr *RHS = Result.Nodes.getNodeAs<DeclRefExpr>("rhs");
32
const ImplicitCastExpr *LHSExpr =
33
dyn_cast<ImplicitCastExpr>(Expression->getLHS());
34
const ImplicitCastExpr *RHSExpr =
35
dyn_cast<ImplicitCastExpr>(Expression->getRHS());
36
// The AST subtree that we are looking for will look like this:
37
// -BinaryOperator ==/!=
38
// |-ImplicitCastExpr LValueToRValue
39
// | |-DeclRefExpr
40
// |-ImplicitCastExpr LValueToRValue
41
// |-DeclRefExpr
42
// The check below ensures that we are dealing with the correct AST subtree
43
// shape, and
44
// also that both of the found DeclRefExpr's point to the same declaration.
45
if (LHS->getFoundDecl() == RHS->getFoundDecl() && LHSExpr && RHSExpr &&
46
std::distance(LHSExpr->child_begin(), LHSExpr->child_end()) == 1 &&
47
std::distance(RHSExpr->child_begin(), RHSExpr->child_end()) == 1 &&
48
*LHSExpr->child_begin() == LHS && *RHSExpr->child_begin() == RHS) {
49
diag(Expression->getBeginLoc(),
50
"comparing a floating point value to itself for "
51
"NaN checking can lead to incorrect results",
52
DiagnosticIDs::Error);
53
diag(Expression->getBeginLoc(), "consider using mozilla::IsNaN instead",
54
DiagnosticIDs::Note);
55
}
56
}