Skip to content

"anonymous types declared in an anonymous union are an extension [-Wnested-anon-types]" #1204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
anntzer opened this issue Dec 4, 2017 · 6 comments

Comments

@anntzer
Copy link
Contributor

anntzer commented Dec 4, 2017

Issue description

Compiling a file that includes pybind11/detail/common.h (it is private but obviously included by other public headers) with clang -Wpedantic triggers the warning

In file included from src/main.cpp:1:
In file included from /usr/include/python3.6m/pybind11/pybind11.h:43:
In file included from /usr/include/python3.6m/pybind11/attr.h:13:
In file included from /usr/include/python3.6m/pybind11/cast.h:13:
In file included from /usr/include/python3.6m/pybind11/pytypes.h:12:
/usr/include/python3.6m/pybind11/detail/common.h:386:9: warning: anonymous types declared in an anonymous union are an extension [-Wnested-anon-types]
        struct 

Reproducible example code

Add #include <pybind11/detail/common.h> to python_example and compile with

CC=clang CFLAGS=-Wpedantic CXX=clang setup.py build_ext
@Psirus
Copy link

Psirus commented Jan 11, 2018

I've run into this as well. Since I'm using the provided CMake macros (thank you for this, btw), I would be fine with a solution like this:

diff --git a/tools/pybind11Tools.cmake b/tools/pybind11Tools.cmake
index a7c471a..b8bfb1c 100644
--- a/tools/pybind11Tools.cmake
+++ b/tools/pybind11Tools.cmake
@@ -130,7 +130,7 @@ function(pybind11_add_module target_name)
 
   add_library(${target_name} ${lib_type} ${exclude_from_all} ${ARG_UNPARSED_ARGUMENTS})
 
-  target_include_directories(${target_name}
+  target_include_directories(${target_name} SYSTEM
     PRIVATE ${PYBIND11_INCLUDE_DIR}  # from project CMakeLists.txt
     PRIVATE ${pybind11_INCLUDE_DIR}  # from pybind11Config
     PRIVATE ${PYTHON_INCLUDE_DIRS})

@wjakob
Copy link
Member

wjakob commented Jan 11, 2018

I'm not a big fan of this approach -- it would be better to suppress this warning as is already done for others. The include files should be warning-free without any special compiler flags.

@Psirus
Copy link

Psirus commented Jan 11, 2018

Was just a suggestion in case getting the headers warning free was too intrusive. Obviously I'd prefer a solution that works for everyone as well.

@Psirus
Copy link

Psirus commented Jan 11, 2018

So rather

diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h
index 82f8a48..ad152f3 100644
--- a/include/pybind11/pybind11.h
+++ b/include/pybind11/pybind11.h
@@ -38,6 +38,9 @@
 #  if __GNUC__ >= 7
 #    pragma GCC diagnostic ignored "-Wnoexcept-type"
 #  endif
+#elif defined(__clang__)
+#  pragma clang diagnostic push
+#  pragma clang diagnostic ignored "-Wnested-anon-types"
 #endif
 
 #include "attr.h"
@@ -1979,4 +1982,6 @@ NAMESPACE_END(PYBIND11_NAMESPACE)
 /* Leave ignored warnings on */
 #elif defined(__GNUG__) && !defined(__clang__)
 #  pragma GCC diagnostic pop
+#elif defined(__clang__)
+#  pragma clang diagnostic pop
 #endif

Should I open a PR for that?

@wjakob
Copy link
Member

wjakob commented Jan 11, 2018

Thank you -- yes, please do.

jagerman added a commit to jagerman/pybind11 that referenced this issue Jan 11, 2018
The anonymous struct nested in a union triggers a -Wnested-anon-type
warning ("anonymous types declared in an anonymous union are an
extension") under clang (pybind#1204).  This names the struct and defines it
out of the definition of `instance` to get around to warning (and makes
the code slightly simpler).
jagerman added a commit that referenced this issue Jan 11, 2018
The anonymous struct nested in a union triggers a -Wnested-anon-type
warning ("anonymous types declared in an anonymous union are an
extension") under clang (#1204).  This names the struct and defines it
out of the definition of `instance` to get around to warning (and makes
the code slightly simpler).
@jagerman
Copy link
Member

Fixed by #1248

jagerman added a commit to jagerman/pybind11 that referenced this issue Jan 12, 2018
The anonymous struct nested in a union triggers a -Wnested-anon-type
warning ("anonymous types declared in an anonymous union are an
extension") under clang (pybind#1204).  This names the struct and defines it
out of the definition of `instance` to get around to warning (and makes
the code slightly simpler).
wjakob pushed a commit that referenced this issue Feb 7, 2018
The anonymous struct nested in a union triggers a -Wnested-anon-type
warning ("anonymous types declared in an anonymous union are an
extension") under clang (#1204).  This names the struct and defines it
out of the definition of `instance` to get around to warning (and makes
the code slightly simpler).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants