Skip to content

Commit eeb1044

Browse files
tkoeppewjakob
authored andcommitted
[common.h] Mark entry point as "unused".
This change defines a new, portable macro PYBIND11_MAYBE_UNUSED to mark declarations as unused, and annotates the PYBIND11_MODULE entry point with this attribute. The purpose of this annotation is to facilitate dead code detection, which might otherwise consider the module entry point function dead, since it isn't otherwise used. (It is only used via FFI.)
1 parent 1817d21 commit eeb1044

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

include/pybind11/detail/common.h

+13
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@
9292
# define PYBIND11_DEPRECATED(reason) __attribute__((deprecated(reason)))
9393
#endif
9494

95+
#if defined(PYBIND11_CPP17)
96+
# define PYBIND11_MAYBE_UNUSED [[maybe_unused]]
97+
#elif defined(_MSC_VER) && !defined(__clang__)
98+
# define PYBIND11_MAYBE_UNUSED
99+
#else
100+
# define PYBIND11_MAYBE_UNUSED __attribute__ ((__unused__))
101+
#endif
102+
95103
#define PYBIND11_VERSION_MAJOR 2
96104
#define PYBIND11_VERSION_MINOR 5
97105
#define PYBIND11_VERSION_PATCH dev1
@@ -285,6 +293,10 @@ extern "C" {
285293
should not be in quotes. The second macro argument defines a variable of type
286294
`py::module` which can be used to initialize the module.
287295
296+
The entry point is marked as "maybe unused" to aid dead-code detection analysis:
297+
since the entry point is typically only looked up at runtime and not referenced
298+
during translation, it would otherwise appear as unused ("dead") code.
299+
288300
.. code-block:: cpp
289301
290302
PYBIND11_MODULE(example, m) {
@@ -297,6 +309,7 @@ extern "C" {
297309
}
298310
\endrst */
299311
#define PYBIND11_MODULE(name, variable) \
312+
PYBIND11_MAYBE_UNUSED \
300313
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
301314
PYBIND11_PLUGIN_IMPL(name) { \
302315
PYBIND11_CHECK_PYTHON_VERSION \

0 commit comments

Comments
 (0)