llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang-tidy Author: Congcong Cai (HerrCai0907) <details> <summary>Changes</summary> compare type kind is the wrong way to compare floating point type compatibility. more generic compatibility check is needed. --- Full diff: https://github.com/llvm/llvm-project/pull/122637.diff 3 Files Affected: - (modified) clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp (+3-1) - (modified) clang-tools-extra/docs/ReleaseNotes.rst (+5) - (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp (+9) ``````````diff diff --git a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp index 408390ebc70b64..bafcd402ca8510 100644 --- a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp @@ -513,7 +513,9 @@ void NarrowingConversionsCheck::handleFloatingCast(const ASTContext &Context, return; } const BuiltinType *FromType = getBuiltinType(Rhs); - if (ToType->getKind() < FromType->getKind()) + if (!llvm::APFloatBase::isRepresentableBy( + Context.getFloatTypeSemantics(FromType->desugar()), + Context.getFloatTypeSemantics(ToType->desugar()))) diagNarrowType(SourceLoc, Lhs, Rhs); } } diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 835a0269a2733c..f709902cfd45e7 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -206,6 +206,11 @@ Changes in existing checks <clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing a crash when determining if an ``enable_if[_t]`` was found. +- Improve :doc:`bugprone-narrowing-conversions + <clang-tidy/checks/bugprone/narrowing-conversions>` to avoid incorrect check + results when floating point type is not ``float``, ``double`` and + ``long double``. + - Improved :doc:`bugprone-optional-value-conversion <clang-tidy/checks/bugprone/optional-value-conversion>` to support detecting conversion directly by ``std::make_unique`` and ``std::make_shared``. diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp index 9ded2f0923f4e6..180b789e45bb37 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/narrowing-conversions-narrowingfloatingpoint-option.cpp @@ -36,6 +36,15 @@ void narrow_double_to_float_not_ok(double d) { f = narrow_double_to_float_return(); } +float narrow_float16_to_float_return(_Float16 f) { + return f; +} + +_Float16 narrow_float_to_float16_return(float f) { + return f; + // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: narrowing conversion from 'float' to '_Float16' [bugprone-narrowing-conversions] +} + void narrow_fp_constants() { float f; f = 0.5; // [dcl.init.list] 7.2 : in-range fp constant to narrower float is not a narrowing. `````````` </details> https://github.com/llvm/llvm-project/pull/122637 _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits