Skip to contentSkip to conversation

QMP queue

GIT PULL Applied Luiz Capitulino 2 participants 22 messages May 16, 2014 – May 19, 2014 Networking

lore
Share & tools
lore
#0 lore raw

The following changes since commit 16a9189921bdb9e351f2f9baec16b376f8d71cdf:

Merge remote-tracking branch 'remotes/kraxel/tags/pull-roms-2' into staging (2014-05-15 18:32:16 +0100)

are available in the git repository at:

git://repo.or.cz/qemu/qmp-unstable.git queue/qmp

for you to fetch changes up to 24fd848950b44de7e2d71fb69ba52b90d6acb220:

qapi: skip redundant includes (2014-05-16 10:35:59 -0400)

----------------------------------------------------------------
Benoît Canet (1):
qapi: skip redundant includes

Hani Benhabiles (6):
monitor: Convert sendkey to use command_completion.
monitor: Add chardev-remove command completion.
monitor: Add chardev-add backend argument completion.
monitor: Add set_link arguments completion.
monitor: Add netdev_add type argument completion.
monitor: Add netdev_del id argument completion.

Markus Armbruster (13):
qapi: Update qapi-code-gen.txt example to match current code
qapi: Normalize marshalling's visitor initialization and cleanup
qapi: Remove unused Visitor callbacks start_handle(), end_handle()
qapi: Replace start_optional()/end_optional() by optional() qapi-visit.py: Clean up confusing push_indent() / pop_indent() use
qapi: Clean up shadowing of parameters and locals in inner scopes qapi-visit.py: Clean up a sloppy use of field prefix
qapi: Un-inline visit of implicit struct
hmp: Call visit_end_struct() after visit_start_struct() succeeds
hw: Don't call visit_end_struct() after visit_start_struct() fails
tests: Don't call visit_end_struct() after visit_start_struct() fails
qapi: Replace uncommon use of the error API by the common one
qapi: Show qapi-commands.py invocation in qapi-code-gen.txt

 docs/qapi-code-gen.txt                        | 194 +++++++++++--------
 hmp-commands.hx                               |   8 +-
 hmp.c                                         |  16 +-
 hmp.h                                         |   6 +
 hw/timer/mc146818rtc.c                        |  41 +++-
 hw/virtio/virtio-balloon.c                    |  33 +++-
 include/qapi/visitor-impl.h                   |   8 +-
 include/qapi/visitor.h                        |   5 +-
 monitor.c                                     | 153 ++++++++++++++-
 net/net.c                                     |   2 +-
 qapi/opts-visitor.c                           |   5 +-
 qapi/qapi-visit-core.c                        | 259 +++++++++++---------------
 qapi/qmp-input-visitor.c                      |   6 +-
 qapi/string-input-visitor.c                   |   6 +-
 scripts/qapi-commands.py                      |  89 +++++----
 scripts/qapi-visit.py                         | 232 +++++++++++++----------
 scripts/qapi.py                               |  14 +-
 tests/Makefile                                |   3 +-
 tests/qapi-schema/include-repetition-sub.json |   2 +
 tests/qapi-schema/include-repetition.err      |   0
 tests/qapi-schema/include-repetition.exit     |   1 +
 tests/qapi-schema/include-repetition.json     |   3 +
 tests/qapi-schema/include-repetition.out      |   3 +
 tests/test-qmp-input-strict.c                 |  28 ++-
 tests/test-qmp-input-visitor.c                |  26 ++-
 tests/test-qmp-output-visitor.c               |  28 ++-
 tests/test-visitor-serialization.c            |  26 ++-
 27 files changed, 752 insertions(+), 445 deletions(-)
 create mode 100644 tests/qapi-schema/include-repetition-sub.json
 create mode 100644 tests/qapi-schema/include-repetition.err
 create mode 100644 tests/qapi-schema/include-repetition.exit
 create mode 100644 tests/qapi-schema/include-repetition.json
 create mode 100644 tests/qapi-schema/include-repetition.out
#1 lore raw

[Qemu-devel] [PULL 01/20] qapi: Update qapi-code-gen.txt example to match current code

From: Markus Armbruster <[email protected]>

---
 docs/qapi-code-gen.txt | 146 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 90 insertions(+), 56 deletions(-)
docs/qapi-code-gen.txt +90 -56
index 26312d8..b915c4d 100644
@@ -258,11 +258,23 @@ Example:
     mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-types.py \
       --output-dir="qapi-generated" --prefix="example-" --input-file=example-schema.json
     mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.c
-    /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
+[Uninteresting stuff omitted...]
+
+    void qapi_free_UserDefOneList(UserDefOneList * obj)
+    {
+        QapiDeallocVisitor *md;
+        Visitor *v;
+
+        if (!obj) {
+            return;
+        }
+
+        md = qapi_dealloc_visitor_new();
+        v = qapi_dealloc_get_visitor(md);
+        visit_type_UserDefOneList(v, &obj, NULL, NULL);
+        qapi_dealloc_visitor_cleanup(md);
+    }
 
-    #include "qapi/qapi-dealloc-visitor.h"
-    #include "example-qapi-types.h"
-    #include "example-qapi-visit.h"
 
     void qapi_free_UserDefOne(UserDefOne * obj)
     {
@@ -280,31 +292,37 @@ Example:
     }
 
     mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.h
-    /* AUTOMATICALLY GENERATED, DO NOT MODIFY */
-    #ifndef QAPI_GENERATED_EXAMPLE_QAPI_TYPES
-    #define QAPI_GENERATED_EXAMPLE_QAPI_TYPES
+[Uninteresting stuff omitted...]
+
+    #ifndef EXAMPLE_QAPI_TYPES_H
+    #define EXAMPLE_QAPI_TYPES_H
 
-    #include "qapi/qapi-types-core.h"
+[Builtin types omitted...]
 
     typedef struct UserDefOne UserDefOne;
 
     typedef struct UserDefOneList
     {
-        UserDefOne *value;
+        union {
+            UserDefOne *value;
+            uint64_t padding;
+        };
         struct UserDefOneList *next;
     } UserDefOneList;
 
+[Functions on builtin types omitted...]
+
     struct UserDefOne
     {
         int64_t integer;
         char * string;
     };
 
+    void qapi_free_UserDefOneList(UserDefOneList * obj);
     void qapi_free_UserDefOne(UserDefOne * obj);
 
     #endif
 
-
 === scripts/qapi-visit.py ===
 
 Used to generate the visitor functions used to walk through and convert
@@ -328,39 +346,63 @@ Example:
     mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-visit.py \
         --output-dir="qapi-generated" --prefix="example-" --input-file=example-schema.json
     mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.c
-    /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
+[Uninteresting stuff omitted...]
 
-    #include "example-qapi-visit.h"
+    static void visit_type_UserDefOne_fields(Visitor *m, UserDefOne ** obj, Error **errp)
+    {
+        Error *err = NULL;
+        visit_type_int(m, &(*obj)->integer, "integer", &err);
+        visit_type_str(m, &(*obj)->string, "string", &err);
+
+        error_propagate(errp, err);
+    }
 
     void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp)
     {
-        visit_start_struct(m, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), errp);
-        visit_type_int(m, (obj && *obj) ? &(*obj)->integer : NULL, "integer", errp);
-        visit_type_str(m, (obj && *obj) ? &(*obj)->string : NULL, "string", errp);
-        visit_end_struct(m, errp);
+        if (!error_is_set(errp)) {
+            Error *err = NULL;
+            visit_start_struct(m, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), &err);
+            if (!err) {
+                if (*obj) {
+                    visit_type_UserDefOne_fields(m, obj, &err);
+                    error_propagate(errp, err);
+                    err = NULL;
+                }
+                /* Always call end_struct if start_struct succeeded.  */
+                visit_end_struct(m, &err);
+            }
+            error_propagate(errp, err);
+        }
     }
 
     void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp)
     {
         GenericList *i, **prev = (GenericList **)obj;
-
-        visit_start_list(m, name, errp);
-
-        for (; (i = visit_next_list(m, prev, errp)) != NULL; prev = &i) {
-            UserDefOneList *native_i = (UserDefOneList *)i;
-            visit_type_UserDefOne(m, &native_i->value, NULL, errp);
+        Error *err = NULL;
+
+        if (!error_is_set(errp)) {
+            visit_start_list(m, name, &err);
+            if (!err) {
+                for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
+                    UserDefOneList *native_i = (UserDefOneList *)i;
+                    visit_type_UserDefOne(m, &native_i->value, NULL, &err);
+                }
+                error_propagate(errp, err);
+                err = NULL;
+
+                /* Always call end_list if start_list succeeded.  */
+                visit_end_list(m, &err);
+            }
+            error_propagate(errp, err);
         }
-
-        visit_end_list(m, errp);
     }
     mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.h
-    /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
+[Uninteresting stuff omitted...]
 
-    #ifndef QAPI_GENERATED_EXAMPLE_QAPI_VISIT
-    #define QAPI_GENERATED_EXAMPLE_QAPI_VISIT
+    #ifndef EXAMPLE_QAPI_VISIT_H
+    #define EXAMPLE_QAPI_VISIT_H
 
-    #include "qapi/qapi-visit-core.h"
-    #include "example-qapi-types.h"
+[Visitors for builtin types omitted...]
 
     void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp);
     void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp);
@@ -368,9 +410,6 @@ Example:
     #endif
     mdroth@illuin:~/w/qemu2.git$
 
-(The actual structure of the visit_type_* functions is a bit more complex
-in order to propagate errors correctly and avoid leaking memory).
-
 === scripts/qapi-commands.py ===
 
 Used to generate the marshaling/dispatch functions for the commands defined
@@ -391,18 +430,8 @@ $(prefix)qmp-commands.h: Function prototypes for the QMP commands
 Example:
 
     mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-marshal.c
-    /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
-
-    #include "qemu-objects.h"
-    #include "qapi/qmp-core.h"
-    #include "qapi/qapi-visit-core.h"
-    #include "qapi/qmp-output-visitor.h"
-    #include "qapi/qmp-input-visitor.h"
-    #include "qapi/qapi-dealloc-visitor.h"
-    #include "example-qapi-types.h"
-    #include "example-qapi-visit.h"
+[Uninteresting stuff omitted...]
 
