Skip to content

Commit

Permalink
qapi: Fix generated code when flat union has member 'kind'
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Markus Armbruster committed Sep 4, 2015
1 parent 5aa05d3 commit 0f61af3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion scripts/qapi-types.py
Expand Up @@ -200,11 +200,12 @@ def generate_union(expr, meta):
ret = mcgen('''
struct %(name)s
{
%(discriminator_type_name)s kind;
%(discriminator_type_name)s %(discriminator)s;
union {
void *data;
''',
name=name,
discriminator=c_name(discriminator or 'kind'),
discriminator_type_name=c_name(discriminator_type_name))

for key in typeinfo:
Expand Down
7 changes: 5 additions & 2 deletions scripts/qapi-visit.py
Expand Up @@ -288,20 +288,23 @@ def generate_visit_union(expr):
name=c_name(name))

if not discriminator:
tag = 'kind'
disc_key = "type"
else:
tag = discriminator
disc_key = discriminator
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) {
goto out_obj;
}
if (!visit_start_union(m, !!(*obj)->data, &err) || err) {
goto out_obj;
}
switch ((*obj)->kind) {
switch ((*obj)->%(c_tag)s) {
''',
disc_type = disc_type,
c_tag=c_name(tag),
disc_key = disc_key)

for key in members:
Expand Down
2 changes: 1 addition & 1 deletion tests/test-qmp-input-visitor.c
Expand Up @@ -313,7 +313,7 @@ static void test_visitor_in_union_flat(TestInputVisitorData *data,

visit_type_UserDefFlatUnion(v, &tmp, NULL, &err);
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");
/* TODO g_assert_cmpint(tmp->integer, ==, 41); */
g_assert_cmpint(tmp->value1->boolean, ==, true);
Expand Down
2 changes: 1 addition & 1 deletion tests/test-qmp-output-visitor.c
Expand Up @@ -437,7 +437,7 @@ static void test_visitor_out_union_flat(TestOutputVisitorData *data,
Error *err = NULL;

UserDefFlatUnion *tmp = g_malloc0(sizeof(UserDefFlatUnion));
tmp->kind = ENUM_ONE_VALUE1;
tmp->enum1 = ENUM_ONE_VALUE1;
tmp->string = g_strdup("str");
tmp->value1 = g_malloc0(sizeof(UserDefA));
/* TODO when generator bug is fixed: tmp->integer = 41; */
Expand Down

0 comments on commit 0f61af3

Please sign in to comment.