Skip to content

Commit af70073

Browse files
rwgkhenryiii
andauthored
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 <[email protected]>
1 parent 3893f37 commit af70073

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

include/pybind11/attr.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -517,23 +517,30 @@ template <size_t Nurse, size_t Patient> struct process_attribute<keep_alive<Nurs
517517
template <typename... Args> struct process_attributes {
518518
static void init(const Args&... args, function_record *r) {
519519
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
520-
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
521-
ignore_unused(unused);
520+
PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
521+
using expander = int[];
522+
(void) expander{
523+
0, ((void) process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...};
522524
}
523525
static void init(const Args&... args, type_record *r) {
524526
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(r);
525-
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::init(args, r), 0) ... };
526-
ignore_unused(unused);
527+
PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(r);
528+
using expander = int[];
529+
(void) expander{0,
530+
(process_attribute<typename std::decay<Args>::type>::init(args, r), 0)...};
527531
}
528532
static void precall(function_call &call) {
529533
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call);
530-
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::precall(call), 0) ... };
531-
ignore_unused(unused);
534+
using expander = int[];
535+
(void) expander{0,
536+
(process_attribute<typename std::decay<Args>::type>::precall(call), 0)...};
532537
}
533538
static void postcall(function_call &call, handle fn_ret) {
534539
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(call, fn_ret);
535-
int unused[] = { 0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0) ... };
536-
ignore_unused(unused);
540+
PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(fn_ret);
541+
using expander = int[];
542+
(void) expander{
543+
0, (process_attribute<typename std::decay<Args>::type>::postcall(call, fn_ret), 0)...};
537544
}
538545
};
539546

include/pybind11/cast.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ template <template<typename...> class Tuple, typename... Ts> class tuple_caster
609609
template <typename T, size_t... Is>
610610
static handle cast_impl(T &&src, return_value_policy policy, handle parent, index_sequence<Is...>) {
611611
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(src, policy, parent);
612+
PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(policy, parent);
612613
std::array<object, size> entries{{
613614
reinterpret_steal<object>(make_caster<Ts>::cast(std::get<Is>(std::forward<T>(src)), policy, parent))...
614615
}};
@@ -1235,8 +1236,8 @@ class unpacking_collector {
12351236
// Tuples aren't (easily) resizable so a list is needed for collection,
12361237
// but the actual function call strictly requires a tuple.
12371238
auto args_list = list();
1238-
int _[] = { 0, (process(args_list, std::forward<Ts>(values)), 0)... };
1239-
ignore_unused(_);
1239+
using expander = int[];
1240+
(void) expander{0, (process(args_list, std::forward<Ts>(values)), 0)...};
12401241

12411242
m_args = std::move(args_list);
12421243
}

include/pybind11/detail/common.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,6 @@ using function_signature_t = conditional_t<
734734
template <typename T> using is_lambda = satisfies_none_of<remove_reference_t<T>,
735735
std::is_function, std::is_pointer, std::is_member_pointer>;
736736

737-
/// Ignore that a variable is unused in compiler warnings
738-
inline void ignore_unused(const int *) { }
739-
740737
// [workaround(intel)] Internal error on fold expression
741738
/// Apply a function over each element of a parameter pack
742739
#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
927924
#endif
928925
}
929926

930-
#if defined(_MSC_VER) && _MSC_VER <= 1916
931-
932-
// warning C4100: Unreferenced formal parameter
927+
// For silencing "unused" compiler warnings in special situations.
933928
template <typename... Args>
934-
inline constexpr void workaround_incorrect_msvc_c4100(Args &&...) {}
929+
#if defined(_MSC_VER) && _MSC_VER >= 1910 && _MSC_VER < 1920 // MSVC 2017
930+
constexpr
931+
#endif
932+
inline void silence_unused_warnings(Args &&...) {}
935933

934+
// MSVC warning C4100: Unreferenced formal parameter
935+
#if defined(_MSC_VER) && _MSC_VER <= 1916
936936
# define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...) \
937-
detail::workaround_incorrect_msvc_c4100(__VA_ARGS__)
938-
937+
detail::silence_unused_warnings(__VA_ARGS__)
939938
#else
940939
# define PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(...)
941940
#endif
942941

942+
// GCC -Wunused-but-set-parameter All GCC versions (as of July 2021).
943+
#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
944+
# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...) \
945+
detail::silence_unused_warnings(__VA_ARGS__)
946+
#else
947+
# define PYBIND11_WORKAROUND_INCORRECT_GCC_UNUSED_BUT_SET_PARAMETER(...)
948+
#endif
949+
943950
#if defined(_MSC_VER) // All versions (as of July 2021).
944951

945952
// warning C4127: Conditional expression is constant

include/pybind11/detail/type_caster_base.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -927,18 +927,17 @@ template <typename type> class type_caster_base : public type_caster_generic {
927927
using Constructor = void *(*)(const void *);
928928

929929
/* Only enabled when the types are {copy,move}-constructible *and* when the type
930-
does not have a private operator new implementation. */
930+
does not have a private operator new implementation. A comma operator is used in the decltype
931+
argument to apply SFINAE to the public copy/move constructors.*/
931932
template <typename T, typename = enable_if_t<is_copy_constructible<T>::value>>
932-
static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) {
933-
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x);
933+
static auto make_copy_constructor(const T *) -> decltype(new T(std::declval<const T>()), Constructor{}) {
934934
return [](const void *arg) -> void * {
935935
return new T(*reinterpret_cast<const T *>(arg));
936936
};
937937
}
938938

939939
template <typename T, typename = enable_if_t<std::is_move_constructible<T>::value>>
940-
static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast<T *>(x))), Constructor{}) {
941-
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(x);
940+
static auto make_move_constructor(const T *) -> decltype(new T(std::declval<T&&>()), Constructor{}) {
942941
return [](const void *arg) -> void * {
943942
return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg))));
944943
};

include/pybind11/pybind11.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
1414
# pragma GCC diagnostic push
15-
# pragma GCC diagnostic ignored "-Wunused-but-set-parameter"
1615
# pragma GCC diagnostic ignored "-Wattributes"
1716
#endif
1817

0 commit comments

Comments
 (0)