Skip to content

Commit 5cd3750

Browse files
Enable -Wstrict-aliasing warning (#2816)
* Enable -Wstrict-aliasing warning * Narrow down the scope of -Wstrict-aliasing * Go home, MSVC, you're drunk * Make sure "pragma GCC" is not executed on ICC Co-authored-by: Yannick Jadoul <yannick.jadoul@belgacom.net>
1 parent c4b0dc7 commit 5cd3750

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

include/pybind11/pybind11.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
# pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
3636
# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
3737
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
38-
# pragma GCC diagnostic ignored "-Wstrict-aliasing"
3938
# pragma GCC diagnostic ignored "-Wattributes"
4039
# if __GNUC__ >= 7
4140
# pragma GCC diagnostic ignored "-Wnoexcept-type"
@@ -49,10 +48,18 @@
4948
#include "detail/init.h"
5049

5150
#include <memory>
51+
#include <new>
5252
#include <vector>
5353
#include <string>
5454
#include <utility>
5555

56+
#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914))
57+
# define PYBIND11_STD_LAUNDER std::launder
58+
# define PYBIND11_HAS_STD_LAUNDER 1
59+
#else
60+
# define PYBIND11_STD_LAUNDER
61+
# define PYBIND11_HAS_STD_LAUNDER 0
62+
#endif
5663
#if defined(__GNUG__) && !defined(__clang__)
5764
# include <cxxabi.h>
5865
#endif
@@ -151,8 +158,21 @@ class cpp_function : public function {
151158
#if defined(__GNUG__) && __GNUC__ >= 6 && !defined(__clang__) && !defined(__INTEL_COMPILER)
152159
# pragma GCC diagnostic pop
153160
#endif
161+
#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER)
162+
# pragma GCC diagnostic push
163+
# pragma GCC diagnostic ignored "-Wstrict-aliasing"
164+
#endif
165+
// UB without std::launder, but without breaking ABI and/or
166+
// a significant refactoring it's "impossible" to solve.
154167
if (!std::is_trivially_destructible<Func>::value)
155-
rec->free_data = [](function_record *r) { ((capture *) &r->data)->~capture(); };
168+
rec->free_data = [](function_record *r) {
169+
auto data = PYBIND11_STD_LAUNDER((capture *) &r->data);
170+
(void) data;
171+
data->~capture();
172+
};
173+
#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER && !defined(__INTEL_COMPILER)
174+
# pragma GCC diagnostic pop
175+
#endif
156176
} else {
157177
rec->data[0] = new capture { std::forward<Func>(f) };
158178
rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); };

0 commit comments

Comments
 (0)