All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PULL 14/31] qapi: Improve generated event use of qapi visitor
Date: Tue,  9 Feb 2016 12:37:46 +0100	[thread overview]
Message-ID: <1455017883-25867-15-git-send-email-armbru@redhat.com> (raw)
In-Reply-To: <1455017883-25867-1-git-send-email-armbru@redhat.com>

From: Eric Blake <eblake@redhat.com>

All other successful clients of visit_start_struct() were paired
with an unconditional visit_end_struct(); but the generated
code for events was relying on qmp_output_visitor_cleanup() to
work on an incomplete visit.  Alter the code to guarantee that
the struct is completed, which will make a future patch to
split visit_end_struct() easier to reason about.  While at it,
drop some assertions and comments that are not present in other
uses of the qmp output visitor, and pass NULL rather than "" as
the 'kind' parameter (matching most other uses where obj is NULL).

The changes to the generated code look like:

|     qmp = qmp_event_build_dict("DEVICE_TRAY_MOVED");
|
|     qov = qmp_output_visitor_new();
|-    g_assert(qov);
|-
|     v = qmp_output_get_visitor(qov);
|-    g_assert(v);
|
|-    /* Fake visit, as if all members are under a structure */
|-    visit_start_struct(v, NULL, "", "DEVICE_TRAY_MOVED", 0, &err);
|+    visit_start_struct(v, NULL, NULL, "DEVICE_TRAY_MOVED", 0, &err);
|     if (err) {
|         goto out;
|     }
|     visit_type_str(v, (char **)&device, "device", &err);
|     if (err) {
|-        goto out;
|+        goto out_obj;
|     }
|     visit_type_bool(v, &tray_open, "tray-open", &err);
|     if (err) {
|-        goto out;
|+        goto out_obj;
|     }
|-    visit_end_struct(v, &err);
|+out_obj:
|+    visit_end_struct(v, err ? NULL : &err);
|     if (err) {
|         goto out;
|     }
|
|     obj = qmp_output_get_qobject(qov);
|-    g_assert(obj != NULL);
|+    g_assert(obj);
|
|     qdict_put_obj(qmp, "data", obj);
|     emit(QAPI_EVENT_DEVICE_TRAY_MOVED, qmp, &err);

Note that the 'goto out_obj' with no intervening code before the
label, as well as the construct of 'err ? NULL : &err', are both
a bit unusual but also temporary; they get fixed in a later patch
that splits visit_end_struct() to drop its errp parameter by moving
some checking before the label.  But until that time, this was the
simplest way to avoid the appearance of passing a possibly-set
error to visit_end_struct(), even though actual code inspection
shows that visit_end_struct() for a QMP output visitor will never
set an error.

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1454075341-13658-11-git-send-email-eblake@redhat.com>
[Commit message's code diff tweaked]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 scripts/qapi-event.py | 16 +++++++---------
 scripts/qapi.py       |  5 +++--
 2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/scripts/qapi-event.py b/scripts/qapi-event.py
index 720486f..0f5534f 100644
--- a/scripts/qapi-event.py
+++ b/scripts/qapi-event.py
@@ -2,7 +2,7 @@
 # QAPI event generator
 #
 # Copyright (c) 2014 Wenchao Xia
-# Copyright (c) 2015 Red Hat Inc.
+# Copyright (c) 2015-2016 Red Hat Inc.
 #
 # Authors:
 #  Wenchao Xia <wenchaoqemu@gmail.com>
@@ -61,25 +61,23 @@ def gen_event_send(name, arg_type):
     if arg_type and arg_type.members:
         ret += mcgen('''
     qov = qmp_output_visitor_new();
-    g_assert(qov);
-
     v = qmp_output_get_visitor(qov);
-    g_assert(v);
 
-    /* Fake visit, as if all members are under a structure */
-    visit_start_struct(v, NULL, "", "%(name)s", 0, &err);
+    visit_start_struct(v, NULL, NULL, "%(name)s", 0, &err);
 ''',
                      name=name)
         ret += gen_err_check()
-        ret += gen_visit_fields(arg_type.members, need_cast=True)
+        ret += gen_visit_fields(arg_type.members, need_cast=True,
+                                label='out_obj')
         ret += mcgen('''
-    visit_end_struct(v, &err);
+out_obj:
+    visit_end_struct(v, err ? NULL : &err);
     if (err) {
         goto out;
     }
 
     obj = qmp_output_get_qobject(qov);
-    g_assert(obj != NULL);
+    g_assert(obj);
 
     qdict_put_obj(qmp, "data", obj);
 ''')
diff --git a/scripts/qapi.py b/scripts/qapi.py
index 0f032c3..9254e48 100644
--- a/scripts/qapi.py
+++ b/scripts/qapi.py
@@ -1636,7 +1636,8 @@ def gen_err_check(label='out', skiperr=False):
                  label=label)
 
 