-    #include "example-qmp-commands.h"
     static void qmp_marshal_output_my_command(UserDefOne * ret_in, QObject **ret_out, Error **errp)
     {
         QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
@@ -411,15 +440,16 @@ Example:
 
         v = qmp_output_get_visitor(mo);
         visit_type_UserDefOne(v, &ret_in, "unused", errp);
+        if (!error_is_set(errp)) {
+            *ret_out = qmp_output_get_qobject(mo);
+        }
+        qmp_output_visitor_cleanup(mo);
         v = qapi_dealloc_get_visitor(md);
-        visit_type_UserDefOne(v, &ret_in, "unused", errp);
+        visit_type_UserDefOne(v, &ret_in, "unused", NULL);
         qapi_dealloc_visitor_cleanup(md);
-
-
-        *ret_out = qmp_output_get_qobject(mo);
     }
 
-    static void qmp_marshal_input_my_command(QmpState *qmp__sess, QDict *args, QObject **ret, Error **errp)
+    static void qmp_marshal_input_my_command(QDict *args, QObject **ret, Error **errp)
     {
         UserDefOne * retval = NULL;
         QmpInputVisitor *mi;
@@ -427,38 +457,42 @@ Example:
         Visitor *v;
         UserDefOne * arg1 = NULL;
 
-        mi = qmp_input_visitor_new(QOBJECT(args));
+        mi = qmp_input_visitor_new_strict(QOBJECT(args));
         v = qmp_input_get_visitor(mi);
         visit_type_UserDefOne(v, &arg1, "arg1", errp);
+        qmp_input_visitor_cleanup(mi);
 
         if (error_is_set(errp)) {
             goto out;
         }
         retval = qmp_my_command(arg1, errp);
-        qmp_marshal_output_my_command(retval, ret, errp);
+        if (!error_is_set(errp)) {
+            qmp_marshal_output_my_command(retval, ret, errp);
+        }
 
     out:
         md = qapi_dealloc_visitor_new();
         v = qapi_dealloc_get_visitor(md);
-        visit_type_UserDefOne(v, &arg1, "arg1", errp);
+        visit_type_UserDefOne(v, &arg1, "arg1", NULL);
         qapi_dealloc_visitor_cleanup(md);
         return;
     }
 
     static void qmp_init_marshal(void)
     {
-        qmp_register_command("my-command", qmp_marshal_input_my_command);
+        qmp_register_command("my-command", qmp_marshal_input_my_command, QCO_NO_OPTIONS);
     }
 
     qapi_init(qmp_init_marshal);
     mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-commands.h
-    /* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT MODIFY */
+[Uninteresting stuff omitted...]
 
-    #ifndef QAPI_GENERATED_EXAMPLE_QMP_COMMANDS
-    #define QAPI_GENERATED_EXAMPLE_QMP_COMMANDS
+    #ifndef EXAMPLE_QMP_COMMANDS_H
+    #define EXAMPLE_QMP_COMMANDS_H
 
     #include "example-qapi-types.h"
-    #include "error.h"
+    #include "qapi/qmp/qdict.h"
+    #include "qapi/error.h"
 
     UserDefOne * qmp_my_command(UserDefOne * arg1, Error **errp);
 
Signature · 1 line
1.9.0
#2 lore raw

[Qemu-devel] [PULL 14/20] monitor: Convert sendkey to use command_completion.

In reply to Luiz Capitulino

From: Hani Benhabiles <[email protected]>

---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 32 +++++++++++++++++++++++---------
 3 files changed, 25 insertions(+), 9 deletions(-)
hmp-commands.hx +1 -0
index 8971f1b..b4b23c8 100644
@@ -556,6 +556,7 @@ ETEXI
         .params     = "keys [hold_ms]",
         .help       = "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)",
         .mhandler.cmd = hmp_send_key,
+        .command_completion = sendkey_completion,
     },
 
 STEXI
hmp.h +1 -0
index 20ef454..12e21e7 100644
@@ -97,5 +97,6 @@ void object_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void object_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
+void sendkey_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +23 -9
index 9af6b0a..cb9b2c2 100644
@@ -4376,6 +4376,28 @@ void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
     qapi_free_ObjectPropertyInfoList(start);
 }
 
+void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    int i;
+    char *sep;
+    size_t len;
+
+    if (nb_args != 2) {
+        return;
+    }
+    sep = strrchr(str, '-');
+    if (sep) {
+        str = sep + 1;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    for (i = 0; i < Q_KEY_CODE_MAX; i++) {
+        if (!strncmp(str, QKeyCode_lookup[i], len)) {
+            readline_add_completion(rs, QKeyCode_lookup[i]);
+        }
+    }
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
@@ -4444,15 +4466,7 @@ static void monitor_find_completion_by_table(Monitor *mon,
             break;
         case 's':
         case 'S':
-            if (!strcmp(cmd->name, "sendkey")) {
-                char *sep = strrchr(str, '-');
-                if (sep)
-                    str = sep + 1;
-                readline_set_completion_index(mon->rs, strlen(str));
-                for (i = 0; i < Q_KEY_CODE_MAX; i++) {
-                    cmd_completion(mon, str, QKeyCode_lookup[i]);
-                }
-            } else if (!strcmp(cmd->name, "help|?")) {
+            if (!strcmp(cmd->name, "help|?")) {
                 monitor_find_completion_by_table(mon, cmd_table,
                                                  &args[1], nb_args - 1);
             }
Signature · 1 line
1.9.0
#3 lore raw

[Qemu-devel] [PULL 13/20] qapi: Show qapi-commands.py invocation in qapi-code-gen.txt

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

While there, pare down the shell prompts.

---
 docs/qapi-code-gen.txt | 27 +++++++++++++--------------
 1 file changed, 13 insertions(+), 14 deletions(-)
docs/qapi-code-gen.txt +13 -14
index 1948946..867d124 100644
@@ -230,14 +230,13 @@ node structure that can be used to chain together a list of such types in
 case we want to accept/return a list of this type with a command), and a
 command which takes that type as a parameter and returns the same type:
 
-    mdroth@illuin:~/w/qemu2.git$ cat example-schema.json
+    $ cat example-schema.json
     { 'type': 'UserDefOne',
       'data': { 'integer': 'int', 'string': 'str' } }
 
     { 'command': 'my-command',
       'data':    {'arg1': 'UserDefOne'},
       'returns': 'UserDefOne' }
-    mdroth@illuin:~/w/qemu2.git$
 
 === scripts/qapi-types.py ===
 
@@ -255,9 +254,9 @@ created code.
 
 Example:
 
-    mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-types.py \
-      --output-dir="qapi-generated" --prefix="example-" --input-file=example-schema.json
-    mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.c
+    $ python scripts/qapi-types.py --output-dir="qapi-generated" \
+    --prefix="example-" --input-file=example-schema.json
+    $ cat qapi-generated/example-qapi-types.c
 [Uninteresting stuff omitted...]
 
     void qapi_free_UserDefOneList(UserDefOneList * obj)
@@ -290,7 +289,7 @@ Example:
         qapi_dealloc_visitor_cleanup(md);
     }
 
-    mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-types.h
+    $ cat qapi-generated/example-qapi-types.h
 [Uninteresting stuff omitted...]
 
     #ifndef EXAMPLE_QAPI_TYPES_H
@@ -342,9 +341,9 @@ $(prefix)qapi-visit.h: declarations for previously mentioned visitor
 
 Example:
 
-    mdroth@illuin:~/w/qemu2.git$ python scripts/qapi-visit.py \
-        --output-dir="qapi-generated" --prefix="example-" --input-file=example-schema.json
-    mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.c
+    $ python scripts/qapi-visit.py --output-dir="qapi-generated"
+    --prefix="example-" --input-file=example-schema.json
+    $ cat qapi-generated/example-qapi-visit.c
 [Uninteresting stuff omitted...]
 
     static void visit_type_UserDefOne_fields(Visitor *m, UserDefOne ** obj, Error **errp)
@@ -400,7 +399,9 @@ Example:
     out:
         error_propagate(errp, err);
     }
-    mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.h
+    $ python scripts/qapi-commands.py --output-dir="qapi-generated" \
+    --prefix="example-" --input-file=example-schema.json
+    $ cat qapi-generated/example-qapi-visit.h
 [Uninteresting stuff omitted...]
 
     #ifndef EXAMPLE_QAPI_VISIT_H
@@ -412,7 +413,6 @@ Example:
     void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp);
 
     #endif
-    mdroth@illuin:~/w/qemu2.git$
 
 === scripts/qapi-commands.py ===
 
@@ -433,7 +433,7 @@ $(prefix)qmp-commands.h: Function prototypes for the QMP commands
 
 Example:
 
-    mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-marshal.c
+    $ cat qapi-generated/example-qmp-marshal.c
 [Uninteresting stuff omitted...]
 
     static void qmp_marshal_output_my_command(UserDefOne * ret_in, QObject **ret_out, Error **errp)
@@ -497,7 +497,7 @@ Example:
     }
 
     qapi_init(qmp_init_marshal);
-    mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qmp-commands.h
+    $ cat qapi-generated/example-qmp-commands.h
 [Uninteresting stuff omitted...]
 
     #ifndef EXAMPLE_QMP_COMMANDS_H
@@ -510,4 +510,3 @@ Example:
     UserDefOne * qmp_my_command(UserDefOne * arg1, Error **errp);
 
     #endif
-    mdroth@illuin:~/w/qemu2.git$
Signature · 1 line
1.9.0
#4 lore raw

[Qemu-devel] [PULL 20/20] qapi: skip redundant includes

In reply to Luiz Capitulino

From: Benoît Canet <[email protected]>

The purpose of this change is to help create a json file containing common definitions; each bit of generated C code must be emitted only one time.

A second history global to all QAPISchema instances has been added to detect when a file is included more than one time and skip these includes.
It does not act as a stack and the changes made to it by the __init__ function are propagated back to the caller so it's really a global state.

