Skip to content

Commit 6154bf5

Browse files
committed
Enable -Wstrict-aliasing warning
1 parent 4853408 commit 6154bf5

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

include/pybind11/detail/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@
162162
#include <memory>
163163
#include <typeindex>
164164
#include <type_traits>
165+
#if defined(__has_include) && __has_include(<version>)
166+
# include <version>
167+
#endif
165168

166169
#if PY_MAJOR_VERSION >= 3 /// Compatibility macros for various Python versions
167170
#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyInstanceMethod_New(ptr)

include/pybind11/pybind11.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
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+
1321
#if defined(__INTEL_COMPILER)
1422
# pragma warning push
1523
# pragma warning disable 68 // integer conversion resulted in a change of sign
@@ -35,7 +43,9 @@
3543
# pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
3644
# pragma GCC diagnostic ignored "-Wunused-but-set-variable"
3745
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
38-
# pragma GCC diagnostic ignored "-Wstrict-aliasing"
46+
# if !PYBIND11_HAS_STD_LAUNDER
47+
# pragma GCC diagnostic ignored "-Wstrict-aliasing"
48+
# endif
3949
# pragma GCC diagnostic ignored "-Wattributes"
4050
# if __GNUC__ >= 7
4151
# pragma GCC diagnostic ignored "-Wnoexcept-type"
@@ -48,6 +58,7 @@
4858
#include "detail/init.h"
4959

5060
#include <memory>
61+
#include <new>
5162
#include <vector>
5263
#include <string>
5364
#include <utility>
@@ -151,7 +162,7 @@ class cpp_function : public function {
151162
# pragma GCC diagnostic pop
152163
#endif
153164
if (!std::is_trivially_destructible<Func>::value)
154-
rec->free_data = [](function_record *r) { ((capture *) &r->data)->~capture(); };
165+
rec->free_data = [](function_record *r) { PYBIND11_STD_LAUNDER((capture *) &r->data)->~capture(); };
155166
} else {
156167
rec->data[0] = new capture { std::forward<Func>(f) };
157168
rec->free_data = [](function_record *r) { delete ((capture *) r->data[0]); };

0 commit comments

Comments
 (0)