From 4b4c43c86d30b237312edde61759a294e1834dcc Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Sat, 23 Jan 2021 19:21:35 +0100 Subject: [PATCH 1/4] Enable -Wstrict-aliasing warning --- include/pybind11/pybind11.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 08c51c6f92..2b2d36f775 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -10,6 +10,14 @@ #pragma once +#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914)) +# define PYBIND11_STD_LAUNDER std::launder +# define PYBIND11_HAS_STD_LAUNDER 1 +#else +# define PYBIND11_STD_LAUNDER +# define PYBIND11_HAS_STD_LAUNDER 0 +#endif + #if defined(__INTEL_COMPILER) # pragma warning push # pragma warning disable 68 // integer conversion resulted in a change of sign @@ -35,7 +43,9 @@ # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wunused-but-set-variable" # pragma GCC diagnostic ignored "-Wmissing-field-initializers" -# pragma GCC diagnostic ignored "-Wstrict-aliasing" +# if !PYBIND11_HAS_STD_LAUNDER +# pragma GCC diagnostic ignored "-Wstrict-aliasing" +# endif # pragma GCC diagnostic ignored "-Wattributes" # if __GNUC__ >= 7 # pragma GCC diagnostic ignored "-Wnoexcept-type" @@ -48,6 +58,7 @@ #include "detail/init.h" #include +#include #include #include #include @@ -151,7 +162,7 @@ class cpp_function : public function { # pragma GCC diagnostic pop #endif if (!std::is_trivially_destructible::value) - rec->free_data = [](function_record *r) { ((capture *) &r->data)->~capture(); }; + rec->free_data = [](function_record *r) { PYBIND11_STD_LAUNDER((capture *) &r->data)->~capture(); }; } else { rec->data[0] = new capture { std::forward(f) }; rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); }; From d4ca9d46a7222b9fcf8dac171c29d5f67a9086bc Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Sat, 23 Jan 2021 21:08:16 +0100 Subject: [PATCH 2/4] Narrow down the scope of -Wstrict-aliasing --- include/pybind11/pybind11.h | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 2b2d36f775..325ec6cc22 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -10,14 +10,6 @@ #pragma once -#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914)) -# define PYBIND11_STD_LAUNDER std::launder -# define PYBIND11_HAS_STD_LAUNDER 1 -#else -# define PYBIND11_STD_LAUNDER -# define PYBIND11_HAS_STD_LAUNDER 0 -#endif - #if defined(__INTEL_COMPILER) # pragma warning push # pragma warning disable 68 // integer conversion resulted in a change of sign @@ -43,9 +35,6 @@ # pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wunused-but-set-variable" # pragma GCC diagnostic ignored "-Wmissing-field-initializers" -# if !PYBIND11_HAS_STD_LAUNDER -# pragma GCC diagnostic ignored "-Wstrict-aliasing" -# endif # pragma GCC diagnostic ignored "-Wattributes" # if __GNUC__ >= 7 # pragma GCC diagnostic ignored "-Wnoexcept-type" @@ -63,6 +52,13 @@ #include #include +#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914)) +# define PYBIND11_STD_LAUNDER std::launder +# define PYBIND11_HAS_STD_LAUNDER 1 +#else +# define PYBIND11_STD_LAUNDER +# define PYBIND11_HAS_STD_LAUNDER 0 +#endif #if defined(__GNUG__) && !defined(__clang__) # include #endif @@ -161,8 +157,20 @@ class cpp_function : public function { #if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6 # pragma GCC diagnostic pop #endif +#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wstrict-aliasing" +#endif + // UB without std::launder, but without breaking ABI and/or + // a significant refactoring it's "impossible" to solve' if (!std::is_trivially_destructible::value) - rec->free_data = [](function_record *r) { PYBIND11_STD_LAUNDER((capture *) &r->data)->~capture(); }; + rec->free_data = [](function_record *r) { + auto cap = PYBIND11_STD_LAUNDER((capture *) &r->data); + cap->~capture(); + }; +#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER +# pragma GCC diagnostic pop +#endif } else { rec->data[0] = new capture { std::forward(f) }; rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); }; From 1578e69ee7a15e0d59364ec817e00532aeb1f72d Mon Sep 17 00:00:00 2001 From: Yannick Jadoul Date: Sun, 24 Jan 2021 01:29:50 +0100 Subject: [PATCH 3/4] Go home, MSVC, you're drunk --- include/pybind11/pybind11.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 325ec6cc22..b50e66506b 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -162,11 +162,12 @@ class cpp_function : public function { # pragma GCC diagnostic ignored "-Wstrict-aliasing" #endif // UB without std::launder, but without breaking ABI and/or - // a significant refactoring it's "impossible" to solve' + // a significant refactoring it's "impossible" to solve. if (!std::is_trivially_destructible::value) rec->free_data = [](function_record *r) { - auto cap = PYBIND11_STD_LAUNDER((capture *) &r->data); - cap->~capture(); + auto data = PYBIND11_STD_LAUNDER((capture *) &r->data); + (void) data; + data->~capture(); }; #if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER # pragma GCC diagnostic pop From 7b2e7be9be554e1ab546664e4ed1fe1659502439 Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Wed, 14 Jul 2021 16:19:15 +0200 Subject: [PATCH 4/4] Make sure "pragma GCC" is not executed on ICC --- include/pybind11/pybind11.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index b50e66506b..59ee31c527 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -157,7 +157,7 @@ class cpp_function : public function { #if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6 # pragma GCC diagnostic pop #endif -#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER +#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wstrict-aliasing" #endif @@ -169,7 +169,7 @@ class cpp_function : public function { (void) data; data->~capture(); }; -#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER +#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER) # pragma GCC diagnostic pop #endif } else {