-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[clang-tidy] fix wrong float to float conversion check when floating point type is not standard type #122637
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
@llvm/pr-subscribers-clang-tools-extra Author: Congcong Cai (HerrCai0907) Changescompare type kind is the wrong way to compare floating point type compatibility. Full diff: https://github.com/llvm/llvm-project/pull/122637.diff 3 Files Affected:
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.
|
@llvm/pr-subscribers-clang-tidy Author: Congcong Cai (HerrCai0907) Changescompare type kind is the wrong way to compare floating point type compatibility. Full diff: https://github.com/llvm/llvm-project/pull/122637.diff 3 Files Affected:
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.
|
4afbab3
to
43cbcd2
Compare
31b602b
to
d138b3d
Compare
43cbcd2
to
ded43fc
Compare
d138b3d
to
ecfe551
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ded43fc
to
1d0bb33
Compare
…point type is not standard type compare type kind is the wrong way to compare floating point type compatibility. more generic compatibility check is needed.
ecfe551
to
a7d6883
Compare
compare type kind is the wrong way to compare floating point type compatibility.
more generic compatibility check is needed.