Repository: pybind/pybind11 (language: python)

Issue:
[BUG]:  token pasting of ',' and __VA_ARGS__ is a GNU extension / pedantic warnings
### Required prerequisites

- [X] Make sure you've read the [documentation](https://pybind11.readthedocs.io). Your issue may be addressed there.
- [X] Search the [issue tracker](https://github.com/pybind/pybind11/issues) and [Discussions](https:/pybind/pybind11/discussions) to verify that this hasn't already been reported. +1 or comment there if it has.
- [ ] Consider asking first in the [Gitter chat room](https://gitter.im/pybind/Lobby) or in a [Discussion](https:/pybind/pybind11/discussions/new).

### What version (or hash if on master) of pybind11 are you using?

2.13.3

### Problem description

The most recent release uses `, ##__VA_ARGS__` in `common.h`. This is not technically in the standard (see e.g. [here](https://en.cppreference.com/w/cpp/preprocessor/replace#.23_and_.23.23_operators)), but most compilers support it. Clang, however, complains about its use when setting `-Wpedantic`. I can work around this by either not passing `-Wpedantic`, or passing `-Wno-gnu-zero-variadic-macro-arguments`, but I'd rather not do that if it can at all be avoided.

Consider the attached example, `test.cpp`. Compiling it with 
```shell
clang -Wall -Wextra -Wpedantic -Werror -shared -std=c++20 -fPIC $(python -m pybind11 --includes) test.cpp -o test.so
```
produces the following:
```shell
In file included from test.cpp:1:
In file included from .venv/lib/python3.11/site-packages/pybind11/include/pybind11/pybind11.h:13:
In file included from .venv/lib/python3.11/site-packages/pybind11/include/pybind11/detail/class.h:12:
In file included from .venv/lib/python3.11/site-packages/pybind11/include/pybind11/attr.h:13:
.venv/lib/python3.11/site-packages/pybind11/include/pybind11/detail/common.h:493:57: error: token pasting of ',' and __VA_ARGS__ is a GNU extension [-Werror,-Wgnu-zero-variadic-macro-arguments]
            &PYBIND11_CONCAT(pybind11_module_def_, name),
  \
                                                        ^
1 error generated.
```
It concerns the following code snippet:
https://github.com/pybind/pybind11/blob/fc97cc41d575d44fa425a0e56765380e15474399/include/pybind11/detail/common.h#L493-L494

I am not sure if pybind needs to consider this a bug or even an issue. But the previous versions of pybind (2.12 and below) worked without a hitch when setting the `-Wpedantic` option, so I figured I'd let you know about this here.


### Reproducible example code

```text
#include <pybind11/pybind11.h>

int test()
{
    return 42;
}

PYBIND11_MODULE(module, m)
{
    m.def("test", &test);
}
```


### Is this a regression? Put the last known working version here if it is.

2.12.0


Discussion / hints:
`PYBIND11_MODULE(module, m, py::mod_gil_not_used())` would fix it, I think?

We probably should push and pop this warning, then.

Fix patch:
diff --git a/docs/advanced/cast/stl.rst b/docs/advanced/cast/stl.rst
index 03d49b2950..42b85532d8 100644
--- a/docs/advanced/cast/stl.rst
+++ b/docs/advanced/cast/stl.rst
@@ -162,7 +162,7 @@ the declaration
 
 .. code-block:: cpp
 
-    PYBIND11_MAKE_OPAQUE(std::vector<int>);
+    PYBIND11_MAKE_OPAQUE(std::vector<int>)
 
 before any binding code (e.g. invocations to ``class_::def()``, etc.). This
 macro must be specified at the top level (and outside of any namespaces), since
@@ -207,8 +207,8 @@ The following example showcases usage of :file:`pybind11/stl_bind.h`:
     // Don't forget this
     #include <pybind11/stl_bind.h>
 
-    PYBIND11_MAKE_OPAQUE(std::vector<int>);
-    PYBIND11_MAKE_OPAQUE(std::map<std::string, double>);
+    PYBIND11_MAKE_OPAQUE(std::vector<int>)
+    PYBIND11_MAKE_OPAQUE(std::map<std::string, double>)
 
     // ...
 
diff --git a/docs/advanced/smart_ptrs.rst b/docs/advanced/smart_ptrs.rst
index 3c40ce1237..b9f100cf8e 100644
--- a/docs/advanced/smart_ptrs.rst
+++ b/docs/advanced/smart_ptrs.rst
@@ -124,7 +124,7 @@ top namespace level before any binding code:
 
 .. code-block:: cpp
 
-    PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
+    PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>)
 
 The first argument of :func:`PYBIND11_DECLARE_HOLDER_TYPE` should be a
 placeholder name that is used as a template parameter of the second argument.
@@ -136,7 +136,7 @@ by default. Specify
 
 .. code-block:: cpp
 
-    PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>, true);
+    PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>, true)
 
 if ``SmartPtr<T>`` can always be initialized from a ``T*`` pointer without the
 risk of inconsistencies (such as multiple independent ``SmartPtr`` instances
@@ -154,7 +154,7 @@ specialized:
 .. code-block:: cpp
 
     // Always needed for custom holder types
-    PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>);
+    PYBIND11_DECLARE_HOLDER_TYPE(T, SmartPtr<T>)
 
     // Only needed if the type's `.get()` goes by another name
     namespace PYBIND11_NAMESPACE { namespace detail {
diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h
index 643fd33d7e..2a39e88f37 100644
--- a/include/pybind11/detail/common.h
+++ b/include/pybind11/detail/common.h
@@ -479,6 +479,8 @@ PYBIND11_WARNING_POP
         }
 
 \endrst */
+PYBIND11_WARNING_PUSH
+PYBIND11_WARNING_DISABLE_CLANG("-Wgnu-zero-variadic-macro-arguments")
 #define PYBIND11_MODULE(name, variable, ...)                                                      \
     static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name)            \
         PYBIND11_MAYBE_UNUSED;                                                                    \
@@ -499,6 +501,7 @@ PYBIND11_WARNING_POP
         PYBIND11_CATCH_INIT_EXCEPTIONS                                                            \
     }                                                                                             \
     void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ & (variable))
