diff mbox

[07/26] qapi: Fix generated code when flat union has member 'kind'

Message ID 1438679896-5077-8-git-send-email-armbru@redhat.com
State New
Headers show

Commit Message

Markus Armbruster Aug. 4, 2015, 9:17 a.m. UTC
A flat union's tag member gets renamed to 'kind' in the generated
code.  Breaks when another member is 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>
---
 scripts/qapi-types.py           | 3 ++-
 scripts/qapi-visit.py           | 7 +++++--
 tests/test-qmp-input-visitor.c  | 2 +-
 tests/test-qmp-output-visitor.c | 2 +-
 4 files changed, 9 insertions(+), 5 deletions(-)

Comments

Eric Blake Aug. 4, 2015, 4:17 p.m. UTC | #1
On 08/04/2015 03:17 AM, Markus Armbruster wrote:
> A flat union's tag member gets renamed to 'kind' in the generated
> code.  Breaks when another member is named 'kind' exists.

Too many verbs. Drop either 'is' or '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>
> ---
>  scripts/qapi-types.py           | 3 ++-
>  scripts/qapi-visit.py           | 7 +++++--
>  tests/test-qmp-input-visitor.c  | 2 +-
>  tests/test-qmp-output-visitor.c | 2 +-
>  4 files changed, 9 insertions(+), 5 deletions(-)

Reviewed-by: Eric Blake <eblake@redhat.com>
Markus Armbruster Aug. 5, 2015, 5:24 a.m. UTC | #2
Eric Blake <eblake@redhat.com> writes:

> On 08/04/2015 03:17 AM, Markus Armbruster wrote:
>> A flat union's tag member gets renamed to 'kind' in the generated
>> code.  Breaks when another member is named 'kind' exists.
>
> Too many verbs. Drop either 'is' or 'exists'.

Will fix.

>> 
>> 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>
>> ---
>>  scripts/qapi-types.py           | 3 ++-
>>  scripts/qapi-visit.py           | 7 +++++--
>>  tests/test-qmp-input-visitor.c  | 2 +-
>>  tests/test-qmp-output-visitor.c | 2 +-
>>  4 files changed, 9 insertions(+), 5 deletions(-)
>
> Reviewed-by: Eric Blake <eblake@redhat.com>

Thanks!
diff mbox

Patch

diff --git a/scripts/qapi-types.py b/scripts/qapi-types.py
index 4902440..ac8dad3 100644
--- a/scripts/qapi-types.py
+++ b/scripts/qapi-types.py
@@ -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:
diff --git a/scripts/qapi-visit.py b/scripts/qapi-visit.py
index e8ee268..4ec79a6 100644
--- a/scripts/qapi-visit.py
+++ b/scripts/qapi-visit.py
@@ -288,20 +288,23 @@  void visit_type_%(name)s(Visitor *m, %(name)s **obj, const char *name, Error **e
                      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:
diff --git a/tests/test-qmp-input-visitor.c b/tests/test-qmp-input-visitor.c
index b961953..b7a87ee 100644
--- a/tests/test-qmp-input-visitor.c
+++ b/tests/test-qmp-input-visitor.c
@@ -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);
diff --git a/tests/test-qmp-output-visitor.c b/tests/test-qmp-output-visitor.c
index 87ba350..338ada0 100644
--- a/tests/test-qmp-output-visitor.c
+++ b/tests/test-qmp-output-visitor.c
@@ -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; */