2016-12-18 00:35:53 +03: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 "ArithmeticArgChecker.h"
|
|
|
|
#include "CustomMatchers.h"
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
void ArithmeticArgChecker::registerMatchers(MatchFinder *AstMatcher) {
|
2016-12-18 05:14:37 +03:00
|
|
|
AstMatcher->addMatcher(
|
2016-12-18 00:35:53 +03:00
|
|
|
callExpr(allOf(hasDeclaration(noArithmeticExprInArgs()),
|
|
|
|
anyOf(hasDescendant(
|
|
|
|
binaryOperator(
|
|
|
|
allOf(binaryArithmeticOperator(),
|
|
|
|
hasLHS(hasDescendant(declRefExpr())),
|
|
|
|
hasRHS(hasDescendant(declRefExpr()))))
|
|
|
|
.bind("node")),
|
|
|
|
hasDescendant(
|
|
|
|
unaryOperator(
|
|
|
|
allOf(unaryArithmeticOperator(),
|
|
|
|
hasUnaryOperand(allOf(
|
|
|
|
hasType(builtinType()),
|
|
|
|
anyOf(hasDescendant(declRefExpr()),
|
|
|
|
declRefExpr())))))
|
|
|
|
.bind("node")))))
|
|
|
|
.bind("call"),
|
|
|
|
this);
|
2016-12-18 05:14:37 +03:00
|
|
|
AstMatcher->addMatcher(
|
2016-12-18 00:35:53 +03:00
|
|
|
cxxConstructExpr(
|
|
|
|
allOf(hasDeclaration(noArithmeticExprInArgs()),
|
|
|
|
anyOf(hasDescendant(
|
|
|
|
binaryOperator(
|
|
|
|
allOf(binaryArithmeticOperator(),
|
|
|
|
hasLHS(hasDescendant(declRefExpr())),
|
|
|
|
hasRHS(hasDescendant(declRefExpr()))))
|
|
|
|
.bind("node")),
|
|
|
|
hasDescendant(
|
|
|
|
unaryOperator(
|
|
|
|
allOf(unaryArithmeticOperator(),
|
|
|
|
hasUnaryOperand(allOf(
|
|
|
|
hasType(builtinType()),
|
|
|
|
anyOf(hasDescendant(declRefExpr()),
|
|
|
|
declRefExpr())))))
|
|
|
|
.bind("node")))))
|
|
|
|
.bind("call"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2017-10-20 20:11:50 +03:00
|
|
|
void ArithmeticArgChecker::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
const char *Error =
|
|
|
|
"cannot pass an arithmetic expression of built-in types to %0";
|
2016-12-18 00:35:53 +03:00
|
|
|
const Expr *Expression = Result.Nodes.getNodeAs<Expr>("node");
|
|
|
|
if (const CallExpr *Call = Result.Nodes.getNodeAs<CallExpr>("call")) {
|
2018-11-03 16:41:36 +03:00
|
|
|
diag(Expression->getBeginLoc(), Error, DiagnosticIDs::Error)
|
2017-10-20 20:11:50 +03:00
|
|
|
<< Call->getDirectCallee();
|
2016-12-18 00:35:53 +03:00
|
|
|
} else if (const CXXConstructExpr *Ctr =
|
|
|
|
Result.Nodes.getNodeAs<CXXConstructExpr>("call")) {
|
2018-11-03 16:41:36 +03:00
|
|
|
diag(Expression->getBeginLoc(), Error, DiagnosticIDs::Error)
|
2017-10-20 20:11:50 +03:00
|
|
|
<< Ctr->getConstructor();
|
2016-12-18 00:35:53 +03:00
|
|
|
}
|
|
|
|
}
|