[clang] d049db8 - [clang] Fix false warning on reinterpret_casting unknown template type (#109430)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 20 01:03:02 PST 2025
Author: Arseniy Zaostrovnykh
Date: 2025-01-20T10:02:57+01:00
New Revision: d049db83627d164e4353f59a5f0b4f87dd74b138
URL: https://github.com/llvm/llvm-project/commit/d049db83627d164e4353f59a5f0b4f87dd74b138
DIFF: https://github.com/llvm/llvm-project/commit/d049db83627d164e4353f59a5f0b4f87dd74b138.diff
LOG: [clang] Fix false warning on reinterpret_casting unknown template type (#109430)
After 1595988ee6f9732e7ea79928af8a470ad5ef7dbe
diag::warn_undefined_reinterpret_cast started raising on
non-instantiated template functions without sufficient knowledge whether
the reinterpret_cast is indeed UB.
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaCast.cpp
clang/test/SemaCXX/reinterpret-cast.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2f7b9227386961..f6d5c346021d60 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -660,6 +660,8 @@ Improvements to Clang's diagnostics
- Don't emit bogus dangling diagnostics when ``[[gsl::Owner]]`` and `[[clang::lifetimebound]]` are used together (#GH108272).
+- Don't emit bogus dignostic about an undefined behavior on ``reinterpret_cast<T>`` for non-instantiated template functions without sufficient knowledge whether it can actually lead to undefined behavior for ``T`` (#GH109430).
+
- The ``-Wreturn-stack-address`` warning now also warns about addresses of
local variables passed to function calls using the ``[[clang::musttail]]``
attribute.
diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index f98857f852b5af..54bc52fa2ac405 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -2094,6 +2094,10 @@ void Sema::CheckCompatibleReinterpretCast(QualType SrcType, QualType DestType,
}
}
+ if (SrcTy->isDependentType() || DestTy->isDependentType()) {
+ return;
+ }
+
Diag(Range.getBegin(), DiagID) << SrcType << DestType << Range;
}
diff --git a/clang/test/SemaCXX/reinterpret-cast.cpp b/clang/test/SemaCXX/reinterpret-cast.cpp
index 45332fd15b5d4e..bfb808773b9004 100644
--- a/clang/test/SemaCXX/reinterpret-cast.cpp
+++ b/clang/test/SemaCXX/reinterpret-cast.cpp
@@ -302,3 +302,77 @@ void reinterpret_cast_allowlist () {
(void)reinterpret_cast<unsigned char&>(b);
(void)*reinterpret_cast<unsigned char*>(&b);
}
+
+namespace templated {
+template <typename TARGETTYPE, typename UATYPE>
+void cast_uninstantiated() {
+ const UATYPE* data;
+ (void)*reinterpret_cast<const TARGETTYPE*>(data); // no warning
+}
+
+
+template <typename TARGETTYPE, typename UATYPE>
+void cast_instantiated_badly() {
+ const UATYPE* data;
+ (void)*reinterpret_cast<const TARGETTYPE*>(data); // expected-warning {{dereference of type 'const int *' that was reinterpret_cast from type 'const float *' has undefined behavior}}
+}
+
+template <typename TARGETTYPE, typename UATYPE>
+void cast_instantiated_well() {
+ const UATYPE* data;
+ (void)*reinterpret_cast<const TARGETTYPE*>(data); // no warning
+}
+
+template <typename TARGETTYPE>
+void cast_one_tmpl_arg_uninstantiated() {
+ const int* data;
+ (void)*reinterpret_cast<const TARGETTYPE*>(data); // no warning
+}
+
+template <typename TARGETTYPE>
+void cast_one_tmpl_arg_instantiated_badly() {
+ const float* data;
+ (void)*reinterpret_cast<const TARGETTYPE*>(data); // expected-warning {{dereference of type 'const int *' that was reinterpret_cast from type 'const float *' has undefined behavior}}
+}
+
+template <typename TARGETTYPE>
+void cast_one_tmpl_arg_instantiated_well() {
+ const float* data;
+ (void)*reinterpret_cast<const TARGETTYPE*>(data); // no warning
+}
+
+template <int size>
+void cast_nontype_template_true_positive_noninstantiated() {
+ const float *data;
+ const int arr[size];
+ (void)*reinterpret_cast<const int*>(data); // expected-warning {{dereference of type 'const int *' that was reinterpret_cast from type 'const float *' has undefined behavior}}
+}
+
+template <int size>
+void cast_nontype_template_true_negative_noninstantiated() {
+ const int data[size];
+ (void)*reinterpret_cast<const int*>(data); // no warning
+}
+
+void top() {
+ cast_instantiated_badly<int, float>();
+ // expected-note at -1 {{in instantiation of function template specialization 'templated::cast_instantiated_badly<int, float>' requested here}}
+ cast_instantiated_well<int, int>();
+ cast_one_tmpl_arg_instantiated_badly<int>();
+ // expected-note at -1 {{in instantiation of function template specialization 'templated::cast_one_tmpl_arg_instantiated_badly<int>' requested here}}
+ cast_one_tmpl_arg_instantiated_well<float>();
+}
+
+template<typename T, typename U>
+void cast_template_dependent_type_noninstantiated(T** x)
+{
+ (void)*reinterpret_cast<U**>(x);
+}
+
+template<typename T, typename U>
+void cast_template_dependent_member_type_noninstantiated(typename T::X x)
+{
+ (void)*reinterpret_cast<typename U::Y>(x);
+}
+
+} // namespace templated
More information about the cfe-commits
mailing list