Skip to content

Commit 998d45e

Browse files
authored
Cleanup of file-scoped and globally-scoped warning suppression pragmas across pybind11 header files. (#3201)
* Removing all MSVC C4127 warning suppression pragmas. * Removing MSVC /WX (WERROR). To get a full list of all warnings. * Inserting PYBIND11_SILENCE_MSVC_C4127. Changing one runtime if to #if. * Changing PYBIND11_SILENCE_MSVC_C4127 macro to use absolute namespace (for use outside pybind11 include directory). * Restoring MSVC /WX (WERROR). * Removing globally-scoped suppression for clang -Wunsequenced. Based on an experiment under PR #3202 it is obsolete and can simply be removed.
1 parent 774b5ff commit 998d45e

File tree

5 files changed

+10
-42
lines changed

5 files changed

+10
-42
lines changed

include/pybind11/detail/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ inline void silence_unused_warnings(Args &&...) {}
968968
// warning C4127: Conditional expression is constant
969969
constexpr inline bool silence_msvc_c4127(bool cond) { return cond; }
970970

971-
# define PYBIND11_SILENCE_MSVC_C4127(...) detail::silence_msvc_c4127(__VA_ARGS__)
971+
# define PYBIND11_SILENCE_MSVC_C4127(...) ::pybind11::detail::silence_msvc_c4127(__VA_ARGS__)
972972

973973
#else
974974
# define PYBIND11_SILENCE_MSVC_C4127(...) __VA_ARGS__

include/pybind11/numpy.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
#include <vector>
2626
#include <typeindex>
2727

28-
#if defined(_MSC_VER)
29-
# pragma warning(push)
30-
# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
31-
#endif
32-
3328
/* This will be true on all flat address space platforms and allows us to reduce the
3429
whole npy_intp / ssize_t / Py_intptr_t business down to just ssize_t for all size
3530
and dimension types (e.g. shape, strides, indexing), instead of inflicting this
@@ -747,7 +742,7 @@ class array : public buffer {
747742
* and the caller must take care not to access invalid dimensions or dimension indices.
748743
*/
749744
template <typename T, ssize_t Dims = -1> detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() & {
750-
if (Dims >= 0 && ndim() != Dims)
745+
if (PYBIND11_SILENCE_MSVC_C4127(Dims >= 0) && ndim() != Dims)
751746
throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) +
752747
"; expected " + std::to_string(Dims));
753748
return detail::unchecked_mutable_reference<T, Dims>(mutable_data(), shape(), strides(), ndim());
@@ -761,7 +756,7 @@ class array : public buffer {
761756
* invalid dimensions or dimension indices.
762757
*/
763758
template <typename T, ssize_t Dims = -1> detail::unchecked_reference<T, Dims> unchecked() const & {
764-
if (Dims >= 0 && ndim() != Dims)
759+
if (PYBIND11_SILENCE_MSVC_C4127(Dims >= 0) && ndim() != Dims)
765760
throw std::domain_error("array has incorrect number of dimensions: " + std::to_string(ndim()) +
766761
"; expected " + std::to_string(Dims));
767762
return detail::unchecked_reference<T, Dims>(data(), shape(), strides(), ndim());
@@ -1708,7 +1703,3 @@ Helper vectorize(Return (Class::*f)(Args...) const) {
17081703
}
17091704

17101705
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
1711-
1712-
#if defined(_MSC_VER)
1713-
#pragma warning(pop)
1714-
#endif

include/pybind11/operators.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111

1212
#include "pybind11.h"
1313

14-
#if defined(__clang__) && !defined(__INTEL_COMPILER)
15-
# pragma clang diagnostic ignored "-Wunsequenced" // multiple unsequenced modifications to 'self' (when using def(py::self OP Type()))
16-
#elif defined(_MSC_VER)
17-
# pragma warning(push)
18-
# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
19-
#endif
20-
2114
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
2215
PYBIND11_NAMESPACE_BEGIN(detail)
2316

@@ -58,7 +51,8 @@ template <op_id id, op_type ot, typename L, typename R> struct op_ {
5851
using op = op_impl<id, ot, Base, L_type, R_type>;
5952
cl.def(op::name(), &op::execute, is_operator(), extra...);
6053
#if PY_MAJOR_VERSION < 3
61-
if (id == op_truediv || id == op_itruediv)
54+
if (PYBIND11_SILENCE_MSVC_C4127(id == op_truediv) ||
55+
PYBIND11_SILENCE_MSVC_C4127(id == op_itruediv))
6256
cl.def(id == op_itruediv ? "__idiv__" : ot == op_l ? "__div__" : "__rdiv__",
6357
&op::execute, is_operator(), extra...);
6458
#endif
@@ -167,7 +161,3 @@ using detail::self;
167161
using detail::hash;
168162

169163
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
170-
171-
#if defined(_MSC_VER)
172-
# pragma warning(pop)
173-
#endif

include/pybind11/stl.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@
1919
#include <deque>
2020
#include <valarray>
2121

22-
#if defined(_MSC_VER)
23-
#pragma warning(push)
24-
#pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
25-
#endif
26-
2722
#ifdef __has_include
2823
// std::optional (but including it in c++14 mode isn't allowed)
2924
# if defined(PYBIND11_CPP17) && __has_include(<optional>)
@@ -390,7 +385,3 @@ inline std::ostream &operator<<(std::ostream &os, const handle &obj) {
390385
}
391386

392387
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
393-
394-
#if defined(_MSC_VER)
395-
#pragma warning(pop)
396-
#endif

tests/test_builtin_casters.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
#include "pybind11_tests.h"
1111
#include <pybind11/complex.h>
1212

13-
#if defined(_MSC_VER)
14-
# pragma warning(push)
15-
# pragma warning(disable: 4127) // warning C4127: Conditional expression is constant
16-
#endif
17-
1813
struct ConstRefCasted {
1914
int tag;
2015
};
@@ -73,7 +68,7 @@ TEST_SUBMODULE(builtin_casters, m) {
7368
std::wstring wstr;
7469
wstr.push_back(0x61); // a
7570
wstr.push_back(0x2e18); //
76-
if (sizeof(wchar_t) == 2) { wstr.push_back(mathbfA16_1); wstr.push_back(mathbfA16_2); } // 𝐀, utf16
71+
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(wchar_t) == 2)) { wstr.push_back(mathbfA16_1); wstr.push_back(mathbfA16_2); } // 𝐀, utf16
7772
else { wstr.push_back((wchar_t) mathbfA32); } // 𝐀, utf32
7873
wstr.push_back(0x7a); // z
7974

@@ -83,11 +78,12 @@ TEST_SUBMODULE(builtin_casters, m) {
8378
m.def("good_wchar_string", [=]() { return wstr; }); // a‽𝐀z
8479
m.def("bad_utf8_string", []() { return std::string("abc\xd0" "def"); });
8580
m.def("bad_utf16_string", [=]() { return std::u16string({ b16, char16_t(0xd800), z16 }); });
81+
#if PY_MAJOR_VERSION >= 3
8682
// Under Python 2.7, invalid unicode UTF-32 characters don't appear to trigger UnicodeDecodeError
87-
if (PY_MAJOR_VERSION >= 3)
88-
m.def("bad_utf32_string", [=]() { return std::u32string({ a32, char32_t(0xd800), z32 }); });
89-
if (PY_MAJOR_VERSION >= 3 || sizeof(wchar_t) == 2)
83+
m.def("bad_utf32_string", [=]() { return std::u32string({ a32, char32_t(0xd800), z32 }); });
84+
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(wchar_t) == 2))
9085
m.def("bad_wchar_string", [=]() { return std::wstring({ wchar_t(0x61), wchar_t(0xd800) }); });
86+
#endif
9187
m.def("u8_Z", []() -> char { return 'Z'; });
9288
m.def("u8_eacute", []() -> char { return '\xe9'; });
9389
m.def("u16_ibang", [=]() -> char16_t { return ib16; });

0 commit comments

Comments
 (0)