blob: ee6f666f0cef22c04d86b289a07e3cc96573398a [file] [log] [blame]
Haojian Wu25779e42016-02-08 10:16:13 +00001//===--- IncorrectRoundings.cpp - clang-tidy ------------------------------===//
2//
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
10#include "IncorrectRoundings.h"
11#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 {
21namespace misc {
22
23namespace {
Haojian Wu25779e42016-02-08 10:16:13 +000024AST_MATCHER(FloatingLiteral, floatHalf) {
25 const auto &literal = Node.getValue();
26 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle)
27 return literal.convertToFloat() == 0.5f;
28 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble)
29 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
Haojian Wu25779e42016-02-08 10:16:13 +000034
Haojian Wu25779e42016-02-08 10:16:13 +000035void IncorrectRoundings::registerMatchers(MatchFinder *MatchFinder) {
36 // Match a floating literal with value 0.5.
37 auto FloatHalf = floatLiteral(floatHalf());
38
39 // Match a floating point expression.
Etienne Bergeron9d265992016-04-21 16:57:56 +000040 auto FloatType = expr(hasType(realFloatingPointType()));
Haojian Wu25779e42016-02-08 10:16:13 +000041
42 // Match a floating literal of 0.5 or a floating literal of 0.5 implicitly.
43 // cast to floating type.
44 auto FloatOrCastHalf =
Piotr Padlewskie93a73f2016-05-31 15:26:56 +000045 anyOf(FloatHalf,
46 implicitCastExpr(FloatType, has(ignoringParenImpCasts(FloatHalf))));
Haojian Wu25779e42016-02-08 10:16:13 +000047
48 // Match if either the LHS or RHS is a floating literal of 0.5 or a floating
49 // literal of 0.5 and the other is of type double or vice versa.
50 auto OneSideHalf = anyOf(allOf(hasLHS(FloatOrCastHalf), hasRHS(FloatType)),
51 allOf(hasRHS(FloatOrCastHalf), hasLHS(FloatType)));
52
53 // Find expressions of cast to int of the sum of a floating point expression
54 // and 0.5.
55 MatchFinder->addMatcher(
56 implicitCastExpr(
57 hasImplicitDestinationType(isInteger()),
58 ignoringParenCasts(binaryOperator(hasOperatorName("+"), OneSideHalf)))
59 .bind("CastExpr"),
60 this);
61}
62
63void IncorrectRoundings::check(const MatchFinder::MatchResult &Result) {
64 const auto *CastExpr = Result.Nodes.getStmtAs<ImplicitCastExpr>("CastExpr");
65 diag(CastExpr->getLocStart(),
66 "casting (double + 0.5) to integer leads to incorrect rounding; "
67 "consider using lround (#include <cmath>) instead");
68}
69
Haojian Wu7a761472016-02-08 15:54:30 +000070} // namespace misc
Haojian Wu25779e42016-02-08 10:16:13 +000071} // namespace tidy
72} // namespace clang