From 6587ce7a36e3ed40be9fce2e13e86677519e6e06 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 08:35:39 -0700 Subject: [PATCH 01/17] Cleanup triggered by work on pragma for GCC -Wunused-but-set-parameter. --- include/pybind11/attr.h | 11 +++++++---- include/pybind11/cast.h | 5 +++-- include/pybind11/detail/common.h | 22 +++++++++++++--------- include/pybind11/eigen.h | 12 +++++++----- include/pybind11/pybind11.h | 1 - 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 20d119f0ff..5a5281092e 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -517,23 +517,26 @@ template struct process_attribute struct process_attributes { static void init(const Args&... args, function_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; - ignore_unused(unused); + silence_unused_warnings(unused); } static void init(const Args&... args, type_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; - ignore_unused(unused); + silence_unused_warnings(unused); } static void precall(function_call &call) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call); int unused[] = { 0, (process_attribute::type>::precall(call), 0) ... }; - ignore_unused(unused); + silence_unused_warnings(unused); } static void postcall(function_call &call, handle fn_ret) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret); int unused[] = { 0, (process_attribute::type>::postcall(call, fn_ret), 0) ... }; - ignore_unused(unused); + silence_unused_warnings(unused); } }; diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 988a236950..c25017b49a 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -609,6 +609,7 @@ template class Tuple, typename... Ts> class tuple_caster template static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(policy, parent); std::array entries{{ reinterpret_steal(make_caster::cast(std::get(std::forward(src)), policy, parent))... }}; @@ -1235,8 +1236,8 @@ class unpacking_collector { // Tuples aren't (easily) resizable so a list is needed for collection, // but the actual function call strictly requires a tuple. auto args_list = list(); - int _[] = { 0, (process(args_list, std::forward(values)), 0)... }; - ignore_unused(_); + int unused[] = { 0, (process(args_list, std::forward(values)), 0)... }; + silence_unused_warnings(unused); m_args = std::move(args_list); } diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 27a79bfdd5..9a451f8e66 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -734,9 +734,6 @@ using function_signature_t = conditional_t< template using is_lambda = satisfies_none_of, std::is_function, std::is_pointer, std::is_member_pointer>; -/// Ignore that a variable is unused in compiler warnings -inline void ignore_unused(const int *) { } - // [workaround(intel)] Internal error on fold expression /// Apply a function over each element of a parameter pack #if defined(__cpp_fold_expressions) && !defined(__INTEL_COMPILER) @@ -927,19 +924,26 @@ inline static std::shared_ptr try_get_shared_from_this(std::enable_shared_fro #endif } -#if defined(_MSC_VER) && _MSC_VER <= 1916 - -// warning C4100: Unreferenced formal parameter +// For silencing "unused" compiler warnings in special situations. template -inline constexpr void workaround_incorrect_msvc_c4100(Args &&...) {} +inline constexpr void silence_unused_warnings(Args &&...) {} +// MSVC warning C4100: Unreferenced formal parameter +#if defined(_MSC_VER) && _MSC_VER <= 1916 # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) \ - detail::workaround_incorrect_msvc_c4100(__VA_ARGS__) - + detail::silence_unused_warnings(__VA_ARGS__) #else # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) #endif +// GCC -Wunused-but-set-parameter +#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) +# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) \ + detail::silence_unused_warnings(__VA_ARGS__) +#else +# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) +#endif + #if defined(_MSC_VER) // All versions (as of July 2021). // warning C4127: Conditional expression is constant diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index 218fe27034..2ffb678baa 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -12,10 +12,12 @@ #include "numpy.h" #if defined(__INTEL_COMPILER) -# pragma warning(disable: 1682) // implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem) +# pragma warning push +# pragma warning disable 1682 // implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem) #elif defined(__GNUG__) || defined(__clang__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wconversion" +# pragma GCC diagnostic ignored "-Wdeprecated-copy" # pragma GCC diagnostic ignored "-Wdeprecated-declarations" # ifdef __clang__ // Eigen generates a bunch of implicit-copy-constructor-is-deprecated warnings with -Wdeprecated @@ -25,9 +27,7 @@ # if __GNUC__ >= 7 # pragma GCC diagnostic ignored "-Wint-in-bool-context" # endif -#endif - -#if defined(_MSC_VER) +#elif defined(_MSC_VER) # pragma warning(push) # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4996) // warning C4996: std::unary_negate is deprecated in C++17 @@ -597,7 +597,9 @@ struct type_caster::value>> { PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) -#if defined(__GNUG__) || defined(__clang__) +#if defined(__INTEL_COMPILER) +# pragma warning pop +#elif defined(__GNUG__) || defined(__clang__) # pragma GCC diagnostic pop #elif defined(_MSC_VER) # pragma warning(pop) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 54213297dc..ba29f96474 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -12,7 +12,6 @@ #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-but-set-parameter" # pragma GCC diagnostic ignored "-Wattributes" #endif From a68105dc455d38a5685a8c94b8b339715282c71e Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 08:48:00 -0700 Subject: [PATCH 02/17] Backing out changes to eigen.h (to be worked on later). --- include/pybind11/eigen.h | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/include/pybind11/eigen.h b/include/pybind11/eigen.h index 2ffb678baa..218fe27034 100644 --- a/include/pybind11/eigen.h +++ b/include/pybind11/eigen.h @@ -12,12 +12,10 @@ #include "numpy.h" #if defined(__INTEL_COMPILER) -# pragma warning push -# pragma warning disable 1682 // implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem) +# pragma warning(disable: 1682) // implicit conversion of a 64-bit integral type to a smaller integral type (potential portability problem) #elif defined(__GNUG__) || defined(__clang__) # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wconversion" -# pragma GCC diagnostic ignored "-Wdeprecated-copy" # pragma GCC diagnostic ignored "-Wdeprecated-declarations" # ifdef __clang__ // Eigen generates a bunch of implicit-copy-constructor-is-deprecated warnings with -Wdeprecated @@ -27,7 +25,9 @@ # if __GNUC__ >= 7 # pragma GCC diagnostic ignored "-Wint-in-bool-context" # endif -#elif defined(_MSC_VER) +#endif + +#if defined(_MSC_VER) # pragma warning(push) # pragma warning(disable: 4127) // warning C4127: Conditional expression is constant # pragma warning(disable: 4996) // warning C4996: std::unary_negate is deprecated in C++17 @@ -597,9 +597,7 @@ struct type_caster::value>> { PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) -#if defined(__INTEL_COMPILER) -# pragma warning pop -#elif defined(__GNUG__) || defined(__clang__) +#if defined(__GNUG__) || defined(__clang__) # pragma GCC diagnostic pop #elif defined(_MSC_VER) # pragma warning(pop) From 3fe85279ffb68f9af9f3a159fd5af305f450723b Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 08:50:24 -0700 Subject: [PATCH 03/17] Adding PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER in type_caster_base.h (apparently needed only for older GCCs). --- include/pybind11/detail/type_caster_base.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index e2d1bcb8cc..ac507a6730 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -931,6 +931,7 @@ template class type_caster_base : public type_caster_generic { template ::value>> static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(x); // TODO GCC < ? return [](const void *arg) -> void * { return new T(*reinterpret_cast(arg)); }; @@ -939,6 +940,7 @@ template class type_caster_base : public type_caster_generic { template ::value>> static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast(x))), Constructor{}) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(x); // TODO GCC < ? return [](const void *arg) -> void * { return new T(std::move(*const_cast(reinterpret_cast(arg)))); }; From 2903e2ae2a8ec54bf5dde682ab7a8554c39a11c5 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 09:09:40 -0700 Subject: [PATCH 04/17] Apparently older compilers need a simpler overload for silence_unused_warnings(). --- include/pybind11/detail/common.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 9a451f8e66..31cd71a417 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -927,6 +927,7 @@ inline static std::shared_ptr try_get_shared_from_this(std::enable_shared_fro // For silencing "unused" compiler warnings in special situations. template inline constexpr void silence_unused_warnings(Args &&...) {} +inline void silence_unused_warnings(const int *) {} // MSVC warning C4100: Unreferenced formal parameter #if defined(_MSC_VER) && _MSC_VER <= 1916 From ee88918d37dd2429854157651108ea54621528b3 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 09:22:26 -0700 Subject: [PATCH 05/17] clang C++11 compatibility: removing constexpr --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 31cd71a417..4e388c30aa 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -926,7 +926,7 @@ inline static std::shared_ptr try_get_shared_from_this(std::enable_shared_fro // For silencing "unused" compiler warnings in special situations. template -inline constexpr void silence_unused_warnings(Args &&...) {} +inline void silence_unused_warnings(Args &&...) {} inline void silence_unused_warnings(const int *) {} // MSVC warning C4100: Unreferenced formal parameter From a3ba06b4853d3335ad0ce55b67641ed796ae98b9 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 09:59:55 -0700 Subject: [PATCH 06/17] Special case for MSVC 2017: `constexpr void` return --- include/pybind11/detail/common.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 4e388c30aa..260249f8b2 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -926,7 +926,11 @@ inline static std::shared_ptr try_get_shared_from_this(std::enable_shared_fro // For silencing "unused" compiler warnings in special situations. template +#if defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER < 1920 // MSVC 2017 +inline constexpr void silence_unused_warnings(Args &&...) {} +#else inline void silence_unused_warnings(Args &&...) {} +#endif inline void silence_unused_warnings(const int *) {} // MSVC warning C4100: Unreferenced formal parameter From c4e0ed3066027c9855ab10556018fe688442c771 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 10:18:18 -0700 Subject: [PATCH 07/17] Trying again without the silence_unused_warnings(const int *) overload. --- include/pybind11/detail/common.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 260249f8b2..4fed18339c 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -926,12 +926,12 @@ inline static std::shared_ptr try_get_shared_from_this(std::enable_shared_fro // For silencing "unused" compiler warnings in special situations. template +inline #if defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER < 1920 // MSVC 2017 -inline constexpr void silence_unused_warnings(Args &&...) {} -#else -inline void silence_unused_warnings(Args &&...) {} +constexpr #endif -inline void silence_unused_warnings(const int *) {} +void silence_unused_warnings(Args &&...) {} +// XXX XXX XXX inline void silence_unused_warnings(const int *) {} // MSVC warning C4100: Unreferenced formal parameter #if defined(_MSC_VER) && _MSC_VER <= 1916 From fab0396249f05f8db701ecff9b7a9852e2ab9538 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 10:53:59 -0700 Subject: [PATCH 08/17] Separate macros for ALL_GCC, OLD_GCC_UNUSED_BUT_SET_PARAMETER --- include/pybind11/attr.h | 6 +++--- include/pybind11/cast.h | 2 +- include/pybind11/detail/common.h | 17 +++++++++++------ include/pybind11/detail/type_caster_base.h | 4 ++-- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 5a5281092e..21e5559df9 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -517,13 +517,13 @@ template struct process_attribute struct process_attributes { static void init(const Args&... args, function_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); - PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); + PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r); int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; silence_unused_warnings(unused); } static void init(const Args&... args, type_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); - PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); + PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r); int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; silence_unused_warnings(unused); } @@ -534,7 +534,7 @@ template struct process_attributes { } static void postcall(function_call &call, handle fn_ret) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); - PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret); + PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret); int unused[] = { 0, (process_attribute::type>::postcall(call, fn_ret), 0) ... }; silence_unused_warnings(unused); } diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index c25017b49a..2d9fa1b088 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -609,7 +609,7 @@ template class Tuple, typename... Ts> class tuple_caster template static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent); - PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(policy, parent); + PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(policy, parent); std::array entries{{ reinterpret_steal(make_caster::cast(std::get(std::forward(src)), policy, parent))... }}; diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 4fed18339c..6d557c65c9 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -926,12 +926,10 @@ inline static std::shared_ptr try_get_shared_from_this(std::enable_shared_fro // For silencing "unused" compiler warnings in special situations. template -inline -#if defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER < 1920 // MSVC 2017 +#if defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER < 1920 // MSVC 2017 constexpr #endif -void silence_unused_warnings(Args &&...) {} -// XXX XXX XXX inline void silence_unused_warnings(const int *) {} +inline void silence_unused_warnings(Args &&...) {} // MSVC warning C4100: Unreferenced formal parameter #if defined(_MSC_VER) && _MSC_VER <= 1916 @@ -943,10 +941,17 @@ void silence_unused_warnings(Args &&...) {} // GCC -Wunused-but-set-parameter #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) -# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) \ +# define PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(...) \ detail::silence_unused_warnings(__VA_ARGS__) +# if defined(__GNUC__) && __GNUC__ <= 7 +# define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) \ + detail::silence_unused_warnings(__VA_ARGS__) +# else +# define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) +# endif #else -# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) +# define PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(...) +# define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) #endif #if defined(_MSC_VER) // All versions (as of July 2021). diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index ac507a6730..a3ce4204bb 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -931,7 +931,7 @@ template class type_caster_base : public type_caster_generic { template ::value>> static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); - PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(x); // TODO GCC < ? + PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(x); return [](const void *arg) -> void * { return new T(*reinterpret_cast(arg)); }; @@ -940,7 +940,7 @@ template class type_caster_base : public type_caster_generic { template ::value>> static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast(x))), Constructor{}) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); - PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(x); // TODO GCC < ? + PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(x); return [](const void *arg) -> void * { return new T(std::move(*const_cast(reinterpret_cast(arg)))); }; From 15cbc94d20313f57baede2c995295ff91f9dfb83 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 11:08:19 -0700 Subject: [PATCH 09/17] Changing to __GNUC__ <= 2 (turning off) --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 6d557c65c9..90878eff6e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -943,7 +943,7 @@ inline void silence_unused_warnings(Args &&...) {} #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # define PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(...) \ detail::silence_unused_warnings(__VA_ARGS__) -# if defined(__GNUC__) && __GNUC__ <= 7 +# if defined(__GNUC__) && __GNUC__ <= 2 # define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) \ detail::silence_unused_warnings(__VA_ARGS__) # else From 9f9747135c3707651a487fc50440c6bfd34bbc32 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 11:28:27 -0700 Subject: [PATCH 10/17] Refined condition for PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER. --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 90878eff6e..49517afc22 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -943,7 +943,7 @@ inline void silence_unused_warnings(Args &&...) {} #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # define PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(...) \ detail::silence_unused_warnings(__VA_ARGS__) -# if defined(__GNUC__) && __GNUC__ <= 2 +# if defined(__GNUC__) && __GNUC__ <= 5 // 4 is certain, 5 unknown, 6 is certain # define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) \ detail::silence_unused_warnings(__VA_ARGS__) # else From e70518dcee64fef431bac097abb7ed38a4678fea Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 31 Jul 2021 23:14:21 -0700 Subject: [PATCH 11/17] Quick experiment trying out suggestion by @henryiii --- include/pybind11/attr.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 21e5559df9..c97052ae87 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -529,8 +529,9 @@ template struct process_attributes { } static void precall(function_call &call) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call); - int unused[] = { 0, (process_attribute::type>::precall(call), 0) ... }; - silence_unused_warnings(unused); + // int unused[] = { 0, (process_attribute::type>::precall(call), 0) ... }; + // silence_unused_warnings(unused); + (int []) { 0, (process_attribute::type>::precall(call), 0) ... }; } static void postcall(function_call &call, handle fn_ret) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); From 1dfd35fbeca57b744f1ef29ed4dda844e43cf408 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 1 Aug 2021 00:34:40 -0700 Subject: [PATCH 12/17] Introducing macro: PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES --- include/pybind11/attr.h | 18 +++++++++--------- include/pybind11/cast.h | 4 ++-- include/pybind11/detail/common.h | 11 +++++++++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index c97052ae87..68ddcf3b00 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -518,26 +518,26 @@ template struct process_attributes { static void init(const Args&... args, function_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r); - int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; - silence_unused_warnings(unused); + PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( + {0, (process_attribute::type>::init(args, r), 0)...}); } static void init(const Args&... args, type_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r); - int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; - silence_unused_warnings(unused); + PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( + {0, (process_attribute::type>::init(args, r), 0)...}); } static void precall(function_call &call) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call); - // int unused[] = { 0, (process_attribute::type>::precall(call), 0) ... }; - // silence_unused_warnings(unused); - (int []) { 0, (process_attribute::type>::precall(call), 0) ... }; + PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( + {0, (process_attribute::type>::precall(call), 0)...}); } static void postcall(function_call &call, handle fn_ret) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret); - int unused[] = { 0, (process_attribute::type>::postcall(call, fn_ret), 0) ... }; - silence_unused_warnings(unused); + PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( + {0, + (process_attribute::type>::postcall(call, fn_ret), 0)...}); } }; diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 2d9fa1b088..9f9ae42ae1 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1236,8 +1236,8 @@ class unpacking_collector { // Tuples aren't (easily) resizable so a list is needed for collection, // but the actual function call strictly requires a tuple. auto args_list = list(); - int unused[] = { 0, (process(args_list, std::forward(values)), 0)... }; - silence_unused_warnings(unused); + PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( + {0, (process(args_list, std::forward(values)), 0)...}); m_args = std::move(args_list); } diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 49517afc22..53a5ecd8f7 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -954,6 +954,17 @@ inline void silence_unused_warnings(Args &&...) {} # define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) #endif +// MSVC error C4576: a parenthesized type followed by an initializer list is a +// non-standard explicit type conversion syntax +// clang: expression result unused [-Wunused-value] +#if defined(_MSC_VER) || defined(__clang__) // All versions (as of July 2021). +# define PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(...) \ + int dummy[] = __VA_ARGS__; \ + detail::silence_unused_warnings(dummy) +#else +# define PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(...) (int[]) __VA_ARGS__ +#endif + #if defined(_MSC_VER) // All versions (as of July 2021). // warning C4127: Conditional expression is constant From 3d2cb7cee8bb7181c40dc8ab0ea056c7ae1a0d38 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 3 Aug 2021 22:58:08 -0700 Subject: [PATCH 13/17] Trying henryiii@ (void) expander idea. --- include/pybind11/attr.h | 21 ++++++++++++--------- include/pybind11/cast.h | 4 ++-- include/pybind11/detail/common.h | 11 ----------- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 68ddcf3b00..ee844484d4 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -518,26 +518,29 @@ template struct process_attributes { static void init(const Args&... args, function_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r); - PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( - {0, (process_attribute::type>::init(args, r), 0)...}); + using expander = int[]; + (void) expander{ + 0, ((void) process_attribute::type>::init(args, r), 0)...}; } static void init(const Args&... args, type_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r); - PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( - {0, (process_attribute::type>::init(args, r), 0)...}); + using expander = int[]; + (void) expander{0, + (process_attribute::type>::init(args, r), 0)...}; } static void precall(function_call &call) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call); - PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( - {0, (process_attribute::type>::precall(call), 0)...}); + using expander = int[]; + (void) expander{0, + (process_attribute::type>::precall(call), 0)...}; } static void postcall(function_call &call, handle fn_ret) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret); - PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( - {0, - (process_attribute::type>::postcall(call, fn_ret), 0)...}); + using expander = int[]; + (void) expander{ + 0, (process_attribute::type>::postcall(call, fn_ret), 0)...}; } }; diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 9f9ae42ae1..47fcc7b94a 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1236,8 +1236,8 @@ class unpacking_collector { // Tuples aren't (easily) resizable so a list is needed for collection, // but the actual function call strictly requires a tuple. auto args_list = list(); - PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES( - {0, (process(args_list, std::forward(values)), 0)...}); + using expander = int[]; + (void) expander{0, (process(args_list, std::forward(values)), 0)...}; m_args = std::move(args_list); } diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 53a5ecd8f7..49517afc22 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -954,17 +954,6 @@ inline void silence_unused_warnings(Args &&...) {} # define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) #endif -// MSVC error C4576: a parenthesized type followed by an initializer list is a -// non-standard explicit type conversion syntax -// clang: expression result unused [-Wunused-value] -#if defined(_MSC_VER) || defined(__clang__) // All versions (as of July 2021). -# define PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(...) \ - int dummy[] = __VA_ARGS__; \ - detail::silence_unused_warnings(dummy) -#else -# define PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES(...) (int[]) __VA_ARGS__ -#endif - #if defined(_MSC_VER) // All versions (as of July 2021). // warning C4127: Conditional expression is constant From 218732d402c80c9ce6308e52a63e1378a067da24 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 6 Aug 2021 13:03:26 -0400 Subject: [PATCH 14/17] fix: apply simpler expression with fewer workarounds --- include/pybind11/detail/type_caster_base.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index a3ce4204bb..a708d87fb4 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -927,20 +927,17 @@ template class type_caster_base : public type_caster_generic { using Constructor = void *(*)(const void *); /* Only enabled when the types are {copy,move}-constructible *and* when the type - does not have a private operator new implementation. */ + does not have a private operator new implementation. A comma operator is used in the decltype + argument to apply SFINAE to the public copy/move constructors.*/ template ::value>> - static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) { - PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); - PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(x); + static auto make_copy_constructor(const T *) -> decltype(new T(std::declval()), Constructor{}) { return [](const void *arg) -> void * { return new T(*reinterpret_cast(arg)); }; } template ::value>> - static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast(x))), Constructor{}) { - PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x); - PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(x); + static auto make_move_constructor(const T *) -> decltype(new T(std::declval()), Constructor{}) { return [](const void *arg) -> void * { return new T(std::move(*const_cast(reinterpret_cast(arg)))); }; From 8a55d0daa39e7b4618c8f2b5b78c38e05ea3415c Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 6 Aug 2021 11:41:24 -0700 Subject: [PATCH 15/17] Purging new-but-already-obsoleted macro, made possible by @henryiii's commit. --- include/pybind11/detail/common.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 49517afc22..f919797c61 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -943,15 +943,8 @@ inline void silence_unused_warnings(Args &&...) {} #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # define PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(...) \ detail::silence_unused_warnings(__VA_ARGS__) -# if defined(__GNUC__) && __GNUC__ <= 5 // 4 is certain, 5 unknown, 6 is certain -# define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) \ - detail::silence_unused_warnings(__VA_ARGS__) -# else -# define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) -# endif #else # define PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(...) -# define PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER(...) #endif #if defined(_MSC_VER) // All versions (as of July 2021). From 5ed43961669dca7cc7830f27f3a86ec6db75e79d Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 6 Aug 2021 11:46:50 -0700 Subject: [PATCH 16/17] Renaming `ALL_GCC` macro back to just `GCC` (because there is no `OLD` anymore, luckily). --- include/pybind11/attr.h | 6 +++--- include/pybind11/cast.h | 2 +- include/pybind11/detail/common.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index ee844484d4..7188fd7e3c 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -517,14 +517,14 @@ template struct process_attribute struct process_attributes { static void init(const Args&... args, function_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); - PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); using expander = int[]; (void) expander{ 0, ((void) process_attribute::type>::init(args, r), 0)...}; } static void init(const Args&... args, type_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); - PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(r); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); using expander = int[]; (void) expander{0, (process_attribute::type>::init(args, r), 0)...}; @@ -537,7 +537,7 @@ template struct process_attributes { } static void postcall(function_call &call, handle fn_ret) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); - PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret); using expander = int[]; (void) expander{ 0, (process_attribute::type>::postcall(call, fn_ret), 0)...}; diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 47fcc7b94a..718dc2de80 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -609,7 +609,7 @@ template class Tuple, typename... Ts> class tuple_caster template static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent); - PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(policy, parent); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(policy, parent); std::array entries{{ reinterpret_steal(make_caster::cast(std::get(std::forward(src)), policy, parent))... }}; diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index f919797c61..66b2004dc9 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -941,10 +941,10 @@ inline void silence_unused_warnings(Args &&...) {} // GCC -Wunused-but-set-parameter #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) -# define PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(...) \ +# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) \ detail::silence_unused_warnings(__VA_ARGS__) #else -# define PYBIND11_WORKAROUND_INCORRECT_ALL_GCC_UNUSED_BUT_SET_PARAMETER(...) +# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) #endif #if defined(_MSC_VER) // All versions (as of July 2021). From 9caa3a21a4865071b57ee11f3ccd7611f4ac5a80 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 6 Aug 2021 11:57:56 -0700 Subject: [PATCH 17/17] [actions skip] Adding "All GCC versions" to comment, to be clear about it. --- include/pybind11/detail/common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index 66b2004dc9..08430b5a2e 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -939,7 +939,7 @@ inline void silence_unused_warnings(Args &&...) {} # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) #endif -// GCC -Wunused-but-set-parameter +// GCC -Wunused-but-set-parameter All GCC versions (as of July 2021). #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) # define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) \ detail::silence_unused_warnings(__VA_ARGS__)