---
 docs/qapi-code-gen.txt                        |  2 +-
 scripts/qapi.py                               | 14 +++++++++++---
 tests/Makefile                                |  3 ++-
 tests/qapi-schema/include-repetition-sub.json |  2 ++
 tests/qapi-schema/include-repetition.err      |  0
 tests/qapi-schema/include-repetition.exit     |  1 +
 tests/qapi-schema/include-repetition.json     |  3 +++
 tests/qapi-schema/include-repetition.out      |  3 +++
 8 files changed, 23 insertions(+), 5 deletions(-)
 create mode 100644 tests/qapi-schema/include-repetition-sub.json
 create mode 100644 tests/qapi-schema/include-repetition.err
 create mode 100644 tests/qapi-schema/include-repetition.exit
 create mode 100644 tests/qapi-schema/include-repetition.json
 create mode 100644 tests/qapi-schema/include-repetition.out
docs/qapi-code-gen.txt +1 -1
index 867d124..dea0d50 100644
@@ -48,7 +48,7 @@ The QAPI schema definitions can be modularized using the 'include' directive:
  { 'include': 'path/to/file.json'}
 
 The directive is evaluated recursively, and include paths are relative to the
-file using the directive.
+file using the directive. Multiple includes of the same file are safe.
 
 
 === Complex types ===
scripts/qapi.py +11 -3
index ec806aa..0265b40 100644
@@ -73,13 +73,18 @@ class QAPIExprError(Exception):
 
 class QAPISchema:
 
-    def __init__(self, fp, input_relname=None, include_hist=[], parent_info=None):
+    def __init__(self, fp, input_relname=None, include_hist=[],
+                 previously_included=[], parent_info=None):
+        """ include_hist is a stack used to detect inclusion cycles
+            previously_included is a global state used to avoid multiple
+                                inclusions of the same file"""
         input_fname = os.path.abspath(fp.name)
         if input_relname is None:
             input_relname = fp.name
         self.input_dir = os.path.dirname(input_fname)
         self.input_file = input_relname
         self.include_hist = include_hist + [(input_relname, input_fname)]
+        previously_included.append(input_fname)
         self.parent_info = parent_info
         self.src = fp.read()
         if self.src == '' or self.src[-1] != '\n':
@@ -106,13 +111,16 @@ class QAPISchema:
                        for elem in self.include_hist):
                     raise QAPIExprError(expr_info, "Inclusion loop for %s"
                                         % include)
+                # skip multiple include of the same file
+                if include_path in previously_included:
+                    continue
                 try:
                     fobj = open(include_path, 'r')
                 except IOError as e:
                     raise QAPIExprError(expr_info,
                                         '%s: %s' % (e.strerror, include))
-                exprs_include = QAPISchema(fobj, include,
-                                           self.include_hist, expr_info)
+                exprs_include = QAPISchema(fobj, include, self.include_hist,
+                                           previously_included, expr_info)
                 self.exprs.extend(exprs_include.exprs)
             else:
                 expr_elem = {'expr': expr,
tests/Makefile +2 -1
index 6b8b6f2..9f7ca61 100644
@@ -193,7 +193,8 @@ check-qapi-schema-y := $(addprefix tests/qapi-schema/, \
         flat-union-string-discriminator.json \
         include-simple.json include-relpath.json include-format-err.json \
         include-non-file.json include-no-file.json include-before-err.json \
-        include-nested-err.json include-self-cycle.json include-cycle.json)
+        include-nested-err.json include-self-cycle.json include-cycle.json \
+        include-repetition.json)
 
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h tests/test-qmp-commands.h
 
tests/qapi-schema/include-repetition-sub.json new file +2 -0
new file mode 100644
index 0000000..6bfffdf
@@ -0,0 +1,2 @@
+{ 'include': 'comments.json' }
+{ 'include': 'comments.json' }
tests/qapi-schema/include-repetition.err new file +0 -0
new file mode 100644
index 0000000..e69de29
tests/qapi-schema/include-repetition.exit new file +1 -0
new file mode 100644
index 0000000..573541a
@@ -0,0 +1 @@
+0
tests/qapi-schema/include-repetition.json new file +3 -0
new file mode 100644
index 0000000..ec329dd
@@ -0,0 +1,3 @@
+{ 'include': 'comments.json' }
+{ 'include': 'include-repetition-sub.json' }
+{ 'include': 'comments.json' }
tests/qapi-schema/include-repetition.out new file +3 -0
new file mode 100644
index 0000000..4ce3dcf
@@ -0,0 +1,3 @@
+[OrderedDict([('enum', 'Status'), ('data', ['good', 'bad', 'ugly'])])]
+[{'enum_name': 'Status', 'enum_values': ['good', 'bad', 'ugly']}]
+[]
Signature · 1 line
1.9.0
#5 lore raw

[Qemu-devel] [PULL 11/20] tests: Don't call visit_end_struct() after visit_start_struct() fails

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

When visit_start_struct() fails, visit_end_struct() must not be called. Three out of four visit_type_TestStruct() call it anyway. As far as I can tell, visit_start_struct() doesn't actually fail there. Fix them anyway.

---
 tests/test-qmp-input-strict.c      | 18 +++++++++++++-----
 tests/test-qmp-output-visitor.c    | 18 +++++++++++++-----
 tests/test-visitor-serialization.c | 18 +++++++++++++-----
 3 files changed, 39 insertions(+), 15 deletions(-)
tests/test-qmp-input-strict.c +13 -5
index 449d285..ec798c2 100644
@@ -72,14 +72,22 @@ typedef struct TestStruct
 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
                                   const char *name, Error **errp)
 {
+    Error *err = NULL;
+
     visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
-                       errp);
+                       &err);
+    if (err) {
+        goto out;
+    }
+
+    visit_type_int(v, &(*obj)->integer, "integer", &err);
+    visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
+    visit_type_str(v, &(*obj)->string, "string", &err);
 
-    visit_type_int(v, &(*obj)->integer, "integer", errp);
-    visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
-    visit_type_str(v, &(*obj)->string, "string", errp);
+    visit_end_struct(v, &err);
 
-    visit_end_struct(v, errp);
+out:
+    error_propagate(errp, err);
 }
 
 static void test_validate_struct(TestInputVisitorData *data,
tests/test-qmp-output-visitor.c +13 -5
index 2580f3d..dfd597c 100644
@@ -176,14 +176,22 @@ typedef struct TestStruct
 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
                                   const char *name, Error **errp)
 {
+    Error *err = NULL;
+
     visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
-                       errp);
+                       &err);
+    if (err) {
+        goto out;
+    }
+
+    visit_type_int(v, &(*obj)->integer, "integer", &err);
+    visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
+    visit_type_str(v, &(*obj)->string, "string", &err);
 
-    visit_type_int(v, &(*obj)->integer, "integer", errp);
-    visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
-    visit_type_str(v, &(*obj)->string, "string", errp);
+    visit_end_struct(v, &err);
 
-    visit_end_struct(v, errp);
+out:
+    error_propagate(errp, err);
 }
 
 static void test_visitor_out_struct(TestOutputVisitorData *data,
tests/test-visitor-serialization.c +13 -5
index 8166cf1..85170e5 100644
@@ -195,13 +195,21 @@ typedef struct TestStruct
 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
                                   const char *name, Error **errp)
 {
-    visit_start_struct(v, (void **)obj, NULL, name, sizeof(TestStruct), errp);
+    Error *err= NULL;
 
-    visit_type_int(v, &(*obj)->integer, "integer", errp);
-    visit_type_bool(v, &(*obj)->boolean, "boolean", errp);
-    visit_type_str(v, &(*obj)->string, "string", errp);
+    visit_start_struct(v, (void **)obj, NULL, name, sizeof(TestStruct), &err);
+    if (err) {
+        goto out;
+    }
+
+    visit_type_int(v, &(*obj)->integer, "integer", &err);
+    visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
+    visit_type_str(v, &(*obj)->string, "string", &err);
+
+    visit_end_struct(v, &err);
 
-    visit_end_struct(v, errp);
+out:
+    error_propagate(errp, err);
 }
 
 static TestStruct *struct_create(void)
Signature · 1 line
1.9.0
#6 lore raw

[Qemu-devel] [PULL 17/20] monitor: Add set_link arguments completion.

In reply to Luiz Capitulino

From: Hani Benhabiles <[email protected]>

Make it possible to query all net clients without specifying an ID when calling qemu_find_net_clients_except().

This also adds the add_completion_option() function which is to be used for other commands completions as well.

---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 34 ++++++++++++++++++++++++++++++++++
 net/net.c       |  2 +-
 4 files changed, 37 insertions(+), 1 deletion(-)
hmp-commands.hx +1 -0
index 93fa534..3e7b29c 100644
@@ -1340,6 +1340,7 @@ ETEXI
         .params     = "name on|off",
         .help       = "change the link status of a network adapter",
         .mhandler.cmd = hmp_set_link,
+        .command_completion = set_link_completion,
     },
 
 STEXI
hmp.h +1 -0
index f8e16a8..91c9c85 100644
@@ -100,5 +100,6 @@ void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +34 -0
index ef5168b..fd50c2d 100644
@@ -4269,6 +4269,17 @@ static const char *next_arg_type(const char *typestr)
     return (p != NULL ? ++p : typestr);
 }
 
+static void add_completion_option(ReadLineState *rs, const char *str,
+                                  const char *option)
+{
+    if (!str || !option) {
+        return;
+    }
+    if (!strncmp(option, str, strlen(str))) {
+        readline_add_completion(rs, option);
+    }
+}
+
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     size_t len;
@@ -4444,6 +4455,29 @@ void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
     }
 }
 
