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 "ArithmeticArgChecker.h"
6 #include "CustomMatchers.h"
8 void ArithmeticArgChecker::registerMatchers(MatchFinder
*AstMatcher
) {
9 AstMatcher
->addMatcher(
10 callExpr(allOf(hasDeclaration(noArithmeticExprInArgs()),
13 allOf(binaryArithmeticOperator(),
14 hasLHS(hasDescendant(declRefExpr())),
15 hasRHS(hasDescendant(declRefExpr()))))
19 allOf(unaryArithmeticOperator(),
20 hasUnaryOperand(allOf(
21 hasType(builtinType()),
22 anyOf(hasDescendant(declRefExpr()),
27 AstMatcher
->addMatcher(
29 allOf(hasDeclaration(noArithmeticExprInArgs()),
32 allOf(binaryArithmeticOperator(),
33 hasLHS(hasDescendant(declRefExpr())),
34 hasRHS(hasDescendant(declRefExpr()))))
38 allOf(unaryArithmeticOperator(),
39 hasUnaryOperand(allOf(
40 hasType(builtinType()),
41 anyOf(hasDescendant(declRefExpr()),
48 void ArithmeticArgChecker::check(const MatchFinder::MatchResult
&Result
) {
50 "cannot pass an arithmetic expression of built-in types to %0";
51 const Expr
*Expression
= Result
.Nodes
.getNodeAs
<Expr
>("node");
52 if (const CallExpr
*Call
= Result
.Nodes
.getNodeAs
<CallExpr
>("call")) {
53 diag(Expression
->getBeginLoc(), Error
, DiagnosticIDs::Error
)
54 << Call
->getDirectCallee();
55 } else if (const CXXConstructExpr
*Ctr
=
56 Result
.Nodes
.getNodeAs
<CXXConstructExpr
>("call")) {
57 diag(Expression
->getBeginLoc(), Error
, DiagnosticIDs::Error
)
58 << Ctr
->getConstructor();