[TargetVersion] Only enable on RISC-V and AArch64 (#115991)
[llvm-project.git] / clang-tools-extra / clang-tidy / bugprone / IntegerDivisionCheck.cpp
blob92e0413caceaa9b0665f436e20200cb7f2f4a598
1 //===--- IntegerDivisionCheck.cpp - clang-tidy-----------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "IntegerDivisionCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 using namespace clang::ast_matchers;
15 namespace clang::tidy::bugprone {
17 void IntegerDivisionCheck::registerMatchers(MatchFinder *Finder) {
18 const auto IntType = hasType(isInteger());
20 const auto BinaryOperators = binaryOperator(
21 hasAnyOperatorName("%", "<<", ">>", "<<", "^", "|", "&", "||", "&&", "<",
22 ">", "<=", ">=", "==", "!="));
24 const auto UnaryOperators = unaryOperator(hasAnyOperatorName("~", "!"));
26 const auto Exceptions =
27 anyOf(BinaryOperators, conditionalOperator(), binaryConditionalOperator(),
28 callExpr(IntType), explicitCastExpr(IntType), UnaryOperators);
30 Finder->addMatcher(
31 traverse(TK_AsIs,
32 binaryOperator(
33 hasOperatorName("/"), hasLHS(expr(IntType)),
34 hasRHS(expr(IntType)),
35 hasAncestor(castExpr(hasCastKind(CK_IntegralToFloating))
36 .bind("FloatCast")),
37 unless(hasAncestor(expr(
38 Exceptions,
39 hasAncestor(castExpr(equalsBoundNode("FloatCast")))))))
40 .bind("IntDiv")),
41 this);
44 void IntegerDivisionCheck::check(const MatchFinder::MatchResult &Result) {
45 const auto *IntDiv = Result.Nodes.getNodeAs<BinaryOperator>("IntDiv");
46 diag(IntDiv->getBeginLoc(), "result of integer division used in a floating "
47 "point context; possible loss of precision");
50 } // namespace clang::tidy::bugprone