+void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    if (nb_args == 2) {
+        NetClientState *ncs[255];
+        int count, i;
+        count = qemu_find_net_clients_except(NULL, ncs,
+                                             NET_CLIENT_OPTIONS_KIND_NONE, 255);
+        for (i = 0; i < count; i++) {
+            const char *name = ncs[i]->name;
+            if (!strncmp(str, name, len)) {
+                readline_add_completion(rs, name);
+            }
+        }
+    } else if (nb_args == 3) {
+        add_completion_option(rs, str, "on");
+        add_completion_option(rs, str, "off");
+    }
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
net/net.c +1 -1
index 9db4dba..0ff2e40 100644
@@ -633,7 +633,7 @@ int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
         if (nc->info->type == type) {
             continue;
         }
-        if (!strcmp(nc->name, id)) {
+        if (!id || !strcmp(nc->name, id)) {
             if (ret < max) {
                 ncs[ret] = nc;
             }
Signature · 1 line
1.9.0
#7 lore raw

[Qemu-devel] [PULL 09/20] hmp: Call visit_end_struct() after visit_start_struct() succeeds

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

When visit_start_struct() succeeds, visit_end_struct() must be called. hmp_object_add() doesn't when a member visit fails. As far as I can tell, the opts visitor copes okay with the misuse. Fix it anyway.

---
 hmp.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
hmp.c +8 -8
index 5c4d612..a9d0236 100644
@@ -1388,6 +1388,7 @@ void hmp_netdev_del(Monitor *mon, const QDict *qdict)
 void hmp_object_add(Monitor *mon, const QDict *qdict)
 {
     Error *err = NULL;
+    Error *err_end = NULL;
     QemuOpts *opts;
     char *type = NULL;
     char *id = NULL;
@@ -1411,24 +1412,23 @@ void hmp_object_add(Monitor *mon, const QDict *qdict)
     qdict_del(pdict, "qom-type");
     visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
     if (err) {
-        goto out_clean;
+        goto out_end;
     }
 
     qdict_del(pdict, "id");
     visit_type_str(opts_get_visitor(ov), &id, "id", &err);
     if (err) {
-        goto out_clean;
+        goto out_end;
     }
 
     object_add(type, id, pdict, opts_get_visitor(ov), &err);
-    if (err) {
-        goto out_clean;
-    }
-    visit_end_struct(opts_get_visitor(ov), &err);
-    if (err) {
+
+out_end:
+    visit_end_struct(opts_get_visitor(ov), &err_end);
+    if (!err && err_end) {
         qmp_object_del(id, NULL);
     }
-
+    error_propagate(&err, err_end);
 out_clean:
     opts_visitor_cleanup(ov);
 
Signature · 1 line
1.9.0
#8 lore raw

[Qemu-devel] [PULL 08/20] qapi: Un-inline visit of implicit struct

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

In preparation of error handling changes. Bonus: generates less duplicated code.

---
 scripts/qapi-visit.py | 48 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 34 insertions(+), 14 deletions(-)
scripts/qapi-visit.py +34 -14
index 1e368be..1ef753a 100644
@@ -17,6 +17,31 @@ import os
 import getopt
 import errno
 
+implicit_structs = []
+
+def generate_visit_implicit_struct(type):
+    global implicit_structs
+    if type in implicit_structs:
+        return ''
+    implicit_structs.append(type)
+    return mcgen('''
+
+static void visit_type_implicit_%(c_type)s(Visitor *m, %(c_type)s **obj, Error **errp)
+{
+    Error *err = NULL;
+
+    visit_start_implicit_struct(m, (void **)obj, sizeof(%(c_type)s), &err);
+    if (!err) {
+        visit_type_%(c_type)s_fields(m, obj, &err);
+        error_propagate(errp, err);
+        err = NULL;
+        visit_end_implicit_struct(m, &err);
+    }
+    error_propagate(errp, err);
+}
+''',
+                 c_type=type_name(type))
+
 def generate_visit_struct_fields(name, field_prefix, fn_prefix, members, base = None):
     substructs = []
     ret = ''
@@ -49,6 +74,9 @@ static void visit_type_%(full_name)s_field_%(c_name)s(Visitor *m, %(name)s **obj
 }
 ''')
 
+    if base:
+        ret += generate_visit_implicit_struct(base)
+
     ret += mcgen('''
 
 static void visit_type_%(full_name)s_fields(Visitor *m, %(name)s ** obj, Error **errp)
@@ -60,13 +88,7 @@ static void visit_type_%(full_name)s_fields(Visitor *m, %(name)s ** obj, Error *
 
     if base:
         ret += mcgen('''
-visit_start_implicit_struct(m, (void**) &(*obj)->%(c_prefix)s%(c_name)s, sizeof(%(type)s), &err);
-if (!err) {
-    visit_type_%(type)s_fields(m, &(*obj)->%(c_prefix)s%(c_name)s, &err);
-    error_propagate(errp, err);
-    err = NULL;
-    visit_end_implicit_struct(m, &err);
-}
+visit_type_implicit_%(type)s(m, &(*obj)->%(c_prefix)s%(c_name)s, &err);
 ''',
                      c_prefix=c_var(field_prefix),
                      type=type_name(base), c_name=c_var('base'))
@@ -292,6 +314,10 @@ def generate_visit_union(expr):
             del base_fields[discriminator]
         ret += generate_visit_struct_fields(name, "", "", base_fields)
 
+    if discriminator:
+        for key in members:
+            ret += generate_visit_implicit_struct(members[key])
+
     ret += mcgen('''
 
 void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **errp)
@@ -330,13 +356,7 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **
         if not discriminator:
             fmt = 'visit_type_%(c_type)s(m, &(*obj)->%(c_name)s, "data", &err);'
         else:
-            fmt = '''visit_start_implicit_struct(m, (void**) &(*obj)->%(c_name)s, sizeof(%(c_type)s), &err);
-                if (!err) {
-                    visit_type_%(c_type)s_fields(m, &(*obj)->%(c_name)s, &err);
-                    error_propagate(errp, err);
-                    err = NULL;
-                    visit_end_implicit_struct(m, &err);
-                }'''
+            fmt = 'visit_type_implicit_%(c_type)s(m, &(*obj)->%(c_name)s, &err);'
 
         enum_full_value = generate_enum_full_value(disc_type, key)
         ret += mcgen('''
Signature · 1 line
1.9.0
#9 lore raw

[Qemu-devel] [PULL 16/20] monitor: Add chardev-add backend argument completion.

In reply to Luiz Capitulino

From: Hani Benhabiles <[email protected]>

---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 23 +++++++++++++++++++++++
 3 files changed, 25 insertions(+)
hmp-commands.hx +1 -0
index ba88e2a..93fa534 100644
@@ -1623,6 +1623,7 @@ ETEXI
         .params     = "args",
         .help       = "add chardev",
         .mhandler.cmd = hmp_chardev_add,
+        .command_completion = chardev_add_completion,
     },
 
 STEXI
hmp.h +1 -0
index affc2b6..f8e16a8 100644
@@ -99,5 +99,6 @@ void device_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
+void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +23 -0
index 55e5696..ef5168b 100644
@@ -4269,6 +4269,29 @@ static const char *next_arg_type(const char *typestr)
     return (p != NULL ? ++p : typestr);
 }
 
+void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+    ChardevBackendInfoList *list, *start;
+
+    if (nb_args != 2) {
+        return;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+
+    start = list = qmp_query_chardev_backends(NULL);
+    while (list) {
+        const char *chr_name = list->value->name;
+
+        if (!strncmp(chr_name, str, len)) {
+            readline_add_completion(rs, chr_name);
+        }
+        list = list->next;
+    }
+    qapi_free_ChardevBackendInfoList(start);
+}
+
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
Signature · 1 line
1.9.0
#10 lore raw

[Qemu-devel] [PULL 15/20] monitor: Add chardev-remove command completion.

In reply to Luiz Capitulino

From: Hani Benhabiles <[email protected]>

---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 23 +++++++++++++++++++++++
 3 files changed, 25 insertions(+)
hmp-commands.hx +1 -0
index b4b23c8..ba88e2a 100644
@@ -1639,6 +1639,7 @@ ETEXI
         .params     = "id",
         .help       = "remove chardev",
         .mhandler.cmd = hmp_chardev_remove,
+        .command_completion = chardev_remove_completion,
     },
 
 STEXI
hmp.h +1 -0
index 12e21e7..affc2b6 100644
@@ -98,5 +98,6 @@ void object_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str);
 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str);
+void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +23 -0
index cb9b2c2..55e5696 100644
@@ -4339,6 +4339,29 @@ static void device_del_bus_completion(ReadLineState *rs,  BusState *bus,
     }
 }
 
+void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+    ChardevInfoList *list, *start;
+
+    if (nb_args != 2) {
+        return;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+
+    start = list = qmp_query_chardev(NULL);
+    while (list) {
+        ChardevInfo *chr = list->value;
+
+        if (!strncmp(chr->label, str, len)) {
+            readline_add_completion(rs, chr->label);
+        }
+        list = list->next;
+    }
+    qapi_free_ChardevInfoList(start);
+}
+
 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     size_t len;
Signature · 1 line
1.9.0
#11 lore raw

[Qemu-devel] [PULL 19/20] monitor: Add netdev_del id argument completion.

In reply to Luiz Capitulino

From: Hani Benhabiles <[email protected]>

---
 hmp-commands.hx |  1 +
 hmp.h           |  1 +
 monitor.c       | 26 ++++++++++++++++++++++++++
 3 files changed, 28 insertions(+)
hmp-commands.hx +1 -0
index a7f9b2f..2e462c0 100644
@@ -1252,6 +1252,7 @@ ETEXI
         .params     = "id",
         .help       = "remove host network device",
         .mhandler.cmd = hmp_netdev_del,
+        .command_completion = netdev_del_completion,
     },
 
 STEXI
hmp.h +1 -0
index bcecd0d..aba59e9 100644
@@ -102,5 +102,6 @@ void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str);
+void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +26 -0
index a3dd361..593679a 100644
@@ -4493,6 +4493,32 @@ void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
     }
 }
 
