Fix gcc compiler warnings (#5523)

* CI: Fail on any warnings with clang 18

* CI: Fail on any warnings with gcc 13

* Fix cmake's try_compile warning

* Guard redundant declarations

* ci.yml: fix syntax error

* Use PYBIND11_WARNING macros

* Fix more redundant declarations

... introduced with merge of smart_holder branch
This commit is contained in:
Robert Haschke
2025-04-30 19:29:00 +02:00
committed by GitHub
parent d3fee42905
commit c7f3460f18
6 changed files with 23 additions and 8 deletions

View File

@@ -199,7 +199,7 @@ jobs:
run: |
uv pip install --python=python --system setuptools
pytest tests/extra_setuptools
if: "!(matrix.runs-on == 'windows-2022')"
if: matrix.runs-on != 'windows-2022'
manylinux:
name: Manylinux on 🐍 3.13t • GIL
@@ -330,9 +330,10 @@ jobs:
container_suffix: "-bullseye"
- clang: 18
std: 20
cxx_flags: "-Werror -Wall -Wextra -Wwrite-strings -Wunreachable-code -Wpointer-arith -Wredundant-decls"
container_suffix: "-bookworm"
name: "🐍 3 • Clang ${{ matrix.clang }} • C++${{ matrix.std }} • x64"
name: "🐍 3 • Clang ${{ matrix.clang }} • C++${{ matrix.std }} • x64${{ matrix.cxx_flags && ' • cxx_flags' || '' }}"
container: "silkeh/clang:${{ matrix.clang }}${{ matrix.container_suffix }}"
steps:
@@ -348,6 +349,7 @@ jobs:
-DPYBIND11_WERROR=ON
-DDOWNLOAD_CATCH=ON
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
-DCMAKE_CXX_FLAGS="${{ matrix.cxx_flags }}"
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
- name: Build
@@ -491,9 +493,9 @@ jobs:
- { gcc: 9, std: 20 }
- { gcc: 10, std: 17 }
- { gcc: 10, std: 20 }
- { gcc: 13, std: 20 }
- { gcc: 13, std: 20, cxx_flags: "-Wall -Wextra -Wwrite-strings -Wunreachable-code -Wpointer-arith -Wredundant-decls" }
name: "🐍 3 • GCC ${{ matrix.gcc }} • C++${{ matrix.std }}• x64"
name: "🐍 3 • GCC ${{ matrix.gcc }} • C++${{ matrix.std }} • x64${{ matrix.cxx_flags && ' • cxx_flags' || '' }}"
container: "gcc:${{ matrix.gcc }}"
steps:
@@ -515,6 +517,7 @@ jobs:
-DPYBIND11_WERROR=ON
-DDOWNLOAD_CATCH=ON
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
-DCMAKE_CXX_FLAGS="${{ matrix.cxx_flags }}"
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
- name: Build

View File

@@ -499,8 +499,13 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) {
Py_DECREF(type);
}
PYBIND11_WARNING_PUSH
PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
std::string error_string();
PYBIND11_WARNING_POP
/** Create the type which can be used as a common base for all classes. This is
needed in order to satisfy Python's requirements for multiple inheritance.
Return value: New reference. */

View File

@@ -173,9 +173,6 @@ inline int tp_init_impl(PyObject *, PyObject *, PyObject *) {
// return -1; // Unreachable.
}
// The implementation needs the definition of `class cpp_function`.
void tp_dealloc_impl(PyObject *self);
inline void tp_free_impl(void *) {
pybind11_fail("UNEXPECTED CALL OF function_record_PyTypeObject_methods::tp_free_impl");
}

View File

@@ -511,9 +511,14 @@ inline PyThreadState *get_thread_state_unchecked() {
void keep_alive_impl(handle nurse, handle patient);
inline PyObject *make_new_instance(PyTypeObject *type);
PYBIND11_WARNING_PUSH
PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
// PYBIND11:REMINDER: Needs refactoring of existing pybind11 code.
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo);
PYBIND11_WARNING_POP
PYBIND11_NAMESPACE_BEGIN(smart_holder_type_caster_support)
struct value_and_holder_helper {

View File

@@ -32,9 +32,14 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)
PYBIND11_WARNING_PUSH
PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
// forward declarations
PyThreadState *get_thread_state_unchecked();
PYBIND11_WARNING_POP
PYBIND11_NAMESPACE_END(detail)
/* The functions below essentially reproduce the PyGILState_* API using a RAII

View File

@@ -363,7 +363,7 @@ elseif(MSVC)
else()
file(
WRITE ${CMAKE_CURRENT_BINARY_DIR}/main.cpp
"#include <filesystem>\nint main(int argc, char ** argv) {\n std::filesystem::path p(argv[0]);\n return p.string().length();\n}"
"#include <filesystem>\nint main(int /*argc*/, char ** argv) {\n std::filesystem::path p(argv[0]);\n return p.string().length();\n}"
)
try_compile(
STD_FS_NO_LIB_NEEDED ${CMAKE_CURRENT_BINARY_DIR}