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"
17 using namespace clang::ast_matchers;
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;
34 void IncorrectRoundings::registerMatchers(MatchFinder *MatchFinder) {
36 auto FloatHalf = floatLiteral(floatHalf());
39 auto FloatType = expr(hasType(realFloatingPointType()));
43 auto FloatOrCastHalf =
45 implicitCastExpr(FloatType, has(ignoringParenImpCasts(FloatHalf))));
49 auto OneSideHalf = anyOf(allOf(hasLHS(FloatOrCastHalf), hasRHS(FloatType)),
50 allOf(hasRHS(FloatOrCastHalf), hasLHS(FloatType)));
54 MatchFinder->addMatcher(
56 hasImplicitDestinationType(isInteger()),
57 ignoringParenCasts(binaryOperator(hasOperatorName(
"+"), OneSideHalf)))
62 void IncorrectRoundings::check(
const MatchFinder::MatchResult &Result) {
63 const auto *CastExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>(
"CastExpr");
64 diag(CastExpr->getLocStart(),
65 "casting (double + 0.5) to integer leads to incorrect rounding; "
66 "consider using lround (#include <cmath>) instead");
AST_MATCHER(VarDecl, isAsm)