blob: ab7b28d69f48c88c443b44d54448ecf1c82cce16 [file] [log] [blame]
Alexander Kornienko4256fd02018-01-30 15:12:24 +00001//===--- IncorrectRoundingsCheck.cpp - clang-tidy ------------------------------===//
Haojian Wu25779e42016-02-08 10:16:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Alexander Kornienko4256fd02018-01-30 15:12:24 +000010#include "IncorrectRoundingsCheck.h"
Haojian Wu25779e42016-02-08 10:16:13 +000011#include "clang/AST/DeclBase.h"
12#include "clang/AST/Type.h"
13#include "clang/ASTMatchers/ASTMatchFinder.h"
14#include "clang/ASTMatchers/ASTMatchers.h"
15#include "clang/Lex/Lexer.h"
16
Etienne Bergeron456177b2016-05-02 18:00:29 +000017using namespace clang::ast_matchers;
18
Haojian Wu25779e42016-02-08 10:16:13 +000019namespace clang {
Etienne Bergeron456177b2016-05-02 18:00:29 +000020namespace tidy {
Alexander Kornienko4256fd02018-01-30 15:12:24 +000021namespace bugprone {
Etienne Bergeron456177b2016-05-02 18:00:29 +000022
23namespace {
Haojian Wu25779e42016-02-08 10:16:13 +000024AST_MATCHER(FloatingLiteral, floatHalf) {
25 const auto &literal = Node.getValue();
Stephan Bergmann17c7f702016-12-14 11:57:17 +000026 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle())
Haojian Wu25779e42016-02-08 10:16:13 +000027 return literal.convertToFloat() == 0.5f;
Stephan Bergmann17c7f702016-12-14 11:57:17 +000028 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble())
Haojian Wu25779e42016-02-08 10:16:13 +000029 return literal.convertToDouble() == 0.5;
30 return false;
31}
Etienne Bergeron456177b2016-05-02 18:00:29 +000032} // namespace
Haojian Wu25779e42016-02-08 10:16:13 +000033
Alexander Kornienko4256fd02018-01-30 15:12:24 +000034void IncorrectRoundingsCheck::registerMatchers(MatchFinder *MatchFinder) {
Haojian Wu25779e42016-02-08 10:16:13 +000035 // Match a floating literal with value 0.5.
36 auto FloatHalf = floatLiteral(floatHalf());
37
38 // Match a floating point expression.
Etienne Bergeron9d265992016-04-21 16:57:56 +000039 auto FloatType = expr(hasType(realFloatingPointType()));
Haojian Wu25779e42016-02-08 10:16:13 +000040
41 // Match a floating literal of 0.5 or a floating literal of 0.5 implicitly.
42 // cast to floating type.
43 auto FloatOrCastHalf =
Piotr Padlewskie93a73f2016-05-31 15:26:56 +000044 anyOf(FloatHalf,
45 implicitCastExpr(FloatType, has(ignoringParenImpCasts(FloatHalf))));
Haojian Wu25779e42016-02-08 10:16:13 +000046
47 // Match if either the LHS or RHS is a floating literal of 0.5 or a floating
48 // literal of 0.5 and the other is of type double or vice versa.
49 auto OneSideHalf = anyOf(allOf(hasLHS(FloatOrCastHalf), hasRHS(FloatType)),
50 allOf(hasRHS(FloatOrCastHalf), hasLHS(FloatType)));
51
52 // Find expressions of cast to int of the sum of a floating point expression
53 // and 0.5.
54 MatchFinder->addMatcher(
55 implicitCastExpr(
56 hasImplicitDestinationType(isInteger()),
57 ignoringParenCasts(binaryOperator(hasOperatorName("+"), OneSideHalf)))
58 .bind("CastExpr"),
59 this);
60}
61
Alexander Kornienko4256fd02018-01-30 15:12:24 +000062void IncorrectRoundingsCheck::check(const MatchFinder::MatchResult &Result) {
Alexander Kornienko9f58fe02016-12-13 16:19:19 +000063 const auto *CastExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>("CastExpr");
Haojian Wu25779e42016-02-08 10:16:13 +000064 diag(CastExpr->getLocStart(),
65 "casting (double + 0.5) to integer leads to incorrect rounding; "
66 "consider using lround (#include <cmath>) instead");
67}
68
Alexander Kornienko4256fd02018-01-30 15:12:24 +000069} // namespace bugprone
Haojian Wu25779e42016-02-08 10:16:13 +000070} // namespace tidy
71} // namespace clang