Skip to content

Commit c7f3460

Browse files
authored
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
1 parent d3fee42 commit c7f3460

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

.github/workflows/ci.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ jobs:
199199
run: |
200200
uv pip install --python=python --system setuptools
201201
pytest tests/extra_setuptools
202-
if: "!(matrix.runs-on == 'windows-2022')"
202+
if: matrix.runs-on != 'windows-2022'
203203

204204
manylinux:
205205
name: Manylinux on 🐍 3.13t • GIL
@@ -330,9 +330,10 @@ jobs:
330330
container_suffix: "-bullseye"
331331
- clang: 18
332332
std: 20
333+
cxx_flags: "-Werror -Wall -Wextra -Wwrite-strings -Wunreachable-code -Wpointer-arith -Wredundant-decls"
333334
container_suffix: "-bookworm"
334335

335-
name: "🐍 3 • Clang ${{ matrix.clang }} • C++${{ matrix.std }} • x64"
336+
name: "🐍 3 • Clang ${{ matrix.clang }} • C++${{ matrix.std }} • x64${{ matrix.cxx_flags && ' • cxx_flags' || '' }}"
336337
container: "silkeh/clang:${{ matrix.clang }}${{ matrix.container_suffix }}"
337338

338339
steps:
@@ -348,6 +349,7 @@ jobs:
348349
-DPYBIND11_WERROR=ON
349350
-DDOWNLOAD_CATCH=ON
350351
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
352+
-DCMAKE_CXX_FLAGS="${{ matrix.cxx_flags }}"
351353
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
352354
353355
- name: Build
@@ -491,9 +493,9 @@ jobs:
491493
- { gcc: 9, std: 20 }
492494
- { gcc: 10, std: 17 }
493495
- { gcc: 10, std: 20 }
494-
- { gcc: 13, std: 20 }
496+
- { gcc: 13, std: 20, cxx_flags: "-Wall -Wextra -Wwrite-strings -Wunreachable-code -Wpointer-arith -Wredundant-decls" }
495497

496-
name: "🐍 3 • GCC ${{ matrix.gcc }} • C++${{ matrix.std }}• x64"
498+
name: "🐍 3 • GCC ${{ matrix.gcc }} • C++${{ matrix.std }} • x64${{ matrix.cxx_flags && ' • cxx_flags' || '' }}"
497499
container: "gcc:${{ matrix.gcc }}"
498500

499501
steps:
@@ -515,6 +517,7 @@ jobs:
515517
-DPYBIND11_WERROR=ON
516518
-DDOWNLOAD_CATCH=ON
517519
-DCMAKE_CXX_STANDARD=${{ matrix.std }}
520+
-DCMAKE_CXX_FLAGS="${{ matrix.cxx_flags }}"
518521
-DPYTHON_EXECUTABLE=$(python3 -c "import sys; print(sys.executable)")
519522
520523
- name: Build

include/pybind11/detail/class.h

+5
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,13 @@ extern "C" inline void pybind11_object_dealloc(PyObject *self) {
499499
Py_DECREF(type);
500500
}
501501

502+
PYBIND11_WARNING_PUSH
503+
PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
504+
502505
std::string error_string();
503506

507+
PYBIND11_WARNING_POP
508+
504509
/** Create the type which can be used as a common base for all classes. This is
505510
needed in order to satisfy Python's requirements for multiple inheritance.
506511
Return value: New reference. */

include/pybind11/detail/function_record_pyobject.h

-3
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ inline int tp_init_impl(PyObject *, PyObject *, PyObject *) {
173173
// return -1; // Unreachable.
174174
}
175175

176-
// The implementation needs the definition of `class cpp_function`.
177-
void tp_dealloc_impl(PyObject *self);
178-
179176
inline void tp_free_impl(void *) {
180177
pybind11_fail("UNEXPECTED CALL OF function_record_PyTypeObject_methods::tp_free_impl");
181178
}

include/pybind11/detail/type_caster_base.h

+5
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,14 @@ inline PyThreadState *get_thread_state_unchecked() {
511511
void keep_alive_impl(handle nurse, handle patient);
512512
inline PyObject *make_new_instance(PyTypeObject *type);
513513

514+
PYBIND11_WARNING_PUSH
515+
PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
516+
514517
// PYBIND11:REMINDER: Needs refactoring of existing pybind11 code.
515518
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo);
516519

520+
PYBIND11_WARNING_POP
521+
517522
PYBIND11_NAMESPACE_BEGIN(smart_holder_type_caster_support)
518523

519524
struct value_and_holder_helper {

include/pybind11/gil.h

+5
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
3232

3333
PYBIND11_NAMESPACE_BEGIN(detail)
3434

35+
PYBIND11_WARNING_PUSH
36+
PYBIND11_WARNING_DISABLE_GCC("-Wredundant-decls")
37+
3538
// forward declarations
3639
PyThreadState *get_thread_state_unchecked();
3740

41+
PYBIND11_WARNING_POP
42+
3843
PYBIND11_NAMESPACE_END(detail)
3944

4045
/* The functions below essentially reproduce the PyGILState_* API using a RAII

tests/CMakeLists.txt

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

0 commit comments

Comments
 (0)