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 <henryschreineriii@gmail.com>
This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-08-06 12:27:11 -07:00 committed by GitHub
parent 3893f37bce
commit af7007331a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 25 deletions

View File

@ -517,23 +517,30 @@ template <size_t Nurse, size_t Patient> struct process_attribute<keep_alive<Nurs
template <typename... Args> struct process_attributes { template <typename... Args> struct process_attributes {
static void init(const Args&... args, function_record *r) { static void init(const Args&... args, function_record *r) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... }; PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
ignore_unused(unused); using expander = int[];
(void) expander{
0, ((void) process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...};
} }
static void init(const Args&... args, type_record *r) { static void init(const Args&... args, type_record *r) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r); PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... }; PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
ignore_unused(unused); using expander = int[];
(void) expander{0,
(process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...};
} }
static void precall(function_call &call) { static void precall(function_call &call) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call); PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call);
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::precall(call), 0) ... }; using expander = int[];
ignore_unused(unused); (void) expander{0,
(process_attribute<typename std::decay<Args>::type>::precall(call), 0)...};
} }
static void postcall(function_call &call, handle fn_ret) { static void postcall(function_call &call, handle fn_ret) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret); PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret);
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0) ... }; PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret);
ignore_unused(unused); using expander = int[];
(void) expander{
0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0)...};
} }
}; };

View File

@ -609,6 +609,7 @@ protected:
template <typename T, size_t... Is> template <typename T, size_t... Is>
static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence<Is...>) { static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence<Is...>) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent); PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent);
PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(policy, parent);
std::array<object, size> entries{{ std::array<object, size> entries{{
reinterpret_steal<object>(make_caster<Ts>::cast(std::get<Is>(std::forward<T>(src)), policy, parent))... reinterpret_steal<object>(make_caster<Ts>::cast(std::get<Is>(std::forward<T>(src)), policy, parent))...
}}; }};
@ -1235,8 +1236,8 @@ public:
// Tuples aren't (easily) resizable so a list is needed for collection, // Tuples aren't (easily) resizable so a list is needed for collection,
// but the actual function call strictly requires a tuple. // but the actual function call strictly requires a tuple.
auto args_list = list(); auto args_list = list();
int _[] = { 0, (process(args_list, std::forward<Ts>(values)), 0)... }; using expander = int[];
ignore_unused(_); (void) expander{0, (process(args_list, std::forward<Ts>(values)), 0)...};
m_args = std::move(args_list); m_args = std::move(args_list);
} }

View File

@ -734,9 +734,6 @@ using function_signature_t = conditional_t<
template <typename T> using is_lambda = satisfies_none_of<remove_reference_t<T>, template <typename T> using is_lambda = satisfies_none_of<remove_reference_t<T>,
std::is_function, std::is_pointer, std::is_member_pointer>; 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 // [workaround(intel)] Internal error on fold expression
/// Apply a function over each element of a parameter pack /// Apply a function over each element of a parameter pack
#if defined(__cpp_fold_expressions) && !defined(__INTEL_COMPILER) #if defined(__cpp_fold_expressions) && !defined(__INTEL_COMPILER)
@ -927,19 +924,29 @@ inline static std::shared_ptr<T> try_get_shared_from_this(std::enable_shared_fro
#endif #endif
} }
#if defined(_MSC_VER) && _MSC_VER <= 1916 // For silencing "unused" compiler warnings in special situations.
// warning C4100: Unreferenced formal parameter
template <typename... Args> template <typename... Args>
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(...) \ # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) \
detail::workaround_incorrect_msvc_c4100(__VA_ARGS__) detail::silence_unused_warnings(__VA_ARGS__)
#else #else
# define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) # define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
#endif #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). #if defined(_MSC_VER) // All versions (as of July 2021).
// warning C4127: Conditional expression is constant // warning C4127: Conditional expression is constant

View File

@ -927,18 +927,17 @@ protected:
using Constructor = void *(*)(const void *); using Constructor = void *(*)(const void *);
/* Only enabled when the types are {copy,move}-constructible *and* when the type /* 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 <typename T, typename = enable_if_t<is_copy_constructible<T>::value>> template <typename T, typename = enable_if_t<is_copy_constructible<T>::value>>
static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) { static auto make_copy_constructor(const T *) -> decltype(new T(std::declval<const T>()), Constructor{}) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x);
return [](const void *arg) -> void * { return [](const void *arg) -> void * {
return new T(*reinterpret_cast<const T *>(arg)); return new T(*reinterpret_cast<const T *>(arg));
}; };
} }
template <typename T, typename = enable_if_t<std::is_move_constructible<T>::value>> template <typename T, typename = enable_if_t<std::is_move_constructible<T>::value>>
static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast<T *>(x))), Constructor{}) { static auto make_move_constructor(const T *) -> decltype(new T(std::declval<T&&>()), Constructor{}) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x);
return [](const void *arg) -> void * { return [](const void *arg) -> void * {
return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg)))); return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg))));
}; };

View File

@ -12,7 +12,6 @@
#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER) #if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
# pragma GCC diagnostic push # pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
# pragma GCC diagnostic ignored "-Wattributes" # pragma GCC diagnostic ignored "-Wattributes"
#endif #endif