Skip to content

Commit e94b70f

Browse files
committed
Remove all uses of PYBIND11_SILENCE_MSVC_C4127 and the macro definition, too.
1 parent 56cddea commit e94b70f

File tree

10 files changed

+43
-42
lines changed

10 files changed

+43
-42
lines changed

include/pybind11/cast.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
#include <vector>
3030

3131
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
32-
PYBIND11_NAMESPACE_BEGIN(detail)
3332

3433
PYBIND11_DISABLE_WARNING_MSVC(4127)
3534

35+
PYBIND11_NAMESPACE_BEGIN(detail)
36+
3637
template <typename type, typename SFINAE = void>
3738
class type_caster : public type_caster_base<type> {};
3839
template <typename type>
@@ -418,7 +419,7 @@ struct string_caster {
418419
= reinterpret_cast<const CharT *>(PYBIND11_BYTES_AS_STRING(utfNbytes.ptr()));
419420
size_t length = (size_t) PYBIND11_BYTES_SIZE(utfNbytes.ptr()) / sizeof(CharT);
420421
// Skip BOM for UTF-16/32
421-
if (PYBIND11_SILENCE_MSVC_C4127(UTF_N > 8)) {
422+
if (UTF_N > 8) {
422423
buffer++;
423424
length--;
424425
}
@@ -574,7 +575,7 @@ struct type_caster<CharT, enable_if_t<is_std_char_type<CharT>::value>> {
574575
// figure out how long the first encoded character is in bytes to distinguish between these
575576
// two errors. We also allow want to allow unicode characters U+0080 through U+00FF, as
576577
// those can fit into a single char value.
577-
if (PYBIND11_SILENCE_MSVC_C4127(StringCaster::UTF_N == 8) && str_len > 1 && str_len <= 4) {
578+
if (StringCaster::UTF_N == 8 && str_len > 1 && str_len <= 4) {
578579
auto v0 = static_cast<unsigned char>(value[0]);
579580
// low bits only: 0-127
580581
// 0b110xxxxx - start of 2-byte sequence
@@ -600,7 +601,7 @@ struct type_caster<CharT, enable_if_t<is_std_char_type<CharT>::value>> {
600601
// UTF-16 is much easier: we can only have a surrogate pair for values above U+FFFF, thus a
601602
// surrogate pair with total length 2 instantly indicates a range error (but not a "your
602603
// string was too long" error).
603-
else if (PYBIND11_SILENCE_MSVC_C4127(StringCaster::UTF_N == 16) && str_len == 2) {
604+
else if (StringCaster::UTF_N == 16 && str_len == 2) {
604605
one_char = static_cast<CharT>(value[0]);
605606
if (one_char >= 0xD800 && one_char < 0xE000) {
606607
throw value_error("Character code point not in range(0x10000)");

include/pybind11/detail/common.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,17 +1187,6 @@ constexpr
11871187
# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...)
11881188
#endif
11891189

1190-
#if defined(_MSC_VER) // All versions (as of July 2021).
1191-
1192-
// warning C4127: Conditional expression is constant
1193-
constexpr inline bool silence_msvc_c4127(bool cond) { return cond; }
1194-
1195-
# define PYBIND11_SILENCE_MSVC_C4127(...) ::pybind11::detail::silence_msvc_c4127(__VA_ARGS__)
1196-
1197-
#else
1198-
# define PYBIND11_SILENCE_MSVC_C4127(...) __VA_ARGS__
1199-
#endif
1200-
12011190
#if defined(__clang__) \
12021191
&& (defined(__apple_build_version__) /* AppleClang 13.0.0.13000029 was the only data point \
12031192
available. */ \

include/pybind11/detail/init.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#include "class.h"
1313

1414
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
15+
16+
PYBIND11_DISABLE_WARNING_MSVC(4127)
17+
1518
PYBIND11_NAMESPACE_BEGIN(detail)
1619

1720
template <>
@@ -115,7 +118,7 @@ template <typename Class>
115118
void construct(value_and_holder &v_h, Cpp<Class> *ptr, bool need_alias) {
116119
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
117120
no_nullptr(ptr);
118-
if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias<Class>(ptr)) {
121+
if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
119122
// We're going to try to construct an alias by moving the cpp type. Whether or not
120123
// that succeeds, we still need to destroy the original cpp pointer (either the
121124
// moved away leftover, if the alias construction works, or the value itself if we
@@ -156,7 +159,7 @@ void construct(value_and_holder &v_h, Holder<Class> holder, bool need_alias) {
156159
auto *ptr = holder_helper<Holder<Class>>::get(holder);
157160
no_nullptr(ptr);
158161
// If we need an alias, check that the held pointer is actually an alias instance
159-
if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias && !is_alias<Class>(ptr)) {
162+
if (Class::has_alias && need_alias && !is_alias<Class>(ptr)) {
160163
throw type_error("pybind11::init(): construction failed: returned holder-wrapped instance "
161164
"is not an alias instance");
162165
}
@@ -174,7 +177,7 @@ void construct(value_and_holder &v_h, Cpp<Class> &&result, bool need_alias) {
174177
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
175178
static_assert(std::is_move_constructible<Cpp<Class>>::value,
176179
"pybind11::init() return-by-value factory function requires a movable class");
177-
if (PYBIND11_SILENCE_MSVC_C4127(Class::has_alias) && need_alias) {
180+
if (Class::has_alias && need_alias) {
178181
construct_alias_from_cpp<Class>(is_alias_constructible<Class>{}, v_h, std::move(result));
179182
} else {
180183
v_h.value_ptr() = new Cpp<Class>(std::move(result));

include/pybind11/eigen/matrix.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,8 @@
1616
https://stackoverflow.com/questions/2579576/i-dir-vs-isystem-dir
1717
https://stackoverflow.com/questions/1741816/isystem-for-ms-visual-studio-c-compiler
1818
*/
19-
// The C4127 suppression was introduced for Eigen 3.4.0. In theory we could
20-
// make it version specific, or even remove it later, but considering that
21-
// 1. C4127 is generally far more distracting than useful for modern template code, and
22-
// 2. we definitely want to ignore any MSVC warnings originating from Eigen code,
23-
// it is probably best to keep this around indefinitely.
2419
#if defined(_MSC_VER)
2520
# pragma warning(push)
26-
# pragma warning(disable : 4127) // C4127: conditional expression is constant
2721
# pragma warning(disable : 5054) // https://github.com/pybind/pybind11/pull/3741
2822
// C5054: operator '&': deprecated between enumerations of different types
2923
#elif defined(__MINGW32__)
@@ -48,6 +42,8 @@ static_assert(EIGEN_VERSION_AT_LEAST(3, 2, 7),
4842

4943
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
5044

45+
PYBIND11_DISABLE_WARNING_MSVC(4127)
46+
5147
// Provide a convenience alias for easier pass-by-ref usage with fully dynamic strides:
5248
using EigenDStride = Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>;
5349
template <typename MatrixType>
@@ -189,8 +185,7 @@ struct EigenProps {
189185
EigenIndex np_rows = a.shape(0), np_cols = a.shape(1),
190186
np_rstride = a.strides(0) / static_cast<ssize_t>(sizeof(Scalar)),
191187
np_cstride = a.strides(1) / static_cast<ssize_t>(sizeof(Scalar));
192-
if ((PYBIND11_SILENCE_MSVC_C4127(fixed_rows) && np_rows != rows)
193-
|| (PYBIND11_SILENCE_MSVC_C4127(fixed_cols) && np_cols != cols)) {
188+
if ((fixed_rows && np_rows != rows) || (fixed_cols && np_cols != cols)) {
194189
return false;
195190
}
196191

@@ -203,7 +198,7 @@ struct EigenProps {
203198
stride = a.strides(0) / static_cast<ssize_t>(sizeof(Scalar));
204199

205200
if (vector) { // Eigen type is a compile-time vector
206-
if (PYBIND11_SILENCE_MSVC_C4127(fixed) && size != n) {
201+
if (fixed && size != n) {
207202
return false; // Vector size mismatch
208203
}
209204
return {rows == 1 ? 1 : n, cols == 1 ? 1 : n, stride};
@@ -220,7 +215,7 @@ struct EigenProps {
220215
}
221216
return {1, n, stride};
222217
} // Otherwise it's either fully dynamic, or column dynamic; both become a column vector
223-
if (PYBIND11_SILENCE_MSVC_C4127(fixed_rows) && rows != n) {
218+
if (fixed_rows && rows != n) {
224219
return false;
225220
}
226221
return {n, 1, stride};

include/pybind11/eigen/tensor.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ static_assert(EIGEN_VERSION_AT_LEAST(3, 3, 0),
3535

3636
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3737

38+
PYBIND11_DISABLE_WARNING_MSVC(4127)
39+
3840
PYBIND11_NAMESPACE_BEGIN(detail)
3941

4042
inline bool is_tensor_aligned(const void *data) {
@@ -389,7 +391,7 @@ struct type_caster<Eigen::TensorMap<Type, Options>,
389391

390392
constexpr bool is_aligned = (Options & Eigen::Aligned) != 0;
391393

392-
if (PYBIND11_SILENCE_MSVC_C4127(is_aligned) && !is_tensor_aligned(arr.data())) {
394+
if (is_aligned && !is_tensor_aligned(arr.data())) {
393395
return false;
394396
}
395397

@@ -399,7 +401,7 @@ struct type_caster<Eigen::TensorMap<Type, Options>,
399401
return false;
400402
}
401403

402-
if (PYBIND11_SILENCE_MSVC_C4127(needs_writeable) && !arr.writeable()) {
404+
if (needs_writeable && !arr.writeable()) {
403405
return false;
404406
}
405407

include/pybind11/numpy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ class array : public buffer {
875875
*/
876876
template <typename T, ssize_t Dims = -1>
877877
detail::unchecked_mutable_reference<T, Dims> mutable_unchecked() & {
878-
if (PYBIND11_SILENCE_MSVC_C4127(Dims >= 0) && ndim() != Dims) {
878+
if (Dims >= 0 && ndim() != Dims) {
879879
throw std::domain_error("array has incorrect number of dimensions: "
880880
+ std::to_string(ndim()) + "; expected "
881881
+ std::to_string(Dims));
@@ -893,7 +893,7 @@ class array : public buffer {
893893
*/
894894
template <typename T, ssize_t Dims = -1>
895895
detail::unchecked_reference<T, Dims> unchecked() const & {
896-
if (PYBIND11_SILENCE_MSVC_C4127(Dims >= 0) && ndim() != Dims) {
896+
if (Dims >= 0 && ndim() != Dims) {
897897
throw std::domain_error("array has incorrect number of dimensions: "
898898
+ std::to_string(ndim()) + "; expected "
899899
+ std::to_string(Dims));

include/pybind11/pybind11.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949

5050
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
5151

52+
PYBIND11_DISABLE_WARNING_MSVC(4127)
53+
5254
PYBIND11_NAMESPACE_BEGIN(detail)
5355

5456
// Apply all the extensions translators from a list
@@ -177,7 +179,7 @@ class cpp_function : public function {
177179
auto *rec = unique_rec.get();
178180

179181
/* Store the capture object directly in the function record if there is enough space */
180-
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(capture) <= sizeof(rec->data))) {
182+
if (sizeof(capture) <= sizeof(rec->data)) {
181183
/* Without these pragmas, GCC warns that there might not be
182184
enough space to use the placement new operator. However, the
183185
'if' statement above ensures that this is the case. */
@@ -1841,8 +1843,7 @@ class class_ : public detail::generic_type {
18411843
if (holder_ptr) {
18421844
init_holder_from_existing(v_h, holder_ptr, std::is_copy_constructible<holder_type>());
18431845
v_h.set_holder_constructed();
1844-
} else if (PYBIND11_SILENCE_MSVC_C4127(detail::always_construct_holder<holder_type>::value)
1845-
|| inst->owned) {
1846+
} else if (detail::always_construct_holder<holder_type>::value || inst->owned) {
18461847
new (std::addressof(v_h.holder<holder_type>())) holder_type(v_h.value_ptr<type>());
18471848
v_h.set_holder_constructed();
18481849
}

include/pybind11/pytypes.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3535

36+
PYBIND11_DISABLE_WARNING_MSVC(4127)
37+
3638
/* A few forward declarations */
3739
class handle;
3840
class object;
@@ -1693,7 +1695,7 @@ PYBIND11_NAMESPACE_BEGIN(detail)
16931695
// unsigned type: (A)-1 != (B)-1 when A and B are unsigned types of different sizes).
16941696
template <typename Unsigned>
16951697
Unsigned as_unsigned(PyObject *o) {
1696-
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(Unsigned) <= sizeof(unsigned long))) {
1698+
if (sizeof(Unsigned) <= sizeof(unsigned long)) {
16971699
unsigned long v = PyLong_AsUnsignedLong(o);
16981700
return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
16991701
}
@@ -1710,7 +1712,7 @@ class int_ : public object {
17101712
template <typename T, detail::enable_if_t<std::is_integral<T>::value, int> = 0>
17111713
// NOLINTNEXTLINE(google-explicit-constructor)
17121714
int_(T value) {
1713-
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(T) <= sizeof(long))) {
1715+
if (sizeof(T) <= sizeof(long)) {
17141716
if (std::is_signed<T>::value) {
17151717
m_ptr = PyLong_FromLong((long) value);
17161718
} else {

tests/test_builtin_casters.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ class type_caster<ConstRefCasted> {
7272
PYBIND11_NAMESPACE_END(detail)
7373
PYBIND11_NAMESPACE_END(pybind11)
7474

75+
PYBIND11_NAMESPACE_BEGIN(test_builtin_casters)
76+
77+
PYBIND11_DISABLE_WARNING_MSVC(4127)
78+
7579
TEST_SUBMODULE(builtin_casters, m) {
7680
// test_simple_string
7781
m.def("string_roundtrip", [](const char *s) { return s; });
@@ -86,7 +90,7 @@ TEST_SUBMODULE(builtin_casters, m) {
8690
std::wstring wstr;
8791
wstr.push_back(0x61); // a
8892
wstr.push_back(0x2e18); //
89-
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(wchar_t) == 2)) {
93+
if (sizeof(wchar_t) == 2) {
9094
wstr.push_back(mathbfA16_1);
9195
wstr.push_back(mathbfA16_2);
9296
} // 𝐀, utf16
@@ -113,7 +117,7 @@ TEST_SUBMODULE(builtin_casters, m) {
113117
// Under Python 2.7, invalid unicode UTF-32 characters didn't appear to trigger
114118
// UnicodeDecodeError
115119
m.def("bad_utf32_string", [=]() { return std::u32string({a32, char32_t(0xd800), z32}); });
116-
if (PYBIND11_SILENCE_MSVC_C4127(sizeof(wchar_t) == 2)) {
120+
if (sizeof(wchar_t) == 2) {
117121
m.def("bad_wchar_string", [=]() {
118122
return std::wstring({wchar_t(0x61), wchar_t(0xd800)});
119123
});
@@ -385,3 +389,5 @@ TEST_SUBMODULE(builtin_casters, m) {
385389
m.def("takes_const_ref_wrap",
386390
[](std::reference_wrapper<const ConstRefCasted> x) { return x.get().tag; });
387391
}
392+
393+
PYBIND11_NAMESPACE_END(test_builtin_casters)

tests/test_eigen_tensor.inl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
#include "pybind11_tests.h"
1111

12-
namespace PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE {
12+
PYBIND11_NAMESPACE_BEGIN(PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE)
13+
14+
PYBIND11_DISABLE_WARNING_MSVC(4127)
1315

1416
template <typename M>
1517
void reset_tensor(M &x) {
@@ -90,7 +92,7 @@ struct CustomExample {
9092
template <int Options>
9193
void init_tensor_module(pybind11::module &m) {
9294
const char *needed_options = "";
93-
if (PYBIND11_SILENCE_MSVC_C4127(Options == Eigen::ColMajor)) {
95+
if (Options == Eigen::ColMajor) {
9496
needed_options = "F";
9597
} else {
9698
needed_options = "C";
@@ -330,4 +332,4 @@ void test_module(py::module_ &m) {
330332
init_tensor_module<Eigen::RowMajor>(c_style);
331333
}
332334

333-
} // namespace PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE
335+
PYBIND11_NAMESPACE_END(PYBIND11_TEST_EIGEN_TENSOR_NAMESPACE)

0 commit comments

Comments
 (0)