blob: 000191161fd5b2d6e70541cc62aba878cda42a2b [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
17namespace clang {
18namespace ast_matchers {
19AST_MATCHER(FloatingLiteral, floatHalf) {
20 const auto &literal = Node.getValue();
21 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle)
22 return literal.convertToFloat() == 0.5f;
23 if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble)
24 return literal.convertToDouble() == 0.5;
25 return false;
26}
27
28// TODO(hokein): Moving it to ASTMatchers.h
29AST_MATCHER(BuiltinType, isFloatingPoint) {
30 return Node.isFloatingPoint();
31}
32} // namespace ast_matchers
33} // namespace clang
34
35using namespace clang::ast_matchers;
36
37namespace clang {
38namespace tidy {
Haojian Wu7a761472016-02-08 15:54:30 +000039namespace misc {
Haojian Wu25779e42016-02-08 10:16:13 +000040void IncorrectRoundings::registerMatchers(MatchFinder *MatchFinder) {
41 // Match a floating literal with value 0.5.
42 auto FloatHalf = floatLiteral(floatHalf());
43
44 // Match a floating point expression.
45 auto FloatType = expr(hasType(builtinType(isFloatingPoint())));
46
47 // Match a floating literal of 0.5 or a floating literal of 0.5 implicitly.
48 // cast to floating type.
49 auto FloatOrCastHalf =
50 anyOf(FloatHalf, implicitCastExpr(FloatType, has(FloatHalf)));
51
52 // Match if either the LHS or RHS is a floating literal of 0.5 or a floating
53 // literal of 0.5 and the other is of type double or vice versa.
54 auto OneSideHalf = anyOf(allOf(hasLHS(FloatOrCastHalf), hasRHS(FloatType)),
55 allOf(hasRHS(FloatOrCastHalf), hasLHS(FloatType)));
56
57 // Find expressions of cast to int of the sum of a floating point expression
58 // and 0.5.
59 MatchFinder->addMatcher(
60 implicitCastExpr(
61 hasImplicitDestinationType(isInteger()),
62 ignoringParenCasts(binaryOperator(hasOperatorName("+"), OneSideHalf)))
63 .bind("CastExpr"),
64 this);
65}
66
67void IncorrectRoundings::check(const MatchFinder::MatchResult &Result) {
68 const auto *CastExpr = Result.Nodes.getStmtAs<ImplicitCastExpr>("CastExpr");
69 diag(CastExpr->getLocStart(),
70 "casting (double + 0.5) to integer leads to incorrect rounding; "
71 "consider using lround (#include <cmath>) instead");
72}
73
Haojian Wu7a761472016-02-08 15:54:30 +000074} // namespace misc
Haojian Wu25779e42016-02-08 10:16:13 +000075} // namespace tidy
76} // namespace clang