2018-01-30 23:12:24 +08:00
|
|
|
//===--- IncorrectRoundingsCheck.cpp - clang-tidy ------------------------------===//
|
2016-02-08 18:16:13 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-02-08 18:16:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-01-30 23:12:24 +08:00
|
|
|
#include "IncorrectRoundingsCheck.h"
|
2016-02-08 18:16:13 +08:00
|
|
|
#include "clang/AST/DeclBase.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchers.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
|
2016-05-03 02:00:29 +08:00
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
2016-02-08 18:16:13 +08:00
|
|
|
namespace clang {
|
2016-05-03 02:00:29 +08:00
|
|
|
namespace tidy {
|
2018-01-30 23:12:24 +08:00
|
|
|
namespace bugprone {
|
2016-05-03 02:00:29 +08:00
|
|
|
|
|
|
|
namespace {
|
2016-02-08 18:16:13 +08:00
|
|
|
AST_MATCHER(FloatingLiteral, floatHalf) {
|
2021-01-29 07:49:53 +08:00
|
|
|
const auto &Literal = Node.getValue();
|
2016-12-14 19:57:17 +08:00
|
|
|
if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle())
|
2021-01-29 07:49:53 +08:00
|
|
|
return Literal.convertToFloat() == 0.5f;
|
2016-12-14 19:57:17 +08:00
|
|
|
if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble())
|
2021-01-29 07:49:53 +08:00
|
|
|
return Literal.convertToDouble() == 0.5;
|
2016-02-08 18:16:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-03 02:00:29 +08:00
|
|
|
} // namespace
|
2016-02-08 18:16:13 +08:00
|
|
|
|
2018-01-30 23:12:24 +08:00
|
|
|
void IncorrectRoundingsCheck::registerMatchers(MatchFinder *MatchFinder) {
|
2016-02-08 18:16:13 +08:00
|
|
|
// Match a floating literal with value 0.5.
|
|
|
|
auto FloatHalf = floatLiteral(floatHalf());
|
|
|
|
|
|
|
|
// Match a floating point expression.
|
2016-04-22 00:57:56 +08:00
|
|
|
auto FloatType = expr(hasType(realFloatingPointType()));
|
2016-02-08 18:16:13 +08:00
|
|
|
|
|
|
|
// Match a floating literal of 0.5 or a floating literal of 0.5 implicitly.
|
|
|
|
// cast to floating type.
|
|
|
|
auto FloatOrCastHalf =
|
2016-05-31 23:26:56 +08:00
|
|
|
anyOf(FloatHalf,
|
|
|
|
implicitCastExpr(FloatType, has(ignoringParenImpCasts(FloatHalf))));
|
2016-02-08 18:16:13 +08:00
|
|
|
|
|
|
|
// Match if either the LHS or RHS is a floating literal of 0.5 or a floating
|
|
|
|
// literal of 0.5 and the other is of type double or vice versa.
|
|
|
|
auto OneSideHalf = anyOf(allOf(hasLHS(FloatOrCastHalf), hasRHS(FloatType)),
|
|
|
|
allOf(hasRHS(FloatOrCastHalf), hasLHS(FloatType)));
|
|
|
|
|
|
|
|
// Find expressions of cast to int of the sum of a floating point expression
|
|
|
|
// and 0.5.
|
|
|
|
MatchFinder->addMatcher(
|
2020-12-11 07:52:35 +08:00
|
|
|
traverse(TK_AsIs,
|
2019-11-12 23:15:56 +08:00
|
|
|
implicitCastExpr(hasImplicitDestinationType(isInteger()),
|
|
|
|
ignoringParenCasts(binaryOperator(
|
|
|
|
hasOperatorName("+"), OneSideHalf)))
|
|
|
|
.bind("CastExpr")),
|
2016-02-08 18:16:13 +08:00
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
2018-01-30 23:12:24 +08:00
|
|
|
void IncorrectRoundingsCheck::check(const MatchFinder::MatchResult &Result) {
|
2016-12-14 00:19:19 +08:00
|
|
|
const auto *CastExpr = Result.Nodes.getNodeAs<ImplicitCastExpr>("CastExpr");
|
2018-08-10 06:42:26 +08:00
|
|
|
diag(CastExpr->getBeginLoc(),
|
2016-02-08 18:16:13 +08:00
|
|
|
"casting (double + 0.5) to integer leads to incorrect rounding; "
|
|
|
|
"consider using lround (#include <cmath>) instead");
|
|
|
|
}
|
|
|
|
|
2018-01-30 23:12:24 +08:00
|
|
|
} // namespace bugprone
|
2016-02-08 18:16:13 +08:00
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|