* [Qemu-devel] [PATCH] scripts/qapi-types.py: Add dummy member to empty structs
@ 2014-12-23 18:24 Peter Maydell
2014-12-23 20:04 ` Eric Blake
0 siblings, 1 reply; 2+ messages in thread
From: Peter Maydell @ 2014-12-23 18:24 UTC (permalink / raw)
To: qemu-devel; +Cc: Markus Armbruster, Luiz Capitulino
Make sure that all generated C structs have at least one field; this
avoids potential issues with attempting to malloc space for
zero-length structs in C (g_malloc(sizeof struct) would return NULL).
It also avoids an incompatibility with C++ (where an empty struct is
size 1); that isn't important to us now but might be in future.
Generated empty structures look like this:
struct Abort
{
char qapi_dummy_field_for_empty_struct;
};
This silences clang warnings like:
./qapi-types.h:3752:1: warning: empty struct has size 0 in C, size 1 in C++ [-Wextern-c-compat]
struct Abort
^
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
scripts/qapi-types.py | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index d2f815b..1eb272d 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -99,6 +99,14 @@ struct %(name)s
ret += generate_struct_fields(members)
+ # Make sure that all structs have at least one field; this avoids
+ # potential issues with attempting to malloc space for zero-length structs
+ # in C, and also incompatibility with C++ (where an empty struct is size 1).
+ if not base and not members:
+ ret += mcgen('''
+ char qapi_dummy_field_for_empty_struct;
+''')
+
if len(fieldname):
fieldname = " " + fieldname
ret += mcgen('''
--
2.2.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [Qemu-devel] [PATCH] scripts/qapi-types.py: Add dummy member to empty structs
2014-12-23 18:24 [Qemu-devel] [PATCH] scripts/qapi-types.py: Add dummy member to empty structs Peter Maydell
@ 2014-12-23 20:04 ` Eric Blake
0 siblings, 0 replies; 2+ messages in thread
From: Eric Blake @ 2014-12-23 20:04 UTC (permalink / raw)
To: Peter Maydell, qemu-devel; +Cc: Markus Armbruster, Luiz Capitulino
[-- Attachment #1: Type: text/plain, Size: 1223 bytes --]
On 12/23/2014 11:24 AM, Peter Maydell wrote:
> Make sure that all generated C structs have at least one field; this
> avoids potential issues with attempting to malloc space for
> zero-length structs in C (g_malloc(sizeof struct) would return NULL).
> It also avoids an incompatibility with C++ (where an empty struct is
> size 1); that isn't important to us now but might be in future.
>
> Generated empty structures look like this:
> struct Abort
> {
> char qapi_dummy_field_for_empty_struct;
> };
>
> This silences clang warnings like:
> ./qapi-types.h:3752:1: warning: empty struct has size 0 in C, size 1 in C++ [-Wextern-c-compat]
> struct Abort
> ^
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> scripts/qapi-types.py | 8 ++++++++
> 1 file changed, 8 insertions(+)
Reviewed-by: Eric Blake <eblake@redhat.com>
However, I'm a bit surprised that there is no testsuite coverage for
this; it would be nice if we had at least one test that used an empty
struct to prove that we continue to generate them and their visitors
correctly.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2014-12-23 20:04 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-12-23 18:24 [Qemu-devel] [PATCH] scripts/qapi-types.py: Add dummy member to empty structs Peter Maydell
2014-12-23 20:04 ` Eric Blake
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).