-def gen_visit_fields(members, prefix='', need_cast=False, skiperr=False):
+def gen_visit_fields(members, prefix='', need_cast=False, skiperr=False,
+                     label='out'):
     ret = ''
     if skiperr:
         errparg = 'NULL'
@@ -1664,7 +1665,7 @@ def gen_visit_fields(members, prefix='', need_cast=False, skiperr=False):
                      c_type=memb.type.c_name(), prefix=prefix, cast=cast,
                      c_name=c_name(memb.name), name=memb.name,
                      errp=errparg)
-        ret += gen_err_check(skiperr=skiperr)
+        ret += gen_err_check(skiperr=skiperr, label=label)
 
         if memb.optional:
             pop_indent()
-- 
2.4.3

  parent reply	other threads:[~2016-02-09 11:38 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-09 11:37 [Qemu-devel] [PULL 00/31] QAPI patches for 2016-02-09 Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 01/31] qapi: Use Python 2.6 "except E as ..." syntax Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 02/31] scripts/qmp: " Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 03/31] Revert "tracetool: use Python 2.4-compatible exception handling syntax" Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 04/31] tests: Use Python 2.6 "except E as ..." syntax Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 05/31] qobject: Document more shortcomings in our number handling Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 06/31] qapi: Avoid use of misnamed DO_UPCAST() Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 07/31] qapi: Drop dead dealloc visitor variable Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 08/31] qapi: Dealloc visitor does not need a type_size() Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 09/31] qapi: Drop dead parameter in gen_params() Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 10/31] hmp: Drop pointless allocation during qapi visit Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 11/31] hmp: Cache use of qapi visitor Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 12/31] vl: Ensure qapi visitor properly ends struct visit Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 13/31] balloon: Improve use of qapi visitor Markus Armbruster
2016-02-09 11:37 ` Markus Armbruster [this message]
2016-02-09 11:37 ` [Qemu-devel] [PULL 15/31] qapi: Track all failures between visit_start/stop Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 16/31] qapi-visit: Kill unused visit_end_union() Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 17/31] qapi: Prefer type_int64 over type_int in visitors Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 18/31] qapi: Make all visitors supply uint64 callbacks Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 19/31] qapi: Consolidate visitor small integer callbacks Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 20/31] qapi: Don't cast Enum* to int* Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 21/31] qom: Use typedef for Visitor Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 22/31] qapi: Swap visit_* arguments for consistent 'name' placement Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 23/31] qom: Swap 'name' next to visitor in ObjectPropertyAccessor Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 24/31] qapi: Swap 'name' in visit_* callbacks to match public API Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 25/31] qapi: Drop unused 'kind' for struct/enum visit Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 26/31] qapi: Tighten qmp_input_end_list() Markus Armbruster
2016-02-09 11:37 ` [Qemu-devel] [PULL 27/31] qapi: Drop unused error argument for list and implicit struct Markus Armbruster
2016-02-09 11:38 ` [Qemu-devel] [PULL 28/31] qmp: Fix reference-counting of qnull on empty output visit Markus Armbruster
2016-02-09 11:38 ` [Qemu-devel] [PULL 29/31] qmp: Don't abuse stack to track qmp-output root Markus Armbruster
2016-02-09 11:38 ` [Qemu-devel] [PULL 30/31] qapi: Fix compilation failure on MIPS and SPARC Markus Armbruster
2016-02-09 11:38 ` [Qemu-devel] [PULL 31/31] qapi: Add missing JSON files in build dependencies Markus Armbruster
2016-02-09 13:08 ` [Qemu-devel] [PULL 00/31] QAPI patches for 2016-02-09 Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1455017883-25867-15-git-send-email-armbru@redhat.com \
    --to=armbru@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.