blob: 9214a7da2729725c6fb06ac29ebaa58c9b66ae4b [file] [log] [blame]
Gabor Horvath0b16c102017-08-10 13:30:30 +00001//===--- IntegerDivisionCheck.cpp - clang-tidy-----------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Gabor Horvath0b16c102017-08-10 13:30:30 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "IntegerDivisionCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang {
16namespace tidy {
17namespace bugprone {
18
19void IntegerDivisionCheck::registerMatchers(MatchFinder *Finder) {
20 const auto IntType = hasType(isInteger());
21
Nathan James97572fa2020-03-10 00:42:21 +000022 const auto BinaryOperators = binaryOperator(
23 hasAnyOperatorName("%", "<<", ">>", "<<", "^", "|", "&", "||", "&&", "<",
24 ">", "<=", ">=", "==", "!="));
Gabor Horvath0b16c102017-08-10 13:30:30 +000025
Nathan James97572fa2020-03-10 00:42:21 +000026 const auto UnaryOperators = unaryOperator(hasAnyOperatorName("~", "!"));
Gabor Horvath0b16c102017-08-10 13:30:30 +000027
28 const auto Exceptions =
29 anyOf(BinaryOperators, conditionalOperator(), binaryConditionalOperator(),
30 callExpr(IntType), explicitCastExpr(IntType), UnaryOperators);
31
32 Finder->addMatcher(
Stephen Kellya72307c2019-11-12 15:15:56 +000033 traverse(ast_type_traits::TK_AsIs,
34 binaryOperator(
35 hasOperatorName("/"), hasLHS(expr(IntType)),
36 hasRHS(expr(IntType)),
37 hasAncestor(castExpr(hasCastKind(CK_IntegralToFloating))
38 .bind("FloatCast")),
39 unless(hasAncestor(expr(
40 Exceptions,
41 hasAncestor(castExpr(equalsBoundNode("FloatCast")))))))
42 .bind("IntDiv")),
Gabor Horvath0b16c102017-08-10 13:30:30 +000043 this);
44}
45
46void IntegerDivisionCheck::check(const MatchFinder::MatchResult &Result) {
47 const auto *IntDiv = Result.Nodes.getNodeAs<BinaryOperator>("IntDiv");
Stephen Kelly43465bf2018-08-09 22:42:26 +000048 diag(IntDiv->getBeginLoc(), "result of integer division used in a floating "
Gabor Horvath0b16c102017-08-10 13:30:30 +000049 "point context; possible loss of precision");
50}
51
52} // namespace bugprone
53} // namespace tidy
54} // namespace clang