+PYBIND11_WARNING_POP
 
 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
 


Test patch:
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 18308731ab..cd94ef3e53 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -382,6 +382,9 @@ function(pybind11_enable_warnings target_name)
               -Wdeprecated
               -Wundef
               -Wnon-virtual-dtor)
+    if(DEFINED CMAKE_CXX_STANDARD AND NOT CMAKE_CXX_STANDARD VERSION_LESS 20)
+      target_compile_options(${target_name} PRIVATE -Wpedantic)
+    endif()
   endif()
 
   if(PYBIND11_WERROR)
diff --git a/tests/local_bindings.h b/tests/local_bindings.h
index 01d2785353..dea1813108 100644
--- a/tests/local_bindings.h
+++ b/tests/local_bindings.h
@@ -56,13 +56,13 @@ class LocalSimpleException : public std::exception {
     std::string message = "";
 };
 
-PYBIND11_MAKE_OPAQUE(LocalVec);
-PYBIND11_MAKE_OPAQUE(LocalVec2);
-PYBIND11_MAKE_OPAQUE(LocalMap);
-PYBIND11_MAKE_OPAQUE(NonLocalVec);
-// PYBIND11_MAKE_OPAQUE(NonLocalVec2); // same type as LocalVec2
-PYBIND11_MAKE_OPAQUE(NonLocalMap);
-PYBIND11_MAKE_OPAQUE(NonLocalMap2);
+PYBIND11_MAKE_OPAQUE(LocalVec)
+PYBIND11_MAKE_OPAQUE(LocalVec2)
+PYBIND11_MAKE_OPAQUE(LocalMap)
+PYBIND11_MAKE_OPAQUE(NonLocalVec)
+// PYBIND11_MAKE_OPAQUE(NonLocalVec2) // same type as LocalVec2
+PYBIND11_MAKE_OPAQUE(NonLocalMap)
+PYBIND11_MAKE_OPAQUE(NonLocalMap2)
 
 // Simple bindings (used with the above):
 template <typename T, int Adjust = 0, typename... Args>
