Alexander Kornienko | 4256fd0 | 2018-01-30 15:12:24 +0000 | [diff] [blame] | 1 | //===--- IncorrectRoundingsCheck.cpp - clang-tidy ------------------------------===// |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 4256fd0 | 2018-01-30 15:12:24 +0000 | [diff] [blame] | 10 | #include "IncorrectRoundingsCheck.h" |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 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 Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 17 | using namespace clang::ast_matchers; |
| 18 | |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 19 | namespace clang { |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 20 | namespace tidy { |
Alexander Kornienko | 4256fd0 | 2018-01-30 15:12:24 +0000 | [diff] [blame] | 21 | namespace bugprone { |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 22 | |
| 23 | namespace { |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 24 | AST_MATCHER(FloatingLiteral, floatHalf) { |
| 25 | const auto &literal = Node.getValue(); |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 26 | if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle()) |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 27 | return literal.convertToFloat() == 0.5f; |
Stephan Bergmann | 17c7f70 | 2016-12-14 11:57:17 +0000 | [diff] [blame] | 28 | if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble()) |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 29 | return literal.convertToDouble() == 0.5; |
| 30 | return false; |
| 31 | } |
Etienne Bergeron | 456177b | 2016-05-02 18:00:29 +0000 | [diff] [blame] | 32 | } // namespace |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 33 | |
Alexander Kornienko | 4256fd0 | 2018-01-30 15:12:24 +0000 | [diff] [blame] | 34 | void IncorrectRoundingsCheck::registerMatchers(MatchFinder *MatchFinder) { |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 35 | // Match a floating literal with value 0.5. |
| 36 | auto FloatHalf = floatLiteral(floatHalf()); |
| 37 | |
| 38 | // Match a floating point expression. |
Etienne Bergeron | 9d26599 | 2016-04-21 16:57:56 +0000 | [diff] [blame] | 39 | auto FloatType = expr(hasType(realFloatingPointType())); |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 40 | |
| 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 Padlewski | e93a73f | 2016-05-31 15:26:56 +0000 | [diff] [blame] | 44 | anyOf(FloatHalf, |
| 45 | implicitCastExpr(FloatType, has(ignoringParenImpCasts(FloatHalf)))); |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 46 | |
| 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 Kornienko | 4256fd0 | 2018-01-30 15:12:24 +0000 | [diff] [blame] | 62 | void IncorrectRoundingsCheck::check(const MatchFinder::MatchResult &Result) { |
Alexander Kornienko | 9f58fe0 | 2016-12-13 16:19:19 +0000 | [diff] [blame] | 63 | const auto *CastExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>("CastExpr"); |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 64 | diag(CastExpr->getLocStart(), |
| 65 | "casting (double + 0.5) to integer leads to incorrect rounding; " |
| 66 | "consider using lround (#include <cmath>) instead"); |
| 67 | } |
| 68 | |
Alexander Kornienko | 4256fd0 | 2018-01-30 15:12:24 +0000 | [diff] [blame] | 69 | } // namespace bugprone |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 70 | } // namespace tidy |
| 71 | } // namespace clang |