From a73b87b8bd47e0634d741e655ae37a22f6b82287 Mon Sep 17 00:00:00 2001 From: Piotr Zegar Date: Sat, 15 Apr 2023 08:34:04 +0000 Subject: [PATCH] [clang-tidy] Avoid float compare in bugprone-incorrect-roundings Using APFloat to compare floating numbers instead of float/double. Fixes: #46424 Reviewed By: carlosgalvezp Differential Revision: https://reviews.llvm.org/D147906 --- .../clang-tidy/bugprone/IncorrectRoundingsCheck.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp index beca7aef..c0f76cc 100644 --- a/clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/IncorrectRoundingsCheck.cpp @@ -17,13 +17,17 @@ using namespace clang::ast_matchers; namespace clang::tidy::bugprone { +static llvm::APFloat getHalf(const llvm::fltSemantics &Semantics) { + return llvm::APFloat(Semantics, 1U) / llvm::APFloat(Semantics, 2U); +} + namespace { AST_MATCHER(FloatingLiteral, floatHalf) { - const auto &Literal = Node.getValue(); + const llvm::APFloat Literal = Node.getValue(); if ((&Node.getSemantics()) == &llvm::APFloat::IEEEsingle()) - return Literal.convertToFloat() == 0.5f; + return Literal == getHalf(llvm::APFloat::IEEEsingle()); if ((&Node.getSemantics()) == &llvm::APFloat::IEEEdouble()) - return Literal.convertToDouble() == 0.5; + return Literal == getHalf(llvm::APFloat::IEEEdouble()); return false; } } // namespace -- 2.7.4