10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
19 void IntegerDivisionCheck::registerMatchers(MatchFinder *Finder) {
20 const auto IntType = hasType(isInteger());
22 const auto BinaryOperators = binaryOperator(
23 hasAnyOperatorName(
"%",
"<<",
">>",
"<<",
"^",
"|",
"&",
"||",
"&&",
"<",
24 ">",
"<=",
">=",
"==",
"!="));
26 const auto UnaryOperators = unaryOperator(hasAnyOperatorName(
"~",
"!"));
28 const auto Exceptions =
29 anyOf(BinaryOperators, conditionalOperator(), binaryConditionalOperator(),
30 callExpr(IntType), explicitCastExpr(IntType), UnaryOperators);
33 traverse(ast_type_traits::TK_AsIs,
35 hasOperatorName(
"/"), hasLHS(expr(IntType)),
36 hasRHS(expr(IntType)),
37 hasAncestor(castExpr(hasCastKind(CK_IntegralToFloating))
39 unless(hasAncestor(expr(
41 hasAncestor(castExpr(equalsBoundNode(
"FloatCast")))))))
46 void IntegerDivisionCheck::check(
const MatchFinder::MatchResult &Result) {
47 const auto *IntDiv = Result.Nodes.getNodeAs<BinaryOperator>(
"IntDiv");
48 diag(IntDiv->getBeginLoc(),
"result of integer division used in a floating "
49 "point context; possible loss of precision");