David Howells
2018-Sep-05 15:54 UTC
[RFC] UAPI: Check headers by compiling all together as C++
Here's a set of patches that inserts a step into the build process to make sure that the UAPI headers can all be built together with C++ (if the compiler being used supports C++). All but the final patch perform fixups, including: (1) Fix member names that conflict with C++ reserved words by providing alternates that can be used anywhere. An anonymous union is used so that that the conflicting name is still available outside of C++. (2) Fix the use of flexible arrays in structs that get embedded (which is illegal in C++). (3) Remove the use of internal kernel structs in UAPI structures. (4) Fix symbol collisions. (5) Replace usage of u32 and co. with __u32 and co. (6) Fix use of sparsely initialised arrays (which g++ doesn't implement). (7) Remove some use of PAGE_SIZE since this isn't valid outside of the kernel. And lastly: (8) Compile all of the UAPI headers (with a few exceptions) together as C++ to catch new errors occurring as part of the regular build process. The patches can also be found here: http://git.kernel.org/cgit/linux/kernel/git/dhowells/linux-fs.git/log/?h=uapi-check Thanks, David --- David Howells (11): UAPI: drm: Fix use of C++ keywords as structural members UAPI: keys: Fix use of C++ keywords as structural members UAPI: virtio_net: Fix use of C++ keywords as structural members UAPI: bcache: Fix use of embedded flexible array UAPI: coda: Don't use internal kernel structs in UAPI UAPI: netfilter: Fix symbol collision issues UAPI: nilfs2: Fix use of undefined byteswapping functions UAPI: sound: Fix use of u32 and co. in UAPI headers UAPI: ndctl: Fix g++-unsupported initialisation in headers UAPI: ndctl: Remove use of PAGE_SIZE UAPI: Check headers build for C++ Makefile | 1 include/linux/ndctl.h | 22 ++++ include/uapi/drm/i810_drm.h | 7 + include/uapi/drm/msm_drm.h | 7 + include/uapi/linux/bcache.h | 2 include/uapi/linux/coda_psdev.h | 4 + include/uapi/linux/keyctl.h | 7 + include/uapi/linux/ndctl.h | 20 ++- include/uapi/linux/netfilter/nfnetlink_cthelper.h | 2 include/uapi/linux/netfilter_ipv4/ipt_ECN.h | 9 -- include/uapi/linux/nilfs2_ondisk.h | 21 ++-- include/uapi/linux/virtio_net.h | 7 + include/uapi/sound/skl-tplg-interface.h | 106 +++++++++--------- scripts/headers-c++.sh | 124 +++++++++++++++++++++ 14 files changed, 255 insertions(+), 84 deletions(-) create mode 100644 include/linux/ndctl.h create mode 100755 scripts/headers-c++.sh
David Howells
2018-Sep-05 15:54 UTC
[PATCH 03/11] UAPI: virtio_net: Fix use of C++ keywords as structural members
The virtio_net_ctrl_hdr struct uses a C++ keyword as structural members. Fix this by inserting an anonymous union that provides an alternative name and then hide the reserved name in C++. Signed-off-by: David Howells <dhowells at redhat.com> cc: "Michael S. Tsirkin" <mst at redhat.com> cc: Jason Wang <jasowang at redhat.com> cc: virtualization at lists.linux-foundation.org --- include/uapi/linux/virtio_net.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/virtio_net.h b/include/uapi/linux/virtio_net.h index a3715a3224c1..967142bc0e05 100644 --- a/include/uapi/linux/virtio_net.h +++ b/include/uapi/linux/virtio_net.h @@ -150,7 +150,12 @@ struct virtio_net_hdr_mrg_rxbuf { * command goes in between. */ struct virtio_net_ctrl_hdr { - __u8 class; + union { +#ifndef __cplusplus + __u8 class; +#endif + __u8 _class; + }; __u8 cmd; } __attribute__((packed));
Greg KH
2018-Sep-05 16:54 UTC
[PATCH 03/11] UAPI: virtio_net: Fix use of C++ keywords as structural members
On Wed, Sep 05, 2018 at 04:54:55PM +0100, David Howells wrote:> The virtio_net_ctrl_hdr struct uses a C++ keyword as structural members. Fix > this by inserting an anonymous union that provides an alternative name and > then hide the reserved name in C++. > > Signed-off-by: David Howells <dhowells at redhat.com> > cc: "Michael S. Tsirkin" <mst at redhat.com> > cc: Jason Wang <jasowang at redhat.com> > cc: virtualization at lists.linux-foundation.org > --- > > include/uapi/linux/virtio_net.h | 7 ++++++- > 1 file changed, 6 insertions(+), 1 deletion(-) > > diff --git a/include/uapi/linux/virtio_net.h b/include/uapi/linux/virtio_net.h > index a3715a3224c1..967142bc0e05 100644 > --- a/include/uapi/linux/virtio_net.h > +++ b/include/uapi/linux/virtio_net.h > @@ -150,7 +150,12 @@ struct virtio_net_hdr_mrg_rxbuf { > * command goes in between. > */ > struct virtio_net_ctrl_hdr { > - __u8 class; > + union { > +#ifndef __cplusplus > + __u8 class; > +#endif > + __u8 _class; > + };Ugh, ick, no! Come on now, either put the whole C namespace stuff around the file, or don't care about this at all. Doing this whack-a-mole style is a mess. "class" is a fine variable name for C code, there's no reason this has to change here at all. greg k-h
On Wed, Sep 05, 2018 at 04:54:27PM +0100, David Howells wrote:> > Here's a set of patches that inserts a step into the build process to make > sure that the UAPI headers can all be built together with C++ (if the > compiler being used supports C++). All but the final patch perform fixups, > including:Wait, why do we care? What has recently changed to start to directly import kernel uapi files into C++ code? And if userspace wants to do this, can't they do the C namespace trick themselves when they do the import? That must be how they are doing it today, right? thanks, greg k-h
David Howells
2018-Sep-05 17:15 UTC
[PATCH 03/11] UAPI: virtio_net: Fix use of C++ keywords as structural members
Greg KH <gregkh at linuxfoundation.org> wrote:> Come on now, either put the whole C namespace stuff around the file,You mean wrap it with 'extern "C" { ... }'? That doesn't fix it. That only affects the symbols generated by the compiler.> "class" is a fine variable name for C code, there's no reason this has > to change here at all.I'm trying to prevent future accidents like the one in linux/keyctl.h. The easiest way to do this[**] is to pass the entire set of UAPI headers[*] through the compiler together. Besides I still have my dark plan to C++-ise the kernel[***] :-D David [*] with some obvious exceptions [**] and it catches other errors too [***] https://lkml.org/lkml/2018/4/1/116
Michael S. Tsirkin
2018-Sep-05 17:35 UTC
[PATCH 03/11] UAPI: virtio_net: Fix use of C++ keywords as structural members
On Wed, Sep 05, 2018 at 04:54:55PM +0100, David Howells wrote:> The virtio_net_ctrl_hdr struct uses a C++ keyword as structural members. Fix > this by inserting an anonymous union that provides an alternative name and > then hide the reserved name in C++. > > Signed-off-by: David Howells <dhowells at redhat.com> > cc: "Michael S. Tsirkin" <mst at redhat.com> > cc: Jason Wang <jasowang at redhat.com> > cc: virtualization at lists.linux-foundation.org > --- > > include/uapi/linux/virtio_net.h | 7 ++++++- > 1 file changed, 6 insertions(+), 1 deletion(-) > > diff --git a/include/uapi/linux/virtio_net.h b/include/uapi/linux/virtio_net.h > index a3715a3224c1..967142bc0e05 100644 > --- a/include/uapi/linux/virtio_net.h > +++ b/include/uapi/linux/virtio_net.h > @@ -150,7 +150,12 @@ struct virtio_net_hdr_mrg_rxbuf { > * command goes in between. > */ > struct virtio_net_ctrl_hdr { > - __u8 class; > + union { > +#ifndef __cplusplus > + __u8 class; > +#endif > + __u8 _class; > + }; > __u8 cmd; > } __attribute__((packed));As long as you do not intend to use any classes, how about simply adding -Dclass=_class to your command line? Seems to work fine with gcc 8.1.1 on Fedora. -- MST
Michael S. Tsirkin
2018-Sep-05 17:42 UTC
[RFC] UAPI: Check headers by compiling all together as C++
On Wed, Sep 05, 2018 at 07:33:38PM +0200, Yann Droneaud wrote:> Hi, > > Le mercredi 05 septembre 2018 ? 18:55 +0200, Greg KH a ?crit : > > On Wed, Sep 05, 2018 at 04:54:27PM +0100, David Howells wrote: > > > > > > Here's a set of patches that inserts a step into the build process to make > > > sure that the UAPI headers can all be built together with C++ (if the > > > compiler being used supports C++). All but the final patch perform fixups, > > > including: > > > > Wait, why do we care? What has recently changed to start to directly > > import kernel uapi files into C++ code? > > > > And if userspace wants to do this, can't they do the C namespace trick > > themselves when they do the import? That must be how they are doing it > > today, right? > > > > They can't. > > > Adding extern "C" { } doesn't magically make "class" a non keyword. > Even if it was the case, writing C++ code using whatever->class would > probably broke because class is a keyword in C++.I think it's a bug in the language TBH.> -- > Yann Droneaud > OPTEYA >
David Howells
2018-Sep-05 17:50 UTC
[RFC] UAPI: Check headers by compiling all together as C++
Greg KH <greg at kroah.com> wrote:> > Here's a set of patches that inserts a step into the build process to make > > sure that the UAPI headers can all be built together with C++ (if the > > compiler being used supports C++). All but the final patch perform fixups, > > including: > > Wait, why do we care? What has recently changed to start to directly > import kernel uapi files into C++ code?There's at least one outstanding bug due to a C++ identifier in the kernel UAPI headers. Are you saying you explicitly don't want people to be able to use the kernel UAPI headers in C++?> And if userspace wants to do this, can't they do the C namespace trick > themselves when they do the import? That must be how they are doing it > today, right?No, because there's no such trick (except with the preprocessor). David
Jan Engelhardt
2018-Sep-05 19:22 UTC
[RFC] UAPI: Check headers by compiling all together as C++
On Wednesday 2018-09-05 18:55, Greg KH wrote:>On Wed, Sep 05, 2018 at 04:54:27PM +0100, David Howells wrote: >> >> Here's a set of patches that inserts a step into the build process to make >> sure that the UAPI headers can all be built together with C++ (if the >> compiler being used supports C++). All but the final patch perform fixups, >> including: > >Wait, why do we care? What has recently changed to start to directly >import kernel uapi files into C++ code?With C++11, C++ has become a much nicer language to use (for userspace, anyway).>And if userspace wants to do this, can't they do the C namespace trick >themselves when they do the import?The only trick is to use an extra C source file and extensively wrap things.
David Howells
2018-Sep-06 07:09 UTC
[PATCH 03/11] UAPI: virtio_net: Fix use of C++ keywords as structural members
Michael S. Tsirkin <mst at redhat.com> wrote:> As long as you do not intend to use any classes, how about > simply adding > > -Dclass=_class > > to your command line?That kind of misses the point;-). It's not reasonable to expect all userspace C++ users to do this. David
Possibly Parallel Threads
- [PATCH 03/11] UAPI: virtio_net: Fix use of C++ keywords as structural members
- [PATCH 03/11] UAPI: virtio_net: Fix use of C++ keywords as structural members [ver #2]
- [RFC] UAPI: Check headers by compiling all together as C++
- [RFC] UAPI: Check headers by compiling all together as C++
- [RFC] UAPI: Check headers by compiling all together as C++