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 "ArithmeticArgChecker.h"
6
#include "CustomMatchers.h"
7
8
void ArithmeticArgChecker::registerMatchers(MatchFinder *AstMatcher) {
9
AstMatcher->addMatcher(
10
callExpr(allOf(hasDeclaration(noArithmeticExprInArgs()),
11
anyOf(hasDescendant(
12
binaryOperator(
13
allOf(binaryArithmeticOperator(),
14
hasLHS(hasDescendant(declRefExpr())),
15
hasRHS(hasDescendant(declRefExpr()))))
16
.bind("node")),
17
hasDescendant(
18
unaryOperator(
19
allOf(unaryArithmeticOperator(),
20
hasUnaryOperand(allOf(
21
hasType(builtinType()),
22
anyOf(hasDescendant(declRefExpr()),
23
declRefExpr())))))
24
.bind("node")))))
25
.bind("call"),
26
this);
27
AstMatcher->addMatcher(
28
cxxConstructExpr(
29
allOf(hasDeclaration(noArithmeticExprInArgs()),
30
anyOf(hasDescendant(
31
binaryOperator(
32
allOf(binaryArithmeticOperator(),
33
hasLHS(hasDescendant(declRefExpr())),
34
hasRHS(hasDescendant(declRefExpr()))))
35
.bind("node")),
36
hasDescendant(
37
unaryOperator(
38
allOf(unaryArithmeticOperator(),
39
hasUnaryOperand(allOf(
40
hasType(builtinType()),
41
anyOf(hasDescendant(declRefExpr()),
42
declRefExpr())))))
43
.bind("node")))))
44
.bind("call"),
45
this);
46
}
47
48
void ArithmeticArgChecker::check(const MatchFinder::MatchResult &Result) {
49
const char *Error =
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();
59
}
60
}