+void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    int len, count, i;
+    NetClientState *ncs[255];
+
+    if (nb_args != 2) {
+        return;
+    }
+
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
+                                         255);
+    for (i = 0; i < count; i++) {
+        QemuOpts *opts;
+        const char *name = ncs[i]->name;
+        if (strncmp(str, name, len)) {
+            continue;
+        }
+        opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
+        if (opts) {
+            readline_add_completion(rs, name);
+        }
+    }
+}
+
 static void monitor_find_completion_by_table(Monitor *mon,
                                              const mon_cmd_t *cmd_table,
                                              char **args,
Signature · 1 line
1.9.0
#12 lore raw

[Qemu-devel] [PULL 06/20] qapi: Clean up shadowing of parameters and locals in inner scopes

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

By un-inlining the visit of nested complex types.

---
 scripts/qapi-visit.py | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)
scripts/qapi-visit.py +17 -3
index 28176ba..57be9bf 100644
@@ -35,6 +35,19 @@ def generate_visit_struct_fields(name, field_prefix, fn_prefix, members, base =
             nested_field_prefix = "%s%s." % (field_prefix, argname)
             ret += generate_visit_struct_fields(name, nested_field_prefix,
                                                 nested_fn_prefix, argentry)
+            ret += mcgen('''
+
+static void visit_type_%(full_name)s_field_%(c_name)s(Visitor *m, %(name)s **obj, Error **errp)
+{
+    Error *err = NULL;
+''',
+                         name=name, full_name=full_name, c_name=c_var(argname))
+            push_indent()
+            ret += generate_visit_struct_body(full_name, argname, argentry)
+            pop_indent()
+            ret += mcgen('''
+}
+''')
 
     ret += mcgen('''
 
@@ -69,7 +82,10 @@ if ((*obj)->%(prefix)shas_%(c_name)s) {
             push_indent()
 
         if structured:
-            ret += generate_visit_struct_body(full_name, argname, argentry)
+            ret += mcgen('''
+visit_type_%(full_name)s_field_%(c_name)s(m, obj, &err);
+''',
+                         full_name=full_name, c_name=c_var(argname))
         else:
             ret += mcgen('''
 visit_type_%(type)s(m, &(*obj)->%(c_prefix)s%(c_name)s, "%(name)s", &err);
@@ -106,8 +122,6 @@ if (!error_is_set(errp)) {
 
     if len(field_prefix):
         ret += mcgen('''
-Error **errp = &err; /* from outer scope */
-Error *err = NULL;
 visit_start_struct(m, NULL, "", "%(name)s", 0, &err);
 ''',
                 name=name)
Signature · 1 line
1.9.0
#13 lore raw

[Qemu-devel] [PULL 18/20] monitor: Add netdev_add type argument completion.

In reply to Luiz Capitulino

From: Hani Benhabiles <[email protected]>

Also update the command's documentation.

---
 hmp-commands.hx |  3 ++-
 hmp.h           |  1 +
 monitor.c       | 15 +++++++++++++++
 3 files changed, 18 insertions(+), 1 deletion(-)
hmp-commands.hx +2 -1
index 3e7b29c..a7f9b2f 100644
@@ -1234,9 +1234,10 @@ ETEXI
     {
         .name       = "netdev_add",
         .args_type  = "netdev:O",
-        .params     = "[user|tap|socket|hubport|netmap],id=str[,prop=value][,...]",
+        .params     = "[user|tap|socket|vde|bridge|hubport|netmap],id=str[,prop=value][,...]",
         .help       = "add host network device",
         .mhandler.cmd = hmp_netdev_add,
+        .command_completion = netdev_add_completion,
     },
 
 STEXI
hmp.h +1 -0
index 91c9c85..bcecd0d 100644
@@ -101,5 +101,6 @@ void sendkey_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str);
 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 void set_link_completion(ReadLineState *rs, int nb_args, const char *str);
+void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str);
 
 #endif
monitor.c +15 -0
index fd50c2d..a3dd361 100644
@@ -4303,6 +4303,21 @@ void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
     qapi_free_ChardevBackendInfoList(start);
 }
 
+void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
+{
+    size_t len;
+    int i;
+
+    if (nb_args != 2) {
+        return;
+    }
+    len = strlen(str);
+    readline_set_completion_index(rs, len);
+    for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
+        add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
+    }
+}
+
 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
 {
     GSList *list, *elt;
Signature · 1 line
1.9.0
#14 lore raw

[Qemu-devel] [PULL 12/20] qapi: Replace uncommon use of the error API by the common one

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

We commonly use the error API like this:

    err = NULL;
    foo(..., &err);
    if (err) {
        goto out;
    }
    bar(..., &err);

Every error source is checked separately. The second function is only called when the first one succeeds. Both functions are free to pass their argument to error_set(). Because error_set() asserts no error has been set, this effectively means they must not be called with an error set.

The qapi-generated code uses the error API differently:

    // *errp was initialized to NULL somewhere up the call chain
    frob(..., errp);
    gnat(..., errp);

Errors accumulate in *errp: first error wins, subsequent errors get dropped. To make this work, the second function does nothing when called with an error set. Requires non-null errp, or else the second function can't see the first one fail.

This usage has also bled into visitor tests, and two device model object property getters rtc_get_date() and balloon_stats_get_all().

With the "accumulate" technique, you need fewer error checks in callers, and buy that with an error check in every callee. Can be nice.

However, mixing the two techniques is confusing. You can't use the "accumulate" technique with functions designed for the "check separately" technique. You can use the "check separately" technique with functions designed for the "accumulate" technique, but then error_set() can't catch you setting an error more than once.

Standardize on the "check separately" technique for now, because it's overwhelmingly prevalent.

---
 docs/qapi-code-gen.txt             |  87 ++++++++------
 hw/timer/mc146818rtc.c             |  24 +++-
 hw/virtio/virtio-balloon.c         |  12 +-
 qapi/qapi-visit-core.c             | 231 ++++++++++++++++---------------------
 scripts/qapi-commands.py           |  57 ++++++---
 scripts/qapi-visit.py              | 171 +++++++++++++--------------
 tests/test-qmp-input-strict.c      |  10 +-
 tests/test-qmp-input-visitor.c     |  26 +++--
 tests/test-qmp-output-visitor.c    |  10 +-
 tests/test-visitor-serialization.c |  12 +-
 10 files changed, 354 insertions(+), 286 deletions(-)
docs/qapi-code-gen.txt +50 -37
index 1a635e2..1948946 100644
@@ -275,7 +275,6 @@ Example:
         qapi_dealloc_visitor_cleanup(md);
     }
 