@@ -70,7 +70,7 @@ py::class_<T> bind_local(Args &&...args) {
     return py::class_<T>(std::forward<Args>(args)...).def(py::init<int>()).def("get", [](T &i) {
         return i.i + Adjust;
     });
-};
+}
 
 // Simulate a foreign library base class (to match the example in the docs):
 namespace pets {
diff --git a/tests/test_callbacks.cpp b/tests/test_callbacks.cpp
index 2fd05dec72..e303e76567 100644
--- a/tests/test_callbacks.cpp
+++ b/tests/test_callbacks.cpp
@@ -148,7 +148,7 @@ TEST_SUBMODULE(callbacks, m) {
     m.def("dummy_function2", [](int i, int j) { return i + j; });
     m.def(
         "roundtrip",
-        [](std::function<int(int)> f, bool expect_none = false) {
+        [](std::function<int(int)> f, bool expect_none) {
             if (expect_none && f) {
                 throw std::runtime_error("Expected None to be converted to empty std::function");
             }
diff --git a/tests/test_eigen_matrix.cpp b/tests/test_eigen_matrix.cpp
index 21261bfc2f..cb8e8c6259 100644
--- a/tests/test_eigen_matrix.cpp
+++ b/tests/test_eigen_matrix.cpp
@@ -55,7 +55,7 @@ void reset_refs() {
 }
 
 // Returns element 2,1 from a matrix (used to test copy/nocopy)
-double get_elem(const Eigen::Ref<const Eigen::MatrixXd> &m) { return m(2, 1); };
+double get_elem(const Eigen::Ref<const Eigen::MatrixXd> &m) { return m(2, 1); }
 
 // Returns a matrix with 10*r + 100*c added to each matrix element (to help test that the matrix
 // reference is referencing rows/columns correctly).
@@ -76,7 +76,7 @@ struct CustomOperatorNew {
     Eigen::Matrix4d a = Eigen::Matrix4d::Zero();
     Eigen::Matrix4d b = Eigen::Matrix4d::Identity();
 
-    EIGEN_MAKE_ALIGNED_OPERATOR_NEW;
+    EIGEN_MAKE_ALIGNED_OPERATOR_NEW
 };
 
 TEST_SUBMODULE(eigen_matrix, m) {
diff --git a/tests/test_opaque_types.cpp b/tests/test_opaque_types.cpp
index 154d0a8d31..da3866cd00 100644
--- a/tests/test_opaque_types.cpp
+++ b/tests/test_opaque_types.cpp
@@ -18,7 +18,7 @@
 // This also deliberately doesn't use the below StringList type alias to test
 // that MAKE_OPAQUE can handle a type containing a `,`.  (The `std::allocator`
 // bit is just the default `std::vector` allocator).
-PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
+PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>)
 
 using StringList = std::vector<std::string, std::allocator<std::string>>;
 
diff --git a/tests/test_sequences_and_iterators.cpp b/tests/test_sequences_and_iterators.cpp
index 4a1d37f4de..c997b73001 100644
--- a/tests/test_sequences_and_iterators.cpp
+++ b/tests/test_sequences_and_iterators.cpp
@@ -86,8 +86,8 @@ class NonCopyableInt {
 };
 using NonCopyableIntPair = std::pair<NonCopyableInt, NonCopyableInt>;
 
-PYBIND11_MAKE_OPAQUE(std::vector<NonCopyableInt>);
-PYBIND11_MAKE_OPAQUE(std::vector<NonCopyableIntPair>);
+PYBIND11_MAKE_OPAQUE(std::vector<NonCopyableInt>)
+PYBIND11_MAKE_OPAQUE(std::vector<NonCopyableIntPair>)
 
 template <typename PythonType>
 py::list test_random_access_iterator(PythonType x) {
diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp
index 496073b3c1..4ab43953f1 100644
--- a/tests/test_smart_ptr.cpp
+++ b/tests/test_smart_ptr.cpp
@@ -11,6 +11,9 @@
 #include "object.h"
 #include "pybind11_tests.h"
 
+// This breaks on PYBIND11_DECLARE_HOLDER_TYPE
+PYBIND11_WARNING_DISABLE_GCC("-Wpedantic")
+
 namespace {
 
 // This is just a wrapper around unique_ptr, but with extra fields to deliberately bloat up the
@@ -279,13 +282,13 @@ struct holder_helper<ref<T>> {
 } // namespace PYBIND11_NAMESPACE
 
 // Make pybind aware of the ref-counted wrapper type (s):
-PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>, true);
+PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>, true)
 // The following is not required anymore for std::shared_ptr, but it should compile without error:
-PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
-PYBIND11_DECLARE_HOLDER_TYPE(T, huge_unique_ptr<T>);
-PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>);
-PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr_with_addressof_operator<T>);
-PYBIND11_DECLARE_HOLDER_TYPE(T, unique_ptr_with_addressof_operator<T>);
+PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>)
+PYBIND11_DECLARE_HOLDER_TYPE(T, huge_unique_ptr<T>)
+PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>)
+PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr_with_addressof_operator<T>)
+PYBIND11_DECLARE_HOLDER_TYPE(T, unique_ptr_with_addressof_operator<T>)
 
 TEST_SUBMODULE(smart_ptr, m) {
     // Please do not interleave `struct` and `class` definitions with bindings code,
diff --git a/tests/test_stl.cpp b/tests/test_stl.cpp
index 6240524a09..dd93d51d0a 100644
--- a/tests/test_stl.cpp
+++ b/tests/test_stl.cpp
@@ -59,7 +59,7 @@ struct visit_helper<boost::variant> {
 } // namespace PYBIND11_NAMESPACE
 #endif
 
-PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>);
+PYBIND11_MAKE_OPAQUE(std::vector<std::string, std::allocator<std::string>>)
 
 /// Issue #528: templated constructor
 struct TplCtorClass {
diff --git a/tests/test_tagbased_polymorphic.cpp b/tests/test_tagbased_polymorphic.cpp
index 24b49021b4..13e5ed3198 100644
--- a/tests/test_tagbased_polymorphic.cpp
+++ b/tests/test_tagbased_polymorphic.cpp
@@ -145,4 +145,4 @@ TEST_SUBMODULE(tagbased_polymorphic, m) {
         .def(py::init<std::string>())
         .def("purr", &Panther::purr);
     m.def("create_zoo", &create_zoo);
-};
+}
diff --git a/tests/test_virtual_functions.cpp b/tests/test_virtual_functions.cpp
index 93b136ad3c..a6164eb81d 100644
--- a/tests/test_virtual_functions.cpp
+++ b/tests/test_virtual_functions.cpp
@@ -589,4 +589,4 @@ void initialize_inherited_virtuals(py::module_ &m) {
     // Fix issue #1454 (crash when acquiring/releasing GIL on another thread in Python 2.7)
     m.def("test_gil", &test_gil);
     m.def("test_gil_from_thread", &test_gil_from_thread);
-};
+}