qapi: Fix generated code when flat union has member 'kind'

A flat union's tag member gets renamed to 'kind' in the generated
code.  Breaks when another member named 'kind' exists.

Example, adapted from qapi-schema-test.json:

    { 'struct': 'UserDefUnionBase',
      'data': { 'kind': 'str', 'enum1': 'EnumOne' } }

We generate:

    struct UserDefFlatUnion
    {
        EnumOne kind;
        union {
            void *data;
            UserDefA *value1;
            UserDefB *value2;
            UserDefB *value3;
        };
        char *kind;
    };

Kill the silly rename.

Reported-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Markus Armbruster 2015-07-31 10:30:04 +02:00
parent 5aa05d3f72
commit 0f61af3eb3
4 changed files with 9 additions and 5 deletions

View File

@ -200,11 +200,12 @@ def generate_union(expr, meta):
ret = mcgen(''' ret = mcgen('''
struct %(name)s struct %(name)s
{ {
%(discriminator_type_name)s kind; %(discriminator_type_name)s %(discriminator)s;
union { union {
void *data; void *data;
''', ''',
name=name, name=name,
discriminator=c_name(discriminator or 'kind'),
discriminator_type_name=c_name(discriminator_type_name)) discriminator_type_name=c_name(discriminator_type_name))
for key in typeinfo: for key in typeinfo:

View File

@ -288,20 +288,23 @@ void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **e
name=c_name(name)) name=c_name(name))
if not discriminator: if not discriminator:
tag = 'kind'
disc_key = "type" disc_key = "type"
else: else:
tag = discriminator
disc_key = discriminator disc_key = discriminator
ret += mcgen(''' ret += mcgen('''
visit_type_%(disc_type)s(m, &(*obj)->kind, "%(disc_key)s", &err); visit_type_%(disc_type)s(m, &(*obj)->%(c_tag)s, "%(disc_key)s", &err);
if (err) { if (err) {
goto out_obj; goto out_obj;
} }
if (!visit_start_union(m, !!(*obj)->data, &err) || err) { if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
goto out_obj; goto out_obj;
} }
switch ((*obj)->kind) { switch ((*obj)->%(c_tag)s) {
''', ''',
disc_type = disc_type, disc_type = disc_type,
c_tag=c_name(tag),
disc_key = disc_key) disc_key = disc_key)
for key in members: for key in members:

View File

@ -313,7 +313,7 @@ static void test_visitor_in_union_flat(TestInputVisitorData *data,
visit_type_UserDefFlatUnion(v, &tmp, NULL, &err); visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
g_assert(err == NULL); g_assert(err == NULL);
g_assert_cmpint(tmp->kind, ==, ENUM_ONE_VALUE1); g_assert_cmpint(tmp->enum1, ==, ENUM_ONE_VALUE1);
g_assert_cmpstr(tmp->string, ==, "str"); g_assert_cmpstr(tmp->string, ==, "str");
/* TODO g_assert_cmpint(tmp->integer, ==, 41); */ /* TODO g_assert_cmpint(tmp->integer, ==, 41); */
g_assert_cmpint(tmp->value1->boolean, ==, true); g_assert_cmpint(tmp->value1->boolean, ==, true);

View File

@ -437,7 +437,7 @@ static void test_visitor_out_union_flat(TestOutputVisitorData *data,
Error *err = NULL; Error *err = NULL;
UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion)); UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
tmp->kind = ENUM_ONE_VALUE1; tmp->enum1 = ENUM_ONE_VALUE1;
tmp->string = g_strdup("str"); tmp->string = g_strdup("str");
tmp->value1 = g_malloc0(sizeof(UserDefA)); tmp->value1 = g_malloc0(sizeof(UserDefA));
/* TODO when generator bug is fixed: tmp->integer = 41; */ /* TODO when generator bug is fixed: tmp->integer = 41; */