-
     void qapi_free_UserDefOne(UserDefOne * obj)
     {
         QapiDeallocVisitor *md;
@@ -352,49 +351,54 @@ Example:
     {
         Error *err = NULL;
         visit_type_int(m, &(*obj)->integer, "integer", &err);
+        if (err) {
+            goto out;
+        }
         visit_type_str(m, &(*obj)->string, "string", &err);
+        if (err) {
+            goto out;
+        }
 
+    out:
         error_propagate(errp, err);
     }
 
     void visit_type_UserDefOne(Visitor *m, UserDefOne ** obj, const char *name, Error **errp)
     {
-        if (!error_is_set(errp)) {
-            Error *err = NULL;
-            visit_start_struct(m, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), &err);
-            if (!err) {
-                if (*obj) {
-                    visit_type_UserDefOne_fields(m, obj, &err);
-                    error_propagate(errp, err);
-                    err = NULL;
-                }
-                /* Always call end_struct if start_struct succeeded.  */
-                visit_end_struct(m, &err);
+        Error *err = NULL;
+
+        visit_start_struct(m, (void **)obj, "UserDefOne", name, sizeof(UserDefOne), &err);
+        if (!err) {
+            if (*obj) {
+                visit_type_UserDefOne_fields(m, obj, errp);
             }
-            error_propagate(errp, err);
+            visit_end_struct(m, &err);
         }
+        error_propagate(errp, err);
     }
 
     void visit_type_UserDefOneList(Visitor *m, UserDefOneList ** obj, const char *name, Error **errp)
     {
-        GenericList *i, **prev = (GenericList **)obj;
         Error *err = NULL;
+        GenericList *i, **prev;
 
-        if (!error_is_set(errp)) {
-            visit_start_list(m, name, &err);
-            if (!err) {
-                for (; (i = visit_next_list(m, prev, &err)) != NULL; prev = &i) {
-                    UserDefOneList *native_i = (UserDefOneList *)i;
-                    visit_type_UserDefOne(m, &native_i->value, NULL, &err);
-                }
-                error_propagate(errp, err);
-                err = NULL;
-
-                /* Always call end_list if start_list succeeded.  */
-                visit_end_list(m, &err);
-            }
-            error_propagate(errp, err);
+        visit_start_list(m, name, &err);
+        if (err) {
+            goto out;
+        }
+
+        for (prev = (GenericList **)obj;
+             !err && (i = visit_next_list(m, prev, &err)) != NULL;
+             prev = &i) {
+            UserDefOneList *native_i = (UserDefOneList *)i;
+            visit_type_UserDefOne(m, &native_i->value, NULL, &err);
         }
+
+        error_propagate(errp, err);
+        err = NULL;
+        visit_end_list(m, &err);
+    out:
+        error_propagate(errp, err);
     }
     mdroth@illuin:~/w/qemu2.git$ cat qapi-generated/example-qapi-visit.h
 [Uninteresting stuff omitted...]
@@ -434,15 +438,20 @@ Example:
 
     static void qmp_marshal_output_my_command(UserDefOne * ret_in, QObject **ret_out, Error **errp)
     {
+        Error *local_err = NULL;
         QmpOutputVisitor *mo = qmp_output_visitor_new();
         QapiDeallocVisitor *md;
         Visitor *v;
 
         v = qmp_output_get_visitor(mo);
-        visit_type_UserDefOne(v, &ret_in, "unused", errp);
-        if (!error_is_set(errp)) {
-            *ret_out = qmp_output_get_qobject(mo);
+        visit_type_UserDefOne(v, &ret_in, "unused", &local_err);
+        if (local_err) {
+            goto out;
         }
+        *ret_out = qmp_output_get_qobject(mo);
+
+    out:
+        error_propagate(errp, local_err);
         qmp_output_visitor_cleanup(mo);
         md = qapi_dealloc_visitor_new();
         v = qapi_dealloc_get_visitor(md);
@@ -452,6 +461,7 @@ Example:
 
     static void qmp_marshal_input_my_command(QDict *args, QObject **ret, Error **errp)
     {
+        Error *local_err = NULL;
         UserDefOne * retval = NULL;
         QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
         QapiDeallocVisitor *md;
@@ -459,17 +469,20 @@ Example:
         UserDefOne * arg1 = NULL;
 
         v = qmp_input_get_visitor(mi);
-        visit_type_UserDefOne(v, &arg1, "arg1", errp);
-
-        if (error_is_set(errp)) {
+        visit_type_UserDefOne(v, &arg1, "arg1", &local_err);
+        if (local_err) {
             goto out;
         }
-        retval = qmp_my_command(arg1, errp);
-        if (!error_is_set(errp)) {
-            qmp_marshal_output_my_command(retval, ret, errp);
+
+        retval = qmp_my_command(arg1, &local_err);
+        if (local_err) {
+            goto out;
         }
 
+        qmp_marshal_output_my_command(retval, ret, &local_err);
+
     out:
+        error_propagate(errp, local_err);
         qmp_input_visitor_cleanup(mi);
         md = qapi_dealloc_visitor_new();
         v = qapi_dealloc_get_visitor(md);
hw/timer/mc146818rtc.c +22 -2
index 6c3e3b6..df54546 100644
@@ -804,13 +804,33 @@ static void rtc_get_date(Object *obj, Visitor *v, void *opaque,
         goto out;
     }
     visit_type_int32(v, &current_tm.tm_year, "tm_year", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_int32(v, &current_tm.tm_mon, "tm_mon", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_int32(v, &current_tm.tm_mday, "tm_mday", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_int32(v, &current_tm.tm_hour, "tm_hour", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_int32(v, &current_tm.tm_min, "tm_min", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_int32(v, &current_tm.tm_sec, "tm_sec", &err);
-    visit_end_struct(v, &err);
-
+    if (err) {
+        goto out_end;
+    }
+out_end:
+    error_propagate(errp, err);
+    err = NULL;
+    visit_end_struct(v, errp);
 out:
     error_propagate(errp, err);
 }
hw/virtio/virtio-balloon.c +8 -4
index ca99bd5..bf2b588 100644
@@ -121,23 +121,27 @@ static void balloon_stats_get_all(Object *obj, struct Visitor *v,
     if (err) {
         goto out;
     }
-
     visit_type_int(v, &s->stats_last_update, "last-update", &err);
+    if (err) {
+        goto out_end;
+    }
 
     visit_start_struct(v, NULL, NULL, "stats", 0, &err);
     if (err) {
         goto out_end;
     }
-        
-    for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
+    for (i = 0; !err && i < VIRTIO_BALLOON_S_NR; i++) {
         visit_type_int64(v, (int64_t *) &s->stats[i], balloon_stat_names[i],
                          &err);
     }
+    error_propagate(errp, err);
+    err = NULL;
     visit_end_struct(v, &err);
 
 out_end:
+    error_propagate(errp, err);
+    err = NULL;
     visit_end_struct(v, &err);
-
 out:
     error_propagate(errp, err);
 }
qapi/qapi-visit-core.c +101 -130

371 lines not on this page · raw message

scripts/qapi-commands.py +41 -16

161 lines not on this page · raw message

scripts/qapi-visit.py +87 -84

323 lines not on this page · raw message

tests/test-qmp-input-strict.c +9 -1
index ec798c2..0f77003 100644
@@ -81,11 +81,19 @@ static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
     }
 
     visit_type_int(v, &(*obj)->integer, "integer", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_str(v, &(*obj)->string, "string", &err);
 
+out_end:
+    error_propagate(errp, err);
+    err = NULL;
     visit_end_struct(v, &err);
-
 out:
     error_propagate(errp, err);
 }
tests/test-qmp-input-visitor.c +17 -9
index a58a3e6..1c8e872 100644
@@ -199,16 +199,24 @@ static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
 
     visit_start_struct(v, (void **)obj, "TestStruct", name, sizeof(TestStruct),
                        &err);
-    if (!err) {
-        visit_type_int(v, &(*obj)->integer, "integer", &err);
-        visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
-        visit_type_str(v, &(*obj)->string, "string", &err);
-
-        /* Always call end_struct if start_struct succeeded.  */
-        error_propagate(errp, err);
-        err = NULL;
-        visit_end_struct(v, &err);
+    if (err) {
+        goto out;
     }
+    visit_type_int(v, &(*obj)->integer, "integer", &err);
+    if (err) {
+        goto out_end;
+    }
+    visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
+    if (err) {
+        goto out_end;
+    }
+    visit_type_str(v, &(*obj)->string, "string", &err);
+
+out_end:
+    error_propagate(errp, err);
+    err = NULL;
+    visit_end_struct(v, &err);
+out:
     error_propagate(errp, err);
 }
 
tests/test-qmp-output-visitor.c +9 -1
index dfd597c..9c15458 100644
@@ -185,11 +185,19 @@ static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
     }
 
     visit_type_int(v, &(*obj)->integer, "integer", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_str(v, &(*obj)->string, "string", &err);
 
+out_end:
+    error_propagate(errp, err);
+    err = NULL;
     visit_end_struct(v, &err);
-
 out:
     error_propagate(errp, err);
 }
tests/test-visitor-serialization.c +10 -2
index 85170e5..74d6481 100644
@@ -195,7 +195,7 @@ typedef struct TestStruct
 static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
                                   const char *name, Error **errp)
 {
-    Error *err= NULL;
+    Error *err = NULL;
 
     visit_start_struct(v, (void **)obj, NULL, name, sizeof(TestStruct), &err);
     if (err) {
@@ -203,11 +203,19 @@ static void visit_type_TestStruct(Visitor *v, TestStruct **obj,
     }
 
     visit_type_int(v, &(*obj)->integer, "integer", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_bool(v, &(*obj)->boolean, "boolean", &err);
+    if (err) {
+        goto out_end;
+    }
     visit_type_str(v, &(*obj)->string, "string", &err);
 
+out_end:
+    error_propagate(errp, err);
+    err = NULL;
     visit_end_struct(v, &err);
-
 out:
     error_propagate(errp, err);
 }
Signature · 1 line
1.9.0
#15 lore raw

[Qemu-devel] [PULL 03/20] qapi: Remove unused Visitor callbacks start_handle(), end_handle()

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

These have never been called or implemented by anything, and their intended use is undocumented, like all of the visitor API.

---
 include/qapi/visitor-impl.h |  3 ---
 qapi/qapi-visit-core.c      | 15 ---------------
 2 files changed, 18 deletions(-)
include/qapi/visitor-impl.h +0 -3
index f3fa420..166aadd 100644
@@ -46,9 +46,6 @@ struct Visitor
                            Error **errp);
     void (*end_optional)(Visitor *v, Error **errp);
 
-    void (*start_handle)(Visitor *v, void **obj, const char *kind,
-                         const char *name, Error **errp);
-    void (*end_handle)(Visitor *v, Error **errp);
     void (*type_uint8)(Visitor *v, uint8_t *obj, const char *name, Error **errp);
     void (*type_uint16)(Visitor *v, uint16_t *obj, const char *name, Error **errp);
     void (*type_uint32)(Visitor *v, uint32_t *obj, const char *name, Error **errp);
qapi/qapi-visit-core.c +0 -15
index 6451a21..1f7475c 100644
@@ -17,21 +17,6 @@
 #include "qapi/visitor.h"
 #include "qapi/visitor-impl.h"
 
-void visit_start_handle(Visitor *v, void **obj, const char *kind,
-                        const char *name, Error **errp)
-{
-    if (!error_is_set(errp) && v->start_handle) {
-        v->start_handle(v, obj, kind, name, errp);
-    }
-}
-
-void visit_end_handle(Visitor *v, Error **errp)
-{
-    if (!error_is_set(errp) && v->end_handle) {
-        v->end_handle(v, errp);
-    }
-}
-
 void visit_start_struct(Visitor *v, void **obj, const char *kind,
                         const char *name, size_t size, Error **errp)
 {
Signature · 1 line
1.9.0
#16 lore raw

[Qemu-devel] [PULL 05/20] qapi-visit.py: Clean up confusing push_indent() / pop_indent() use

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

Changing implicit indentation in the middle of generating a block makes following the code being generated unnecessarily hard.

---
 scripts/qapi-visit.py | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)
scripts/qapi-visit.py +14 -18
index 9f22eee..28176ba 100644
@@ -128,12 +128,14 @@ if (!err) {
 ''',
         name=full_name)
 
+    ret += mcgen('''
+    /* Always call end_struct if start_struct succeeded.  */
+    visit_end_struct(m, &err);
+}
+error_propagate(errp, err);
+''')
     pop_indent()
     ret += mcgen('''
-        /* Always call end_struct if start_struct succeeded.  */
-        visit_end_struct(m, &err);
-    }
-    error_propagate(errp, err);
 }
 ''')
     return ret
@@ -289,19 +291,15 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **
 ''',
                  name=name)
 
-
-    push_indent()
     push_indent()
     push_indent()
 
     if base:
         ret += mcgen('''
-    visit_type_%(name)s_fields(m, obj, &err);
+        visit_type_%(name)s_fields(m, obj, &err);
 ''',
             name=name)
 
-    pop_indent()
-
     if not discriminator:
         disc_key = "type"
     else:
@@ -343,19 +341,17 @@ void visit_type_%(name)s(Visitor *m, %(name)s ** obj, const char *name, Error **
         }
         error_propagate(errp, err);
         err = NULL;
-    }
 ''')
     pop_indent()
-    ret += mcgen('''
-        /* Always call end_struct if start_struct succeeded.  */
-        visit_end_struct(m, &err);
-    }
-    error_propagate(errp, err);
-}
-''')
+    pop_indent()
 
-    pop_indent();
     ret += mcgen('''
+            }
+            /* Always call end_struct if start_struct succeeded.  */
+            visit_end_struct(m, &err);
+        }
+        error_propagate(errp, err);
+    }
 }
 ''')
 
Signature · 1 line
1.9.0
#17 lore raw

[Qemu-devel] [PULL 10/20] hw: Don't call visit_end_struct() after visit_start_struct() fails

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

When visit_start_struct() fails, visit_end_struct() must not be called. rtc_get_date() and balloon_stats_all() call it anyway. As far as I can tell, they're only used with the string output visitor, which doesn't care. Fix them anyway.

---
 hw/timer/mc146818rtc.c     | 23 +++++++++++++++--------
 hw/virtio/virtio-balloon.c | 25 +++++++++++++++++++------
 2 files changed, 34 insertions(+), 14 deletions(-)
hw/timer/mc146818rtc.c +15 -8
index 8509309..6c3e3b6 100644
@@ -793,19 +793,26 @@ static const MemoryRegionOps cmos_ops = {
 static void rtc_get_date(Object *obj, Visitor *v, void *opaque,
                          const char *name, Error **errp)
 {
+    Error *err = NULL;
     RTCState *s = MC146818_RTC(obj);
     struct tm current_tm;
 
     rtc_update_time(s);
     rtc_get_time(s, &current_tm);
-    visit_start_struct(v, NULL, "struct tm", name, 0, errp);
-    visit_type_int32(v, &current_tm.tm_year, "tm_year", errp);
-    visit_type_int32(v, &current_tm.tm_mon, "tm_mon", errp);
-    visit_type_int32(v, &current_tm.tm_mday, "tm_mday", errp);
-    visit_type_int32(v, &current_tm.tm_hour, "tm_hour", errp);
-    visit_type_int32(v, &current_tm.tm_min, "tm_min", errp);
-    visit_type_int32(v, &current_tm.tm_sec, "tm_sec", errp);
-    visit_end_struct(v, errp);
+    visit_start_struct(v, NULL, "struct tm", name, 0, &err);
+    if (err) {
+        goto out;
+    }
+    visit_type_int32(v, &current_tm.tm_year, "tm_year", &err);
+    visit_type_int32(v, &current_tm.tm_mon, "tm_mon", &err);
+    visit_type_int32(v, &current_tm.tm_mday, "tm_mday", &err);
+    visit_type_int32(v, &current_tm.tm_hour, "tm_hour", &err);
+    visit_type_int32(v, &current_tm.tm_min, "tm_min", &err);
+    visit_type_int32(v, &current_tm.tm_sec, "tm_sec", &err);
+    visit_end_struct(v, &err);
+
+out:
+    error_propagate(errp, err);
 }
 
 static void rtc_realizefn(DeviceState *dev, Error **errp)
hw/virtio/virtio-balloon.c +19 -6
index 971a921..ca99bd5 100644
@@ -108,6 +108,7 @@ static void balloon_stats_poll_cb(void *opaque)
 static void balloon_stats_get_all(Object *obj, struct Visitor *v,
                                   void *opaque, const char *name, Error **errp)
 {
+    Error *err = NULL;
     VirtIOBalloon *s = opaque;
     int i;
 
@@ -116,17 +117,29 @@ static void balloon_stats_get_all(Object *obj, struct Visitor *v,
         return;
     }
 
-    visit_start_struct(v, NULL, "guest-stats", name, 0, errp);
-    visit_type_int(v, &s->stats_last_update, "last-update", errp);
+    visit_start_struct(v, NULL, "guest-stats", name, 0, &err);
+    if (err) {
+        goto out;
+    }
+
+    visit_type_int(v, &s->stats_last_update, "last-update", &err);
 
-    visit_start_struct(v, NULL, NULL, "stats", 0, errp);
+    visit_start_struct(v, NULL, NULL, "stats", 0, &err);
+    if (err) {
+        goto out_end;
+    }
+        
     for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
         visit_type_int64(v, (int64_t *) &s->stats[i], balloon_stat_names[i],
-                         errp);
+                         &err);
     }
-    visit_end_struct(v, errp);
+    visit_end_struct(v, &err);
+
+out_end:
+    visit_end_struct(v, &err);
 
-    visit_end_struct(v, errp);
+out:
+    error_propagate(errp, err);
 }
 
 static void balloon_stats_get_poll_interval(Object *obj, struct Visitor *v,
Signature · 1 line
1.9.0
#18 lore raw

[Qemu-devel] [PULL 04/20] qapi: Replace start_optional()/end_optional() by optional()

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

Semantics of end_optional() differ subtly from the other end_FOO()
callbacks: when start_FOO() succeeds, the matching end_FOO() gets called regardless of what happens in between. end_optional() gets called only when everything in between succeeds as well. Entirely undocumented, like all of the visitor API.

The only user of Visitor Callback end_optional() never did anything, and was removed in commit 9f9ab46.

I'm about to clean up error handling in the generated visitor code, and end_optional() is in my way. No users mean no test cases, and making non-trivial cleanup transformations without test cases doesn't strike me as a good idea.

Drop end_optional(), and rename start_optional() to optional(). We can always go back to a pair of callbacks when we have an actual need.

---
 include/qapi/visitor-impl.h |  5 ++---
 include/qapi/visitor.h      |  5 ++---
 qapi/opts-visitor.c         |  5 ++---
 qapi/qapi-visit-core.c      | 15 ++++-----------
 qapi/qmp-input-visitor.c    |  6 +++---
 qapi/string-input-visitor.c |  6 +++---
 scripts/qapi-commands.py    |  5 ++---
 scripts/qapi-visit.py       |  3 +--
 8 files changed, 19 insertions(+), 31 deletions(-)
include/qapi/visitor-impl.h +2 -3
index 166aadd..ecc0183 100644
@@ -42,9 +42,8 @@ struct Visitor
                         Error **errp);
 
     /* May be NULL */
-    void (*start_optional)(Visitor *v, bool *present, const char *name,
-                           Error **errp);
-    void (*end_optional)(Visitor *v, Error **errp);
+    void (*optional)(Visitor *v, bool *present, const char *name,
+                     Error **errp);
 
     void (*type_uint8)(Visitor *v, uint8_t *obj, const char *name, Error **errp);
     void (*type_uint16)(Visitor *v, uint16_t *obj, const char *name, Error **errp);
include/qapi/visitor.h +2 -3
index 29da211..4a0178f 100644
@@ -39,9 +39,8 @@ void visit_end_implicit_struct(Visitor *v, Error **errp);
 void visit_start_list(Visitor *v, const char *name, Error **errp);
 GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp);
 void visit_end_list(Visitor *v, Error **errp);
-void visit_start_optional(Visitor *v, bool *present, const char *name,
-                          Error **errp);
-void visit_end_optional(Visitor *v, Error **errp);
+void visit_optional(Visitor *v, bool *present, const char *name,
+                    Error **errp);
 void visit_get_next_type(Visitor *v, int *obj, const int *qtypes,
                          const char *name, Error **errp);
 void visit_type_enum(Visitor *v, int *obj, const char *strings[],
qapi/opts-visitor.c +2 -3
index 87c1c78..16382e7 100644
@@ -484,8 +484,7 @@ opts_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
 
 
 static void
-opts_start_optional(Visitor *v, bool *present, const char *name,
-                       Error **errp)
+opts_optional(Visitor *v, bool *present, const char *name, Error **errp)
 {
     OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
 
@@ -528,7 +527,7 @@ opts_visitor_new(const QemuOpts *opts)
     /* type_number() is not filled in, but this is not the first visitor to
      * skip some mandatory methods... */
 
-    ov->visitor.start_optional = &opts_start_optional;
+    ov->visitor.optional = &opts_optional;
 
     ov->opts_root = opts;
 
qapi/qapi-visit-core.c +4 -11
index 1f7475c..ffd7637 100644
@@ -69,18 +69,11 @@ void visit_end_list(Visitor *v, Error **errp)
     v->end_list(v, errp);
 }
 
-void visit_start_optional(Visitor *v, bool *present, const char *name,
-                          Error **errp)
+void visit_optional(Visitor *v, bool *present, const char *name,
+                    Error **errp)
 {
-    if (!error_is_set(errp) && v->start_optional) {
-        v->start_optional(v, present, name, errp);
-    }
-}
-
-void visit_end_optional(Visitor *v, Error **errp)
-{
-    if (!error_is_set(errp) && v->end_optional) {
-        v->end_optional(v, errp);
+    if (!error_is_set(errp) && v->optional) {
+        v->optional(v, present, name, errp);
     }
 }
 
qapi/qmp-input-visitor.c +3 -3
index a2bed1e..d861206 100644
@@ -286,8 +286,8 @@ static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
     }
 }
 
-static void qmp_input_start_optional(Visitor *v, bool *present,
-                                     const char *name, Error **errp)
+static void qmp_input_optional(Visitor *v, bool *present, const char *name,
+                               Error **errp)
 {
     QmpInputVisitor *qiv = to_qiv(v);
     QObject *qobj = qmp_input_get_object(qiv, name, true);
@@ -329,7 +329,7 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
     v->visitor.type_bool = qmp_input_type_bool;
     v->visitor.type_str = qmp_input_type_str;
     v->visitor.type_number = qmp_input_type_number;
-    v->visitor.start_optional = qmp_input_start_optional;
+    v->visitor.optional = qmp_input_optional;
     v->visitor.get_next_type = qmp_input_get_next_type;
 
     qmp_input_push(v, obj, NULL);
qapi/string-input-visitor.c +3 -3
index 793548a..5780944 100644
@@ -120,8 +120,8 @@ static void parse_type_number(Visitor *v, double *obj, const char *name,
     *obj = val;
 }
 
-static void parse_start_optional(Visitor *v, bool *present,
-                                 const char *name, Error **errp)
+static void parse_optional(Visitor *v, bool *present, const char *name,
+                           Error **errp)
 {
     StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
 
@@ -155,7 +155,7 @@ StringInputVisitor *string_input_visitor_new(const char *str)
     v->visitor.type_bool = parse_type_bool;
     v->visitor.type_str = parse_type_str;
     v->visitor.type_number = parse_type_number;
-    v->visitor.start_optional = parse_start_optional;
+    v->visitor.optional = parse_optional;
 
     v->string = str;
     return v;
scripts/qapi-commands.py +2 -3
index 1399826..341dba2 100644
@@ -131,7 +131,7 @@ v = qmp_input_get_visitor(mi);
     for argname, argtype, optional, structured in parse_args(args):
         if optional:
             ret += mcgen('''
