From: Piotr Zegar Date: Sat, 15 Apr 2023 08:34:04 +0000 (+0000) Subject: [clang-tidy] Avoid float compare in bugprone-incorrect-roundings X-Git-Tag: upstream/17.0.6~11514 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a73b87b8bd47e0634d741e655ae37a22f6b82287;p=platform%2Fupstream%2Fllvm.git [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 --- 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