Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 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 | namespace clang { |
| 18 | namespace ast_matchers { |
| 19 | AST_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 | } |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 27 | } // namespace ast_matchers |
| 28 | } // namespace clang |
| 29 | |
| 30 | using namespace clang::ast_matchers; |
| 31 | |
| 32 | namespace clang { |
| 33 | namespace tidy { |
Haojian Wu | 7a76147 | 2016-02-08 15:54:30 +0000 | [diff] [blame] | 34 | namespace misc { |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 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. |
Etienne Bergeron | 9d26599 | 2016-04-21 16:57:56 +0000 | [diff] [blame^] | 40 | auto FloatType = expr(hasType(realFloatingPointType())); |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 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, implicitCastExpr(FloatType, has(FloatHalf))); |
| 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 | |
| 62 | void IncorrectRoundings::check(const MatchFinder::MatchResult &Result) { |
| 63 | const auto *CastExpr = Result.Nodes.getStmtAs<ImplicitCastExpr>("CastExpr"); |
| 64 | diag(CastExpr->getLocStart(), |
| 65 | "casting (double + 0.5) to integer leads to incorrect rounding; " |
| 66 | "consider using lround (#include <cmath>) instead"); |
| 67 | } |
| 68 | |
Haojian Wu | 7a76147 | 2016-02-08 15:54:30 +0000 | [diff] [blame] | 69 | } // namespace misc |
Haojian Wu | 25779e4 | 2016-02-08 10:16:13 +0000 | [diff] [blame] | 70 | } // namespace tidy |
| 71 | } // namespace clang |