-visit_start_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
+visit_optional(v, &has_%(c_name)s, "%(name)s", %(errp)s);
 if (has_%(c_name)s) {
 ''',
                          c_name=c_var(argname), name=argname, errp=errparg)
@@ -145,8 +145,7 @@ if (has_%(c_name)s) {
             pop_indent()
             ret += mcgen('''
 }
-visit_end_optional(v, %(errp)s);
-''', errp=errparg)
+''')
 
     if dealloc:
         ret += mcgen('''
scripts/qapi-visit.py +1 -2
index c6579be..9f22eee 100644
@@ -61,7 +61,7 @@ if (!err) {
     for argname, argentry, optional, structured in parse_args(members):
         if optional:
             ret += mcgen('''
-visit_start_optional(m, &(*obj)->%(c_prefix)shas_%(c_name)s, "%(name)s", &err);
+visit_optional(m, &(*obj)->%(c_prefix)shas_%(c_name)s, "%(name)s", &err);
 if ((*obj)->%(prefix)shas_%(c_name)s) {
 ''',
                          c_prefix=c_var(field_prefix), prefix=field_prefix,
@@ -82,7 +82,6 @@ visit_type_%(type)s(m, &(*obj)->%(c_prefix)s%(c_name)s, "%(name)s", &err);
             pop_indent()
             ret += mcgen('''
 }
-visit_end_optional(m, &err);
 ''')
 
     pop_indent()
Signature · 1 line
1.9.0
#19 lore raw

[Qemu-devel] [PULL 07/20] qapi-visit.py: Clean up a sloppy use of field prefix

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

generate_visit_struct_fields() generates the base type's struct member name both with and without the field prefix. Harmless, because the field prefix is always empty there: only unboxed complex members have a prefix, and those can't have a base type.

Clean it up anyway.

---
 scripts/qapi-visit.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/qapi-visit.py +1 -1
index 57be9bf..1e368be 100644
@@ -60,7 +60,7 @@ static void visit_type_%(full_name)s_fields(Visitor *m, %(name)s ** obj, Error *
 
     if base:
         ret += mcgen('''
-visit_start_implicit_struct(m, (void**) &(*obj)->%(c_name)s, sizeof(%(type)s), &err);
+visit_start_implicit_struct(m, (void**) &(*obj)->%(c_prefix)s%(c_name)s, sizeof(%(type)s), &err);
 if (!err) {
     visit_type_%(type)s_fields(m, &(*obj)->%(c_prefix)s%(c_name)s, &err);
     error_propagate(errp, err);
Signature · 1 line
1.9.0
#20 lore raw

[Qemu-devel] [PULL 02/20] qapi: Normalize marshalling's visitor initialization and cleanup

In reply to Luiz Capitulino

From: Markus Armbruster <[email protected]>

Input and output marshalling functions do it differently. Change them to work the same: initialize the I/O visitor, use it, clean it up, initialize the dealloc visitor, use it, clean it up.

This delays dealloc visitor initialization in output marshalling functions, and input visitor cleanup in input marshalling functions. No functional change, but the latter will be convenient when I change the error handling.

---
 docs/qapi-code-gen.txt   |  8 ++++----
 scripts/qapi-commands.py | 27 ++++++++++++---------------
 2 files changed, 16 insertions(+), 19 deletions(-)
docs/qapi-code-gen.txt +4 -4
index b915c4d..1a635e2 100644
@@ -434,8 +434,8 @@ Example:
 
     static void qmp_marshal_output_my_command(UserDefOne * ret_in, QObject **ret_out, Error **errp)
     {
-        QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
         QmpOutputVisitor *mo = qmp_output_visitor_new();
+        QapiDeallocVisitor *md;
         Visitor *v;
 
         v = qmp_output_get_visitor(mo);
@@ -444,6 +444,7 @@ Example:
             *ret_out = qmp_output_get_qobject(mo);
         }
         qmp_output_visitor_cleanup(mo);
+        md = qapi_dealloc_visitor_new();
         v = qapi_dealloc_get_visitor(md);
         visit_type_UserDefOne(v, &ret_in, "unused", NULL);
         qapi_dealloc_visitor_cleanup(md);
@@ -452,15 +453,13 @@ Example:
     static void qmp_marshal_input_my_command(QDict *args, QObject **ret, Error **errp)
     {
         UserDefOne * retval = NULL;
-        QmpInputVisitor *mi;
+        QmpInputVisitor *mi = qmp_input_visitor_new_strict(QOBJECT(args));
         QapiDeallocVisitor *md;
         Visitor *v;
         UserDefOne * arg1 = NULL;
 
-        mi = qmp_input_visitor_new_strict(QOBJECT(args));
         v = qmp_input_get_visitor(mi);
         visit_type_UserDefOne(v, &arg1, "arg1", errp);
-        qmp_input_visitor_cleanup(mi);
 
         if (error_is_set(errp)) {
             goto out;
@@ -471,6 +470,7 @@ Example:
         }
 
     out:
+        qmp_input_visitor_cleanup(mi);
         md = qapi_dealloc_visitor_new();
         v = qapi_dealloc_get_visitor(md);
         visit_type_UserDefOne(v, &arg1, "arg1", NULL);
scripts/qapi-commands.py +12 -15
index 8d9096f..1399826 100644
@@ -69,16 +69,17 @@ def gen_marshal_output_call(name, ret_type):
         return ""
     return "qmp_marshal_output_%s(retval, ret, errp);" % c_fun(name)
 
-def gen_visitor_input_containers_decl(args):
+def gen_visitor_input_containers_decl(args, obj):
     ret = ""
 
     push_indent()
     if len(args) > 0:
         ret += mcgen('''
-QmpInputVisitor *mi;
+QmpInputVisitor *mi = qmp_input_visitor_new_strict(%(obj)s);
 QapiDeallocVisitor *md;
 Visitor *v;
-''')
+''',
+                     obj=obj)
     pop_indent()
 
     return ret.rstrip()
@@ -106,7 +107,7 @@ bool has_%(argname)s = false;
     pop_indent()
     return ret.rstrip()
 
-def gen_visitor_input_block(args, obj, dealloc=False):
+def gen_visitor_input_block(args, dealloc=False):
     ret = ""
     errparg = 'errp'
 
@@ -118,15 +119,14 @@ def gen_visitor_input_block(args, obj, dealloc=False):
     if dealloc:
         errparg = 'NULL'
         ret += mcgen('''
+qmp_input_visitor_cleanup(mi);
 md = qapi_dealloc_visitor_new();
 v = qapi_dealloc_get_visitor(md);
 ''')
     else:
         ret += mcgen('''
-mi = qmp_input_visitor_new_strict(%(obj)s);
 v = qmp_input_get_visitor(mi);
-''',
-                     obj=obj)
+''')
 
     for argname, argtype, optional, structured in parse_args(args):
         if optional:
@@ -152,10 +152,6 @@ visit_end_optional(v, %(errp)s);
         ret += mcgen('''
 qapi_dealloc_visitor_cleanup(md);
 ''')
-    else:
-        ret += mcgen('''
-qmp_input_visitor_cleanup(mi);
-''')
     pop_indent()
     return ret.rstrip()
 
@@ -166,8 +162,8 @@ def gen_marshal_output(name, args, ret_type, middle_mode):
     ret = mcgen('''
 static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_out, Error **errp)
 {
-    QapiDeallocVisitor *md = qapi_dealloc_visitor_new();
     QmpOutputVisitor *mo = qmp_output_visitor_new();
+    QapiDeallocVisitor *md;
     Visitor *v;
 
     v = qmp_output_get_visitor(mo);
@@ -176,6 +172,7 @@ static void qmp_marshal_output_%(c_name)s(%(c_ret_type)s ret_in, QObject **ret_o
         *ret_out = qmp_output_get_qobject(mo);
     }
     qmp_output_visitor_cleanup(mo);
+    md = qapi_dealloc_visitor_new();
     v = qapi_dealloc_get_visitor(md);
     %(visitor)s(v, &ret_in, "unused", NULL);
     qapi_dealloc_visitor_cleanup(md);
@@ -228,9 +225,9 @@ def gen_marshal_input(name, args, ret_type, middle_mode):
 %(visitor_input_block)s
 
 ''',
-                     visitor_input_containers_decl=gen_visitor_input_containers_decl(args),
+                     visitor_input_containers_decl=gen_visitor_input_containers_decl(args, "QOBJECT(args)"),
                      visitor_input_vars_decl=gen_visitor_input_vars_decl(args),
-                     visitor_input_block=gen_visitor_input_block(args, "QOBJECT(args)"))
+                     visitor_input_block=gen_visitor_input_block(args))
     else:
         ret += mcgen('''
     (void)args;
@@ -250,7 +247,7 @@ out:
     ret += mcgen('''
 %(visitor_input_block_cleanup)s
 ''',
-                 visitor_input_block_cleanup=gen_visitor_input_block(args, None,
+                 visitor_input_block_cleanup=gen_visitor_input_block(args,
                                                                      dealloc=True))
 
     if middle_mode:
Signature · 1 line
1.9.0
#21 lore raw
In reply to Luiz Capitulino
Quoted 4 lines · Luiz Capitulino

The following changes since commit 16a9189921bdb9e351f2f9baec16b376f8d71cdf:

Merge remote-tracking branch 'remotes/kraxel/tags/pull-roms-2' into staging (2014-05-15 18:32:16 +0100)

Quoted 9 lines

are available in the git repository at:

git://repo.or.cz/qemu/qmp-unstable.git queue/qmp

for you to fetch changes up to 24fd848950b44de7e2d71fb69ba52b90d6acb220:

qapi: skip redundant includes (2014-05-16 10:35:59 -0400)

Applied, thanks.

-- PMM