Skip to content

Commit e7225e6

Browse files
committed
Narrow down the scope of -Wstrict-aliasing
1 parent d68f534 commit e7225e6

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

include/pybind11/pybind11.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010

1111
#pragma once
1212

13-
#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914))
14-
# define PYBIND11_STD_LAUNDER std::launder
15-
# define PYBIND11_HAS_STD_LAUNDER 1
16-
#else
17-
# define PYBIND11_STD_LAUNDER
18-
# define PYBIND11_HAS_STD_LAUNDER 0
19-
#endif
20-
2113
#if defined(__INTEL_COMPILER)
2214
# pragma warning push
2315
# pragma warning disable 68 // integer conversion resulted in a change of sign
@@ -43,9 +35,6 @@
4335
# pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
4436
# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
4537
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
46-
# if !PYBIND11_HAS_STD_LAUNDER
47-
# pragma GCC diagnostic ignored "-Wstrict-aliasing"
48-
# endif
4938
# pragma GCC diagnostic ignored "-Wattributes"
5039
# if __GNUC__ >= 7
5140
# pragma GCC diagnostic ignored "-Wnoexcept-type"
@@ -63,6 +52,13 @@
6352
#include <string>
6453
#include <utility>
6554

55+
#if defined(__cpp_lib_launder) && !(defined(_MSC_VER) && (_MSC_VER < 1914))
56+
# define PYBIND11_STD_LAUNDER std::launder
57+
# define PYBIND11_HAS_STD_LAUNDER 1
58+
#else
59+
# define PYBIND11_STD_LAUNDER
60+
# define PYBIND11_HAS_STD_LAUNDER 0
61+
#endif
6662
#if defined(__GNUG__) && !defined(__clang__)
6763
# include <cxxabi.h>
6864
#endif
@@ -161,8 +157,20 @@ class cpp_function : public function {
161157
#if defined(__GNUG__) && !defined(__clang__) && __GNUC__ >= 6
162158
# pragma GCC diagnostic pop
163159
#endif
160+
#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER
161+
# pragma GCC diagnostic push
162+
# pragma GCC diagnostic ignored "-Wstrict-aliasing"
163+
#endif
164+
// UB without std::launder, but without breaking ABI and/or
165+
// a significant refactoring it's "impossible" to solve'
164166
if (!std::is_trivially_destructible<Func>::value)
165-
rec->free_data = [](function_record *r) { PYBIND11_STD_LAUNDER((capture *) &r->data)->~capture(); };
167+
rec->free_data = [](function_record *r) {
168+
auto data = PYBIND11_STD_LAUNDER((capture *) &r->data);
169+
data->~capture();
170+
};
171+
#if defined(__GNUG__) && !PYBIND11_HAS_STD_LAUNDER
172+
# pragma GCC diagnostic pop
173+
#endif
166174
} else {
167175
rec->data[0] = new capture { std::forward<Func>(f) };
168176
rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); };

0 commit comments

Comments
 (0)