From af7007331ab5a6f82a1fe38f041b624dea0b4084 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 6 Aug 2021 12:27:11 -0700 Subject: [PATCH] Removing GCC -Wunused-but-set-parameter from pragma block at the top of pybind11.h (#3164) * Cleanup triggered by work on pragma for GCC -Wunused-but-set-parameter. * Backing out changes to eigen.h (to be worked on later). * Adding PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER in type_caster_base.h (apparently needed only for older GCCs). * Apparently older compilers need a simpler overload for silence_unused_warnings(). * clang C++11 compatibility: removing constexpr * Special case for MSVC 2017: `constexpr void` return * Trying again without the silence_unused_warnings(const int *) overload. * Separate macros for ALL_GCC, OLD_GCC_UNUSED_BUT_SET_PARAMETER * Changing to __GNUC__ <= 2 (turning off) * Refined condition for PYBIND11_WORKAROUND_INCORRECT_OLD_GCC_UNUSED_BUT_SET_PARAMETER. * Quick experiment trying out suggestion by @henryiii * Introducing macro: PYBIND11_INT_ARRAY_WORKING_AROUND_MSVC_CLANG_ISSUES * Trying henryiii@ (void) expander idea. * fix: apply simpler expression with fewer workarounds * Purging new-but-already-obsoleted macro, made possible by @henryiii's commit. * Renaming `ALL_GCC` macro back to just `GCC` (because there is no `OLD` anymore, luckily). * [actions skip] Adding "All GCC versions" to comment, to be clear about it. Co-authored-by: Henry Schreiner --- include/pybind11/attr.h | 23 +++++++++++++------- include/pybind11/cast.h | 5 +++-- include/pybind11/detail/common.h | 25 ++++++++++++++-------- include/pybind11/detail/type_caster_base.h | 9 ++++---- include/pybind11/pybind11.h | 1 - 5 files changed, 38 insertions(+), 25 deletions(-) diff --git a/include/pybind11/attr.h b/include/pybind11/attr.h index 20d119f0ff..7188fd7e3c 100644 --- a/include/pybind11/attr.h +++ b/include/pybind11/attr.h @@ -517,23 +517,30 @@ template struct process_attribute struct process_attributes { static void init(const Args&... args, function_record *r) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); - int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; - ignore_unused(unused); + 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); - int unused[] = { 0, (process_attribute::type>::init(args, r), 0) ... }; - ignore_unused(unused); + PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r); + 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); - int unused[] = { 0, (process_attribute::type>::precall(call), 0) ... }; - ignore_unused(unused); + 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); - int unused[] = { 0, (process_attribute::type>::postcall(call, fn_ret), 0) ... }; - ignore_unused(unused); + 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 988a236950..718dc2de80 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(_); + 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 27a79bfdd5..08430b5a2e 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,29 @@ 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 &&...) {} +#if defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER < 1920 // MSVC 2017 +constexpr +#endif +inline 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 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__) +#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/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index e2d1bcb8cc..a708d87fb4 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -927,18 +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); + 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); + 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)))); }; 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