clang-tools  3.9.0
IncorrectRoundings.cpp
Go to the documentation of this file.
1 //===--- 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 
17 using namespace clang::ast_matchers;
18 
19 namespace clang {
20 namespace tidy {
21 namespace misc {
22 
23 namespace {
24 AST_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 }
32 } // namespace
33 
34 
35 void 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.
40  auto FloatType = expr(hasType(realFloatingPointType()));
41 
42  // Match a floating literal of 0.5 or a floating literal of 0.5 implicitly.
43  // cast to floating type.
44  auto FloatOrCastHalf =
45  anyOf(FloatHalf,
46  implicitCastExpr(FloatType, has(ignoringParenImpCasts(FloatHalf))));
47 
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 
63 void 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 
70 } // namespace misc
71 } // namespace tidy
72 } // namespace clang
AST_MATCHER(Type, isStrictlyInteger)
const NamedDecl * Result
Definition: USRFinder.cpp:137